[LIBXML2]
[reactos.git] / reactos / sdk / lib / 3rdparty / libxml2 / relaxng.c
1 /*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
9 /**
10 * TODO:
11 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
13 * - report better mem allocations pbms at runtime and abort immediately.
14 */
15
16 #define IN_LIBXML
17 #include "libxml.h"
18
19 #ifdef LIBXML_SCHEMAS_ENABLED
20
21 #include <string.h>
22 #include <stdio.h>
23 #include <libxml/xmlmemory.h>
24 #include <libxml/parser.h>
25 #include <libxml/parserInternals.h>
26 #include <libxml/hash.h>
27 #include <libxml/uri.h>
28
29 #include <libxml/relaxng.h>
30
31 #include <libxml/xmlschemastypes.h>
32 #include <libxml/xmlautomata.h>
33 #include <libxml/xmlregexp.h>
34 #include <libxml/xmlschemastypes.h>
35
36 /*
37 * The Relax-NG namespace
38 */
39 static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
42 #define IS_RELAXNG(node, typ) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (node->type == XML_ELEMENT_NODE) && \
45 (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
46 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
47
48
49 #if 0
50 #define DEBUG 1
51
52 #define DEBUG_GRAMMAR 1
53
54 #define DEBUG_CONTENT 1
55
56 #define DEBUG_TYPE 1
57
58 #define DEBUG_VALID 1
59
60 #define DEBUG_INTERLEAVE 1
61
62 #define DEBUG_LIST 1
63
64 #define DEBUG_INCLUDE 1
65
66 #define DEBUG_ERROR 1
67
68 #define DEBUG_COMPILE 1
69
70 #define DEBUG_PROGRESSIVE 1
71 #endif
72
73 #define MAX_ERROR 5
74
75 #define TODO \
76 xmlGenericError(xmlGenericErrorContext, \
77 "Unimplemented block at %s:%d\n", \
78 __FILE__, __LINE__);
79
80 typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
81 typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
82
83 typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
84 typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
85
86 typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
87 typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
88
89 typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
90 typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
91
92 typedef enum {
93 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
94 XML_RELAXNG_COMBINE_CHOICE, /* choice */
95 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
96 } xmlRelaxNGCombine;
97
98 typedef enum {
99 XML_RELAXNG_CONTENT_ERROR = -1,
100 XML_RELAXNG_CONTENT_EMPTY = 0,
101 XML_RELAXNG_CONTENT_SIMPLE,
102 XML_RELAXNG_CONTENT_COMPLEX
103 } xmlRelaxNGContentType;
104
105 typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
106 typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
107
108 struct _xmlRelaxNGGrammar {
109 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
110 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
111 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
112 xmlRelaxNGDefinePtr start; /* <start> content */
113 xmlRelaxNGCombine combine; /* the default combine value */
114 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
115 xmlHashTablePtr defs; /* define* */
116 xmlHashTablePtr refs; /* references */
117 };
118
119
120 typedef enum {
121 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
122 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
123 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
124 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
125 XML_RELAXNG_TEXT, /* textual content */
126 XML_RELAXNG_ELEMENT, /* an element */
127 XML_RELAXNG_DATATYPE, /* extenal data type definition */
128 XML_RELAXNG_PARAM, /* extenal data type parameter */
129 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
130 XML_RELAXNG_LIST, /* a list of patterns */
131 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
132 XML_RELAXNG_DEF, /* a definition */
133 XML_RELAXNG_REF, /* reference to a definition */
134 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
135 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
136 XML_RELAXNG_OPTIONAL, /* optional patterns */
137 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
138 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
139 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
140 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
141 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
142 XML_RELAXNG_START /* Used to keep track of starts on grammars */
143 } xmlRelaxNGType;
144
145 #define IS_NULLABLE (1 << 0)
146 #define IS_NOT_NULLABLE (1 << 1)
147 #define IS_INDETERMINIST (1 << 2)
148 #define IS_MIXED (1 << 3)
149 #define IS_TRIABLE (1 << 4)
150 #define IS_PROCESSED (1 << 5)
151 #define IS_COMPILABLE (1 << 6)
152 #define IS_NOT_COMPILABLE (1 << 7)
153 #define IS_EXTERNAL_REF (1 << 8)
154
155 struct _xmlRelaxNGDefine {
156 xmlRelaxNGType type; /* the type of definition */
157 xmlNodePtr node; /* the node in the source */
158 xmlChar *name; /* the element local name if present */
159 xmlChar *ns; /* the namespace local name if present */
160 xmlChar *value; /* value when available */
161 void *data; /* data lib or specific pointer */
162 xmlRelaxNGDefinePtr content; /* the expected content */
163 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
164 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
165 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
166 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
167 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
168 short depth; /* used for the cycle detection */
169 short dflags; /* define related flags */
170 xmlRegexpPtr contModel; /* a compiled content model if available */
171 };
172
173 /**
174 * _xmlRelaxNG:
175 *
176 * A RelaxNGs definition
177 */
178 struct _xmlRelaxNG {
179 void *_private; /* unused by the library for users or bindings */
180 xmlRelaxNGGrammarPtr topgrammar;
181 xmlDocPtr doc;
182
183 int idref; /* requires idref checking */
184
185 xmlHashTablePtr defs; /* define */
186 xmlHashTablePtr refs; /* references */
187 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
188 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
189 int defNr; /* number of defines used */
190 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
191
192 };
193
194 #define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
195 #define XML_RELAXNG_IN_ONEORMORE (1 << 1)
196 #define XML_RELAXNG_IN_LIST (1 << 2)
197 #define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
198 #define XML_RELAXNG_IN_START (1 << 4)
199 #define XML_RELAXNG_IN_OOMGROUP (1 << 5)
200 #define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
201 #define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
202 #define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
203 #define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
204
205 struct _xmlRelaxNGParserCtxt {
206 void *userData; /* user specific data block */
207 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
208 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
209 xmlStructuredErrorFunc serror;
210 xmlRelaxNGValidErr err;
211
212 xmlRelaxNGPtr schema; /* The schema in use */
213 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
214 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
215 int flags; /* parser flags */
216 int nbErrors; /* number of errors at parse time */
217 int nbWarnings; /* number of warnings at parse time */
218 const xmlChar *define; /* the current define scope */
219 xmlRelaxNGDefinePtr def; /* the current define */
220
221 int nbInterleaves;
222 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
223
224 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
225 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
226 xmlChar *URL;
227 xmlDocPtr document;
228
229 int defNr; /* number of defines used */
230 int defMax; /* number of defines aloocated */
231 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
232
233 const char *buffer;
234 int size;
235
236 /* the document stack */
237 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
238 int docNr; /* Depth of the parsing stack */
239 int docMax; /* Max depth of the parsing stack */
240 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
241
242 /* the include stack */
243 xmlRelaxNGIncludePtr inc; /* Current parsed include */
244 int incNr; /* Depth of the include parsing stack */
245 int incMax; /* Max depth of the parsing stack */
246 xmlRelaxNGIncludePtr *incTab; /* array of incs */
247
248 int idref; /* requires idref checking */
249
250 /* used to compile content models */
251 xmlAutomataPtr am; /* the automata */
252 xmlAutomataStatePtr state; /* used to build the automata */
253
254 int crng; /* compact syntax and other flags */
255 int freedoc; /* need to free the document */
256 };
257
258 #define FLAGS_IGNORABLE 1
259 #define FLAGS_NEGATIVE 2
260 #define FLAGS_MIXED_CONTENT 4
261 #define FLAGS_NOERROR 8
262
263 /**
264 * xmlRelaxNGInterleaveGroup:
265 *
266 * A RelaxNGs partition set associated to lists of definitions
267 */
268 typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
269 typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
270 struct _xmlRelaxNGInterleaveGroup {
271 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
272 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
273 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
274 };
275
276 #define IS_DETERMINIST 1
277 #define IS_NEEDCHECK 2
278
279 /**
280 * xmlRelaxNGPartitions:
281 *
282 * A RelaxNGs partition associated to an interleave group
283 */
284 typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
285 typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
286 struct _xmlRelaxNGPartition {
287 int nbgroups; /* number of groups in the partitions */
288 xmlHashTablePtr triage; /* hash table used to direct nodes to the
289 * right group when possible */
290 int flags; /* determinist ? */
291 xmlRelaxNGInterleaveGroupPtr *groups;
292 };
293
294 /**
295 * xmlRelaxNGValidState:
296 *
297 * A RelaxNGs validation state
298 */
299 #define MAX_ATTR 20
300 typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
301 typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
302 struct _xmlRelaxNGValidState {
303 xmlNodePtr node; /* the current node */
304 xmlNodePtr seq; /* the sequence of children left to validate */
305 int nbAttrs; /* the number of attributes */
306 int maxAttrs; /* the size of attrs */
307 int nbAttrLeft; /* the number of attributes left to validate */
308 xmlChar *value; /* the value when operating on string */
309 xmlChar *endvalue; /* the end value when operating on string */
310 xmlAttrPtr *attrs; /* the array of attributes */
311 };
312
313 /**
314 * xmlRelaxNGStates:
315 *
316 * A RelaxNGs container for validation state
317 */
318 typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
319 typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
320 struct _xmlRelaxNGStates {
321 int nbState; /* the number of states */
322 int maxState; /* the size of the array */
323 xmlRelaxNGValidStatePtr *tabState;
324 };
325
326 #define ERROR_IS_DUP 1
327
328 /**
329 * xmlRelaxNGValidError:
330 *
331 * A RelaxNGs validation error
332 */
333 typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
334 typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
335 struct _xmlRelaxNGValidError {
336 xmlRelaxNGValidErr err; /* the error number */
337 int flags; /* flags */
338 xmlNodePtr node; /* the current node */
339 xmlNodePtr seq; /* the current child */
340 const xmlChar *arg1; /* first arg */
341 const xmlChar *arg2; /* second arg */
342 };
343
344 /**
345 * xmlRelaxNGValidCtxt:
346 *
347 * A RelaxNGs validation context
348 */
349
350 struct _xmlRelaxNGValidCtxt {
351 void *userData; /* user specific data block */
352 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
353 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
354 xmlStructuredErrorFunc serror;
355 int nbErrors; /* number of errors in validation */
356
357 xmlRelaxNGPtr schema; /* The schema in use */
358 xmlDocPtr doc; /* the document being validated */
359 int flags; /* validation flags */
360 int depth; /* validation depth */
361 int idref; /* requires idref checking */
362 int errNo; /* the first error found */
363
364 /*
365 * Errors accumulated in branches may have to be stacked to be
366 * provided back when it's sure they affect validation.
367 */
368 xmlRelaxNGValidErrorPtr err; /* Last error */
369 int errNr; /* Depth of the error stack */
370 int errMax; /* Max depth of the error stack */
371 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
372
373 xmlRelaxNGValidStatePtr state; /* the current validation state */
374 xmlRelaxNGStatesPtr states; /* the accumulated state list */
375
376 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
377 int freeStatesNr;
378 int freeStatesMax;
379 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
380
381 /*
382 * This is used for "progressive" validation
383 */
384 xmlRegExecCtxtPtr elem; /* the current element regexp */
385 int elemNr; /* the number of element validated */
386 int elemMax; /* the max depth of elements */
387 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
388 int pstate; /* progressive state */
389 xmlNodePtr pnode; /* the current node */
390 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
391 int perr; /* signal error in content model
392 * outside the regexp */
393 };
394
395 /**
396 * xmlRelaxNGInclude:
397 *
398 * Structure associated to a RelaxNGs document element
399 */
400 struct _xmlRelaxNGInclude {
401 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
402 xmlChar *href; /* the normalized href value */
403 xmlDocPtr doc; /* the associated XML document */
404 xmlRelaxNGDefinePtr content; /* the definitions */
405 xmlRelaxNGPtr schema; /* the schema */
406 };
407
408 /**
409 * xmlRelaxNGDocument:
410 *
411 * Structure associated to a RelaxNGs document element
412 */
413 struct _xmlRelaxNGDocument {
414 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
415 xmlChar *href; /* the normalized href value */
416 xmlDocPtr doc; /* the associated XML document */
417 xmlRelaxNGDefinePtr content; /* the definitions */
418 xmlRelaxNGPtr schema; /* the schema */
419 int externalRef; /* 1 if an external ref */
420 };
421
422
423 /************************************************************************
424 * *
425 * Some factorized error routines *
426 * *
427 ************************************************************************/
428
429 /**
430 * xmlRngPErrMemory:
431 * @ctxt: an Relax-NG parser context
432 * @extra: extra informations
433 *
434 * Handle a redefinition of attribute error
435 */
436 static void
437 xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
438 {
439 xmlStructuredErrorFunc schannel = NULL;
440 xmlGenericErrorFunc channel = NULL;
441 void *data = NULL;
442
443 if (ctxt != NULL) {
444 if (ctxt->serror != NULL)
445 schannel = ctxt->serror;
446 else
447 channel = ctxt->error;
448 data = ctxt->userData;
449 ctxt->nbErrors++;
450 }
451 if (extra)
452 __xmlRaiseError(schannel, channel, data,
453 NULL, NULL, XML_FROM_RELAXNGP,
454 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
455 NULL, NULL, 0, 0,
456 "Memory allocation failed : %s\n", extra);
457 else
458 __xmlRaiseError(schannel, channel, data,
459 NULL, NULL, XML_FROM_RELAXNGP,
460 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
461 NULL, NULL, 0, 0, "Memory allocation failed\n");
462 }
463
464 /**
465 * xmlRngVErrMemory:
466 * @ctxt: a Relax-NG validation context
467 * @extra: extra informations
468 *
469 * Handle a redefinition of attribute error
470 */
471 static void
472 xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
473 {
474 xmlStructuredErrorFunc schannel = NULL;
475 xmlGenericErrorFunc channel = NULL;
476 void *data = NULL;
477
478 if (ctxt != NULL) {
479 if (ctxt->serror != NULL)
480 schannel = ctxt->serror;
481 else
482 channel = ctxt->error;
483 data = ctxt->userData;
484 ctxt->nbErrors++;
485 }
486 if (extra)
487 __xmlRaiseError(schannel, channel, data,
488 NULL, NULL, XML_FROM_RELAXNGV,
489 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
490 NULL, NULL, 0, 0,
491 "Memory allocation failed : %s\n", extra);
492 else
493 __xmlRaiseError(schannel, channel, data,
494 NULL, NULL, XML_FROM_RELAXNGV,
495 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
496 NULL, NULL, 0, 0, "Memory allocation failed\n");
497 }
498
499 /**
500 * xmlRngPErr:
501 * @ctxt: a Relax-NG parser context
502 * @node: the node raising the error
503 * @error: the error code
504 * @msg: message
505 * @str1: extra info
506 * @str2: extra info
507 *
508 * Handle a Relax NG Parsing error
509 */
510 static void LIBXML_ATTR_FORMAT(4,0)
511 xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
512 const char *msg, const xmlChar * str1, const xmlChar * str2)
513 {
514 xmlStructuredErrorFunc schannel = NULL;
515 xmlGenericErrorFunc channel = NULL;
516 void *data = NULL;
517
518 if (ctxt != NULL) {
519 if (ctxt->serror != NULL)
520 schannel = ctxt->serror;
521 else
522 channel = ctxt->error;
523 data = ctxt->userData;
524 ctxt->nbErrors++;
525 }
526 __xmlRaiseError(schannel, channel, data,
527 NULL, node, XML_FROM_RELAXNGP,
528 error, XML_ERR_ERROR, NULL, 0,
529 (const char *) str1, (const char *) str2, NULL, 0, 0,
530 msg, str1, str2);
531 }
532
533 /**
534 * xmlRngVErr:
535 * @ctxt: a Relax-NG validation context
536 * @node: the node raising the error
537 * @error: the error code
538 * @msg: message
539 * @str1: extra info
540 * @str2: extra info
541 *
542 * Handle a Relax NG Validation error
543 */
544 static void LIBXML_ATTR_FORMAT(4,0)
545 xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
546 const char *msg, const xmlChar * str1, const xmlChar * str2)
547 {
548 xmlStructuredErrorFunc schannel = NULL;
549 xmlGenericErrorFunc channel = NULL;
550 void *data = NULL;
551
552 if (ctxt != NULL) {
553 if (ctxt->serror != NULL)
554 schannel = ctxt->serror;
555 else
556 channel = ctxt->error;
557 data = ctxt->userData;
558 ctxt->nbErrors++;
559 }
560 __xmlRaiseError(schannel, channel, data,
561 NULL, node, XML_FROM_RELAXNGV,
562 error, XML_ERR_ERROR, NULL, 0,
563 (const char *) str1, (const char *) str2, NULL, 0, 0,
564 msg, str1, str2);
565 }
566
567 /************************************************************************
568 * *
569 * Preliminary type checking interfaces *
570 * *
571 ************************************************************************/
572
573 /**
574 * xmlRelaxNGTypeHave:
575 * @data: data needed for the library
576 * @type: the type name
577 * @value: the value to check
578 *
579 * Function provided by a type library to check if a type is exported
580 *
581 * Returns 1 if yes, 0 if no and -1 in case of error.
582 */
583 typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
584
585 /**
586 * xmlRelaxNGTypeCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @value: the value to check
590 * @result: place to store the result if needed
591 *
592 * Function provided by a type library to check if a value match a type
593 *
594 * Returns 1 if yes, 0 if no and -1 in case of error.
595 */
596 typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
597 const xmlChar * value, void **result,
598 xmlNodePtr node);
599
600 /**
601 * xmlRelaxNGFacetCheck:
602 * @data: data needed for the library
603 * @type: the type name
604 * @facet: the facet name
605 * @val: the facet value
606 * @strval: the string value
607 * @value: the value to check
608 *
609 * Function provided by a type library to check a value facet
610 *
611 * Returns 1 if yes, 0 if no and -1 in case of error.
612 */
613 typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
614 const xmlChar * facet,
615 const xmlChar * val,
616 const xmlChar * strval, void *value);
617
618 /**
619 * xmlRelaxNGTypeFree:
620 * @data: data needed for the library
621 * @result: the value to free
622 *
623 * Function provided by a type library to free a returned result
624 */
625 typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
626
627 /**
628 * xmlRelaxNGTypeCompare:
629 * @data: data needed for the library
630 * @type: the type name
631 * @value1: the first value
632 * @value2: the second value
633 *
634 * Function provided by a type library to compare two values accordingly
635 * to a type.
636 *
637 * Returns 1 if yes, 0 if no and -1 in case of error.
638 */
639 typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
640 const xmlChar * value1,
641 xmlNodePtr ctxt1,
642 void *comp1,
643 const xmlChar * value2,
644 xmlNodePtr ctxt2);
645 typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
646 typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
647 struct _xmlRelaxNGTypeLibrary {
648 const xmlChar *namespace; /* the datatypeLibrary value */
649 void *data; /* data needed for the library */
650 xmlRelaxNGTypeHave have; /* the export function */
651 xmlRelaxNGTypeCheck check; /* the checking function */
652 xmlRelaxNGTypeCompare comp; /* the compare function */
653 xmlRelaxNGFacetCheck facet; /* the facet check function */
654 xmlRelaxNGTypeFree freef; /* the freeing function */
655 };
656
657 /************************************************************************
658 * *
659 * Allocation functions *
660 * *
661 ************************************************************************/
662 static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
663 static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
664 static void xmlRelaxNGNormExtSpace(xmlChar * value);
665 static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
666 static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
667 ATTRIBUTE_UNUSED,
668 xmlRelaxNGValidStatePtr state1,
669 xmlRelaxNGValidStatePtr state2);
670 static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
671 xmlRelaxNGValidStatePtr state);
672
673 /**
674 * xmlRelaxNGFreeDocument:
675 * @docu: a document structure
676 *
677 * Deallocate a RelaxNG document structure.
678 */
679 static void
680 xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
681 {
682 if (docu == NULL)
683 return;
684
685 if (docu->href != NULL)
686 xmlFree(docu->href);
687 if (docu->doc != NULL)
688 xmlFreeDoc(docu->doc);
689 if (docu->schema != NULL)
690 xmlRelaxNGFreeInnerSchema(docu->schema);
691 xmlFree(docu);
692 }
693
694 /**
695 * xmlRelaxNGFreeDocumentList:
696 * @docu: a list of document structure
697 *
698 * Deallocate a RelaxNG document structures.
699 */
700 static void
701 xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
702 {
703 xmlRelaxNGDocumentPtr next;
704
705 while (docu != NULL) {
706 next = docu->next;
707 xmlRelaxNGFreeDocument(docu);
708 docu = next;
709 }
710 }
711
712 /**
713 * xmlRelaxNGFreeInclude:
714 * @incl: a include structure
715 *
716 * Deallocate a RelaxNG include structure.
717 */
718 static void
719 xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
720 {
721 if (incl == NULL)
722 return;
723
724 if (incl->href != NULL)
725 xmlFree(incl->href);
726 if (incl->doc != NULL)
727 xmlFreeDoc(incl->doc);
728 if (incl->schema != NULL)
729 xmlRelaxNGFree(incl->schema);
730 xmlFree(incl);
731 }
732
733 /**
734 * xmlRelaxNGFreeIncludeList:
735 * @incl: a include structure list
736 *
737 * Deallocate a RelaxNG include structure.
738 */
739 static void
740 xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
741 {
742 xmlRelaxNGIncludePtr next;
743
744 while (incl != NULL) {
745 next = incl->next;
746 xmlRelaxNGFreeInclude(incl);
747 incl = next;
748 }
749 }
750
751 /**
752 * xmlRelaxNGNewRelaxNG:
753 * @ctxt: a Relax-NG validation context (optional)
754 *
755 * Allocate a new RelaxNG structure.
756 *
757 * Returns the newly allocated structure or NULL in case or error
758 */
759 static xmlRelaxNGPtr
760 xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
761 {
762 xmlRelaxNGPtr ret;
763
764 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
765 if (ret == NULL) {
766 xmlRngPErrMemory(ctxt, NULL);
767 return (NULL);
768 }
769 memset(ret, 0, sizeof(xmlRelaxNG));
770
771 return (ret);
772 }
773
774 /**
775 * xmlRelaxNGFreeInnerSchema:
776 * @schema: a schema structure
777 *
778 * Deallocate a RelaxNG schema structure.
779 */
780 static void
781 xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
782 {
783 if (schema == NULL)
784 return;
785
786 if (schema->doc != NULL)
787 xmlFreeDoc(schema->doc);
788 if (schema->defTab != NULL) {
789 int i;
790
791 for (i = 0; i < schema->defNr; i++)
792 xmlRelaxNGFreeDefine(schema->defTab[i]);
793 xmlFree(schema->defTab);
794 }
795
796 xmlFree(schema);
797 }
798
799 /**
800 * xmlRelaxNGFree:
801 * @schema: a schema structure
802 *
803 * Deallocate a RelaxNG structure.
804 */
805 void
806 xmlRelaxNGFree(xmlRelaxNGPtr schema)
807 {
808 if (schema == NULL)
809 return;
810
811 if (schema->topgrammar != NULL)
812 xmlRelaxNGFreeGrammar(schema->topgrammar);
813 if (schema->doc != NULL)
814 xmlFreeDoc(schema->doc);
815 if (schema->documents != NULL)
816 xmlRelaxNGFreeDocumentList(schema->documents);
817 if (schema->includes != NULL)
818 xmlRelaxNGFreeIncludeList(schema->includes);
819 if (schema->defTab != NULL) {
820 int i;
821
822 for (i = 0; i < schema->defNr; i++)
823 xmlRelaxNGFreeDefine(schema->defTab[i]);
824 xmlFree(schema->defTab);
825 }
826
827 xmlFree(schema);
828 }
829
830 /**
831 * xmlRelaxNGNewGrammar:
832 * @ctxt: a Relax-NG validation context (optional)
833 *
834 * Allocate a new RelaxNG grammar.
835 *
836 * Returns the newly allocated structure or NULL in case or error
837 */
838 static xmlRelaxNGGrammarPtr
839 xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
840 {
841 xmlRelaxNGGrammarPtr ret;
842
843 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
844 if (ret == NULL) {
845 xmlRngPErrMemory(ctxt, NULL);
846 return (NULL);
847 }
848 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
849
850 return (ret);
851 }
852
853 /**
854 * xmlRelaxNGFreeGrammar:
855 * @grammar: a grammar structure
856 *
857 * Deallocate a RelaxNG grammar structure.
858 */
859 static void
860 xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
861 {
862 if (grammar == NULL)
863 return;
864
865 if (grammar->children != NULL) {
866 xmlRelaxNGFreeGrammar(grammar->children);
867 }
868 if (grammar->next != NULL) {
869 xmlRelaxNGFreeGrammar(grammar->next);
870 }
871 if (grammar->refs != NULL) {
872 xmlHashFree(grammar->refs, NULL);
873 }
874 if (grammar->defs != NULL) {
875 xmlHashFree(grammar->defs, NULL);
876 }
877
878 xmlFree(grammar);
879 }
880
881 /**
882 * xmlRelaxNGNewDefine:
883 * @ctxt: a Relax-NG validation context
884 * @node: the node in the input document.
885 *
886 * Allocate a new RelaxNG define.
887 *
888 * Returns the newly allocated structure or NULL in case or error
889 */
890 static xmlRelaxNGDefinePtr
891 xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
892 {
893 xmlRelaxNGDefinePtr ret;
894
895 if (ctxt->defMax == 0) {
896 ctxt->defMax = 16;
897 ctxt->defNr = 0;
898 ctxt->defTab = (xmlRelaxNGDefinePtr *)
899 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
900 if (ctxt->defTab == NULL) {
901 xmlRngPErrMemory(ctxt, "allocating define\n");
902 return (NULL);
903 }
904 } else if (ctxt->defMax <= ctxt->defNr) {
905 xmlRelaxNGDefinePtr *tmp;
906
907 ctxt->defMax *= 2;
908 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
909 ctxt->defMax *
910 sizeof
911 (xmlRelaxNGDefinePtr));
912 if (tmp == NULL) {
913 xmlRngPErrMemory(ctxt, "allocating define\n");
914 return (NULL);
915 }
916 ctxt->defTab = tmp;
917 }
918 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
919 if (ret == NULL) {
920 xmlRngPErrMemory(ctxt, "allocating define\n");
921 return (NULL);
922 }
923 memset(ret, 0, sizeof(xmlRelaxNGDefine));
924 ctxt->defTab[ctxt->defNr++] = ret;
925 ret->node = node;
926 ret->depth = -1;
927 return (ret);
928 }
929
930 /**
931 * xmlRelaxNGFreePartition:
932 * @partitions: a partition set structure
933 *
934 * Deallocate RelaxNG partition set structures.
935 */
936 static void
937 xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
938 {
939 xmlRelaxNGInterleaveGroupPtr group;
940 int j;
941
942 if (partitions != NULL) {
943 if (partitions->groups != NULL) {
944 for (j = 0; j < partitions->nbgroups; j++) {
945 group = partitions->groups[j];
946 if (group != NULL) {
947 if (group->defs != NULL)
948 xmlFree(group->defs);
949 if (group->attrs != NULL)
950 xmlFree(group->attrs);
951 xmlFree(group);
952 }
953 }
954 xmlFree(partitions->groups);
955 }
956 if (partitions->triage != NULL) {
957 xmlHashFree(partitions->triage, NULL);
958 }
959 xmlFree(partitions);
960 }
961 }
962
963 /**
964 * xmlRelaxNGFreeDefine:
965 * @define: a define structure
966 *
967 * Deallocate a RelaxNG define structure.
968 */
969 static void
970 xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
971 {
972 if (define == NULL)
973 return;
974
975 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
976 xmlRelaxNGTypeLibraryPtr lib;
977
978 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
979 if ((lib != NULL) && (lib->freef != NULL))
980 lib->freef(lib->data, (void *) define->attrs);
981 }
982 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
983 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
984 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
985 xmlHashFree((xmlHashTablePtr) define->data, NULL);
986 if (define->name != NULL)
987 xmlFree(define->name);
988 if (define->ns != NULL)
989 xmlFree(define->ns);
990 if (define->value != NULL)
991 xmlFree(define->value);
992 if (define->contModel != NULL)
993 xmlRegFreeRegexp(define->contModel);
994 xmlFree(define);
995 }
996
997 /**
998 * xmlRelaxNGNewStates:
999 * @ctxt: a Relax-NG validation context
1000 * @size: the default size for the container
1001 *
1002 * Allocate a new RelaxNG validation state container
1003 *
1004 * Returns the newly allocated structure or NULL in case or error
1005 */
1006 static xmlRelaxNGStatesPtr
1007 xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1008 {
1009 xmlRelaxNGStatesPtr ret;
1010
1011 if ((ctxt != NULL) &&
1012 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
1013 ctxt->freeStatesNr--;
1014 ret = ctxt->freeStates[ctxt->freeStatesNr];
1015 ret->nbState = 0;
1016 return (ret);
1017 }
1018 if (size < 16)
1019 size = 16;
1020
1021 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
1022 (size -
1023 1) *
1024 sizeof(xmlRelaxNGValidStatePtr));
1025 if (ret == NULL) {
1026 xmlRngVErrMemory(ctxt, "allocating states\n");
1027 return (NULL);
1028 }
1029 ret->nbState = 0;
1030 ret->maxState = size;
1031 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1032 sizeof
1033 (xmlRelaxNGValidStatePtr));
1034 if (ret->tabState == NULL) {
1035 xmlRngVErrMemory(ctxt, "allocating states\n");
1036 xmlFree(ret);
1037 return (NULL);
1038 }
1039 return (ret);
1040 }
1041
1042 /**
1043 * xmlRelaxNGAddStateUniq:
1044 * @ctxt: a Relax-NG validation context
1045 * @states: the states container
1046 * @state: the validation state
1047 *
1048 * Add a RelaxNG validation state to the container without checking
1049 * for unicity.
1050 *
1051 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1052 */
1053 static int
1054 xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
1055 xmlRelaxNGStatesPtr states,
1056 xmlRelaxNGValidStatePtr state)
1057 {
1058 if (state == NULL) {
1059 return (-1);
1060 }
1061 if (states->nbState >= states->maxState) {
1062 xmlRelaxNGValidStatePtr *tmp;
1063 int size;
1064
1065 size = states->maxState * 2;
1066 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1067 (size) *
1068 sizeof
1069 (xmlRelaxNGValidStatePtr));
1070 if (tmp == NULL) {
1071 xmlRngVErrMemory(ctxt, "adding states\n");
1072 return (-1);
1073 }
1074 states->tabState = tmp;
1075 states->maxState = size;
1076 }
1077 states->tabState[states->nbState++] = state;
1078 return (1);
1079 }
1080
1081 /**
1082 * xmlRelaxNGAddState:
1083 * @ctxt: a Relax-NG validation context
1084 * @states: the states container
1085 * @state: the validation state
1086 *
1087 * Add a RelaxNG validation state to the container
1088 *
1089 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1090 */
1091 static int
1092 xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1093 xmlRelaxNGStatesPtr states,
1094 xmlRelaxNGValidStatePtr state)
1095 {
1096 int i;
1097
1098 if (state == NULL || states == NULL) {
1099 return (-1);
1100 }
1101 if (states->nbState >= states->maxState) {
1102 xmlRelaxNGValidStatePtr *tmp;
1103 int size;
1104
1105 size = states->maxState * 2;
1106 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1107 (size) *
1108 sizeof
1109 (xmlRelaxNGValidStatePtr));
1110 if (tmp == NULL) {
1111 xmlRngVErrMemory(ctxt, "adding states\n");
1112 return (-1);
1113 }
1114 states->tabState = tmp;
1115 states->maxState = size;
1116 }
1117 for (i = 0; i < states->nbState; i++) {
1118 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1119 xmlRelaxNGFreeValidState(ctxt, state);
1120 return (0);
1121 }
1122 }
1123 states->tabState[states->nbState++] = state;
1124 return (1);
1125 }
1126
1127 /**
1128 * xmlRelaxNGFreeStates:
1129 * @ctxt: a Relax-NG validation context
1130 * @states: teh container
1131 *
1132 * Free a RelaxNG validation state container
1133 */
1134 static void
1135 xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
1136 xmlRelaxNGStatesPtr states)
1137 {
1138 if (states == NULL)
1139 return;
1140 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
1141 ctxt->freeStatesMax = 40;
1142 ctxt->freeStatesNr = 0;
1143 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1144 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1145 if (ctxt->freeStates == NULL) {
1146 xmlRngVErrMemory(ctxt, "storing states\n");
1147 }
1148 } else if ((ctxt != NULL)
1149 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1150 xmlRelaxNGStatesPtr *tmp;
1151
1152 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1153 2 * ctxt->freeStatesMax *
1154 sizeof
1155 (xmlRelaxNGStatesPtr));
1156 if (tmp == NULL) {
1157 xmlRngVErrMemory(ctxt, "storing states\n");
1158 xmlFree(states->tabState);
1159 xmlFree(states);
1160 return;
1161 }
1162 ctxt->freeStates = tmp;
1163 ctxt->freeStatesMax *= 2;
1164 }
1165 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
1166 xmlFree(states->tabState);
1167 xmlFree(states);
1168 } else {
1169 ctxt->freeStates[ctxt->freeStatesNr++] = states;
1170 }
1171 }
1172
1173 /**
1174 * xmlRelaxNGNewValidState:
1175 * @ctxt: a Relax-NG validation context
1176 * @node: the current node or NULL for the document
1177 *
1178 * Allocate a new RelaxNG validation state
1179 *
1180 * Returns the newly allocated structure or NULL in case or error
1181 */
1182 static xmlRelaxNGValidStatePtr
1183 xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1184 {
1185 xmlRelaxNGValidStatePtr ret;
1186 xmlAttrPtr attr;
1187 xmlAttrPtr attrs[MAX_ATTR];
1188 int nbAttrs = 0;
1189 xmlNodePtr root = NULL;
1190
1191 if (node == NULL) {
1192 root = xmlDocGetRootElement(ctxt->doc);
1193 if (root == NULL)
1194 return (NULL);
1195 } else {
1196 attr = node->properties;
1197 while (attr != NULL) {
1198 if (nbAttrs < MAX_ATTR)
1199 attrs[nbAttrs++] = attr;
1200 else
1201 nbAttrs++;
1202 attr = attr->next;
1203 }
1204 }
1205 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1206 ctxt->freeState->nbState--;
1207 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1208 } else {
1209 ret =
1210 (xmlRelaxNGValidStatePtr)
1211 xmlMalloc(sizeof(xmlRelaxNGValidState));
1212 if (ret == NULL) {
1213 xmlRngVErrMemory(ctxt, "allocating states\n");
1214 return (NULL);
1215 }
1216 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1217 }
1218 ret->value = NULL;
1219 ret->endvalue = NULL;
1220 if (node == NULL) {
1221 ret->node = (xmlNodePtr) ctxt->doc;
1222 ret->seq = root;
1223 } else {
1224 ret->node = node;
1225 ret->seq = node->children;
1226 }
1227 ret->nbAttrs = 0;
1228 if (nbAttrs > 0) {
1229 if (ret->attrs == NULL) {
1230 if (nbAttrs < 4)
1231 ret->maxAttrs = 4;
1232 else
1233 ret->maxAttrs = nbAttrs;
1234 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1235 sizeof(xmlAttrPtr));
1236 if (ret->attrs == NULL) {
1237 xmlRngVErrMemory(ctxt, "allocating states\n");
1238 return (ret);
1239 }
1240 } else if (ret->maxAttrs < nbAttrs) {
1241 xmlAttrPtr *tmp;
1242
1243 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1244 sizeof(xmlAttrPtr));
1245 if (tmp == NULL) {
1246 xmlRngVErrMemory(ctxt, "allocating states\n");
1247 return (ret);
1248 }
1249 ret->attrs = tmp;
1250 ret->maxAttrs = nbAttrs;
1251 }
1252 ret->nbAttrs = nbAttrs;
1253 if (nbAttrs < MAX_ATTR) {
1254 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1255 } else {
1256 attr = node->properties;
1257 nbAttrs = 0;
1258 while (attr != NULL) {
1259 ret->attrs[nbAttrs++] = attr;
1260 attr = attr->next;
1261 }
1262 }
1263 }
1264 ret->nbAttrLeft = ret->nbAttrs;
1265 return (ret);
1266 }
1267
1268 /**
1269 * xmlRelaxNGCopyValidState:
1270 * @ctxt: a Relax-NG validation context
1271 * @state: a validation state
1272 *
1273 * Copy the validation state
1274 *
1275 * Returns the newly allocated structure or NULL in case or error
1276 */
1277 static xmlRelaxNGValidStatePtr
1278 xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1279 xmlRelaxNGValidStatePtr state)
1280 {
1281 xmlRelaxNGValidStatePtr ret;
1282 unsigned int maxAttrs;
1283 xmlAttrPtr *attrs;
1284
1285 if (state == NULL)
1286 return (NULL);
1287 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1288 ctxt->freeState->nbState--;
1289 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1290 } else {
1291 ret =
1292 (xmlRelaxNGValidStatePtr)
1293 xmlMalloc(sizeof(xmlRelaxNGValidState));
1294 if (ret == NULL) {
1295 xmlRngVErrMemory(ctxt, "allocating states\n");
1296 return (NULL);
1297 }
1298 memset(ret, 0, sizeof(xmlRelaxNGValidState));
1299 }
1300 attrs = ret->attrs;
1301 maxAttrs = ret->maxAttrs;
1302 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1303 ret->attrs = attrs;
1304 ret->maxAttrs = maxAttrs;
1305 if (state->nbAttrs > 0) {
1306 if (ret->attrs == NULL) {
1307 ret->maxAttrs = state->maxAttrs;
1308 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1309 sizeof(xmlAttrPtr));
1310 if (ret->attrs == NULL) {
1311 xmlRngVErrMemory(ctxt, "allocating states\n");
1312 ret->nbAttrs = 0;
1313 return (ret);
1314 }
1315 } else if (ret->maxAttrs < state->nbAttrs) {
1316 xmlAttrPtr *tmp;
1317
1318 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1319 sizeof(xmlAttrPtr));
1320 if (tmp == NULL) {
1321 xmlRngVErrMemory(ctxt, "allocating states\n");
1322 ret->nbAttrs = 0;
1323 return (ret);
1324 }
1325 ret->maxAttrs = state->maxAttrs;
1326 ret->attrs = tmp;
1327 }
1328 memcpy(ret->attrs, state->attrs,
1329 state->nbAttrs * sizeof(xmlAttrPtr));
1330 }
1331 return (ret);
1332 }
1333
1334 /**
1335 * xmlRelaxNGEqualValidState:
1336 * @ctxt: a Relax-NG validation context
1337 * @state1: a validation state
1338 * @state2: a validation state
1339 *
1340 * Compare the validation states for equality
1341 *
1342 * Returns 1 if equald, 0 otherwise
1343 */
1344 static int
1345 xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1346 xmlRelaxNGValidStatePtr state1,
1347 xmlRelaxNGValidStatePtr state2)
1348 {
1349 int i;
1350
1351 if ((state1 == NULL) || (state2 == NULL))
1352 return (0);
1353 if (state1 == state2)
1354 return (1);
1355 if (state1->node != state2->node)
1356 return (0);
1357 if (state1->seq != state2->seq)
1358 return (0);
1359 if (state1->nbAttrLeft != state2->nbAttrLeft)
1360 return (0);
1361 if (state1->nbAttrs != state2->nbAttrs)
1362 return (0);
1363 if (state1->endvalue != state2->endvalue)
1364 return (0);
1365 if ((state1->value != state2->value) &&
1366 (!xmlStrEqual(state1->value, state2->value)))
1367 return (0);
1368 for (i = 0; i < state1->nbAttrs; i++) {
1369 if (state1->attrs[i] != state2->attrs[i])
1370 return (0);
1371 }
1372 return (1);
1373 }
1374
1375 /**
1376 * xmlRelaxNGFreeValidState:
1377 * @state: a validation state structure
1378 *
1379 * Deallocate a RelaxNG validation state structure.
1380 */
1381 static void
1382 xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1383 xmlRelaxNGValidStatePtr state)
1384 {
1385 if (state == NULL)
1386 return;
1387
1388 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1389 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1390 }
1391 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1392 if (state->attrs != NULL)
1393 xmlFree(state->attrs);
1394 xmlFree(state);
1395 } else {
1396 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1397 }
1398 }
1399
1400 /************************************************************************
1401 * *
1402 * Semi internal functions *
1403 * *
1404 ************************************************************************/
1405
1406 /**
1407 * xmlRelaxParserSetFlag:
1408 * @ctxt: a RelaxNG parser context
1409 * @flags: a set of flags values
1410 *
1411 * Semi private function used to pass informations to a parser context
1412 * which are a combination of xmlRelaxNGParserFlag .
1413 *
1414 * Returns 0 if success and -1 in case of error
1415 */
1416 int
1417 xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1418 {
1419 if (ctxt == NULL) return(-1);
1420 if (flags & XML_RELAXNGP_FREE_DOC) {
1421 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1422 flags -= XML_RELAXNGP_FREE_DOC;
1423 }
1424 if (flags & XML_RELAXNGP_CRNG) {
1425 ctxt->crng |= XML_RELAXNGP_CRNG;
1426 flags -= XML_RELAXNGP_CRNG;
1427 }
1428 if (flags != 0) return(-1);
1429 return(0);
1430 }
1431
1432 /************************************************************************
1433 * *
1434 * Document functions *
1435 * *
1436 ************************************************************************/
1437 static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1438 xmlDocPtr doc);
1439
1440 /**
1441 * xmlRelaxNGIncludePush:
1442 * @ctxt: the parser context
1443 * @value: the element doc
1444 *
1445 * Pushes a new include on top of the include stack
1446 *
1447 * Returns 0 in case of error, the index in the stack otherwise
1448 */
1449 static int
1450 xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1451 xmlRelaxNGIncludePtr value)
1452 {
1453 if (ctxt->incTab == NULL) {
1454 ctxt->incMax = 4;
1455 ctxt->incNr = 0;
1456 ctxt->incTab =
1457 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1458 sizeof(ctxt->incTab[0]));
1459 if (ctxt->incTab == NULL) {
1460 xmlRngPErrMemory(ctxt, "allocating include\n");
1461 return (0);
1462 }
1463 }
1464 if (ctxt->incNr >= ctxt->incMax) {
1465 ctxt->incMax *= 2;
1466 ctxt->incTab =
1467 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1468 ctxt->incMax *
1469 sizeof(ctxt->incTab[0]));
1470 if (ctxt->incTab == NULL) {
1471 xmlRngPErrMemory(ctxt, "allocating include\n");
1472 return (0);
1473 }
1474 }
1475 ctxt->incTab[ctxt->incNr] = value;
1476 ctxt->inc = value;
1477 return (ctxt->incNr++);
1478 }
1479
1480 /**
1481 * xmlRelaxNGIncludePop:
1482 * @ctxt: the parser context
1483 *
1484 * Pops the top include from the include stack
1485 *
1486 * Returns the include just removed
1487 */
1488 static xmlRelaxNGIncludePtr
1489 xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1490 {
1491 xmlRelaxNGIncludePtr ret;
1492
1493 if (ctxt->incNr <= 0)
1494 return (NULL);
1495 ctxt->incNr--;
1496 if (ctxt->incNr > 0)
1497 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1498 else
1499 ctxt->inc = NULL;
1500 ret = ctxt->incTab[ctxt->incNr];
1501 ctxt->incTab[ctxt->incNr] = NULL;
1502 return (ret);
1503 }
1504
1505 /**
1506 * xmlRelaxNGRemoveRedefine:
1507 * @ctxt: the parser context
1508 * @URL: the normalized URL
1509 * @target: the included target
1510 * @name: the define name to eliminate
1511 *
1512 * Applies the elimination algorithm of 4.7
1513 *
1514 * Returns 0 in case of error, 1 in case of success.
1515 */
1516 static int
1517 xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1518 const xmlChar * URL ATTRIBUTE_UNUSED,
1519 xmlNodePtr target, const xmlChar * name)
1520 {
1521 int found = 0;
1522 xmlNodePtr tmp, tmp2;
1523 xmlChar *name2;
1524
1525 #ifdef DEBUG_INCLUDE
1526 if (name == NULL)
1527 xmlGenericError(xmlGenericErrorContext,
1528 "Elimination of <include> start from %s\n", URL);
1529 else
1530 xmlGenericError(xmlGenericErrorContext,
1531 "Elimination of <include> define %s from %s\n",
1532 name, URL);
1533 #endif
1534 tmp = target;
1535 while (tmp != NULL) {
1536 tmp2 = tmp->next;
1537 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1538 found = 1;
1539 xmlUnlinkNode(tmp);
1540 xmlFreeNode(tmp);
1541 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1542 name2 = xmlGetProp(tmp, BAD_CAST "name");
1543 xmlRelaxNGNormExtSpace(name2);
1544 if (name2 != NULL) {
1545 if (xmlStrEqual(name, name2)) {
1546 found = 1;
1547 xmlUnlinkNode(tmp);
1548 xmlFreeNode(tmp);
1549 }
1550 xmlFree(name2);
1551 }
1552 } else if (IS_RELAXNG(tmp, "include")) {
1553 xmlChar *href = NULL;
1554 xmlRelaxNGDocumentPtr inc = tmp->psvi;
1555
1556 if ((inc != NULL) && (inc->doc != NULL) &&
1557 (inc->doc->children != NULL)) {
1558
1559 if (xmlStrEqual
1560 (inc->doc->children->name, BAD_CAST "grammar")) {
1561 #ifdef DEBUG_INCLUDE
1562 href = xmlGetProp(tmp, BAD_CAST "href");
1563 #endif
1564 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1565 xmlDocGetRootElement(inc->doc)->children,
1566 name) == 1) {
1567 found = 1;
1568 }
1569 #ifdef DEBUG_INCLUDE
1570 if (href != NULL)
1571 xmlFree(href);
1572 #endif
1573 }
1574 }
1575 }
1576 tmp = tmp2;
1577 }
1578 return (found);
1579 }
1580
1581 /**
1582 * xmlRelaxNGLoadInclude:
1583 * @ctxt: the parser context
1584 * @URL: the normalized URL
1585 * @node: the include node.
1586 * @ns: the namespace passed from the context.
1587 *
1588 * First lookup if the document is already loaded into the parser context,
1589 * check against recursion. If not found the resource is loaded and
1590 * the content is preprocessed before being returned back to the caller.
1591 *
1592 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1593 */
1594 static xmlRelaxNGIncludePtr
1595 xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1596 xmlNodePtr node, const xmlChar * ns)
1597 {
1598 xmlRelaxNGIncludePtr ret = NULL;
1599 xmlDocPtr doc;
1600 int i;
1601 xmlNodePtr root, cur;
1602
1603 #ifdef DEBUG_INCLUDE
1604 xmlGenericError(xmlGenericErrorContext,
1605 "xmlRelaxNGLoadInclude(%s)\n", URL);
1606 #endif
1607
1608 /*
1609 * check against recursion in the stack
1610 */
1611 for (i = 0; i < ctxt->incNr; i++) {
1612 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1613 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1614 "Detected an Include recursion for %s\n", URL,
1615 NULL);
1616 return (NULL);
1617 }
1618 }
1619
1620 /*
1621 * load the document
1622 */
1623 doc = xmlReadFile((const char *) URL,NULL,0);
1624 if (doc == NULL) {
1625 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1626 "xmlRelaxNG: could not load %s\n", URL, NULL);
1627 return (NULL);
1628 }
1629 #ifdef DEBUG_INCLUDE
1630 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
1631 #endif
1632
1633 /*
1634 * Allocate the document structures and register it first.
1635 */
1636 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1637 if (ret == NULL) {
1638 xmlRngPErrMemory(ctxt, "allocating include\n");
1639 xmlFreeDoc(doc);
1640 return (NULL);
1641 }
1642 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1643 ret->doc = doc;
1644 ret->href = xmlStrdup(URL);
1645 ret->next = ctxt->includes;
1646 ctxt->includes = ret;
1647
1648 /*
1649 * transmit the ns if needed
1650 */
1651 if (ns != NULL) {
1652 root = xmlDocGetRootElement(doc);
1653 if (root != NULL) {
1654 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1655 xmlSetProp(root, BAD_CAST "ns", ns);
1656 }
1657 }
1658 }
1659
1660 /*
1661 * push it on the stack
1662 */
1663 xmlRelaxNGIncludePush(ctxt, ret);
1664
1665 /*
1666 * Some preprocessing of the document content, this include recursing
1667 * in the include stack.
1668 */
1669 #ifdef DEBUG_INCLUDE
1670 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
1671 #endif
1672
1673 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1674 if (doc == NULL) {
1675 ctxt->inc = NULL;
1676 return (NULL);
1677 }
1678
1679 /*
1680 * Pop up the include from the stack
1681 */
1682 xmlRelaxNGIncludePop(ctxt);
1683
1684 #ifdef DEBUG_INCLUDE
1685 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
1686 #endif
1687 /*
1688 * Check that the top element is a grammar
1689 */
1690 root = xmlDocGetRootElement(doc);
1691 if (root == NULL) {
1692 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1693 "xmlRelaxNG: included document is empty %s\n", URL,
1694 NULL);
1695 return (NULL);
1696 }
1697 if (!IS_RELAXNG(root, "grammar")) {
1698 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1699 "xmlRelaxNG: included document %s root is not a grammar\n",
1700 URL, NULL);
1701 return (NULL);
1702 }
1703
1704 /*
1705 * Elimination of redefined rules in the include.
1706 */
1707 cur = node->children;
1708 while (cur != NULL) {
1709 if (IS_RELAXNG(cur, "start")) {
1710 int found = 0;
1711
1712 found =
1713 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1714 if (!found) {
1715 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1716 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1717 URL, NULL);
1718 }
1719 } else if (IS_RELAXNG(cur, "define")) {
1720 xmlChar *name;
1721
1722 name = xmlGetProp(cur, BAD_CAST "name");
1723 if (name == NULL) {
1724 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1725 "xmlRelaxNG: include %s has define without name\n",
1726 URL, NULL);
1727 } else {
1728 int found;
1729
1730 xmlRelaxNGNormExtSpace(name);
1731 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1732 root->children, name);
1733 if (!found) {
1734 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1735 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1736 URL, name);
1737 }
1738 xmlFree(name);
1739 }
1740 }
1741 cur = cur->next;
1742 }
1743
1744
1745 return (ret);
1746 }
1747
1748 /**
1749 * xmlRelaxNGValidErrorPush:
1750 * @ctxt: the validation context
1751 * @err: the error code
1752 * @arg1: the first string argument
1753 * @arg2: the second string argument
1754 * @dup: arg need to be duplicated
1755 *
1756 * Pushes a new error on top of the error stack
1757 *
1758 * Returns 0 in case of error, the index in the stack otherwise
1759 */
1760 static int
1761 xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1762 xmlRelaxNGValidErr err, const xmlChar * arg1,
1763 const xmlChar * arg2, int dup)
1764 {
1765 xmlRelaxNGValidErrorPtr cur;
1766
1767 #ifdef DEBUG_ERROR
1768 xmlGenericError(xmlGenericErrorContext,
1769 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1770 #endif
1771 if (ctxt->errTab == NULL) {
1772 ctxt->errMax = 8;
1773 ctxt->errNr = 0;
1774 ctxt->errTab =
1775 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1776 sizeof
1777 (xmlRelaxNGValidError));
1778 if (ctxt->errTab == NULL) {
1779 xmlRngVErrMemory(ctxt, "pushing error\n");
1780 return (0);
1781 }
1782 ctxt->err = NULL;
1783 }
1784 if (ctxt->errNr >= ctxt->errMax) {
1785 ctxt->errMax *= 2;
1786 ctxt->errTab =
1787 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1788 ctxt->errMax *
1789 sizeof
1790 (xmlRelaxNGValidError));
1791 if (ctxt->errTab == NULL) {
1792 xmlRngVErrMemory(ctxt, "pushing error\n");
1793 return (0);
1794 }
1795 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1796 }
1797 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
1798 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1799 return (ctxt->errNr);
1800 cur = &ctxt->errTab[ctxt->errNr];
1801 cur->err = err;
1802 if (dup) {
1803 cur->arg1 = xmlStrdup(arg1);
1804 cur->arg2 = xmlStrdup(arg2);
1805 cur->flags = ERROR_IS_DUP;
1806 } else {
1807 cur->arg1 = arg1;
1808 cur->arg2 = arg2;
1809 cur->flags = 0;
1810 }
1811 if (ctxt->state != NULL) {
1812 cur->node = ctxt->state->node;
1813 cur->seq = ctxt->state->seq;
1814 } else {
1815 cur->node = NULL;
1816 cur->seq = NULL;
1817 }
1818 ctxt->err = cur;
1819 return (ctxt->errNr++);
1820 }
1821
1822 /**
1823 * xmlRelaxNGValidErrorPop:
1824 * @ctxt: the validation context
1825 *
1826 * Pops the top error from the error stack
1827 */
1828 static void
1829 xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1830 {
1831 xmlRelaxNGValidErrorPtr cur;
1832
1833 if (ctxt->errNr <= 0) {
1834 ctxt->err = NULL;
1835 return;
1836 }
1837 ctxt->errNr--;
1838 if (ctxt->errNr > 0)
1839 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1840 else
1841 ctxt->err = NULL;
1842 cur = &ctxt->errTab[ctxt->errNr];
1843 if (cur->flags & ERROR_IS_DUP) {
1844 if (cur->arg1 != NULL)
1845 xmlFree((xmlChar *) cur->arg1);
1846 cur->arg1 = NULL;
1847 if (cur->arg2 != NULL)
1848 xmlFree((xmlChar *) cur->arg2);
1849 cur->arg2 = NULL;
1850 cur->flags = 0;
1851 }
1852 }
1853
1854 /**
1855 * xmlRelaxNGDocumentPush:
1856 * @ctxt: the parser context
1857 * @value: the element doc
1858 *
1859 * Pushes a new doc on top of the doc stack
1860 *
1861 * Returns 0 in case of error, the index in the stack otherwise
1862 */
1863 static int
1864 xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1865 xmlRelaxNGDocumentPtr value)
1866 {
1867 if (ctxt->docTab == NULL) {
1868 ctxt->docMax = 4;
1869 ctxt->docNr = 0;
1870 ctxt->docTab =
1871 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1872 sizeof(ctxt->docTab[0]));
1873 if (ctxt->docTab == NULL) {
1874 xmlRngPErrMemory(ctxt, "adding document\n");
1875 return (0);
1876 }
1877 }
1878 if (ctxt->docNr >= ctxt->docMax) {
1879 ctxt->docMax *= 2;
1880 ctxt->docTab =
1881 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1882 ctxt->docMax *
1883 sizeof(ctxt->docTab[0]));
1884 if (ctxt->docTab == NULL) {
1885 xmlRngPErrMemory(ctxt, "adding document\n");
1886 return (0);
1887 }
1888 }
1889 ctxt->docTab[ctxt->docNr] = value;
1890 ctxt->doc = value;
1891 return (ctxt->docNr++);
1892 }
1893
1894 /**
1895 * xmlRelaxNGDocumentPop:
1896 * @ctxt: the parser context
1897 *
1898 * Pops the top doc from the doc stack
1899 *
1900 * Returns the doc just removed
1901 */
1902 static xmlRelaxNGDocumentPtr
1903 xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1904 {
1905 xmlRelaxNGDocumentPtr ret;
1906
1907 if (ctxt->docNr <= 0)
1908 return (NULL);
1909 ctxt->docNr--;
1910 if (ctxt->docNr > 0)
1911 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1912 else
1913 ctxt->doc = NULL;
1914 ret = ctxt->docTab[ctxt->docNr];
1915 ctxt->docTab[ctxt->docNr] = NULL;
1916 return (ret);
1917 }
1918
1919 /**
1920 * xmlRelaxNGLoadExternalRef:
1921 * @ctxt: the parser context
1922 * @URL: the normalized URL
1923 * @ns: the inherited ns if any
1924 *
1925 * First lookup if the document is already loaded into the parser context,
1926 * check against recursion. If not found the resource is loaded and
1927 * the content is preprocessed before being returned back to the caller.
1928 *
1929 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1930 */
1931 static xmlRelaxNGDocumentPtr
1932 xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1933 const xmlChar * URL, const xmlChar * ns)
1934 {
1935 xmlRelaxNGDocumentPtr ret = NULL;
1936 xmlDocPtr doc;
1937 xmlNodePtr root;
1938 int i;
1939
1940 /*
1941 * check against recursion in the stack
1942 */
1943 for (i = 0; i < ctxt->docNr; i++) {
1944 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1945 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1946 "Detected an externalRef recursion for %s\n", URL,
1947 NULL);
1948 return (NULL);
1949 }
1950 }
1951
1952 /*
1953 * load the document
1954 */
1955 doc = xmlReadFile((const char *) URL,NULL,0);
1956 if (doc == NULL) {
1957 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1958 "xmlRelaxNG: could not load %s\n", URL, NULL);
1959 return (NULL);
1960 }
1961
1962 /*
1963 * Allocate the document structures and register it first.
1964 */
1965 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1966 if (ret == NULL) {
1967 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1968 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1969 xmlFreeDoc(doc);
1970 return (NULL);
1971 }
1972 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1973 ret->doc = doc;
1974 ret->href = xmlStrdup(URL);
1975 ret->next = ctxt->documents;
1976 ret->externalRef = 1;
1977 ctxt->documents = ret;
1978
1979 /*
1980 * transmit the ns if needed
1981 */
1982 if (ns != NULL) {
1983 root = xmlDocGetRootElement(doc);
1984 if (root != NULL) {
1985 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1986 xmlSetProp(root, BAD_CAST "ns", ns);
1987 }
1988 }
1989 }
1990
1991 /*
1992 * push it on the stack and register it in the hash table
1993 */
1994 xmlRelaxNGDocumentPush(ctxt, ret);
1995
1996 /*
1997 * Some preprocessing of the document content
1998 */
1999 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2000 if (doc == NULL) {
2001 ctxt->doc = NULL;
2002 return (NULL);
2003 }
2004
2005 xmlRelaxNGDocumentPop(ctxt);
2006
2007 return (ret);
2008 }
2009
2010 /************************************************************************
2011 * *
2012 * Error functions *
2013 * *
2014 ************************************************************************/
2015
2016 #define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2017 #define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2018 #define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2019 #define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2020 #define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
2021
2022 static const char *
2023 xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2024 {
2025 if (def == NULL)
2026 return ("none");
2027 switch (def->type) {
2028 case XML_RELAXNG_EMPTY:
2029 return ("empty");
2030 case XML_RELAXNG_NOT_ALLOWED:
2031 return ("notAllowed");
2032 case XML_RELAXNG_EXCEPT:
2033 return ("except");
2034 case XML_RELAXNG_TEXT:
2035 return ("text");
2036 case XML_RELAXNG_ELEMENT:
2037 return ("element");
2038 case XML_RELAXNG_DATATYPE:
2039 return ("datatype");
2040 case XML_RELAXNG_VALUE:
2041 return ("value");
2042 case XML_RELAXNG_LIST:
2043 return ("list");
2044 case XML_RELAXNG_ATTRIBUTE:
2045 return ("attribute");
2046 case XML_RELAXNG_DEF:
2047 return ("def");
2048 case XML_RELAXNG_REF:
2049 return ("ref");
2050 case XML_RELAXNG_EXTERNALREF:
2051 return ("externalRef");
2052 case XML_RELAXNG_PARENTREF:
2053 return ("parentRef");
2054 case XML_RELAXNG_OPTIONAL:
2055 return ("optional");
2056 case XML_RELAXNG_ZEROORMORE:
2057 return ("zeroOrMore");
2058 case XML_RELAXNG_ONEORMORE:
2059 return ("oneOrMore");
2060 case XML_RELAXNG_CHOICE:
2061 return ("choice");
2062 case XML_RELAXNG_GROUP:
2063 return ("group");
2064 case XML_RELAXNG_INTERLEAVE:
2065 return ("interleave");
2066 case XML_RELAXNG_START:
2067 return ("start");
2068 case XML_RELAXNG_NOOP:
2069 return ("noop");
2070 case XML_RELAXNG_PARAM:
2071 return ("param");
2072 }
2073 return ("unknown");
2074 }
2075
2076 /**
2077 * xmlRelaxNGGetErrorString:
2078 * @err: the error code
2079 * @arg1: the first string argument
2080 * @arg2: the second string argument
2081 *
2082 * computes a formatted error string for the given error code and args
2083 *
2084 * Returns the error string, it must be deallocated by the caller
2085 */
2086 static xmlChar *
2087 xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2088 const xmlChar * arg2)
2089 {
2090 char msg[1000];
2091 xmlChar *result;
2092
2093 if (arg1 == NULL)
2094 arg1 = BAD_CAST "";
2095 if (arg2 == NULL)
2096 arg2 = BAD_CAST "";
2097
2098 msg[0] = 0;
2099 switch (err) {
2100 case XML_RELAXNG_OK:
2101 return (NULL);
2102 case XML_RELAXNG_ERR_MEMORY:
2103 return (xmlCharStrdup("out of memory\n"));
2104 case XML_RELAXNG_ERR_TYPE:
2105 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2106 break;
2107 case XML_RELAXNG_ERR_TYPEVAL:
2108 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2109 arg2);
2110 break;
2111 case XML_RELAXNG_ERR_DUPID:
2112 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2113 break;
2114 case XML_RELAXNG_ERR_TYPECMP:
2115 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2116 break;
2117 case XML_RELAXNG_ERR_NOSTATE:
2118 return (xmlCharStrdup("Internal error: no state\n"));
2119 case XML_RELAXNG_ERR_NODEFINE:
2120 return (xmlCharStrdup("Internal error: no define\n"));
2121 case XML_RELAXNG_ERR_INTERNAL:
2122 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2123 break;
2124 case XML_RELAXNG_ERR_LISTEXTRA:
2125 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2126 break;
2127 case XML_RELAXNG_ERR_INTERNODATA:
2128 return (xmlCharStrdup
2129 ("Internal: interleave block has no data\n"));
2130 case XML_RELAXNG_ERR_INTERSEQ:
2131 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2132 case XML_RELAXNG_ERR_INTEREXTRA:
2133 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2134 break;
2135 case XML_RELAXNG_ERR_ELEMNAME:
2136 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2137 arg2);
2138 break;
2139 case XML_RELAXNG_ERR_ELEMNONS:
2140 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2141 arg1);
2142 break;
2143 case XML_RELAXNG_ERR_ELEMWRONGNS:
2144 snprintf(msg, 1000,
2145 "Element %s has wrong namespace: expecting %s\n", arg1,
2146 arg2);
2147 break;
2148 case XML_RELAXNG_ERR_ELEMWRONG:
2149 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2150 break;
2151 case XML_RELAXNG_ERR_TEXTWRONG:
2152 snprintf(msg, 1000,
2153 "Did not expect text in element %s content\n", arg1);
2154 break;
2155 case XML_RELAXNG_ERR_ELEMEXTRANS:
2156 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2157 arg1);
2158 break;
2159 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2160 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2161 break;
2162 case XML_RELAXNG_ERR_NOELEM:
2163 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2164 arg1);
2165 break;
2166 case XML_RELAXNG_ERR_NOTELEM:
2167 return (xmlCharStrdup("Expecting an element got text\n"));
2168 case XML_RELAXNG_ERR_ATTRVALID:
2169 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2170 arg1);
2171 break;
2172 case XML_RELAXNG_ERR_CONTENTVALID:
2173 snprintf(msg, 1000, "Element %s failed to validate content\n",
2174 arg1);
2175 break;
2176 case XML_RELAXNG_ERR_EXTRACONTENT:
2177 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2178 arg1, arg2);
2179 break;
2180 case XML_RELAXNG_ERR_INVALIDATTR:
2181 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2182 arg1, arg2);
2183 break;
2184 case XML_RELAXNG_ERR_LACKDATA:
2185 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2186 arg1);
2187 break;
2188 case XML_RELAXNG_ERR_DATAELEM:
2189 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2190 arg1);
2191 break;
2192 case XML_RELAXNG_ERR_VALELEM:
2193 snprintf(msg, 1000, "Value element %s has child elements\n",
2194 arg1);
2195 break;
2196 case XML_RELAXNG_ERR_LISTELEM:
2197 snprintf(msg, 1000, "List element %s has child elements\n",
2198 arg1);
2199 break;
2200 case XML_RELAXNG_ERR_DATATYPE:
2201 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2202 break;
2203 case XML_RELAXNG_ERR_VALUE:
2204 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2205 break;
2206 case XML_RELAXNG_ERR_LIST:
2207 return (xmlCharStrdup("Error validating list\n"));
2208 case XML_RELAXNG_ERR_NOGRAMMAR:
2209 return (xmlCharStrdup("No top grammar defined\n"));
2210 case XML_RELAXNG_ERR_EXTRADATA:
2211 return (xmlCharStrdup("Extra data in the document\n"));
2212 default:
2213 return (xmlCharStrdup("Unknown error !\n"));
2214 }
2215 if (msg[0] == 0) {
2216 snprintf(msg, 1000, "Unknown error code %d\n", err);
2217 }
2218 msg[1000 - 1] = 0;
2219 result = xmlCharStrdup(msg);
2220 return (xmlEscapeFormatString(&result));
2221 }
2222
2223 /**
2224 * xmlRelaxNGShowValidError:
2225 * @ctxt: the validation context
2226 * @err: the error number
2227 * @node: the node
2228 * @child: the node child generating the problem.
2229 * @arg1: the first argument
2230 * @arg2: the second argument
2231 *
2232 * Show a validation error.
2233 */
2234 static void
2235 xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2236 xmlRelaxNGValidErr err, xmlNodePtr node,
2237 xmlNodePtr child, const xmlChar * arg1,
2238 const xmlChar * arg2)
2239 {
2240 xmlChar *msg;
2241
2242 if (ctxt->flags & FLAGS_NOERROR)
2243 return;
2244
2245 #ifdef DEBUG_ERROR
2246 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
2247 #endif
2248 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2249 if (msg == NULL)
2250 return;
2251
2252 if (ctxt->errNo == XML_RELAXNG_OK)
2253 ctxt->errNo = err;
2254 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2255 (const char *) msg, arg1, arg2);
2256 xmlFree(msg);
2257 }
2258
2259 /**
2260 * xmlRelaxNGPopErrors:
2261 * @ctxt: the validation context
2262 * @level: the error level in the stack
2263 *
2264 * pop and discard all errors until the given level is reached
2265 */
2266 static void
2267 xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2268 {
2269 int i;
2270 xmlRelaxNGValidErrorPtr err;
2271
2272 #ifdef DEBUG_ERROR
2273 xmlGenericError(xmlGenericErrorContext,
2274 "Pop errors till level %d\n", level);
2275 #endif
2276 for (i = level; i < ctxt->errNr; i++) {
2277 err = &ctxt->errTab[i];
2278 if (err->flags & ERROR_IS_DUP) {
2279 if (err->arg1 != NULL)
2280 xmlFree((xmlChar *) err->arg1);
2281 err->arg1 = NULL;
2282 if (err->arg2 != NULL)
2283 xmlFree((xmlChar *) err->arg2);
2284 err->arg2 = NULL;
2285 err->flags = 0;
2286 }
2287 }
2288 ctxt->errNr = level;
2289 if (ctxt->errNr <= 0)
2290 ctxt->err = NULL;
2291 }
2292
2293 /**
2294 * xmlRelaxNGDumpValidError:
2295 * @ctxt: the validation context
2296 *
2297 * Show all validation error over a given index.
2298 */
2299 static void
2300 xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2301 {
2302 int i, j, k;
2303 xmlRelaxNGValidErrorPtr err, dup;
2304
2305 #ifdef DEBUG_ERROR
2306 xmlGenericError(xmlGenericErrorContext,
2307 "Dumping error stack %d errors\n", ctxt->errNr);
2308 #endif
2309 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2310 err = &ctxt->errTab[i];
2311 if (k < MAX_ERROR) {
2312 for (j = 0; j < i; j++) {
2313 dup = &ctxt->errTab[j];
2314 if ((err->err == dup->err) && (err->node == dup->node) &&
2315 (xmlStrEqual(err->arg1, dup->arg1)) &&
2316 (xmlStrEqual(err->arg2, dup->arg2))) {
2317 goto skip;
2318 }
2319 }
2320 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2321 err->arg1, err->arg2);
2322 k++;
2323 }
2324 skip:
2325 if (err->flags & ERROR_IS_DUP) {
2326 if (err->arg1 != NULL)
2327 xmlFree((xmlChar *) err->arg1);
2328 err->arg1 = NULL;
2329 if (err->arg2 != NULL)
2330 xmlFree((xmlChar *) err->arg2);
2331 err->arg2 = NULL;
2332 err->flags = 0;
2333 }
2334 }
2335 ctxt->errNr = 0;
2336 }
2337
2338 /**
2339 * xmlRelaxNGAddValidError:
2340 * @ctxt: the validation context
2341 * @err: the error number
2342 * @arg1: the first argument
2343 * @arg2: the second argument
2344 * @dup: need to dup the args
2345 *
2346 * Register a validation error, either generating it if it's sure
2347 * or stacking it for later handling if unsure.
2348 */
2349 static void
2350 xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2351 xmlRelaxNGValidErr err, const xmlChar * arg1,
2352 const xmlChar * arg2, int dup)
2353 {
2354 if (ctxt == NULL)
2355 return;
2356 if (ctxt->flags & FLAGS_NOERROR)
2357 return;
2358
2359 #ifdef DEBUG_ERROR
2360 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
2361 #endif
2362 /*
2363 * generate the error directly
2364 */
2365 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2366 (ctxt->flags & FLAGS_NEGATIVE)) {
2367 xmlNodePtr node, seq;
2368
2369 /*
2370 * Flush first any stacked error which might be the
2371 * real cause of the problem.
2372 */
2373 if (ctxt->errNr != 0)
2374 xmlRelaxNGDumpValidError(ctxt);
2375 if (ctxt->state != NULL) {
2376 node = ctxt->state->node;
2377 seq = ctxt->state->seq;
2378 } else {
2379 node = seq = NULL;
2380 }
2381 if ((node == NULL) && (seq == NULL)) {
2382 node = ctxt->pnode;
2383 }
2384 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2385 }
2386 /*
2387 * Stack the error for later processing if needed
2388 */
2389 else {
2390 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2391 }
2392 }
2393
2394
2395 /************************************************************************
2396 * *
2397 * Type library hooks *
2398 * *
2399 ************************************************************************/
2400 static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2401 const xmlChar * str);
2402
2403 /**
2404 * xmlRelaxNGSchemaTypeHave:
2405 * @data: data needed for the library
2406 * @type: the type name
2407 *
2408 * Check if the given type is provided by
2409 * the W3C XMLSchema Datatype library.
2410 *
2411 * Returns 1 if yes, 0 if no and -1 in case of error.
2412 */
2413 static int
2414 xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2415 {
2416 xmlSchemaTypePtr typ;
2417
2418 if (type == NULL)
2419 return (-1);
2420 typ = xmlSchemaGetPredefinedType(type,
2421 BAD_CAST
2422 "http://www.w3.org/2001/XMLSchema");
2423 if (typ == NULL)
2424 return (0);
2425 return (1);
2426 }
2427
2428 /**
2429 * xmlRelaxNGSchemaTypeCheck:
2430 * @data: data needed for the library
2431 * @type: the type name
2432 * @value: the value to check
2433 * @node: the node
2434 *
2435 * Check if the given type and value are validated by
2436 * the W3C XMLSchema Datatype library.
2437 *
2438 * Returns 1 if yes, 0 if no and -1 in case of error.
2439 */
2440 static int
2441 xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2442 const xmlChar * type,
2443 const xmlChar * value,
2444 void **result, xmlNodePtr node)
2445 {
2446 xmlSchemaTypePtr typ;
2447 int ret;
2448
2449 if ((type == NULL) || (value == NULL))
2450 return (-1);
2451 typ = xmlSchemaGetPredefinedType(type,
2452 BAD_CAST
2453 "http://www.w3.org/2001/XMLSchema");
2454 if (typ == NULL)
2455 return (-1);
2456 ret = xmlSchemaValPredefTypeNode(typ, value,
2457 (xmlSchemaValPtr *) result, node);
2458 if (ret == 2) /* special ID error code */
2459 return (2);
2460 if (ret == 0)
2461 return (1);
2462 if (ret > 0)
2463 return (0);
2464 return (-1);
2465 }
2466
2467 /**
2468 * xmlRelaxNGSchemaFacetCheck:
2469 * @data: data needed for the library
2470 * @type: the type name
2471 * @facet: the facet name
2472 * @val: the facet value
2473 * @strval: the string value
2474 * @value: the value to check
2475 *
2476 * Function provided by a type library to check a value facet
2477 *
2478 * Returns 1 if yes, 0 if no and -1 in case of error.
2479 */
2480 static int
2481 xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2482 const xmlChar * type, const xmlChar * facetname,
2483 const xmlChar * val, const xmlChar * strval,
2484 void *value)
2485 {
2486 xmlSchemaFacetPtr facet;
2487 xmlSchemaTypePtr typ;
2488 int ret;
2489
2490 if ((type == NULL) || (strval == NULL))
2491 return (-1);
2492 typ = xmlSchemaGetPredefinedType(type,
2493 BAD_CAST
2494 "http://www.w3.org/2001/XMLSchema");
2495 if (typ == NULL)
2496 return (-1);
2497
2498 facet = xmlSchemaNewFacet();
2499 if (facet == NULL)
2500 return (-1);
2501
2502 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2503 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2504 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2505 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2506 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2507 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2508 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2509 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2510 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2511 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2512 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2513 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2514 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2515 facet->type = XML_SCHEMA_FACET_PATTERN;
2516 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2517 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2518 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2519 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2520 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2521 facet->type = XML_SCHEMA_FACET_LENGTH;
2522 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2523 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2524 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2525 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2526 } else {
2527 xmlSchemaFreeFacet(facet);
2528 return (-1);
2529 }
2530 facet->value = val;
2531 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2532 if (ret != 0) {
2533 xmlSchemaFreeFacet(facet);
2534 return (-1);
2535 }
2536 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2537 xmlSchemaFreeFacet(facet);
2538 if (ret != 0)
2539 return (-1);
2540 return (0);
2541 }
2542
2543 /**
2544 * xmlRelaxNGSchemaFreeValue:
2545 * @data: data needed for the library
2546 * @value: the value to free
2547 *
2548 * Function provided by a type library to free a Schemas value
2549 *
2550 * Returns 1 if yes, 0 if no and -1 in case of error.
2551 */
2552 static void
2553 xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2554 {
2555 xmlSchemaFreeValue(value);
2556 }
2557
2558 /**
2559 * xmlRelaxNGSchemaTypeCompare:
2560 * @data: data needed for the library
2561 * @type: the type name
2562 * @value1: the first value
2563 * @value2: the second value
2564 *
2565 * Compare two values for equality accordingly a type from the W3C XMLSchema
2566 * Datatype library.
2567 *
2568 * Returns 1 if equal, 0 if no and -1 in case of error.
2569 */
2570 static int
2571 xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2572 const xmlChar * type,
2573 const xmlChar * value1,
2574 xmlNodePtr ctxt1,
2575 void *comp1,
2576 const xmlChar * value2, xmlNodePtr ctxt2)
2577 {
2578 int ret;
2579 xmlSchemaTypePtr typ;
2580 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2581
2582 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2583 return (-1);
2584 typ = xmlSchemaGetPredefinedType(type,
2585 BAD_CAST
2586 "http://www.w3.org/2001/XMLSchema");
2587 if (typ == NULL)
2588 return (-1);
2589 if (comp1 == NULL) {
2590 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2591 if (ret != 0)
2592 return (-1);
2593 if (res1 == NULL)
2594 return (-1);
2595 } else {
2596 res1 = (xmlSchemaValPtr) comp1;
2597 }
2598 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2599 if (ret != 0) {
2600 if (res1 != (xmlSchemaValPtr) comp1)
2601 xmlSchemaFreeValue(res1);
2602 return (-1);
2603 }
2604 ret = xmlSchemaCompareValues(res1, res2);
2605 if (res1 != (xmlSchemaValPtr) comp1)
2606 xmlSchemaFreeValue(res1);
2607 xmlSchemaFreeValue(res2);
2608 if (ret == -2)
2609 return (-1);
2610 if (ret == 0)
2611 return (1);
2612 return (0);
2613 }
2614
2615 /**
2616 * xmlRelaxNGDefaultTypeHave:
2617 * @data: data needed for the library
2618 * @type: the type name
2619 *
2620 * Check if the given type is provided by
2621 * the default datatype library.
2622 *
2623 * Returns 1 if yes, 0 if no and -1 in case of error.
2624 */
2625 static int
2626 xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2627 const xmlChar * type)
2628 {
2629 if (type == NULL)
2630 return (-1);
2631 if (xmlStrEqual(type, BAD_CAST "string"))
2632 return (1);
2633 if (xmlStrEqual(type, BAD_CAST "token"))
2634 return (1);
2635 return (0);
2636 }
2637
2638 /**
2639 * xmlRelaxNGDefaultTypeCheck:
2640 * @data: data needed for the library
2641 * @type: the type name
2642 * @value: the value to check
2643 * @node: the node
2644 *
2645 * Check if the given type and value are validated by
2646 * the default datatype library.
2647 *
2648 * Returns 1 if yes, 0 if no and -1 in case of error.
2649 */
2650 static int
2651 xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2652 const xmlChar * type ATTRIBUTE_UNUSED,
2653 const xmlChar * value ATTRIBUTE_UNUSED,
2654 void **result ATTRIBUTE_UNUSED,
2655 xmlNodePtr node ATTRIBUTE_UNUSED)
2656 {
2657 if (value == NULL)
2658 return (-1);
2659 if (xmlStrEqual(type, BAD_CAST "string"))
2660 return (1);
2661 if (xmlStrEqual(type, BAD_CAST "token")) {
2662 return (1);
2663 }
2664
2665 return (0);
2666 }
2667
2668 /**
2669 * xmlRelaxNGDefaultTypeCompare:
2670 * @data: data needed for the library
2671 * @type: the type name
2672 * @value1: the first value
2673 * @value2: the second value
2674 *
2675 * Compare two values accordingly a type from the default
2676 * datatype library.
2677 *
2678 * Returns 1 if yes, 0 if no and -1 in case of error.
2679 */
2680 static int
2681 xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2682 const xmlChar * type,
2683 const xmlChar * value1,
2684 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2685 void *comp1 ATTRIBUTE_UNUSED,
2686 const xmlChar * value2,
2687 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2688 {
2689 int ret = -1;
2690
2691 if (xmlStrEqual(type, BAD_CAST "string")) {
2692 ret = xmlStrEqual(value1, value2);
2693 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2694 if (!xmlStrEqual(value1, value2)) {
2695 xmlChar *nval, *nvalue;
2696
2697 /*
2698 * TODO: trivial optimizations are possible by
2699 * computing at compile-time
2700 */
2701 nval = xmlRelaxNGNormalize(NULL, value1);
2702 nvalue = xmlRelaxNGNormalize(NULL, value2);
2703
2704 if ((nval == NULL) || (nvalue == NULL))
2705 ret = -1;
2706 else if (xmlStrEqual(nval, nvalue))
2707 ret = 1;
2708 else
2709 ret = 0;
2710 if (nval != NULL)
2711 xmlFree(nval);
2712 if (nvalue != NULL)
2713 xmlFree(nvalue);
2714 } else
2715 ret = 1;
2716 }
2717 return (ret);
2718 }
2719
2720 static int xmlRelaxNGTypeInitialized = 0;
2721 static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2722
2723 /**
2724 * xmlRelaxNGFreeTypeLibrary:
2725 * @lib: the type library structure
2726 * @namespace: the URI bound to the library
2727 *
2728 * Free the structure associated to the type library
2729 */
2730 static void
2731 xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2732 const xmlChar * namespace ATTRIBUTE_UNUSED)
2733 {
2734 if (lib == NULL)
2735 return;
2736 if (lib->namespace != NULL)
2737 xmlFree((xmlChar *) lib->namespace);
2738 xmlFree(lib);
2739 }
2740
2741 /**
2742 * xmlRelaxNGRegisterTypeLibrary:
2743 * @namespace: the URI bound to the library
2744 * @data: data associated to the library
2745 * @have: the provide function
2746 * @check: the checking function
2747 * @comp: the comparison function
2748 *
2749 * Register a new type library
2750 *
2751 * Returns 0 in case of success and -1 in case of error.
2752 */
2753 static int
2754 xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2755 xmlRelaxNGTypeHave have,
2756 xmlRelaxNGTypeCheck check,
2757 xmlRelaxNGTypeCompare comp,
2758 xmlRelaxNGFacetCheck facet,
2759 xmlRelaxNGTypeFree freef)
2760 {
2761 xmlRelaxNGTypeLibraryPtr lib;
2762 int ret;
2763
2764 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2765 (check == NULL) || (comp == NULL))
2766 return (-1);
2767 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2768 xmlGenericError(xmlGenericErrorContext,
2769 "Relax-NG types library '%s' already registered\n",
2770 namespace);
2771 return (-1);
2772 }
2773 lib =
2774 (xmlRelaxNGTypeLibraryPtr)
2775 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2776 if (lib == NULL) {
2777 xmlRngVErrMemory(NULL, "adding types library\n");
2778 return (-1);
2779 }
2780 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2781 lib->namespace = xmlStrdup(namespace);
2782 lib->data = data;
2783 lib->have = have;
2784 lib->comp = comp;
2785 lib->check = check;
2786 lib->facet = facet;
2787 lib->freef = freef;
2788 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2789 if (ret < 0) {
2790 xmlGenericError(xmlGenericErrorContext,
2791 "Relax-NG types library failed to register '%s'\n",
2792 namespace);
2793 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2794 return (-1);
2795 }
2796 return (0);
2797 }
2798
2799 /**
2800 * xmlRelaxNGInitTypes:
2801 *
2802 * Initilize the default type libraries.
2803 *
2804 * Returns 0 in case of success and -1 in case of error.
2805 */
2806 int
2807 xmlRelaxNGInitTypes(void)
2808 {
2809 if (xmlRelaxNGTypeInitialized != 0)
2810 return (0);
2811 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2812 if (xmlRelaxNGRegisteredTypes == NULL) {
2813 xmlGenericError(xmlGenericErrorContext,
2814 "Failed to allocate sh table for Relax-NG types\n");
2815 return (-1);
2816 }
2817 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2818 "http://www.w3.org/2001/XMLSchema-datatypes",
2819 NULL, xmlRelaxNGSchemaTypeHave,
2820 xmlRelaxNGSchemaTypeCheck,
2821 xmlRelaxNGSchemaTypeCompare,
2822 xmlRelaxNGSchemaFacetCheck,
2823 xmlRelaxNGSchemaFreeValue);
2824 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2825 xmlRelaxNGDefaultTypeHave,
2826 xmlRelaxNGDefaultTypeCheck,
2827 xmlRelaxNGDefaultTypeCompare, NULL,
2828 NULL);
2829 xmlRelaxNGTypeInitialized = 1;
2830 return (0);
2831 }
2832
2833 /**
2834 * xmlRelaxNGCleanupTypes:
2835 *
2836 * Cleanup the default Schemas type library associated to RelaxNG
2837 */
2838 void
2839 xmlRelaxNGCleanupTypes(void)
2840 {
2841 xmlSchemaCleanupTypes();
2842 if (xmlRelaxNGTypeInitialized == 0)
2843 return;
2844 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2845 xmlRelaxNGFreeTypeLibrary);
2846 xmlRelaxNGTypeInitialized = 0;
2847 }
2848
2849 /************************************************************************
2850 * *
2851 * Compiling element content into regexp *
2852 * *
2853 * Sometime the element content can be compiled into a pure regexp, *
2854 * This allows a faster execution and streamability at that level *
2855 * *
2856 ************************************************************************/
2857
2858 /* from automata.c but not exported */
2859 void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2860
2861
2862 static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2863 xmlRelaxNGDefinePtr def);
2864
2865 /**
2866 * xmlRelaxNGIsCompileable:
2867 * @define: the definition to check
2868 *
2869 * Check if a definition is nullable.
2870 *
2871 * Returns 1 if yes, 0 if no and -1 in case of error
2872 */
2873 static int
2874 xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2875 {
2876 int ret = -1;
2877
2878 if (def == NULL) {
2879 return (-1);
2880 }
2881 if ((def->type != XML_RELAXNG_ELEMENT) &&
2882 (def->dflags & IS_COMPILABLE))
2883 return (1);
2884 if ((def->type != XML_RELAXNG_ELEMENT) &&
2885 (def->dflags & IS_NOT_COMPILABLE))
2886 return (0);
2887 switch (def->type) {
2888 case XML_RELAXNG_NOOP:
2889 ret = xmlRelaxNGIsCompileable(def->content);
2890 break;
2891 case XML_RELAXNG_TEXT:
2892 case XML_RELAXNG_EMPTY:
2893 ret = 1;
2894 break;
2895 case XML_RELAXNG_ELEMENT:
2896 /*
2897 * Check if the element content is compileable
2898 */
2899 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2900 ((def->dflags & IS_COMPILABLE) == 0)) {
2901 xmlRelaxNGDefinePtr list;
2902
2903 list = def->content;
2904 while (list != NULL) {
2905 ret = xmlRelaxNGIsCompileable(list);
2906 if (ret != 1)
2907 break;
2908 list = list->next;
2909 }
2910 /*
2911 * Because the routine is recursive, we must guard against
2912 * discovering both COMPILABLE and NOT_COMPILABLE
2913 */
2914 if (ret == 0) {
2915 def->dflags &= ~IS_COMPILABLE;
2916 def->dflags |= IS_NOT_COMPILABLE;
2917 }
2918 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2919 def->dflags |= IS_COMPILABLE;
2920 #ifdef DEBUG_COMPILE
2921 if (ret == 1) {
2922 xmlGenericError(xmlGenericErrorContext,
2923 "element content for %s is compilable\n",
2924 def->name);
2925 } else if (ret == 0) {
2926 xmlGenericError(xmlGenericErrorContext,
2927 "element content for %s is not compilable\n",
2928 def->name);
2929 } else {
2930 xmlGenericError(xmlGenericErrorContext,
2931 "Problem in RelaxNGIsCompileable for element %s\n",
2932 def->name);
2933 }
2934 #endif
2935 }
2936 /*
2937 * All elements return a compileable status unless they
2938 * are generic like anyName
2939 */
2940 if ((def->nameClass != NULL) || (def->name == NULL))
2941 ret = 0;
2942 else
2943 ret = 1;
2944 return (ret);
2945 case XML_RELAXNG_REF:
2946 case XML_RELAXNG_EXTERNALREF:
2947 case XML_RELAXNG_PARENTREF:
2948 if (def->depth == -20) {
2949 return (1);
2950 } else {
2951 xmlRelaxNGDefinePtr list;
2952
2953 def->depth = -20;
2954 list = def->content;
2955 while (list != NULL) {
2956 ret = xmlRelaxNGIsCompileable(list);
2957 if (ret != 1)
2958 break;
2959 list = list->next;
2960 }
2961 }
2962 break;
2963 case XML_RELAXNG_START:
2964 case XML_RELAXNG_OPTIONAL:
2965 case XML_RELAXNG_ZEROORMORE:
2966 case XML_RELAXNG_ONEORMORE:
2967 case XML_RELAXNG_CHOICE:
2968 case XML_RELAXNG_GROUP:
2969 case XML_RELAXNG_DEF:{
2970 xmlRelaxNGDefinePtr list;
2971
2972 list = def->content;
2973 while (list != NULL) {
2974 ret = xmlRelaxNGIsCompileable(list);
2975 if (ret != 1)
2976 break;
2977 list = list->next;
2978 }
2979 break;
2980 }
2981 case XML_RELAXNG_EXCEPT:
2982 case XML_RELAXNG_ATTRIBUTE:
2983 case XML_RELAXNG_INTERLEAVE:
2984 case XML_RELAXNG_DATATYPE:
2985 case XML_RELAXNG_LIST:
2986 case XML_RELAXNG_PARAM:
2987 case XML_RELAXNG_VALUE:
2988 case XML_RELAXNG_NOT_ALLOWED:
2989 ret = 0;
2990 break;
2991 }
2992 if (ret == 0)
2993 def->dflags |= IS_NOT_COMPILABLE;
2994 if (ret == 1)
2995 def->dflags |= IS_COMPILABLE;
2996 #ifdef DEBUG_COMPILE
2997 if (ret == 1) {
2998 xmlGenericError(xmlGenericErrorContext,
2999 "RelaxNGIsCompileable %s : true\n",
3000 xmlRelaxNGDefName(def));
3001 } else if (ret == 0) {
3002 xmlGenericError(xmlGenericErrorContext,
3003 "RelaxNGIsCompileable %s : false\n",
3004 xmlRelaxNGDefName(def));
3005 } else {
3006 xmlGenericError(xmlGenericErrorContext,
3007 "Problem in RelaxNGIsCompileable %s\n",
3008 xmlRelaxNGDefName(def));
3009 }
3010 #endif
3011 return (ret);
3012 }
3013
3014 /**
3015 * xmlRelaxNGCompile:
3016 * ctxt: the RelaxNG parser context
3017 * @define: the definition tree to compile
3018 *
3019 * Compile the set of definitions, it works recursively, till the
3020 * element boundaries, where it tries to compile the content if possible
3021 *
3022 * Returns 0 if success and -1 in case of error
3023 */
3024 static int
3025 xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3026 {
3027 int ret = 0;
3028 xmlRelaxNGDefinePtr list;
3029
3030 if ((ctxt == NULL) || (def == NULL))
3031 return (-1);
3032
3033 switch (def->type) {
3034 case XML_RELAXNG_START:
3035 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
3036 xmlAutomataPtr oldam = ctxt->am;
3037 xmlAutomataStatePtr oldstate = ctxt->state;
3038
3039 def->depth = -25;
3040
3041 list = def->content;
3042 ctxt->am = xmlNewAutomata();
3043 if (ctxt->am == NULL)
3044 return (-1);
3045
3046 /*
3047 * assume identical strings but not same pointer are different
3048 * atoms, needed for non-determinism detection
3049 * That way if 2 elements with the same name are in a choice
3050 * branch the automata is found non-deterministic and
3051 * we fallback to the normal validation which does the right
3052 * thing of exploring both choices.
3053 */
3054 xmlAutomataSetFlags(ctxt->am, 1);
3055
3056 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3057 while (list != NULL) {
3058 xmlRelaxNGCompile(ctxt, list);
3059 list = list->next;
3060 }
3061 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3062 if (xmlAutomataIsDeterminist(ctxt->am))
3063 def->contModel = xmlAutomataCompile(ctxt->am);
3064
3065 xmlFreeAutomata(ctxt->am);
3066 ctxt->state = oldstate;
3067 ctxt->am = oldam;
3068 }
3069 break;
3070 case XML_RELAXNG_ELEMENT:
3071 if ((ctxt->am != NULL) && (def->name != NULL)) {
3072 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3073 ctxt->state, NULL,
3074 def->name, def->ns,
3075 def);
3076 }
3077 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3078 xmlAutomataPtr oldam = ctxt->am;
3079 xmlAutomataStatePtr oldstate = ctxt->state;
3080
3081 def->depth = -25;
3082
3083 list = def->content;
3084 ctxt->am = xmlNewAutomata();
3085 if (ctxt->am == NULL)
3086 return (-1);
3087 xmlAutomataSetFlags(ctxt->am, 1);
3088 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3089 while (list != NULL) {
3090 xmlRelaxNGCompile(ctxt, list);
3091 list = list->next;
3092 }
3093 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3094 def->contModel = xmlAutomataCompile(ctxt->am);
3095 if (!xmlRegexpIsDeterminist(def->contModel)) {
3096 #ifdef DEBUG_COMPILE
3097 xmlGenericError(xmlGenericErrorContext,
3098 "Content model not determinist %s\n",
3099 def->name);
3100 #endif
3101 /*
3102 * we can only use the automata if it is determinist
3103 */
3104 xmlRegFreeRegexp(def->contModel);
3105 def->contModel = NULL;
3106 }
3107 xmlFreeAutomata(ctxt->am);
3108 ctxt->state = oldstate;
3109 ctxt->am = oldam;
3110 } else {
3111 xmlAutomataPtr oldam = ctxt->am;
3112
3113 /*
3114 * we can't build the content model for this element content
3115 * but it still might be possible to build it for some of its
3116 * children, recurse.
3117 */
3118 ret = xmlRelaxNGTryCompile(ctxt, def);
3119 ctxt->am = oldam;
3120 }
3121 break;
3122 case XML_RELAXNG_NOOP:
3123 ret = xmlRelaxNGCompile(ctxt, def->content);
3124 break;
3125 case XML_RELAXNG_OPTIONAL:{
3126 xmlAutomataStatePtr oldstate = ctxt->state;
3127
3128 list = def->content;
3129 while (list != NULL) {
3130 xmlRelaxNGCompile(ctxt, list);
3131 list = list->next;
3132 }
3133 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3134 break;
3135 }
3136 case XML_RELAXNG_ZEROORMORE:{
3137 xmlAutomataStatePtr oldstate;
3138
3139 ctxt->state =
3140 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3141 oldstate = ctxt->state;
3142 list = def->content;
3143 while (list != NULL) {
3144 xmlRelaxNGCompile(ctxt, list);
3145 list = list->next;
3146 }
3147 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3148 ctxt->state =