56a3344a2fe1a198d23a11fdf6678b58b6719d85
[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
2092 if (arg1 == NULL)
2093 arg1 = BAD_CAST "";
2094 if (arg2 == NULL)
2095 arg2 = BAD_CAST "";
2096
2097 msg[0] = 0;
2098 switch (err) {
2099 case XML_RELAXNG_OK:
2100 return (NULL);
2101 case XML_RELAXNG_ERR_MEMORY:
2102 return (xmlCharStrdup("out of memory\n"));
2103 case XML_RELAXNG_ERR_TYPE:
2104 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2105 break;
2106 case XML_RELAXNG_ERR_TYPEVAL:
2107 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2108 arg2);
2109 break;
2110 case XML_RELAXNG_ERR_DUPID:
2111 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2112 break;
2113 case XML_RELAXNG_ERR_TYPECMP:
2114 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2115 break;
2116 case XML_RELAXNG_ERR_NOSTATE:
2117 return (xmlCharStrdup("Internal error: no state\n"));
2118 case XML_RELAXNG_ERR_NODEFINE:
2119 return (xmlCharStrdup("Internal error: no define\n"));
2120 case XML_RELAXNG_ERR_INTERNAL:
2121 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2122 break;
2123 case XML_RELAXNG_ERR_LISTEXTRA:
2124 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2125 break;
2126 case XML_RELAXNG_ERR_INTERNODATA:
2127 return (xmlCharStrdup
2128 ("Internal: interleave block has no data\n"));
2129 case XML_RELAXNG_ERR_INTERSEQ:
2130 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2131 case XML_RELAXNG_ERR_INTEREXTRA:
2132 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2133 break;
2134 case XML_RELAXNG_ERR_ELEMNAME:
2135 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2136 arg2);
2137 break;
2138 case XML_RELAXNG_ERR_ELEMNONS:
2139 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2140 arg1);
2141 break;
2142 case XML_RELAXNG_ERR_ELEMWRONGNS:
2143 snprintf(msg, 1000,
2144 "Element %s has wrong namespace: expecting %s\n", arg1,
2145 arg2);
2146 break;
2147 case XML_RELAXNG_ERR_ELEMWRONG:
2148 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_TEXTWRONG:
2151 snprintf(msg, 1000,
2152 "Did not expect text in element %s content\n", arg1);
2153 break;
2154 case XML_RELAXNG_ERR_ELEMEXTRANS:
2155 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2156 arg1);
2157 break;
2158 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2159 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2160 break;
2161 case XML_RELAXNG_ERR_NOELEM:
2162 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2163 arg1);
2164 break;
2165 case XML_RELAXNG_ERR_NOTELEM:
2166 return (xmlCharStrdup("Expecting an element got text\n"));
2167 case XML_RELAXNG_ERR_ATTRVALID:
2168 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2169 arg1);
2170 break;
2171 case XML_RELAXNG_ERR_CONTENTVALID:
2172 snprintf(msg, 1000, "Element %s failed to validate content\n",
2173 arg1);
2174 break;
2175 case XML_RELAXNG_ERR_EXTRACONTENT:
2176 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2177 arg1, arg2);
2178 break;
2179 case XML_RELAXNG_ERR_INVALIDATTR:
2180 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2181 arg1, arg2);
2182 break;
2183 case XML_RELAXNG_ERR_LACKDATA:
2184 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_DATAELEM:
2188 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_VALELEM:
2192 snprintf(msg, 1000, "Value element %s has child elements\n",
2193 arg1);
2194 break;
2195 case XML_RELAXNG_ERR_LISTELEM:
2196 snprintf(msg, 1000, "List element %s has child elements\n",
2197 arg1);
2198 break;
2199 case XML_RELAXNG_ERR_DATATYPE:
2200 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2201 break;
2202 case XML_RELAXNG_ERR_VALUE:
2203 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2204 break;
2205 case XML_RELAXNG_ERR_LIST:
2206 return (xmlCharStrdup("Error validating list\n"));
2207 case XML_RELAXNG_ERR_NOGRAMMAR:
2208 return (xmlCharStrdup("No top grammar defined\n"));
2209 case XML_RELAXNG_ERR_EXTRADATA:
2210 return (xmlCharStrdup("Extra data in the document\n"));
2211 default:
2212 return (xmlCharStrdup("Unknown error !\n"));
2213 }
2214 if (msg[0] == 0) {
2215 snprintf(msg, 1000, "Unknown error code %d\n", err);
2216 }
2217 msg[1000 - 1] = 0;
2218 xmlChar *result = xmlCharStrdup(msg);
2219 return (xmlEscapeFormatString(&result));
2220 }
2221
2222 /**
2223 * xmlRelaxNGShowValidError:
2224 * @ctxt: the validation context
2225 * @err: the error number
2226 * @node: the node
2227 * @child: the node child generating the problem.
2228 * @arg1: the first argument
2229 * @arg2: the second argument
2230 *
2231 * Show a validation error.
2232 */
2233 static void
2234 xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2235 xmlRelaxNGValidErr err, xmlNodePtr node,
2236 xmlNodePtr child, const xmlChar * arg1,
2237 const xmlChar * arg2)
2238 {
2239 xmlChar *msg;
2240
2241 if (ctxt->flags & FLAGS_NOERROR)
2242 return;
2243
2244 #ifdef DEBUG_ERROR
2245 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
2246 #endif
2247 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2248 if (msg == NULL)
2249 return;
2250
2251 if (ctxt->errNo == XML_RELAXNG_OK)
2252 ctxt->errNo = err;
2253 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2254 (const char *) msg, arg1, arg2);
2255 xmlFree(msg);
2256 }
2257
2258 /**
2259 * xmlRelaxNGPopErrors:
2260 * @ctxt: the validation context
2261 * @level: the error level in the stack
2262 *
2263 * pop and discard all errors until the given level is reached
2264 */
2265 static void
2266 xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2267 {
2268 int i;
2269 xmlRelaxNGValidErrorPtr err;
2270
2271 #ifdef DEBUG_ERROR
2272 xmlGenericError(xmlGenericErrorContext,
2273 "Pop errors till level %d\n", level);
2274 #endif
2275 for (i = level; i < ctxt->errNr; i++) {
2276 err = &ctxt->errTab[i];
2277 if (err->flags & ERROR_IS_DUP) {
2278 if (err->arg1 != NULL)
2279 xmlFree((xmlChar *) err->arg1);
2280 err->arg1 = NULL;
2281 if (err->arg2 != NULL)
2282 xmlFree((xmlChar *) err->arg2);
2283 err->arg2 = NULL;
2284 err->flags = 0;
2285 }
2286 }
2287 ctxt->errNr = level;
2288 if (ctxt->errNr <= 0)
2289 ctxt->err = NULL;
2290 }
2291
2292 /**
2293 * xmlRelaxNGDumpValidError:
2294 * @ctxt: the validation context
2295 *
2296 * Show all validation error over a given index.
2297 */
2298 static void
2299 xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2300 {
2301 int i, j, k;
2302 xmlRelaxNGValidErrorPtr err, dup;
2303
2304 #ifdef DEBUG_ERROR
2305 xmlGenericError(xmlGenericErrorContext,
2306 "Dumping error stack %d errors\n", ctxt->errNr);
2307 #endif
2308 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2309 err = &ctxt->errTab[i];
2310 if (k < MAX_ERROR) {
2311 for (j = 0; j < i; j++) {
2312 dup = &ctxt->errTab[j];
2313 if ((err->err == dup->err) && (err->node == dup->node) &&
2314 (xmlStrEqual(err->arg1, dup->arg1)) &&
2315 (xmlStrEqual(err->arg2, dup->arg2))) {
2316 goto skip;
2317 }
2318 }
2319 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2320 err->arg1, err->arg2);
2321 k++;
2322 }
2323 skip:
2324 if (err->flags & ERROR_IS_DUP) {
2325 if (err->arg1 != NULL)
2326 xmlFree((xmlChar *) err->arg1);
2327 err->arg1 = NULL;
2328 if (err->arg2 != NULL)
2329 xmlFree((xmlChar *) err->arg2);
2330 err->arg2 = NULL;
2331 err->flags = 0;
2332 }
2333 }
2334 ctxt->errNr = 0;
2335 }
2336
2337 /**
2338 * xmlRelaxNGAddValidError:
2339 * @ctxt: the validation context
2340 * @err: the error number
2341 * @arg1: the first argument
2342 * @arg2: the second argument
2343 * @dup: need to dup the args
2344 *
2345 * Register a validation error, either generating it if it's sure
2346 * or stacking it for later handling if unsure.
2347 */
2348 static void
2349 xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2350 xmlRelaxNGValidErr err, const xmlChar * arg1,
2351 const xmlChar * arg2, int dup)
2352 {
2353 if (ctxt == NULL)
2354 return;
2355 if (ctxt->flags & FLAGS_NOERROR)
2356 return;
2357
2358 #ifdef DEBUG_ERROR
2359 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
2360 #endif
2361 /*
2362 * generate the error directly
2363 */
2364 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2365 (ctxt->flags & FLAGS_NEGATIVE)) {
2366 xmlNodePtr node, seq;
2367
2368 /*
2369 * Flush first any stacked error which might be the
2370 * real cause of the problem.
2371 */
2372 if (ctxt->errNr != 0)
2373 xmlRelaxNGDumpValidError(ctxt);
2374 if (ctxt->state != NULL) {
2375 node = ctxt->state->node;
2376 seq = ctxt->state->seq;
2377 } else {
2378 node = seq = NULL;
2379 }
2380 if ((node == NULL) && (seq == NULL)) {
2381 node = ctxt->pnode;
2382 }
2383 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2384 }
2385 /*
2386 * Stack the error for later processing if needed
2387 */
2388 else {
2389 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2390 }
2391 }
2392
2393
2394 /************************************************************************
2395 * *
2396 * Type library hooks *
2397 * *
2398 ************************************************************************/
2399 static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2400 const xmlChar * str);
2401
2402 /**
2403 * xmlRelaxNGSchemaTypeHave:
2404 * @data: data needed for the library
2405 * @type: the type name
2406 *
2407 * Check if the given type is provided by
2408 * the W3C XMLSchema Datatype library.
2409 *
2410 * Returns 1 if yes, 0 if no and -1 in case of error.
2411 */
2412 static int
2413 xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2414 {
2415 xmlSchemaTypePtr typ;
2416
2417 if (type == NULL)
2418 return (-1);
2419 typ = xmlSchemaGetPredefinedType(type,
2420 BAD_CAST
2421 "http://www.w3.org/2001/XMLSchema");
2422 if (typ == NULL)
2423 return (0);
2424 return (1);
2425 }
2426
2427 /**
2428 * xmlRelaxNGSchemaTypeCheck:
2429 * @data: data needed for the library
2430 * @type: the type name
2431 * @value: the value to check
2432 * @node: the node
2433 *
2434 * Check if the given type and value are validated by
2435 * the W3C XMLSchema Datatype library.
2436 *
2437 * Returns 1 if yes, 0 if no and -1 in case of error.
2438 */
2439 static int
2440 xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2441 const xmlChar * type,
2442 const xmlChar * value,
2443 void **result, xmlNodePtr node)
2444 {
2445 xmlSchemaTypePtr typ;
2446 int ret;
2447
2448 if ((type == NULL) || (value == NULL))
2449 return (-1);
2450 typ = xmlSchemaGetPredefinedType(type,
2451 BAD_CAST
2452 "http://www.w3.org/2001/XMLSchema");
2453 if (typ == NULL)
2454 return (-1);
2455 ret = xmlSchemaValPredefTypeNode(typ, value,
2456 (xmlSchemaValPtr *) result, node);
2457 if (ret == 2) /* special ID error code */
2458 return (2);
2459 if (ret == 0)
2460 return (1);
2461 if (ret > 0)
2462 return (0);
2463 return (-1);
2464 }
2465
2466 /**
2467 * xmlRelaxNGSchemaFacetCheck:
2468 * @data: data needed for the library
2469 * @type: the type name
2470 * @facet: the facet name
2471 * @val: the facet value
2472 * @strval: the string value
2473 * @value: the value to check
2474 *
2475 * Function provided by a type library to check a value facet
2476 *
2477 * Returns 1 if yes, 0 if no and -1 in case of error.
2478 */
2479 static int
2480 xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2481 const xmlChar * type, const xmlChar * facetname,
2482 const xmlChar * val, const xmlChar * strval,
2483 void *value)
2484 {
2485 xmlSchemaFacetPtr facet;
2486 xmlSchemaTypePtr typ;
2487 int ret;
2488
2489 if ((type == NULL) || (strval == NULL))
2490 return (-1);
2491 typ = xmlSchemaGetPredefinedType(type,
2492 BAD_CAST
2493 "http://www.w3.org/2001/XMLSchema");
2494 if (typ == NULL)
2495 return (-1);
2496
2497 facet = xmlSchemaNewFacet();
2498 if (facet == NULL)
2499 return (-1);
2500
2501 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2502 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2503 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2504 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2505 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2506 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2507 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2508 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2509 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2510 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2511 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2512 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2513 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2514 facet->type = XML_SCHEMA_FACET_PATTERN;
2515 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2516 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2517 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2518 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2519 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2520 facet->type = XML_SCHEMA_FACET_LENGTH;
2521 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2522 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2523 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2524 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2525 } else {
2526 xmlSchemaFreeFacet(facet);
2527 return (-1);
2528 }
2529 facet->value = val;
2530 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2531 if (ret != 0) {
2532 xmlSchemaFreeFacet(facet);
2533 return (-1);
2534 }
2535 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2536 xmlSchemaFreeFacet(facet);
2537 if (ret != 0)
2538 return (-1);
2539 return (0);
2540 }
2541
2542 /**
2543 * xmlRelaxNGSchemaFreeValue:
2544 * @data: data needed for the library
2545 * @value: the value to free
2546 *
2547 * Function provided by a type library to free a Schemas value
2548 *
2549 * Returns 1 if yes, 0 if no and -1 in case of error.
2550 */
2551 static void
2552 xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2553 {
2554 xmlSchemaFreeValue(value);
2555 }
2556
2557 /**
2558 * xmlRelaxNGSchemaTypeCompare:
2559 * @data: data needed for the library
2560 * @type: the type name
2561 * @value1: the first value
2562 * @value2: the second value
2563 *
2564 * Compare two values for equality accordingly a type from the W3C XMLSchema
2565 * Datatype library.
2566 *
2567 * Returns 1 if equal, 0 if no and -1 in case of error.
2568 */
2569 static int
2570 xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2571 const xmlChar * type,
2572 const xmlChar * value1,
2573 xmlNodePtr ctxt1,
2574 void *comp1,
2575 const xmlChar * value2, xmlNodePtr ctxt2)
2576 {
2577 int ret;
2578 xmlSchemaTypePtr typ;
2579 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2580
2581 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2582 return (-1);
2583 typ = xmlSchemaGetPredefinedType(type,
2584 BAD_CAST
2585 "http://www.w3.org/2001/XMLSchema");
2586 if (typ == NULL)
2587 return (-1);
2588 if (comp1 == NULL) {
2589 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2590 if (ret != 0)
2591 return (-1);
2592 if (res1 == NULL)
2593 return (-1);
2594 } else {
2595 res1 = (xmlSchemaValPtr) comp1;
2596 }
2597 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2598 if (ret != 0) {
2599 if (res1 != (xmlSchemaValPtr) comp1)
2600 xmlSchemaFreeValue(res1);
2601 return (-1);
2602 }
2603 ret = xmlSchemaCompareValues(res1, res2);
2604 if (res1 != (xmlSchemaValPtr) comp1)
2605 xmlSchemaFreeValue(res1);
2606 xmlSchemaFreeValue(res2);
2607 if (ret == -2)
2608 return (-1);
2609 if (ret == 0)
2610 return (1);
2611 return (0);
2612 }
2613
2614 /**
2615 * xmlRelaxNGDefaultTypeHave:
2616 * @data: data needed for the library
2617 * @type: the type name
2618 *
2619 * Check if the given type is provided by
2620 * the default datatype library.
2621 *
2622 * Returns 1 if yes, 0 if no and -1 in case of error.
2623 */
2624 static int
2625 xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2626 const xmlChar * type)
2627 {
2628 if (type == NULL)
2629 return (-1);
2630 if (xmlStrEqual(type, BAD_CAST "string"))
2631 return (1);
2632 if (xmlStrEqual(type, BAD_CAST "token"))
2633 return (1);
2634 return (0);
2635 }
2636
2637 /**
2638 * xmlRelaxNGDefaultTypeCheck:
2639 * @data: data needed for the library
2640 * @type: the type name
2641 * @value: the value to check
2642 * @node: the node
2643 *
2644 * Check if the given type and value are validated by
2645 * the default datatype library.
2646 *
2647 * Returns 1 if yes, 0 if no and -1 in case of error.
2648 */
2649 static int
2650 xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2651 const xmlChar * type ATTRIBUTE_UNUSED,
2652 const xmlChar * value ATTRIBUTE_UNUSED,
2653 void **result ATTRIBUTE_UNUSED,
2654 xmlNodePtr node ATTRIBUTE_UNUSED)
2655 {
2656 if (value == NULL)
2657 return (-1);
2658 if (xmlStrEqual(type, BAD_CAST "string"))
2659 return (1);
2660 if (xmlStrEqual(type, BAD_CAST "token")) {
2661 return (1);
2662 }
2663
2664 return (0);
2665 }
2666
2667 /**
2668 * xmlRelaxNGDefaultTypeCompare:
2669 * @data: data needed for the library
2670 * @type: the type name
2671 * @value1: the first value
2672 * @value2: the second value
2673 *
2674 * Compare two values accordingly a type from the default
2675 * datatype library.
2676 *
2677 * Returns 1 if yes, 0 if no and -1 in case of error.
2678 */
2679 static int
2680 xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2681 const xmlChar * type,
2682 const xmlChar * value1,
2683 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2684 void *comp1 ATTRIBUTE_UNUSED,
2685 const xmlChar * value2,
2686 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2687 {
2688 int ret = -1;
2689
2690 if (xmlStrEqual(type, BAD_CAST "string")) {
2691 ret = xmlStrEqual(value1, value2);
2692 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2693 if (!xmlStrEqual(value1, value2)) {
2694 xmlChar *nval, *nvalue;
2695
2696 /*
2697 * TODO: trivial optimizations are possible by
2698 * computing at compile-time
2699 */
2700 nval = xmlRelaxNGNormalize(NULL, value1);
2701 nvalue = xmlRelaxNGNormalize(NULL, value2);
2702
2703 if ((nval == NULL) || (nvalue == NULL))
2704 ret = -1;
2705 else if (xmlStrEqual(nval, nvalue))
2706 ret = 1;
2707 else
2708 ret = 0;
2709 if (nval != NULL)
2710 xmlFree(nval);
2711 if (nvalue != NULL)
2712 xmlFree(nvalue);
2713 } else
2714 ret = 1;
2715 }
2716 return (ret);
2717 }
2718
2719 static int xmlRelaxNGTypeInitialized = 0;
2720 static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2721
2722 /**
2723 * xmlRelaxNGFreeTypeLibrary:
2724 * @lib: the type library structure
2725 * @namespace: the URI bound to the library
2726 *
2727 * Free the structure associated to the type library
2728 */
2729 static void
2730 xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2731 const xmlChar * namespace ATTRIBUTE_UNUSED)
2732 {
2733 if (lib == NULL)
2734 return;
2735 if (lib->namespace != NULL)
2736 xmlFree((xmlChar *) lib->namespace);
2737 xmlFree(lib);
2738 }
2739
2740 /**
2741 * xmlRelaxNGRegisterTypeLibrary:
2742 * @namespace: the URI bound to the library
2743 * @data: data associated to the library
2744 * @have: the provide function
2745 * @check: the checking function
2746 * @comp: the comparison function
2747 *
2748 * Register a new type library
2749 *
2750 * Returns 0 in case of success and -1 in case of error.
2751 */
2752 static int
2753 xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2754 xmlRelaxNGTypeHave have,
2755 xmlRelaxNGTypeCheck check,
2756 xmlRelaxNGTypeCompare comp,
2757 xmlRelaxNGFacetCheck facet,
2758 xmlRelaxNGTypeFree freef)
2759 {
2760 xmlRelaxNGTypeLibraryPtr lib;
2761 int ret;
2762
2763 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2764 (check == NULL) || (comp == NULL))
2765 return (-1);
2766 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2767 xmlGenericError(xmlGenericErrorContext,
2768 "Relax-NG types library '%s' already registered\n",
2769 namespace);
2770 return (-1);
2771 }
2772 lib =
2773 (xmlRelaxNGTypeLibraryPtr)
2774 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2775 if (lib == NULL) {
2776 xmlRngVErrMemory(NULL, "adding types library\n");
2777 return (-1);
2778 }
2779 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2780 lib->namespace = xmlStrdup(namespace);
2781 lib->data = data;
2782 lib->have = have;
2783 lib->comp = comp;
2784 lib->check = check;
2785 lib->facet = facet;
2786 lib->freef = freef;
2787 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2788 if (ret < 0) {
2789 xmlGenericError(xmlGenericErrorContext,
2790 "Relax-NG types library failed to register '%s'\n",
2791 namespace);
2792 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2793 return (-1);
2794 }
2795 return (0);
2796 }
2797
2798 /**
2799 * xmlRelaxNGInitTypes:
2800 *
2801 * Initilize the default type libraries.
2802 *
2803 * Returns 0 in case of success and -1 in case of error.
2804 */
2805 int
2806 xmlRelaxNGInitTypes(void)
2807 {
2808 if (xmlRelaxNGTypeInitialized != 0)
2809 return (0);
2810 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2811 if (xmlRelaxNGRegisteredTypes == NULL) {
2812 xmlGenericError(xmlGenericErrorContext,
2813 "Failed to allocate sh table for Relax-NG types\n");
2814 return (-1);
2815 }
2816 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2817 "http://www.w3.org/2001/XMLSchema-datatypes",
2818 NULL, xmlRelaxNGSchemaTypeHave,
2819 xmlRelaxNGSchemaTypeCheck,
2820 xmlRelaxNGSchemaTypeCompare,
2821 xmlRelaxNGSchemaFacetCheck,
2822 xmlRelaxNGSchemaFreeValue);
2823 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2824 xmlRelaxNGDefaultTypeHave,
2825 xmlRelaxNGDefaultTypeCheck,
2826 xmlRelaxNGDefaultTypeCompare, NULL,
2827 NULL);
2828 xmlRelaxNGTypeInitialized = 1;
2829 return (0);
2830 }
2831
2832 /**
2833 * xmlRelaxNGCleanupTypes:
2834 *
2835 * Cleanup the default Schemas type library associated to RelaxNG
2836 */
2837 void
2838 xmlRelaxNGCleanupTypes(void)
2839 {
2840 xmlSchemaCleanupTypes();
2841 if (xmlRelaxNGTypeInitialized == 0)
2842 return;
2843 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2844 xmlRelaxNGFreeTypeLibrary);
2845 xmlRelaxNGTypeInitialized = 0;
2846 }
2847
2848 /************************************************************************
2849 * *
2850 * Compiling element content into regexp *
2851 * *
2852 * Sometime the element content can be compiled into a pure regexp, *
2853 * This allows a faster execution and streamability at that level *
2854 * *
2855 ************************************************************************/
2856
2857 /* from automata.c but not exported */
2858 void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2859
2860
2861 static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2862 xmlRelaxNGDefinePtr def);
2863
2864 /**
2865 * xmlRelaxNGIsCompileable:
2866 * @define: the definition to check
2867 *
2868 * Check if a definition is nullable.
2869 *
2870 * Returns 1 if yes, 0 if no and -1 in case of error
2871 */
2872 static int
2873 xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2874 {
2875 int ret = -1;
2876
2877 if (def == NULL) {
2878 return (-1);
2879 }
2880 if ((def->type != XML_RELAXNG_ELEMENT) &&
2881 (def->dflags & IS_COMPILABLE))
2882 return (1);
2883 if ((def->type != XML_RELAXNG_ELEMENT) &&
2884 (def->dflags & IS_NOT_COMPILABLE))
2885 return (0);
2886 switch (def->type) {
2887 case XML_RELAXNG_NOOP:
2888 ret = xmlRelaxNGIsCompileable(def->content);
2889 break;
2890 case XML_RELAXNG_TEXT:
2891 case XML_RELAXNG_EMPTY:
2892 ret = 1;
2893 break;
2894 case XML_RELAXNG_ELEMENT:
2895 /*
2896 * Check if the element content is compileable
2897 */
2898 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2899 ((def->dflags & IS_COMPILABLE) == 0)) {
2900 xmlRelaxNGDefinePtr list;
2901
2902 list = def->content;
2903 while (list != NULL) {
2904 ret = xmlRelaxNGIsCompileable(list);
2905 if (ret != 1)
2906 break;
2907 list = list->next;
2908 }
2909 /*
2910 * Because the routine is recursive, we must guard against
2911 * discovering both COMPILABLE and NOT_COMPILABLE
2912 */
2913 if (ret == 0) {
2914 def->dflags &= ~IS_COMPILABLE;
2915 def->dflags |= IS_NOT_COMPILABLE;
2916 }
2917 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2918 def->dflags |= IS_COMPILABLE;
2919 #ifdef DEBUG_COMPILE
2920 if (ret == 1) {
2921 xmlGenericError(xmlGenericErrorContext,
2922 "element content for %s is compilable\n",
2923 def->name);
2924 } else if (ret == 0) {
2925 xmlGenericError(xmlGenericErrorContext,
2926 "element content for %s is not compilable\n",
2927 def->name);
2928 } else {
2929 xmlGenericError(xmlGenericErrorContext,
2930 "Problem in RelaxNGIsCompileable for element %s\n",
2931 def->name);
2932 }
2933 #endif
2934 }
2935 /*
2936 * All elements return a compileable status unless they
2937 * are generic like anyName
2938 */
2939 if ((def->nameClass != NULL) || (def->name == NULL))
2940 ret = 0;
2941 else
2942 ret = 1;
2943 return (ret);
2944 case XML_RELAXNG_REF:
2945 case XML_RELAXNG_EXTERNALREF:
2946 case XML_RELAXNG_PARENTREF:
2947 if (def->depth == -20) {
2948 return (1);
2949 } else {
2950 xmlRelaxNGDefinePtr list;
2951
2952 def->depth = -20;
2953 list = def->content;
2954 while (list != NULL) {
2955 ret = xmlRelaxNGIsCompileable(list);
2956 if (ret != 1)
2957 break;
2958 list = list->next;
2959 }
2960 }
2961 break;
2962 case XML_RELAXNG_START:
2963 case XML_RELAXNG_OPTIONAL:
2964 case XML_RELAXNG_ZEROORMORE:
2965 case XML_RELAXNG_ONEORMORE:
2966 case XML_RELAXNG_CHOICE:
2967 case XML_RELAXNG_GROUP:
2968 case XML_RELAXNG_DEF:{
2969 xmlRelaxNGDefinePtr list;
2970
2971 list = def->content;
2972 while (list != NULL) {
2973 ret = xmlRelaxNGIsCompileable(list);
2974 if (ret != 1)
2975 break;
2976 list = list->next;
2977 }
2978 break;
2979 }
2980 case XML_RELAXNG_EXCEPT:
2981 case XML_RELAXNG_ATTRIBUTE:
2982 case XML_RELAXNG_INTERLEAVE:
2983 case XML_RELAXNG_DATATYPE:
2984 case XML_RELAXNG_LIST:
2985 case XML_RELAXNG_PARAM:
2986 case XML_RELAXNG_VALUE:
2987 case XML_RELAXNG_NOT_ALLOWED:
2988 ret = 0;
2989 break;
2990 }
2991 if (ret == 0)
2992 def->dflags |= IS_NOT_COMPILABLE;
2993 if (ret == 1)
2994 def->dflags |= IS_COMPILABLE;
2995 #ifdef DEBUG_COMPILE
2996 if (ret == 1) {
2997 xmlGenericError(xmlGenericErrorContext,
2998 "RelaxNGIsCompileable %s : true\n",
2999 xmlRelaxNGDefName(def));
3000 } else if (ret == 0) {
3001 xmlGenericError(xmlGenericErrorContext,
3002 "RelaxNGIsCompileable %s : false\n",
3003 xmlRelaxNGDefName(def));
3004 } else {
3005 xmlGenericError(xmlGenericErrorContext,
3006 "Problem in RelaxNGIsCompileable %s\n",
3007 xmlRelaxNGDefName(def));
3008 }
3009 #endif
3010 return (ret);
3011 }
3012
3013 /**
3014 * xmlRelaxNGCompile:
3015 * ctxt: the RelaxNG parser context
3016 * @define: the definition tree to compile
3017 *
3018 * Compile the set of definitions, it works recursively, till the
3019 * element boundaries, where it tries to compile the content if possible
3020 *
3021 * Returns 0 if success and -1 in case of error
3022 */
3023 static int
3024 xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3025 {
3026 int ret = 0;
3027 xmlRelaxNGDefinePtr list;
3028
3029 if ((ctxt == NULL) || (def == NULL))
3030 return (-1);
3031
3032 switch (def->type) {
3033 case XML_RELAXNG_START:
3034 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
3035 xmlAutomataPtr oldam = ctxt->am;
3036 xmlAutomataStatePtr oldstate = ctxt->state;
3037
3038 def->depth = -25;
3039
3040 list = def->content;
3041 ctxt->am = xmlNewAutomata();
3042 if (ctxt->am == NULL)
3043 return (-1);
3044
3045 /*
3046 * assume identical strings but not same pointer are different
3047 * atoms, needed for non-determinism detection
3048 * That way if 2 elements with the same name are in a choice
3049 * branch the automata is found non-deterministic and
3050 * we fallback to the normal validation which does the right
3051 * thing of exploring both choices.
3052 */
3053 xmlAutomataSetFlags(ctxt->am, 1);
3054
3055 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3056 while (list != NULL) {
3057 xmlRelaxNGCompile(ctxt, list);
3058 list = list->next;
3059 }
3060 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3061 if (xmlAutomataIsDeterminist(ctxt->am))
3062 def->contModel = xmlAutomataCompile(ctxt->am);
3063
3064 xmlFreeAutomata(ctxt->am);
3065 ctxt->state = oldstate;
3066 ctxt->am = oldam;
3067 }
3068 break;
3069 case XML_RELAXNG_ELEMENT:
3070 if ((ctxt->am != NULL) && (def->name != NULL)) {
3071 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3072 ctxt->state, NULL,
3073 def->name, def->ns,
3074 def);
3075 }
3076 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3077 xmlAutomataPtr oldam = ctxt->am;
3078 xmlAutomataStatePtr oldstate = ctxt->state;
3079
3080 def->depth = -25;
3081
3082 list = def->content;
3083 ctxt->am = xmlNewAutomata();
3084 if (ctxt->am == NULL)
3085 return (-1);
3086 xmlAutomataSetFlags(ctxt->am, 1);
3087 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3088 while (list != NULL) {
3089 xmlRelaxNGCompile(ctxt, list);
3090 list = list->next;
3091 }
3092 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3093 def->contModel = xmlAutomataCompile(ctxt->am);
3094 if (!xmlRegexpIsDeterminist(def->contModel)) {
3095 #ifdef DEBUG_COMPILE
3096 xmlGenericError(xmlGenericErrorContext,
3097 "Content model not determinist %s\n",
3098 def->name);
3099 #endif
3100 /*
3101 * we can only use the automata if it is determinist
3102 */
3103 xmlRegFreeRegexp(def->contModel);
3104 def->contModel = NULL;
3105 }
3106 xmlFreeAutomata(ctxt->am);
3107 ctxt->state = oldstate;
3108 ctxt->am = oldam;
3109 } else {
3110 xmlAutomataPtr oldam = ctxt->am;
3111
3112 /*
3113 * we can't build the content model for this element content
3114 * but it still might be possible to build it for some of its
3115 * children, recurse.
3116 */
3117 ret = xmlRelaxNGTryCompile(ctxt, def);
3118 ctxt->am = oldam;
3119 }
3120 break;
3121 case XML_RELAXNG_NOOP:
3122 ret = xmlRelaxNGCompile(ctxt, def->content);
3123 break;
3124 case XML_RELAXNG_OPTIONAL:{
3125 xmlAutomataStatePtr oldstate = ctxt->state;
3126
3127 list = def->content;
3128 while (list != NULL) {
3129 xmlRelaxNGCompile(ctxt, list);
3130 list = list->next;
3131 }
3132 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3133 break;
3134 }
3135 case XML_RELAXNG_ZEROORMORE:{
3136 xmlAutomataStatePtr oldstate;
3137
3138 ctxt->state =
3139 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3140 oldstate = ctxt->state;
3141 list = def->content;
3142 while (list != NULL) {
3143 xmlRelaxNGCompile(ctxt, list);
3144 list = list->next;
3145 }
3146 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3147 ctxt->state =
3148 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3149 break;
3150 }
3151 case XML_RELAXNG_ONEORMORE:{
3152 xmlAutomataStatePtr oldstate;
3153
3154 list = def->content;
3155 while (list != NULL) {
3156 xmlRelaxNGCompile(ctxt, list);
3157 list = list->next;
3158 }
3159 oldstate = ctxt->state;
3160 list = def->content;
3161 while (list != NULL) {
3162 xmlRelaxNGCompile(ctxt, list);
3163 list = list->next;
3164 }
3165 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3166 ctxt->state =
3167 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3168 break;
3169 }
3170 case XML_RELAXNG_CHOICE:{
3171 xmlAutomataStatePtr target = NULL;
3172 xmlAutomataStatePtr oldstate = ctxt->state;
3173
3174 list = def->content;
3175 while (list != NULL) {
3176 ctxt->state = oldstate;
3177 ret = xmlRelaxNGCompile(ctxt, list);
3178 if (ret != 0)
3179 break;
3180 if (target == NULL)
3181 target = ctxt->state;
3182 else {
3183 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3184 target);
3185 }
3186 list = list->next;
3187 }
3188 ctxt->state = target;
3189
3190 break;
3191 }
3192 case XML_RELAXNG_REF:
3193 case XML_RELAXNG_EXTERNALREF:
3194 case XML_RELAXNG_PARENTREF:
3195 case XML_RELAXNG_GROUP:
3196 case XML_RELAXNG_DEF:
3197 list = def->content;
3198 while (list != NULL) {
3199 ret = xmlRelaxNGCompile(ctxt, list);
3200 if (ret != 0)
3201 break;
3202 list = list->next;
3203 }
3204 break;
3205 case XML_RELAXNG_TEXT:{
3206 xmlAutomataStatePtr oldstate;
3207
3208 ctxt->state =
3209 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3210 oldstate = ctxt->state;
3211 xmlRelaxNGCompile(ctxt, def->content);
3212 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3213 ctxt->state, BAD_CAST "#text",
3214 NULL);
3215 ctxt->state =
3216 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3217 break;
3218 }
3219 case XML_RELAXNG_EMPTY:
3220 ctxt->state =
3221 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3222 break;
3223 case XML_RELAXNG_EXCEPT:
3224 case XML_RELAXNG_ATTRIBUTE:
3225 case XML_RELAXNG_INTERLEAVE:
3226 case XML_RELAXNG_NOT_ALLOWED:
3227 case XML_RELAXNG_DATATYPE:
3228 case XML_RELAXNG_LIST:
3229 case XML_RELAXNG_PARAM:
3230 case XML_RELAXNG_VALUE:
3231 /* This should not happen and generate an internal error */
3232 fprintf(stderr, "RNG internal error trying to compile %s\n",
3233 xmlRelaxNGDefName(def));
3234 break;
3235 }
3236 return (ret);
3237 }
3238
3239 /**
3240 * xmlRelaxNGTryCompile:
3241 * ctxt: the RelaxNG parser context
3242 * @define: the definition tree to compile
3243 *
3244 * Try to compile the set of definitions, it works recursively,
3245 * possibly ignoring parts which cannot be compiled.
3246 *
3247 * Returns 0 if success and -1 in case of error
3248 */
3249 static int
3250 xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3251 {
3252 int ret = 0;
3253 xmlRelaxNGDefinePtr list;
3254
3255 if ((ctxt == NULL) || (def == NULL))
3256 return (-1);
3257
3258 if ((def->type == XML_RELAXNG_START) ||
3259 (def->type == XML_RELAXNG_ELEMENT)) {
3260 ret = xmlRelaxNGIsCompileable(def);
3261 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3262 ctxt->am = NULL;
3263 ret = xmlRelaxNGCompile(ctxt, def);
3264 #ifdef DEBUG_PROGRESSIVE
3265 if (ret == 0) {
3266 if (def->type == XML_RELAXNG_START)
3267 xmlGenericError(xmlGenericErrorContext,
3268 "compiled the start\n");
3269 else
3270 xmlGenericError(xmlGenericErrorContext,
3271 "compiled element %s\n", def->name);
3272 } else {
3273 if (def->type == XML_RELAXNG_START)
3274 xmlGenericError(xmlGenericErrorContext,
3275 "failed to compile the start\n");
3276 else
3277 xmlGenericError(xmlGenericErrorContext,
3278 "failed to compile element %s\n",
3279 def->name);
3280 }
3281 #endif
3282 return (ret);
3283 }
3284 }
3285 switch (def->type) {
3286 case XML_RELAXNG_NOOP:
3287 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3288 break;
3289 case XML_RELAXNG_TEXT:
3290 case XML_RELAXNG_DATATYPE:
3291 case XML_RELAXNG_LIST:
3292 case XML_RELAXNG_PARAM:
3293 case XML_RELAXNG_VALUE:
3294 case XML_RELAXNG_EMPTY:
3295 case XML_RELAXNG_ELEMENT:
3296 ret = 0;
3297 break;
3298 case XML_RELAXNG_OPTIONAL:
3299 case XML_RELAXNG_ZEROORMORE:
3300 case XML_RELAXNG_ONEORMORE:
3301 case XML_RELAXNG_CHOICE:
3302 case XML_RELAXNG_GROUP:
3303 case XML_RELAXNG_DEF:
3304 case XML_RELAXNG_START:
3305 case XML_RELAXNG_REF:
3306 case XML_RELAXNG_EXTERNALREF:
3307 case XML_RELAXNG_PARENTREF:
3308 list = def->content;
3309 while (list != NULL) {
3310 ret = xmlRelaxNGTryCompile(ctxt, list);
3311 if (ret != 0)
3312 break;
3313 list = list->next;
3314 }
3315 break;
3316 case XML_RELAXNG_EXCEPT:
3317 case XML_RELAXNG_ATTRIBUTE:
3318 case XML_RELAXNG_INTERLEAVE:
3319 case XML_RELAXNG_NOT_ALLOWED:
3320 ret = 0;
3321 break;
3322 }
3323 return (ret);
3324 }
3325
3326 /************************************************************************
3327 * *
3328 * Parsing functions *
3329 * *
3330 ************************************************************************/
3331
3332 static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3333 ctxt, xmlNodePtr node);
3334 static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3335 ctxt, xmlNodePtr node);
3336 static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3337 ctxt, xmlNodePtr nodes,
3338 int group);
3339 static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3340 ctxt, xmlNodePtr node);
3341 static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3342 xmlNodePtr node);
3343 static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3344 xmlNodePtr nodes);
3345 static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3346 ctxt, xmlNodePtr node,
3347 xmlRelaxNGDefinePtr
3348 def);
3349 static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3350 ctxt, xmlNodePtr nodes);
3351 static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3352 xmlRelaxNGDefinePtr define,
3353 xmlNodePtr elem);
3354
3355
3356 #define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3357
3358 /**
3359 * xmlRelaxNGIsNullable:
3360 * @define: the definition to verify
3361 *
3362 * Check if a definition is nullable.
3363 *
3364 * Returns 1 if yes, 0 if no and -1 in case of error
3365 */
3366 static int
3367 xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3368 {
3369 int ret;
3370
3371 if (define == NULL)
3372 return (-1);
3373
3374 if (define->dflags & IS_NULLABLE)
3375 return (1);
3376 if (define->dflags & IS_NOT_NULLABLE)
3377 return (0);
3378 switch (define->type) {
3379 case XML_RELAXNG_EMPTY:
3380 case XML_RELAXNG_TEXT:
3381 ret = 1;
3382 break;
3383 case XML_RELAXNG_NOOP:
3384 case XML_RELAXNG_DEF:
3385 case XML_RELAXNG_REF:
3386 case XML_RELAXNG_EXTERNALREF:
3387 case XML_RELAXNG_PARENTREF:
3388 case XML_RELAXNG_ONEORMORE:
3389 ret = xmlRelaxNGIsNullable(define->content);
3390 break;
3391 case XML_RELAXNG_EXCEPT:
3392 case XML_RELAXNG_NOT_ALLOWED:
3393 case XML_RELAXNG_ELEMENT:
3394 case XML_RELAXNG_DATATYPE:
3395 case XML_RELAXNG_PARAM:
3396 case XML_RELAXNG_VALUE:
3397 case XML_RELAXNG_LIST:
3398 case XML_RELAXNG_ATTRIBUTE:
3399 ret = 0;
3400 break;
3401 case XML_RELAXNG_CHOICE:{
3402 xmlRelaxNGDefinePtr list = define->content;
3403
3404 while (list != NULL) {
3405 ret = xmlRelaxNGIsNullable(list);
3406 if (ret != 0)
3407 goto done;
3408 list = list->next;
3409 }
3410 ret = 0;
3411 break;
3412 }
3413 case XML_RELAXNG_START:
3414 case XML_RELAXNG_INTERLEAVE:
3415 case XML_RELAXNG_GROUP:{
3416 xmlRelaxNGDefinePtr list = define->content;
3417
3418 while (list != NULL) {
3419 ret = xmlRelaxNGIsNullable(list);
3420 if (ret != 1)
3421 goto done;
3422 list = list->next;
3423 }
3424 return (1);
3425 }
3426 default:
3427 return (-1);
3428 }
3429 done:
3430 if (ret == 0)
3431 define->dflags |= IS_NOT_NULLABLE;
3432 if (ret == 1)
3433 define->dflags |= IS_NULLABLE;
3434 return (ret);
3435 }
3436
3437 /**
3438 * xmlRelaxNGIsBlank:
3439 * @str: a string
3440 *
3441 * Check if a string is ignorable c.f. 4.2. Whitespace
3442 *
3443 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3444 */
3445 static int
3446 xmlRelaxNGIsBlank(xmlChar * str)
3447 {
3448 if (str == NULL)
3449 return (1);
3450 while (*str != 0) {
3451 if (!(IS_BLANK_CH(*str)))
3452 return (0);
3453 str++;
3454 }
3455 return (1);
3456 }
3457
3458 /**
3459 * xmlRelaxNGGetDataTypeLibrary:
3460 * @ctxt: a Relax-NG parser context
3461 * @node: the current data or value element
3462 *
3463 * Applies algorithm from 4.3. datatypeLibrary attribute
3464 *
3465 * Returns the datatypeLibary value or NULL if not found
3466 */
3467 static xmlChar *
3468 xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3469 xmlNodePtr node)
3470 {
3471 xmlChar *ret, *escape;
3472
3473 if (node == NULL)
3474 return(NULL);
3475
3476 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3477 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3478 if (ret != NULL) {
3479 if (ret[0] == 0) {
3480 xmlFree(ret);
3481 return (NULL);
3482 }
3483 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3484 if (escape == NULL) {
3485 return (ret);
3486 }
3487 xmlFree(ret);
3488 return (escape);
3489 }
3490 }
3491 node = node->parent;
3492 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3493 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3494 if (ret != NULL) {
3495 if (ret[0] == 0) {
3496 xmlFree(ret);
3497 return (NULL);
3498 }
3499 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3500 if (escape == NULL) {
3501 return (ret);
3502 }
3503 xmlFree(ret);
3504 return (escape);
3505 }
3506 node = node->parent;
3507 }
3508 return (NULL);
3509 }
3510
3511 /**
3512 * xmlRelaxNGParseValue:
3513 * @ctxt: a Relax-NG parser context
3514 * @node: the data node.
3515 *
3516 * parse the content of a RelaxNG value node.
3517 *
3518 * Returns the definition pointer or NULL in case of error
3519 */
3520 static xmlRelaxNGDefinePtr
3521 xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3522 {
3523 xmlRelaxNGDefinePtr def = NULL;
3524 xmlRelaxNGTypeLibraryPtr lib = NULL;
3525 xmlChar *type;
3526 xmlChar *library;
3527 int success = 0;
3528
3529 def = xmlRelaxNGNewDefine(ctxt, node);
3530 if (def == NULL)
3531 return (NULL);
3532 def->type = XML_RELAXNG_VALUE;
3533
3534 type = xmlGetProp(node, BAD_CAST "type");
3535 if (type != NULL) {
3536 xmlRelaxNGNormExtSpace(type);
3537 if (xmlValidateNCName(type, 0)) {
3538 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3539 "value type '%s' is not an NCName\n", type, NULL);
3540 }
3541 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3542 if (library == NULL)
3543 library =
3544 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3545
3546 def->name = type;
3547 def->ns = library;
3548
3549 lib = (xmlRelaxNGTypeLibraryPtr)
3550 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3551 if (lib == NULL) {
3552 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3553 "Use of unregistered type library '%s'\n", library,
3554 NULL);
3555 def->data = NULL;
3556 } else {
3557 def->data = lib;
3558 if (lib->have == NULL) {
3559 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3560 "Internal error with type library '%s': no 'have'\n",
3561 library, NULL);
3562 } else {
3563 success = lib->have(lib->data, def->name);
3564 if (success != 1) {
3565 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3566 "Error type '%s' is not exported by type library '%s'\n",
3567 def->name, library);
3568 }
3569 }
3570 }
3571 }
3572 if (node->children == NULL) {
3573 def->value = xmlStrdup(BAD_CAST "");
3574 } else if (((node->children->type != XML_TEXT_NODE) &&
3575 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3576 (node->children->next != NULL)) {
3577 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3578 "Expecting a single text value for <value>content\n",
3579 NULL, NULL);
3580 } else if (def != NULL) {
3581 def->value = xmlNodeGetContent(node);
3582 if (def->value == NULL) {
3583 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3584 "Element <value> has no content\n", NULL, NULL);
3585 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3586 void *val = NULL;
3587
3588 success =
3589 lib->check(lib->data, def->name, def->value, &val, node);
3590 if (success != 1) {
3591 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3592 "Value '%s' is not acceptable for type '%s'\n",
3593 def->value, def->name);
3594 } else {
3595 if (val != NULL)
3596 def->attrs = val;
3597 }
3598 }
3599 }
3600 return (def);
3601 }
3602
3603 /**
3604 * xmlRelaxNGParseData:
3605 * @ctxt: a Relax-NG parser context
3606 * @node: the data node.
3607 *
3608 * parse the content of a RelaxNG data node.
3609 *
3610 * Returns the definition pointer or NULL in case of error
3611 */
3612 static xmlRelaxNGDefinePtr
3613 xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3614 {
3615 xmlRelaxNGDefinePtr def = NULL, except;
3616 xmlRelaxNGDefinePtr param, lastparam = NULL;
3617 xmlRelaxNGTypeLibraryPtr lib;
3618 xmlChar *type;
3619 xmlChar *library;
3620 xmlNodePtr content;
3621 int tmp;
3622
3623 type = xmlGetProp(node, BAD_CAST "type");
3624 if (type == NULL) {
3625 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3626 NULL);