Coverage Report

Created: 2024-09-06 07:53

/src/libxml2/relaxng.c
Line
Count
Source (jump to first uncovered line)
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 <stddef.h>
24
#include <libxml/xmlmemory.h>
25
#include <libxml/parser.h>
26
#include <libxml/parserInternals.h>
27
#include <libxml/hash.h>
28
#include <libxml/uri.h>
29
30
#include <libxml/relaxng.h>
31
32
#include <libxml/xmlschemastypes.h>
33
#include <libxml/xmlautomata.h>
34
#include <libxml/xmlregexp.h>
35
#include <libxml/xmlschemastypes.h>
36
37
#include "private/error.h"
38
#include "private/regexp.h"
39
#include "private/string.h"
40
41
/*
42
 * The Relax-NG namespace
43
 */
44
static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
45
    "http://relaxng.org/ns/structure/1.0";
46
47
#define IS_RELAXNG(node, typ)           \
48
0
   ((node != NULL) && (node->ns != NULL) &&       \
49
0
    (node->type == XML_ELEMENT_NODE) &&         \
50
0
    (xmlStrEqual(node->name, (const xmlChar *) typ)) &&   \
51
0
    (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
52
53
54
0
#define MAX_ERROR 5
55
56
typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
57
typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
58
59
typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
60
typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
61
62
typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
63
typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
64
65
typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
66
typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
67
68
typedef enum {
69
    XML_RELAXNG_COMBINE_UNDEFINED = 0,  /* undefined */
70
    XML_RELAXNG_COMBINE_CHOICE, /* choice */
71
    XML_RELAXNG_COMBINE_INTERLEAVE      /* interleave */
72
} xmlRelaxNGCombine;
73
74
typedef enum {
75
    XML_RELAXNG_CONTENT_ERROR = -1,
76
    XML_RELAXNG_CONTENT_EMPTY = 0,
77
    XML_RELAXNG_CONTENT_SIMPLE,
78
    XML_RELAXNG_CONTENT_COMPLEX
79
} xmlRelaxNGContentType;
80
81
typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
82
typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
83
84
struct _xmlRelaxNGGrammar {
85
    xmlRelaxNGGrammarPtr parent;        /* the parent grammar if any */
86
    xmlRelaxNGGrammarPtr children;      /* the children grammar if any */
87
    xmlRelaxNGGrammarPtr next;  /* the next grammar if any */
88
    xmlRelaxNGDefinePtr start;  /* <start> content */
89
    xmlRelaxNGCombine combine;  /* the default combine value */
90
    xmlRelaxNGDefinePtr startList;      /* list of <start> definitions */
91
    xmlHashTablePtr defs;       /* define* */
92
    xmlHashTablePtr refs;       /* references */
93
};
94
95
96
typedef enum {
97
    XML_RELAXNG_NOOP = -1,      /* a no operation from simplification  */
98
    XML_RELAXNG_EMPTY = 0,      /* an empty pattern */
99
    XML_RELAXNG_NOT_ALLOWED,    /* not allowed top */
100
    XML_RELAXNG_EXCEPT,         /* except present in nameclass defs */
101
    XML_RELAXNG_TEXT,           /* textual content */
102
    XML_RELAXNG_ELEMENT,        /* an element */
103
    XML_RELAXNG_DATATYPE,       /* external data type definition */
104
    XML_RELAXNG_PARAM,          /* external data type parameter */
105
    XML_RELAXNG_VALUE,          /* value from an external data type definition */
106
    XML_RELAXNG_LIST,           /* a list of patterns */
107
    XML_RELAXNG_ATTRIBUTE,      /* an attribute following a pattern */
108
    XML_RELAXNG_DEF,            /* a definition */
109
    XML_RELAXNG_REF,            /* reference to a definition */
110
    XML_RELAXNG_EXTERNALREF,    /* reference to an external def */
111
    XML_RELAXNG_PARENTREF,      /* reference to a def in the parent grammar */
112
    XML_RELAXNG_OPTIONAL,       /* optional patterns */
113
    XML_RELAXNG_ZEROORMORE,     /* zero or more non empty patterns */
114
    XML_RELAXNG_ONEORMORE,      /* one or more non empty patterns */
115
    XML_RELAXNG_CHOICE,         /* a choice between non empty patterns */
116
    XML_RELAXNG_GROUP,          /* a pair/group of non empty patterns */
117
    XML_RELAXNG_INTERLEAVE,     /* interleaving choice of non-empty patterns */
118
    XML_RELAXNG_START           /* Used to keep track of starts on grammars */
119
} xmlRelaxNGType;
120
121
0
#define IS_NULLABLE   (1 << 0)
122
0
#define IS_NOT_NULLABLE   (1 << 1)
123
0
#define IS_INDETERMINIST  (1 << 2)
124
0
#define IS_MIXED    (1 << 3)
125
0
#define IS_TRIABLE    (1 << 4)
126
0
#define IS_PROCESSED    (1 << 5)
127
0
#define IS_COMPILABLE   (1 << 6)
128
0
#define IS_NOT_COMPILABLE (1 << 7)
129
0
#define IS_EXTERNAL_REF         (1 << 8)
130
131
struct _xmlRelaxNGDefine {
132
    xmlRelaxNGType type;        /* the type of definition */
133
    xmlNodePtr node;            /* the node in the source */
134
    xmlChar *name;              /* the element local name if present */
135
    xmlChar *ns;                /* the namespace local name if present */
136
    xmlChar *value;             /* value when available */
137
    void *data;                 /* data lib or specific pointer */
138
    xmlRelaxNGDefinePtr content;        /* the expected content */
139
    xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
140
    xmlRelaxNGDefinePtr next;   /* list within grouping sequences */
141
    xmlRelaxNGDefinePtr attrs;  /* list of attributes for elements */
142
    xmlRelaxNGDefinePtr nameClass;      /* the nameClass definition if any */
143
    xmlRelaxNGDefinePtr nextHash;       /* next define in defs/refs hash tables */
144
    short depth;                /* used for the cycle detection */
145
    short dflags;               /* define related flags */
146
    xmlRegexpPtr contModel;     /* a compiled content model if available */
147
};
148
149
/**
150
 * _xmlRelaxNG:
151
 *
152
 * A RelaxNGs definition
153
 */
154
struct _xmlRelaxNG {
155
    void *_private;             /* unused by the library for users or bindings */
156
    xmlRelaxNGGrammarPtr topgrammar;
157
    xmlDocPtr doc;
158
159
    int idref;                  /* requires idref checking */
160
161
    xmlHashTablePtr defs;       /* define */
162
    xmlHashTablePtr refs;       /* references */
163
    xmlRelaxNGDocumentPtr documents;    /* all the documents loaded */
164
    xmlRelaxNGIncludePtr includes;      /* all the includes loaded */
165
    int defNr;                  /* number of defines used */
166
    xmlRelaxNGDefinePtr *defTab;        /* pointer to the allocated definitions */
167
168
};
169
170
0
#define XML_RELAXNG_IN_ATTRIBUTE  (1 << 0)
171
0
#define XML_RELAXNG_IN_ONEORMORE  (1 << 1)
172
0
#define XML_RELAXNG_IN_LIST   (1 << 2)
173
0
#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
174
0
#define XML_RELAXNG_IN_START    (1 << 4)
175
0
#define XML_RELAXNG_IN_OOMGROUP   (1 << 5)
176
0
#define XML_RELAXNG_IN_OOMINTERLEAVE  (1 << 6)
177
0
#define XML_RELAXNG_IN_EXTERNALREF  (1 << 7)
178
0
#define XML_RELAXNG_IN_ANYEXCEPT  (1 << 8)
179
0
#define XML_RELAXNG_IN_NSEXCEPT   (1 << 9)
180
181
struct _xmlRelaxNGParserCtxt {
182
    void *userData;             /* user specific data block */
183
    xmlRelaxNGValidityErrorFunc error;  /* the callback in case of errors */
184
    xmlRelaxNGValidityWarningFunc warning;      /* the callback in case of warning */
185
    xmlStructuredErrorFunc serror;
186
    xmlRelaxNGValidErr err;
187
188
    xmlRelaxNGPtr schema;       /* The schema in use */
189
    xmlRelaxNGGrammarPtr grammar;       /* the current grammar */
190
    xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
191
    int flags;                  /* parser flags */
192
    int nbErrors;               /* number of errors at parse time */
193
    int nbWarnings;             /* number of warnings at parse time */
194
    const xmlChar *define;      /* the current define scope */
195
    xmlRelaxNGDefinePtr def;    /* the current define */
196
197
    int nbInterleaves;
198
    xmlHashTablePtr interleaves;        /* keep track of all the interleaves */
199
200
    xmlRelaxNGDocumentPtr documents;    /* all the documents loaded */
201
    xmlRelaxNGIncludePtr includes;      /* all the includes loaded */
202
    xmlChar *URL;
203
    xmlDocPtr document;
204
205
    int defNr;                  /* number of defines used */
206
    int defMax;                 /* number of defines allocated */
207
    xmlRelaxNGDefinePtr *defTab;        /* pointer to the allocated definitions */
208
209
    const char *buffer;
210
    int size;
211
212
    /* the document stack */
213
    xmlRelaxNGDocumentPtr doc;  /* Current parsed external ref */
214
    int docNr;                  /* Depth of the parsing stack */
215
    int docMax;                 /* Max depth of the parsing stack */
216
    xmlRelaxNGDocumentPtr *docTab;      /* array of docs */
217
218
    /* the include stack */
219
    xmlRelaxNGIncludePtr inc;   /* Current parsed include */
220
    int incNr;                  /* Depth of the include parsing stack */
221
    int incMax;                 /* Max depth of the parsing stack */
222
    xmlRelaxNGIncludePtr *incTab;       /* array of incs */
223
224
    int idref;                  /* requires idref checking */
225
226
    /* used to compile content models */
227
    xmlAutomataPtr am;          /* the automata */
228
    xmlAutomataStatePtr state;  /* used to build the automata */
229
230
    int crng;     /* compact syntax and other flags */
231
    int freedoc;    /* need to free the document */
232
233
    xmlResourceLoader resourceLoader;
234
    void *resourceCtxt;
235
};
236
237
0
#define FLAGS_IGNORABLE   1
238
0
#define FLAGS_NEGATIVE    2
239
0
#define FLAGS_MIXED_CONTENT 4
240
0
#define FLAGS_NOERROR   8
241
242
/**
243
 * xmlRelaxNGInterleaveGroup:
244
 *
245
 * A RelaxNGs partition set associated to lists of definitions
246
 */
247
typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
248
typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
249
struct _xmlRelaxNGInterleaveGroup {
250
    xmlRelaxNGDefinePtr rule;   /* the rule to satisfy */
251
    xmlRelaxNGDefinePtr *defs;  /* the array of element definitions */
252
    xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
253
};
254
255
0
#define IS_DETERMINIST    1
256
0
#define IS_NEEDCHECK    2
257
258
/**
259
 * xmlRelaxNGPartitions:
260
 *
261
 * A RelaxNGs partition associated to an interleave group
262
 */
263
typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
264
typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
265
struct _xmlRelaxNGPartition {
266
    int nbgroups;               /* number of groups in the partitions */
267
    xmlHashTablePtr triage;     /* hash table used to direct nodes to the
268
                                 * right group when possible */
269
    int flags;                  /* determinist ? */
270
    xmlRelaxNGInterleaveGroupPtr *groups;
271
};
272
273
/**
274
 * xmlRelaxNGValidState:
275
 *
276
 * A RelaxNGs validation state
277
 */
278
0
#define MAX_ATTR 20
279
typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
280
typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
281
struct _xmlRelaxNGValidState {
282
    xmlNodePtr node;            /* the current node */
283
    xmlNodePtr seq;             /* the sequence of children left to validate */
284
    int nbAttrs;                /* the number of attributes */
285
    int maxAttrs;               /* the size of attrs */
286
    int nbAttrLeft;             /* the number of attributes left to validate */
287
    xmlChar *value;             /* the value when operating on string */
288
    xmlChar *endvalue;          /* the end value when operating on string */
289
    xmlAttrPtr *attrs;          /* the array of attributes */
290
};
291
292
/**
293
 * xmlRelaxNGStates:
294
 *
295
 * A RelaxNGs container for validation state
296
 */
297
typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
298
typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
299
struct _xmlRelaxNGStates {
300
    int nbState;                /* the number of states */
301
    int maxState;               /* the size of the array */
302
    xmlRelaxNGValidStatePtr *tabState;
303
};
304
305
0
#define ERROR_IS_DUP  1
306
307
/**
308
 * xmlRelaxNGValidError:
309
 *
310
 * A RelaxNGs validation error
311
 */
312
typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
313
typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
314
struct _xmlRelaxNGValidError {
315
    xmlRelaxNGValidErr err;     /* the error number */
316
    int flags;                  /* flags */
317
    xmlNodePtr node;            /* the current node */
318
    xmlNodePtr seq;             /* the current child */
319
    const xmlChar *arg1;        /* first arg */
320
    const xmlChar *arg2;        /* second arg */
321
};
322
323
/**
324
 * xmlRelaxNGValidCtxt:
325
 *
326
 * A RelaxNGs validation context
327
 */
328
329
struct _xmlRelaxNGValidCtxt {
330
    void *userData;             /* user specific data block */
331
    xmlRelaxNGValidityErrorFunc error;  /* the callback in case of errors */
332
    xmlRelaxNGValidityWarningFunc warning;      /* the callback in case of warning */
333
    xmlStructuredErrorFunc serror;
334
    int nbErrors;               /* number of errors in validation */
335
336
    xmlRelaxNGPtr schema;       /* The schema in use */
337
    xmlDocPtr doc;              /* the document being validated */
338
    int flags;                  /* validation flags */
339
    int depth;                  /* validation depth */
340
    int idref;                  /* requires idref checking */
341
    int errNo;                  /* the first error found */
342
343
    /*
344
     * Errors accumulated in branches may have to be stacked to be
345
     * provided back when it's sure they affect validation.
346
     */
347
    xmlRelaxNGValidErrorPtr err;        /* Last error */
348
    int errNr;                  /* Depth of the error stack */
349
    int errMax;                 /* Max depth of the error stack */
350
    xmlRelaxNGValidErrorPtr errTab;     /* stack of errors */
351
352
    xmlRelaxNGValidStatePtr state;      /* the current validation state */
353
    xmlRelaxNGStatesPtr states; /* the accumulated state list */
354
355
    xmlRelaxNGStatesPtr freeState;      /* the pool of free valid states */
356
    int freeStatesNr;
357
    int freeStatesMax;
358
    xmlRelaxNGStatesPtr *freeStates;    /* the pool of free state groups */
359
360
    /*
361
     * This is used for "progressive" validation
362
     */
363
    xmlRegExecCtxtPtr elem;     /* the current element regexp */
364
    int elemNr;                 /* the number of element validated */
365
    int elemMax;                /* the max depth of elements */
366
    xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
367
    int pstate;                 /* progressive state */
368
    xmlNodePtr pnode;           /* the current node */
369
    xmlRelaxNGDefinePtr pdef;   /* the non-streamable definition */
370
    int perr;                   /* signal error in content model
371
                                 * outside the regexp */
372
};
373
374
/**
375
 * xmlRelaxNGInclude:
376
 *
377
 * Structure associated to a RelaxNGs document element
378
 */
379
struct _xmlRelaxNGInclude {
380
    xmlRelaxNGIncludePtr next;  /* keep a chain of includes */
381
    xmlChar *href;              /* the normalized href value */
382
    xmlDocPtr doc;              /* the associated XML document */
383
    xmlRelaxNGDefinePtr content;        /* the definitions */
384
    xmlRelaxNGPtr schema;       /* the schema */
385
};
386
387
/**
388
 * xmlRelaxNGDocument:
389
 *
390
 * Structure associated to a RelaxNGs document element
391
 */
392
struct _xmlRelaxNGDocument {
393
    xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
394
    xmlChar *href;              /* the normalized href value */
395
    xmlDocPtr doc;              /* the associated XML document */
396
    xmlRelaxNGDefinePtr content;        /* the definitions */
397
    xmlRelaxNGPtr schema;       /* the schema */
398
    int externalRef;            /* 1 if an external ref */
399
};
400
401
402
/************************************************************************
403
 *                  *
404
 *    Some factorized error routines        *
405
 *                  *
406
 ************************************************************************/
407
408
/**
409
 * xmlRngPErrMemory:
410
 * @ctxt:  an Relax-NG parser context
411
 * @extra:  extra information
412
 *
413
 * Handle a redefinition of attribute error
414
 */
415
static void
416
xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt)
417
0
{
418
0
    xmlStructuredErrorFunc schannel = NULL;
419
0
    xmlGenericErrorFunc channel = NULL;
420
0
    void *data = NULL;
421
422
0
    if (ctxt != NULL) {
423
0
        if (ctxt->serror != NULL)
424
0
      schannel = ctxt->serror;
425
0
  else
426
0
      channel = ctxt->error;
427
0
        data = ctxt->userData;
428
0
        ctxt->nbErrors++;
429
0
    }
430
431
0
    xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGP, NULL);
432
0
}
433
434
/**
435
 * xmlRngVErrMemory:
436
 * @ctxt:  a Relax-NG validation context
437
 * @extra:  extra information
438
 *
439
 * Handle a redefinition of attribute error
440
 */
441
static void
442
xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt)
443
0
{
444
0
    xmlStructuredErrorFunc schannel = NULL;
445
0
    xmlGenericErrorFunc channel = NULL;
446
0
    void *data = NULL;
447
448
0
    if (ctxt != NULL) {
449
0
        if (ctxt->serror != NULL)
450
0
      schannel = ctxt->serror;
451
0
  else
452
0
      channel = ctxt->error;
453
0
        data = ctxt->userData;
454
0
        ctxt->nbErrors++;
455
0
    }
456
457
0
    xmlRaiseMemoryError(schannel, channel, data, XML_FROM_RELAXNGV, NULL);
458
0
}
459
460
/**
461
 * xmlRngPErr:
462
 * @ctxt:  a Relax-NG parser context
463
 * @node:  the node raising the error
464
 * @error:  the error code
465
 * @msg:  message
466
 * @str1:  extra info
467
 * @str2:  extra info
468
 *
469
 * Handle a Relax NG Parsing error
470
 */
471
static void LIBXML_ATTR_FORMAT(4,0)
472
xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
473
           const char *msg, const xmlChar * str1, const xmlChar * str2)
474
0
{
475
0
    xmlStructuredErrorFunc schannel = NULL;
476
0
    xmlGenericErrorFunc channel = NULL;
477
0
    void *data = NULL;
478
0
    int res;
479
480
0
    if (ctxt != NULL) {
481
0
        if (ctxt->serror != NULL)
482
0
      schannel = ctxt->serror;
483
0
  else
484
0
      channel = ctxt->error;
485
0
        data = ctxt->userData;
486
0
        ctxt->nbErrors++;
487
0
    }
488
489
0
    if ((channel == NULL) && (schannel == NULL)) {
490
0
        channel = xmlGenericError;
491
0
        data = xmlGenericErrorContext;
492
0
    }
493
494
0
    res = xmlRaiseError(schannel, channel, data, NULL, node,
495
0
                        XML_FROM_RELAXNGP, error, XML_ERR_ERROR, NULL, 0,
496
0
                        (const char *) str1, (const char *) str2, NULL, 0, 0,
497
0
                        msg, str1, str2);
498
0
    if (res < 0)
499
0
        xmlRngPErrMemory(ctxt);
500
0
}
501
502
/**
503
 * xmlRngVErr:
504
 * @ctxt:  a Relax-NG validation context
505
 * @node:  the node raising the error
506
 * @error:  the error code
507
 * @msg:  message
508
 * @str1:  extra info
509
 * @str2:  extra info
510
 *
511
 * Handle a Relax NG Validation error
512
 */
513
static void LIBXML_ATTR_FORMAT(4,0)
514
xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
515
           const char *msg, const xmlChar * str1, const xmlChar * str2)
516
0
{
517
0
    xmlStructuredErrorFunc schannel = NULL;
518
0
    xmlGenericErrorFunc channel = NULL;
519
0
    void *data = NULL;
520
0
    int res;
521
522
0
    if (ctxt != NULL) {
523
0
        if (ctxt->serror != NULL)
524
0
      schannel = ctxt->serror;
525
0
  else
526
0
      channel = ctxt->error;
527
0
        data = ctxt->userData;
528
0
        ctxt->nbErrors++;
529
0
    }
530
531
0
    if ((channel == NULL) && (schannel == NULL)) {
532
0
        channel = xmlGenericError;
533
0
        data = xmlGenericErrorContext;
534
0
    }
535
536
0
    res = xmlRaiseError(schannel, channel, data, NULL, node,
537
0
                        XML_FROM_RELAXNGV, error, XML_ERR_ERROR, NULL, 0,
538
0
                        (const char *) str1, (const char *) str2, NULL, 0, 0,
539
0
                        msg, str1, str2);
540
0
    if (res < 0)
541
0
        xmlRngVErrMemory(ctxt);
542
0
}
543
544
/************************************************************************
545
 *                  *
546
 *    Preliminary type checking interfaces      *
547
 *                  *
548
 ************************************************************************/
549
550
/**
551
 * xmlRelaxNGTypeHave:
552
 * @data:  data needed for the library
553
 * @type:  the type name
554
 * @value:  the value to check
555
 *
556
 * Function provided by a type library to check if a type is exported
557
 *
558
 * Returns 1 if yes, 0 if no and -1 in case of error.
559
 */
560
typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
561
562
/**
563
 * xmlRelaxNGTypeCheck:
564
 * @data:  data needed for the library
565
 * @type:  the type name
566
 * @value:  the value to check
567
 * @result:  place to store the result if needed
568
 *
569
 * Function provided by a type library to check if a value match a type
570
 *
571
 * Returns 1 if yes, 0 if no and -1 in case of error.
572
 */
573
typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
574
                                    const xmlChar * value, void **result,
575
                                    xmlNodePtr node);
576
577
/**
578
 * xmlRelaxNGFacetCheck:
579
 * @data:  data needed for the library
580
 * @type:  the type name
581
 * @facet:  the facet name
582
 * @val:  the facet value
583
 * @strval:  the string value
584
 * @value:  the value to check
585
 *
586
 * Function provided by a type library to check a value facet
587
 *
588
 * Returns 1 if yes, 0 if no and -1 in case of error.
589
 */
590
typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
591
                                     const xmlChar * facet,
592
                                     const xmlChar * val,
593
                                     const xmlChar * strval, void *value);
594
595
/**
596
 * xmlRelaxNGTypeFree:
597
 * @data:  data needed for the library
598
 * @result:  the value to free
599
 *
600
 * Function provided by a type library to free a returned result
601
 */
602
typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
603
604
/**
605
 * xmlRelaxNGTypeCompare:
606
 * @data:  data needed for the library
607
 * @type:  the type name
608
 * @value1:  the first value
609
 * @value2:  the second value
610
 *
611
 * Function provided by a type library to compare two values accordingly
612
 * to a type.
613
 *
614
 * Returns 1 if yes, 0 if no and -1 in case of error.
615
 */
616
typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
617
                                      const xmlChar * value1,
618
                                      xmlNodePtr ctxt1,
619
                                      void *comp1,
620
                                      const xmlChar * value2,
621
                                      xmlNodePtr ctxt2);
622
typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
623
typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
624
struct _xmlRelaxNGTypeLibrary {
625
    const xmlChar *namespace;   /* the datatypeLibrary value */
626
    void *data;                 /* data needed for the library */
627
    xmlRelaxNGTypeHave have;    /* the export function */
628
    xmlRelaxNGTypeCheck check;  /* the checking function */
629
    xmlRelaxNGTypeCompare comp; /* the compare function */
630
    xmlRelaxNGFacetCheck facet; /* the facet check function */
631
    xmlRelaxNGTypeFree freef;   /* the freeing function */
632
};
633
634
/************************************************************************
635
 *                  *
636
 *      Allocation functions        *
637
 *                  *
638
 ************************************************************************/
639
static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
640
static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
641
static void xmlRelaxNGNormExtSpace(xmlChar * value);
642
static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
643
static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
644
                                     ATTRIBUTE_UNUSED,
645
                                     xmlRelaxNGValidStatePtr state1,
646
                                     xmlRelaxNGValidStatePtr state2);
647
static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
648
                                     xmlRelaxNGValidStatePtr state);
649
650
/**
651
 * xmlRelaxNGFreeDocument:
652
 * @docu:  a document structure
653
 *
654
 * Deallocate a RelaxNG document structure.
655
 */
656
static void
657
xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
658
0
{
659
0
    if (docu == NULL)
660
0
        return;
661
662
0
    if (docu->href != NULL)
663
0
        xmlFree(docu->href);
664
0
    if (docu->doc != NULL)
665
0
        xmlFreeDoc(docu->doc);
666
0
    if (docu->schema != NULL)
667
0
        xmlRelaxNGFreeInnerSchema(docu->schema);
668
0
    xmlFree(docu);
669
0
}
670
671
/**
672
 * xmlRelaxNGFreeDocumentList:
673
 * @docu:  a list of  document structure
674
 *
675
 * Deallocate a RelaxNG document structures.
676
 */
677
static void
678
xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
679
0
{
680
0
    xmlRelaxNGDocumentPtr next;
681
682
0
    while (docu != NULL) {
683
0
        next = docu->next;
684
0
        xmlRelaxNGFreeDocument(docu);
685
0
        docu = next;
686
0
    }
687
0
}
688
689
/**
690
 * xmlRelaxNGFreeInclude:
691
 * @incl:  a include structure
692
 *
693
 * Deallocate a RelaxNG include structure.
694
 */
695
static void
696
xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
697
0
{
698
0
    if (incl == NULL)
699
0
        return;
700
701
0
    if (incl->href != NULL)
702
0
        xmlFree(incl->href);
703
0
    if (incl->doc != NULL)
704
0
        xmlFreeDoc(incl->doc);
705
0
    if (incl->schema != NULL)
706
0
        xmlRelaxNGFree(incl->schema);
707
0
    xmlFree(incl);
708
0
}
709
710
/**
711
 * xmlRelaxNGFreeIncludeList:
712
 * @incl:  a include structure list
713
 *
714
 * Deallocate a RelaxNG include structure.
715
 */
716
static void
717
xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
718
0
{
719
0
    xmlRelaxNGIncludePtr next;
720
721
0
    while (incl != NULL) {
722
0
        next = incl->next;
723
0
        xmlRelaxNGFreeInclude(incl);
724
0
        incl = next;
725
0
    }
726
0
}
727
728
/**
729
 * xmlRelaxNGNewRelaxNG:
730
 * @ctxt:  a Relax-NG validation context (optional)
731
 *
732
 * Allocate a new RelaxNG structure.
733
 *
734
 * Returns the newly allocated structure or NULL in case or error
735
 */
736
static xmlRelaxNGPtr
737
xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
738
0
{
739
0
    xmlRelaxNGPtr ret;
740
741
0
    ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
742
0
    if (ret == NULL) {
743
0
        xmlRngPErrMemory(ctxt);
744
0
        return (NULL);
745
0
    }
746
0
    memset(ret, 0, sizeof(xmlRelaxNG));
747
748
0
    return (ret);
749
0
}
750
751
/**
752
 * xmlRelaxNGFreeInnerSchema:
753
 * @schema:  a schema structure
754
 *
755
 * Deallocate a RelaxNG schema structure.
756
 */
757
static void
758
xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
759
0
{
760
0
    if (schema == NULL)
761
0
        return;
762
763
0
    if (schema->doc != NULL)
764
0
        xmlFreeDoc(schema->doc);
765
0
    if (schema->defTab != NULL) {
766
0
        int i;
767
768
0
        for (i = 0; i < schema->defNr; i++)
769
0
            xmlRelaxNGFreeDefine(schema->defTab[i]);
770
0
        xmlFree(schema->defTab);
771
0
    }
772
773
0
    xmlFree(schema);
774
0
}
775
776
/**
777
 * xmlRelaxNGFree:
778
 * @schema:  a schema structure
779
 *
780
 * Deallocate a RelaxNG structure.
781
 */
782
void
783
xmlRelaxNGFree(xmlRelaxNGPtr schema)
784
0
{
785
0
    if (schema == NULL)
786
0
        return;
787
788
0
    if (schema->topgrammar != NULL)
789
0
        xmlRelaxNGFreeGrammar(schema->topgrammar);
790
0
    if (schema->doc != NULL)
791
0
        xmlFreeDoc(schema->doc);
792
0
    if (schema->documents != NULL)
793
0
        xmlRelaxNGFreeDocumentList(schema->documents);
794
0
    if (schema->includes != NULL)
795
0
        xmlRelaxNGFreeIncludeList(schema->includes);
796
0
    if (schema->defTab != NULL) {
797
0
        int i;
798
799
0
        for (i = 0; i < schema->defNr; i++)
800
0
            xmlRelaxNGFreeDefine(schema->defTab[i]);
801
0
        xmlFree(schema->defTab);
802
0
    }
803
804
0
    xmlFree(schema);
805
0
}
806
807
/**
808
 * xmlRelaxNGNewGrammar:
809
 * @ctxt:  a Relax-NG validation context (optional)
810
 *
811
 * Allocate a new RelaxNG grammar.
812
 *
813
 * Returns the newly allocated structure or NULL in case or error
814
 */
815
static xmlRelaxNGGrammarPtr
816
xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
817
0
{
818
0
    xmlRelaxNGGrammarPtr ret;
819
820
0
    ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
821
0
    if (ret == NULL) {
822
0
        xmlRngPErrMemory(ctxt);
823
0
        return (NULL);
824
0
    }
825
0
    memset(ret, 0, sizeof(xmlRelaxNGGrammar));
826
827
0
    return (ret);
828
0
}
829
830
/**
831
 * xmlRelaxNGFreeGrammar:
832
 * @grammar:  a grammar structure
833
 *
834
 * Deallocate a RelaxNG grammar structure.
835
 */
836
static void
837
xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
838
0
{
839
0
    if (grammar == NULL)
840
0
        return;
841
842
0
    if (grammar->children != NULL) {
843
0
        xmlRelaxNGFreeGrammar(grammar->children);
844
0
    }
845
0
    if (grammar->next != NULL) {
846
0
        xmlRelaxNGFreeGrammar(grammar->next);
847
0
    }
848
0
    if (grammar->refs != NULL) {
849
0
        xmlHashFree(grammar->refs, NULL);
850
0
    }
851
0
    if (grammar->defs != NULL) {
852
0
        xmlHashFree(grammar->defs, NULL);
853
0
    }
854
855
0
    xmlFree(grammar);
856
0
}
857
858
/**
859
 * xmlRelaxNGNewDefine:
860
 * @ctxt:  a Relax-NG validation context
861
 * @node:  the node in the input document.
862
 *
863
 * Allocate a new RelaxNG define.
864
 *
865
 * Returns the newly allocated structure or NULL in case or error
866
 */
867
static xmlRelaxNGDefinePtr
868
xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
869
0
{
870
0
    xmlRelaxNGDefinePtr ret;
871
872
0
    if (ctxt->defMax == 0) {
873
0
        ctxt->defMax = 16;
874
0
        ctxt->defNr = 0;
875
0
        ctxt->defTab = (xmlRelaxNGDefinePtr *)
876
0
            xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
877
0
        if (ctxt->defTab == NULL) {
878
0
            xmlRngPErrMemory(ctxt);
879
0
            return (NULL);
880
0
        }
881
0
    } else if (ctxt->defMax <= ctxt->defNr) {
882
0
        xmlRelaxNGDefinePtr *tmp;
883
884
0
        ctxt->defMax *= 2;
885
0
        tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
886
0
                                                 ctxt->defMax *
887
0
                                                 sizeof
888
0
                                                 (xmlRelaxNGDefinePtr));
889
0
        if (tmp == NULL) {
890
0
            xmlRngPErrMemory(ctxt);
891
0
            return (NULL);
892
0
        }
893
0
        ctxt->defTab = tmp;
894
0
    }
895
0
    ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
896
0
    if (ret == NULL) {
897
0
        xmlRngPErrMemory(ctxt);
898
0
        return (NULL);
899
0
    }
900
0
    memset(ret, 0, sizeof(xmlRelaxNGDefine));
901
0
    ctxt->defTab[ctxt->defNr++] = ret;
902
0
    ret->node = node;
903
0
    ret->depth = -1;
904
0
    return (ret);
905
0
}
906
907
/**
908
 * xmlRelaxNGFreePartition:
909
 * @partitions:  a partition set structure
910
 *
911
 * Deallocate RelaxNG partition set structures.
912
 */
913
static void
914
xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
915
0
{
916
0
    xmlRelaxNGInterleaveGroupPtr group;
917
0
    int j;
918
919
0
    if (partitions != NULL) {
920
0
        if (partitions->groups != NULL) {
921
0
            for (j = 0; j < partitions->nbgroups; j++) {
922
0
                group = partitions->groups[j];
923
0
                if (group != NULL) {
924
0
                    if (group->defs != NULL)
925
0
                        xmlFree(group->defs);
926
0
                    if (group->attrs != NULL)
927
0
                        xmlFree(group->attrs);
928
0
                    xmlFree(group);
929
0
                }
930
0
            }
931
0
            xmlFree(partitions->groups);
932
0
        }
933
0
        if (partitions->triage != NULL) {
934
0
            xmlHashFree(partitions->triage, NULL);
935
0
        }
936
0
        xmlFree(partitions);
937
0
    }
938
0
}
939
940
/**
941
 * xmlRelaxNGFreeDefine:
942
 * @define:  a define structure
943
 *
944
 * Deallocate a RelaxNG define structure.
945
 */
946
static void
947
xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
948
0
{
949
0
    if (define == NULL)
950
0
        return;
951
952
0
    if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
953
0
        xmlRelaxNGTypeLibraryPtr lib;
954
955
0
        lib = (xmlRelaxNGTypeLibraryPtr) define->data;
956
0
        if ((lib != NULL) && (lib->freef != NULL))
957
0
            lib->freef(lib->data, (void *) define->attrs);
958
0
    }
959
0
    if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
960
0
        xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
961
0
    if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
962
0
        xmlHashFree((xmlHashTablePtr) define->data, NULL);
963
0
    if (define->name != NULL)
964
0
        xmlFree(define->name);
965
0
    if (define->ns != NULL)
966
0
        xmlFree(define->ns);
967
0
    if (define->value != NULL)
968
0
        xmlFree(define->value);
969
0
    if (define->contModel != NULL)
970
0
        xmlRegFreeRegexp(define->contModel);
971
0
    xmlFree(define);
972
0
}
973
974
/**
975
 * xmlRelaxNGNewStates:
976
 * @ctxt:  a Relax-NG validation context
977
 * @size:  the default size for the container
978
 *
979
 * Allocate a new RelaxNG validation state container
980
 *
981
 * Returns the newly allocated structure or NULL in case or error
982
 */
983
static xmlRelaxNGStatesPtr
984
xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
985
0
{
986
0
    xmlRelaxNGStatesPtr ret;
987
988
0
    if ((ctxt != NULL) &&
989
0
        (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
990
0
        ctxt->freeStatesNr--;
991
0
        ret = ctxt->freeStates[ctxt->freeStatesNr];
992
0
        ret->nbState = 0;
993
0
        return (ret);
994
0
    }
995
0
    if (size < 16)
996
0
        size = 16;
997
998
0
    ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
999
0
                                          (size -
1000
0
                                           1) *
1001
0
                                          sizeof(xmlRelaxNGValidStatePtr));
1002
0
    if (ret == NULL) {
1003
0
        xmlRngVErrMemory(ctxt);
1004
0
        return (NULL);
1005
0
    }
1006
0
    ret->nbState = 0;
1007
0
    ret->maxState = size;
1008
0
    ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1009
0
                                                          sizeof
1010
0
                                                          (xmlRelaxNGValidStatePtr));
1011
0
    if (ret->tabState == NULL) {
1012
0
        xmlRngVErrMemory(ctxt);
1013
0
        xmlFree(ret);
1014
0
        return (NULL);
1015
0
    }
1016
0
    return (ret);
1017
0
}
1018
1019
/**
1020
 * xmlRelaxNGAddStateUniq:
1021
 * @ctxt:  a Relax-NG validation context
1022
 * @states:  the states container
1023
 * @state:  the validation state
1024
 *
1025
 * Add a RelaxNG validation state to the container without checking
1026
 * for unicity.
1027
 *
1028
 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1029
 */
1030
static int
1031
xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
1032
                        xmlRelaxNGStatesPtr states,
1033
                        xmlRelaxNGValidStatePtr state)
1034
0
{
1035
0
    if (state == NULL) {
1036
0
        return (-1);
1037
0
    }
1038
0
    if (states->nbState >= states->maxState) {
1039
0
        xmlRelaxNGValidStatePtr *tmp;
1040
0
        int size;
1041
1042
0
        size = states->maxState * 2;
1043
0
        tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1044
0
                                                     (size) *
1045
0
                                                     sizeof
1046
0
                                                     (xmlRelaxNGValidStatePtr));
1047
0
        if (tmp == NULL) {
1048
0
            xmlRngVErrMemory(ctxt);
1049
0
            return (-1);
1050
0
        }
1051
0
        states->tabState = tmp;
1052
0
        states->maxState = size;
1053
0
    }
1054
0
    states->tabState[states->nbState++] = state;
1055
0
    return (1);
1056
0
}
1057
1058
/**
1059
 * xmlRelaxNGAddState:
1060
 * @ctxt:  a Relax-NG validation context
1061
 * @states:  the states container
1062
 * @state:  the validation state
1063
 *
1064
 * Add a RelaxNG validation state to the container
1065
 *
1066
 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1067
 */
1068
static int
1069
xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1070
                    xmlRelaxNGStatesPtr states,
1071
                    xmlRelaxNGValidStatePtr state)
1072
0
{
1073
0
    int i;
1074
1075
0
    if (state == NULL || states == NULL) {
1076
0
        return (-1);
1077
0
    }
1078
0
    if (states->nbState >= states->maxState) {
1079
0
        xmlRelaxNGValidStatePtr *tmp;
1080
0
        int size;
1081
1082
0
        size = states->maxState * 2;
1083
0
        tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1084
0
                                                     (size) *
1085
0
                                                     sizeof
1086
0
                                                     (xmlRelaxNGValidStatePtr));
1087
0
        if (tmp == NULL) {
1088
0
            xmlRngVErrMemory(ctxt);
1089
0
            return (-1);
1090
0
        }
1091
0
        states->tabState = tmp;
1092
0
        states->maxState = size;
1093
0
    }
1094
0
    for (i = 0; i < states->nbState; i++) {
1095
0
        if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1096
0
            xmlRelaxNGFreeValidState(ctxt, state);
1097
0
            return (0);
1098
0
        }
1099
0
    }
1100
0
    states->tabState[states->nbState++] = state;
1101
0
    return (1);
1102
0
}
1103
1104
/**
1105
 * xmlRelaxNGFreeStates:
1106
 * @ctxt:  a Relax-NG validation context
1107
 * @states:  the container
1108
 *
1109
 * Free a RelaxNG validation state container
1110
 */
1111
static void
1112
xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
1113
                     xmlRelaxNGStatesPtr states)
1114
0
{
1115
0
    if (states == NULL)
1116
0
        return;
1117
0
    if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
1118
0
        ctxt->freeStatesMax = 40;
1119
0
        ctxt->freeStatesNr = 0;
1120
0
        ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1121
0
            xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1122
0
        if (ctxt->freeStates == NULL) {
1123
0
            xmlRngVErrMemory(ctxt);
1124
0
        }
1125
0
    } else if ((ctxt != NULL)
1126
0
               && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1127
0
        xmlRelaxNGStatesPtr *tmp;
1128
1129
0
        tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1130
0
                                                 2 * ctxt->freeStatesMax *
1131
0
                                                 sizeof
1132
0
                                                 (xmlRelaxNGStatesPtr));
1133
0
        if (tmp == NULL) {
1134
0
            xmlRngVErrMemory(ctxt);
1135
0
            xmlFree(states->tabState);
1136
0
            xmlFree(states);
1137
0
            return;
1138
0
        }
1139
0
        ctxt->freeStates = tmp;
1140
0
        ctxt->freeStatesMax *= 2;
1141
0
    }
1142
0
    if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
1143
0
        xmlFree(states->tabState);
1144
0
        xmlFree(states);
1145
0
    } else {
1146
0
        ctxt->freeStates[ctxt->freeStatesNr++] = states;
1147
0
    }
1148
0
}
1149
1150
/**
1151
 * xmlRelaxNGNewValidState:
1152
 * @ctxt:  a Relax-NG validation context
1153
 * @node:  the current node or NULL for the document
1154
 *
1155
 * Allocate a new RelaxNG validation state
1156
 *
1157
 * Returns the newly allocated structure or NULL in case or error
1158
 */
1159
static xmlRelaxNGValidStatePtr
1160
xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1161
0
{
1162
0
    xmlRelaxNGValidStatePtr ret;
1163
0
    xmlAttrPtr attr;
1164
0
    xmlAttrPtr attrs[MAX_ATTR];
1165
0
    int nbAttrs = 0;
1166
0
    xmlNodePtr root = NULL;
1167
1168
0
    if (node == NULL) {
1169
0
        root = xmlDocGetRootElement(ctxt->doc);
1170
0
        if (root == NULL)
1171
0
            return (NULL);
1172
0
    } else {
1173
0
        attr = node->properties;
1174
0
        while (attr != NULL) {
1175
0
            if (nbAttrs < MAX_ATTR)
1176
0
                attrs[nbAttrs++] = attr;
1177
0
            else
1178
0
                nbAttrs++;
1179
0
            attr = attr->next;
1180
0
        }
1181
0
    }
1182
0
    if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1183
0
        ctxt->freeState->nbState--;
1184
0
        ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1185
0
    } else {
1186
0
        ret =
1187
0
            (xmlRelaxNGValidStatePtr)
1188
0
            xmlMalloc(sizeof(xmlRelaxNGValidState));
1189
0
        if (ret == NULL) {
1190
0
            xmlRngVErrMemory(ctxt);
1191
0
            return (NULL);
1192
0
        }
1193
0
        memset(ret, 0, sizeof(xmlRelaxNGValidState));
1194
0
    }
1195
0
    ret->value = NULL;
1196
0
    ret->endvalue = NULL;
1197
0
    if (node == NULL) {
1198
0
        ret->node = (xmlNodePtr) ctxt->doc;
1199
0
        ret->seq = root;
1200
0
    } else {
1201
0
        ret->node = node;
1202
0
        ret->seq = node->children;
1203
0
    }
1204
0
    ret->nbAttrs = 0;
1205
0
    if (nbAttrs > 0) {
1206
0
        if (ret->attrs == NULL) {
1207
0
            if (nbAttrs < 4)
1208
0
                ret->maxAttrs = 4;
1209
0
            else
1210
0
                ret->maxAttrs = nbAttrs;
1211
0
            ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1212
0
                                                  sizeof(xmlAttrPtr));
1213
0
            if (ret->attrs == NULL) {
1214
0
                xmlRngVErrMemory(ctxt);
1215
0
                return (ret);
1216
0
            }
1217
0
        } else if (ret->maxAttrs < nbAttrs) {
1218
0
            xmlAttrPtr *tmp;
1219
1220
0
            tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1221
0
                                            sizeof(xmlAttrPtr));
1222
0
            if (tmp == NULL) {
1223
0
                xmlRngVErrMemory(ctxt);
1224
0
                return (ret);
1225
0
            }
1226
0
            ret->attrs = tmp;
1227
0
            ret->maxAttrs = nbAttrs;
1228
0
        }
1229
0
        ret->nbAttrs = nbAttrs;
1230
0
        if (nbAttrs < MAX_ATTR) {
1231
0
            memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1232
0
        } else {
1233
0
            attr = node->properties;
1234
0
            nbAttrs = 0;
1235
0
            while (attr != NULL) {
1236
0
                ret->attrs[nbAttrs++] = attr;
1237
0
                attr = attr->next;
1238
0
            }
1239
0
        }
1240
0
    }
1241
0
    ret->nbAttrLeft = ret->nbAttrs;
1242
0
    return (ret);
1243
0
}
1244
1245
/**
1246
 * xmlRelaxNGCopyValidState:
1247
 * @ctxt:  a Relax-NG validation context
1248
 * @state:  a validation state
1249
 *
1250
 * Copy the validation state
1251
 *
1252
 * Returns the newly allocated structure or NULL in case or error
1253
 */
1254
static xmlRelaxNGValidStatePtr
1255
xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1256
                         xmlRelaxNGValidStatePtr state)
1257
0
{
1258
0
    xmlRelaxNGValidStatePtr ret;
1259
0
    unsigned int maxAttrs;
1260
0
    xmlAttrPtr *attrs;
1261
1262
0
    if (state == NULL)
1263
0
        return (NULL);
1264
0
    if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1265
0
        ctxt->freeState->nbState--;
1266
0
        ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1267
0
    } else {
1268
0
        ret =
1269
0
            (xmlRelaxNGValidStatePtr)
1270
0
            xmlMalloc(sizeof(xmlRelaxNGValidState));
1271
0
        if (ret == NULL) {
1272
0
            xmlRngVErrMemory(ctxt);
1273
0
            return (NULL);
1274
0
        }
1275
0
        memset(ret, 0, sizeof(xmlRelaxNGValidState));
1276
0
    }
1277
0
    attrs = ret->attrs;
1278
0
    maxAttrs = ret->maxAttrs;
1279
0
    memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1280
0
    ret->attrs = attrs;
1281
0
    ret->maxAttrs = maxAttrs;
1282
0
    if (state->nbAttrs > 0) {
1283
0
        if (ret->attrs == NULL) {
1284
0
            ret->maxAttrs = state->maxAttrs;
1285
0
            ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1286
0
                                                  sizeof(xmlAttrPtr));
1287
0
            if (ret->attrs == NULL) {
1288
0
                xmlRngVErrMemory(ctxt);
1289
0
                ret->nbAttrs = 0;
1290
0
                return (ret);
1291
0
            }
1292
0
        } else if (ret->maxAttrs < state->nbAttrs) {
1293
0
            xmlAttrPtr *tmp;
1294
1295
0
            tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1296
0
                                            sizeof(xmlAttrPtr));
1297
0
            if (tmp == NULL) {
1298
0
                xmlRngVErrMemory(ctxt);
1299
0
                ret->nbAttrs = 0;
1300
0
                return (ret);
1301
0
            }
1302
0
            ret->maxAttrs = state->maxAttrs;
1303
0
            ret->attrs = tmp;
1304
0
        }
1305
0
        memcpy(ret->attrs, state->attrs,
1306
0
               state->nbAttrs * sizeof(xmlAttrPtr));
1307
0
    }
1308
0
    return (ret);
1309
0
}
1310
1311
/**
1312
 * xmlRelaxNGEqualValidState:
1313
 * @ctxt:  a Relax-NG validation context
1314
 * @state1:  a validation state
1315
 * @state2:  a validation state
1316
 *
1317
 * Compare the validation states for equality
1318
 *
1319
 * Returns 1 if equal, 0 otherwise
1320
 */
1321
static int
1322
xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1323
                          xmlRelaxNGValidStatePtr state1,
1324
                          xmlRelaxNGValidStatePtr state2)
1325
0
{
1326
0
    int i;
1327
1328
0
    if ((state1 == NULL) || (state2 == NULL))
1329
0
        return (0);
1330
0
    if (state1 == state2)
1331
0
        return (1);
1332
0
    if (state1->node != state2->node)
1333
0
        return (0);
1334
0
    if (state1->seq != state2->seq)
1335
0
        return (0);
1336
0
    if (state1->nbAttrLeft != state2->nbAttrLeft)
1337
0
        return (0);
1338
0
    if (state1->nbAttrs != state2->nbAttrs)
1339
0
        return (0);
1340
0
    if (state1->endvalue != state2->endvalue)
1341
0
        return (0);
1342
0
    if ((state1->value != state2->value) &&
1343
0
        (!xmlStrEqual(state1->value, state2->value)))
1344
0
        return (0);
1345
0
    for (i = 0; i < state1->nbAttrs; i++) {
1346
0
        if (state1->attrs[i] != state2->attrs[i])
1347
0
            return (0);
1348
0
    }
1349
0
    return (1);
1350
0
}
1351
1352
/**
1353
 * xmlRelaxNGFreeValidState:
1354
 * @state:  a validation state structure
1355
 *
1356
 * Deallocate a RelaxNG validation state structure.
1357
 */
1358
static void
1359
xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1360
                         xmlRelaxNGValidStatePtr state)
1361
0
{
1362
0
    if (state == NULL)
1363
0
        return;
1364
1365
0
    if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1366
0
        ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1367
0
    }
1368
0
    if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1369
0
        if (state->attrs != NULL)
1370
0
            xmlFree(state->attrs);
1371
0
        xmlFree(state);
1372
0
    } else {
1373
0
        xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1374
0
    }
1375
0
}
1376
1377
/************************************************************************
1378
 *                  *
1379
 *      Semi internal functions       *
1380
 *                  *
1381
 ************************************************************************/
1382
1383
/**
1384
 * xmlRelaxParserSetFlag:
1385
 * @ctxt: a RelaxNG parser context
1386
 * @flags: a set of flags values
1387
 *
1388
 * Semi private function used to pass information to a parser context
1389
 * which are a combination of xmlRelaxNGParserFlag .
1390
 *
1391
 * Returns 0 if success and -1 in case of error
1392
 */
1393
int
1394
xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1395
0
{
1396
0
    if (ctxt == NULL) return(-1);
1397
0
    if (flags & XML_RELAXNGP_FREE_DOC) {
1398
0
        ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1399
0
  flags -= XML_RELAXNGP_FREE_DOC;
1400
0
    }
1401
0
    if (flags & XML_RELAXNGP_CRNG) {
1402
0
        ctxt->crng |= XML_RELAXNGP_CRNG;
1403
0
  flags -= XML_RELAXNGP_CRNG;
1404
0
    }
1405
0
    if (flags != 0) return(-1);
1406
0
    return(0);
1407
0
}
1408
1409
/************************************************************************
1410
 *                  *
1411
 *      Document functions        *
1412
 *                  *
1413
 ************************************************************************/
1414
static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1415
                                      xmlDocPtr doc);
1416
1417
static xmlDoc *
1418
0
xmlRelaxReadFile(xmlRelaxNGParserCtxtPtr ctxt, const char *filename) {
1419
0
    xmlParserCtxtPtr pctxt;
1420
0
    xmlDocPtr doc;
1421
1422
0
    pctxt = xmlNewParserCtxt();
1423
0
    if (pctxt == NULL) {
1424
0
        xmlRngPErrMemory(ctxt);
1425
0
        return(NULL);
1426
0
    }
1427
0
    if (ctxt->serror != NULL)
1428
0
        xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData);
1429
0
    if (ctxt->resourceLoader != NULL)
1430
0
        xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
1431
0
                                 ctxt->resourceCtxt);
1432
0
    doc = xmlCtxtReadFile(pctxt, filename, NULL, 0);
1433
0
    xmlFreeParserCtxt(pctxt);
1434
1435
0
    return(doc);
1436
0
}
1437
1438
static xmlDoc *
1439
0
xmlRelaxReadMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *buf, int size) {
1440
0
    xmlParserCtxtPtr pctxt;
1441
0
    xmlDocPtr doc;
1442
1443
0
    pctxt = xmlNewParserCtxt();
1444
0
    if (pctxt == NULL) {
1445
0
        xmlRngPErrMemory(ctxt);
1446
0
        return(NULL);
1447
0
    }
1448
0
    if (ctxt->serror != NULL)
1449
0
        xmlCtxtSetErrorHandler(pctxt, ctxt->serror, ctxt->userData);
1450
0
    if (ctxt->resourceLoader != NULL)
1451
0
        xmlCtxtSetResourceLoader(pctxt, ctxt->resourceLoader,
1452
0
                                 ctxt->resourceCtxt);
1453
0
    doc = xmlCtxtReadMemory(pctxt, buf, size, NULL, NULL, 0);
1454
0
    xmlFreeParserCtxt(pctxt);
1455
1456
0
    return(doc);
1457
0
}
1458
1459
/**
1460
 * xmlRelaxNGIncludePush:
1461
 * @ctxt:  the parser context
1462
 * @value:  the element doc
1463
 *
1464
 * Pushes a new include on top of the include stack
1465
 *
1466
 * Returns 0 in case of error, the index in the stack otherwise
1467
 */
1468
static int
1469
xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1470
                      xmlRelaxNGIncludePtr value)
1471
0
{
1472
0
    if (ctxt->incTab == NULL) {
1473
0
        ctxt->incMax = 4;
1474
0
        ctxt->incNr = 0;
1475
0
        ctxt->incTab =
1476
0
            (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1477
0
                                               sizeof(ctxt->incTab[0]));
1478
0
        if (ctxt->incTab == NULL) {
1479
0
            xmlRngPErrMemory(ctxt);
1480
0
            return (0);
1481
0
        }
1482
0
    }
1483
0
    if (ctxt->incNr >= ctxt->incMax) {
1484
0
        ctxt->incMax *= 2;
1485
0
        ctxt->incTab =
1486
0
            (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1487
0
                                                ctxt->incMax *
1488
0
                                                sizeof(ctxt->incTab[0]));
1489
0
        if (ctxt->incTab == NULL) {
1490
0
            xmlRngPErrMemory(ctxt);
1491
0
            return (0);
1492
0
        }
1493
0
    }
1494
0
    ctxt->incTab[ctxt->incNr] = value;
1495
0
    ctxt->inc = value;
1496
0
    return (ctxt->incNr++);
1497
0
}
1498
1499
/**
1500
 * xmlRelaxNGIncludePop:
1501
 * @ctxt: the parser context
1502
 *
1503
 * Pops the top include from the include stack
1504
 *
1505
 * Returns the include just removed
1506
 */
1507
static xmlRelaxNGIncludePtr
1508
xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1509
0
{
1510
0
    xmlRelaxNGIncludePtr ret;
1511
1512
0
    if (ctxt->incNr <= 0)
1513
0
        return (NULL);
1514
0
    ctxt->incNr--;
1515
0
    if (ctxt->incNr > 0)
1516
0
        ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1517
0
    else
1518
0
        ctxt->inc = NULL;
1519
0
    ret = ctxt->incTab[ctxt->incNr];
1520
0
    ctxt->incTab[ctxt->incNr] = NULL;
1521
0
    return (ret);
1522
0
}
1523
1524
/**
1525
 * xmlRelaxNGRemoveRedefine:
1526
 * @ctxt: the parser context
1527
 * @URL:  the normalized URL
1528
 * @target:  the included target
1529
 * @name:  the define name to eliminate
1530
 *
1531
 * Applies the elimination algorithm of 4.7
1532
 *
1533
 * Returns 0 in case of error, 1 in case of success.
1534
 */
1535
static int
1536
xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1537
                         const xmlChar * URL ATTRIBUTE_UNUSED,
1538
                         xmlNodePtr target, const xmlChar * name)
1539
0
{
1540
0
    int found = 0;
1541
0
    xmlNodePtr tmp, tmp2;
1542
0
    xmlChar *name2;
1543
1544
0
    tmp = target;
1545
0
    while (tmp != NULL) {
1546
0
        tmp2 = tmp->next;
1547
0
        if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1548
0
            found = 1;
1549
0
            xmlUnlinkNode(tmp);
1550
0
            xmlFreeNode(tmp);
1551
0
        } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1552
0
            name2 = xmlGetProp(tmp, BAD_CAST "name");
1553
0
            xmlRelaxNGNormExtSpace(name2);
1554
0
            if (name2 != NULL) {
1555
0
                if (xmlStrEqual(name, name2)) {
1556
0
                    found = 1;
1557
0
                    xmlUnlinkNode(tmp);
1558
0
                    xmlFreeNode(tmp);
1559
0
                }
1560
0
                xmlFree(name2);
1561
0
            }
1562
0
        } else if (IS_RELAXNG(tmp, "include")) {
1563
0
            xmlChar *href = NULL;
1564
0
            xmlRelaxNGDocumentPtr inc = tmp->psvi;
1565
1566
0
            if ((inc != NULL) && (inc->doc != NULL) &&
1567
0
                (inc->doc->children != NULL)) {
1568
1569
0
                if (xmlStrEqual
1570
0
                    (inc->doc->children->name, BAD_CAST "grammar")) {
1571
0
                    if (xmlRelaxNGRemoveRedefine(ctxt, href,
1572
0
                                                 xmlDocGetRootElement(inc->doc)->children,
1573
0
                                                 name) == 1) {
1574
0
                        found = 1;
1575
0
                    }
1576
0
                }
1577
0
            }
1578
0
            if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) {
1579
0
                found = 1;
1580
0
            }
1581
0
        }
1582
0
        tmp = tmp2;
1583
0
    }
1584
0
    return (found);
1585
0
}
1586
1587
/**
1588
 * xmlRelaxNGLoadInclude:
1589
 * @ctxt: the parser context
1590
 * @URL:  the normalized URL
1591
 * @node: the include node.
1592
 * @ns:  the namespace passed from the context.
1593
 *
1594
 * First lookup if the document is already loaded into the parser context,
1595
 * check against recursion. If not found the resource is loaded and
1596
 * the content is preprocessed before being returned back to the caller.
1597
 *
1598
 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1599
 */
1600
static xmlRelaxNGIncludePtr
1601
xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1602
                      xmlNodePtr node, const xmlChar * ns)
1603
0
{
1604
0
    xmlRelaxNGIncludePtr ret = NULL;
1605
0
    xmlDocPtr doc;
1606
0
    int i;
1607
0
    xmlNodePtr root, cur;
1608
1609
    /*
1610
     * check against recursion in the stack
1611
     */
1612
0
    for (i = 0; i < ctxt->incNr; i++) {
1613
0
        if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1614
0
            xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1615
0
                       "Detected an Include recursion for %s\n", URL,
1616
0
                       NULL);
1617
0
            return (NULL);
1618
0
        }
1619
0
    }
1620
1621
    /*
1622
     * load the document
1623
     */
1624
0
    doc = xmlRelaxReadFile(ctxt, (const char *) URL);
1625
0
    if (doc == NULL) {
1626
0
        xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1627
0
                   "xmlRelaxNG: could not load %s\n", URL, NULL);
1628
0
        return (NULL);
1629
0
    }
1630
1631
    /*
1632
     * Allocate the document structures and register it first.
1633
     */
1634
0
    ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1635
0
    if (ret == NULL) {
1636
0
        xmlRngPErrMemory(ctxt);
1637
0
        xmlFreeDoc(doc);
1638
0
        return (NULL);
1639
0
    }
1640
0
    memset(ret, 0, sizeof(xmlRelaxNGInclude));
1641
0
    ret->doc = doc;
1642
0
    ret->href = xmlStrdup(URL);
1643
0
    ret->next = ctxt->includes;
1644
0
    ctxt->includes = ret;
1645
1646
    /*
1647
     * transmit the ns if needed
1648
     */
1649
0
    if (ns != NULL) {
1650
0
        root = xmlDocGetRootElement(doc);
1651
0
        if (root != NULL) {
1652
0
            if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1653
0
                xmlSetProp(root, BAD_CAST "ns", ns);
1654
0
            }
1655
0
        }
1656
0
    }
1657
1658
    /*
1659
     * push it on the stack
1660
     */
1661
0
    xmlRelaxNGIncludePush(ctxt, ret);
1662
1663
    /*
1664
     * Some preprocessing of the document content, this include recursing
1665
     * in the include stack.
1666
     */
1667
1668
0
    doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1669
0
    if (doc == NULL) {
1670
0
        ctxt->inc = NULL;
1671
0
        return (NULL);
1672
0
    }
1673
1674
    /*
1675
     * Pop up the include from the stack
1676
     */
1677
0
    xmlRelaxNGIncludePop(ctxt);
1678
1679
    /*
1680
     * Check that the top element is a grammar
1681
     */
1682
0
    root = xmlDocGetRootElement(doc);
1683
0
    if (root == NULL) {
1684
0
        xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1685
0
                   "xmlRelaxNG: included document is empty %s\n", URL,
1686
0
                   NULL);
1687
0
        return (NULL);
1688
0
    }
1689
0
    if (!IS_RELAXNG(root, "grammar")) {
1690
0
        xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1691
0
                   "xmlRelaxNG: included document %s root is not a grammar\n",
1692
0
                   URL, NULL);
1693
0
        return (NULL);
1694
0
    }
1695
1696
    /*
1697
     * Elimination of redefined rules in the include.
1698
     */
1699
0
    cur = node->children;
1700
0
    while (cur != NULL) {
1701
0
        if (IS_RELAXNG(cur, "start")) {
1702
0
            int found = 0;
1703
1704
0
            found =
1705
0
                xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1706
0
            if (!found) {
1707
0
                xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1708
0
                           "xmlRelaxNG: include %s has a start but not the included grammar\n",
1709
0
                           URL, NULL);
1710
0
            }
1711
0
        } else if (IS_RELAXNG(cur, "define")) {
1712
0
            xmlChar *name;
1713
1714
0
            name = xmlGetProp(cur, BAD_CAST "name");
1715
0
            if (name == NULL) {
1716
0
                xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1717
0
                           "xmlRelaxNG: include %s has define without name\n",
1718
0
                           URL, NULL);
1719
0
            } else {
1720
0
                int found;
1721
1722
0
                xmlRelaxNGNormExtSpace(name);
1723
0
                found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1724
0
                                                 root->children, name);
1725
0
                if (!found) {
1726
0
                    xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1727
0
                               "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1728
0
                               URL, name);
1729
0
                }
1730
0
                xmlFree(name);
1731
0
            }
1732
0
        }
1733
0
        if (IS_RELAXNG(cur, "div") && cur->children != NULL) {
1734
0
            cur = cur->children;
1735
0
        } else {
1736
0
            if (cur->next != NULL) {
1737
0
                cur = cur->next;
1738
0
            } else {
1739
0
                while (cur->parent != node && cur->parent->next == NULL) {
1740
0
                    cur = cur->parent;
1741
0
                }
1742
0
                cur = cur->parent != node ? cur->parent->next : NULL;
1743
0
            }
1744
0
        }
1745
0
    }
1746
1747
1748
0
    return (ret);
1749
0
}
1750
1751
/**
1752
 * xmlRelaxNGValidErrorPush:
1753
 * @ctxt:  the validation context
1754
 * @err:  the error code
1755
 * @arg1:  the first string argument
1756
 * @arg2:  the second string argument
1757
 * @dup:  arg need to be duplicated
1758
 *
1759
 * Pushes a new error on top of the error stack
1760
 *
1761
 * Returns 0 in case of error, the index in the stack otherwise
1762
 */
1763
static int
1764
xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1765
                         xmlRelaxNGValidErr err, const xmlChar * arg1,
1766
                         const xmlChar * arg2, int dup)
1767
0
{
1768
0
    xmlRelaxNGValidErrorPtr cur;
1769
1770
0
    if (ctxt->errTab == NULL) {
1771
0
        ctxt->errMax = 8;
1772
0
        ctxt->errNr = 0;
1773
0
        ctxt->errTab =
1774
0
            (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1775
0
                                                sizeof
1776
0
                                                (xmlRelaxNGValidError));
1777
0
        if (ctxt->errTab == NULL) {
1778
0
            xmlRngVErrMemory(ctxt);
1779
0
            return (0);
1780
0
        }
1781
0
        ctxt->err = NULL;
1782
0
    }
1783
0
    if (ctxt->errNr >= ctxt->errMax) {
1784
0
        ctxt->errMax *= 2;
1785
0
        ctxt->errTab =
1786
0
            (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1787
0
                                                 ctxt->errMax *
1788
0
                                                 sizeof
1789
0
                                                 (xmlRelaxNGValidError));
1790
0
        if (ctxt->errTab == NULL) {
1791
0
            xmlRngVErrMemory(ctxt);
1792
0
            return (0);
1793
0
        }
1794
0
        ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1795
0
    }
1796
0
    if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
1797
0
        (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1798
0
        return (ctxt->errNr);
1799
0
    cur = &ctxt->errTab[ctxt->errNr];
1800
0
    cur->err = err;
1801
0
    if (dup) {
1802
0
        cur->arg1 = xmlStrdup(arg1);
1803
0
        cur->arg2 = xmlStrdup(arg2);
1804
0
        cur->flags = ERROR_IS_DUP;
1805
0
    } else {
1806
0
        cur->arg1 = arg1;
1807
0
        cur->arg2 = arg2;
1808
0
        cur->flags = 0;
1809
0
    }
1810
0
    if (ctxt->state != NULL) {
1811
0
        cur->node = ctxt->state->node;
1812
0
        cur->seq = ctxt->state->seq;
1813
0
    } else {
1814
0
        cur->node = NULL;
1815
0
        cur->seq = NULL;
1816
0
    }
1817
0
    ctxt->err = cur;
1818
0
    return (ctxt->errNr++);
1819
0
}
1820
1821
/**
1822
 * xmlRelaxNGValidErrorPop:
1823
 * @ctxt: the validation context
1824
 *
1825
 * Pops the top error from the error stack
1826
 */
1827
static void
1828
xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1829
0
{
1830
0
    xmlRelaxNGValidErrorPtr cur;
1831
1832
0
    if (ctxt->errNr <= 0) {
1833
0
        ctxt->err = NULL;
1834
0
        return;
1835
0
    }
1836
0
    ctxt->errNr--;
1837
0
    if (ctxt->errNr > 0)
1838
0
        ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1839
0
    else
1840
0
        ctxt->err = NULL;
1841
0
    cur = &ctxt->errTab[ctxt->errNr];
1842
0
    if (cur->flags & ERROR_IS_DUP) {
1843
0
        if (cur->arg1 != NULL)
1844
0
            xmlFree((xmlChar *) cur->arg1);
1845
0
        cur->arg1 = NULL;
1846
0
        if (cur->arg2 != NULL)
1847
0
            xmlFree((xmlChar *) cur->arg2);
1848
0
        cur->arg2 = NULL;
1849
0
        cur->flags = 0;
1850
0
    }
1851
0
}
1852
1853
/**
1854
 * xmlRelaxNGDocumentPush:
1855
 * @ctxt:  the parser context
1856
 * @value:  the element doc
1857
 *
1858
 * Pushes a new doc on top of the doc stack
1859
 *
1860
 * Returns 0 in case of error, the index in the stack otherwise
1861
 */
1862
static int
1863
xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1864
                       xmlRelaxNGDocumentPtr value)
1865
0
{
1866
0
    if (ctxt->docTab == NULL) {
1867
0
        ctxt->docMax = 4;
1868
0
        ctxt->docNr = 0;
1869
0
        ctxt->docTab =
1870
0
            (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1871
0
                                                sizeof(ctxt->docTab[0]));
1872
0
        if (ctxt->docTab == NULL) {
1873
0
            xmlRngPErrMemory(ctxt);
1874
0
            return (0);
1875
0
        }
1876
0
    }
1877
0
    if (ctxt->docNr >= ctxt->docMax) {
1878
0
        ctxt->docMax *= 2;
1879
0
        ctxt->docTab =
1880
0
            (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1881
0
                                                 ctxt->docMax *
1882
0
                                                 sizeof(ctxt->docTab[0]));
1883
0
        if (ctxt->docTab == NULL) {
1884
0
            xmlRngPErrMemory(ctxt);
1885
0
            return (0);
1886
0
        }
1887
0
    }
1888
0
    ctxt->docTab[ctxt->docNr] = value;
1889
0
    ctxt->doc = value;
1890
0
    return (ctxt->docNr++);
1891
0
}
1892
1893
/**
1894
 * xmlRelaxNGDocumentPop:
1895
 * @ctxt: the parser context
1896
 *
1897
 * Pops the top doc from the doc stack
1898
 *
1899
 * Returns the doc just removed
1900
 */
1901
static xmlRelaxNGDocumentPtr
1902
xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1903
0
{
1904
0
    xmlRelaxNGDocumentPtr ret;
1905
1906
0
    if (ctxt->docNr <= 0)
1907
0
        return (NULL);
1908
0
    ctxt->docNr--;
1909
0
    if (ctxt->docNr > 0)
1910
0
        ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1911
0
    else
1912
0
        ctxt->doc = NULL;
1913
0
    ret = ctxt->docTab[ctxt->docNr];
1914
0
    ctxt->docTab[ctxt->docNr] = NULL;
1915
0
    return (ret);
1916
0
}
1917
1918
/**
1919
 * xmlRelaxNGLoadExternalRef:
1920
 * @ctxt: the parser context
1921
 * @URL:  the normalized URL
1922
 * @ns:  the inherited ns if any
1923
 *
1924
 * First lookup if the document is already loaded into the parser context,
1925
 * check against recursion. If not found the resource is loaded and
1926
 * the content is preprocessed before being returned back to the caller.
1927
 *
1928
 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1929
 */
1930
static xmlRelaxNGDocumentPtr
1931
xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1932
                          const xmlChar * URL, const xmlChar * ns)
1933
0
{
1934
0
    xmlRelaxNGDocumentPtr ret = NULL;
1935
0
    xmlDocPtr doc;
1936
0
    xmlNodePtr root;
1937
0
    int i;
1938
1939
    /*
1940
     * check against recursion in the stack
1941
     */
1942
0
    for (i = 0; i < ctxt->docNr; i++) {
1943
0
        if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1944
0
            xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1945
0
                       "Detected an externalRef recursion for %s\n", URL,
1946
0
                       NULL);
1947
0
            return (NULL);
1948
0
        }
1949
0
    }
1950
1951
    /*
1952
     * load the document
1953
     */
1954
0
    doc = xmlRelaxReadFile(ctxt, (const char *) URL);
1955
0
    if (doc == NULL) {
1956
0
        xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1957
0
                   "xmlRelaxNG: could not load %s\n", URL, NULL);
1958
0
        return (NULL);
1959
0
    }
1960
1961
    /*
1962
     * Allocate the document structures and register it first.
1963
     */
1964
0
    ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1965
0
    if (ret == NULL) {
1966
0
        xmlRngPErrMemory(ctxt);
1967
0
        xmlFreeDoc(doc);
1968
0
        return (NULL);
1969
0
    }
1970
0
    memset(ret, 0, sizeof(xmlRelaxNGDocument));
1971
0
    ret->doc = doc;
1972
0
    ret->href = xmlStrdup(URL);
1973
0
    ret->next = ctxt->documents;
1974
0
    ret->externalRef = 1;
1975
0
    ctxt->documents = ret;
1976
1977
    /*
1978
     * transmit the ns if needed
1979
     */
1980
0
    if (ns != NULL) {
1981
0
        root = xmlDocGetRootElement(doc);
1982
0
        if (root != NULL) {
1983
0
            if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1984
0
                xmlSetProp(root, BAD_CAST "ns", ns);
1985
0
            }
1986
0
        }
1987
0
    }
1988
1989
    /*
1990
     * push it on the stack and register it in the hash table
1991
     */
1992
0
    xmlRelaxNGDocumentPush(ctxt, ret);
1993
1994
    /*
1995
     * Some preprocessing of the document content
1996
     */
1997
0
    doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1998
0
    if (doc == NULL) {
1999
0
        ctxt->doc = NULL;
2000
0
        return (NULL);
2001
0
    }
2002
2003
0
    xmlRelaxNGDocumentPop(ctxt);
2004
2005
0
    return (ret);
2006
0
}
2007
2008
/************************************************************************
2009
 *                  *
2010
 *      Error functions         *
2011
 *                  *
2012
 ************************************************************************/
2013
2014
0
#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2015
0
#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2016
0
#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2017
0
#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2018
0
#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
2019
2020
static const char *
2021
xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2022
0
{
2023
0
    if (def == NULL)
2024
0
        return ("none");
2025
0
    switch (def->type) {
2026
0
        case XML_RELAXNG_EMPTY:
2027
0
            return ("empty");
2028
0
        case XML_RELAXNG_NOT_ALLOWED:
2029
0
            return ("notAllowed");
2030
0
        case XML_RELAXNG_EXCEPT:
2031
0
            return ("except");
2032
0
        case XML_RELAXNG_TEXT:
2033
0
            return ("text");
2034
0
        case XML_RELAXNG_ELEMENT:
2035
0
            return ("element");
2036
0
        case XML_RELAXNG_DATATYPE:
2037
0
            return ("datatype");
2038
0
        case XML_RELAXNG_VALUE:
2039
0
            return ("value");
2040
0
        case XML_RELAXNG_LIST:
2041
0
            return ("list");
2042
0
        case XML_RELAXNG_ATTRIBUTE:
2043
0
            return ("attribute");
2044
0
        case XML_RELAXNG_DEF:
2045
0
            return ("def");
2046
0
        case XML_RELAXNG_REF:
2047
0
            return ("ref");
2048
0
        case XML_RELAXNG_EXTERNALREF:
2049
0
            return ("externalRef");
2050
0
        case XML_RELAXNG_PARENTREF:
2051
0
            return ("parentRef");
2052
0
        case XML_RELAXNG_OPTIONAL:
2053
0
            return ("optional");
2054
0
        case XML_RELAXNG_ZEROORMORE:
2055
0
            return ("zeroOrMore");
2056
0
        case XML_RELAXNG_ONEORMORE:
2057
0
            return ("oneOrMore");
2058
0
        case XML_RELAXNG_CHOICE:
2059
0
            return ("choice");
2060
0
        case XML_RELAXNG_GROUP:
2061
0
            return ("group");
2062
0
        case XML_RELAXNG_INTERLEAVE:
2063
0
            return ("interleave");
2064
0
        case XML_RELAXNG_START:
2065
0
            return ("start");
2066
0
        case XML_RELAXNG_NOOP:
2067
0
            return ("noop");
2068
0
        case XML_RELAXNG_PARAM:
2069
0
            return ("param");
2070
0
    }
2071
0
    return ("unknown");
2072
0
}
2073
2074
/**
2075
 * xmlRelaxNGGetErrorString:
2076
 * @err:  the error code
2077
 * @arg1:  the first string argument
2078
 * @arg2:  the second string argument
2079
 *
2080
 * computes a formatted error string for the given error code and args
2081
 *
2082
 * Returns the error string, it must be deallocated by the caller
2083
 */
2084
static xmlChar *
2085
xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2086
                         const xmlChar * arg2)
2087
0
{
2088
0
    char msg[1000];
2089
0
    xmlChar *result;
2090
2091
0
    if (arg1 == NULL)
2092
0
        arg1 = BAD_CAST "";
2093
0
    if (arg2 == NULL)
2094
0
        arg2 = BAD_CAST "";
2095
2096
0
    msg[0] = 0;
2097
0
    switch (err) {
2098
0
        case XML_RELAXNG_OK:
2099
0
            return (NULL);
2100
0
        case XML_RELAXNG_ERR_MEMORY:
2101
0
            return (xmlCharStrdup("out of memory\n"));
2102
0
        case XML_RELAXNG_ERR_TYPE:
2103
0
            snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2104
0
            break;
2105
0
        case XML_RELAXNG_ERR_TYPEVAL:
2106
0
            snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2107
0
                     arg2);
2108
0
            break;
2109
0
        case XML_RELAXNG_ERR_DUPID:
2110
0
            snprintf(msg, 1000, "ID %s redefined\n", arg1);
2111
0
            break;
2112
0
        case XML_RELAXNG_ERR_TYPECMP:
2113
0
            snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2114
0
            break;
2115
0
        case XML_RELAXNG_ERR_NOSTATE:
2116
0
            return (xmlCharStrdup("Internal error: no state\n"));
2117
0
        case XML_RELAXNG_ERR_NODEFINE:
2118
0
            return (xmlCharStrdup("Internal error: no define\n"));
2119
0
        case XML_RELAXNG_ERR_INTERNAL:
2120
0
            snprintf(msg, 1000, "Internal error: %s\n", arg1);
2121
0
            break;
2122
0
        case XML_RELAXNG_ERR_LISTEXTRA:
2123
0
            snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2124
0
            break;
2125
0
        case XML_RELAXNG_ERR_INTERNODATA:
2126
0
            return (xmlCharStrdup
2127
0
                    ("Internal: interleave block has no data\n"));
2128
0
        case XML_RELAXNG_ERR_INTERSEQ:
2129
0
            return (xmlCharStrdup("Invalid sequence in interleave\n"));
2130
0
        case XML_RELAXNG_ERR_INTEREXTRA:
2131
0
            snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2132
0
            break;
2133
0
        case XML_RELAXNG_ERR_ELEMNAME:
2134
0
            snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2135
0
                     arg2);
2136
0
            break;
2137
0
        case XML_RELAXNG_ERR_ELEMNONS:
2138
0
            snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2139
0
                     arg1);
2140
0
            break;
2141
0
        case XML_RELAXNG_ERR_ELEMWRONGNS:
2142
0
            snprintf(msg, 1000,
2143
0
                     "Element %s has wrong namespace: expecting %s\n", arg1,
2144
0
                     arg2);
2145
0
            break;
2146
0
        case XML_RELAXNG_ERR_ELEMWRONG:
2147
0
            snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2148
0
            break;
2149
0
        case XML_RELAXNG_ERR_TEXTWRONG:
2150
0
            snprintf(msg, 1000,
2151
0
                     "Did not expect text in element %s content\n", arg1);
2152
0
            break;
2153
0
        case XML_RELAXNG_ERR_ELEMEXTRANS:
2154
0
            snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2155
0
                     arg1);
2156
0
            break;
2157
0
        case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2158
0
            snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2159
0
            break;
2160
0
        case XML_RELAXNG_ERR_NOELEM:
2161
0
            snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2162
0
                     arg1);
2163
0
            break;
2164
0
        case XML_RELAXNG_ERR_NOTELEM:
2165
0
            return (xmlCharStrdup("Expecting an element got text\n"));
2166
0
        case XML_RELAXNG_ERR_ATTRVALID:
2167
0
            snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2168
0
                     arg1);
2169
0
            break;
2170
0
        case XML_RELAXNG_ERR_CONTENTVALID:
2171
0
            snprintf(msg, 1000, "Element %s failed to validate content\n",
2172
0
                     arg1);
2173
0
            break;
2174
0
        case XML_RELAXNG_ERR_EXTRACONTENT:
2175
0
            snprintf(msg, 1000, "Element %s has extra content: %s\n",
2176
0
                     arg1, arg2);
2177
0
            break;
2178
0
        case XML_RELAXNG_ERR_INVALIDATTR:
2179
0
            snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2180
0
                     arg1, arg2);
2181
0
            break;
2182
0
        case XML_RELAXNG_ERR_LACKDATA:
2183
0
            snprintf(msg, 1000, "Datatype element %s contains no data\n",
2184
0
                     arg1);
2185
0
            break;
2186
0
        case XML_RELAXNG_ERR_DATAELEM:
2187
0
            snprintf(msg, 1000, "Datatype element %s has child elements\n",
2188
0
                     arg1);
2189
0
            break;
2190
0
        case XML_RELAXNG_ERR_VALELEM:
2191
0
            snprintf(msg, 1000, "Value element %s has child elements\n",
2192
0
                     arg1);
2193
0
            break;
2194
0
        case XML_RELAXNG_ERR_LISTELEM:
2195
0
            snprintf(msg, 1000, "List element %s has child elements\n",
2196
0
                     arg1);
2197
0
            break;
2198
0
        case XML_RELAXNG_ERR_DATATYPE:
2199
0
            snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2200
0
            break;
2201
0
        case XML_RELAXNG_ERR_VALUE:
2202
0
            snprintf(msg, 1000, "Error validating value %s\n", arg1);
2203
0
            break;
2204
0
        case XML_RELAXNG_ERR_LIST:
2205
0
            return (xmlCharStrdup("Error validating list\n"));
2206
0
        case XML_RELAXNG_ERR_NOGRAMMAR:
2207
0
            return (xmlCharStrdup("No top grammar defined\n"));
2208
0
        case XML_RELAXNG_ERR_EXTRADATA:
2209
0
            return (xmlCharStrdup("Extra data in the document\n"));
2210
0
        default:
2211
0
            return (xmlCharStrdup("Unknown error !\n"));
2212
0
    }
2213
0
    if (msg[0] == 0) {
2214
0
        snprintf(msg, 1000, "Unknown error code %d\n", err);
2215
0
    }
2216
0
    msg[1000 - 1] = 0;
2217
0
    result = xmlCharStrdup(msg);
2218
0
    return (xmlEscapeFormatString(&result));
2219
0
}
2220
2221
/**
2222
 * xmlRelaxNGShowValidError:
2223
 * @ctxt:  the validation context
2224
 * @err:  the error number
2225
 * @node:  the node
2226
 * @child:  the node child generating the problem.
2227
 * @arg1:  the first argument
2228
 * @arg2:  the second argument
2229
 *
2230
 * Show a validation error.
2231
 */
2232
static void
2233
xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2234
                         xmlRelaxNGValidErr err, xmlNodePtr node,
2235
                         xmlNodePtr child, const xmlChar * arg1,
2236
                         const xmlChar * arg2)
2237
0
{
2238
0
    xmlChar *msg;
2239
2240
0
    if (ctxt->flags & FLAGS_NOERROR)
2241
0
        return;
2242
2243
0
    msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2244
0
    if (msg == NULL)
2245
0
        return;
2246
2247
0
    if (ctxt->errNo == XML_RELAXNG_OK)
2248
0
        ctxt->errNo = err;
2249
0
    xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2250
0
               (const char *) msg, arg1, arg2);
2251
0
    xmlFree(msg);
2252
0
}
2253
2254
/**
2255
 * xmlRelaxNGPopErrors:
2256
 * @ctxt:  the validation context
2257
 * @level:  the error level in the stack
2258
 *
2259
 * pop and discard all errors until the given level is reached
2260
 */
2261
static void
2262
xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2263
0
{
2264
0
    int i;
2265
0
    xmlRelaxNGValidErrorPtr err;
2266
2267
0
    for (i = level; i < ctxt->errNr; i++) {
2268
0
        err = &ctxt->errTab[i];
2269
0
        if (err->flags & ERROR_IS_DUP) {
2270
0
            if (err->arg1 != NULL)
2271
0
                xmlFree((xmlChar *) err->arg1);
2272
0
            err->arg1 = NULL;
2273
0
            if (err->arg2 != NULL)
2274
0
                xmlFree((xmlChar *) err->arg2);
2275
0
            err->arg2 = NULL;
2276
0
            err->flags = 0;
2277
0
        }
2278
0
    }
2279
0
    ctxt->errNr = level;
2280
0
    if (ctxt->errNr <= 0)
2281
0
        ctxt->err = NULL;
2282
0
}
2283
2284
/**
2285
 * xmlRelaxNGDumpValidError:
2286
 * @ctxt:  the validation context
2287
 *
2288
 * Show all validation error over a given index.
2289
 */
2290
static void
2291
xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2292
0
{
2293
0
    int i, j, k;
2294
0
    xmlRelaxNGValidErrorPtr err, dup;
2295
2296
0
    for (i = 0, k = 0; i < ctxt->errNr; i++) {
2297
0
        err = &ctxt->errTab[i];
2298
0
        if (k < MAX_ERROR) {
2299
0
            for (j = 0; j < i; j++) {
2300
0
                dup = &ctxt->errTab[j];
2301
0
                if ((err->err == dup->err) && (err->node == dup->node) &&
2302
0
                    (xmlStrEqual(err->arg1, dup->arg1)) &&
2303
0
                    (xmlStrEqual(err->arg2, dup->arg2))) {
2304
0
                    goto skip;
2305
0
                }
2306
0
            }
2307
0
            xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2308
0
                                     err->arg1, err->arg2);
2309
0
            k++;
2310
0
        }
2311
0
      skip:
2312
0
        if (err->flags & ERROR_IS_DUP) {
2313
0
            if (err->arg1 != NULL)
2314
0
                xmlFree((xmlChar *) err->arg1);
2315
0
            err->arg1 = NULL;
2316
0
            if (err->arg2 != NULL)
2317
0
                xmlFree((xmlChar *) err->arg2);
2318
0
            err->arg2 = NULL;
2319
0
            err->flags = 0;
2320
0
        }
2321
0
    }
2322
0
    ctxt->errNr = 0;
2323
0
}
2324
2325
/**
2326
 * xmlRelaxNGAddValidError:
2327
 * @ctxt:  the validation context
2328
 * @err:  the error number
2329
 * @arg1:  the first argument
2330
 * @arg2:  the second argument
2331
 * @dup:  need to dup the args
2332
 *
2333
 * Register a validation error, either generating it if it's sure
2334
 * or stacking it for later handling if unsure.
2335
 */
2336
static void
2337
xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2338
                        xmlRelaxNGValidErr err, const xmlChar * arg1,
2339
                        const xmlChar * arg2, int dup)
2340
0
{
2341
0
    if (ctxt == NULL)
2342
0
        return;
2343
0
    if (ctxt->flags & FLAGS_NOERROR)
2344
0
        return;
2345
2346
    /*
2347
     * generate the error directly
2348
     */
2349
0
    if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2350
0
   (ctxt->flags & FLAGS_NEGATIVE)) {
2351
0
        xmlNodePtr node, seq;
2352
2353
        /*
2354
         * Flush first any stacked error which might be the
2355
         * real cause of the problem.
2356
         */
2357
0
        if (ctxt->errNr != 0)
2358
0
            xmlRelaxNGDumpValidError(ctxt);
2359
0
        if (ctxt->state != NULL) {
2360
0
            node = ctxt->state->node;
2361
0
            seq = ctxt->state->seq;
2362
0
        } else {
2363
0
            node = seq = NULL;
2364
0
        }
2365
0
        if ((node == NULL) && (seq == NULL)) {
2366
0
            node = ctxt->pnode;
2367
0
        }
2368
0
        xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2369
0
    }
2370
    /*
2371
     * Stack the error for later processing if needed
2372
     */
2373
0
    else {
2374
0
        xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2375
0
    }
2376
0
}
2377
2378
2379
/************************************************************************
2380
 *                  *
2381
 *      Type library hooks        *
2382
 *                  *
2383
 ************************************************************************/
2384
static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2385
                                    const xmlChar * str);
2386
2387
/**
2388
 * xmlRelaxNGSchemaTypeHave:
2389
 * @data:  data needed for the library
2390
 * @type:  the type name
2391
 *
2392
 * Check if the given type is provided by
2393
 * the W3C XMLSchema Datatype library.
2394
 *
2395
 * Returns 1 if yes, 0 if no and -1 in case of error.
2396
 */
2397
static int
2398
xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2399
0
{
2400
0
    xmlSchemaTypePtr typ;
2401
2402
0
    if (type == NULL)
2403
0
        return (-1);
2404
0
    typ = xmlSchemaGetPredefinedType(type,
2405
0
                                     BAD_CAST
2406
0
                                     "http://www.w3.org/2001/XMLSchema");
2407
0
    if (typ == NULL)
2408
0
        return (0);
2409
0
    return (1);
2410
0
}
2411
2412
/**
2413
 * xmlRelaxNGSchemaTypeCheck:
2414
 * @data:  data needed for the library
2415
 * @type:  the type name
2416
 * @value:  the value to check
2417
 * @node:  the node
2418
 *
2419
 * Check if the given type and value are validated by
2420
 * the W3C XMLSchema Datatype library.
2421
 *
2422
 * Returns 1 if yes, 0 if no and -1 in case of error.
2423
 */
2424
static int
2425
xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2426
                          const xmlChar * type,
2427
                          const xmlChar * value,
2428
                          void **result, xmlNodePtr node)
2429
0
{
2430
0
    xmlSchemaTypePtr typ;
2431
0
    int ret;
2432
2433
0
    if ((type == NULL) || (value == NULL))
2434
0
        return (-1);
2435
0
    typ = xmlSchemaGetPredefinedType(type,
2436
0
                                     BAD_CAST
2437
0
                                     "http://www.w3.org/2001/XMLSchema");
2438
0
    if (typ == NULL)
2439
0
        return (-1);
2440
0
    ret = xmlSchemaValPredefTypeNode(typ, value,
2441
0
                                     (xmlSchemaValPtr *) result, node);
2442
0
    if (ret == 2)               /* special ID error code */
2443
0
        return (2);
2444
0
    if (ret == 0)
2445
0
        return (1);
2446
0
    if (ret > 0)
2447
0
        return (0);
2448
0
    return (-1);
2449
0
}
2450
2451
/**
2452
 * xmlRelaxNGSchemaFacetCheck:
2453
 * @data:  data needed for the library
2454
 * @type:  the type name
2455
 * @facet:  the facet name
2456
 * @val:  the facet value
2457
 * @strval:  the string value
2458
 * @value:  the value to check
2459
 *
2460
 * Function provided by a type library to check a value facet
2461
 *
2462
 * Returns 1 if yes, 0 if no and -1 in case of error.
2463
 */
2464
static int
2465
xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2466
                           const xmlChar * type, const xmlChar * facetname,
2467
                           const xmlChar * val, const xmlChar * strval,
2468
                           void *value)
2469
0
{
2470
0
    xmlSchemaFacetPtr facet;
2471
0
    xmlSchemaTypePtr typ;
2472
0
    int ret;
2473
2474
0
    if ((type == NULL) || (strval == NULL))
2475
0
        return (-1);
2476
0
    typ = xmlSchemaGetPredefinedType(type,
2477
0
                                     BAD_CAST
2478
0
                                     "http://www.w3.org/2001/XMLSchema");
2479
0
    if (typ == NULL)
2480
0
        return (-1);
2481
2482
0
    facet = xmlSchemaNewFacet();
2483
0
    if (facet == NULL)
2484
0
        return (-1);
2485
2486
0
    if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2487
0
        facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2488
0
    } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2489
0
        facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2490
0
    } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2491
0
        facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2492
0
    } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2493
0
        facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2494
0
    } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2495
0
        facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2496
0
    } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2497
0
        facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2498
0
    } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2499
0
        facet->type = XML_SCHEMA_FACET_PATTERN;
2500
0
    } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2501
0
        facet->type = XML_SCHEMA_FACET_ENUMERATION;
2502
0
    } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2503
0
        facet->type = XML_SCHEMA_FACET_WHITESPACE;
2504
0
    } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2505
0
        facet->type = XML_SCHEMA_FACET_LENGTH;
2506
0
    } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2507
0
        facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2508
0
    } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2509
0
        facet->type = XML_SCHEMA_FACET_MINLENGTH;
2510
0
    } else {
2511
0
        xmlSchemaFreeFacet(facet);
2512
0
        return (-1);
2513
0
    }
2514
0
    facet->value = val;
2515
0
    ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2516
0
    if (ret != 0) {
2517
0
        xmlSchemaFreeFacet(facet);
2518
0
        return (-1);
2519
0
    }
2520
0
    ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2521
0
    xmlSchemaFreeFacet(facet);
2522
0
    if (ret != 0)
2523
0
        return (-1);
2524
0
    return (0);
2525
0
}
2526
2527
/**
2528
 * xmlRelaxNGSchemaFreeValue:
2529
 * @data:  data needed for the library
2530
 * @value:  the value to free
2531
 *
2532
 * Function provided by a type library to free a Schemas value
2533
 *
2534
 * Returns 1 if yes, 0 if no and -1 in case of error.
2535
 */
2536
static void
2537
xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2538
0
{
2539
0
    xmlSchemaFreeValue(value);
2540
0
}
2541
2542
/**
2543
 * xmlRelaxNGSchemaTypeCompare:
2544
 * @data:  data needed for the library
2545
 * @type:  the type name
2546
 * @value1:  the first value
2547
 * @value2:  the second value
2548
 *
2549
 * Compare two values for equality accordingly a type from the W3C XMLSchema
2550
 * Datatype library.
2551
 *
2552
 * Returns 1 if equal, 0 if no and -1 in case of error.
2553
 */
2554
static int
2555
xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2556
                            const xmlChar * type,
2557
                            const xmlChar * value1,
2558
                            xmlNodePtr ctxt1,
2559
                            void *comp1,
2560
                            const xmlChar * value2, xmlNodePtr ctxt2)
2561
0
{
2562
0
    int ret;
2563
0
    xmlSchemaTypePtr typ;
2564
0
    xmlSchemaValPtr res1 = NULL, res2 = NULL;
2565
2566
0
    if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2567
0
        return (-1);
2568
0
    typ = xmlSchemaGetPredefinedType(type,
2569
0
                                     BAD_CAST
2570
0
                                     "http://www.w3.org/2001/XMLSchema");
2571
0
    if (typ == NULL)
2572
0
        return (-1);
2573
0
    if (comp1 == NULL) {
2574
0
        ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2575
0
        if (ret != 0)
2576
0
            return (-1);
2577
0
        if (res1 == NULL)
2578
0
            return (-1);
2579
0
    } else {
2580
0
        res1 = (xmlSchemaValPtr) comp1;
2581
0
    }
2582
0
    ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2583
0
    if (ret != 0) {
2584
0
  if (res1 != (xmlSchemaValPtr) comp1)
2585
0
      xmlSchemaFreeValue(res1);
2586
0
        return (-1);
2587
0
    }
2588
0
    ret = xmlSchemaCompareValues(res1, res2);
2589
0
    if (res1 != (xmlSchemaValPtr) comp1)
2590
0
        xmlSchemaFreeValue(res1);
2591
0
    xmlSchemaFreeValue(res2);
2592
0
    if (ret == -2)
2593
0
        return (-1);
2594
0
    if (ret == 0)
2595
0
        return (1);
2596
0
    return (0);
2597
0
}
2598
2599
/**
2600
 * xmlRelaxNGDefaultTypeHave:
2601
 * @data:  data needed for the library
2602
 * @type:  the type name
2603
 *
2604
 * Check if the given type is provided by
2605
 * the default datatype library.
2606
 *
2607
 * Returns 1 if yes, 0 if no and -1 in case of error.
2608
 */
2609
static int
2610
xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2611
                          const xmlChar * type)
2612
0
{
2613
0
    if (type == NULL)
2614
0
        return (-1);
2615
0
    if (xmlStrEqual(type, BAD_CAST "string"))
2616
0
        return (1);
2617
0
    if (xmlStrEqual(type, BAD_CAST "token"))
2618
0
        return (1);
2619
0
    return (0);
2620
0
}
2621
2622
/**
2623
 * xmlRelaxNGDefaultTypeCheck:
2624
 * @data:  data needed for the library
2625
 * @type:  the type name
2626
 * @value:  the value to check
2627
 * @node:  the node
2628
 *
2629
 * Check if the given type and value are validated by
2630
 * the default datatype library.
2631
 *
2632
 * Returns 1 if yes, 0 if no and -1 in case of error.
2633
 */
2634
static int
2635
xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2636
                           const xmlChar * type ATTRIBUTE_UNUSED,
2637
                           const xmlChar * value ATTRIBUTE_UNUSED,
2638
                           void **result ATTRIBUTE_UNUSED,
2639
                           xmlNodePtr node ATTRIBUTE_UNUSED)
2640
0
{
2641
0
    if (value == NULL)
2642
0
        return (-1);
2643
0
    if (xmlStrEqual(type, BAD_CAST "string"))
2644
0
        return (1);
2645
0
    if (xmlStrEqual(type, BAD_CAST "token")) {
2646
0
        return (1);
2647
0
    }
2648
2649
0
    return (0);
2650
0
}
2651
2652
/**
2653
 * xmlRelaxNGDefaultTypeCompare:
2654
 * @data:  data needed for the library
2655
 * @type:  the type name
2656
 * @value1:  the first value
2657
 * @value2:  the second value
2658
 *
2659
 * Compare two values accordingly a type from the default
2660
 * datatype library.
2661
 *
2662
 * Returns 1 if yes, 0 if no and -1 in case of error.
2663
 */
2664
static int
2665
xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2666
                             const xmlChar * type,
2667
                             const xmlChar * value1,
2668
                             xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2669
                             void *comp1 ATTRIBUTE_UNUSED,
2670
                             const xmlChar * value2,
2671
                             xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2672
0
{
2673
0
    int ret = -1;
2674
2675
0
    if (xmlStrEqual(type, BAD_CAST "string")) {
2676
0
        ret = xmlStrEqual(value1, value2);
2677
0
    } else if (xmlStrEqual(type, BAD_CAST "token")) {
2678
0
        if (!xmlStrEqual(value1, value2)) {
2679
0
            xmlChar *nval, *nvalue;
2680
2681
            /*
2682
             * TODO: trivial optimizations are possible by
2683
             * computing at compile-time
2684
             */
2685
0
            nval = xmlRelaxNGNormalize(NULL, value1);
2686
0
            nvalue = xmlRelaxNGNormalize(NULL, value2);
2687
2688
0
            if ((nval == NULL) || (nvalue == NULL))
2689
0
                ret = -1;
2690
0
            else if (xmlStrEqual(nval, nvalue))
2691
0
                ret = 1;
2692
0
            else
2693
0
                ret = 0;
2694
0
            if (nval != NULL)
2695
0
                xmlFree(nval);
2696
0
            if (nvalue != NULL)
2697
0
                xmlFree(nvalue);
2698
0
        } else
2699
0
            ret = 1;
2700
0
    }
2701
0
    return (ret);
2702
0
}
2703
2704
static int xmlRelaxNGTypeInitialized = 0;
2705
static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2706
2707
/**
2708
 * xmlRelaxNGFreeTypeLibrary:
2709
 * @lib:  the type library structure
2710
 * @namespace:  the URI bound to the library
2711
 *
2712
 * Free the structure associated to the type library
2713
 */
2714
static void
2715
xmlRelaxNGFreeTypeLibrary(void *payload,
2716
                          const xmlChar * namespace ATTRIBUTE_UNUSED)
2717
0
{
2718
0
    xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload;
2719
0
    if (lib == NULL)
2720
0
        return;
2721
0
    if (lib->namespace != NULL)
2722
0
        xmlFree((xmlChar *) lib->namespace);
2723
0
    xmlFree(lib);
2724
0
}
2725
2726
/**
2727
 * xmlRelaxNGRegisterTypeLibrary:
2728
 * @namespace:  the URI bound to the library
2729
 * @data:  data associated to the library
2730
 * @have:  the provide function
2731
 * @check:  the checking function
2732
 * @comp:  the comparison function
2733
 *
2734
 * Register a new type library
2735
 *
2736
 * Returns 0 in case of success and -1 in case of error.
2737
 */
2738
static int
2739
xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2740
                              xmlRelaxNGTypeHave have,
2741
                              xmlRelaxNGTypeCheck check,
2742
                              xmlRelaxNGTypeCompare comp,
2743
                              xmlRelaxNGFacetCheck facet,
2744
                              xmlRelaxNGTypeFree freef)
2745
0
{
2746
0
    xmlRelaxNGTypeLibraryPtr lib;
2747
0
    int ret;
2748
2749
0
    if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2750
0
        (check == NULL) || (comp == NULL))
2751
0
        return (-1);
2752
0
    if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL)
2753
0
        return (-1);
2754
0
    lib =
2755
0
        (xmlRelaxNGTypeLibraryPtr)
2756
0
        xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2757
0
    if (lib == NULL) {
2758
0
        xmlRngVErrMemory(NULL);
2759
0
        return (-1);
2760
0
    }
2761
0
    memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2762
0
    lib->namespace = xmlStrdup(namespace);
2763
0
    lib->data = data;
2764
0
    lib->have = have;
2765
0
    lib->comp = comp;
2766
0
    lib->check = check;
2767
0
    lib->facet = facet;
2768
0
    lib->freef = freef;
2769
0
    ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2770
0
    if (ret < 0) {
2771
0
        xmlRelaxNGFreeTypeLibrary(lib, namespace);
2772
0
        return (-1);
2773
0
    }
2774
0
    return (0);
2775
0
}
2776
2777
/**
2778
 * xmlRelaxNGInitTypes:
2779
 *
2780
 * Initialize the default type libraries.
2781
 *
2782
 * Returns 0 in case of success and -1 in case of error.
2783
 */
2784
int
2785
xmlRelaxNGInitTypes(void)
2786
0
{
2787
0
    if (xmlRelaxNGTypeInitialized != 0)
2788
0
        return (0);
2789
0
    xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2790
0
    if (xmlRelaxNGRegisteredTypes == NULL)
2791
0
        return (-1);
2792
0
    xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2793
0
                                  "http://www.w3.org/2001/XMLSchema-datatypes",
2794
0
                                  NULL, xmlRelaxNGSchemaTypeHave,
2795
0
                                  xmlRelaxNGSchemaTypeCheck,
2796
0
                                  xmlRelaxNGSchemaTypeCompare,
2797
0
                                  xmlRelaxNGSchemaFacetCheck,
2798
0
                                  xmlRelaxNGSchemaFreeValue);
2799
0
    xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2800
0
                                  xmlRelaxNGDefaultTypeHave,
2801
0
                                  xmlRelaxNGDefaultTypeCheck,
2802
0
                                  xmlRelaxNGDefaultTypeCompare, NULL,
2803
0
                                  NULL);
2804
0
    xmlRelaxNGTypeInitialized = 1;
2805
0
    return (0);
2806
0
}
2807
2808
/**
2809
 * xmlRelaxNGCleanupTypes:
2810
 *
2811
 * DEPRECATED: This function will be made private. Call xmlCleanupParser
2812
 * to free global state but see the warnings there. xmlCleanupParser
2813
 * should be only called once at program exit. In most cases, you don't
2814
 * have call cleanup functions at all.
2815
 *
2816
 * Cleanup the default Schemas type library associated to RelaxNG
2817
 */
2818
void
2819
xmlRelaxNGCleanupTypes(void)
2820
9.89k
{
2821
9.89k
    xmlSchemaCleanupTypes();
2822
9.89k
    if (xmlRelaxNGTypeInitialized == 0)
2823
9.89k
        return;
2824
0
    xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary);
2825
0
    xmlRelaxNGTypeInitialized = 0;
2826
0
}
2827
2828
/************************************************************************
2829
 *                  *
2830
 *    Compiling element content into regexp     *
2831
 *                  *
2832
 * Sometime the element content can be compiled into a pure regexp, *
2833
 * This allows a faster execution and streamability at that level *
2834
 *                  *
2835
 ************************************************************************/
2836
2837
static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2838
                                xmlRelaxNGDefinePtr def);
2839
2840
/**
2841
 * xmlRelaxNGIsCompilable:
2842
 * @define:  the definition to check
2843
 *
2844
 * Check if a definition is nullable.
2845
 *
2846
 * Returns 1 if yes, 0 if no and -1 in case of error
2847
 */
2848
static int
2849
xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def)
2850
0
{
2851
0
    int ret = -1;
2852
2853
0
    if (def == NULL) {
2854
0
        return (-1);
2855
0
    }
2856
0
    if ((def->type != XML_RELAXNG_ELEMENT) &&
2857
0
        (def->dflags & IS_COMPILABLE))
2858
0
        return (1);
2859
0
    if ((def->type != XML_RELAXNG_ELEMENT) &&
2860
0
        (def->dflags & IS_NOT_COMPILABLE))
2861
0
        return (0);
2862
0
    switch (def->type) {
2863
0
        case XML_RELAXNG_NOOP:
2864
0
            ret = xmlRelaxNGIsCompilable(def->content);
2865
0
            break;
2866
0
        case XML_RELAXNG_TEXT:
2867
0
        case XML_RELAXNG_EMPTY:
2868
0
            ret = 1;
2869
0
            break;
2870
0
        case XML_RELAXNG_ELEMENT:
2871
            /*
2872
             * Check if the element content is compilable
2873
             */
2874
0
            if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2875
0
                ((def->dflags & IS_COMPILABLE) == 0)) {
2876
0
                xmlRelaxNGDefinePtr list;
2877
2878
0
                list = def->content;
2879
0
                while (list != NULL) {
2880
0
                    ret = xmlRelaxNGIsCompilable(list);
2881
0
                    if (ret != 1)
2882
0
                        break;
2883
0
                    list = list->next;
2884
0
                }
2885
    /*
2886
     * Because the routine is recursive, we must guard against
2887
     * discovering both COMPILABLE and NOT_COMPILABLE
2888
     */
2889
0
                if (ret == 0) {
2890
0
        def->dflags &= ~IS_COMPILABLE;
2891
0
                    def->dflags |= IS_NOT_COMPILABLE;
2892
0
    }
2893
0
                if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2894
0
                    def->dflags |= IS_COMPILABLE;
2895
0
            }
2896
            /*
2897
             * All elements return a compilable status unless they
2898
             * are generic like anyName
2899
             */
2900
0
            if ((def->nameClass != NULL) || (def->name == NULL))
2901
0
                ret = 0;
2902
0
            else
2903
0
                ret = 1;
2904
0
            return (ret);
2905
0
        case XML_RELAXNG_REF:
2906
0
        case XML_RELAXNG_EXTERNALREF:
2907
0
        case XML_RELAXNG_PARENTREF:
2908
0
            if (def->depth == -20) {
2909
0
                return (1);
2910
0
            } else {
2911
0
                xmlRelaxNGDefinePtr list;
2912
2913
0
                def->depth = -20;
2914
0
                list = def->content;
2915
0
                while (list != NULL) {
2916
0
                    ret = xmlRelaxNGIsCompilable(list);
2917
0
                    if (ret != 1)
2918
0
                        break;
2919
0
                    list = list->next;
2920
0
                }
2921
0
            }
2922
0
            break;
2923
0
        case XML_RELAXNG_START:
2924
0
        case XML_RELAXNG_OPTIONAL:
2925
0
        case XML_RELAXNG_ZEROORMORE:
2926
0
        case XML_RELAXNG_ONEORMORE:
2927
0
        case XML_RELAXNG_CHOICE:
2928
0
        case XML_RELAXNG_GROUP:
2929
0
        case XML_RELAXNG_DEF:{
2930
0
                xmlRelaxNGDefinePtr list;
2931
2932
0
                list = def->content;
2933
0
                while (list != NULL) {
2934
0
                    ret = xmlRelaxNGIsCompilable(list);
2935
0
                    if (ret != 1)
2936
0
                        break;
2937
0
                    list = list->next;
2938
0
                }
2939
0
                break;
2940
0
            }
2941
0
        case XML_RELAXNG_EXCEPT:
2942
0
        case XML_RELAXNG_ATTRIBUTE:
2943
0
        case XML_RELAXNG_INTERLEAVE:
2944
0
        case XML_RELAXNG_DATATYPE:
2945
0
        case XML_RELAXNG_LIST:
2946
0
        case XML_RELAXNG_PARAM:
2947
0
        case XML_RELAXNG_VALUE:
2948
0
        case XML_RELAXNG_NOT_ALLOWED:
2949
0
            ret = 0;
2950
0
            break;
2951
0
    }
2952
0
    if (ret == 0)
2953
0
        def->dflags |= IS_NOT_COMPILABLE;
2954
0
    if (ret == 1)
2955
0
        def->dflags |= IS_COMPILABLE;
2956
0
    return (ret);
2957
0
}
2958
2959
/**
2960
 * xmlRelaxNGCompile:
2961
 * ctxt:  the RelaxNG parser context
2962
 * @define:  the definition tree to compile
2963
 *
2964
 * Compile the set of definitions, it works recursively, till the
2965
 * element boundaries, where it tries to compile the content if possible
2966
 *
2967
 * Returns 0 if success and -1 in case of error
2968
 */
2969
static int
2970
xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
2971
0
{
2972
0
    int ret = 0;
2973
0
    xmlRelaxNGDefinePtr list;
2974
2975
0
    if ((ctxt == NULL) || (def == NULL))
2976
0
        return (-1);
2977
2978
0
    switch (def->type) {
2979
0
        case XML_RELAXNG_START:
2980
0
            if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) {
2981
0
                xmlAutomataPtr oldam = ctxt->am;
2982
0
                xmlAutomataStatePtr oldstate = ctxt->state;
2983
2984
0
                def->depth = -25;
2985
2986
0
                list = def->content;
2987
0
                ctxt->am = xmlNewAutomata();
2988
0
                if (ctxt->am == NULL)
2989
0
                    return (-1);
2990
2991
                /*
2992
                 * assume identical strings but not same pointer are different
2993
                 * atoms, needed for non-determinism detection
2994
                 * That way if 2 elements with the same name are in a choice
2995
                 * branch the automata is found non-deterministic and
2996
                 * we fallback to the normal validation which does the right
2997
                 * thing of exploring both choices.
2998
                 */
2999
0
                xmlAutomataSetFlags(ctxt->am, 1);
3000
3001
0
                ctxt->state = xmlAutomataGetInitState(ctxt->am);
3002
0
                while (list != NULL) {
3003
0
                    xmlRelaxNGCompile(ctxt, list);
3004
0
                    list = list->next;
3005
0
                }
3006
0
                xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3007
0
                if (xmlAutomataIsDeterminist(ctxt->am))
3008
0
                    def->contModel = xmlAutomataCompile(ctxt->am);
3009
3010
0
                xmlFreeAutomata(ctxt->am);
3011
0
                ctxt->state = oldstate;
3012
0
                ctxt->am = oldam;
3013
0
            }
3014
0
            break;
3015
0
        case XML_RELAXNG_ELEMENT:
3016
0
            if ((ctxt->am != NULL) && (def->name != NULL)) {
3017
0
                ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3018
0
                                                        ctxt->state, NULL,
3019
0
                                                        def->name, def->ns,
3020
0
                                                        def);
3021
0
            }
3022
0
            if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3023
0
                xmlAutomataPtr oldam = ctxt->am;
3024
0
                xmlAutomataStatePtr oldstate = ctxt->state;
3025
3026
0
                def->depth = -25;
3027
3028
0
                list = def->content;
3029
0
                ctxt->am = xmlNewAutomata();
3030
0
                if (ctxt->am == NULL)
3031
0
                    return (-1);
3032
0
                xmlAutomataSetFlags(ctxt->am, 1);
3033
0
                ctxt->state = xmlAutomataGetInitState(ctxt->am);
3034
0
                while (list != NULL) {
3035
0
                    xmlRelaxNGCompile(ctxt, list);
3036
0
                    list = list->next;
3037
0
                }
3038
0
                xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3039
0
                def->contModel = xmlAutomataCompile(ctxt->am);
3040
0
                if (!xmlRegexpIsDeterminist(def->contModel)) {
3041
                    /*
3042
                     * we can only use the automata if it is determinist
3043
                     */
3044
0
                    xmlRegFreeRegexp(def->contModel);
3045
0
                    def->contModel = NULL;
3046
0
                }
3047
0
                xmlFreeAutomata(ctxt->am);
3048
0
                ctxt->state = oldstate;
3049
0
                ctxt->am = oldam;
3050
0
            } else {
3051
0
                xmlAutomataPtr oldam = ctxt->am;
3052
3053
                /*
3054
                 * we can't build the content model for this element content
3055
                 * but it still might be possible to build it for some of its
3056
                 * children, recurse.
3057
                 */
3058
0
                ret = xmlRelaxNGTryCompile(ctxt, def);
3059
0
                ctxt->am = oldam;
3060
0
            }
3061
0
            break;
3062
0
        case XML_RELAXNG_NOOP:
3063
0
            ret = xmlRelaxNGCompile(ctxt, def->content);
3064
0
            break;
3065
0
        case XML_RELAXNG_OPTIONAL:{
3066
0
                xmlAutomataStatePtr oldstate = ctxt->state;
3067
3068
0
                list = def->content;
3069
0
                while (list != NULL) {
3070
0
                    xmlRelaxNGCompile(ctxt, list);
3071
0
                    list = list->next;
3072
0
                }
3073
0
                xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3074
0
                break;
3075
0
            }
3076
0
        case XML_RELAXNG_ZEROORMORE:{
3077
0
                xmlAutomataStatePtr oldstate;
3078
3079
0
                ctxt->state =
3080
0
                    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3081
0
                oldstate = ctxt->state;
3082
0
                list = def->content;
3083
0
                while (list != NULL) {
3084
0
                    xmlRelaxNGCompile(ctxt, list);
3085
0
                    list = list->next;
3086
0
                }
3087
0
                xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3088
0
                ctxt->state =
3089
0
                    xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3090
0
                break;
3091
0
            }
3092
0
        case XML_RELAXNG_ONEORMORE:{
3093
0
                xmlAutomataStatePtr oldstate;
3094
3095
0
                list = def->content;
3096
0
                while (list != NULL) {
3097
0
                    xmlRelaxNGCompile(ctxt, list);
3098
0
                    list = list->next;
3099
0
                }
3100
0
                oldstate = ctxt->state;
3101
0
                list = def->content;
3102
0
                while (list != NULL) {
3103
0
                    xmlRelaxNGCompile(ctxt, list);
3104
0
                    list = list->next;
3105
0
                }
3106
0
                xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3107
0
                ctxt->state =
3108
0
                    xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3109
0
                break;
3110
0
            }
3111
0
        case XML_RELAXNG_CHOICE:{
3112
0
                xmlAutomataStatePtr target = NULL;
3113
0
                xmlAutomataStatePtr oldstate = ctxt->state;
3114
3115
0
                list = def->content;
3116
0
                while (list != NULL) {
3117
0
                    ctxt->state = oldstate;
3118
0
                    ret = xmlRelaxNGCompile(ctxt, list);
3119
0
                    if (ret != 0)
3120
0
                        break;
3121
0
                    if (target == NULL)
3122
0
                        target = ctxt->state;
3123
0
                    else {
3124
0
                        xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3125
0
                                              target);
3126
0
                    }
3127
0
                    list = list->next;
3128
0
                }
3129
0
                ctxt->state = target;
3130
3131
0
                break;
3132
0
            }
3133
0
        case XML_RELAXNG_REF:
3134
0
        case XML_RELAXNG_EXTERNALREF:
3135
0
        case XML_RELAXNG_PARENTREF:
3136
0
        case XML_RELAXNG_GROUP:
3137
0
        case XML_RELAXNG_DEF:
3138
0
            list = def->content;
3139
0
            while (list != NULL) {
3140
0
                ret = xmlRelaxNGCompile(ctxt, list);
3141
0
                if (ret != 0)
3142
0
                    break;
3143
0
                list = list->next;
3144
0
            }
3145
0
            break;
3146
0
        case XML_RELAXNG_TEXT:{
3147
0
                xmlAutomataStatePtr oldstate;
3148
3149
0
                ctxt->state =
3150
0
                    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3151
0
                oldstate = ctxt->state;
3152
0
                xmlRelaxNGCompile(ctxt, def->content);
3153
0
                xmlAutomataNewTransition(ctxt->am, ctxt->state,
3154
0
                                         ctxt->state, BAD_CAST "#text",
3155
0
                                         NULL);
3156
0
                ctxt->state =
3157
0
                    xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3158
0
                break;
3159
0
            }
3160
0
        case XML_RELAXNG_EMPTY:
3161
0
            ctxt->state =
3162
0
                xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3163
0
            break;
3164
0
        case XML_RELAXNG_EXCEPT:
3165
0
        case XML_RELAXNG_ATTRIBUTE:
3166
0
        case XML_RELAXNG_INTERLEAVE:
3167
0
        case XML_RELAXNG_NOT_ALLOWED:
3168
0
        case XML_RELAXNG_DATATYPE:
3169
0
        case XML_RELAXNG_LIST:
3170
0
        case XML_RELAXNG_PARAM:
3171
0
        case XML_RELAXNG_VALUE:
3172
0
            xmlRngPErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
3173
0
                       "RNG internal error trying to compile %s\n",
3174
0
                       BAD_CAST xmlRelaxNGDefName(def), NULL);
3175
0
            break;
3176
0
    }
3177
0
    return (ret);
3178
0
}
3179
3180
/**
3181
 * xmlRelaxNGTryCompile:
3182
 * ctxt:  the RelaxNG parser context
3183
 * @define:  the definition tree to compile
3184
 *
3185
 * Try to compile the set of definitions, it works recursively,
3186
 * possibly ignoring parts which cannot be compiled.
3187
 *
3188
 * Returns 0 if success and -1 in case of error
3189
 */
3190
static int
3191
xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3192
0
{
3193
0
    int ret = 0;
3194
0
    xmlRelaxNGDefinePtr list;
3195
3196
0
    if ((ctxt == NULL) || (def == NULL))
3197
0
        return (-1);
3198
3199
0
    if ((def->type == XML_RELAXNG_START) ||
3200
0
        (def->type == XML_RELAXNG_ELEMENT)) {
3201
0
        ret = xmlRelaxNGIsCompilable(def);
3202
0
        if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3203
0
            ctxt->am = NULL;
3204
0
            ret = xmlRelaxNGCompile(ctxt, def);
3205
0
            return (ret);
3206
0
        }
3207
0
    }
3208
0
    switch (def->type) {
3209
0
        case XML_RELAXNG_NOOP:
3210
0
            ret = xmlRelaxNGTryCompile(ctxt, def->content);
3211
0
            break;
3212
0
        case XML_RELAXNG_TEXT:
3213
0
        case XML_RELAXNG_DATATYPE:
3214
0
        case XML_RELAXNG_LIST:
3215
0
        case XML_RELAXNG_PARAM:
3216
0
        case XML_RELAXNG_VALUE:
3217
0
        case XML_RELAXNG_EMPTY:
3218
0
        case XML_RELAXNG_ELEMENT:
3219
0
            ret = 0;
3220
0
            break;
3221
0
        case XML_RELAXNG_OPTIONAL:
3222
0
        case XML_RELAXNG_ZEROORMORE:
3223
0
        case XML_RELAXNG_ONEORMORE:
3224
0
        case XML_RELAXNG_CHOICE:
3225
0
        case XML_RELAXNG_GROUP:
3226
0
        case XML_RELAXNG_DEF:
3227
0
        case XML_RELAXNG_START:
3228
0
        case XML_RELAXNG_REF:
3229
0
        case XML_RELAXNG_EXTERNALREF:
3230
0
        case XML_RELAXNG_PARENTREF:
3231
0
            list = def->content;
3232
0
            while (list != NULL) {
3233
0
                ret = xmlRelaxNGTryCompile(ctxt, list);
3234
0
                if (ret != 0)
3235
0
                    break;
3236
0
                list = list->next;
3237
0
            }
3238
0
            break;
3239
0
        case XML_RELAXNG_EXCEPT:
3240
0
        case XML_RELAXNG_ATTRIBUTE:
3241
0
        case XML_RELAXNG_INTERLEAVE:
3242
0
        case XML_RELAXNG_NOT_ALLOWED:
3243
0
            ret = 0;
3244
0
            break;
3245
0
    }
3246
0
    return (ret);
3247
0
}
3248
3249
/************************************************************************
3250
 *                  *
3251
 *      Parsing functions       *
3252
 *                  *
3253
 ************************************************************************/
3254
3255
static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3256
                                                    ctxt, xmlNodePtr node);
3257
static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3258
                                                  ctxt, xmlNodePtr node);
3259
static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3260
                                                   ctxt, xmlNodePtr nodes,
3261
                                                   int group);
3262
static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3263
                                                  ctxt, xmlNodePtr node);
3264
static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3265
                                             xmlNodePtr node);
3266
static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3267
                                         xmlNodePtr nodes);
3268
static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3269
                                                    ctxt, xmlNodePtr node,
3270
                                                    xmlRelaxNGDefinePtr
3271
                                                    def);
3272
static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3273
                                                   ctxt, xmlNodePtr nodes);
3274
static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3275
                                  xmlRelaxNGDefinePtr define,
3276
                                  xmlNodePtr elem);
3277
3278
3279
0
#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3280
3281
/**
3282
 * xmlRelaxNGIsNullable:
3283
 * @define:  the definition to verify
3284
 *
3285
 * Check if a definition is nullable.
3286
 *
3287
 * Returns 1 if yes, 0 if no and -1 in case of error
3288
 */
3289
static int
3290
xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3291
0
{
3292
0
    int ret;
3293
3294
0
    if (define == NULL)
3295
0
        return (-1);
3296
3297
0
    if (define->dflags & IS_NULLABLE)
3298
0
        return (1);
3299
0
    if (define->dflags & IS_NOT_NULLABLE)
3300
0
        return (0);
3301
0
    switch (define->type) {
3302
0
        case XML_RELAXNG_EMPTY:
3303
0
        case XML_RELAXNG_TEXT:
3304
0
            ret = 1;
3305
0
            break;
3306
0
        case XML_RELAXNG_NOOP:
3307
0
        case XML_RELAXNG_DEF:
3308
0
        case XML_RELAXNG_REF:
3309
0
        case XML_RELAXNG_EXTERNALREF:
3310
0
        case XML_RELAXNG_PARENTREF:
3311
0
        case XML_RELAXNG_ONEORMORE:
3312
0
            ret = xmlRelaxNGIsNullable(define->content);
3313
0
            break;
3314
0
        case XML_RELAXNG_EXCEPT:
3315
0
        case XML_RELAXNG_NOT_ALLOWED:
3316
0
        case XML_RELAXNG_ELEMENT:
3317
0
        case XML_RELAXNG_DATATYPE:
3318
0
        case XML_RELAXNG_PARAM:
3319
0
        case XML_RELAXNG_VALUE:
3320
0
        case XML_RELAXNG_LIST:
3321
0
        case XML_RELAXNG_ATTRIBUTE:
3322
0
            ret = 0;
3323
0
            break;
3324
0
        case XML_RELAXNG_CHOICE:{
3325
0
                xmlRelaxNGDefinePtr list = define->content;
3326
3327
0
                while (list != NULL) {
3328
0
                    ret = xmlRelaxNGIsNullable(list);
3329
0
                    if (ret != 0)
3330
0
                        goto done;
3331
0
                    list = list->next;
3332
0
                }
3333
0
                ret = 0;
3334
0
                break;
3335
0
            }
3336
0
        case XML_RELAXNG_START:
3337
0
        case XML_RELAXNG_INTERLEAVE:
3338
0
        case XML_RELAXNG_GROUP:{
3339
0
                xmlRelaxNGDefinePtr list = define->content;
3340
3341
0
                while (list != NULL) {
3342
0
                    ret = xmlRelaxNGIsNullable(list);
3343
0
                    if (ret != 1)
3344
0
                        goto done;
3345
0
                    list = list->next;
3346
0
                }
3347
0
                return (1);
3348
0
            }
3349
0
        default:
3350
0
            return (-1);
3351
0
    }
3352
0
  done:
3353
0
    if (ret == 0)
3354
0
        define->dflags |= IS_NOT_NULLABLE;
3355
0
    if (ret == 1)
3356
0
        define->dflags |= IS_NULLABLE;
3357
0
    return (ret);
3358
0
}
3359
3360
/**
3361
 * xmlRelaxNGIsBlank:
3362
 * @str:  a string
3363
 *
3364
 * Check if a string is ignorable c.f. 4.2. Whitespace
3365
 *
3366
 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3367
 */
3368
static int
3369
xmlRelaxNGIsBlank(xmlChar * str)
3370
0
{
3371
0
    if (str == NULL)
3372
0
        return (1);
3373
0
    while (*str != 0) {
3374
0
        if (!(IS_BLANK_CH(*str)))
3375
0
            return (0);
3376
0
        str++;
3377
0
    }
3378
0
    return (1);
3379
0
}
3380
3381
/**
3382
 * xmlRelaxNGGetDataTypeLibrary:
3383
 * @ctxt:  a Relax-NG parser context
3384
 * @node:  the current data or value element
3385
 *
3386
 * Applies algorithm from 4.3. datatypeLibrary attribute
3387
 *
3388
 * Returns the datatypeLibrary value or NULL if not found
3389
 */
3390
static xmlChar *
3391
xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3392
                             xmlNodePtr node)
3393
0
{
3394
0
    xmlChar *ret, *escape;
3395
3396
0
    if (node == NULL)
3397
0
        return(NULL);
3398
3399
0
    if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3400
0
        ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3401
0
        if (ret != NULL) {
3402
0
            if (ret[0] == 0) {
3403
0
                xmlFree(ret);
3404
0
                return (NULL);
3405
0
            }
3406
0
            escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3407
0
            if (escape == NULL) {
3408
0
                return (ret);
3409
0
            }
3410
0
            xmlFree(ret);
3411
0
            return (escape);
3412
0
        }
3413
0
    }
3414
0
    node = node->parent;
3415
0
    while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3416
0
        ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3417
0
        if (ret != NULL) {
3418
0
            if (ret[0] == 0) {
3419
0
                xmlFree(ret);
3420
0
                return (NULL);
3421
0
            }
3422
0
            escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3423
0
            if (escape == NULL) {
3424
0
                return (ret);
3425
0
            }
3426
0
            xmlFree(ret);
3427
0
            return (escape);
3428
0
        }
3429
0
        node = node->parent;
3430
0
    }
3431
0
    return (NULL);
3432
0
}
3433
3434
/**
3435
 * xmlRelaxNGParseValue:
3436
 * @ctxt:  a Relax-NG parser context
3437
 * @node:  the data node.
3438
 *
3439
 * parse the content of a RelaxNG value node.
3440
 *
3441
 * Returns the definition pointer or NULL in case of error
3442
 */
3443
static xmlRelaxNGDefinePtr
3444
xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3445
0
{
3446
0
    xmlRelaxNGDefinePtr def = NULL;
3447
0
    xmlRelaxNGTypeLibraryPtr lib = NULL;
3448
0
    xmlChar *type;
3449
0
    xmlChar *library;
3450
0
    int success = 0;
3451
3452
0
    def = xmlRelaxNGNewDefine(ctxt, node);
3453
0
    if (def == NULL)
3454
0
        return (NULL);
3455
0
    def->type = XML_RELAXNG_VALUE;
3456
3457
0
    type = xmlGetProp(node, BAD_CAST "type");
3458
0
    if (type != NULL) {
3459
0
        xmlRelaxNGNormExtSpace(type);
3460
0
        if (xmlValidateNCName(type, 0)) {
3461
0
            xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3462
0
                       "value type '%s' is not an NCName\n", type, NULL);
3463
0
        }
3464
0
        library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3465
0
        if (library == NULL)
3466
0
            library =
3467
0
                xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3468
3469
0
        def->name = type;
3470
0
        def->ns = library;
3471
3472
0
        lib = (xmlRelaxNGTypeLibraryPtr)
3473
0
            xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3474
0
        if (lib == NULL) {
3475
0
            xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3476
0
                       "Use of unregistered type library '%s'\n", library,
3477
0
                       NULL);
3478
0
            def->data = NULL;
3479
0
        } else {
3480
0
            def->data = lib;
3481
0
            if (lib->have == NULL) {
3482
0
                xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3483
0
                           "Internal error with type library '%s': no 'have'\n",
3484
0
                           library, NULL);
3485
0
            } else {
3486
0
                success = lib->have(lib->data, def->name);
3487
0
                if (success != 1) {
3488
0
                    xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3489
0
                               "Error type '%s' is not exported by type library '%s'\n",
3490
0
                               def->name, library);
3491
0
                }
3492
0
            }
3493
0
        }
3494
0
    }
3495
0
    if (node->children == NULL) {
3496
0
        def->value = xmlStrdup(BAD_CAST "");
3497
0
    } else if (((node->children->type != XML_TEXT_NODE) &&
3498
0
                (node->children->type != XML_CDATA_SECTION_NODE)) ||
3499
0
               (node->children->next != NULL)) {
3500
0
        xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3501
0
                   "Expecting a single text value for <value>content\n",
3502
0
                   NULL, NULL);
3503
0
    } else if (def != NULL) {
3504
0
        def->value = xmlNodeGetContent(node);
3505
0
        if (def->value == NULL) {
3506
0
            xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3507
0
                       "Element <value> has no content\n", NULL, NULL);
3508
0
        } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3509
0
            void *val = NULL;
3510
3511
0
            success =
3512
0
                lib->check(lib->data, def->name, def->value, &val, node);
3513
0
            if (success != 1) {
3514
0
                xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3515
0
                           "Value '%s' is not acceptable for type '%s'\n",
3516
0
                           def->value, def->name);
3517
0
            } else {
3518
0
                if (val != NULL)
3519
0
                    def->attrs = val;
3520
0
            }
3521
0
        }
3522
0
    }
3523
0
    return (def);
3524
0
}
3525
3526
/**
3527
 * xmlRelaxNGParseData:
3528
 * @ctxt:  a Relax-NG parser context
3529
 * @node:  the data node.
3530
 *
3531
 * parse the content of a RelaxNG data node.
3532
 *
3533
 * Returns the definition pointer or NULL in case of error
3534
 */
3535
static xmlRelaxNGDefinePtr
3536
xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3537
0
{
3538
0
    xmlRelaxNGDefinePtr def = NULL, except;
3539
0
    xmlRelaxNGDefinePtr param, lastparam = NULL;
3540
0
    xmlRelaxNGTypeLibraryPtr lib;
3541
0
    xmlChar *type;
3542
0
    xmlChar *library;
3543
0
    xmlNodePtr content;
3544
0
    int tmp;
3545
3546
0
    type = xmlGetProp(node, BAD_CAST "type");
3547
0
    if (type == NULL) {
3548
0
        xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3549
0
                   NULL);
3550
0
        return (NULL);
3551
0
    }
3552
0
    xmlRelaxNGNormExtSpace(type);
3553
0
    if (xmlValidateNCName(type, 0)) {
3554
0
        xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3555
0
                   "data type '%s' is not an NCName\n", type, NULL);
3556
0
    }
3557
0
    library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3558
0
    if (library == NULL)
3559
0
        library =
3560
0
            xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3561
3562
0
    def = xmlRelaxNGNewDefine(ctxt, node);
3563
0
    if (def == NULL) {
3564
0
        xmlFree(library);
3565
0
        xmlFree(type);
3566
0
        return (NULL);
3567
0
    }
3568
0
    def->type = XML_RELAXNG_DATATYPE;
3569
0
    def->name = type;
3570
0
    def->ns = library;
3571
3572
0
    lib = (xmlRelaxNGTypeLibraryPtr)
3573
0
        xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3574
0
    if (lib == NULL) {
3575
0
        xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3576
0
                   "Use of unregistered type library '%s'\n", library,
3577
0
                   NULL);
3578
0
        def->data = NULL;
3579
0
    } else {
3580
0
        def->data = lib;
3581
0
        if (lib->have == NULL) {
3582
0
            xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3583
0
                       "Internal error with type library '%s': no 'have'\n",
3584
0
                       library, NULL);
3585
0
        } else {
3586
0
            tmp = lib->have(lib->data, def->name);
3587
0
            if (tmp != 1) {
3588
0
                xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3589
0
                           "Error type '%s' is not exported by type library '%s'\n",
3590
0
                           def->name, library);
3591
0
            } else
3592
0
                if ((xmlStrEqual
3593
0
                     (library,
3594
0
                      BAD_CAST
3595
0
                      "http://www.w3.org/2001/XMLSchema-datatypes"))
3596
0
                    && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3597
0
                        || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3598
0
                ctxt->idref = 1;
3599
0
            }
3600
0
        }
3601
0
    }
3602
0
    content = node->children;
3603
3604
    /*
3605
     * Handle optional params
3606
     */
3607
0
    while (content != NULL) {
3608
0
        if (!xmlStrEqual(content->name, BAD_CAST "param"))
3609
0
            break;
3610
0
        if (xmlStrEqual(library,
3611
0
                        BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3612
0
            xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3613
0
                       "Type library '%s' does not allow type parameters\n",
3614
0
                       library, NULL);
3615
0
            content = content->next;
3616
0
            while ((content != NULL) &&
3617
0
                   (xmlStrEqual(content->name, BAD_CAST "param")))
3618
0
                content = content->next;
3619
0
        } else {
3620
0
            param = xmlRelaxNGNewDefine(ctxt, node);
3621
0
            if (param != NULL) {
3622
0
                param->type = XML_RELAXNG_PARAM;
3623
0
                param->name = xmlGetProp(content, BAD_CAST "name");
3624
0
                if (param->name == NULL) {
3625
0
                    xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3626
0
                               "param has no name\n", NULL, NULL);
3627
0
                }
3628
0
                param->value = xmlNodeGetContent(content);
3629
0
                if (lastparam == NULL) {
3630
0
                    def->attrs = lastparam = param;
3631
0
                } else {
3632
0
                    lastparam->next = param;
3633
0
                    lastparam = param;
3634
0
                }
3635
0
                if (lib != NULL) {
3636
0
                }
3637
0
            }
3638
0
            content = content->next;
3639
0
        }
3640
0
    }
3641
    /*
3642
     * Handle optional except
3643
     */
3644
0
    if ((content != NULL)
3645
0
        && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3646
0
        xmlNodePtr child;
3647
0
        xmlRelaxNGDefinePtr tmp2, last = NULL;
3648
3649
0
        except = xmlRelaxNGNewDefine(ctxt, node);
3650
0
        if (except == NULL) {
3651
0
            return (def);
3652
0
        }
3653
0
        except->type = XML_RELAXNG_EXCEPT;
3654
0
        child = content->children;
3655
0
  def->content = except;
3656
0
        if (child == NULL) {
3657
0
            xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3658
0
                       "except has no content\n", NULL, NULL);
3659
0
        }
3660
0
        while (child != NULL) {
3661
0
            tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3662
0
            if (tmp2 != NULL) {
3663
0
                if (last == NULL) {
3664
0
                    except->content = last = tmp2;
3665
0
                } else {
3666
0
                    last->next = tmp2;
3667
0
                    last = tmp2;
3668
0
                }
3669
0
            }
3670
0
            child = child->next;
3671
0
        }
3672
0
        content = content->next;
3673
0
    }
3674
    /*
3675
     * Check there is no unhandled data
3676
     */
3677
0
    if (content != NULL) {
3678
0
        xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3679
0
                   "Element data has unexpected content %s\n",
3680
0
                   content->name, NULL);
3681
0
    }
3682
3683
0
    return (def);
3684
0
}
3685
3686
static const xmlChar *invalidName = BAD_CAST "\1";
3687
3688
/**
3689
 * xmlRelaxNGCompareNameClasses:
3690
 * @defs1:  the first element/attribute defs
3691
 * @defs2:  the second element/attribute defs
3692
 * @name:  the restriction on the name
3693
 * @ns:  the restriction on the namespace
3694
 *
3695
 * Compare the 2 lists of element definitions. The comparison is
3696
 * that if both lists do not accept the same QNames, it returns 1
3697
 * If the 2 lists can accept the same QName the comparison returns 0
3698
 *
3699
 * Returns 1 distinct, 0 if equal
3700
 */
3701
static int
3702
xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3703
                             xmlRelaxNGDefinePtr def2)
3704
0
{
3705
0
    int ret = 1;
3706
0
    xmlNode node;
3707
0
    xmlNs ns;
3708
0
    xmlRelaxNGValidCtxt ctxt;
3709
3710
0
    memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3711
3712
0
    ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3713
3714
0
    if ((def1->type == XML_RELAXNG_ELEMENT) ||
3715
0
        (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3716
0
        if (def2->type == XML_RELAXNG_TEXT)
3717
0
            return (1);
3718
0
        if (def1->name != NULL) {
3719
0
            node.name = def1->name;
3720
0
        } else {
3721
0
            node.name = invalidName;
3722
0
        }
3723
0
        if (def1->ns != NULL) {
3724
0
            if (def1->ns[0] == 0) {
3725
0
                node.ns = NULL;
3726
0
            } else {
3727
0
          node.ns = &ns;
3728
0
                ns.href = def1->ns;
3729
0
            }
3730
0
        } else {
3731
0
            node.ns = NULL;
3732
0
        }
3733
0
        if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
3734
0
            if (def1->nameClass != NULL) {
3735
0
                ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3736
0
            } else {
3737
0
                ret = 0;
3738
0
            }
3739
0
        } else {
3740
0
            ret = 1;
3741
0
        }
3742
0
    } else if (def1->type == XML_RELAXNG_TEXT) {
3743
0
        if (def2->type == XML_RELAXNG_TEXT)
3744
0
            return (0);
3745
0
        return (1);
3746
0
    } else if (def1->type == XML_RELAXNG_EXCEPT) {
3747
0
        ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3748
0
  if (ret == 0)
3749
0
      ret = 1;
3750
0
  else if (ret == 1)
3751
0
      ret = 0;
3752
0
    } else {
3753
        /* TODO */
3754
0
        ret = 0;
3755
0
    }
3756
0
    if (ret == 0)
3757
0
        return (ret);
3758
0
    if ((def2->type == XML_RELAXNG_ELEMENT) ||
3759
0
        (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3760
0
        if (def2->name != NULL) {
3761
0
            node.name = def2->name;
3762
0
        } else {
3763
0
            node.name = invalidName;
3764
0
        }
3765
0
        node.ns = &ns;
3766
0
        if (def2->ns != NULL) {
3767
0
            if (def2->ns[0] == 0) {
3768
0
                node.ns = NULL;
3769
0
            } else {
3770
0
                ns.href = def2->ns;
3771
0
            }
3772
0
        } else {
3773
0
            ns.href = invalidName;
3774
0
        }
3775
0
        if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
3776
0
            if (def2->nameClass != NULL) {
3777
0
                ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3778
0
            } else {
3779
0
                ret = 0;
3780
0
            }
3781
0
        } else {
3782
0
            ret = 1;
3783
0
        }
3784
0
    } else {
3785
        /* TODO */
3786
0
        ret = 0;
3787
0
    }
3788
3789
0
    return (ret);
3790
0
}
3791
3792
/**
3793
 * xmlRelaxNGCompareElemDefLists:
3794
 * @ctxt:  a Relax-NG parser context
3795
 * @defs1:  the first list of element/attribute defs
3796
 * @defs2:  the second list of element/attribute defs
3797
 *
3798
 * Compare the 2 lists of element or attribute definitions. The comparison
3799
 * is that if both lists do not accept the same QNames, it returns 1
3800
 * If the 2 lists can accept the same QName the comparison returns 0
3801
 *
3802
 * Returns 1 distinct, 0 if equal
3803
 */
3804
static int
3805
xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3806
                              ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3807
                              xmlRelaxNGDefinePtr * def2)
3808
0
{
3809
0
    xmlRelaxNGDefinePtr *basedef2 = def2;
3810
3811
0
    if ((def1 == NULL) || (def2 == NULL))
3812
0
        return (1);
3813
0
    if ((*def1 == NULL) || (*def2 == NULL))
3814
0
        return (1);
3815
0
    while (*def1 != NULL) {
3816
0
        while ((*def2) != NULL) {
3817
0
            if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3818
0
                return (0);
3819
0
            def2++;
3820
0
        }
3821
0
        def2 = basedef2;
3822
0
        def1++;
3823
0
    }
3824
0
    return (1);
3825
0
}
3826
3827
/**
3828
 * xmlRelaxNGGenerateAttributes:
3829
 * @ctxt:  a Relax-NG parser context
3830
 * @def:  the definition definition
3831
 *
3832
 * Check if the definition can only generate attributes
3833
 *
3834
 * Returns 1 if yes, 0 if no and -1 in case of error.
3835
 */
3836
static int
3837
xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3838
                             xmlRelaxNGDefinePtr def)
3839
0
{
3840
0
    xmlRelaxNGDefinePtr parent, cur, tmp;
3841
3842
    /*
3843
     * Don't run that check in case of error. Infinite recursion
3844
     * becomes possible.
3845
     */
3846
0
    if (ctxt->nbErrors != 0)
3847
0
        return (-1);
3848
3849
0
    parent = NULL;
3850
0
    cur = def;
3851
0
    while (cur != NULL) {
3852
0
        if ((cur->type == XML_RELAXNG_ELEMENT) ||
3853
0
            (cur->type == XML_RELAXNG_TEXT) ||
3854
0
            (cur->type == XML_RELAXNG_DATATYPE) ||
3855
0
            (cur->type == XML_RELAXNG_PARAM) ||
3856
0
            (cur->type == XML_RELAXNG_LIST) ||
3857
0
            (cur->type == XML_RELAXNG_VALUE) ||
3858
0
            (cur->type == XML_RELAXNG_EMPTY))
3859
0
            return (0);
3860
0
        if ((cur->type == XML_RELAXNG_CHOICE) ||
3861
0
            (cur->type == XML_RELAXNG_INTERLEAVE) ||
3862
0
            (cur->type == XML_RELAXNG_GROUP) ||
3863
0
            (cur->type == XML_RELAXNG_ONEORMORE) ||
3864
0
            (cur->type == XML_RELAXNG_ZEROORMORE) ||
3865
0
            (cur->type == XML_RELAXNG_OPTIONAL) ||
3866
0
            (cur->type == XML_RELAXNG_PARENTREF) ||
3867
0
            (cur->type == XML_RELAXNG_EXTERNALREF) ||
3868
0
            (cur->type == XML_RELAXNG_REF) ||
3869
0
            (cur->type == XML_RELAXNG_DEF)) {
3870
0
            if (cur->content != NULL) {
3871
0
                parent = cur;
3872
0
                cur = cur->content;
3873
0
                tmp = cur;
3874
0
                while (tmp != NULL) {
3875
0
                    tmp->parent = parent;
3876
0
                    tmp = tmp->next;
3877
0
                }
3878
0
                continue;
3879
0
            }
3880
0
        }
3881
0
        if (cur == def)
3882
0
            break;
3883
0
        if (cur->next != NULL) {
3884
0
            cur = cur->next;
3885
0
            continue;
3886
0
        }
3887
0
        do {
3888
0
            cur = cur->parent;
3889
0
            if (cur == NULL)
3890
0
                break;
3891
0
            if (cur == def)
3892
0
                return (1);
3893
0
            if (cur->next != NULL) {
3894
0
                cur = cur->next;
3895
0
                break;
3896
0
            }
3897
0
        } while (cur != NULL);
3898
0
    }
3899
0
    return (1);
3900
0
}
3901
3902
/**
3903
 * xmlRelaxNGGetElements:
3904
 * @ctxt:  a Relax-NG parser context
3905
 * @def:  the definition definition
3906
 * @eora:  gather elements (0), attributes (1) or elements and text (2)
3907
 *
3908
 * Compute the list of top elements a definition can generate
3909
 *
3910
 * Returns a list of elements or NULL if none was found.
3911
 */
3912
static xmlRelaxNGDefinePtr *
3913
xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
3914
                      xmlRelaxNGDefinePtr def, int eora)
3915
0
{
3916
0
    xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
3917
0
    int len = 0;
3918
0
    int max = 0;
3919
3920
    /*
3921
     * Don't run that check in case of error. Infinite recursion
3922
     * becomes possible.
3923
     */
3924
0
    if (ctxt->nbErrors != 0)
3925
0
        return (NULL);
3926
3927
0
    parent = NULL;
3928
0
    cur = def;
3929
0
    while (cur != NULL) {
3930
0
        if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3931
0
                             (cur->type == XML_RELAXNG_TEXT))) ||
3932
0
            ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) ||
3933
0
            ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) ||
3934
0
                       (cur->type == XML_RELAXNG_ELEMENT) ||
3935
0
           (cur->type == XML_RELAXNG_LIST) ||
3936
0
                             (cur->type == XML_RELAXNG_TEXT) ||
3937
0
           (cur->type == XML_RELAXNG_VALUE)))) {
3938
0
            if (ret == NULL) {
3939
0
                max = 10;
3940
0
                ret = (xmlRelaxNGDefinePtr *)
3941
0
                    xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3942
0
                if (ret == NULL) {
3943
0
                    xmlRngPErrMemory(ctxt);
3944
0
                    return (NULL);
3945
0
                }
3946
0
            } else if (max <= len) {
3947
0
          xmlRelaxNGDefinePtr *temp;
3948
3949
0
                max *= 2;
3950
0
                temp = xmlRealloc(ret,
3951
0
                               (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3952
0
                if (temp == NULL) {
3953
0
                    xmlRngPErrMemory(ctxt);
3954
0
        xmlFree(ret);
3955
0
                    return (NULL);
3956
0
                }
3957
0
    ret = temp;
3958
0
            }
3959
0
            ret[len++] = cur;
3960
0
            ret[len] = NULL;
3961
0
        } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3962
0
                   (cur->type == XML_RELAXNG_INTERLEAVE) ||
3963
0
                   (cur->type == XML_RELAXNG_GROUP) ||
3964
0
                   (cur->type == XML_RELAXNG_ONEORMORE) ||
3965
0
                   (cur->type == XML_RELAXNG_ZEROORMORE) ||
3966
0
                   (cur->type == XML_RELAXNG_OPTIONAL) ||
3967
0
                   (cur->type == XML_RELAXNG_PARENTREF) ||
3968
0
                   (cur->type == XML_RELAXNG_REF) ||
3969
0
                   (cur->type == XML_RELAXNG_DEF) ||
3970
0
       (cur->type == XML_RELAXNG_EXTERNALREF)) {
3971
            /*
3972
             * Don't go within elements or attributes or string values.
3973
             * Just gather the element top list
3974
             */
3975
0
            if (cur->content != NULL) {
3976
0
                parent = cur;
3977
0
                cur = cur->content;
3978
0
                tmp = cur;
3979
0
                while (tmp != NULL) {
3980
0
                    tmp->parent = parent;
3981
0
                    tmp = tmp->next;
3982
0
                }
3983
0
                continue;
3984
0
            }
3985
0
        }
3986
0
        if (cur == def)
3987
0
            break;
3988
0
        if (cur->next != NULL) {
3989
0
            cur = cur->next;
3990
0
            continue;
3991
0
        }
3992
0
        do {
3993
0
            cur = cur->parent;
3994
0
            if (cur == NULL)
3995
0
                break;
3996
0
            if (cur == def)
3997
0
                return (ret);
3998
0
            if (cur->next != NULL) {
3999
0
                cur = cur->next;
4000
0
                break;
4001
0
            }
4002
0
        } while (cur != NULL);
4003
0
    }
4004
0
    return (ret);
4005
0
}
4006
4007
/**
4008
 * xmlRelaxNGCheckChoiceDeterminism:
4009
 * @ctxt:  a Relax-NG parser context
4010
 * @def:  the choice definition
4011
 *
4012
 * Also used to find indeterministic pattern in choice
4013
 */
4014
static void
4015
xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
4016
                                 xmlRelaxNGDefinePtr def)
4017
0
{
4018
0
    xmlRelaxNGDefinePtr **list;
4019
0
    xmlRelaxNGDefinePtr cur;
4020
0
    int nbchild = 0, i, j, ret;
4021
0
    int is_nullable = 0;
4022
0
    int is_indeterminist = 0;
4023
0
    xmlHashTablePtr triage = NULL;
4024
0
    int is_triable = 1;
4025
4026
0
    if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4027
0
        return;
4028
4029
0
    if (def->dflags & IS_PROCESSED)
4030
0
        return;
4031
4032
    /*
4033
     * Don't run that check in case of error. Infinite recursion
4034
     * becomes possible.
4035
     */
4036
0
    if (ctxt->nbErrors != 0)
4037
0
        return;
4038
4039
0
    is_nullable = xmlRelaxNGIsNullable(def);
4040
4041
0
    cur = def->content;
4042
0
    while (cur != NULL) {
4043
0
        nbchild++;
4044
0
        cur = cur->next;
4045
0
    }
4046
4047
0
    list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4048
0
                                              sizeof(xmlRelaxNGDefinePtr
4049
0
                                                     *));
4050
0
    if (list == NULL) {
4051
0
        xmlRngPErrMemory(ctxt);
4052
0
        return;
4053
0
    }
4054
0
    i = 0;
4055
    /*
4056
     * a bit strong but safe
4057
     */
4058
0
    if (is_nullable == 0) {
4059
0
        triage = xmlHashCreate(10);
4060
0
    } else {
4061
0
        is_triable = 0;
4062
0
    }
4063
0
    cur = def->content;
4064
0
    while (cur != NULL) {
4065
0
        list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4066
0
        if ((list[i] == NULL) || (list[i][0] == NULL)) {
4067
0
            is_triable = 0;
4068
0
        } else if (is_triable == 1) {
4069
0
            xmlRelaxNGDefinePtr *tmp;
4070
0
            int res;
4071
4072
0
            tmp = list[i];
4073
0
            while ((*tmp != NULL) && (is_triable == 1)) {
4074
0
                if ((*tmp)->type == XML_RELAXNG_TEXT) {
4075
0
                    res = xmlHashAddEntry2(triage,
4076
0
                                           BAD_CAST "#text", NULL,
4077
0
                                           (void *) cur);
4078
0
                    if (res != 0)
4079
0
                        is_triable = -1;
4080
0
                } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4081
0
                           ((*tmp)->name != NULL)) {
4082
0
                    if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4083
0
                        res = xmlHashAddEntry2(triage,
4084
0
                                               (*tmp)->name, NULL,
4085
0
                                               (void *) cur);
4086
0
                    else
4087
0
                        res = xmlHashAddEntry2(triage,
4088
0
                                               (*tmp)->name, (*tmp)->ns,
4089
0
                                               (void *) cur);
4090
0
                    if (res != 0)
4091
0
                        is_triable = -1;
4092
0
                } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4093
0
                    if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4094
0
                        res = xmlHashAddEntry2(triage,
4095
0
                                               BAD_CAST "#any", NULL,
4096
0
                                               (void *) cur);
4097
0
                    else
4098
0
                        res = xmlHashAddEntry2(triage,
4099
0
                                               BAD_CAST "#any", (*tmp)->ns,
4100
0
                                               (void *) cur);
4101
0
                    if (res != 0)
4102
0
                        is_triable = -1;
4103
0
                } else {
4104
0
                    is_triable = -1;
4105
0
                }
4106
0
                tmp++;
4107
0
            }
4108
0
        }
4109
0
        i++;
4110
0
        cur = cur->next;
4111
0
    }
4112
4113
0
    for (i = 0; i < nbchild; i++) {
4114
0
        if (list[i] == NULL)
4115
0
            continue;
4116
0
        for (j = 0; j < i; j++) {
4117
0
            if (list[j] == NULL)
4118
0
                continue;
4119
0
            ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4120
0
            if (ret == 0) {
4121
0
                is_indeterminist = 1;
4122
0
            }
4123
0
        }
4124
0
    }
4125
0
    for (i = 0; i < nbchild; i++) {
4126
0
        if (list[i] != NULL)
4127
0
            xmlFree(list[i]);
4128
0
    }
4129
4130
0
    xmlFree(list);
4131
0
    if (is_indeterminist) {
4132
0
        def->dflags |= IS_INDETERMINIST;
4133
0
    }
4134
0
    if (is_triable == 1) {
4135
0
        def->dflags |= IS_TRIABLE;
4136
0
        def->data = triage;
4137
0
    } else if (triage != NULL) {
4138
0
        xmlHashFree(triage, NULL);
4139
0
    }
4140
0
    def->dflags |= IS_PROCESSED;
4141
0
}
4142
4143
/**
4144
 * xmlRelaxNGCheckGroupAttrs:
4145
 * @ctxt:  a Relax-NG parser context
4146
 * @def:  the group definition
4147
 *
4148
 * Detects violations of rule 7.3
4149
 */
4150
static void
4151
xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
4152
                          xmlRelaxNGDefinePtr def)
4153
0
{
4154
0
    xmlRelaxNGDefinePtr **list;
4155
0
    xmlRelaxNGDefinePtr cur;
4156
0
    int nbchild = 0, i, j, ret;
4157
4158
0
    if ((def == NULL) ||
4159
0
        ((def->type != XML_RELAXNG_GROUP) &&
4160
0
         (def->type != XML_RELAXNG_ELEMENT)))
4161
0
        return;
4162
4163
0
    if (def->dflags & IS_PROCESSED)
4164
0
        return;
4165
4166
    /*
4167
     * Don't run that check in case of error. Infinite recursion
4168
     * becomes possible.
4169
     */
4170
0
    if (ctxt->nbErrors != 0)
4171
0
        return;
4172
4173
0
    cur = def->attrs;
4174
0
    while (cur != NULL) {
4175
0
        nbchild++;
4176
0
        cur = cur->next;
4177
0
    }
4178
0
    cur = def->content;
4179
0
    while (cur != NULL) {
4180
0
        nbchild++;
4181
0
        cur = cur->next;
4182
0
    }
4183
4184
0
    list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4185
0
                                              sizeof(xmlRelaxNGDefinePtr
4186
0
                                                     *));
4187
0
    if (list == NULL) {
4188
0
        xmlRngPErrMemory(ctxt);
4189
0
        return;
4190
0
    }
4191
0
    i = 0;
4192
0
    cur = def->attrs;
4193
0
    while (cur != NULL) {
4194
0
        list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4195
0
        i++;
4196
0
        cur = cur->next;
4197
0
    }
4198
0
    cur = def->content;
4199
0
    while (cur != NULL) {
4200
0
        list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4201
0
        i++;
4202
0
        cur = cur->next;
4203
0
    }
4204
4205
0
    for (i = 0; i < nbchild; i++) {
4206
0
        if (list[i] == NULL)
4207
0
            continue;
4208
0
        for (j = 0; j < i; j++) {
4209
0
            if (list[j] == NULL)
4210
0
                continue;
4211
0
            ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4212
0
            if (ret == 0) {
4213
0
                xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4214
0
                           "Attributes conflicts in group\n", NULL, NULL);
4215
0
            }
4216
0
        }
4217
0
    }
4218
0
    for (i = 0; i < nbchild; i++) {
4219
0
        if (list[i] != NULL)
4220
0
            xmlFree(list[i]);
4221
0
    }
4222
4223
0
    xmlFree(list);
4224
0
    def->dflags |= IS_PROCESSED;
4225
0
}
4226
4227
/**
4228
 * xmlRelaxNGComputeInterleaves:
4229
 * @def:  the interleave definition
4230
 * @ctxt:  a Relax-NG parser context
4231
 * @name:  the definition name
4232
 *
4233
 * A lot of work for preprocessing interleave definitions
4234
 * is potentially needed to get a decent execution speed at runtime
4235
 *   - trying to get a total order on the element nodes generated
4236
 *     by the interleaves, order the list of interleave definitions
4237
 *     following that order.
4238
 *   - if <text/> is used to handle mixed content, it is better to
4239
 *     flag this in the define and simplify the runtime checking
4240
 *     algorithm
4241
 */
4242
static void
4243
xmlRelaxNGComputeInterleaves(void *payload, void *data,
4244
                             const xmlChar * name ATTRIBUTE_UNUSED)
4245
0
{
4246
0
    xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4247
0
    xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4248
0
    xmlRelaxNGDefinePtr cur, *tmp;
4249
4250
0
    xmlRelaxNGPartitionPtr partitions = NULL;
4251
0
    xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4252
0
    xmlRelaxNGInterleaveGroupPtr group;
4253
0
    int i, j, ret, res;
4254
0
    int nbgroups = 0;
4255
0
    int nbchild = 0;
4256
0
    int is_mixed = 0;
4257
0
    int is_determinist = 1;
4258
4259
    /*
4260
     * Don't run that check in case of error. Infinite recursion
4261
     * becomes possible.
4262
     */
4263
0
    if (ctxt->nbErrors != 0)
4264
0
        return;
4265
4266
0
    cur = def->content;
4267
0
    while (cur != NULL) {
4268
0
        nbchild++;
4269
0
        cur = cur->next;
4270
0
    }
4271
4272
0
    groups = (xmlRelaxNGInterleaveGroupPtr *)
4273
0
        xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4274
0
    if (groups == NULL)
4275
0
        goto error;
4276
0
    cur = def->content;
4277
0
    while (cur != NULL) {
4278
0
        groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4279
0
            xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4280
0
        if (groups[nbgroups] == NULL)
4281
0
            goto error;
4282
0
        if (cur->type == XML_RELAXNG_TEXT)
4283
0
            is_mixed++;
4284
0
        groups[nbgroups]->rule = cur;
4285
0
        groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2);
4286
0
        groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4287
0
        nbgroups++;
4288
0
        cur = cur->next;
4289
0
    }
4290
4291
    /*
4292
     * Let's check that all rules makes a partitions according to 7.4
4293
     */
4294
0
    partitions = (xmlRelaxNGPartitionPtr)
4295
0
        xmlMalloc(sizeof(xmlRelaxNGPartition));
4296
0
    if (partitions == NULL)
4297
0
        goto error;
4298
0
    memset(partitions, 0, sizeof(xmlRelaxNGPartition));
4299
0
    partitions->nbgroups = nbgroups;
4300
0
    partitions->triage = xmlHashCreate(nbgroups);
4301
0
    for (i = 0; i < nbgroups; i++) {
4302
0
        group = groups[i];
4303
0
        for (j = i + 1; j < nbgroups; j++) {
4304
0
            if (groups[j] == NULL)
4305
0
                continue;
4306
4307
0
            ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4308
0
                                                groups[j]->defs);
4309
0
            if (ret == 0) {
4310
0
                xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4311
0
                           "Element or text conflicts in interleave\n",
4312
0
                           NULL, NULL);
4313
0
            }
4314
0
            ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4315
0
                                                groups[j]->attrs);
4316
0
            if (ret == 0) {
4317
0
                xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4318
0
                           "Attributes conflicts in interleave\n", NULL,
4319
0
                           NULL);
4320
0
            }
4321
0
        }
4322
0
        tmp = group->defs;
4323
0
        if ((tmp != NULL) && (*tmp != NULL)) {
4324
0
            while (*tmp != NULL) {
4325
0
                if ((*tmp)->type == XML_RELAXNG_TEXT) {
4326
0
                    res = xmlHashAddEntry2(partitions->triage,
4327
0
                                           BAD_CAST "#text", NULL,
4328
0
                                           (void *) (ptrdiff_t) (i + 1));
4329
0
                    if (res != 0)
4330
0
                        is_determinist = -1;
4331
0
                } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4332
0
                           ((*tmp)->name != NULL)) {
4333
0
                    if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4334
0
                        res = xmlHashAddEntry2(partitions->triage,
4335
0
                                               (*tmp)->name, NULL,
4336
0
                                               (void *) (ptrdiff_t) (i + 1));
4337
0
                    else
4338
0
                        res = xmlHashAddEntry2(partitions->triage,
4339
0
                                               (*tmp)->name, (*tmp)->ns,
4340
0
                                               (void *) (ptrdiff_t) (i + 1));
4341
0
                    if (res != 0)
4342
0
                        is_determinist = -1;
4343
0
                } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4344
0
                    if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4345
0
                        res = xmlHashAddEntry2(partitions->triage,
4346
0
                                               BAD_CAST "#any", NULL,
4347
0
                                               (void *) (ptrdiff_t) (i + 1));
4348
0
                    else
4349
0
                        res = xmlHashAddEntry2(partitions->triage,
4350
0
                                               BAD_CAST "#any", (*tmp)->ns,
4351
0
                                               (void *) (ptrdiff_t) (i + 1));
4352
0
                    if ((*tmp)->nameClass != NULL)
4353
0
                        is_determinist = 2;
4354
0
                    if (res != 0)
4355
0
                        is_determinist = -1;
4356
0
                } else {
4357
0
                    is_determinist = -1;
4358
0
                }
4359
0
                tmp++;
4360
0
            }
4361
0
        } else {
4362
0
            is_determinist = 0;
4363
0
        }
4364
0
    }
4365
0
    partitions->groups = groups;
4366
4367
    /*
4368
     * and save the partition list back in the def
4369
     */
4370
0
    def->data = partitions;
4371
0
    if (is_mixed != 0)
4372
0
        def->dflags |= IS_MIXED;
4373
0
    if (is_determinist == 1)
4374
0
        partitions->flags = IS_DETERMINIST;
4375
0
    if (is_determinist == 2)
4376
0
        partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
4377
0
    return;
4378
4379
0
  error:
4380
0
    xmlRngPErrMemory(ctxt);
4381
0
    if (groups != NULL) {
4382
0
        for (i = 0; i < nbgroups; i++)
4383
0
            if (groups[i] != NULL) {
4384
0
                if (groups[i]->defs != NULL)
4385
0
                    xmlFree(groups[i]->defs);
4386
0
                xmlFree(groups[i]);
4387
0
            }
4388
0
        xmlFree(groups);
4389
0
    }
4390
0
    xmlRelaxNGFreePartition(partitions);
4391
0
}
4392
4393
/**
4394
 * xmlRelaxNGParseInterleave:
4395
 * @ctxt:  a Relax-NG parser context
4396
 * @node:  the data node.
4397
 *
4398
 * parse the content of a RelaxNG interleave node.
4399
 *
4400
 * Returns the definition pointer or NULL in case of error
4401
 */
4402
static xmlRelaxNGDefinePtr
4403
xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4404
0
{
4405
0
    xmlRelaxNGDefinePtr def = NULL;
4406
0
    xmlRelaxNGDefinePtr last = NULL, cur;
4407
0
    xmlNodePtr child;
4408
4409
0
    def = xmlRelaxNGNewDefine(ctxt, node);
4410
0
    if (def == NULL) {
4411
0
        return (NULL);
4412
0
    }
4413
0
    def->type = XML_RELAXNG_INTERLEAVE;
4414
4415
0
    if (ctxt->interleaves == NULL)
4416
0
        ctxt->interleaves = xmlHashCreate(10);
4417
0
    if (ctxt->interleaves == NULL) {
4418
0
        xmlRngPErrMemory(ctxt);
4419
0
    } else {
4420
0
        char name[32];
4421
4422
0
        snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4423
0
        if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4424
0
            xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4425
0
                       "Failed to add %s to hash table\n",
4426
0
           (const xmlChar *) name, NULL);
4427
0
        }
4428
0
    }
4429
0
    child = node->children;
4430
0
    if (child == NULL) {
4431
0
        xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4432
0
                   "Element interleave is empty\n", NULL, NULL);
4433
0
    }
4434
0
    while (child != NULL) {
4435
0
        if (IS_RELAXNG(child, "element")) {
4436
0
            cur = xmlRelaxNGParseElement(ctxt, child);
4437
0
        } else {
4438
0
            cur = xmlRelaxNGParsePattern(ctxt, child);
4439
0
        }
4440
0
        if (cur != NULL) {
4441
0
            cur->parent = def;
4442
0
            if (last == NULL) {
4443
0
                def->content = last = cur;
4444
0
            } else {
4445
0
                last->next = cur;
4446
0
                last = cur;
4447
0
            }
4448
0
        }
4449
0
        child = child->next;
4450
0
    }
4451
4452
0
    return (def);
4453
0
}
4454
4455
/**
4456
 * xmlRelaxNGParseInclude:
4457
 * @ctxt:  a Relax-NG parser context
4458
 * @node:  the include node
4459
 *
4460
 * Integrate the content of an include node in the current grammar
4461
 *
4462
 * Returns 0 in case of success or -1 in case of error
4463
 */
4464
static int
4465
xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4466
0
{
4467
0
    xmlRelaxNGIncludePtr incl;
4468
0
    xmlNodePtr root;
4469
0
    int ret = 0, tmp;
4470
4471
0
    incl = node->psvi;
4472
0
    if (incl == NULL) {
4473
0
        xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4474
0
                   "Include node has no data\n", NULL, NULL);
4475
0
        return (-1);
4476
0
    }
4477
0
    root = xmlDocGetRootElement(incl->doc);
4478
0
    if (root == NULL) {
4479
0
        xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4480
0
                   NULL, NULL);
4481
0
        return (-1);
4482
0
    }
4483
0
    if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4484
0
        xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4485
0
                   "Include document root is not a grammar\n", NULL, NULL);
4486
0
        return (-1);
4487
0
    }
4488
4489
    /*
4490
     * Merge the definition from both the include and the internal list
4491
     */
4492
0
    if (root->children != NULL) {
4493
0
        tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4494
0
        if (tmp != 0)
4495
0
            ret = -1;
4496
0
    }
4497
0
    if (node->children != NULL) {
4498
0
        tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4499
0
        if (tmp != 0)
4500
0
            ret = -1;
4501
0
    }
4502
0
    return (ret);
4503
0
}
4504
4505
/**
4506
 * xmlRelaxNGParseDefine:
4507
 * @ctxt:  a Relax-NG parser context
4508
 * @node:  the define node
4509
 *
4510
 * parse the content of a RelaxNG define element node.
4511
 *
4512
 * Returns 0 in case of success or -1 in case of error
4513
 */
4514
static int
4515
xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4516
0
{
4517
0
    xmlChar *name;
4518
0
    int ret = 0, tmp;
4519
0
    xmlRelaxNGDefinePtr def;
4520
0
    const xmlChar *olddefine;
4521
4522
0
    name = xmlGetProp(node, BAD_CAST "name");
4523
0
    if (name == NULL) {
4524
0
        xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4525
0
                   "define has no name\n", NULL, NULL);
4526
0
    } else {
4527
0
        xmlRelaxNGNormExtSpace(name);
4528
0
        if (xmlValidateNCName(name, 0)) {
4529
0
            xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4530
0
                       "define name '%s' is not an NCName\n", name, NULL);
4531
0
        }
4532
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4533
0
        if (def == NULL) {
4534
0
            xmlFree(name);
4535
0
            return (-1);
4536
0
        }
4537
0
        def->type = XML_RELAXNG_DEF;
4538
0
        def->name = name;
4539
0
        if (node->children == NULL) {
4540
0
            xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4541
0
                       "define has no children\n", NULL, NULL);
4542
0
        } else {
4543
0
            olddefine = ctxt->define;
4544
0
            ctxt->define = name;
4545
0
            def->content =
4546
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4547
0
            ctxt->define = olddefine;
4548
0
        }
4549
0
        if (ctxt->grammar->defs == NULL)
4550
0
            ctxt->grammar->defs = xmlHashCreate(10);
4551
0
        if (ctxt->grammar->defs == NULL) {
4552
0
            xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4553
0
                       "Could not create definition hash\n", NULL, NULL);
4554
0
            ret = -1;
4555
0
        } else {
4556
0
            tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4557
0
            if (tmp < 0) {
4558
0
                xmlRelaxNGDefinePtr prev;
4559
4560
0
                prev = xmlHashLookup(ctxt->grammar->defs, name);
4561
0
                if (prev == NULL) {
4562
0
                    xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4563
0
                               "Internal error on define aggregation of %s\n",
4564
0
                               name, NULL);
4565
0
                    ret = -1;
4566
0
                } else {
4567
0
                    while (prev->nextHash != NULL)
4568
0
                        prev = prev->nextHash;
4569
0
                    prev->nextHash = def;
4570
0
                }
4571
0
            }
4572
0
        }
4573
0
    }
4574
0
    return (ret);
4575
0
}
4576
4577
/**
4578
 * xmlRelaxNGParseImportRef:
4579
 * @payload: the parser context
4580
 * @data: the current grammar
4581
 * @name: the reference name
4582
 *
4583
 * Import import one references into the current grammar
4584
 */
4585
static void
4586
0
xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) {
4587
0
    xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4588
0
    xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4589
0
    int tmp;
4590
4591
0
    def->dflags |= IS_EXTERNAL_REF;
4592
4593
0
    tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4594
0
    if (tmp < 0) {
4595
0
        xmlRelaxNGDefinePtr prev;
4596
4597
0
        prev = (xmlRelaxNGDefinePtr)
4598
0
            xmlHashLookup(ctxt->grammar->refs, def->name);
4599
0
        if (prev == NULL) {
4600
0
            if (def->name != NULL) {
4601
0
                xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4602
0
                           "Error refs definitions '%s'\n",
4603
0
                           def->name, NULL);
4604
0
            } else {
4605
0
                xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4606
0
                           "Error refs definitions\n",
4607
0
                           NULL, NULL);
4608
0
            }
4609
0
        } else {
4610
0
            def->nextHash = prev->nextHash;
4611
0
            prev->nextHash = def;
4612
0
        }
4613
0
    }
4614
0
}
4615
4616
/**
4617
 * xmlRelaxNGParseImportRefs:
4618
 * @ctxt: the parser context
4619
 * @grammar: the sub grammar
4620
 *
4621
 * Import references from the subgrammar into the current grammar
4622
 *
4623
 * Returns 0 in case of success, -1 in case of failure
4624
 */
4625
static int
4626
xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4627
0
                          xmlRelaxNGGrammarPtr grammar) {
4628
0
    if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4629
0
        return(-1);
4630
0
    if (grammar->refs == NULL)
4631
0
        return(0);
4632
0
    if (ctxt->grammar->refs == NULL)
4633
0
        ctxt->grammar->refs = xmlHashCreate(10);
4634
0
    if (ctxt->grammar->refs == NULL) {
4635
0
        xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4636
0
                   "Could not create references hash\n", NULL, NULL);
4637
0
        return(-1);
4638
0
    }
4639
0
    xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
4640
0
    return(0);
4641
0
}
4642
4643
/**
4644
 * xmlRelaxNGProcessExternalRef:
4645
 * @ctxt: the parser context
4646
 * @node:  the externalRef node
4647
 *
4648
 * Process and compile an externalRef node
4649
 *
4650
 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4651
 */
4652
static xmlRelaxNGDefinePtr
4653
xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4654
0
{
4655
0
    xmlRelaxNGDocumentPtr docu;
4656
0
    xmlNodePtr root, tmp;
4657
0
    xmlChar *ns;
4658
0
    int newNs = 0, oldflags;
4659
0
    xmlRelaxNGDefinePtr def;
4660
4661
0
    docu = node->psvi;
4662
0
    if (docu != NULL) {
4663
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4664
0
        if (def == NULL)
4665
0
            return (NULL);
4666
0
        def->type = XML_RELAXNG_EXTERNALREF;
4667
4668
0
        if (docu->content == NULL) {
4669
            /*
4670
             * Then do the parsing for good
4671
             */
4672
0
            root = xmlDocGetRootElement(docu->doc);
4673
0
            if (root == NULL) {
4674
0
                xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4675
0
                           "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4676
0
                           NULL);
4677
0
                return (NULL);
4678
0
            }
4679
            /*
4680
             * ns transmission rules
4681
             */
4682
0
            ns = xmlGetProp(root, BAD_CAST "ns");
4683
0
            if (ns == NULL) {
4684
0
                tmp = node;
4685
0
                while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4686
0
                    ns = xmlGetProp(tmp, BAD_CAST "ns");
4687
0
                    if (ns != NULL) {
4688
0
                        break;
4689
0
                    }
4690
0
                    tmp = tmp->parent;
4691
0
                }
4692
0
                if (ns != NULL) {
4693
0
                    xmlSetProp(root, BAD_CAST "ns", ns);
4694
0
                    newNs = 1;
4695
0
                    xmlFree(ns);
4696
0
                }
4697
0
            } else {
4698
0
                xmlFree(ns);
4699
0
            }
4700
4701
            /*
4702
             * Parsing to get a precompiled schemas.
4703
             */
4704
0
            oldflags = ctxt->flags;
4705
0
            ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4706
0
            docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4707
0
            ctxt->flags = oldflags;
4708
0
            if ((docu->schema != NULL) &&
4709
0
                (docu->schema->topgrammar != NULL)) {
4710
0
                docu->content = docu->schema->topgrammar->start;
4711
0
                if (docu->schema->topgrammar->refs)
4712
0
                    xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
4713
0
            }
4714
4715
            /*
4716
             * the externalRef may be reused in a different ns context
4717
             */
4718
0
            if (newNs == 1) {
4719
0
                xmlUnsetProp(root, BAD_CAST "ns");
4720
0
            }
4721
0
        }
4722
0
        def->content = docu->content;
4723
0
    } else {
4724
0
        def = NULL;
4725
0
    }
4726
0
    return (def);
4727
0
}
4728
4729
/**
4730
 * xmlRelaxNGParsePattern:
4731
 * @ctxt:  a Relax-NG parser context
4732
 * @node:  the pattern node.
4733
 *
4734
 * parse the content of a RelaxNG pattern node.
4735
 *
4736
 * Returns the definition pointer or NULL in case of error or if no
4737
 *     pattern is generated.
4738
 */
4739
static xmlRelaxNGDefinePtr
4740
xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4741
0
{
4742
0
    xmlRelaxNGDefinePtr def = NULL;
4743
4744
0
    if (node == NULL) {
4745
0
        return (NULL);
4746
0
    }
4747
0
    if (IS_RELAXNG(node, "element")) {
4748
0
        def = xmlRelaxNGParseElement(ctxt, node);
4749
0
    } else if (IS_RELAXNG(node, "attribute")) {
4750
0
        def = xmlRelaxNGParseAttribute(ctxt, node);
4751
0
    } else if (IS_RELAXNG(node, "empty")) {
4752
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4753
0
        if (def == NULL)
4754
0
            return (NULL);
4755
0
        def->type = XML_RELAXNG_EMPTY;
4756
0
        if (node->children != NULL) {
4757
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4758
0
                       "empty: had a child node\n", NULL, NULL);
4759
0
        }
4760
0
    } else if (IS_RELAXNG(node, "text")) {
4761
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4762
0
        if (def == NULL)
4763
0
            return (NULL);
4764
0
        def->type = XML_RELAXNG_TEXT;
4765
0
        if (node->children != NULL) {
4766
0
            xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4767
0
                       "text: had a child node\n", NULL, NULL);
4768
0
        }
4769
0
    } else if (IS_RELAXNG(node, "zeroOrMore")) {
4770
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4771
0
        if (def == NULL)
4772
0
            return (NULL);
4773
0
        def->type = XML_RELAXNG_ZEROORMORE;
4774
0
        if (node->children == NULL) {
4775
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4776
0
                       "Element %s is empty\n", node->name, NULL);
4777
0
        } else {
4778
0
            def->content =
4779
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4780
0
        }
4781
0
    } else if (IS_RELAXNG(node, "oneOrMore")) {
4782
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4783
0
        if (def == NULL)
4784
0
            return (NULL);
4785
0
        def->type = XML_RELAXNG_ONEORMORE;
4786
0
        if (node->children == NULL) {
4787
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4788
0
                       "Element %s is empty\n", node->name, NULL);
4789
0
        } else {
4790
0
            def->content =
4791
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4792
0
        }
4793
0
    } else if (IS_RELAXNG(node, "optional")) {
4794
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4795
0
        if (def == NULL)
4796
0
            return (NULL);
4797
0
        def->type = XML_RELAXNG_OPTIONAL;
4798
0
        if (node->children == NULL) {
4799
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4800
0
                       "Element %s is empty\n", node->name, NULL);
4801
0
        } else {
4802
0
            def->content =
4803
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4804
0
        }
4805
0
    } else if (IS_RELAXNG(node, "choice")) {
4806
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4807
0
        if (def == NULL)
4808
0
            return (NULL);
4809
0
        def->type = XML_RELAXNG_CHOICE;
4810
0
        if (node->children == NULL) {
4811
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4812
0
                       "Element %s is empty\n", node->name, NULL);
4813
0
        } else {
4814
0
            def->content =
4815
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4816
0
        }
4817
0
    } else if (IS_RELAXNG(node, "group")) {
4818
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4819
0
        if (def == NULL)
4820
0
            return (NULL);
4821
0
        def->type = XML_RELAXNG_GROUP;
4822
0
        if (node->children == NULL) {
4823
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4824
0
                       "Element %s is empty\n", node->name, NULL);
4825
0
        } else {
4826
0
            def->content =
4827
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4828
0
        }
4829
0
    } else if (IS_RELAXNG(node, "ref")) {
4830
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4831
0
        if (def == NULL)
4832
0
            return (NULL);
4833
0
        def->type = XML_RELAXNG_REF;
4834
0
        def->name = xmlGetProp(node, BAD_CAST "name");
4835
0
        if (def->name == NULL) {
4836
0
            xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4837
0
                       NULL, NULL);
4838
0
        } else {
4839
0
            xmlRelaxNGNormExtSpace(def->name);
4840
0
            if (xmlValidateNCName(def->name, 0)) {
4841
0
                xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4842
0
                           "ref name '%s' is not an NCName\n", def->name,
4843
0
                           NULL);
4844
0
            }
4845
0
        }
4846
0
        if (node->children != NULL) {
4847
0
            xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4848
0
                       NULL, NULL);
4849
0
        }
4850
0
        if (ctxt->grammar->refs == NULL)
4851
0
            ctxt->grammar->refs = xmlHashCreate(10);
4852
0
        if (ctxt->grammar->refs == NULL) {
4853
0
            xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4854
0
                       "Could not create references hash\n", NULL, NULL);
4855
0
            def = NULL;
4856
0
        } else {
4857
0
            int tmp;
4858
4859
0
            tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4860
0
            if (tmp < 0) {
4861
0
                xmlRelaxNGDefinePtr prev;
4862
4863
0
                prev = (xmlRelaxNGDefinePtr)
4864
0
                    xmlHashLookup(ctxt->grammar->refs, def->name);
4865
0
                if (prev == NULL) {
4866
0
                    if (def->name != NULL) {
4867
0
            xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4868
0
           "Error refs definitions '%s'\n",
4869
0
           def->name, NULL);
4870
0
                    } else {
4871
0
            xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4872
0
           "Error refs definitions\n",
4873
0
           NULL, NULL);
4874
0
                    }
4875
0
                    def = NULL;
4876
0
                } else {
4877
0
                    def->nextHash = prev->nextHash;
4878
0
                    prev->nextHash = def;
4879
0
                }
4880
0
            }
4881
0
        }
4882
0
    } else if (IS_RELAXNG(node, "data")) {
4883
0
        def = xmlRelaxNGParseData(ctxt, node);
4884
0
    } else if (IS_RELAXNG(node, "value")) {
4885
0
        def = xmlRelaxNGParseValue(ctxt, node);
4886
0
    } else if (IS_RELAXNG(node, "list")) {
4887
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4888
0
        if (def == NULL)
4889
0
            return (NULL);
4890
0
        def->type = XML_RELAXNG_LIST;
4891
0
        if (node->children == NULL) {
4892
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4893
0
                       "Element %s is empty\n", node->name, NULL);
4894
0
        } else {
4895
0
            def->content =
4896
0
                xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4897
0
        }
4898
0
    } else if (IS_RELAXNG(node, "interleave")) {
4899
0
        def = xmlRelaxNGParseInterleave(ctxt, node);
4900
0
    } else if (IS_RELAXNG(node, "externalRef")) {
4901
0
        def = xmlRelaxNGProcessExternalRef(ctxt, node);
4902
0
    } else if (IS_RELAXNG(node, "notAllowed")) {
4903
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4904
0
        if (def == NULL)
4905
0
            return (NULL);
4906
0
        def->type = XML_RELAXNG_NOT_ALLOWED;
4907
0
        if (node->children != NULL) {
4908
0
            xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4909
0
                       "xmlRelaxNGParse: notAllowed element is not empty\n",
4910
0
                       NULL, NULL);
4911
0
        }
4912
0
    } else if (IS_RELAXNG(node, "grammar")) {
4913
0
        xmlRelaxNGGrammarPtr grammar, old;
4914
0
        xmlRelaxNGGrammarPtr oldparent;
4915
4916
0
        oldparent = ctxt->parentgrammar;
4917
0
        old = ctxt->grammar;
4918
0
        ctxt->parentgrammar = old;
4919
0
        grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4920
0
        if (old != NULL) {
4921
0
            ctxt->grammar = old;
4922
0
            ctxt->parentgrammar = oldparent;
4923
#if 0
4924
            if (grammar != NULL) {
4925
                grammar->next = old->next;
4926
                old->next = grammar;
4927
            }
4928
#endif
4929
0
        }
4930
0
        if (grammar != NULL)
4931
0
            def = grammar->start;
4932
0
        else
4933
0
            def = NULL;
4934
0
    } else if (IS_RELAXNG(node, "parentRef")) {
4935
0
        if (ctxt->parentgrammar == NULL) {
4936
0
            xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4937
0
                       "Use of parentRef without a parent grammar\n", NULL,
4938
0
                       NULL);
4939
0
            return (NULL);
4940
0
        }
4941
0
        def = xmlRelaxNGNewDefine(ctxt, node);
4942
0
        if (def == NULL)
4943
0
            return (NULL);
4944
0
        def->type = XML_RELAXNG_PARENTREF;
4945
0
        def->name = xmlGetProp(node, BAD_CAST "name");
4946
0
        if (def->name == NULL) {
4947
0
            xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4948
0
                       "parentRef has no name\n", NULL, NULL);
4949
0
        } else {
4950
0
            xmlRelaxNGNormExtSpace(def->name);
4951
0
            if (xmlValidateNCName(def->name, 0)) {
4952
0
                xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4953
0
                           "parentRef name '%s' is not an NCName\n",
4954
0
                           def->name, NULL);
4955
0
            }
4956
0
        }
4957
0
        if (node->children != NULL) {
4958
0
            xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4959
0
                       "parentRef is not empty\n", NULL, NULL);
4960
0
        }
4961
0
        if (ctxt->parentgrammar->refs == NULL)
4962
0
            ctxt->parentgrammar->refs = xmlHashCreate(10);
4963
0
        if (ctxt->parentgrammar->refs == NULL) {
4964
0
            xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4965
0
                       "Could not create references hash\n", NULL, NULL);
4966
0
            def = NULL;
4967
0
        } else if (def->name != NULL) {
4968
0
            int tmp;
4969
4970
0
            tmp =
4971
0
                xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4972
0
            if (tmp < 0) {
4973
0
                xmlRelaxNGDefinePtr prev;
4974
4975
0
                prev = (xmlRelaxNGDefinePtr)
4976
0
                    xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4977
0
                if (prev == NULL) {
4978
0
                    xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4979
0
                               "Internal error parentRef definitions '%s'\n",
4980
0
                               def->name, NULL);
4981
0
                    def = NULL;
4982
0
                } else {
4983
0
                    def->nextHash = prev->nextHash;
4984
0
                    prev->nextHash = def;
4985
0
                }
4986
0
            }
4987
0
        }
4988
0
    } else if (IS_RELAXNG(node, "mixed")) {
4989
0
        if (node->children == NULL) {
4990
0
            xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4991
0
                       NULL, NULL);
4992
0
            def = NULL;
4993
0
        } else {
4994
0
            def = xmlRelaxNGParseInterleave(ctxt, node);
4995
0
            if (def != NULL) {
4996
0
                xmlRelaxNGDefinePtr tmp;
4997
4998
0
                if ((def->content != NULL) && (def->content->next != NULL)) {
4999
0
                    tmp = xmlRelaxNGNewDefine(ctxt, node);
5000
0
                    if (tmp != NULL) {
5001
0
                        tmp->type = XML_RELAXNG_GROUP;
5002
0
                        tmp->content = def->content;
5003
0
                        def->content = tmp;
5004
0
                    }
5005
0
                }
5006
5007
0
                tmp = xmlRelaxNGNewDefine(ctxt, node);
5008
0
                if (tmp == NULL)
5009
0
                    return (def);
5010
0
                tmp->type = XML_RELAXNG_TEXT;
5011
0
                tmp->next = def->content;
5012
0
                def->content = tmp;
5013
0
            }
5014
0
        }
5015
0
    } else {
5016
0
        xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5017
0
                   "Unexpected node %s is not a pattern\n", node->name,
5018
0
                   NULL);
5019
0
        def = NULL;
5020
0
    }
5021
0
    return (def);
5022
0
}
5023
5024
/**
5025
 * xmlRelaxNGParseAttribute:
5026
 * @ctxt:  a Relax-NG parser context
5027
 * @node:  the element node
5028
 *
5029
 * parse the content of a RelaxNG attribute node.
5030
 *
5031
 * Returns the definition pointer or NULL in case of error.
5032
 */
5033
static xmlRelaxNGDefinePtr
5034
xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5035
0
{
5036
0
    xmlRelaxNGDefinePtr ret, cur;
5037
0
    xmlNodePtr child;
5038
0
    int old_flags;
5039
5040
0
    ret = xmlRelaxNGNewDefine(ctxt, node);
5041
0
    if (ret == NULL)
5042
0
        return (NULL);
5043
0
    ret->type = XML_RELAXNG_ATTRIBUTE;
5044
0
    ret->parent = ctxt->def;
5045
0
    child = node->children;
5046
0
    if (child == NULL) {
5047
0
        xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5048
0
                   "xmlRelaxNGParseattribute: attribute has no children\n",
5049
0
                   NULL, NULL);
5050
0
        return (ret);
5051
0
    }
5052
0
    old_flags = ctxt->flags;
5053
0
    ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
5054
0
    cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5055
0
    if (cur != NULL)
5056
0
        child = child->next;
5057
5058
0
    if (child != NULL) {
5059
0
        cur = xmlRelaxNGParsePattern(ctxt, child);
5060
0
        if (cur != NULL) {
5061
0
            switch (cur->type) {
5062
0
                case XML_RELAXNG_EMPTY:
5063
0
                case XML_RELAXNG_NOT_ALLOWED:
5064
0
                case XML_RELAXNG_TEXT:
5065
0
                case XML_RELAXNG_ELEMENT:
5066
0
                case XML_RELAXNG_DATATYPE:
5067
0
                case XML_RELAXNG_VALUE:
5068
0
                case XML_RELAXNG_LIST:
5069
0
                case XML_RELAXNG_REF:
5070
0
                case XML_RELAXNG_PARENTREF:
5071
0
                case XML_RELAXNG_EXTERNALREF:
5072
0
                case XML_RELAXNG_DEF:
5073
0
                case XML_RELAXNG_ONEORMORE:
5074
0
                case XML_RELAXNG_ZEROORMORE:
5075
0
                case XML_RELAXNG_OPTIONAL:
5076
0
                case XML_RELAXNG_CHOICE:
5077
0
                case XML_RELAXNG_GROUP:
5078
0
                case XML_RELAXNG_INTERLEAVE:
5079
0
                case XML_RELAXNG_ATTRIBUTE:
5080
0
                    ret->content = cur;
5081
0
                    cur->parent = ret;
5082
0
                    break;
5083
0
                case XML_RELAXNG_START:
5084
0
                case XML_RELAXNG_PARAM:
5085
0
                case XML_RELAXNG_EXCEPT:
5086
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5087
0
                               "attribute has invalid content\n", NULL,
5088
0
                               NULL);
5089
0
                    break;
5090
0
                case XML_RELAXNG_NOOP:
5091
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5092
0
                               "RNG Internal error, noop found in attribute\n",
5093
0
                               NULL, NULL);
5094
0
                    break;
5095
0
            }
5096
0
        }
5097
0
        child = child->next;
5098
0
    }
5099
0
    if (child != NULL) {
5100
0
        xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5101
0
                   "attribute has multiple children\n", NULL, NULL);
5102
0
    }
5103
0
    ctxt->flags = old_flags;
5104
0
    return (ret);
5105
0
}
5106
5107
/**
5108
 * xmlRelaxNGParseExceptNameClass:
5109
 * @ctxt:  a Relax-NG parser context
5110
 * @node:  the except node
5111
 * @attr:  1 if within an attribute, 0 if within an element
5112
 *
5113
 * parse the content of a RelaxNG nameClass node.
5114
 *
5115
 * Returns the definition pointer or NULL in case of error.
5116
 */
5117
static xmlRelaxNGDefinePtr
5118
xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
5119
                               xmlNodePtr node, int attr)
5120
0
{
5121
0
    xmlRelaxNGDefinePtr ret, cur, last = NULL;
5122
0
    xmlNodePtr child;
5123
5124
0
    if (!IS_RELAXNG(node, "except")) {
5125
0
        xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5126
0
                   "Expecting an except node\n", NULL, NULL);
5127
0
        return (NULL);
5128
0
    }
5129
0
    if (node->next != NULL) {
5130
0
        xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5131
0
                   "exceptNameClass allows only a single except node\n",
5132
0
                   NULL, NULL);
5133
0
    }
5134
0
    if (node->children == NULL) {
5135
0
        xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5136
0
                   NULL, NULL);
5137
0
        return (NULL);
5138
0
    }
5139
5140
0
    ret = xmlRelaxNGNewDefine(ctxt, node);
5141
0
    if (ret == NULL)
5142
0
        return (NULL);
5143
0
    ret->type = XML_RELAXNG_EXCEPT;
5144
0
    child = node->children;
5145
0
    while (child != NULL) {
5146
0
        cur = xmlRelaxNGNewDefine(ctxt, child);
5147
0
        if (cur == NULL)
5148
0
            break;
5149
0
        if (attr)
5150
0
            cur->type = XML_RELAXNG_ATTRIBUTE;
5151
0
        else
5152
0
            cur->type = XML_RELAXNG_ELEMENT;
5153
5154
0
        if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
5155
0
            if (last == NULL) {
5156
0
                ret->content = cur;
5157
0
            } else {
5158
0
                last->next = cur;
5159
0
            }
5160
0
            last = cur;
5161
0
        }
5162
0
        child = child->next;
5163
0
    }
5164
5165
0
    return (ret);
5166
0
}
5167
5168
/**
5169
 * xmlRelaxNGParseNameClass:
5170
 * @ctxt:  a Relax-NG parser context
5171
 * @node:  the nameClass node
5172
 * @def:  the current definition
5173
 *
5174
 * parse the content of a RelaxNG nameClass node.
5175
 *
5176
 * Returns the definition pointer or NULL in case of error.
5177
 */
5178
static xmlRelaxNGDefinePtr
5179
xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5180
                         xmlRelaxNGDefinePtr def)
5181
0
{
5182
0
    xmlRelaxNGDefinePtr ret, tmp;
5183
0
    xmlChar *val;
5184
5185
0
    ret = def;
5186
0
    if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
5187
0
        (IS_RELAXNG(node, "nsName"))) {
5188
0
        if ((def->type != XML_RELAXNG_ELEMENT) &&
5189
0
            (def->type != XML_RELAXNG_ATTRIBUTE)) {
5190
0
            ret = xmlRelaxNGNewDefine(ctxt, node);
5191
0
            if (ret == NULL)
5192
0
                return (NULL);
5193
0
            ret->parent = def;
5194
0
            if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5195
0
                ret->type = XML_RELAXNG_ATTRIBUTE;
5196
0
            else
5197
0
                ret->type = XML_RELAXNG_ELEMENT;
5198
0
        }
5199
0
    }
5200
0
    if (IS_RELAXNG(node, "name")) {
5201
0
        val = xmlNodeGetContent(node);
5202
0
        xmlRelaxNGNormExtSpace(val);
5203
0
        if (xmlValidateNCName(val, 0)) {
5204
0
      if (node->parent != NULL)
5205
0
    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5206
0
         "Element %s name '%s' is not an NCName\n",
5207
0
         node->parent->name, val);
5208
0
      else
5209
0
    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5210
0
         "name '%s' is not an NCName\n",
5211
0
         val, NULL);
5212
0
        }
5213
0
        ret->name = val;
5214
0
        val = xmlGetProp(node, BAD_CAST "ns");
5215
0
        ret->ns = val;
5216
0
        if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5217
0
            (val != NULL) &&
5218
0
            (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5219
0
      xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5220
0
                        "Attribute with namespace '%s' is not allowed\n",
5221
0
                        val, NULL);
5222
0
        }
5223
0
        if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5224
0
            (val != NULL) &&
5225
0
            (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
5226
0
      xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5227
0
                       "Attribute with QName 'xmlns' is not allowed\n",
5228
0
                       val, NULL);
5229
0
        }
5230
0
    } else if (IS_RELAXNG(node, "anyName")) {
5231
0
        ret->name = NULL;
5232
0
        ret->ns = NULL;
5233
0
        if (node->children != NULL) {
5234
0
            ret->nameClass =
5235
0
                xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5236
0
                                               (def->type ==
5237
0
                                                XML_RELAXNG_ATTRIBUTE));
5238
0
        }
5239
0
    } else if (IS_RELAXNG(node, "nsName")) {
5240
0
        ret->name = NULL;
5241
0
        ret->ns = xmlGetProp(node, BAD_CAST "ns");
5242
0
        if (ret->ns == NULL) {
5243
0
            xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5244
0
                       "nsName has no ns attribute\n", NULL, NULL);
5245
0
        }
5246
0
        if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5247
0
            (ret->ns != NULL) &&
5248
0
            (xmlStrEqual
5249
0
             (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5250
0
            xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5251
0
                       "Attribute with namespace '%s' is not allowed\n",
5252
0
                       ret->ns, NULL);
5253
0
        }
5254
0
        if (node->children != NULL) {
5255
0
            ret->nameClass =
5256
0
                xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5257
0
                                               (def->type ==
5258
0
                                                XML_RELAXNG_ATTRIBUTE));
5259
0
        }
5260
0
    } else if (IS_RELAXNG(node, "choice")) {
5261
0
        xmlNodePtr child;
5262
0
        xmlRelaxNGDefinePtr last = NULL;
5263
5264
0
        if (def->type == XML_RELAXNG_CHOICE) {
5265
0
            ret = def;
5266
0
        } else {
5267
0
            ret = xmlRelaxNGNewDefine(ctxt, node);
5268
0
            if (ret == NULL)
5269
0
                return (NULL);
5270
0
            ret->parent = def;
5271
0
            ret->type = XML_RELAXNG_CHOICE;
5272
0
        }
5273
5274
0
        if (node->children == NULL) {
5275
0
            xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5276
0
                       "Element choice is empty\n", NULL, NULL);
5277
0
        } else {
5278
5279
0
            child = node->children;
5280
0
            while (child != NULL) {
5281
0
                tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5282
0
                if (tmp != NULL) {
5283
0
                    if (last == NULL) {
5284
0
                        last = tmp;
5285
0
                    } else if (tmp != ret) {
5286
0
                        last->next = tmp;
5287
0
                        last = tmp;
5288
0
                    }
5289
0
                }
5290
0
                child = child->next;
5291
0
            }
5292
0
        }
5293
0
    } else {
5294
0
        xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5295
0
                   "expecting name, anyName, nsName or choice : got %s\n",
5296
0
                   (node == NULL ? (const xmlChar *) "nothing" : node->name),
5297
0
       NULL);
5298
0
        return (NULL);
5299
0
    }
5300
0
    if (ret != def) {
5301
0
        if (def->nameClass == NULL) {
5302
0
            def->nameClass = ret;
5303
0
        } else {
5304
0
            tmp = def->nameClass;
5305
0
            while (tmp->next != NULL) {
5306
0
                tmp = tmp->next;
5307
0
            }
5308
0
            tmp->next = ret;
5309
0
        }
5310
0
    }
5311
0
    return (ret);
5312
0
}
5313
5314
/**
5315
 * xmlRelaxNGParseElement:
5316
 * @ctxt:  a Relax-NG parser context
5317
 * @node:  the element node
5318
 *
5319
 * parse the content of a RelaxNG element node.
5320
 *
5321
 * Returns the definition pointer or NULL in case of error.
5322
 */
5323
static xmlRelaxNGDefinePtr
5324
xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5325
0
{
5326
0
    xmlRelaxNGDefinePtr ret, cur, last;
5327
0
    xmlNodePtr child;
5328
0
    const xmlChar *olddefine;
5329
5330
0
    ret = xmlRelaxNGNewDefine(ctxt, node);
5331
0
    if (ret == NULL)
5332
0
        return (NULL);
5333
0
    ret->type = XML_RELAXNG_ELEMENT;
5334
0
    ret->parent = ctxt->def;
5335
0
    child = node->children;
5336
0
    if (child == NULL) {
5337
0
        xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5338
0
                   "xmlRelaxNGParseElement: element has no children\n",
5339
0
                   NULL, NULL);
5340
0
        return (ret);
5341
0
    }
5342
0
    cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5343
0
    if (cur != NULL)
5344
0
        child = child->next;
5345
5346
0
    if (child == NULL) {
5347
0
        xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5348
0
                   "xmlRelaxNGParseElement: element has no content\n",
5349
0
                   NULL, NULL);
5350
0
        return (ret);
5351
0
    }
5352
0
    olddefine = ctxt->define;
5353
0
    ctxt->define = NULL;
5354
0
    last = NULL;
5355
0
    while (child != NULL) {
5356
0
        cur = xmlRelaxNGParsePattern(ctxt, child);
5357
0
        if (cur != NULL) {
5358
0
            cur->parent = ret;
5359
0
            switch (cur->type) {
5360
0
                case XML_RELAXNG_EMPTY:
5361
0
                case XML_RELAXNG_NOT_ALLOWED:
5362
0
                case XML_RELAXNG_TEXT:
5363
0
                case XML_RELAXNG_ELEMENT:
5364
0
                case XML_RELAXNG_DATATYPE:
5365
0
                case XML_RELAXNG_VALUE:
5366
0
                case XML_RELAXNG_LIST:
5367
0
                case XML_RELAXNG_REF:
5368
0
                case XML_RELAXNG_PARENTREF:
5369
0
                case XML_RELAXNG_EXTERNALREF:
5370
0
                case XML_RELAXNG_DEF:
5371
0
                case XML_RELAXNG_ZEROORMORE:
5372
0
                case XML_RELAXNG_ONEORMORE:
5373
0
                case XML_RELAXNG_OPTIONAL:
5374
0
                case XML_RELAXNG_CHOICE:
5375
0
                case XML_RELAXNG_GROUP:
5376
0
                case XML_RELAXNG_INTERLEAVE:
5377
0
                    if (last == NULL) {
5378
0
                        ret->content = last = cur;
5379
0
                    } else {
5380
0
                        if ((last->type == XML_RELAXNG_ELEMENT) &&
5381
0
                            (ret->content == last)) {
5382
0
                            ret->content = xmlRelaxNGNewDefine(ctxt, node);
5383
0
                            if (ret->content != NULL) {
5384
0
                                ret->content->type = XML_RELAXNG_GROUP;
5385
0
                                ret->content->content = last;
5386
0
                            } else {
5387
0
                                ret->content = last;
5388
0
                            }
5389
0
                        }
5390
0
                        last->next = cur;
5391
0
                        last = cur;
5392
0
                    }
5393
0
                    break;
5394
0
                case XML_RELAXNG_ATTRIBUTE:
5395
0
                    cur->next = ret->attrs;
5396
0
                    ret->attrs = cur;
5397
0
                    break;
5398
0
                case XML_RELAXNG_START:
5399
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5400
0
                               "RNG Internal error, start found in element\n",
5401
0
                               NULL, NULL);
5402
0
                    break;
5403
0
                case XML_RELAXNG_PARAM:
5404
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5405
0
                               "RNG Internal error, param found in element\n",
5406
0
                               NULL, NULL);
5407
0
                    break;
5408
0
                case XML_RELAXNG_EXCEPT:
5409
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5410
0
                               "RNG Internal error, except found in element\n",
5411
0
                               NULL, NULL);
5412
0
                    break;
5413
0
                case XML_RELAXNG_NOOP:
5414
0
                    xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5415
0
                               "RNG Internal error, noop found in element\n",
5416
0
                               NULL, NULL);
5417
0
                    break;
5418
0
            }
5419
0
        }
5420
0
        child = child->next;
5421
0
    }
5422
0
    ctxt->define = olddefine;
5423
0
    return (ret);
5424
0
}
5425
5426
/**
5427
 * xmlRelaxNGParsePatterns:
5428
 * @ctxt:  a Relax-NG parser context
5429
 * @nodes:  list of nodes
5430
 * @group:  use an implicit <group> for elements
5431
 *
5432
 * parse the content of a RelaxNG start node.
5433
 *
5434
 * Returns the definition pointer or NULL in case of error.
5435
 */
5436
static xmlRelaxNGDefinePtr
5437
xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5438
                        int group)
5439
0
{
5440
0
    xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
5441
5442
0
    parent = ctxt->def;
5443
0
    while (nodes != NULL) {
5444
0
        if (IS_RELAXNG(nodes, "element")) {
5445
0
            cur = xmlRelaxNGParseElement(ctxt, nodes);
5446
0
            if (cur == NULL)
5447
0
                return (NULL);
5448
0
            if (def == NULL) {
5449
0
                def = last = cur;
5450
0
            } else {
5451
0
                if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5452
0
                    (def == last)) {
5453
0
                    def = xmlRelaxNGNewDefine(ctxt, nodes);
5454
0
                    if (def == NULL)
5455
0
                        return (NULL);
5456
0
                    def->type = XML_RELAXNG_GROUP;
5457
0
                    def->content = last;
5458
0
                }
5459
0
                last->next = cur;
5460
0
                last = cur;
5461
0
            }
5462
0
            cur->parent = parent;
5463
0
        } else {
5464
0
            cur = xmlRelaxNGParsePattern(ctxt, nodes);
5465
0
            if (cur != NULL) {
5466
0
                if (def == NULL) {
5467
0
                    def = last = cur;
5468
0
                } else {
5469
0
                    last->next = cur;
5470
0
                    last = cur;
5471
0
                }
5472
0
            }
5473
0
        }
5474
0
        nodes = nodes->next;
5475
0
    }
5476
0
    return (def);
5477
0
}
5478
5479
/**
5480
 * xmlRelaxNGParseStart:
5481
 * @ctxt:  a Relax-NG parser context
5482
 * @nodes:  start children nodes
5483
 *
5484
 * parse the content of a RelaxNG start node.
5485
 *
5486
 * Returns 0 in case of success, -1 in case of error
5487
 */
5488
static int
5489
xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5490
0
{
5491
0
    int ret = 0;
5492
0
    xmlRelaxNGDefinePtr def = NULL, last;
5493
5494
0
    if (nodes == NULL) {
5495
0
        xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5496
0
                   NULL, NULL);
5497
0
        return (-1);
5498
0
    }
5499
0
    if (IS_RELAXNG(nodes, "empty")) {
5500
0
        def = xmlRelaxNGNewDefine(ctxt, nodes);
5501
0
        if (def == NULL)
5502
0
            return (-1);
5503
0
        def->type = XML_RELAXNG_EMPTY;
5504
0
        if (nodes->children != NULL) {
5505
0
            xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5506
0
                       "element empty is not empty\n", NULL, NULL);
5507
0
        }
5508
0
    } else if (IS_RELAXNG(nodes, "notAllowed")) {
5509
0
        def = xmlRelaxNGNewDefine(ctxt, nodes);
5510
0
        if (def == NULL)
5511
0
            return (-1);
5512
0
        def->type = XML_RELAXNG_NOT_ALLOWED;
5513
0
        if (nodes->children != NULL) {
5514
0
            xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5515
0
                       "element notAllowed is not empty\n", NULL, NULL);
5516
0
        }
5517
0
    } else {
5518
0
        def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
5519
0
    }
5520
0
    if (ctxt->grammar->start != NULL) {
5521
0
        last = ctxt->grammar->start;
5522
0
        while (last->next != NULL)
5523
0
            last = last->next;
5524
0
        last->next = def;
5525
0
    } else {
5526
0
        ctxt->grammar->start = def;
5527
0
    }
5528
0
    nodes = nodes->next;
5529
0
    if (nodes != NULL) {
5530
0
        xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5531
0
                   "start more than one children\n", NULL, NULL);
5532
0
        return (-1);
5533
0
    }
5534
0
    return (ret);
5535
0
}
5536
5537
/**
5538
 * xmlRelaxNGParseGrammarContent:
5539
 * @ctxt:  a Relax-NG parser context
5540
 * @nodes:  grammar children nodes
5541
 *
5542
 * parse the content of a RelaxNG grammar node.
5543
 *
5544
 * Returns 0 in case of success, -1 in case of error
5545
 */
5546
static int
5547
xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5548
                              xmlNodePtr nodes)
5549
0
{
5550
0
    int ret = 0, tmp;
5551
5552
0
    if (nodes == NULL) {
5553
0
        xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5554
0
                   "grammar has no children\n", NULL, NULL);
5555
0
        return (-1);
5556
0
    }
5557
0
    while (nodes != NULL) {
5558
0
        if (IS_RELAXNG(nodes, "start")) {
5559
0
            if (nodes->children == NULL) {
5560
0
                xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5561
0
                           "start has no children\n", NULL, NULL);
5562
0
            } else {
5563
0
                tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5564
0
                if (tmp != 0)
5565
0
                    ret = -1;
5566
0
            }
5567
0
        } else if (IS_RELAXNG(nodes, "define")) {
5568
0
            tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5569
0
            if (tmp != 0)
5570
0
                ret = -1;
5571
0
        } else if (IS_RELAXNG(nodes, "include")) {
5572
0
            tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5573
0
            if (tmp != 0)
5574
0
                ret = -1;
5575
0
        } else {
5576
0
            xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5577
0
                       "grammar has unexpected child %s\n", nodes->name,
5578
0
                       NULL);
5579
0
            ret = -1;
5580
0
        }
5581
0
        nodes = nodes->next;
5582
0
    }
5583
0
    return (ret);
5584
0
}
5585
5586
/**
5587
 * xmlRelaxNGCheckReference:
5588
 * @ref:  the ref
5589
 * @ctxt:  a Relax-NG parser context
5590
 * @name:  the name associated to the defines
5591
 *
5592
 * Applies the 4.17. combine attribute rule for all the define
5593
 * element of a given grammar using the same name.
5594
 */
5595
static void
5596
xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name)
5597
0
{
5598
0
    xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload;
5599
0
    xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
5600
0
    xmlRelaxNGGrammarPtr grammar;
5601
0
    xmlRelaxNGDefinePtr def, cur;
5602
5603
    /*
5604
     * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5605
     */
5606
0
    if (ref->dflags & IS_EXTERNAL_REF)
5607
0
        return;
5608
5609
0
    grammar = ctxt->grammar;
5610
0
    if (grammar == NULL) {
5611
0
        xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5612
0
                   "Internal error: no grammar in CheckReference %s\n",
5613
0
                   name, NULL);
5614
0
        return;
5615
0
    }
5616
0
    if (ref->content != NULL) {
5617
0
        xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5618
0
                   "Internal error: reference has content in CheckReference %s\n",
5619
0
                   name, NULL);
5620
0
        return;
5621
0
    }
5622
0
    if (grammar->defs != NULL) {
5623
0
        def = xmlHashLookup(grammar->defs, name);
5624
0
        if (def != NULL) {
5625
0
            cur = ref;
5626
0
            while (cur != NULL) {
5627
0
                cur->content = def;
5628
0
                cur = cur->nextHash;
5629
0
            }
5630
0
        } else {
5631
0
            xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5632
0
                       "Reference %s has no matching definition\n", name,
5633
0
                       NULL);
5634
0
        }
5635
0
    } else {
5636
0
        xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5637
0
                   "Reference %s has no matching definition\n", name,
5638
0
                   NULL);
5639
0
    }
5640
0
}
5641
5642
/**
5643
 * xmlRelaxNGCheckCombine:
5644
 * @define:  the define(s) list
5645
 * @ctxt:  a Relax-NG parser context
5646
 * @name:  the name associated to the defines
5647
 *
5648
 * Applies the 4.17. combine attribute rule for all the define
5649
 * element of a given grammar using the same name.
5650
 */
5651
static void
5652
xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name)
5653
0
{
5654
0
    xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload;
5655
0
    xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
5656
0
    xmlChar *combine;
5657
0
    int choiceOrInterleave = -1;
5658
0
    int missing = 0;
5659
0
    xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5660
5661
0
    if (define->nextHash == NULL)
5662
0
        return;
5663
0
    cur = define;
5664
0
    while (cur != NULL) {
5665
0
        combine = xmlGetProp(cur->node, BAD_CAST "combine");
5666
0
        if (combine != NULL) {
5667
0
            if (xmlStrEqual(combine, BAD_CAST "choice")) {
5668
0
                if (choiceOrInterleave == -1)
5669
0
                    choiceOrInterleave = 1;
5670
0
                else if (choiceOrInterleave == 0) {
5671
0
                    xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5672
0
                               "Defines for %s use both 'choice' and 'interleave'\n",
5673
0
                               name, NULL);
5674
0
                }
5675
0
            } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5676
0
                if (choiceOrInterleave == -1)
5677
0
                    choiceOrInterleave = 0;
5678
0
                else if (choiceOrInterleave == 1) {
5679
0
                    xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5680
0
                               "Defines for %s use both 'choice' and 'interleave'\n",
5681
0
                               name, NULL);
5682
0
                }
5683
0
            } else {
5684
0
                xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5685
0
                           "Defines for %s use unknown combine value '%s''\n",
5686
0
                           name, combine);
5687
0
            }
5688
0
            xmlFree(combine);
5689
0
        } else {
5690
0
            if (missing == 0)
5691
0
                missing = 1;
5692
0
            else {
5693
0
                xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5694
0
                           "Some defines for %s needs the combine attribute\n",
5695
0
                           name, NULL);
5696
0
            }
5697
0
        }
5698
5699
0
        cur = cur->nextHash;
5700
0
    }
5701
0
    if (choiceOrInterleave == -1)
5702
0
        choiceOrInterleave = 0;
5703
0
    cur = xmlRelaxNGNewDefine(ctxt, define->node);
5704
0
    if (cur == NULL)
5705
0
        return;
5706
0
    if (choiceOrInterleave == 0)
5707
0
        cur->type = XML_RELAXNG_INTERLEAVE;
5708
0
    else
5709
0
        cur->type = XML_RELAXNG_CHOICE;
5710
0
    tmp = define;
5711
0
    last = NULL;
5712
0
    while (tmp != NULL) {
5713
0
        if (tmp->content != NULL) {
5714
0
            if (tmp->content->next != NULL) {
5715
                /*
5716
                 * we need first to create a wrapper.
5717
                 */
5718
0
                tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5719
0
                if (tmp2 == NULL)
5720
0
                    break;
5721
0
                tmp2->type = XML_RELAXNG_GROUP;
5722
0
                tmp2->content = tmp->content;
5723
0
            } else {
5724
0
                tmp2 = tmp->content;
5725
0
            }
5726
0
            if (last == NULL) {
5727
0
                cur->content = tmp2;
5728
0
            } else {
5729
0
                last->next = tmp2;
5730
0
            }
5731
0
            last = tmp2;
5732
0
        }
5733
0
        tmp->content = cur;
5734
0
        tmp = tmp->nextHash;
5735
0
    }
5736
0
    define->content = cur;
5737
0
    if (choiceOrInterleave == 0) {
5738
0
        if (ctxt->interleaves == NULL)
5739
0
            ctxt->interleaves = xmlHashCreate(10);
5740
0
        if (ctxt->interleaves == NULL) {
5741
0
            xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5742
0
                       "Failed to create interleaves hash table\n", NULL,
5743
0
                       NULL);
5744
0
        } else {
5745
0
            char tmpname[32];
5746
5747
0
            snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5748
0
            if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5749
0
                0) {
5750
0
                xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5751
0
                           "Failed to add %s to hash table\n",
5752
0
         (const xmlChar *) tmpname, NULL);
5753
0
            }
5754
0
        }
5755
0
    }
5756
0
}
5757
5758
/**
5759
 * xmlRelaxNGCombineStart:
5760
 * @ctxt:  a Relax-NG parser context
5761
 * @grammar:  the grammar
5762
 *
5763
 * Applies the 4.17. combine rule for all the start
5764
 * element of a given grammar.
5765
 */
5766
static void
5767
xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5768
                       xmlRelaxNGGrammarPtr grammar)
5769
0
{
5770
0
    xmlRelaxNGDefinePtr starts;
5771
0
    xmlChar *combine;
5772
0
    int choiceOrInterleave = -1;
5773
0
    int missing = 0;
5774
0
    xmlRelaxNGDefinePtr cur;
5775
5776
0
    starts = grammar->start;
5777
0
    if ((starts == NULL) || (starts->next == NULL))
5778
0
        return;
5779
0
    cur = starts;
5780
0
    while (cur != NULL) {
5781
0
        if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5782
0
            (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5783
0
            combine = NULL;
5784
0
            xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5785
0
                       "Internal error: start element not found\n", NULL,
5786
0
                       NULL);
5787
0
        } else {
5788
0
            combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5789
0
        }
5790
5791
0
        if (combine != NULL) {
5792
0
            if (xmlStrEqual(combine, BAD_CAST "choice")) {
5793
0
                if (choiceOrInterleave == -1)
5794
0
                    choiceOrInterleave = 1;
5795
0
                else if (choiceOrInterleave == 0) {
5796
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5797
0
                               "<start> use both 'choice' and 'interleave'\n",
5798
0
                               NULL, NULL);
5799
0
                }
5800
0
            } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5801
0
                if (choiceOrInterleave == -1)
5802
0
                    choiceOrInterleave = 0;
5803
0
                else if (choiceOrInterleave == 1) {
5804
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5805
0
                               "<start> use both 'choice' and 'interleave'\n",
5806
0
                               NULL, NULL);
5807
0
                }
5808
0
            } else {
5809
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5810
0
                           "<start> uses unknown combine value '%s''\n",
5811
0
                           combine, NULL);
5812
0
            }
5813
0
            xmlFree(combine);
5814
0
        } else {
5815
0
            if (missing == 0)
5816
0
                missing = 1;
5817
0
            else {
5818
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5819
0
                           "Some <start> element miss the combine attribute\n",
5820
0
                           NULL, NULL);
5821
0
            }
5822
0
        }
5823
5824
0
        cur = cur->next;
5825
0
    }
5826
0
    if (choiceOrInterleave == -1)
5827
0
        choiceOrInterleave = 0;
5828
0
    cur = xmlRelaxNGNewDefine(ctxt, starts->node);
5829
0
    if (cur == NULL)
5830
0
        return;
5831
0
    if (choiceOrInterleave == 0)
5832
0
        cur->type = XML_RELAXNG_INTERLEAVE;
5833
0
    else
5834
0
        cur->type = XML_RELAXNG_CHOICE;
5835
0
    cur->content = grammar->start;
5836
0
    grammar->start = cur;
5837
0
    if (choiceOrInterleave == 0) {
5838
0
        if (ctxt->interleaves == NULL)
5839
0
            ctxt->interleaves = xmlHashCreate(10);
5840
0
        if (ctxt->interleaves == NULL) {
5841
0
            xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5842
0
                       "Failed to create interleaves hash table\n", NULL,
5843
0
                       NULL);
5844
0
        } else {
5845
0
            char tmpname[32];
5846
5847
0
            snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5848
0
            if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5849
0
                0) {
5850
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5851
0
                           "Failed to add %s to hash table\n",
5852
0
         (const xmlChar *) tmpname, NULL);
5853
0
            }
5854
0
        }
5855
0
    }
5856
0
}
5857
5858
/**
5859
 * xmlRelaxNGCheckCycles:
5860
 * @ctxt:  a Relax-NG parser context
5861
 * @nodes:  grammar children nodes
5862
 * @depth:  the counter
5863
 *
5864
 * Check for cycles.
5865
 *
5866
 * Returns 0 if check passed, and -1 in case of error
5867
 */
5868
static int
5869
xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5870
                      xmlRelaxNGDefinePtr cur, int depth)
5871
0
{
5872
0
    int ret = 0;
5873
5874
0
    while ((ret == 0) && (cur != NULL)) {
5875
0
        if ((cur->type == XML_RELAXNG_REF) ||
5876
0
            (cur->type == XML_RELAXNG_PARENTREF)) {
5877
0
            if (cur->depth == -1) {
5878
0
                cur->depth = depth;
5879
0
                ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5880
0
                cur->depth = -2;
5881
0
            } else if (depth == cur->depth) {
5882
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5883
0
                           "Detected a cycle in %s references\n",
5884
0
                           cur->name, NULL);
5885
0
                return (-1);
5886
0
            }
5887
0
        } else if (cur->type == XML_RELAXNG_ELEMENT) {
5888
0
            ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5889
0
        } else {
5890
0
            ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5891
0
        }
5892
0
        cur = cur->next;
5893
0
    }
5894
0
    return (ret);
5895
0
}
5896
5897
/**
5898
 * xmlRelaxNGTryUnlink:
5899
 * @ctxt:  a Relax-NG parser context
5900
 * @cur:  the definition to unlink
5901
 * @parent:  the parent definition
5902
 * @prev:  the previous sibling definition
5903
 *
5904
 * Try to unlink a definition. If not possible make it a NOOP
5905
 *
5906
 * Returns the new prev definition
5907
 */
5908
static xmlRelaxNGDefinePtr
5909
xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5910
                    xmlRelaxNGDefinePtr cur,
5911
                    xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5912
0
{
5913
0
    if (prev != NULL) {
5914
0
        prev->next = cur->next;
5915
0
    } else {
5916
0
        if (parent != NULL) {
5917
0
            if (parent->content == cur)
5918
0
                parent->content = cur->next;
5919
0
            else if (parent->attrs == cur)
5920
0
                parent->attrs = cur->next;
5921
0
            else if (parent->nameClass == cur)
5922
0
                parent->nameClass = cur->next;
5923
0
        } else {
5924
0
            cur->type = XML_RELAXNG_NOOP;
5925
0
            prev = cur;
5926
0
        }
5927
0
    }
5928
0
    return (prev);
5929
0
}
5930
5931
/**
5932
 * xmlRelaxNGSimplify:
5933
 * @ctxt:  a Relax-NG parser context
5934
 * @nodes:  grammar children nodes
5935
 *
5936
 * Check for simplification of empty and notAllowed
5937
 */
5938
static void
5939
xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5940
                   xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5941
0
{
5942
0
    xmlRelaxNGDefinePtr prev = NULL;
5943
5944
0
    while (cur != NULL) {
5945
0
        if ((cur->type == XML_RELAXNG_REF) ||
5946
0
            (cur->type == XML_RELAXNG_PARENTREF)) {
5947
0
            if (cur->depth != -3) {
5948
0
                cur->depth = -3;
5949
0
                xmlRelaxNGSimplify(ctxt, cur->content, cur);
5950
0
            }
5951
0
        } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5952
0
            cur->parent = parent;
5953
0
            if ((parent != NULL) &&
5954
0
                ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5955
0
                 (parent->type == XML_RELAXNG_LIST) ||
5956
0
                 (parent->type == XML_RELAXNG_GROUP) ||
5957
0
                 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5958
0
                 (parent->type == XML_RELAXNG_ONEORMORE) ||
5959
0
                 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5960
0
                parent->type = XML_RELAXNG_NOT_ALLOWED;
5961
0
                break;
5962
0
            }
5963
0
            if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5964
0
                prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5965
0
            } else
5966
0
                prev = cur;
5967
0
        } else if (cur->type == XML_RELAXNG_EMPTY) {
5968
0
            cur->parent = parent;
5969
0
            if ((parent != NULL) &&
5970
0
                ((parent->type == XML_RELAXNG_ONEORMORE) ||
5971
0
                 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5972
0
                parent->type = XML_RELAXNG_EMPTY;
5973
0
                break;
5974
0
            }
5975
0
            if ((parent != NULL) &&
5976
0
                ((parent->type == XML_RELAXNG_GROUP) ||
5977
0
                 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5978
0
                prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5979
0
            } else
5980
0
                prev = cur;
5981
0
        } else {
5982
0
            cur->parent = parent;
5983
0
            if (cur->content != NULL)
5984
0
                xmlRelaxNGSimplify(ctxt, cur->content, cur);
5985
0
            if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5986
0
                xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5987
0
            if (cur->nameClass != NULL)
5988
0
                xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5989
            /*
5990
             * On Elements, try to move attribute only generating rules on
5991
             * the attrs rules.
5992
             */
5993
0
            if (cur->type == XML_RELAXNG_ELEMENT) {
5994
0
                int attronly;
5995
0
                xmlRelaxNGDefinePtr tmp, pre;
5996
5997
0
                while (cur->content != NULL) {
5998
0
                    attronly =
5999
0
                        xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6000
0
                    if (attronly == 1) {
6001
                        /*
6002
                         * migrate cur->content to attrs
6003
                         */
6004
0
                        tmp = cur->content;
6005
0
                        cur->content = tmp->next;
6006
0
                        tmp->next = cur->attrs;
6007
0
                        cur->attrs = tmp;
6008
0
                    } else {
6009
                        /*
6010
                         * cur->content can generate elements or text
6011
                         */
6012
0
                        break;
6013
0
                    }
6014
0
                }
6015
0
                pre = cur->content;
6016
0
                while ((pre != NULL) && (pre->next != NULL)) {
6017
0
                    tmp = pre->next;
6018
0
                    attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6019
0
                    if (attronly == 1) {
6020
                        /*
6021
                         * migrate tmp to attrs
6022
                         */
6023
0
                        pre->next = tmp->next;
6024
0
                        tmp->next = cur->attrs;
6025
0
                        cur->attrs = tmp;
6026
0
                    } else {
6027
0
                        pre = tmp;
6028
0
                    }
6029
0
                }
6030
0
            }
6031
            /*
6032
             * This may result in a simplification
6033
             */
6034
0
            if ((cur->type == XML_RELAXNG_GROUP) ||
6035
0
                (cur->type == XML_RELAXNG_INTERLEAVE)) {
6036
0
                if (cur->content == NULL)
6037
0
                    cur->type = XML_RELAXNG_EMPTY;
6038
0
                else if (cur->content->next == NULL) {
6039
0
                    if ((parent == NULL) && (prev == NULL)) {
6040
0
                        cur->type = XML_RELAXNG_NOOP;
6041
0
                    } else if (prev == NULL) {
6042
0
                        parent->content = cur->content;
6043
0
                        cur->content->next = cur->next;
6044
0
                        cur = cur->content;
6045
0
                    } else {
6046
0
                        cur->content->next = cur->next;
6047
0
                        prev->next = cur->content;
6048
0
                        cur = cur->content;
6049
0
                    }
6050
0
                }
6051
0
            }
6052
            /*
6053
             * the current node may have been transformed back
6054
             */
6055
0
            if ((cur->type == XML_RELAXNG_EXCEPT) &&
6056
0
                (cur->content != NULL) &&
6057
0
                (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6058
0
                prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6059
0
            } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6060
0
                if ((parent != NULL) &&
6061
0
                    ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6062
0
                     (parent->type == XML_RELAXNG_LIST) ||
6063
0
                     (parent->type == XML_RELAXNG_GROUP) ||
6064
0
                     (parent->type == XML_RELAXNG_INTERLEAVE) ||
6065
0
                     (parent->type == XML_RELAXNG_ONEORMORE) ||
6066
0
                     (parent->type == XML_RELAXNG_ZEROORMORE))) {
6067
0
                    parent->type = XML_RELAXNG_NOT_ALLOWED;
6068
0
                    break;
6069
0
                }
6070
0
                if ((parent != NULL) &&
6071
0
                    (parent->type == XML_RELAXNG_CHOICE)) {
6072
0
                    prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6073
0
                } else
6074
0
                    prev = cur;
6075
0
            } else if (cur->type == XML_RELAXNG_EMPTY) {
6076
0
                if ((parent != NULL) &&
6077
0
                    ((parent->type == XML_RELAXNG_ONEORMORE) ||
6078
0
                     (parent->type == XML_RELAXNG_ZEROORMORE))) {
6079
0
                    parent->type = XML_RELAXNG_EMPTY;
6080
0
                    break;
6081
0
                }
6082
0
                if ((parent != NULL) &&
6083
0
                    ((parent->type == XML_RELAXNG_GROUP) ||
6084
0
                     (parent->type == XML_RELAXNG_INTERLEAVE) ||
6085
0
                     (parent->type == XML_RELAXNG_CHOICE))) {
6086
0
                    prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6087
0
                } else
6088
0
                    prev = cur;
6089
0
            } else {
6090
0
                prev = cur;
6091
0
            }
6092
0
        }
6093
0
        cur = cur->next;
6094
0
    }
6095
0
}
6096
6097
/**
6098
 * xmlRelaxNGGroupContentType:
6099
 * @ct1:  the first content type
6100
 * @ct2:  the second content type
6101
 *
6102
 * Try to group 2 content types
6103
 *
6104
 * Returns the content type
6105
 */
6106
static xmlRelaxNGContentType
6107
xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6108
                           xmlRelaxNGContentType ct2)
6109
0
{
6110
0
    if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6111
0
        (ct2 == XML_RELAXNG_CONTENT_ERROR))
6112
0
        return (XML_RELAXNG_CONTENT_ERROR);
6113
0
    if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
6114
0
        return (ct2);
6115
0
    if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6116
0
        return (ct1);
6117
0
    if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
6118
0
        (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6119
0
        return (XML_RELAXNG_CONTENT_COMPLEX);
6120
0
    return (XML_RELAXNG_CONTENT_ERROR);
6121
0
}
6122
6123
/**
6124
 * xmlRelaxNGMaxContentType:
6125
 * @ct1:  the first content type
6126
 * @ct2:  the second content type
6127
 *
6128
 * Compute the max content-type
6129
 *
6130
 * Returns the content type
6131
 */
6132
static xmlRelaxNGContentType
6133
xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6134
                         xmlRelaxNGContentType ct2)
6135
0
{
6136
0
    if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6137
0
        (ct2 == XML_RELAXNG_CONTENT_ERROR))
6138
0
        return (XML_RELAXNG_CONTENT_ERROR);
6139
0
    if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
6140
0
        (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6141
0
        return (XML_RELAXNG_CONTENT_SIMPLE);
6142
0
    if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
6143
0
        (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6144
0
        return (XML_RELAXNG_CONTENT_COMPLEX);
6145
0
    return (XML_RELAXNG_CONTENT_EMPTY);
6146
0
}
6147
6148
/**
6149
 * xmlRelaxNGCheckRules:
6150
 * @ctxt:  a Relax-NG parser context
6151
 * @cur:  the current definition
6152
 * @flags:  some accumulated flags
6153
 * @ptype:  the parent type
6154
 *
6155
 * Check for rules in section 7.1 and 7.2
6156
 *
6157
 * Returns the content type of @cur
6158
 */
6159
static xmlRelaxNGContentType
6160
xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6161
                     xmlRelaxNGDefinePtr cur, int flags,
6162
                     xmlRelaxNGType ptype)
6163
0
{
6164
0
    int nflags;
6165
0
    xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
6166
6167
0
    while (cur != NULL) {
6168
0
        ret = XML_RELAXNG_CONTENT_EMPTY;
6169
0
        if ((cur->type == XML_RELAXNG_REF) ||
6170
0
            (cur->type == XML_RELAXNG_PARENTREF)) {
6171
           /*
6172
            * This should actually be caught by list//element(ref) at the
6173
            * element boundaries, c.f. Bug #159968 local refs are dropped
6174
            * in step 4.19.
6175
            */
6176
#if 0
6177
            if (flags & XML_RELAXNG_IN_LIST) {
6178
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6179
                           "Found forbidden pattern list//ref\n", NULL,
6180
                           NULL);
6181
            }
6182
#endif
6183
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6184
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6185
0
                           "Found forbidden pattern data/except//ref\n",
6186
0
                           NULL, NULL);
6187
0
            }
6188
0
            if (cur->content == NULL) {
6189
0
                if (cur->type == XML_RELAXNG_PARENTREF)
6190
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6191
0
                               "Internal found no define for parent refs\n",
6192
0
                               NULL, NULL);
6193
0
                else
6194
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6195
0
                               "Internal found no define for ref %s\n",
6196
0
                               (cur->name ? cur->name: BAD_CAST "null"), NULL);
6197
0
            }
6198
0
            if (cur->depth > -4) {
6199
0
                cur->depth = -4;
6200
0
                ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6201
0
                                           flags, cur->type);
6202
0
                cur->depth = ret - 15;
6203
0
            } else if (cur->depth == -4) {
6204
0
                ret = XML_RELAXNG_CONTENT_COMPLEX;
6205
0
            } else {
6206
0
                ret = (xmlRelaxNGContentType) (cur->depth + 15);
6207
0
            }
6208
0
        } else if (cur->type == XML_RELAXNG_ELEMENT) {
6209
            /*
6210
             * The 7.3 Attribute derivation rule for groups is plugged there
6211
             */
6212
0
            xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6213
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6214
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6215
0
                           "Found forbidden pattern data/except//element(ref)\n",
6216
0
                           NULL, NULL);
6217
0
            }
6218
0
            if (flags & XML_RELAXNG_IN_LIST) {
6219
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6220
0
                           "Found forbidden pattern list//element(ref)\n",
6221
0
                           NULL, NULL);
6222
0
            }
6223
0
            if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6224
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6225
0
                           "Found forbidden pattern attribute//element(ref)\n",
6226
0
                           NULL, NULL);
6227
0
            }
6228
0
            if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6229
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6230
0
                           "Found forbidden pattern attribute//element(ref)\n",
6231
0
                           NULL, NULL);
6232
0
            }
6233
            /*
6234
             * reset since in the simple form elements are only child
6235
             * of grammar/define
6236
             */
6237
0
            nflags = 0;
6238
0
            ret =
6239
0
                xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6240
0
            if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6241
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6242
0
                           "Element %s attributes have a content type error\n",
6243
0
                           cur->name, NULL);
6244
0
            }
6245
0
            ret =
6246
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6247
0
                                     cur->type);
6248
0
            if (ret == XML_RELAXNG_CONTENT_ERROR) {
6249
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6250
0
                           "Element %s has a content type error\n",
6251
0
                           cur->name, NULL);
6252
0
            } else {
6253
0
                ret = XML_RELAXNG_CONTENT_COMPLEX;
6254
0
            }
6255
0
        } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6256
0
            if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6257
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6258
0
                           "Found forbidden pattern attribute//attribute\n",
6259
0
                           NULL, NULL);
6260
0
            }
6261
0
            if (flags & XML_RELAXNG_IN_LIST) {
6262
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6263
0
                           "Found forbidden pattern list//attribute\n",
6264
0
                           NULL, NULL);
6265
0
            }
6266
0
            if (flags & XML_RELAXNG_IN_OOMGROUP) {
6267
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6268
0
                           "Found forbidden pattern oneOrMore//group//attribute\n",
6269
0
                           NULL, NULL);
6270
0
            }
6271
0
            if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6272
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6273
0
                           "Found forbidden pattern oneOrMore//interleave//attribute\n",
6274
0
                           NULL, NULL);
6275
0
            }
6276
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6277
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6278
0
                           "Found forbidden pattern data/except//attribute\n",
6279
0
                           NULL, NULL);
6280
0
            }
6281
0
            if (flags & XML_RELAXNG_IN_START) {
6282
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6283
0
                           "Found forbidden pattern start//attribute\n",
6284
0
                           NULL, NULL);
6285
0
            }
6286
0
            if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6287
0
                && cur->name == NULL
6288
                /* following is checking alternative name class readiness
6289
                   in case it went the "choice" route */
6290
0
                && cur->nameClass == NULL) {
6291
0
                if (cur->ns == NULL) {
6292
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6293
0
                               "Found anyName attribute without oneOrMore ancestor\n",
6294
0
                               NULL, NULL);
6295
0
                } else {
6296
0
                    xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6297
0
                               "Found nsName attribute without oneOrMore ancestor\n",
6298
0
                               NULL, NULL);
6299
0
                }
6300
0
            }
6301
0
            nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6302
0
            xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6303
0
            ret = XML_RELAXNG_CONTENT_EMPTY;
6304
0
        } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6305
0
                   (cur->type == XML_RELAXNG_ZEROORMORE)) {
6306
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6307
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6308
0
                           "Found forbidden pattern data/except//oneOrMore\n",
6309
0
                           NULL, NULL);
6310
0
            }
6311
0
            if (flags & XML_RELAXNG_IN_START) {
6312
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6313
0
                           "Found forbidden pattern start//oneOrMore\n",
6314
0
                           NULL, NULL);
6315
0
            }
6316
0
            nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6317
0
            ret =
6318
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6319
0
                                     cur->type);
6320
0
            ret = xmlRelaxNGGroupContentType(ret, ret);
6321
0
        } else if (cur->type == XML_RELAXNG_LIST) {
6322
0
            if (flags & XML_RELAXNG_IN_LIST) {
6323
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6324
0
                           "Found forbidden pattern list//list\n", NULL,
6325
0
                           NULL);
6326
0
            }
6327
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6328
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6329
0
                           "Found forbidden pattern data/except//list\n",
6330
0
                           NULL, NULL);
6331
0
            }
6332
0
            if (flags & XML_RELAXNG_IN_START) {
6333
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6334
0
                           "Found forbidden pattern start//list\n", NULL,
6335
0
                           NULL);
6336
0
            }
6337
0
            nflags = flags | XML_RELAXNG_IN_LIST;
6338
0
            ret =
6339
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6340
0
                                     cur->type);
6341
0
        } else if (cur->type == XML_RELAXNG_GROUP) {
6342
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6343
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6344
0
                           "Found forbidden pattern data/except//group\n",
6345
0
                           NULL, NULL);
6346
0
            }
6347
0
            if (flags & XML_RELAXNG_IN_START) {
6348
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6349
0
                           "Found forbidden pattern start//group\n", NULL,
6350
0
                           NULL);
6351
0
            }
6352
0
            if (flags & XML_RELAXNG_IN_ONEORMORE)
6353
0
                nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6354
0
            else
6355
0
                nflags = flags;
6356
0
            ret =
6357
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6358
0
                                     cur->type);
6359
            /*
6360
             * The 7.3 Attribute derivation rule for groups is plugged there
6361
             */
6362
0
            xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6363
0
        } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6364
0
            if (flags & XML_RELAXNG_IN_LIST) {
6365
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6366
0
                           "Found forbidden pattern list//interleave\n",
6367
0
                           NULL, NULL);
6368
0
            }
6369
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6370
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6371
0
                           "Found forbidden pattern data/except//interleave\n",
6372
0
                           NULL, NULL);
6373
0
            }
6374
0
            if (flags & XML_RELAXNG_IN_START) {
6375
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6376
0
                           "Found forbidden pattern start//interleave\n",
6377
0
                           NULL, NULL);
6378
0
            }
6379
0
            if (flags & XML_RELAXNG_IN_ONEORMORE)
6380
0
                nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6381
0
            else
6382
0
                nflags = flags;
6383
0
            ret =
6384
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6385
0
                                     cur->type);
6386
0
        } else if (cur->type == XML_RELAXNG_EXCEPT) {
6387
0
            if ((cur->parent != NULL) &&
6388
0
                (cur->parent->type == XML_RELAXNG_DATATYPE))
6389
0
                nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6390
0
            else
6391
0
                nflags = flags;
6392
0
            ret =
6393
0
                xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6394
0
                                     cur->type);
6395
0
        } else if (cur->type == XML_RELAXNG_DATATYPE) {
6396
0
            if (flags & XML_RELAXNG_IN_START) {
6397
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6398
0
                           "Found forbidden pattern start//data\n", NULL,
6399
0
                           NULL);
6400
0
            }
6401
0
            xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6402
0
            ret = XML_RELAXNG_CONTENT_SIMPLE;
6403
0
        } else if (cur->type == XML_RELAXNG_VALUE) {
6404
0
            if (flags & XML_RELAXNG_IN_START) {
6405
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6406
0
                           "Found forbidden pattern start//value\n", NULL,
6407
0
                           NULL);
6408
0
            }
6409
0
            xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6410
0
            ret = XML_RELAXNG_CONTENT_SIMPLE;
6411
0
        } else if (cur->type == XML_RELAXNG_TEXT) {
6412
0
            if (flags & XML_RELAXNG_IN_LIST) {
6413
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6414
0
                           "Found forbidden pattern list//text\n", NULL,
6415
0
                           NULL);
6416
0
            }
6417
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6418
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6419
0
                           "Found forbidden pattern data/except//text\n",
6420
0
                           NULL, NULL);
6421
0
            }
6422
0
            if (flags & XML_RELAXNG_IN_START) {
6423
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6424
0
                           "Found forbidden pattern start//text\n", NULL,
6425
0
                           NULL);
6426
0
            }
6427
0
            ret = XML_RELAXNG_CONTENT_COMPLEX;
6428
0
        } else if (cur->type == XML_RELAXNG_EMPTY) {
6429
0
            if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6430
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6431
0
                           "Found forbidden pattern data/except//empty\n",
6432
0
                           NULL, NULL);
6433
0
            }
6434
0
            if (flags & XML_RELAXNG_IN_START) {
6435
0
                xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6436
0
                           "Found forbidden pattern start//empty\n", NULL,
6437
0
                           NULL);
6438
0
            }
6439
0
            ret = XML_RELAXNG_CONTENT_EMPTY;
6440
0
        } else if (cur->type == XML_RELAXNG_CHOICE) {
6441
0
            xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6442
0
            ret =
6443
0
                xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6444
0
        } else {
6445
0
            ret =
6446
0
                xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6447
0
        }
6448
0
        cur = cur->next;
6449
0
        if (ptype == XML_RELAXNG_GROUP) {
6450
0
            val = xmlRelaxNGGroupContentType(val, ret);
6451
0
        } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6452
            /*
6453
             * TODO: scan complain that tmp is never used, seems on purpose
6454
             *       need double-checking
6455
             */
6456
0
            tmp = xmlRelaxNGGroupContentType(val, ret);
6457
0
            if (tmp != XML_RELAXNG_CONTENT_ERROR)
6458
0
                tmp = xmlRelaxNGMaxContentType(val, ret);
6459
0
        } else if (ptype == XML_RELAXNG_CHOICE) {
6460
0
            val = xmlRelaxNGMaxContentType(val, ret);
6461
0
        } else if (ptype == XML_RELAXNG_LIST) {
6462
0
            val = XML_RELAXNG_CONTENT_SIMPLE;
6463
0
        } else if (ptype == XML_RELAXNG_EXCEPT) {
6464
0
            if (ret == XML_RELAXNG_CONTENT_ERROR)
6465
0
                val = XML_RELAXNG_CONTENT_ERROR;
6466
0
            else
6467
0
                val = XML_RELAXNG_CONTENT_SIMPLE;
6468
0
        } else {
6469
0
            val = xmlRelaxNGGroupContentType(val, ret);
6470
0
        }
6471
6472
0
    }
6473
0
    return (val);
6474
0
}
6475
6476
/**
6477
 * xmlRelaxNGParseGrammar:
6478
 * @ctxt:  a Relax-NG parser context
6479
 * @nodes:  grammar children nodes
6480
 *
6481
 * parse a Relax-NG <grammar> node
6482
 *
6483
 * Returns the internal xmlRelaxNGGrammarPtr built or
6484
 *         NULL in case of error
6485
 */
6486
static xmlRelaxNGGrammarPtr
6487
xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6488
0
{
6489
0
    xmlRelaxNGGrammarPtr ret, tmp, old;
6490
6491
0
    ret = xmlRelaxNGNewGrammar(ctxt);
6492
0
    if (ret == NULL)
6493
0
        return (NULL);
6494
6495
    /*
6496
     * Link the new grammar in the tree
6497
     */
6498
0
    ret->parent = ctxt->grammar;
6499
0
    if (ctxt->grammar != NULL) {
6500
0
        tmp = ctxt->grammar->children;
6501
0
        if (tmp == NULL) {
6502
0
            ctxt->grammar->children = ret;
6503
0
        } else {
6504
0
            while (tmp->next != NULL)
6505
0
                tmp = tmp->next;
6506
0
            tmp->next = ret;
6507
0
        }
6508
0
    }
6509
6510
0
    old = ctxt->grammar;
6511
0
    ctxt->grammar = ret;
6512
0
    xmlRelaxNGParseGrammarContent(ctxt, nodes);
6513
0
    ctxt->grammar = ret;
6514
0
    if (ctxt->grammar == NULL) {
6515
0
        xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6516
0
                   "Failed to parse <grammar> content\n", NULL, NULL);
6517
0
    } else if (ctxt->grammar->start == NULL) {
6518
0
        xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6519
0
                   "Element <grammar> has no <start>\n", NULL, NULL);
6520
0
    }
6521
6522
    /*
6523
     * Apply 4.17 merging rules to defines and starts
6524
     */
6525
0
    xmlRelaxNGCombineStart(ctxt, ret);
6526
0
    if (ret->defs != NULL) {
6527
0
        xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt);
6528
0
    }
6529
6530
    /*
6531
     * link together defines and refs in this grammar
6532
     */
6533
0
    if (ret->refs != NULL) {
6534
0
        xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt);
6535
0
    }
6536
6537
6538
    /* @@@@ */
6539
6540
0
    ctxt->grammar = old;
6541
0
    return (ret);
6542
0
}
6543
6544
/**
6545
 * xmlRelaxNGParseDocument:
6546
 * @ctxt:  a Relax-NG parser context
6547
 * @node:  the root node of the RelaxNG schema
6548
 *
6549
 * parse a Relax-NG definition resource and build an internal
6550
 * xmlRelaxNG structure which can be used to validate instances.
6551
 *
6552
 * Returns the internal XML RelaxNG structure built or
6553
 *         NULL in case of error
6554
 */
6555
static xmlRelaxNGPtr
6556
xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6557
0
{
6558
0
    xmlRelaxNGPtr schema = NULL;
6559
0
    const xmlChar *olddefine;
6560
0
    xmlRelaxNGGrammarPtr old;
6561
6562
0
    if ((ctxt == NULL) || (node == NULL))
6563
0
        return (NULL);
6564
6565
0
    schema = xmlRelaxNGNewRelaxNG(ctxt);
6566
0
    if (schema == NULL)
6567
0
        return (NULL);
6568
6569
0
    olddefine = ctxt->define;
6570
0
    ctxt->define = NULL;
6571
0
    if (IS_RELAXNG(node, "grammar")) {
6572
0
        schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6573
0
        if (schema->topgrammar == NULL) {
6574
0
            xmlRelaxNGFree(schema);
6575
0
            return (NULL);
6576
0
        }
6577
0
    } else {
6578
0
        xmlRelaxNGGrammarPtr tmp, ret;
6579
6580
0
        schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6581
0
        if (schema->topgrammar == NULL) {
6582
0
            xmlRelaxNGFree(schema);
6583
0
            return (NULL);
6584
0
        }
6585
        /*
6586
         * Link the new grammar in the tree
6587
         */
6588
0
        ret->parent = ctxt->grammar;
6589
0
        if (ctxt->grammar != NULL) {
6590
0
            tmp = ctxt->grammar->children;
6591
0
            if (tmp == NULL) {
6592
0
                ctxt->grammar->children = ret;
6593
0
            } else {
6594
0
                while (tmp->next != NULL)
6595
0
                    tmp = tmp->next;
6596
0
                tmp->next = ret;
6597
0
            }
6598
0
        }
6599
0
        old = ctxt->grammar;
6600
0
        ctxt->grammar = ret;
6601
0
        xmlRelaxNGParseStart(ctxt, node);
6602
0
        if (old != NULL)
6603
0
            ctxt->grammar = old;
6604
0
    }
6605
0
    ctxt->define = olddefine;
6606
0
    if (schema->topgrammar->start != NULL) {
6607
0
        xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6608
0
        if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6609
0
            xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6610
0
            while ((schema->topgrammar->start != NULL) &&
6611
0
                   (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6612
0
                   (schema->topgrammar->start->next != NULL))
6613
0
                schema->topgrammar->start =
6614
0
                    schema->topgrammar->start->content;
6615
0
            xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6616
0
                                 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6617
0
        }
6618
0
    }
6619
6620
0
    return (schema);
6621
0
}
6622
6623
/************************************************************************
6624
 *                  *
6625
 *      Reading RelaxNGs        *
6626
 *                  *
6627
 ************************************************************************/
6628
6629
/**
6630
 * xmlRelaxNGNewParserCtxt:
6631
 * @URL:  the location of the schema
6632
 *
6633
 * Create an XML RelaxNGs parse context for that file/resource expected
6634
 * to contain an XML RelaxNGs file.
6635
 *
6636
 * Returns the parser context or NULL in case of error
6637
 */
6638
xmlRelaxNGParserCtxtPtr
6639
xmlRelaxNGNewParserCtxt(const char *URL)
6640
0
{
6641
0
    xmlRelaxNGParserCtxtPtr ret;
6642
6643
0
    if (URL == NULL)
6644
0
        return (NULL);
6645
6646
0
    ret =
6647
0
        (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6648
0
    if (ret == NULL) {
6649
0
        xmlRngPErrMemory(NULL);
6650
0
        return (NULL);
6651
0
    }
6652
0
    memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6653
0
    ret->URL = xmlStrdup((const xmlChar *) URL);
6654
0
    return (ret);
6655
0
}
6656
6657
/**
6658
 * xmlRelaxNGNewMemParserCtxt:
6659
 * @buffer:  a pointer to a char array containing the schemas
6660
 * @size:  the size of the array
6661
 *
6662
 * Create an XML RelaxNGs parse context for that memory buffer expected
6663
 * to contain an XML RelaxNGs file.
6664
 *
6665
 * Returns the parser context or NULL in case of error
6666
 */
6667
xmlRelaxNGParserCtxtPtr
6668
xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6669
0
{
6670
0
    xmlRelaxNGParserCtxtPtr ret;
6671
6672
0
    if ((buffer == NULL) || (size <= 0))
6673
0
        return (NULL);
6674
6675
0
    ret =
6676
0
        (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6677
0
    if (ret == NULL) {
6678
0
        xmlRngPErrMemory(NULL);
6679
0
        return (NULL);
6680
0
    }
6681
0
    memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6682
0
    ret->buffer = buffer;
6683
0
    ret->size = size;
6684
0
    return (ret);
6685
0
}
6686
6687
/**
6688
 * xmlRelaxNGNewDocParserCtxt:
6689
 * @doc:  a preparsed document tree
6690
 *
6691
 * Create an XML RelaxNGs parser context for that document.
6692
 * Note: since the process of compiling a RelaxNG schemas modifies the
6693
 *       document, the @doc parameter is duplicated internally.
6694
 *
6695
 * Returns the parser context or NULL in case of error
6696
 */
6697
xmlRelaxNGParserCtxtPtr
6698
xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6699
0
{
6700
0
    xmlRelaxNGParserCtxtPtr ret;
6701
0
    xmlDocPtr copy;
6702
6703
0
    if (doc == NULL)
6704
0
        return (NULL);
6705
0
    copy = xmlCopyDoc(doc, 1);
6706
0
    if (copy == NULL)
6707
0
        return (NULL);
6708
6709
0
    ret =
6710
0
        (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6711
0
    if (ret == NULL) {
6712
0
        xmlRngPErrMemory(NULL);
6713
0
        xmlFreeDoc(copy);
6714
0
        return (NULL);
6715
0
    }
6716
0
    memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6717
0
    ret->document = copy;
6718
0
    ret->freedoc = 1;
6719
0
    ret->userData = xmlGenericErrorContext;
6720
0
    return (ret);
6721
0
}
6722
6723
/**
6724
 * xmlRelaxNGFreeParserCtxt:
6725
 * @ctxt:  the schema parser context
6726
 *
6727
 * Free the resources associated to the schema parser context
6728
 */
6729
void
6730
xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6731
0
{
6732
0
    if (ctxt == NULL)
6733
0
        return;
6734
0
    if (ctxt->URL != NULL)
6735
0
        xmlFree(ctxt->URL);
6736
0
    if (ctxt->doc != NULL)
6737
0
        xmlRelaxNGFreeDocument(ctxt->doc);
6738
0
    if (ctxt->interleaves != NULL)
6739
0
        xmlHashFree(ctxt->interleaves, NULL);
6740
0
    if (ctxt->documents != NULL)
6741
0
        xmlRelaxNGFreeDocumentList(ctxt->documents);
6742
0
    if (ctxt->includes != NULL)
6743
0
        xmlRelaxNGFreeIncludeList(ctxt->includes);
6744
0
    if (ctxt->docTab != NULL)
6745
0
        xmlFree(ctxt->docTab);
6746
0
    if (ctxt->incTab != NULL)
6747
0
        xmlFree(ctxt->incTab);
6748
0
    if (ctxt->defTab != NULL) {
6749
0
        int i;
6750
6751
0
        for (i = 0; i < ctxt->defNr; i++)
6752
0
            xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6753
0
        xmlFree(ctxt->defTab);
6754
0
    }
6755
0
    if ((ctxt->document != NULL) && (ctxt->freedoc))
6756
0
        xmlFreeDoc(ctxt->document);
6757
0
    xmlFree(ctxt);
6758
0
}
6759
6760
/**
6761
 * xmlRelaxNGNormExtSpace:
6762
 * @value:  a value
6763
 *
6764
 * Removes the leading and ending spaces of the value
6765
 * The string is modified "in situ"
6766
 */
6767
static void
6768
xmlRelaxNGNormExtSpace(xmlChar * value)
6769
0
{
6770
0
    xmlChar *start = value;
6771
0
    xmlChar *cur = value;
6772
6773
0
    if (value == NULL)
6774
0
        return;
6775
6776
0
    while (IS_BLANK_CH(*cur))
6777
0
        cur++;
6778
0
    if (cur == start) {
6779
0
        do {
6780
0
            while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6781
0
                cur++;
6782
0
            if (*cur == 0)
6783
0
                return;
6784
0
            start = cur;
6785
0
            while (IS_BLANK_CH(*cur))
6786
0
                cur++;
6787
0
            if (*cur == 0) {
6788
0
                *start = 0;
6789
0
                return;
6790
0
            }
6791
0
        } while (1);
6792
0
    } else {
6793
0
        do {
6794
0
            while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6795
0
                *start++ = *cur++;
6796
0
            if (*cur == 0) {
6797
0
                *start = 0;
6798
0
                return;
6799
0
            }
6800
            /* don't try to normalize the inner spaces */
6801
0
            while (IS_BLANK_CH(*cur))
6802
0
                cur++;
6803
0
            if (*cur == 0) {
6804
0
                *start = 0;
6805
0
                return;
6806
0
            }
6807
0
            *start++ = *cur++;
6808
0
        } while (1);
6809
0
    }
6810
0
}
6811
6812
/**
6813
 * xmlRelaxNGCleanupAttributes:
6814
 * @ctxt:  a Relax-NG parser context
6815
 * @node:  a Relax-NG node
6816
 *
6817
 * Check all the attributes on the given node
6818
 */
6819
static void
6820
xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6821
0
{
6822
0
    xmlAttrPtr cur, next;
6823
6824
0
    cur = node->properties;
6825
0
    while (cur != NULL) {
6826
0
        next = cur->next;
6827
0
        if ((cur->ns == NULL) ||
6828
0
            (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6829
0
            if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6830
0
                if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6831
0
                    (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6832
0
                    (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6833
0
                    (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6834
0
                    (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6835
0
                    (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6836
0
                    xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6837
0
                               "Attribute %s is not allowed on %s\n",
6838
0
                               cur->name, node->name);
6839
0
                }
6840
0
            } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6841
0
                if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6842
0
                    (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6843
0
                    xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6844
0
                               "Attribute %s is not allowed on %s\n",
6845
0
                               cur->name, node->name);
6846
0
                }
6847
0
            } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6848
0
                if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6849
0
                    (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6850
0
                    xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6851
0
                               "Attribute %s is not allowed on %s\n",
6852
0
                               cur->name, node->name);
6853
0
                }
6854
0
            } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6855
0
                if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6856
0
                    (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6857
0
                    xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6858
0
                               "Attribute %s is not allowed on %s\n",
6859
0
                               cur->name, node->name);
6860
0
                }
6861
0
            } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6862
0
                xmlChar *val;
6863
0
                xmlURIPtr uri;
6864
6865
0
                val = xmlNodeListGetString(node->doc, cur->children, 1);
6866
0
                if (val != NULL) {
6867
0
                    if (val[0] != 0) {
6868
0
                        uri = xmlParseURI((const char *) val);
6869
0
                        if (uri == NULL) {
6870
0
                            xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6871
0
                                       "Attribute %s contains invalid URI %s\n",
6872
0
                                       cur->name, val);
6873
0
                        } else {
6874
0
                            if (uri->scheme == NULL) {
6875
0
                                xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6876
0
                                           "Attribute %s URI %s is not absolute\n",
6877
0
                                           cur->name, val);
6878
0
                            }
6879
0
                            if (uri->fragment != NULL) {
6880
0
                                xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6881
0
                                           "Attribute %s URI %s has a fragment ID\n",
6882
0
                                           cur->name, val);
6883
0
                            }
6884
0
                            xmlFreeURI(uri);
6885
0
                        }
6886
0
                    }
6887
0
                    xmlFree(val);
6888
0
                }
6889
0
            } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6890
0
                xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6891
0
                           "Unknown attribute %s on %s\n", cur->name,
6892
0
                           node->name);
6893
0
            }
6894
0
        }
6895
0
        cur = next;
6896
0
    }
6897
0
}
6898
6899
/**
6900
 * xmlRelaxNGCleanupTree:
6901
 * @ctxt:  a Relax-NG parser context
6902
 * @root:  an xmlNodePtr subtree
6903
 *
6904
 * Cleanup the subtree from unwanted nodes for parsing, resolve
6905
 * Include and externalRef lookups.
6906
 */
6907
static void
6908
xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6909
0
{
6910
0
    xmlNodePtr cur, delete;
6911
6912
0
    delete = NULL;
6913
0
    cur = root;
6914
0
    while (cur != NULL) {
6915
0
        if (delete != NULL) {
6916
0
            xmlUnlinkNode(delete);
6917
0
            xmlFreeNode(delete);
6918
0
            delete = NULL;
6919
0
        }
6920
0
        if (cur->type == XML_ELEMENT_NODE) {
6921
            /*
6922
             * Simplification 4.1. Annotations
6923
             */
6924
0
            if ((cur->ns == NULL) ||
6925
0
                (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6926
0
                if ((cur->parent != NULL) &&
6927
0
                    (cur->parent->type == XML_ELEMENT_NODE) &&
6928
0
                    ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6929
0
                     (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6930
0
                     (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6931
0
                    xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6932
0
                               "element %s doesn't allow foreign elements\n",
6933
0
                               cur->parent->name, NULL);
6934
0
                }
6935
0
                delete = cur;
6936
0
                goto skip_children;
6937
0
            } else {
6938
0
                xmlRelaxNGCleanupAttributes(ctxt, cur);
6939
0
                if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6940
0
                    xmlChar *href, *ns, *base, *URL;
6941
0
                    xmlRelaxNGDocumentPtr docu;
6942
0
                    xmlNodePtr tmp;
6943
0
        xmlURIPtr uri;
6944
6945
0
                    ns = xmlGetProp(cur, BAD_CAST "ns");
6946
0
                    if (ns == NULL) {
6947
0
                        tmp = cur->parent;
6948
0
                        while ((tmp != NULL) &&
6949
0
                               (tmp->type == XML_ELEMENT_NODE)) {
6950
0
                            ns = xmlGetProp(tmp, BAD_CAST "ns");
6951
0
                            if (ns != NULL)
6952
0
                                break;
6953
0
                            tmp = tmp->parent;
6954
0
                        }
6955
0
                    }
6956
0
                    href = xmlGetProp(cur, BAD_CAST "href");
6957
0
                    if (href == NULL) {
6958
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6959
0
                                   "xmlRelaxNGParse: externalRef has no href attribute\n",
6960
0
                                   NULL, NULL);
6961
0
                        if (ns != NULL)
6962
0
                            xmlFree(ns);
6963
0
                        delete = cur;
6964
0
                        goto skip_children;
6965
0
                    }
6966
0
        uri = xmlParseURI((const char *) href);
6967
0
        if (uri == NULL) {
6968
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6969
0
                                   "Incorrect URI for externalRef %s\n",
6970
0
                                   href, NULL);
6971
0
                        if (ns != NULL)
6972
0
                            xmlFree(ns);
6973
0
                        if (href != NULL)
6974
0
                            xmlFree(href);
6975
0
                        delete = cur;
6976
0
                        goto skip_children;
6977
0
        }
6978
0
        if (uri->fragment != NULL) {
6979
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6980
0
             "Fragment forbidden in URI for externalRef %s\n",
6981
0
                                   href, NULL);
6982
0
                        if (ns != NULL)
6983
0
                            xmlFree(ns);
6984
0
            xmlFreeURI(uri);
6985
0
                        if (href != NULL)
6986
0
                            xmlFree(href);
6987
0
                        delete = cur;
6988
0
                        goto skip_children;
6989
0
        }
6990
0
        xmlFreeURI(uri);
6991
0
                    base = xmlNodeGetBase(cur->doc, cur);
6992
0
                    URL = xmlBuildURI(href, base);
6993
0
                    if (URL == NULL) {
6994
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6995
0
                                   "Failed to compute URL for externalRef %s\n",
6996
0
                                   href, NULL);
6997
0
                        if (ns != NULL)
6998
0
                            xmlFree(ns);
6999
0
                        if (href != NULL)
7000
0
                            xmlFree(href);
7001
0
                        if (base != NULL)
7002
0
                            xmlFree(base);
7003
0
                        delete = cur;
7004
0
                        goto skip_children;
7005
0
                    }
7006
0
                    if (href != NULL)
7007
0
                        xmlFree(href);
7008
0
                    if (base != NULL)
7009
0
                        xmlFree(base);
7010
0
                    docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7011
0
                    if (docu == NULL) {
7012
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7013
0
                                   "Failed to load externalRef %s\n", URL,
7014
0
                                   NULL);
7015
0
                        if (ns != NULL)
7016
0
                            xmlFree(ns);
7017
0
                        xmlFree(URL);
7018
0
                        delete = cur;
7019
0
                        goto skip_children;
7020
0
                    }
7021
0
                    if (ns != NULL)
7022
0
                        xmlFree(ns);
7023
0
                    xmlFree(URL);
7024
0
                    cur->psvi = docu;
7025
0
                } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7026
0
                    xmlChar *href, *ns, *base, *URL;
7027
0
                    xmlRelaxNGIncludePtr incl;
7028
0
                    xmlNodePtr tmp;
7029
7030
0
                    href = xmlGetProp(cur, BAD_CAST "href");
7031
0
                    if (href == NULL) {
7032
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7033
0
                                   "xmlRelaxNGParse: include has no href attribute\n",
7034
0
                                   NULL, NULL);
7035
0
                        delete = cur;
7036
0
                        goto skip_children;
7037
0
                    }
7038
0
                    base = xmlNodeGetBase(cur->doc, cur);
7039
0
                    URL = xmlBuildURI(href, base);
7040
0
                    if (URL == NULL) {
7041
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7042
0
                                   "Failed to compute URL for include %s\n",
7043
0
                                   href, NULL);
7044
0
                        if (href != NULL)
7045
0
                            xmlFree(href);
7046
0
                        if (base != NULL)
7047
0
                            xmlFree(base);
7048
0
                        delete = cur;
7049
0
                        goto skip_children;
7050
0
                    }
7051
0
                    if (href != NULL)
7052
0
                        xmlFree(href);
7053
0
                    if (base != NULL)
7054
0
                        xmlFree(base);
7055
0
                    ns = xmlGetProp(cur, BAD_CAST "ns");
7056
0
                    if (ns == NULL) {
7057
0
                        tmp = cur->parent;
7058
0
                        while ((tmp != NULL) &&
7059
0
                               (tmp->type == XML_ELEMENT_NODE)) {
7060
0
                            ns = xmlGetProp(tmp, BAD_CAST "ns");
7061
0
                            if (ns != NULL)
7062
0
                                break;
7063
0
                            tmp = tmp->parent;
7064
0
                        }
7065
0
                    }
7066
0
                    incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7067
0
                    if (ns != NULL)
7068
0
                        xmlFree(ns);
7069
0
                    if (incl == NULL) {
7070
0
                        xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7071
0
                                   "Failed to load include %s\n", URL,
7072
0
                                   NULL);
7073
0
                        xmlFree(URL);
7074
0
                        delete = cur;
7075
0
                        goto skip_children;
7076
0
                    }
7077
0
                    xmlFree(URL);
7078
0
                    cur->psvi = incl;
7079
0
                } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7080
0
                           (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7081
0
                {
7082
0
                    xmlChar *name, *ns;
7083
0
                    xmlNodePtr text = NULL;
7084
7085
                    /*
7086
                     * Simplification 4.8. name attribute of element
7087
                     * and attribute elements
7088
                     */
7089
0
                    name = xmlGetProp(cur, BAD_CAST "name");
7090
0
                    if (name != NULL) {
7091
0
                        if (cur->children == NULL) {
7092
0
                            text =
7093
0
                                xmlNewChild(cur, cur->ns, BAD_CAST "name",
7094
0
                                            name);
7095
0
                        } else {
7096
0
                            xmlNodePtr node;
7097
7098
0
                            node = xmlNewDocNode(cur->doc, cur->ns,
7099
0
                               BAD_CAST "name", NULL);
7100
0
                            if (node != NULL) {
7101
0
                                xmlAddPrevSibling(cur->children, node);
7102
0
                                text = xmlNewDocText(node->doc, name);
7103
0
                                xmlAddChild(node, text);
7104
0
                                text = node;
7105
0
                            }
7106
0
                        }
7107
0
                        if (text == NULL) {
7108
0
                            xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7109
0
                                       "Failed to create a name %s element\n",
7110
0
                                       name, NULL);
7111
0
                        }
7112
0
                        xmlUnsetProp(cur, BAD_CAST "name");
7113
0
                        xmlFree(name);
7114
0
                        ns = xmlGetProp(cur, BAD_CAST "ns");
7115
0
                        if (ns != NULL) {
7116
0
                            if (text != NULL) {
7117
0
                                xmlSetProp(text, BAD_CAST "ns", ns);
7118
                                /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7119
0
                            }
7120
0
                            xmlFree(ns);
7121
0
                        } else if (xmlStrEqual(cur->name,
7122
0
                                               BAD_CAST "attribute")) {
7123
0
                            xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7124
0
                        }
7125
0
                    }
7126
0
                } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7127
0
                           (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7128
0
                           (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7129
                    /*
7130
                     * Simplification 4.8. name attribute of element
7131
                     * and attribute elements
7132
                     */
7133
0
                    if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7134
0
                        xmlNodePtr node;
7135
0
                        xmlChar *ns = NULL;
7136
7137
0
                        node = cur->parent;
7138
0
                        while ((node != NULL) &&
7139
0
                               (node->type == XML_ELEMENT_NODE)) {
7140
0
                            ns = xmlGetProp(node, BAD_CAST "ns");
7141
0
                            if (ns != NULL) {
7142
0
                                break;
7143
0
                            }
7144
0
                            node = node->parent;
7145
0
                        }
7146
0
                        if (ns == NULL) {
7147
0
                            xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7148
0
                        } else {
7149
0
                            xmlSetProp(cur, BAD_CAST "ns", ns);
7150
0
                            xmlFree(ns);
7151
0
                        }
7152
0
                    }
7153
0
                    if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7154
0
                        xmlChar *name, *local, *prefix;
7155
7156
                        /*
7157
                         * Simplification: 4.10. QNames
7158
                         */
7159
0
                        name = xmlNodeGetContent(cur);
7160
0
                        if (name != NULL) {
7161
0
                            local = xmlSplitQName2(name, &prefix);
7162
0
                            if (local != NULL) {
7163
0
                                xmlNsPtr ns;
7164
7165
0
                                ns = xmlSearchNs(cur->doc, cur, prefix);
7166
0
                                if (ns == NULL) {
7167
0
                                    xmlRngPErr(ctxt, cur,
7168
0
                                               XML_RNGP_PREFIX_UNDEFINED,
7169
0
                                               "xmlRelaxNGParse: no namespace for prefix %s\n",
7170
0
                                               prefix, NULL);
7171
0
                                } else {
7172
0
                                    xmlSetProp(cur, BAD_CAST "ns",
7173
0
                                               ns->href);
7174
0
                                    xmlNodeSetContent(cur, local);
7175
0
                                }
7176
0
                                xmlFree(local);
7177
0
                                xmlFree(prefix);
7178
0
                            }
7179
0
                            xmlFree(name);
7180
0
                        }
7181
0
                    }
7182
                    /*
7183
                     * 4.16
7184
                     */
7185
0
                    if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7186
0
                        if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7187
0
                            xmlRngPErr(ctxt, cur,
7188
0
                                       XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7189
0
                                       "Found nsName/except//nsName forbidden construct\n",
7190
0
                                       NULL, NULL);
7191
0
                        }
7192
0
                    }
7193
0
                } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7194
0
                           (cur != root)) {
7195
0
                    int oldflags = ctxt->flags;
7196
7197
                    /*
7198
                     * 4.16
7199
                     */
7200
0
                    if ((cur->parent != NULL) &&
7201
0
                        (xmlStrEqual
7202
0
                         (cur->parent->name, BAD_CAST "anyName"))) {
7203
0
                        ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7204
0
                        xmlRelaxNGCleanupTree(ctxt, cur);
7205
0
                        ctxt->flags = oldflags;
7206
0
                        goto skip_children;
7207
0
                    } else if ((cur->parent != NULL) &&
7208
0
                               (xmlStrEqual
7209
0
                                (cur->parent->name, BAD_CAST "nsName"))) {
7210
0
                        ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7211
0
                        xmlRelaxNGCleanupTree(ctxt, cur);
7212
0
                        ctxt->flags = oldflags;
7213
0
                        goto skip_children;
7214
0
                    }
7215
0
                } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7216
                    /*
7217
                     * 4.16
7218
                     */
7219
0
                    if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7220
0
                        xmlRngPErr(ctxt, cur,
7221
0
                                   XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7222
0
                                   "Found anyName/except//anyName forbidden construct\n",
7223
0
                                   NULL, NULL);
7224
0
                    } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7225
0
                        xmlRngPErr(ctxt, cur,
7226
0
                                   XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7227
0
                                   "Found nsName/except//anyName forbidden construct\n",
7228
0
                                   NULL, NULL);
7229
0
                    }
7230
0
                }
7231
                /*
7232
                 * This is not an else since "include" is transformed
7233
                 * into a div
7234
                 */
7235
0
                if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7236
0
                    xmlChar *ns;
7237
0
                    xmlNodePtr child, ins, tmp;
7238
7239
                    /*
7240
                     * implements rule 4.11
7241
                     */
7242
7243
0
                    ns = xmlGetProp(cur, BAD_CAST "ns");
7244
7245
0
                    child = cur->children;
7246
0
                    ins = cur;
7247
0
                    while (child != NULL) {
7248
0
                        if (ns != NULL) {
7249
0
                            if (!xmlHasProp(child, BAD_CAST "ns")) {
7250
0
                                xmlSetProp(child, BAD_CAST "ns", ns);
7251
0
                            }
7252
0
                        }
7253
0
                        tmp = child->next;
7254
0
                        xmlUnlinkNode(child);
7255
0
                        ins = xmlAddNextSibling(ins, child);
7256
0
                        child = tmp;
7257
0
                    }
7258
0
                    if (ns != NULL)
7259
0
                        xmlFree(ns);
7260
        /*
7261
         * Since we are about to delete cur, if its nsDef is non-NULL we
7262
         * need to preserve it (it contains the ns definitions for the
7263
         * children we just moved).  We'll just stick it on to the end
7264
         * of cur->parent's list, since it's never going to be re-serialized
7265
         * (bug 143738).
7266
         */
7267
0
        if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
7268
0
      xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7269
0
      while (parDef->next != NULL)
7270
0
          parDef = parDef->next;
7271
0
      parDef->next = cur->nsDef;
7272
0
      cur->nsDef = NULL;
7273
0
        }
7274
0
                    delete = cur;
7275
0
                    goto skip_children;
7276
0
                }
7277
0
            }
7278
0
        }
7279
        /*
7280
         * Simplification 4.2 whitespaces
7281
         */
7282
0
        else if ((cur->type == XML_TEXT_NODE) ||
7283
0
                 (cur->type == XML_CDATA_SECTION_NODE)) {
7284
0
            if (IS_BLANK_NODE(cur)) {
7285
0
                if ((cur->parent != NULL) &&
7286
0
        (cur->parent->type == XML_ELEMENT_NODE)) {
7287
0
                    if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7288
0
                        &&
7289
0
                        (!xmlStrEqual
7290
0
                         (cur->parent->name, BAD_CAST "param")))
7291
0
                        delete = cur;
7292
0
                } else {
7293
0
                    delete = cur;
7294
0
                    goto skip_children;
7295
0
                }
7296
0
            }
7297
0
        } else {
7298
0
            delete = cur;
7299
0
            goto skip_children;
7300
0
        }
7301
7302
        /*
7303
         * Skip to next node
7304
         */
7305
0
        if (cur->children != NULL) {
7306
0
            if ((cur->children->type != XML_ENTITY_DECL) &&
7307
0
                (cur->children->type != XML_ENTITY_REF_NODE) &&
7308
0
                (cur->children->type != XML_ENTITY_NODE)) {
7309
0
                cur = cur->children;
7310
0
                continue;
7311
0
            }
7312
0
        }
7313
0
      skip_children:
7314
0
        if (cur->next != NULL) {
7315
0
            cur = cur->next;
7316
0
            continue;
7317
0
        }
7318
7319
0
        do {
7320
0
            cur = cur->parent;
7321
0
            if (cur == NULL)
7322
0
                break;
7323
0
            if (cur == root) {
7324
0
                cur = NULL;
7325
0
                break;
7326
0
            }
7327
0
            if (cur->next != NULL) {
7328
0
                cur = cur->next;
7329
0
                break;
7330
0
            }
7331
0
        } while (cur != NULL);
7332
0
    }
7333
0
    if (delete != NULL) {
7334
0
        xmlUnlinkNode(delete);
7335
0
        xmlFreeNode(delete);
7336
0
        delete = NULL;
7337
0
    }
7338
0
}
7339
7340
/**
7341
 * xmlRelaxNGCleanupDoc:
7342
 * @ctxt:  a Relax-NG parser context
7343
 * @doc:  an xmldocPtr document pointer
7344
 *
7345
 * Cleanup the document from unwanted nodes for parsing, resolve
7346
 * Include and externalRef lookups.
7347
 *
7348
 * Returns the cleaned up document or NULL in case of error
7349
 */
7350
static xmlDocPtr
7351
xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7352
0
{
7353
0
    xmlNodePtr root;
7354
7355
    /*
7356
     * Extract the root
7357
     */
7358
0
    root = xmlDocGetRootElement(doc);
7359
0
    if (root == NULL) {
7360
0
        xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7361
0
                   ctxt->URL, NULL);
7362
0
        return (NULL);
7363
0
    }
7364
0
    xmlRelaxNGCleanupTree(ctxt, root);
7365
0
    return (doc);
7366
0
}
7367
7368
/**
7369
 * xmlRelaxNGParse:
7370
 * @ctxt:  a Relax-NG parser context
7371
 *
7372
 * parse a schema definition resource and build an internal
7373
 * XML Schema structure which can be used to validate instances.
7374
 *
7375
 * Returns the internal XML RelaxNG structure built from the resource or
7376
 *         NULL in case of error
7377
 */
7378
xmlRelaxNGPtr
7379
xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7380
0
{
7381
0
    xmlRelaxNGPtr ret = NULL;
7382
0
    xmlDocPtr doc;
7383
0
    xmlNodePtr root;
7384
7385
0
    xmlRelaxNGInitTypes();
7386
7387
0
    if (ctxt == NULL)
7388
0
        return (NULL);
7389
7390
    /*
7391
     * First step is to parse the input document into an DOM/Infoset
7392
     */
7393
0
    if (ctxt->URL != NULL) {
7394
0
        doc = xmlRelaxReadFile(ctxt, (const char *) ctxt->URL);
7395
0
        if (doc == NULL) {
7396
0
            xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7397
0
                       "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7398
0
                       NULL);
7399
0
            return (NULL);
7400
0
        }
7401
0
    } else if (ctxt->buffer != NULL) {
7402
0
        doc = xmlRelaxReadMemory(ctxt, ctxt->buffer, ctxt->size);
7403
0
        if (doc == NULL) {
7404
0
            xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7405
0
                       "xmlRelaxNGParse: could not parse schemas\n", NULL,
7406
0
                       NULL);
7407
0
            return (NULL);
7408
0
        }
7409
0
        doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7410
0
        ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7411
0
    } else if (ctxt->document != NULL) {
7412
0
        doc = ctxt->document;
7413
0
    } else {
7414
0
        xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7415
0
                   "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7416
0
        return (NULL);
7417
0
    }
7418
0
    ctxt->document = doc;
7419
7420
    /*
7421
     * Some preprocessing of the document content
7422
     */
7423
0
    doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7424
0
    if (doc == NULL) {
7425
0
        xmlFreeDoc(ctxt->document);
7426
0
        ctxt->document = NULL;
7427
0
        return (NULL);
7428
0
    }
7429
7430
    /*
7431
     * Then do the parsing for good
7432
     */
7433
0
    root = xmlDocGetRootElement(doc);
7434
0
    if (root == NULL) {
7435
0
        xmlRngPErr(ctxt, (xmlNodePtr) doc,
7436
0
             XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7437
0
                   (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
7438
7439
0
        xmlFreeDoc(ctxt->document);
7440
0
        ctxt->document = NULL;
7441
0
        return (NULL);
7442
0
    }
7443
0
    ret = xmlRelaxNGParseDocument(ctxt, root);
7444
0
    if (ret == NULL) {
7445
0
        xmlFreeDoc(ctxt->document);
7446
0
        ctxt->document = NULL;
7447
0
        return (NULL);
7448
0
    }
7449
7450
    /*
7451
     * Check the ref/defines links
7452
     */
7453
    /*
7454
     * try to preprocess interleaves
7455
     */
7456
0
    if (ctxt->interleaves != NULL) {
7457
0
        xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt);
7458
0
    }
7459
7460
    /*
7461
     * if there was a parsing error return NULL
7462
     */
7463
0
    if (ctxt->nbErrors > 0) {
7464
0
        xmlRelaxNGFree(ret);
7465
0
        ctxt->document = NULL;
7466
0
        xmlFreeDoc(doc);
7467
0
        return (NULL);
7468
0
    }
7469
7470
    /*
7471
     * try to compile (parts of) the schemas
7472
     */
7473
0
    if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7474
0
        if (ret->topgrammar->start->type != XML_RELAXNG_START) {
7475
0
            xmlRelaxNGDefinePtr def;
7476
7477
0
            def = xmlRelaxNGNewDefine(ctxt, NULL);
7478
0
            if (def != NULL) {
7479
0
                def->type = XML_RELAXNG_START;
7480
0
                def->content = ret->topgrammar->start;
7481
0
                ret->topgrammar->start = def;
7482
0
            }
7483
0
        }
7484
0
        xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
7485
0
    }
7486
7487
    /*
7488
     * Transfer the pointer for cleanup at the schema level.
7489
     */
7490
0
    ret->doc = doc;
7491
0
    ctxt->document = NULL;
7492
0
    ret->documents = ctxt->documents;
7493
0
    ctxt->documents = NULL;
7494
7495
0
    ret->includes = ctxt->includes;
7496
0
    ctxt->includes = NULL;
7497
0
    ret->defNr = ctxt->defNr;
7498
0
    ret->defTab = ctxt->defTab;
7499
0
    ctxt->defTab = NULL;
7500
0
    if (ctxt->idref == 1)
7501
0
        ret->idref = 1;
7502
7503
0
    return (ret);
7504
0
}
7505
7506
/**
7507
 * xmlRelaxNGSetParserErrors:
7508
 * @ctxt:  a Relax-NG validation context
7509
 * @err:  the error callback
7510
 * @warn:  the warning callback
7511
 * @ctx:  contextual data for the callbacks
7512
 *
7513
 * DEPRECATED: Use xmlRelaxNGSetParserStructuredErrors.
7514
 *
7515
 * Set the callback functions used to handle errors for a validation context
7516
 */
7517
void
7518
xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7519
                          xmlRelaxNGValidityErrorFunc err,
7520
                          xmlRelaxNGValidityWarningFunc warn, void *ctx)
7521
0
{
7522
0
    if (ctxt == NULL)
7523
0
        return;
7524
0
    ctxt->error = err;
7525
0
    ctxt->warning = warn;
7526
0
    ctxt->serror = NULL;
7527
0
    ctxt->userData = ctx;
7528
0
}
7529
7530
/**
7531
 * xmlRelaxNGGetParserErrors:
7532
 * @ctxt:  a Relax-NG validation context
7533
 * @err:  the error callback result
7534
 * @warn:  the warning callback result
7535
 * @ctx:  contextual data for the callbacks result
7536
 *
7537
 * Get the callback information used to handle errors for a validation context
7538
 *
7539
 * Returns -1 in case of failure, 0 otherwise.
7540
 */
7541
int
7542
xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7543
                          xmlRelaxNGValidityErrorFunc * err,
7544
                          xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7545
0
{
7546
0
    if (ctxt == NULL)
7547
0
        return (-1);
7548
0
    if (err != NULL)
7549
0
        *err = ctxt->error;
7550
0
    if (warn != NULL)
7551
0
        *warn = ctxt->warning;
7552
0
    if (ctx != NULL)
7553
0
        *ctx = ctxt->userData;
7554
0
    return (0);
7555
0
}
7556
7557
/**
7558
 * xmlRelaxNGSetParserStructuredErrors:
7559
 * @ctxt:  a Relax-NG parser context
7560
 * @serror:  the error callback
7561
 * @ctx:  contextual data for the callbacks
7562
 *
7563
 * Set the callback functions used to handle errors for a parsing context
7564
 */
7565
void
7566
xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
7567
            xmlStructuredErrorFunc serror,
7568
            void *ctx)
7569
0
{
7570
0
    if (ctxt == NULL)
7571
0
        return;
7572
0
    ctxt->serror = serror;
7573
0
    ctxt->error = NULL;
7574
0
    ctxt->warning = NULL;
7575
0
    ctxt->userData = ctx;
7576
0
}
7577
7578
/**
7579
 * xmlRelaxNGSetResourceLoader:
7580
 * @ctxt:  a Relax-NG parser context
7581
 * @loader:  the callback
7582
 * @vctxt:  contextual data for the callbacks
7583
 *
7584
 * Set the callback function used to load external resources.
7585
 */
7586
void
7587
xmlRelaxNGSetResourceLoader(xmlRelaxNGParserCtxtPtr ctxt,
7588
0
                            xmlResourceLoader loader, void *vctxt) {
7589
0
    if (ctxt == NULL)
7590
0
        return;
7591
0
    ctxt->resourceLoader = loader;
7592
0
    ctxt->resourceCtxt = vctxt;
7593
0
}
7594
7595
#ifdef LIBXML_OUTPUT_ENABLED
7596
7597
/************************************************************************
7598
 *                  *
7599
 *      Dump back a compiled form     *
7600
 *                  *
7601
 ************************************************************************/
7602
static void xmlRelaxNGDumpDefine(FILE * output,
7603
                                 xmlRelaxNGDefinePtr define);
7604
7605
/**
7606
 * xmlRelaxNGDumpDefines:
7607
 * @output:  the file output
7608
 * @defines:  a list of define structures
7609
 *
7610
 * Dump a RelaxNG structure back
7611
 */
7612
static void
7613
xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7614
0
{
7615
0
    while (defines != NULL) {
7616
0
        xmlRelaxNGDumpDefine(output, defines);
7617
0
        defines = defines->next;
7618
0
    }
7619
0
}
7620
7621
/**
7622
 * xmlRelaxNGDumpDefine:
7623
 * @output:  the file output
7624
 * @define:  a define structure
7625
 *
7626
 * Dump a RelaxNG structure back
7627
 */
7628
static void
7629
xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7630
0
{
7631
0
    if (define == NULL)
7632
0
        return;
7633
0
    switch (define->type) {
7634
0
        case XML_RELAXNG_EMPTY:
7635
0
            fprintf(output, "<empty/>\n");
7636
0
            break;
7637
0
        case XML_RELAXNG_NOT_ALLOWED:
7638
0
            fprintf(output, "<notAllowed/>\n");
7639
0
            break;
7640
0
        case XML_RELAXNG_TEXT:
7641
0
            fprintf(output, "<text/>\n");
7642
0
            break;
7643
0
        case XML_RELAXNG_ELEMENT:
7644
0
            fprintf(output, "<element>\n");
7645
0
            if (define->name != NULL) {
7646
0
                fprintf(output, "<name");
7647
0
                if (define->ns != NULL)
7648
0
                    fprintf(output, " ns=\"%s\"", define->ns);
7649
0
                fprintf(output, ">%s</name>\n", define->name);
7650
0
            }
7651
0
            xmlRelaxNGDumpDefines(output, define->attrs);
7652
0
            xmlRelaxNGDumpDefines(output, define->content);
7653
0
            fprintf(output, "</element>\n");
7654
0
            break;
7655
0
        case XML_RELAXNG_LIST:
7656
0
            fprintf(output, "<list>\n");
7657
0
            xmlRelaxNGDumpDefines(output, define->content);
7658
0
            fprintf(output, "</list>\n");
7659
0
            break;
7660
0
        case XML_RELAXNG_ONEORMORE:
7661
0
            fprintf(output, "<oneOrMore>\n");
7662
0
            xmlRelaxNGDumpDefines(output, define->content);
7663
0
            fprintf(output, "</oneOrMore>\n");
7664
0
            break;
7665
0
        case XML_RELAXNG_ZEROORMORE:
7666
0
            fprintf(output, "<zeroOrMore>\n");
7667
0
            xmlRelaxNGDumpDefines(output, define->content);
7668
0
            fprintf(output, "</zeroOrMore>\n");
7669
0
            break;
7670
0
        case XML_RELAXNG_CHOICE:
7671
0
            fprintf(output, "<choice>\n");
7672
0
            xmlRelaxNGDumpDefines(output, define->content);
7673
0
            fprintf(output, "</choice>\n");
7674
0
            break;
7675
0
        case XML_RELAXNG_GROUP:
7676
0
            fprintf(output, "<group>\n");
7677
0
            xmlRelaxNGDumpDefines(output, define->content);
7678
0
            fprintf(output, "</group>\n");
7679
0
            break;
7680
0
        case XML_RELAXNG_INTERLEAVE:
7681
0
            fprintf(output, "<interleave>\n");
7682
0
            xmlRelaxNGDumpDefines(output, define->content);
7683
0
            fprintf(output, "</interleave>\n");
7684
0
            break;
7685
0
        case XML_RELAXNG_OPTIONAL:
7686
0
            fprintf(output, "<optional>\n");
7687
0
            xmlRelaxNGDumpDefines(output, define->content);
7688
0
            fprintf(output, "</optional>\n");
7689
0
            break;
7690
0
        case XML_RELAXNG_ATTRIBUTE:
7691
0
            fprintf(output, "<attribute>\n");
7692
0
            xmlRelaxNGDumpDefines(output, define->content);
7693
0
            fprintf(output, "</attribute>\n");
7694
0
            break;
7695
0
        case XML_RELAXNG_DEF:
7696
0
            fprintf(output, "<define");
7697
0
            if (define->name != NULL)
7698
0
                fprintf(output, " name=\"%s\"", define->name);
7699
0
            fprintf(output, ">\n");
7700
0
            xmlRelaxNGDumpDefines(output, define->content);
7701
0
            fprintf(output, "</define>\n");
7702
0
            break;
7703
0
        case XML_RELAXNG_REF:
7704
0
            fprintf(output, "<ref");
7705
0
            if (define->name != NULL)
7706
0
                fprintf(output, " name=\"%s\"", define->name);
7707
0
            fprintf(output, ">\n");
7708
0
            xmlRelaxNGDumpDefines(output, define->content);
7709
0
            fprintf(output, "</ref>\n");
7710
0
            break;
7711
0
        case XML_RELAXNG_PARENTREF:
7712
0
            fprintf(output, "<parentRef");
7713
0
            if (define->name != NULL)
7714
0
                fprintf(output, " name=\"%s\"", define->name);
7715
0
            fprintf(output, ">\n");
7716
0
            xmlRelaxNGDumpDefines(output, define->content);
7717
0
            fprintf(output, "</parentRef>\n");
7718
0
            break;
7719
0
        case XML_RELAXNG_EXTERNALREF:
7720
0
            fprintf(output, "<externalRef>");
7721
0
            xmlRelaxNGDumpDefines(output, define->content);
7722
0
            fprintf(output, "</externalRef>\n");
7723
0
            break;
7724
0
        case XML_RELAXNG_DATATYPE:
7725
0
        case XML_RELAXNG_VALUE:
7726
            /* TODO */
7727
0
            break;
7728
0
        case XML_RELAXNG_START:
7729
0
        case XML_RELAXNG_EXCEPT:
7730
0
        case XML_RELAXNG_PARAM:
7731
            /* TODO */
7732
0
            break;
7733
0
        case XML_RELAXNG_NOOP:
7734
0
            xmlRelaxNGDumpDefines(output, define->content);
7735
0
            break;
7736
0
    }
7737
0
}
7738
7739
/**
7740
 * xmlRelaxNGDumpGrammar:
7741
 * @output:  the file output
7742
 * @grammar:  a grammar structure
7743
 * @top:  is this a top grammar
7744
 *
7745
 * Dump a RelaxNG structure back
7746
 */
7747
static void
7748
xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7749
0
{
7750
0
    if (grammar == NULL)
7751
0
        return;
7752
7753
0
    fprintf(output, "<grammar");
7754
0
    if (top)
7755
0
        fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7756
0
    switch (grammar->combine) {
7757
0
        case XML_RELAXNG_COMBINE_UNDEFINED:
7758
0
            break;
7759
0
        case XML_RELAXNG_COMBINE_CHOICE:
7760
0
            fprintf(output, " combine=\"choice\"");
7761
0
            break;
7762
0
        case XML_RELAXNG_COMBINE_INTERLEAVE:
7763
0
            fprintf(output, " combine=\"interleave\"");
7764
0
            break;
7765
0
        default:
7766
0
            fprintf(output, " <!-- invalid combine value -->");
7767
0
    }
7768
0
    fprintf(output, ">\n");
7769
0
    if (grammar->start == NULL) {
7770
0
        fprintf(output, " <!-- grammar had no start -->");
7771
0
    } else {
7772
0
        fprintf(output, "<start>\n");
7773
0
        xmlRelaxNGDumpDefine(output, grammar->start);
7774
0
        fprintf(output, "</start>\n");
7775
0
    }
7776
    /* TODO ? Dump the defines ? */
7777
0
    fprintf(output, "</grammar>\n");
7778
0
}
7779
7780
/**
7781
 * xmlRelaxNGDump:
7782
 * @output:  the file output
7783
 * @schema:  a schema structure
7784
 *
7785
 * Dump a RelaxNG structure back
7786
 */
7787
void
7788
xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7789
0
{
7790
0
    if (output == NULL)
7791
0
        return;
7792
0
    if (schema == NULL) {
7793
0
        fprintf(output, "RelaxNG empty or failed to compile\n");
7794
0
        return;
7795
0
    }
7796
0
    fprintf(output, "RelaxNG: ");
7797
0
    if (schema->doc == NULL) {
7798
0
        fprintf(output, "no document\n");
7799
0
    } else if (schema->doc->URL != NULL) {
7800
0
        fprintf(output, "%s\n", schema->doc->URL);
7801
0
    } else {
7802
0
        fprintf(output, "\n");
7803
0
    }
7804
0
    if (schema->topgrammar == NULL) {
7805
0
        fprintf(output, "RelaxNG has no top grammar\n");
7806
0
        return;
7807
0
    }
7808
0
    xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7809
0
}
7810
7811
/**
7812
 * xmlRelaxNGDumpTree:
7813
 * @output:  the file output
7814
 * @schema:  a schema structure
7815
 *
7816
 * Dump the transformed RelaxNG tree.
7817
 */
7818
void
7819
xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7820
0
{
7821
0
    if (output == NULL)
7822
0
        return;
7823
0
    if (schema == NULL) {
7824
0
        fprintf(output, "RelaxNG empty or failed to compile\n");
7825
0
        return;
7826
0
    }
7827
0
    if (schema->doc == NULL) {
7828
0
        fprintf(output, "no document\n");
7829
0
    } else {
7830
0
        xmlDocDump(output, schema->doc);
7831
0
    }
7832
0
}
7833
#endif /* LIBXML_OUTPUT_ENABLED */
7834
7835
/************************************************************************
7836
 *                  *
7837
 *    Validation of compiled content        *
7838
 *                  *
7839
 ************************************************************************/
7840
static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7841
                                        xmlRelaxNGDefinePtr define);
7842
7843
/**
7844
 * xmlRelaxNGValidateCompiledCallback:
7845
 * @exec:  the regular expression instance
7846
 * @token:  the token which matched
7847
 * @transdata:  callback data, the define for the subelement if available
7848
 @ @inputdata:  callback data, the Relax NG validation context
7849
 *
7850
 * Handle the callback and if needed validate the element children.
7851
 */
7852
static void
7853
xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7854
                                   const xmlChar * token,
7855
                                   void *transdata, void *inputdata)
7856
0
{
7857
0
    xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7858
0
    xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7859
0
    int ret;
7860
7861
0
    if (ctxt == NULL) {
7862
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
7863
0
                   "callback on %s missing context\n", token, NULL);
7864
0
        return;
7865
0
    }
7866
0
    if (define == NULL) {
7867
0
        if (token[0] == '#')
7868
0
            return;
7869
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
7870
0
                   "callback on %s missing define\n", token, NULL);
7871
0
        if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7872
0
            ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7873
0
        return;
7874
0
    }
7875
0
    if (define->type != XML_RELAXNG_ELEMENT) {
7876
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
7877
0
                   "callback on %s define is not element\n", token, NULL);
7878
0
        if (ctxt->errNo == XML_RELAXNG_OK)
7879
0
            ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7880
0
        return;
7881
0
    }
7882
0
    ret = xmlRelaxNGValidateDefinition(ctxt, define);
7883
0
    if (ret != 0)
7884
0
        ctxt->perr = ret;
7885
0
}
7886
7887
/**
7888
 * xmlRelaxNGValidateCompiledContent:
7889
 * @ctxt:  the RelaxNG validation context
7890
 * @regexp:  the regular expression as compiled
7891
 * @content:  list of children to test against the regexp
7892
 *
7893
 * Validate the content model of an element or start using the regexp
7894
 *
7895
 * Returns 0 in case of success, -1 in case of error.
7896
 */
7897
static int
7898
xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7899
                                  xmlRegexpPtr regexp, xmlNodePtr content)
7900
0
{
7901
0
    xmlRegExecCtxtPtr exec;
7902
0
    xmlNodePtr cur;
7903
0
    int ret = 0;
7904
0
    int oldperr;
7905
7906
0
    if ((ctxt == NULL) || (regexp == NULL))
7907
0
        return (-1);
7908
0
    oldperr = ctxt->perr;
7909
0
    exec = xmlRegNewExecCtxt(regexp,
7910
0
                             xmlRelaxNGValidateCompiledCallback, ctxt);
7911
0
    ctxt->perr = 0;
7912
0
    cur = content;
7913
0
    while (cur != NULL) {
7914
0
        ctxt->state->seq = cur;
7915
0
        switch (cur->type) {
7916
0
            case XML_TEXT_NODE:
7917
0
            case XML_CDATA_SECTION_NODE:
7918
0
                if (xmlIsBlankNode(cur))
7919
0
                    break;
7920
0
                ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7921
0
                if (ret < 0) {
7922
0
                    VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7923
0
                               cur->parent->name);
7924
0
                }
7925
0
                break;
7926
0
            case XML_ELEMENT_NODE:
7927
0
                if (cur->ns != NULL) {
7928
0
                    ret = xmlRegExecPushString2(exec, cur->name,
7929
0
                                                cur->ns->href, ctxt);
7930
0
                } else {
7931
0
                    ret = xmlRegExecPushString(exec, cur->name, ctxt);
7932
0
                }
7933
0
                if (ret < 0) {
7934
0
                    VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7935
0
                }
7936
0
                break;
7937
0
            default:
7938
0
                break;
7939
0
        }
7940
0
        if (ret < 0)
7941
0
            break;
7942
        /*
7943
         * Switch to next element
7944
         */
7945
0
        cur = cur->next;
7946
0
    }
7947
0
    ret = xmlRegExecPushString(exec, NULL, NULL);
7948
0
    if (ret == 1) {
7949
0
        ret = 0;
7950
0
        ctxt->state->seq = NULL;
7951
0
    } else if (ret == 0) {
7952
        /*
7953
         * TODO: get some of the names needed to exit the current state of exec
7954
         */
7955
0
        VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7956
0
        ret = -1;
7957
0
        if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7958
0
            xmlRelaxNGDumpValidError(ctxt);
7959
0
    } else {
7960
0
        ret = -1;
7961
0
    }
7962
0
    xmlRegFreeExecCtxt(exec);
7963
    /*
7964
     * There might be content model errors outside of the pure
7965
     * regexp validation, e.g. for attribute values.
7966
     */
7967
0
    if ((ret == 0) && (ctxt->perr != 0)) {
7968
0
        ret = ctxt->perr;
7969
0
    }
7970
0
    ctxt->perr = oldperr;
7971
0
    return (ret);
7972
0
}
7973
7974
/************************************************************************
7975
 *                  *
7976
 *    Progressive validation of when possible     *
7977
 *                  *
7978
 ************************************************************************/
7979
static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7980
                                           xmlRelaxNGDefinePtr defines);
7981
static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
7982
                                        int dolog);
7983
static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
7984
7985
/**
7986
 * xmlRelaxNGElemPush:
7987
 * @ctxt:  the validation context
7988
 * @exec:  the regexp runtime for the new content model
7989
 *
7990
 * Push a new regexp for the current node content model on the stack
7991
 *
7992
 * Returns 0 in case of success and -1 in case of error.
7993
 */
7994
static int
7995
xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7996
0
{
7997
0
    if (ctxt->elemTab == NULL) {
7998
0
        ctxt->elemMax = 10;
7999
0
        ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8000
0
                                                        sizeof
8001
0
                                                        (xmlRegExecCtxtPtr));
8002
0
        if (ctxt->elemTab == NULL) {
8003
0
            xmlRngVErrMemory(ctxt);
8004
0
            return (-1);
8005
0
        }
8006
0
    }
8007
0
    if (ctxt->elemNr >= ctxt->elemMax) {
8008
0
        ctxt->elemMax *= 2;
8009
0
        ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
8010
0
                                                         ctxt->elemMax *
8011
0
                                                         sizeof
8012
0
                                                         (xmlRegExecCtxtPtr));
8013
0
        if (ctxt->elemTab == NULL) {
8014
0
            xmlRngVErrMemory(ctxt);
8015
0
            return (-1);
8016
0
        }
8017
0
    }
8018
0
    ctxt->elemTab[ctxt->elemNr++] = exec;
8019
0
    ctxt->elem = exec;
8020
0
    return (0);
8021
0
}
8022
8023
/**
8024
 * xmlRelaxNGElemPop:
8025
 * @ctxt:  the validation context
8026
 *
8027
 * Pop the regexp of the current node content model from the stack
8028
 *
8029
 * Returns the exec or NULL if empty
8030
 */
8031
static xmlRegExecCtxtPtr
8032
xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8033
0
{
8034
0
    xmlRegExecCtxtPtr ret;
8035
8036
0
    if (ctxt->elemNr <= 0)
8037
0
        return (NULL);
8038
0
    ctxt->elemNr--;
8039
0
    ret = ctxt->elemTab[ctxt->elemNr];
8040
0
    ctxt->elemTab[ctxt->elemNr] = NULL;
8041
0
    if (ctxt->elemNr > 0)
8042
0
        ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8043
0
    else
8044
0
        ctxt->elem = NULL;
8045
0
    return (ret);
8046
0
}
8047
8048
/**
8049
 * xmlRelaxNGValidateProgressiveCallback:
8050
 * @exec:  the regular expression instance
8051
 * @token:  the token which matched
8052
 * @transdata:  callback data, the define for the subelement if available
8053
 @ @inputdata:  callback data, the Relax NG validation context
8054
 *
8055
 * Handle the callback and if needed validate the element children.
8056
 * some of the in/out information are passed via the context in @inputdata.
8057
 */
8058
static void
8059
xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8060
                                      ATTRIBUTE_UNUSED,
8061
                                      const xmlChar * token,
8062
                                      void *transdata, void *inputdata)
8063
0
{
8064
0
    xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8065
0
    xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
8066
0
    xmlRelaxNGValidStatePtr state, oldstate;
8067
0
    xmlNodePtr node;
8068
0
    int ret = 0, oldflags;
8069
8070
0
    if (ctxt == NULL) {
8071
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
8072
0
                   "callback on %s missing context\n", token, NULL);
8073
0
        return;
8074
0
    }
8075
0
    node = ctxt->pnode;
8076
0
    ctxt->pstate = 1;
8077
0
    if (define == NULL) {
8078
0
        if (token[0] == '#')
8079
0
            return;
8080
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
8081
0
                   "callback on %s missing define\n", token, NULL);
8082
0
        if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8083
0
            ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8084
0
        ctxt->pstate = -1;
8085
0
        return;
8086
0
    }
8087
0
    if (define->type != XML_RELAXNG_ELEMENT) {
8088
0
        xmlRngVErr(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
8089
0
                   "callback on %s define is not element\n", token, NULL);
8090
0
        if (ctxt->errNo == XML_RELAXNG_OK)
8091
0
            ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8092
0
        ctxt->pstate = -1;
8093
0
        return;
8094
0
    }
8095
0
    if (node->type != XML_ELEMENT_NODE) {
8096
0
        VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8097
0
        if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8098
0
            xmlRelaxNGDumpValidError(ctxt);
8099
0
        ctxt->pstate = -1;
8100
0
        return;
8101
0
    }
8102
0
    if (define->contModel == NULL) {
8103
        /*
8104
         * this node cannot be validated in a streamable fashion
8105
         */
8106
0
        ctxt->pstate = 0;
8107
0
        ctxt->pdef = define;
8108
0
        return;
8109
0
    }
8110
0
    exec = xmlRegNewExecCtxt(define->contModel,
8111
0
                             xmlRelaxNGValidateProgressiveCallback, ctxt);
8112
0
    if (exec == NULL) {
8113
0
        ctxt->pstate = -1;
8114
0
        return;
8115
0
    }
8116
0
    xmlRelaxNGElemPush(ctxt, exec);
8117
8118
    /*
8119
     * Validate the attributes part of the content.
8120
     */
8121
0
    state = xmlRelaxNGNewValidState(ctxt, node);
8122
0
    if (state == NULL) {
8123
0
        ctxt->pstate = -1;
8124
0
        return;
8125
0
    }
8126
0
    oldstate = ctxt->state;
8127
0
    ctxt->state = state;
8128
0
    if (define->attrs != NULL) {
8129
0
        ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8130
0
        if (ret != 0) {
8131
0
            ctxt->pstate = -1;
8132
0
            VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8133
0
        }
8134
0
    }
8135
0
    if (ctxt->state != NULL) {
8136
0
        ctxt->state->seq = NULL;
8137
0
        ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8138
0
        if (ret != 0) {
8139
0
            ctxt->pstate = -1;
8140
0
        }
8141
0
        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8142
0
    } else if (ctxt->states != NULL) {
8143
0
        int tmp = -1, i;
8144
8145
0
        oldflags = ctxt->flags;
8146
8147
0
        for (i = 0; i < ctxt->states->nbState; i++) {
8148
0
            state = ctxt->states->tabState[i];
8149
0
            ctxt->state = state;
8150
0
            ctxt->state->seq = NULL;
8151
8152
0
            if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8153
0
                tmp = 0;
8154
0
                break;
8155
0
            }
8156
0
        }
8157
0
        if (tmp != 0) {
8158
            /*
8159
             * validation error, log the message for the "best" one
8160
             */
8161
0
            ctxt->flags |= FLAGS_IGNORABLE;
8162
0
            xmlRelaxNGLogBestError(ctxt);
8163
0
        }
8164
0
        for (i = 0; i < ctxt->states->nbState; i++) {
8165
0
            xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8166
0
        }
8167
0
        xmlRelaxNGFreeStates(ctxt, ctxt->states);
8168
0
        ctxt->states = NULL;
8169
0
        if ((ret == 0) && (tmp == -1))
8170
0
            ctxt->pstate = -1;
8171
0
        ctxt->flags = oldflags;
8172
0
    }
8173
0
    if (ctxt->pstate == -1) {
8174
0
        if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8175
0
            xmlRelaxNGDumpValidError(ctxt);
8176
0
        }
8177
0
    }
8178
0
    ctxt->state = oldstate;
8179
0
}
8180
8181
/**
8182
 * xmlRelaxNGValidatePushElement:
8183
 * @ctxt:  the validation context
8184
 * @doc:  a document instance
8185
 * @elem:  an element instance
8186
 *
8187
 * Push a new element start on the RelaxNG validation stack.
8188
 *
8189
 * returns 1 if no validation problem was found or 0 if validating the
8190
 *         element requires a full node, and -1 in case of error.
8191
 */
8192
int
8193
xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8194
                              xmlDocPtr doc ATTRIBUTE_UNUSED,
8195
                              xmlNodePtr elem)
8196
0
{
8197
0
    int ret = 1;
8198
8199
0
    if ((ctxt == NULL) || (elem == NULL))
8200
0
        return (-1);
8201
8202
0
    if (ctxt->elem == 0) {
8203
0
        xmlRelaxNGPtr schema;
8204
0
        xmlRelaxNGGrammarPtr grammar;
8205
0
        xmlRegExecCtxtPtr exec;
8206
0
        xmlRelaxNGDefinePtr define;
8207
8208
0
        schema = ctxt->schema;
8209
0
        if (schema == NULL) {
8210
0
            VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8211
0
            return (-1);
8212
0
        }
8213
0
        grammar = schema->topgrammar;
8214
0
        if ((grammar == NULL) || (grammar->start == NULL)) {
8215
0
            VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8216
0
            return (-1);
8217
0
        }
8218
0
        define = grammar->start;
8219
0
        if (define->contModel == NULL) {
8220
0
            ctxt->pdef = define;
8221
0
            return (0);
8222
0
        }
8223
0
        exec = xmlRegNewExecCtxt(define->contModel,
8224
0
                                 xmlRelaxNGValidateProgressiveCallback,
8225
0
                                 ctxt);
8226
0
        if (exec == NULL) {
8227
0
            return (-1);
8228
0
        }
8229
0
        xmlRelaxNGElemPush(ctxt, exec);
8230
0
    }
8231
0
    ctxt->pnode = elem;
8232
0
    ctxt->pstate = 0;
8233
0
    if (elem->ns != NULL) {
8234
0
        ret =
8235
0
            xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8236
0
                                  ctxt);
8237
0
    } else {
8238
0
        ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8239
0
    }
8240
0
    if (ret < 0) {
8241
0
        VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8242
0
    } else {
8243
0
        if (ctxt->pstate == 0)
8244
0
            ret = 0;
8245
0
        else if (ctxt->pstate < 0)
8246
0
            ret = -1;
8247
0
        else
8248
0
            ret = 1;
8249
0
    }
8250
0
    return (ret);
8251
0
}
8252
8253
/**
8254
 * xmlRelaxNGValidatePushCData:
8255
 * @ctxt:  the RelaxNG validation context
8256
 * @data:  some character data read
8257
 * @len:  the length of the data
8258
 *
8259
 * check the CData parsed for validation in the current stack
8260
 *
8261
 * returns 1 if no validation problem was found or -1 otherwise
8262
 */
8263
int
8264
xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
8265
                            const xmlChar * data, int len ATTRIBUTE_UNUSED)
8266
0
{
8267
0
    int ret = 1;
8268
8269
0
    if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8270
0
        return (-1);
8271
8272
0
    while (*data != 0) {
8273
0
        if (!IS_BLANK_CH(*data))
8274
0
            break;
8275
0
        data++;
8276
0
    }
8277
0
    if (*data == 0)
8278
0
        return (1);
8279
8280
0
    ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8281
0
    if (ret < 0) {
8282
0
        VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
8283
8284
0
        return (-1);
8285
0
    }
8286
0
    return (1);
8287
0
}
8288
8289
/**
8290
 * xmlRelaxNGValidatePopElement:
8291
 * @ctxt:  the RelaxNG validation context
8292
 * @doc:  a document instance
8293
 * @elem:  an element instance
8294
 *
8295
 * Pop the element end from the RelaxNG validation stack.
8296
 *
8297
 * returns 1 if no validation problem was found or 0 otherwise
8298
 */
8299
int
8300
xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8301
                             xmlDocPtr doc ATTRIBUTE_UNUSED,
8302
                             xmlNodePtr elem)
8303
0
{
8304
0
    int ret;
8305
0
    xmlRegExecCtxtPtr exec;
8306
8307
0
    if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8308
0
        return (-1);
8309
    /*
8310
     * verify that we reached a terminal state of the content model.
8311
     */
8312
0
    exec = xmlRelaxNGElemPop(ctxt);
8313
0
    ret = xmlRegExecPushString(exec, NULL, NULL);
8314
0
    if (ret == 0) {
8315
        /*
8316
         * TODO: get some of the names needed to exit the current state of exec
8317
         */
8318
0
        VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8319
0
        ret = -1;
8320
0
    } else if (ret < 0) {
8321
0
        ret = -1;
8322
0
    } else {
8323
0
        ret = 1;
8324
0
    }
8325
0
    xmlRegFreeExecCtxt(exec);
8326
0
    return (ret);
8327
0
}
8328
8329
/**
8330
 * xmlRelaxNGValidateFullElement:
8331
 * @ctxt:  the validation context
8332
 * @doc:  a document instance
8333
 * @elem:  an element instance
8334
 *
8335
 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8336
 * 0 and the content of the node has been expanded.
8337
 *
8338
 * returns 1 if no validation problem was found or -1 in case of error.
8339
 */
8340
int
8341
xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8342
                              xmlDocPtr doc ATTRIBUTE_UNUSED,
8343
                              xmlNodePtr elem)
8344
0
{
8345
0
    int ret;
8346
0
    xmlRelaxNGValidStatePtr state;
8347
8348
0
    if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8349
0
        return (-1);
8350
0
    state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8351
0
    if (state == NULL) {
8352
0
        return (-1);
8353
0
    }
8354
0
    state->seq = elem;
8355
0
    ctxt->state = state;
8356
0
    ctxt->errNo = XML_RELAXNG_OK;
8357
0
    ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8358
0
    if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8359
0
        ret = -1;
8360
0
    else
8361
0
        ret = 1;
8362
0
    xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8363
0
    ctxt->state = NULL;
8364
0
    return (ret);
8365
0
}
8366
8367
/************************************************************************
8368
 *                  *
8369
 *    Generic interpreted validation implementation   *
8370
 *                  *
8371
 ************************************************************************/
8372
static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8373
                                   xmlRelaxNGDefinePtr define);
8374
8375
/**
8376
 * xmlRelaxNGSkipIgnored:
8377
 * @ctxt:  a schema validation context
8378
 * @node:  the top node.
8379
 *
8380
 * Skip ignorable nodes in that context
8381
 *
8382
 * Returns the new sibling or NULL in case of error.
8383
 */
8384
static xmlNodePtr
8385
xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8386
                      xmlNodePtr node)
8387
0
{
8388
    /*
8389
     * TODO complete and handle entities
8390
     */
8391
0
    while ((node != NULL) &&
8392
0
           ((node->type == XML_COMMENT_NODE) ||
8393
0
            (node->type == XML_PI_NODE) ||
8394
0
      (node->type == XML_XINCLUDE_START) ||
8395
0
      (node->type == XML_XINCLUDE_END) ||
8396
0
            (((node->type == XML_TEXT_NODE) ||
8397
0
              (node->type == XML_CDATA_SECTION_NODE)) &&
8398
0
             ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8399
0
              (IS_BLANK_NODE(node)))))) {
8400
0
        node = node->next;
8401
0
    }
8402
0
    return (node);
8403
0
}
8404
8405
/**
8406
 * xmlRelaxNGNormalize:
8407
 * @ctxt:  a schema validation context
8408
 * @str:  the string to normalize
8409
 *
8410
 * Implements the  normalizeWhiteSpace( s ) function from
8411
 * section 6.2.9 of the spec
8412
 *
8413
 * Returns the new string or NULL in case of error.
8414
 */
8415
static xmlChar *
8416
xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8417
0
{
8418
0
    xmlChar *ret, *p;
8419
0
    const xmlChar *tmp;
8420
0
    int len;
8421
8422
0
    if (str == NULL)
8423
0
        return (NULL);
8424
0
    tmp = str;
8425
0
    while (*tmp != 0)
8426
0
        tmp++;
8427
0
    len = tmp - str;
8428
8429
0
    ret = xmlMalloc(len + 1);
8430
0
    if (ret == NULL) {
8431
0
        xmlRngVErrMemory(ctxt);
8432
0
        return (NULL);
8433
0
    }
8434
0
    p = ret;
8435
0
    while (IS_BLANK_CH(*str))
8436
0
        str++;
8437
0
    while (*str != 0) {
8438
0
        if (IS_BLANK_CH(*str)) {
8439
0
            while (IS_BLANK_CH(*str))
8440
0
                str++;
8441
0
            if (*str == 0)
8442
0
                break;
8443
0
            *p++ = ' ';
8444
0
        } else
8445
0
            *p++ = *str++;
8446
0
    }
8447
0
    *p = 0;
8448
0
    return (ret);
8449
0
}
8450
8451
/**
8452
 * xmlRelaxNGValidateDatatype:
8453
 * @ctxt:  a Relax-NG validation context
8454
 * @value:  the string value
8455
 * @type:  the datatype definition
8456
 * @node:  the node
8457
 *
8458
 * Validate the given value against the datatype
8459
 *
8460
 * Returns 0 if the validation succeeded or an error code.
8461
 */
8462
static int
8463
xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8464
                           const xmlChar * value,
8465
                           xmlRelaxNGDefinePtr define, xmlNodePtr node)
8466
0
{
8467
0
    int ret, tmp;
8468
0
    xmlRelaxNGTypeLibraryPtr lib;
8469
0
    void *result = NULL;
8470
0
    xmlRelaxNGDefinePtr cur;
8471
8472
0
    if ((define == NULL) || (define->data == NULL)) {
8473
0
        return (-1);
8474
0
    }
8475
0
    lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8476
0
    if (lib->check != NULL) {
8477
0
        if ((define->attrs != NULL) &&
8478
0
            (define->attrs->type == XML_RELAXNG_PARAM)) {
8479
0
            ret =
8480
0
                lib->check(lib->data, define->name, value, &result, node);
8481
0
        } else {
8482
0
            ret = lib->check(lib->data, define->name, value, NULL, node);
8483
0
        }
8484
0
    } else
8485
0
        ret = -1;
8486
0
    if (ret < 0) {
8487
0
        VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8488
0
        if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8489
0
            lib->freef(lib->data, result);
8490
0
        return (-1);
8491
0
    } else if (ret == 1) {
8492
0
        ret = 0;
8493
0
    } else if (ret == 2) {
8494
0
        VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
8495
0
    } else {
8496
0
        VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8497
0
        ret = -1;
8498
0
    }
8499
0
    cur = define->attrs;
8500
0
    while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8501
0
        if (lib->facet != NULL) {
8502
0
            tmp = lib->facet(lib->data, define->name, cur->name,
8503
0
                             cur->value, value, result);
8504
0
            if (tmp != 0)
8505
0
                ret = -1;
8506
0
        }
8507
0
        cur = cur->next;
8508
0
    }
8509
0
    if ((ret == 0) && (define->content != NULL)) {
8510
0
        const xmlChar *oldvalue, *oldendvalue;
8511
8512
0
        oldvalue = ctxt->state->value;
8513
0
        oldendvalue = ctxt->state->endvalue;
8514
0
        ctxt->state->value = (xmlChar *) value;
8515
0
        ctxt->state->endvalue = NULL;
8516
0
        ret = xmlRelaxNGValidateValue(ctxt, define->content);
8517
0
        ctxt->state->value = (xmlChar *) oldvalue;
8518
0
        ctxt->state->endvalue = (xmlChar *) oldendvalue;
8519
0
    }
8520
0
    if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8521
0
        lib->freef(lib->data, result);
8522
0
    return (ret);
8523
0
}
8524
8525
/**
8526
 * xmlRelaxNGNextValue:
8527
 * @ctxt:  a Relax-NG validation context
8528
 *
8529
 * Skip to the next value when validating within a list
8530
 *
8531
 * Returns 0 if the operation succeeded or an error code.
8532
 */
8533
static int
8534
xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8535
0
{
8536
0
    xmlChar *cur;
8537
8538
0
    cur = ctxt->state->value;
8539
0
    if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8540
0
        ctxt->state->value = NULL;
8541
0
        ctxt->state->endvalue = NULL;
8542
0
        return (0);
8543
0
    }
8544
0
    while (*cur != 0)
8545
0
        cur++;
8546
0
    while ((cur != ctxt->state->endvalue) && (*cur == 0))
8547
0
        cur++;
8548
0
    if (cur == ctxt->state->endvalue)
8549
0
        ctxt->state->value = NULL;
8550
0
    else
8551
0
        ctxt->state->value = cur;
8552
0
    return (0);
8553
0
}
8554
8555
/**
8556
 * xmlRelaxNGValidateValueList:
8557
 * @ctxt:  a Relax-NG validation context
8558
 * @defines:  the list of definitions to verify
8559
 *
8560
 * Validate the given set of definitions for the current value
8561
 *
8562
 * Returns 0 if the validation succeeded or an error code.
8563
 */
8564
static int
8565
xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8566
                            xmlRelaxNGDefinePtr defines)
8567
0
{
8568
0
    int ret = 0;
8569
8570
0
    while (defines != NULL) {
8571
0
        ret = xmlRelaxNGValidateValue(ctxt, defines);
8572
0
        if (ret != 0)
8573
0
            break;
8574
0
        defines = defines->next;
8575
0
    }
8576
0
    return (ret);
8577
0
}
8578
8579
/**
8580
 * xmlRelaxNGValidateValue:
8581
 * @ctxt:  a Relax-NG validation context
8582
 * @define:  the definition to verify
8583
 *
8584
 * Validate the given definition for the current value
8585
 *
8586
 * Returns 0 if the validation succeeded or an error code.
8587
 */
8588
static int
8589
xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8590
                        xmlRelaxNGDefinePtr define)
8591
0
{
8592
0
    int ret = 0, oldflags;
8593
0
    xmlChar *value;
8594
8595
0
    value = ctxt->state->value;
8596
0
    switch (define->type) {
8597
0
        case XML_RELAXNG_EMPTY:{
8598
0
                if ((value != NULL) && (value[0] != 0)) {
8599
0
                    int idx = 0;
8600
8601
0
                    while (IS_BLANK_CH(value[idx]))
8602
0
                        idx++;
8603
0
                    if (value[idx] != 0)
8604
0
                        ret = -1;
8605
0
                }
8606
0
                break;
8607
0
            }
8608
0
        case XML_RELAXNG_TEXT:
8609
0
            break;
8610
0
        case XML_RELAXNG_VALUE:{
8611
0
                if (!xmlStrEqual(value, define->value)) {
8612
0
                    if (define->name != NULL) {
8613
0
                        xmlRelaxNGTypeLibraryPtr lib;
8614
8615
0
                        lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8616
0
                        if ((lib != NULL) && (lib->comp != NULL)) {
8617
0
                            ret = lib->comp(lib->data, define->name,
8618
0
                                            define->value, define->node,
8619
0
                                            (void *) define->attrs,
8620
0
                                            value, ctxt->state->node);
8621
0
                        } else
8622
0
                            ret = -1;
8623
0
                        if (ret < 0) {
8624
0
                            VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8625
0
                                       define->name);
8626
0
                            return (-1);
8627
0
                        } else if (ret == 1) {
8628
0
                            ret = 0;
8629
0
                        } else {
8630
0
                            ret = -1;
8631
0
                        }
8632
0
                    } else {
8633
0
                        xmlChar *nval, *nvalue;
8634
8635
                        /*
8636
                         * TODO: trivial optimizations are possible by
8637
                         * computing at compile-time
8638
                         */
8639
0
                        nval = xmlRelaxNGNormalize(ctxt, define->value);
8640
0
                        nvalue = xmlRelaxNGNormalize(ctxt, value);
8641
8642
0
                        if ((nval == NULL) || (nvalue == NULL) ||
8643
0
                            (!xmlStrEqual(nval, nvalue)))
8644
0
                            ret = -1;
8645
0
                        if (nval != NULL)
8646
0
                            xmlFree(nval);
8647
0
                        if (nvalue != NULL)
8648
0
                            xmlFree(nvalue);
8649
0
                    }
8650
0
                }
8651
0
                if (ret == 0)
8652
0
                    xmlRelaxNGNextValue(ctxt);
8653
0
                break;
8654
0
            }
8655
0
        case XML_RELAXNG_DATATYPE:{
8656
0
                ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8657
0
                                                 ctxt->state->seq);
8658
0
                if (ret == 0)
8659
0
                    xmlRelaxNGNextValue(ctxt);
8660
8661
0
                break;
8662
0
            }
8663
0
        case XML_RELAXNG_CHOICE:{
8664
0
                xmlRelaxNGDefinePtr list = define->content;
8665
0
                xmlChar *oldvalue;
8666
8667
0
                oldflags = ctxt->flags;
8668
0
                ctxt->flags |= FLAGS_IGNORABLE;
8669
8670
0
                oldvalue = ctxt->state->value;
8671
0
                while (list != NULL) {
8672
0
                    ret = xmlRelaxNGValidateValue(ctxt, list);
8673
0
                    if (ret == 0) {
8674
0
                        break;
8675
0
                    }
8676
0
                    ctxt->state->value = oldvalue;
8677
0
                    list = list->next;
8678
0
                }
8679
0
                ctxt->flags = oldflags;
8680
0
                if (ret != 0) {
8681
0
                    if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8682
0
                        xmlRelaxNGDumpValidError(ctxt);
8683
0
                } else {
8684
0
                    if (ctxt->errNr > 0)
8685
0
                        xmlRelaxNGPopErrors(ctxt, 0);
8686
0
                }
8687
0
                break;
8688
0
            }
8689
0
        case XML_RELAXNG_LIST:{
8690
0
                xmlRelaxNGDefinePtr list = define->content;
8691
0
                xmlChar *oldvalue, *oldend, *val, *cur;
8692
8693
0
                oldvalue = ctxt->state->value;
8694
0
                oldend = ctxt->state->endvalue;
8695
8696
0
                val = xmlStrdup(oldvalue);
8697
0
                if (val == NULL) {
8698
0
                    val = xmlStrdup(BAD_CAST "");
8699
0
                }
8700
0
                if (val == NULL) {
8701
0
                    VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8702
0
                    return (-1);
8703
0
                }
8704
0
                cur = val;
8705
0
                while (*cur != 0) {
8706
0
                    if (IS_BLANK_CH(*cur)) {
8707
0
                        *cur = 0;
8708
0
                        cur++;
8709
0
                        while (IS_BLANK_CH(*cur))
8710
0
                            *cur++ = 0;
8711
0
                    } else
8712
0
                        cur++;
8713
0
                }
8714
0
                ctxt->state->endvalue = cur;
8715
0
                cur = val;
8716
0
                while ((*cur == 0) && (cur != ctxt->state->endvalue))
8717
0
                    cur++;
8718
8719
0
                ctxt->state->value = cur;
8720
8721
0
                while (list != NULL) {
8722
0
                    if (ctxt->state->value == ctxt->state->endvalue)
8723
0
                        ctxt->state->value = NULL;
8724
0
                    ret = xmlRelaxNGValidateValue(ctxt, list);
8725
0
                    if (ret != 0) {
8726
0
                        break;
8727
0
                    }
8728
0
                    list = list->next;
8729
0
                }
8730
8731
0
                if ((ret == 0) && (ctxt->state->value != NULL) &&
8732
0
                    (ctxt->state->value != ctxt->state->endvalue)) {
8733
0
                    VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8734
0
                               ctxt->state->value);
8735
0
                    ret = -1;
8736
0
                }
8737
0
                xmlFree(val);
8738
0
                ctxt->state->value = oldvalue;
8739
0
                ctxt->state->endvalue = oldend;
8740
0
                break;
8741
0
            }
8742
0
        case XML_RELAXNG_ONEORMORE:
8743
0
            ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8744
0
            if (ret != 0) {
8745
0
                break;
8746
0
            }
8747
            /* Falls through. */
8748
0
        case XML_RELAXNG_ZEROORMORE:{
8749
0
                xmlChar *cur, *temp;
8750
8751
0
                if ((ctxt->state->value == NULL) ||
8752
0
                    (*ctxt->state->value == 0)) {
8753
0
                    ret = 0;
8754
0
                    break;
8755
0
                }
8756
0
                oldflags = ctxt->flags;
8757
0
                ctxt->flags |= FLAGS_IGNORABLE;
8758
0
                cur = ctxt->state->value;
8759
0
                temp = NULL;
8760
0
                while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8761
0
                       (temp != cur)) {
8762
0
                    temp = cur;
8763
0
                    ret =
8764
0
                        xmlRelaxNGValidateValueList(ctxt, define->content);
8765
0
                    if (ret != 0) {
8766
0
                        ctxt->state->value = temp;
8767
0
                        ret = 0;
8768
0
                        break;
8769
0
                    }
8770
0
                    cur = ctxt->state->value;
8771
0
                }
8772
0
                ctxt->flags = oldflags;
8773
0
    if (ctxt->errNr > 0)
8774
0
        xmlRelaxNGPopErrors(ctxt, 0);
8775
0
                break;
8776
0
            }
8777
0
        case XML_RELAXNG_OPTIONAL:{
8778
0
                xmlChar *temp;
8779
8780
0
                if ((ctxt->state->value == NULL) ||
8781
0
                    (*ctxt->state->value == 0)) {
8782
0
                    ret = 0;
8783
0
                    break;
8784
0
                }
8785
0
                oldflags = ctxt->flags;
8786
0
                ctxt->flags |= FLAGS_IGNORABLE;
8787
0
                temp = ctxt->state->value;
8788
0
                ret = xmlRelaxNGValidateValue(ctxt, define->content);
8789
0
                ctxt->flags = oldflags;
8790
0
                if (ret != 0) {
8791
0
                    ctxt->state->value = temp;
8792
0
                    if (ctxt->errNr > 0)
8793
0
                        xmlRelaxNGPopErrors(ctxt, 0);
8794
0
                    ret = 0;
8795
0
                    break;
8796
0
                }
8797
0
    if (ctxt->errNr > 0)
8798
0
        xmlRelaxNGPopErrors(ctxt, 0);
8799
0
                break;
8800
0
            }
8801
0
        case XML_RELAXNG_EXCEPT:{
8802
0
                xmlRelaxNGDefinePtr list;
8803
8804
0
                list = define->content;
8805
0
                while (list != NULL) {
8806
0
                    ret = xmlRelaxNGValidateValue(ctxt, list);
8807
0
                    if (ret == 0) {
8808
0
                        ret = -1;
8809
0
                        break;
8810
0
                    } else
8811
0
                        ret = 0;
8812
0
                    list = list->next;
8813
0
                }
8814
0
                break;
8815
0
            }
8816
0
        case XML_RELAXNG_DEF:
8817
0
        case XML_RELAXNG_GROUP:{
8818
0
                xmlRelaxNGDefinePtr list;
8819
8820
0
                list = define->content;
8821
0
                while (list != NULL) {
8822
0
                    ret = xmlRelaxNGValidateValue(ctxt, list);
8823
0
                    if (ret != 0) {
8824
0
                        ret = -1;
8825
0
                        break;
8826
0
                    } else
8827
0
                        ret = 0;
8828
0
                    list = list->next;
8829
0
                }
8830
0
                break;
8831
0
            }
8832
0
        case XML_RELAXNG_REF:
8833
0
        case XML_RELAXNG_PARENTREF:
8834
0
      if (define->content == NULL) {
8835
0
                VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8836
0
                ret = -1;
8837
0
      } else {
8838
0
                ret = xmlRelaxNGValidateValue(ctxt, define->content);
8839
0
            }
8840
0
            break;
8841
0
        default:
8842
            /* TODO */
8843
0
            ret = -1;
8844
0
    }
8845
0
    return (ret);
8846
0
}
8847
8848
/**
8849
 * xmlRelaxNGValidateValueContent:
8850
 * @ctxt:  a Relax-NG validation context
8851
 * @defines:  the list of definitions to verify
8852
 *
8853
 * Validate the given definitions for the current value
8854
 *
8855
 * Returns 0 if the validation succeeded or an error code.
8856
 */
8857
static int
8858
xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8859
                               xmlRelaxNGDefinePtr defines)
8860
0
{
8861
0
    int ret = 0;
8862
8863
0
    while (defines != NULL) {
8864
0
        ret = xmlRelaxNGValidateValue(ctxt, defines);
8865
0
        if (ret != 0)
8866
0
            break;
8867
0
        defines = defines->next;
8868
0
    }
8869
0
    return (ret);
8870
0
}
8871
8872
/**
8873
 * xmlRelaxNGAttributeMatch:
8874
 * @ctxt:  a Relax-NG validation context
8875
 * @define:  the definition to check
8876
 * @prop:  the attribute
8877
 *
8878
 * Check if the attribute matches the definition nameClass
8879
 *
8880
 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8881
 */
8882
static int
8883
xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8884
                         xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8885
0
{
8886
0
    int ret;
8887
8888
0
    if (define->name != NULL) {
8889
0
        if (!xmlStrEqual(define->name, prop->name))
8890
0
            return (0);
8891
0
    }
8892
0
    if (define->ns != NULL) {
8893
0
        if (define->ns[0] == 0) {
8894
0
            if (prop->ns != NULL)
8895
0
                return (0);
8896
0
        } else {
8897
0
            if ((prop->ns == NULL) ||
8898
0
                (!xmlStrEqual(define->ns, prop->ns->href)))
8899
0
                return (0);
8900
0
        }
8901
0
    }
8902
0
    if (define->nameClass == NULL)
8903
0
        return (1);
8904
0
    define = define->nameClass;
8905
0
    if (define->type == XML_RELAXNG_EXCEPT) {
8906
0
        xmlRelaxNGDefinePtr list;
8907
8908
0
        list = define->content;
8909
0
        while (list != NULL) {
8910
0
            ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8911
0
            if (ret == 1)
8912
0
                return (0);
8913
0
            if (ret < 0)
8914
0
                return (ret);
8915
0
            list = list->next;
8916
0
        }
8917
0
    } else if (define->type == XML_RELAXNG_CHOICE) {
8918
0
        xmlRelaxNGDefinePtr list;
8919
8920
0
        list = define->nameClass;
8921
0
        while (list != NULL) {
8922
0
            ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8923
0
            if (ret == 1)
8924
0
                return (1);
8925
0
            if (ret < 0)
8926
0
                return (ret);
8927
0
            list = list->next;
8928
0
        }
8929
0
        return (0);
8930
0
    } else {
8931
        /* TODO */
8932
0
        return (0);
8933
0
    }
8934
0
    return (1);
8935
0
}
8936
8937
/**
8938
 * xmlRelaxNGValidateAttribute:
8939
 * @ctxt:  a Relax-NG validation context
8940
 * @define:  the definition to verify
8941
 *
8942
 * Validate the given attribute definition for that node
8943
 *
8944
 * Returns 0 if the validation succeeded or an error code.
8945
 */
8946
static int
8947
xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8948
                            xmlRelaxNGDefinePtr define)
8949
0
{
8950
0
    int ret = 0, i;
8951
0
    xmlChar *value, *oldvalue;
8952
0
    xmlAttrPtr prop = NULL, tmp;
8953
0
    xmlNodePtr oldseq;
8954
8955
0
    if (ctxt->state->nbAttrLeft <= 0)
8956
0
        return (-1);
8957
0
    if (define->name != NULL) {
8958
0
        for (i = 0; i < ctxt->state->nbAttrs; i++) {
8959
0
            tmp = ctxt->state->attrs[i];
8960
0
            if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8961
0
                if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8962
0
                     (tmp->ns == NULL)) ||
8963
0
                    ((tmp->ns != NULL) &&
8964
0
                     (xmlStrEqual(define->ns, tmp->ns->href)))) {
8965
0
                    prop = tmp;
8966
0
                    break;
8967
0
                }
8968
0
            }
8969
0
        }
8970
0
        if (prop != NULL) {
8971
0
            value = xmlNodeListGetString(prop->doc, prop->children, 1);
8972
0
            oldvalue = ctxt->state->value;
8973
0
            oldseq = ctxt->state->seq;
8974
0
            ctxt->state->seq = (xmlNodePtr) prop;
8975
0
            ctxt->state->value = value;
8976
0
            ctxt->state->endvalue = NULL;
8977
0
            ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8978
0
            if (ctxt->state->value != NULL)
8979
0
                value = ctxt->state->value;
8980
0
            if (value != NULL)
8981
0
                xmlFree(value);
8982
0
            ctxt->state->value = oldvalue;
8983
0
            ctxt->state->seq = oldseq;
8984
0
            if (ret == 0) {
8985
                /*
8986
                 * flag the attribute as processed
8987
                 */
8988
0
                ctxt->state->attrs[i] = NULL;
8989
0
                ctxt->state->nbAttrLeft--;
8990
0
            }
8991
0
        } else {
8992
0
            ret = -1;
8993
0
        }
8994
0
    } else {
8995
0
        for (i = 0; i < ctxt->state->nbAttrs; i++) {
8996
0
            tmp = ctxt->state->attrs[i];
8997
0
            if ((tmp != NULL) &&
8998
0
                (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8999
0
                prop = tmp;
9000
0
                break;
9001
0
            }
9002
0
        }
9003
0
        if (prop != NULL) {
9004
0
            value = xmlNodeListGetString(prop->doc, prop->children, 1);
9005
0
            oldvalue = ctxt->state->value;
9006
0
            oldseq = ctxt->state->seq;
9007
0
            ctxt->state->seq = (xmlNodePtr) prop;
9008
0
            ctxt->state->value = value;
9009
0
            ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9010
0
            if (ctxt->state->value != NULL)
9011
0
                value = ctxt->state->value;
9012
0
            if (value != NULL)
9013
0
                xmlFree(value);
9014
0
            ctxt->state->value = oldvalue;
9015
0
            ctxt->state->seq = oldseq;
9016
0
            if (ret == 0) {
9017
                /*
9018
                 * flag the attribute as processed
9019
                 */
9020
0
                ctxt->state->attrs[i] = NULL;
9021
0
                ctxt->state->nbAttrLeft--;
9022
0
            }
9023
0
        } else {
9024
0
            ret = -1;
9025
0
        }
9026
0
    }
9027
9028
0
    return (ret);
9029
0
}
9030
9031
/**
9032
 * xmlRelaxNGValidateAttributeList:
9033
 * @ctxt:  a Relax-NG validation context
9034
 * @define:  the list of definition to verify
9035
 *
9036
 * Validate the given node against the list of attribute definitions
9037
 *
9038
 * Returns 0 if the validation succeeded or an error code.
9039
 */
9040
static int
9041
xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9042
                                xmlRelaxNGDefinePtr defines)
9043
0
{
9044
0
    int ret = 0, res;
9045
0
    int needmore = 0;
9046
0
    xmlRelaxNGDefinePtr cur;
9047
9048
0
    cur = defines;
9049
0
    while (cur != NULL) {
9050
0
        if (cur->type == XML_RELAXNG_ATTRIBUTE) {
9051
0
            if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9052
0
                ret = -1;
9053
0
        } else
9054
0
            needmore = 1;
9055
0
        cur = cur->next;
9056
0
    }
9057
0
    if (!needmore)
9058
0
        return (ret);
9059
0
    cur = defines;
9060
0
    while (cur != NULL) {
9061
0
        if (cur->type != XML_RELAXNG_ATTRIBUTE) {
9062
0
            if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9063
0
                res = xmlRelaxNGValidateDefinition(ctxt, cur);
9064
0
                if (res < 0)
9065
0
                    ret = -1;
9066
0
            } else {
9067
0
                VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9068
0
                return (-1);
9069
0
            }
9070
0
            if (res == -1)      /* continues on -2 */
9071
0
                break;
9072
0
        }
9073
0
        cur = cur->next;
9074
0
    }
9075
9076
0
    return (ret);
9077
0
}
9078
9079
/**
9080
 * xmlRelaxNGNodeMatchesList:
9081
 * @node:  the node
9082
 * @list:  a NULL terminated array of definitions
9083
 *
9084
 * Check if a node can be matched by one of the definitions
9085
 *
9086
 * Returns 1 if matches 0 otherwise
9087
 */
9088
static int
9089
xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9090
0
{
9091
0
    xmlRelaxNGDefinePtr cur;
9092
0
    int i = 0, tmp;
9093
9094
0
    if ((node == NULL) || (list == NULL))
9095
0
        return (0);
9096
9097
0
    cur = list[i++];
9098
0
    while (cur != NULL) {
9099
0
        if ((node->type == XML_ELEMENT_NODE) &&
9100
0
            (cur->type == XML_RELAXNG_ELEMENT)) {
9101
0
            tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9102
0
            if (tmp == 1)
9103
0
                return (1);
9104
0
        } else if (((node->type == XML_TEXT_NODE) ||
9105
0
                    (node->type == XML_CDATA_SECTION_NODE)) &&
9106
0
                   ((cur->type == XML_RELAXNG_DATATYPE) ||
9107
0
        (cur->type == XML_RELAXNG_LIST) ||
9108
0
                    (cur->type == XML_RELAXNG_TEXT) ||
9109
0
                    (cur->type == XML_RELAXNG_VALUE))) {
9110
0
            return (1);
9111
0
        }
9112
0
        cur = list[i++];
9113
0
    }
9114
0
    return (0);
9115
0
}
9116
9117
/**
9118
 * xmlRelaxNGValidateInterleave:
9119
 * @ctxt:  a Relax-NG validation context
9120
 * @define:  the definition to verify
9121
 *
9122
 * Validate an interleave definition for a node.
9123
 *
9124
 * Returns 0 if the validation succeeded or an error code.
9125
 */
9126
static int
9127
xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9128
                             xmlRelaxNGDefinePtr define)
9129
0
{
9130
0
    int ret = 0, i, nbgroups;
9131
0
    int errNr = ctxt->errNr;
9132
0
    int oldflags;
9133
9134
0
    xmlRelaxNGValidStatePtr oldstate;
9135
0
    xmlRelaxNGPartitionPtr partitions;
9136
0
    xmlRelaxNGInterleaveGroupPtr group = NULL;
9137
0
    xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9138
0
    xmlNodePtr *list = NULL, *lasts = NULL;
9139
9140
0
    if (define->data != NULL) {
9141
0
        partitions = (xmlRelaxNGPartitionPtr) define->data;
9142
0
        nbgroups = partitions->nbgroups;
9143
0
    } else {
9144
0
        VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9145
0
        return (-1);
9146
0
    }
9147
    /*
9148
     * Optimizations for MIXED
9149
     */
9150
0
    oldflags = ctxt->flags;
9151
0
    if (define->dflags & IS_MIXED) {
9152
0
        ctxt->flags |= FLAGS_MIXED_CONTENT;
9153
0
        if (nbgroups == 2) {
9154
            /*
9155
             * this is a pure <mixed> case
9156
             */
9157
0
            if (ctxt->state != NULL)
9158
0
                ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9159
0
                                                         ctxt->state->seq);
9160
0
            if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9161
0
                ret = xmlRelaxNGValidateDefinition(ctxt,
9162
0
                                                   partitions->groups[1]->
9163
0
                                                   rule);
9164
0
            else
9165
0
                ret = xmlRelaxNGValidateDefinition(ctxt,
9166
0
                                                   partitions->groups[0]->
9167
0
                                                   rule);
9168
0
            if (ret == 0) {
9169
0
                if (ctxt->state != NULL)
9170
0
                    ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9171
0
                                                             ctxt->state->
9172
0
                                                             seq);
9173
0
            }
9174
0
            ctxt->flags = oldflags;
9175
0
            return (ret);
9176
0
        }
9177
0
    }
9178
9179
    /*
9180
     * Build arrays to store the first and last node of the chain
9181
     * pertaining to each group
9182
     */
9183
0
    list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9184
0
    if (list == NULL) {
9185
0
        xmlRngVErrMemory(ctxt);
9186
0
        return (-1);
9187
0
    }
9188
0
    memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9189
0
    lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9190
0
    if (lasts == NULL) {
9191
0
        xmlRngVErrMemory(ctxt);
9192
0
        return (-1);
9193
0
    }
9194
0
    memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9195
9196
    /*
9197
     * Walk the sequence of children finding the right group and
9198
     * sorting them in sequences.
9199
     */
9200
0
    cur = ctxt->state->seq;
9201
0
    cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9202
0
    start = cur;
9203
0
    while (cur != NULL) {
9204
0
        ctxt->state->seq = cur;
9205
0
        if ((partitions->triage != NULL) &&
9206
0
            (partitions->flags & IS_DETERMINIST)) {
9207
0
            void *tmp = NULL;
9208
9209
0
            if ((cur->type == XML_TEXT_NODE) ||
9210
0
                (cur->type == XML_CDATA_SECTION_NODE)) {
9211
0
                tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9212
0
                                     NULL);
9213
0
            } else if (cur->type == XML_ELEMENT_NODE) {
9214
0
                if (cur->ns != NULL) {
9215
0
                    tmp = xmlHashLookup2(partitions->triage, cur->name,
9216
0
                                         cur->ns->href);
9217
0
                    if (tmp == NULL)
9218
0
                        tmp = xmlHashLookup2(partitions->triage,
9219
0
                                             BAD_CAST "#any",
9220
0
                                             cur->ns->href);
9221
0
                } else
9222
0
                    tmp =
9223
0
                        xmlHashLookup2(partitions->triage, cur->name,
9224
0
                                       NULL);
9225
0
                if (tmp == NULL)
9226
0
                    tmp =
9227
0
                        xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9228
0
                                       NULL);
9229
0
            }
9230
9231
0
            if (tmp == NULL) {
9232
0
                i = nbgroups;
9233
0
            } else {
9234
0
                i = ((ptrdiff_t) tmp) - 1;
9235
0
                if (partitions->flags & IS_NEEDCHECK) {
9236
0
                    group = partitions->groups[i];
9237
0
                    if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9238
0
                        i = nbgroups;
9239
0
                }
9240
0
            }
9241
0
        } else {
9242
0
            for (i = 0; i < nbgroups; i++) {
9243
0
                group = partitions->groups[i];
9244
0
                if (group == NULL)
9245
0
                    continue;
9246
0
                if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9247
0
                    break;
9248
0
            }
9249
0
        }
9250
        /*
9251
         * We break as soon as an element not matched is found
9252
         */
9253
0
        if (i >= nbgroups) {
9254
0
            break;
9255
0
        }
9256
0
        if (lasts[i] != NULL) {
9257
0
            lasts[i]->next = cur;
9258
0
            lasts[i] = cur;
9259
0
        } else {
9260
0
            list[i] = cur;
9261
0
            lasts[i] = cur;
9262
0
        }
9263
0
        if (cur->next != NULL)
9264
0
            lastchg = cur->next;
9265
0
        else
9266
0
            lastchg = cur;
9267
0
        cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9268
0
    }
9269
0
    if (ret != 0) {
9270
0
        VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9271
0
        ret = -1;
9272
0
        goto done;
9273
0
    }
9274
0
    lastelem = cur;
9275
0
    oldstate = ctxt->state;
9276
0
    for (i = 0; i < nbgroups; i++) {
9277
0
        ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9278
0
  if (ctxt->state == NULL) {
9279
0
      ret = -1;
9280
0
      break;
9281
0
  }
9282
0
        group = partitions->groups[i];
9283
0
        if (lasts[i] != NULL) {
9284
0
            last = lasts[i]->next;
9285
0
            lasts[i]->next = NULL;
9286
0
        }
9287
0
        ctxt->state->seq = list[i];
9288
0
        ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9289
0
        if (ret != 0)
9290
0
            break;
9291
0
        if (ctxt->state != NULL) {
9292
0
            cur = ctxt->state->seq;
9293
0
            cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9294
0
            xmlRelaxNGFreeValidState(ctxt, oldstate);
9295
0
            oldstate = ctxt->state;
9296
0
            ctxt->state = NULL;
9297
0
            if (cur != NULL
9298
                    /* there's a nasty violation of context-free unambiguities,
9299
                       since in open-name-class context, interleave in the
9300
                       production shall finish without caring about anything
9301
                       else that is OK to follow in that case -- it would
9302
                       otherwise get marked as "extra content" and would
9303
                       hence fail the validation, hence this perhaps
9304
                       dirty attempt to rectify such a situation */
9305
0
                    && (define->parent->type != XML_RELAXNG_DEF
9306
0
                        || !xmlStrEqual(define->parent->name,
9307
0
                                        (const xmlChar *) "open-name-class"))) {
9308
0
                VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9309
0
                ret = -1;
9310
0
                ctxt->state = oldstate;
9311
0
                goto done;
9312
0
            }
9313
0
        } else if (ctxt->states != NULL) {
9314
0
            int j;
9315
0
            int found = 0;
9316
0
      int best = -1;
9317
0
      int lowattr = -1;
9318
9319
      /*
9320
       * PBM: what happen if there is attributes checks in the interleaves
9321
       */
9322
9323
0
            for (j = 0; j < ctxt->states->nbState; j++) {
9324
0
                cur = ctxt->states->tabState[j]->seq;
9325
0
                cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9326
0
                if (cur == NULL) {
9327
0
        if (found == 0) {
9328
0
            lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9329
0
      best = j;
9330
0
        }
9331
0
                    found = 1;
9332
0
        if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9333
            /* try  to keep the latest one to mach old heuristic */
9334
0
            lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9335
0
      best = j;
9336
0
        }
9337
0
                    if (lowattr == 0)
9338
0
            break;
9339
0
                } else if (found == 0) {
9340
0
                    if (lowattr == -1) {
9341
0
            lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9342
0
      best = j;
9343
0
        } else
9344
0
        if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr)  {
9345
            /* try  to keep the latest one to mach old heuristic */
9346
0
            lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9347
0
      best = j;
9348
0
        }
9349
0
    }
9350
0
            }
9351
      /*
9352
       * BIG PBM: here we pick only one restarting point :-(
9353
       */
9354
0
            if (ctxt->states->nbState > 0) {
9355
0
                xmlRelaxNGFreeValidState(ctxt, oldstate);
9356
0
    if (best != -1) {
9357
0
        oldstate = ctxt->states->tabState[best];
9358
0
        ctxt->states->tabState[best] = NULL;
9359
0
    } else {
9360
0
        oldstate =
9361
0
      ctxt->states->tabState[ctxt->states->nbState - 1];
9362
0
                    ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
9363
0
                    ctxt->states->nbState--;
9364
0
    }
9365
0
            }
9366
0
            for (j = 0; j < ctxt->states->nbState ; j++) {
9367
0
                xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9368
0
            }
9369
0
            xmlRelaxNGFreeStates(ctxt, ctxt->states);
9370
0
            ctxt->states = NULL;
9371
0
            if (found == 0) {
9372
0
                if (cur == NULL) {
9373
0
        VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9374
0
             (const xmlChar *) "noname");
9375
0
                } else {
9376
0
                    VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9377
0
                }
9378
0
                ret = -1;
9379
0
                ctxt->state = oldstate;
9380
0
                goto done;
9381
0
            }
9382
0
        } else {
9383
0
            ret = -1;
9384
0
            break;
9385
0
        }
9386
0
        if (lasts[i] != NULL) {
9387
0
            lasts[i]->next = last;
9388
0
        }
9389
0
    }
9390
0
    if (ctxt->state != NULL)
9391
0
        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9392
0
    ctxt->state = oldstate;
9393
0
    ctxt->state->seq = lastelem;
9394
0
    if (ret != 0) {
9395
0
        VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9396
0
        ret = -1;
9397
0
        goto done;
9398
0
    }
9399
9400
0
  done:
9401
0
    ctxt->flags = oldflags;
9402
    /*
9403
     * builds the next links chain from the prev one
9404
     */
9405
0
    cur = lastchg;
9406
0
    while (cur != NULL) {
9407
0
        if ((cur == start) || (cur->prev == NULL))
9408
0
            break;
9409
0
        cur->prev->next = cur;
9410
0
        cur = cur->prev;
9411
0
    }
9412
0
    if (ret == 0) {
9413
0
        if (ctxt->errNr > errNr)
9414
0
            xmlRelaxNGPopErrors(ctxt, errNr);
9415
0
    }
9416
9417
0
    xmlFree(list);
9418
0
    xmlFree(lasts);
9419
0
    return (ret);
9420
0
}
9421
9422
/**
9423
 * xmlRelaxNGValidateDefinitionList:
9424
 * @ctxt:  a Relax-NG validation context
9425
 * @define:  the list of definition to verify
9426
 *
9427
 * Validate the given node content against the (list) of definitions
9428
 *
9429
 * Returns 0 if the validation succeeded or an error code.
9430
 */
9431
static int
9432
xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9433
                                 xmlRelaxNGDefinePtr defines)
9434
0
{
9435
0
    int ret = 0, res;
9436
9437
9438
0
    if (defines == NULL) {
9439
0
        VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9440
0
                   BAD_CAST "NULL definition list");
9441
0
        return (-1);
9442
0
    }
9443
0
    while (defines != NULL) {
9444
0
        if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9445
0
            res = xmlRelaxNGValidateDefinition(ctxt, defines);
9446
0
            if (res < 0)
9447
0
                ret = -1;
9448
0
        } else {
9449
0
            VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9450
0
            return (-1);
9451
0
        }
9452
0
        if (res == -1)          /* continues on -2 */
9453
0
            break;
9454
0
        defines = defines->next;
9455
0
    }
9456
9457
0
    return (ret);
9458
0
}
9459
9460
/**
9461
 * xmlRelaxNGElementMatch:
9462
 * @ctxt:  a Relax-NG validation context
9463
 * @define:  the definition to check
9464
 * @elem:  the element
9465
 *
9466
 * Check if the element matches the definition nameClass
9467
 *
9468
 * Returns 1 if the element matches, 0 if no, or -1 in case of error
9469
 */
9470
static int
9471
xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9472
                       xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9473
0
{
9474
0
    int ret = 0, oldflags = 0;
9475
9476
0
    if (define->name != NULL) {
9477
0
        if (!xmlStrEqual(elem->name, define->name)) {
9478
0
            VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9479
0
            return (0);
9480
0
        }
9481
0
    }
9482
0
    if ((define->ns != NULL) && (define->ns[0] != 0)) {
9483
0
        if (elem->ns == NULL) {
9484
0
            VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9485
0
            return (0);
9486
0
        } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9487
0
            VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9488
0
                       elem->name, define->ns);
9489
0
            return (0);
9490
0
        }
9491
0
    } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9492
0
               (define->name == NULL)) {
9493
0
        VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9494
0
        return (0);
9495
0
    } else if ((elem->ns != NULL) && (define->name != NULL)) {
9496
0
        VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9497
0
        return (0);
9498
0
    }
9499
9500
0
    if (define->nameClass == NULL)
9501
0
        return (1);
9502
9503
0
    define = define->nameClass;
9504
0
    if (define->type == XML_RELAXNG_EXCEPT) {
9505
0
        xmlRelaxNGDefinePtr list;
9506
9507
0
        if (ctxt != NULL) {
9508
0
            oldflags = ctxt->flags;
9509
0
            ctxt->flags |= FLAGS_IGNORABLE;
9510
0
        }
9511
9512
0
        list = define->content;
9513
0
        while (list != NULL) {
9514
0
            ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9515
0
            if (ret == 1) {
9516
0
                if (ctxt != NULL)
9517
0
                    ctxt->flags = oldflags;
9518
0
                return (0);
9519
0
            }
9520
0
            if (ret < 0) {
9521
0
                if (ctxt != NULL)
9522
0
                    ctxt->flags = oldflags;
9523
0
                return (ret);
9524
0
            }
9525
0
            list = list->next;
9526
0
        }
9527
0
        ret = 1;
9528
0
        if (ctxt != NULL) {
9529
0
            ctxt->flags = oldflags;
9530
0
        }
9531
0
    } else if (define->type == XML_RELAXNG_CHOICE) {
9532
0
        xmlRelaxNGDefinePtr list;
9533
9534
0
        if (ctxt != NULL) {
9535
0
            oldflags = ctxt->flags;
9536
0
            ctxt->flags |= FLAGS_IGNORABLE;
9537
0
        }
9538
9539
0
        list = define->nameClass;
9540
0
        while (list != NULL) {
9541
0
            ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9542
0
            if (ret == 1) {
9543
0
                if (ctxt != NULL)
9544
0
                    ctxt->flags = oldflags;
9545
0
                return (1);
9546
0
            }
9547
0
            if (ret < 0) {
9548
0
                if (ctxt != NULL)
9549
0
                    ctxt->flags = oldflags;
9550
0
                return (ret);
9551
0
            }
9552
0
            list = list->next;
9553
0
        }
9554
0
        if (ctxt != NULL) {
9555
0
            if (ret != 0) {
9556
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9557
0
                    xmlRelaxNGDumpValidError(ctxt);
9558
0
            } else {
9559
0
                if (ctxt->errNr > 0)
9560
0
                    xmlRelaxNGPopErrors(ctxt, 0);
9561
0
            }
9562
0
        }
9563
0
        ret = 0;
9564
0
        if (ctxt != NULL) {
9565
0
            ctxt->flags = oldflags;
9566
0
        }
9567
0
    } else {
9568
        /* TODO */
9569
0
        ret = -1;
9570
0
    }
9571
0
    return (ret);
9572
0
}
9573
9574
/**
9575
 * xmlRelaxNGBestState:
9576
 * @ctxt:  a Relax-NG validation context
9577
 *
9578
 * Find the "best" state in the ctxt->states list of states to report
9579
 * errors about. I.e. a state with no element left in the child list
9580
 * or the one with the less attributes left.
9581
 * This is called only if a validation error was detected
9582
 *
9583
 * Returns the index of the "best" state or -1 in case of error
9584
 */
9585
static int
9586
xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9587
0
{
9588
0
    xmlRelaxNGValidStatePtr state;
9589
0
    int i, tmp;
9590
0
    int best = -1;
9591
0
    int value = 1000000;
9592
9593
0
    if ((ctxt == NULL) || (ctxt->states == NULL) ||
9594
0
        (ctxt->states->nbState <= 0))
9595
0
        return (-1);
9596
9597
0
    for (i = 0; i < ctxt->states->nbState; i++) {
9598
0
        state = ctxt->states->tabState[i];
9599
0
        if (state == NULL)
9600
0
            continue;
9601
0
        if (state->seq != NULL) {
9602
0
            if ((best == -1) || (value > 100000)) {
9603
0
                value = 100000;
9604
0
                best = i;
9605
0
            }
9606
0
        } else {
9607
0
            tmp = state->nbAttrLeft;
9608
0
            if ((best == -1) || (value > tmp)) {
9609
0
                value = tmp;
9610
0
                best = i;
9611
0
            }
9612
0
        }
9613
0
    }
9614
0
    return (best);
9615
0
}
9616
9617
/**
9618
 * xmlRelaxNGLogBestError:
9619
 * @ctxt:  a Relax-NG validation context
9620
 *
9621
 * Find the "best" state in the ctxt->states list of states to report
9622
 * errors about and log it.
9623
 */
9624
static void
9625
xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9626
0
{
9627
0
    int best;
9628
9629
0
    if ((ctxt == NULL) || (ctxt->states == NULL) ||
9630
0
        (ctxt->states->nbState <= 0))
9631
0
        return;
9632
9633
0
    best = xmlRelaxNGBestState(ctxt);
9634
0
    if ((best >= 0) && (best < ctxt->states->nbState)) {
9635
0
        ctxt->state = ctxt->states->tabState[best];
9636
9637
0
        xmlRelaxNGValidateElementEnd(ctxt, 1);
9638
0
    }
9639
0
}
9640
9641
/**
9642
 * xmlRelaxNGValidateElementEnd:
9643
 * @ctxt:  a Relax-NG validation context
9644
 * @dolog:  indicate that error logging should be done
9645
 *
9646
 * Validate the end of the element, implements check that
9647
 * there is nothing left not consumed in the element content
9648
 * or in the attribute list.
9649
 *
9650
 * Returns 0 if the validation succeeded or an error code.
9651
 */
9652
static int
9653
xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
9654
0
{
9655
0
    int i;
9656
0
    xmlRelaxNGValidStatePtr state;
9657
9658
0
    state = ctxt->state;
9659
0
    if (state->seq != NULL) {
9660
0
        state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9661
0
        if (state->seq != NULL) {
9662
0
            if (dolog) {
9663
0
                VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9664
0
                           state->node->name, state->seq->name);
9665
0
            }
9666
0
            return (-1);
9667
0
        }
9668
0
    }
9669
0
    for (i = 0; i < state->nbAttrs; i++) {
9670
0
        if (state->attrs[i] != NULL) {
9671
0
            if (dolog) {
9672
0
                VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9673
0
                           state->attrs[i]->name, state->node->name);
9674
0
            }
9675
0
            return (-1 - i);
9676
0
        }
9677
0
    }
9678
0
    return (0);
9679
0
}
9680
9681
/**
9682
 * xmlRelaxNGValidateState:
9683
 * @ctxt:  a Relax-NG validation context
9684
 * @define:  the definition to verify
9685
 *
9686
 * Validate the current state against the definition
9687
 *
9688
 * Returns 0 if the validation succeeded or an error code.
9689
 */
9690
static int
9691
xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9692
                        xmlRelaxNGDefinePtr define)
9693
0
{
9694
0
    xmlNodePtr node;
9695
0
    int ret = 0, i, tmp, oldflags, errNr;
9696
0
    xmlRelaxNGValidStatePtr oldstate = NULL, state;
9697
9698
0
    if (define == NULL) {
9699
0
        VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9700
0
        return (-1);
9701
0
    }
9702
9703
0
    if (ctxt->state != NULL) {
9704
0
        node = ctxt->state->seq;
9705
0
    } else {
9706
0
        node = NULL;
9707
0
    }
9708
0
    ctxt->depth++;
9709
0
    switch (define->type) {
9710
0
        case XML_RELAXNG_EMPTY:
9711
0
            ret = 0;
9712
0
            break;
9713
0
        case XML_RELAXNG_NOT_ALLOWED:
9714
0
            ret = -1;
9715
0
            break;
9716
0
        case XML_RELAXNG_TEXT:
9717
0
            while ((node != NULL) &&
9718
0
                   ((node->type == XML_TEXT_NODE) ||
9719
0
                    (node->type == XML_COMMENT_NODE) ||
9720
0
                    (node->type == XML_PI_NODE) ||
9721
0
                    (node->type == XML_CDATA_SECTION_NODE)))
9722
0
                node = node->next;
9723
0
            ctxt->state->seq = node;
9724
0
            break;
9725
0
        case XML_RELAXNG_ELEMENT:
9726
0
            errNr = ctxt->errNr;
9727
0
            node = xmlRelaxNGSkipIgnored(ctxt, node);
9728
0
            if (node == NULL) {
9729
0
                VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9730
0
                ret = -1;
9731
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9732
0
                    xmlRelaxNGDumpValidError(ctxt);
9733
0
                break;
9734
0
            }
9735
0
            if (node->type != XML_ELEMENT_NODE) {
9736
0
                VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9737
0
                ret = -1;
9738
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9739
0
                    xmlRelaxNGDumpValidError(ctxt);
9740
0
                break;
9741
0
            }
9742
            /*
9743
             * This node was already validated successfully against
9744
             * this definition.
9745
             */
9746
0
            if (node->psvi == define) {
9747
0
                ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9748
0
                if (ctxt->errNr > errNr)
9749
0
                    xmlRelaxNGPopErrors(ctxt, errNr);
9750
0
                if (ctxt->errNr != 0) {
9751
0
                    while ((ctxt->err != NULL) &&
9752
0
                           (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9753
0
                             && (xmlStrEqual(ctxt->err->arg2, node->name)))
9754
0
                            ||
9755
0
                            ((ctxt->err->err ==
9756
0
                              XML_RELAXNG_ERR_ELEMEXTRANS)
9757
0
                             && (xmlStrEqual(ctxt->err->arg1, node->name)))
9758
0
                            || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9759
0
                            || (ctxt->err->err ==
9760
0
                                XML_RELAXNG_ERR_NOTELEM)))
9761
0
                        xmlRelaxNGValidErrorPop(ctxt);
9762
0
                }
9763
0
                break;
9764
0
            }
9765
9766
0
            ret = xmlRelaxNGElementMatch(ctxt, define, node);
9767
0
            if (ret <= 0) {
9768
0
                ret = -1;
9769
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9770
0
                    xmlRelaxNGDumpValidError(ctxt);
9771
0
                break;
9772
0
            }
9773
0
            ret = 0;
9774
0
            if (ctxt->errNr != 0) {
9775
0
                if (ctxt->errNr > errNr)
9776
0
                    xmlRelaxNGPopErrors(ctxt, errNr);
9777
0
                while ((ctxt->err != NULL) &&
9778
0
                       (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9779
0
                         (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9780
0
                        ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9781
0
                         (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9782
0
                        (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9783
0
                        (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9784
0
                    xmlRelaxNGValidErrorPop(ctxt);
9785
0
            }
9786
0
            errNr = ctxt->errNr;
9787
9788
0
            oldflags = ctxt->flags;
9789
0
            if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9790
0
                ctxt->flags -= FLAGS_MIXED_CONTENT;
9791
0
            }
9792
0
            state = xmlRelaxNGNewValidState(ctxt, node);
9793
0
            if (state == NULL) {
9794
0
                ret = -1;
9795
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9796
0
                    xmlRelaxNGDumpValidError(ctxt);
9797
0
                break;
9798
0
            }
9799
9800
0
            oldstate = ctxt->state;
9801
0
            ctxt->state = state;
9802
0
            if (define->attrs != NULL) {
9803
0
                tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9804
0
                if (tmp != 0) {
9805
0
                    ret = -1;
9806
0
                    VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9807
0
                }
9808
0
            }
9809
0
            if (define->contModel != NULL) {
9810
0
                xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9811
0
                xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9812
0
                xmlNodePtr nseq;
9813
9814
0
                nstate = xmlRelaxNGNewValidState(ctxt, node);
9815
0
                ctxt->state = nstate;
9816
0
                ctxt->states = NULL;
9817
9818
0
                tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9819
0
                                                        define->contModel,
9820
0
                                                        ctxt->state->seq);
9821
0
                nseq = ctxt->state->seq;
9822
0
                ctxt->state = tmpstate;
9823
0
                ctxt->states = tmpstates;
9824
0
                xmlRelaxNGFreeValidState(ctxt, nstate);
9825
9826
0
                if (tmp != 0)
9827
0
                    ret = -1;
9828
9829
0
                if (ctxt->states != NULL) {
9830
0
                    tmp = -1;
9831
9832
0
                    for (i = 0; i < ctxt->states->nbState; i++) {
9833
0
                        state = ctxt->states->tabState[i];
9834
0
                        ctxt->state = state;
9835
0
                        ctxt->state->seq = nseq;
9836
9837
0
                        if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
9838
0
                            tmp = 0;
9839
0
                            break;
9840
0
                        }
9841
0
                    }
9842
0
                    if (tmp != 0) {
9843
                        /*
9844
                         * validation error, log the message for the "best" one
9845
                         */
9846
0
                        ctxt->flags |= FLAGS_IGNORABLE;
9847
0
                        xmlRelaxNGLogBestError(ctxt);
9848
0
                    }
9849
0
                    for (i = 0; i < ctxt->states->nbState; i++) {
9850
0
                        xmlRelaxNGFreeValidState(ctxt,
9851
0
                                                 ctxt->states->
9852
0
                                                 tabState[i]);
9853
0
                    }
9854
0
                    xmlRelaxNGFreeStates(ctxt, ctxt->states);
9855
0
                    ctxt->flags = oldflags;
9856
0
                    ctxt->states = NULL;
9857
0
                    if ((ret == 0) && (tmp == -1))
9858
0
                        ret = -1;
9859
0
                } else {
9860
0
                    state = ctxt->state;
9861
0
        if (ctxt->state != NULL)
9862
0
      ctxt->state->seq = nseq;
9863
0
                    if (ret == 0)
9864
0
                        ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
9865
0
                    xmlRelaxNGFreeValidState(ctxt, state);
9866
0
                }
9867
0
            } else {
9868
0
                if (define->content != NULL) {
9869
0
                    tmp = xmlRelaxNGValidateDefinitionList(ctxt,
9870
0
                                                           define->
9871
0
                                                           content);
9872
0
                    if (tmp != 0) {
9873
0
                        ret = -1;
9874
0
                        if (ctxt->state == NULL) {
9875
0
                            ctxt->state = oldstate;
9876
0
                            VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9877
0
                                       node->name);
9878
0
                            ctxt->state = NULL;
9879
0
                        } else {
9880
0
                            VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9881
0
                                       node->name);
9882
0
                        }
9883
9884
0
                    }
9885
0
                }
9886
0
                if (ctxt->states != NULL) {
9887
0
                    tmp = -1;
9888
9889
0
                    for (i = 0; i < ctxt->states->nbState; i++) {
9890
0
                        state = ctxt->states->tabState[i];
9891
0
                        ctxt->state = state;
9892
9893
0
                        if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
9894
0
                            tmp = 0;
9895
0
                            break;
9896
0
                        }
9897
0
                    }
9898
0
                    if (tmp != 0) {
9899
                        /*
9900
                         * validation error, log the message for the "best" one
9901
                         */
9902
0
                        ctxt->flags |= FLAGS_IGNORABLE;
9903
0
                        xmlRelaxNGLogBestError(ctxt);
9904
0
                    }
9905
0
                    for (i = 0; i < ctxt->states->nbState; i++) {
9906
0
                        xmlRelaxNGFreeValidState(ctxt,
9907
0
                                                 ctxt->states->tabState[i]);
9908
0
                        ctxt->states->tabState[i] = NULL;
9909
0
                    }
9910
0
                    xmlRelaxNGFreeStates(ctxt, ctxt->states);
9911
0
                    ctxt->flags = oldflags;
9912
0
                    ctxt->states = NULL;
9913
0
                    if ((ret == 0) && (tmp == -1))
9914
0
                        ret = -1;
9915
0
                } else {
9916
0
                    state = ctxt->state;
9917
0
                    if (ret == 0)
9918
0
                        ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
9919
0
                    xmlRelaxNGFreeValidState(ctxt, state);
9920
0
                }
9921
0
            }
9922
0
            if (ret == 0) {
9923
0
                node->psvi = define;
9924
0
            }
9925
0
            ctxt->flags = oldflags;
9926
0
            ctxt->state = oldstate;
9927
0
            if (oldstate != NULL)
9928
0
                oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9929
0
            if (ret != 0) {
9930
0
                if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9931
0
                    xmlRelaxNGDumpValidError(ctxt);
9932
0
                    ret = 0;
9933
#if 0
9934
                } else {
9935
                    ret = -2;
9936
#endif
9937
0
                }
9938
0
            } else {
9939
0
                if (ctxt->errNr > errNr)
9940
0
                    xmlRelaxNGPopErrors(ctxt, errNr);
9941
0
            }
9942
9943
0
            break;
9944
0
        case XML_RELAXNG_OPTIONAL:{
9945
0
                errNr = ctxt->errNr;
9946
0
                oldflags = ctxt->flags;
9947
0
                ctxt->flags |= FLAGS_IGNORABLE;
9948
0
                oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9949
0
                ret =
9950
0
                    xmlRelaxNGValidateDefinitionList(ctxt,
9951
0
                                                     define->content);
9952
0
                if (ret != 0) {
9953
0
                    if (ctxt->state != NULL)
9954
0
                        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9955
0
                    ctxt->state = oldstate;
9956
0
                    ctxt->flags = oldflags;
9957
0
                    ret = 0;
9958
0
                    if (ctxt->errNr > errNr)
9959
0
                        xmlRelaxNGPopErrors(ctxt, errNr);
9960
0
                    break;
9961
0
                }
9962
0
                if (ctxt->states != NULL) {
9963
0
                    xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9964
0
                } else {
9965
0
                    ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9966
0
                    if (ctxt->states == NULL) {
9967
0
                        xmlRelaxNGFreeValidState(ctxt, oldstate);
9968
0
                        ctxt->flags = oldflags;
9969
0
                        ret = -1;
9970
0
                        if (ctxt->errNr > errNr)
9971
0
                            xmlRelaxNGPopErrors(ctxt, errNr);
9972
0
                        break;
9973
0
                    }
9974
0
                    xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9975
0
                    xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9976
0
                    ctxt->state = NULL;
9977
0
                }
9978
0
                ctxt->flags = oldflags;
9979
0
                ret = 0;
9980
0
                if (ctxt->errNr > errNr)
9981
0
                    xmlRelaxNGPopErrors(ctxt, errNr);
9982
0
                break;
9983
0
            }
9984
0
        case XML_RELAXNG_ONEORMORE:
9985
0
            errNr = ctxt->errNr;
9986
0
            ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9987
0
            if (ret != 0) {
9988
0
                break;
9989
0
            }
9990
0
            if (ctxt->errNr > errNr)
9991
0
                xmlRelaxNGPopErrors(ctxt, errNr);
9992
            /* Falls through. */
9993
0
        case XML_RELAXNG_ZEROORMORE:{
9994
0
                int progress;
9995
0
                xmlRelaxNGStatesPtr states = NULL, res = NULL;
9996
0
                int base, j;
9997
9998
0
                errNr = ctxt->errNr;
9999
0
                res = xmlRelaxNGNewStates(ctxt, 1);
10000
0
                if (res == NULL) {
10001
0
                    ret = -1;
10002
0
                    break;
10003
0
                }
10004
                /*
10005
                 * All the input states are also exit states
10006
                 */
10007
0
                if (ctxt->state != NULL) {
10008
0
                    xmlRelaxNGAddStates(ctxt, res,
10009
0
                                        xmlRelaxNGCopyValidState(ctxt,
10010
0
                                                                 ctxt->
10011
0
                                                                 state));
10012
0
                } else {
10013
0
                    for (j = 0; j < ctxt->states->nbState; j++) {
10014
0
                        xmlRelaxNGAddStates(ctxt, res,
10015
0
                            xmlRelaxNGCopyValidState(ctxt,
10016
0
                                            ctxt->states->tabState[j]));
10017
0
                    }
10018
0
                }
10019
0
                oldflags = ctxt->flags;
10020
0
                ctxt->flags |= FLAGS_IGNORABLE;
10021
0
                do {
10022
0
                    progress = 0;
10023
0
                    base = res->nbState;
10024
10025
0
                    if (ctxt->states != NULL) {
10026
0
                        states = ctxt->states;
10027
0
                        for (i = 0; i < states->nbState; i++) {
10028
0
                            ctxt->state = states->tabState[i];
10029
0
                            ctxt->states = NULL;
10030
0
                            ret = xmlRelaxNGValidateDefinitionList(ctxt,
10031
0
                                                                   define->
10032
0
                                                                   content);
10033
0
                            if (ret == 0) {
10034
0
                                if (ctxt->state != NULL) {
10035
0
                                    tmp = xmlRelaxNGAddStates(ctxt, res,
10036
0
                                                              ctxt->state);
10037
0
                                    ctxt->state = NULL;
10038
0
                                    if (tmp == 1)
10039
0
                                        progress = 1;
10040
0
                                } else if (ctxt->states != NULL) {
10041
0
                                    for (j = 0; j < ctxt->states->nbState;
10042
0
                                         j++) {
10043
0
                                        tmp =
10044
0
                                            xmlRelaxNGAddStates(ctxt, res,
10045
0
                                                   ctxt->states->tabState[j]);
10046
0
                                        if (tmp == 1)
10047
0
                                            progress = 1;
10048
0
                                    }
10049
0
                                    xmlRelaxNGFreeStates(ctxt,
10050
0
                                                         ctxt->states);
10051
0
                                    ctxt->states = NULL;
10052
0
                                }
10053
0
                            } else {
10054
0
                                if (ctxt->state != NULL) {
10055
0
                                    xmlRelaxNGFreeValidState(ctxt,
10056
0
                                                             ctxt->state);
10057
0
                                    ctxt->state = NULL;
10058
0
                                }
10059
0
                            }
10060
0
                        }
10061
0
                    } else {
10062
0
                        ret = xmlRelaxNGValidateDefinitionList(ctxt,
10063
0
                                                               define->
10064
0
                                                               content);
10065
0
                        if (ret != 0) {
10066
0
                            xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10067
0
                            ctxt->state = NULL;
10068
0
                        } else {
10069
0
                            base = res->nbState;
10070
0
                            if (ctxt->state != NULL) {
10071
0
                                tmp = xmlRelaxNGAddStates(ctxt, res,
10072
0
                                                          ctxt->state);
10073
0
                                ctxt->state = NULL;
10074
0
                                if (tmp == 1)
10075
0
                                    progress = 1;
10076
0
                            } else if (ctxt->states != NULL) {
10077
0
                                for (j = 0; j < ctxt->states->nbState; j++) {
10078
0
                                    tmp = xmlRelaxNGAddStates(ctxt, res,
10079
0
                                               ctxt->states->tabState[j]);
10080
0
                                    if (tmp == 1)
10081
0
                                        progress = 1;
10082
0
                                }
10083
0
                                if (states == NULL) {
10084
0
                                    states = ctxt->states;
10085
0
                                } else {
10086
0
                                    xmlRelaxNGFreeStates(ctxt,
10087
0
                                                         ctxt->states);
10088
0
                                }
10089
0
                                ctxt->states = NULL;
10090
0
                            }
10091
0
                        }
10092
0
                    }
10093
0
                    if (progress) {
10094
                        /*
10095
                         * Collect all the new nodes added at that step
10096
                         * and make them the new node set
10097
                         */
10098
0
                        if (res->nbState - base == 1) {
10099
0
                            ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10100
0
                                                                   res->
10101
0
                                                                   tabState
10102
0
                                                                   [base]);
10103
0
                        } else {
10104
0
                            if (states == NULL) {
10105
0
                                xmlRelaxNGNewStates(ctxt,
10106
0
                                                    res->nbState - base);
10107
0
              states = ctxt->states;
10108
0
        if (states == NULL) {
10109
0
            progress = 0;
10110
0
            break;
10111
0
        }
10112
0
                            }
10113
0
                            states->nbState = 0;
10114
0
                            for (i = base; i < res->nbState; i++)
10115
0
                                xmlRelaxNGAddStates(ctxt, states,
10116
0
                                                    xmlRelaxNGCopyValidState
10117
0
                                                    (ctxt, res->tabState[i]));
10118
0
                            ctxt->states = states;
10119
0
                        }
10120
0
                    }
10121
0
                } while (progress == 1);
10122
0
                if (states != NULL) {
10123
0
                    xmlRelaxNGFreeStates(ctxt, states);
10124
0
                }
10125
0
                ctxt->states = res;
10126
0
                ctxt->flags = oldflags;
10127
#if 0
10128
                /*
10129
                 * errors may have to be propagated back...
10130
                 */
10131
                if (ctxt->errNr > errNr)
10132
                    xmlRelaxNGPopErrors(ctxt, errNr);
10133
#endif
10134
0
                ret = 0;
10135
0
                break;
10136
0
            }
10137
0
        case XML_RELAXNG_CHOICE:{
10138
0
                xmlRelaxNGDefinePtr list = NULL;
10139
0
                xmlRelaxNGStatesPtr states = NULL;
10140
10141
0
                node = xmlRelaxNGSkipIgnored(ctxt, node);
10142
10143
0
                errNr = ctxt->errNr;
10144
0
                if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10145
0
        (node != NULL)) {
10146
        /*
10147
         * node == NULL can't be optimized since IS_TRIABLE
10148
         * doesn't account for choice which may lead to
10149
         * only attributes.
10150
         */
10151
0
                    xmlHashTablePtr triage =
10152
0
                        (xmlHashTablePtr) define->data;
10153
10154
                    /*
10155
                     * Something we can optimize cleanly there is only one
10156
                     * possible branch out !
10157
                     */
10158
0
                    if ((node->type == XML_TEXT_NODE) ||
10159
0
                        (node->type == XML_CDATA_SECTION_NODE)) {
10160
0
                        list =
10161
0
                            xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10162
0
                    } else if (node->type == XML_ELEMENT_NODE) {
10163
0
                        if (node->ns != NULL) {
10164
0
                            list = xmlHashLookup2(triage, node->name,
10165
0
                                                  node->ns->href);
10166
0
                            if (list == NULL)
10167
0
                                list =
10168
0
                                    xmlHashLookup2(triage, BAD_CAST "#any",
10169
0
                                                   node->ns->href);
10170
0
                        } else
10171
0
                            list =
10172
0
                                xmlHashLookup2(triage, node->name, NULL);
10173
0
                        if (list == NULL)
10174
0
                            list =
10175
0
                                xmlHashLookup2(triage, BAD_CAST "#any",
10176
0
                                               NULL);
10177
0
                    }
10178
0
                    if (list == NULL) {
10179
0
                        ret = -1;
10180
0
      VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
10181
0
                        break;
10182
0
                    }
10183
0
                    ret = xmlRelaxNGValidateDefinition(ctxt, list);
10184
0
                    if (ret == 0) {
10185
0
                    }
10186
0
                    break;
10187
0
                }
10188
10189
0
                list = define->content;
10190
0
                oldflags = ctxt->flags;
10191
0
                ctxt->flags |= FLAGS_IGNORABLE;
10192
10193
0
                while (list != NULL) {
10194
0
                    oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10195
0
                    ret = xmlRelaxNGValidateDefinition(ctxt, list);
10196
0
                    if (ret == 0) {
10197
0
                        if (states == NULL) {
10198
0
                            states = xmlRelaxNGNewStates(ctxt, 1);
10199
0
                        }
10200
0
                        if (ctxt->state != NULL) {
10201
0
                            xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10202
0
                        } else if (ctxt->states != NULL) {
10203
0
                            for (i = 0; i < ctxt->states->nbState; i++) {
10204
0
                                xmlRelaxNGAddStates(ctxt, states,
10205
0
                                                    ctxt->states->
10206
0
                                                    tabState[i]);
10207
0
                            }
10208
0
                            xmlRelaxNGFreeStates(ctxt, ctxt->states);
10209
0
                            ctxt->states = NULL;
10210
0
                        }
10211
0
                    } else {
10212
0
                        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10213
0
                    }
10214
0
                    ctxt->state = oldstate;
10215
0
                    list = list->next;
10216
0
                }
10217
0
                if (states != NULL) {
10218
0
                    xmlRelaxNGFreeValidState(ctxt, oldstate);
10219
0
                    ctxt->states = states;
10220
0
                    ctxt->state = NULL;
10221
0
                    ret = 0;
10222
0
                } else {
10223
0
                    ctxt->states = NULL;
10224
0
                }
10225
0
                ctxt->flags = oldflags;
10226
0
                if (ret != 0) {
10227
0
                    if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10228
0
                        xmlRelaxNGDumpValidError(ctxt);
10229
0
                    }
10230
0
                } else {
10231
0
                    if (ctxt->errNr > errNr)
10232
0
                        xmlRelaxNGPopErrors(ctxt, errNr);
10233
0
                }
10234
0
                break;
10235
0
            }
10236
0
        case XML_RELAXNG_DEF:
10237
0
        case XML_RELAXNG_GROUP:
10238
0
            ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10239
0
            break;
10240
0
        case XML_RELAXNG_INTERLEAVE:
10241
0
            ret = xmlRelaxNGValidateInterleave(ctxt, define);
10242
0
            break;
10243
0
        case XML_RELAXNG_ATTRIBUTE:
10244
0
            ret = xmlRelaxNGValidateAttribute(ctxt, define);
10245
0
            break;
10246
0
        case XML_RELAXNG_START:
10247
0
        case XML_RELAXNG_NOOP:
10248
0
        case XML_RELAXNG_REF:
10249
0
        case XML_RELAXNG_EXTERNALREF:
10250
0
        case XML_RELAXNG_PARENTREF:
10251
0
            ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10252
0
            break;
10253
0
        case XML_RELAXNG_DATATYPE:{
10254
0
                xmlNodePtr child;
10255
0
                xmlChar *content = NULL;
10256
10257
0
                child = node;
10258
0
                while (child != NULL) {
10259
0
                    if (child->type == XML_ELEMENT_NODE) {
10260
0
                        VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10261
0
                                   node->parent->name);
10262
0
                        ret = -1;
10263
0
                        break;
10264
0
                    } else if ((child->type == XML_TEXT_NODE) ||
10265
0
                               (child->type == XML_CDATA_SECTION_NODE)) {
10266
0
                        content = xmlStrcat(content, child->content);
10267
0
                    }
10268
                    /* TODO: handle entities ... */
10269
0
                    child = child->next;
10270
0
                }
10271
0
                if (ret == -1) {
10272
0
                    if (content != NULL)
10273
0
                        xmlFree(content);
10274
0
                    break;
10275
0
                }
10276
0
                if (content == NULL) {
10277
0
                    content = xmlStrdup(BAD_CAST "");
10278
0
                    if (content == NULL) {
10279
0
                        xmlRngVErrMemory(ctxt);
10280
0
                        ret = -1;
10281
0
                        break;
10282
0
                    }
10283
0
                }
10284
0
                ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10285
0
                                                 ctxt->state->seq);
10286
0
                if (ret == -1) {
10287
0
                    VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10288
0
                } else if (ret == 0) {
10289
0
                    ctxt->state->seq = NULL;
10290
0
                }
10291
0
                if (content != NULL)
10292
0
                    xmlFree(content);
10293
0
                break;
10294
0
            }
10295
0
        case XML_RELAXNG_VALUE:{
10296
0
                xmlChar *content = NULL;
10297
0
                xmlChar *oldvalue;
10298
0
                xmlNodePtr child;
10299
10300
0
                child = node;
10301
0
                while (child != NULL) {
10302
0
                    if (child->type == XML_ELEMENT_NODE) {
10303
0
                        VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10304
0
                                   node->parent->name);
10305
0
                        ret = -1;
10306
0
                        break;
10307
0
                    } else if ((child->type == XML_TEXT_NODE) ||
10308
0
                               (child->type == XML_CDATA_SECTION_NODE)) {
10309
0
                        content = xmlStrcat(content, child->content);
10310
0
                    }
10311
                    /* TODO: handle entities ... */
10312
0
                    child = child->next;
10313
0
                }
10314
0
                if (ret == -1) {
10315
0
                    if (content != NULL)
10316
0
                        xmlFree(content);
10317
0
                    break;
10318
0
                }
10319
0
                if (content == NULL) {
10320
0
                    content = xmlStrdup(BAD_CAST "");
10321
0
                    if (content == NULL) {
10322
0
                        xmlRngVErrMemory(ctxt);
10323
0
                        ret = -1;
10324
0
                        break;
10325
0
                    }
10326
0
                }
10327
0
                oldvalue = ctxt->state->value;
10328
0
                ctxt->state->value = content;
10329
0
                ret = xmlRelaxNGValidateValue(ctxt, define);
10330
0
                ctxt->state->value = oldvalue;
10331
0
                if (ret == -1) {
10332
0
                    VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10333
0
                } else if (ret == 0) {
10334
0
                    ctxt->state->seq = NULL;
10335
0
                }
10336
0
                if (content != NULL)
10337
0
                    xmlFree(content);
10338
0
                break;
10339
0
            }
10340
0
        case XML_RELAXNG_LIST:{
10341
0
                xmlChar *content;
10342
0
                xmlNodePtr child;
10343
0
                xmlChar *oldvalue, *oldendvalue;
10344
0
                int len;
10345
10346
                /*
10347
                 * Make sure it's only text nodes
10348
                 */
10349
10350
0
                content = NULL;
10351
0
                child = node;
10352
0
                while (child != NULL) {
10353
0
                    if (child->type == XML_ELEMENT_NODE) {
10354
0
                        VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10355
0
                                   node->parent->name);
10356
0
                        ret = -1;
10357
0
                        break;
10358
0
                    } else if ((child->type == XML_TEXT_NODE) ||
10359
0
                               (child->type == XML_CDATA_SECTION_NODE)) {
10360
0
                        content = xmlStrcat(content, child->content);
10361
0
                    }
10362
                    /* TODO: handle entities ... */
10363
0
                    child = child->next;
10364
0
                }
10365
0
                if (ret == -1) {
10366
0
                    if (content != NULL)
10367
0
                        xmlFree(content);
10368
0
                    break;
10369
0
                }
10370
0
                if (content == NULL) {
10371
0
                    content = xmlStrdup(BAD_CAST "");
10372
0
                    if (content == NULL) {
10373
0
                        xmlRngVErrMemory(ctxt);
10374
0
                        ret = -1;
10375
0
                        break;
10376
0
                    }
10377
0
                }
10378
0
                len = xmlStrlen(content);
10379
0
                oldvalue = ctxt->state->value;
10380
0
                oldendvalue = ctxt->state->endvalue;
10381
0
                ctxt->state->value = content;
10382
0
                ctxt->state->endvalue = content + len;
10383
0
                ret = xmlRelaxNGValidateValue(ctxt, define);
10384
0
                ctxt->state->value = oldvalue;
10385
0
                ctxt->state->endvalue = oldendvalue;
10386
0
                if (ret == -1) {
10387
0
                    VALID_ERR(XML_RELAXNG_ERR_LIST);
10388
0
                } else if ((ret == 0) && (node != NULL)) {
10389
0
                    ctxt->state->seq = node->next;
10390
0
                }
10391
0
                if (content != NULL)
10392
0
                    xmlFree(content);
10393
0
                break;
10394
0
            }
10395
0
        case XML_RELAXNG_EXCEPT:
10396
0
        case XML_RELAXNG_PARAM:
10397
            /* TODO */
10398
0
            ret = -1;
10399
0
            break;
10400
0
    }
10401
0
    ctxt->depth--;
10402
0
    return (ret);
10403
0
}
10404
10405
/**
10406
 * xmlRelaxNGValidateDefinition:
10407
 * @ctxt:  a Relax-NG validation context
10408
 * @define:  the definition to verify
10409
 *
10410
 * Validate the current node lists against the definition
10411
 *
10412
 * Returns 0 if the validation succeeded or an error code.
10413
 */
10414
static int
10415
xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10416
                             xmlRelaxNGDefinePtr define)
10417
0
{
10418
0
    xmlRelaxNGStatesPtr states, res;
10419
0
    int i, j, k, ret, oldflags;
10420
10421
    /*
10422
     * We should NOT have both ctxt->state and ctxt->states
10423
     */
10424
0
    if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10425
        /* TODO */
10426
0
        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10427
0
        ctxt->state = NULL;
10428
0
    }
10429
10430
0
    if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10431
0
        if (ctxt->states != NULL) {
10432
0
            ctxt->state = ctxt->states->tabState[0];
10433
0
            xmlRelaxNGFreeStates(ctxt, ctxt->states);
10434
0
            ctxt->states = NULL;
10435
0
        }
10436
0
        ret = xmlRelaxNGValidateState(ctxt, define);
10437
0
        if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10438
            /* TODO */
10439
0
            xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10440
0
            ctxt->state = NULL;
10441
0
        }
10442
0
        if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10443
0
            ctxt->state = ctxt->states->tabState[0];
10444
0
            xmlRelaxNGFreeStates(ctxt, ctxt->states);
10445
0
            ctxt->states = NULL;
10446
0
        }
10447
0
        return (ret);
10448
0
    }
10449
10450
0
    states = ctxt->states;
10451
0
    ctxt->states = NULL;
10452
0
    res = NULL;
10453
0
    j = 0;
10454
0
    oldflags = ctxt->flags;
10455
0
    ctxt->flags |= FLAGS_IGNORABLE;
10456
0
    for (i = 0; i < states->nbState; i++) {
10457
0
        ctxt->state = states->tabState[i];
10458
0
        ctxt->states = NULL;
10459
0
        ret = xmlRelaxNGValidateState(ctxt, define);
10460
        /*
10461
         * We should NOT have both ctxt->state and ctxt->states
10462
         */
10463
0
        if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10464
            /* TODO */
10465
0
            xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10466
0
            ctxt->state = NULL;
10467
0
        }
10468
0
        if (ret == 0) {
10469
0
            if (ctxt->states == NULL) {
10470
0
                if (res != NULL) {
10471
                    /* add the state to the container */
10472
0
                    xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10473
0
                    ctxt->state = NULL;
10474
0
                } else {
10475
                    /* add the state directly in states */
10476
0
                    states->tabState[j++] = ctxt->state;
10477
0
                    ctxt->state = NULL;
10478
0
                }
10479
0
            } else {
10480
0
                if (res == NULL) {
10481
                    /* make it the new container and copy other results */
10482
0
                    res = ctxt->states;
10483
0
                    ctxt->states = NULL;
10484
0
                    for (k = 0; k < j; k++)
10485
0
                        xmlRelaxNGAddStates(ctxt, res,
10486
0
                                            states->tabState[k]);
10487
0
                } else {
10488
                    /* add all the new results to res and reff the container */
10489
0
                    for (k = 0; k < ctxt->states->nbState; k++)
10490
0
                        xmlRelaxNGAddStates(ctxt, res,
10491
0
                                            ctxt->states->tabState[k]);
10492
0
                    xmlRelaxNGFreeStates(ctxt, ctxt->states);
10493
0
                    ctxt->states = NULL;
10494
0
                }
10495
0
            }
10496
0
        } else {
10497
0
            if (ctxt->state != NULL) {
10498
0
                xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10499
0
                ctxt->state = NULL;
10500
0
            } else if (ctxt->states != NULL) {
10501
0
                for (k = 0; k < ctxt->states->nbState; k++)
10502
0
                    xmlRelaxNGFreeValidState(ctxt,
10503
0
                                             ctxt->states->tabState[k]);
10504
0
                xmlRelaxNGFreeStates(ctxt, ctxt->states);
10505
0
                ctxt->states = NULL;
10506
0
            }
10507
0
        }
10508
0
    }
10509
0
    ctxt->flags = oldflags;
10510
0
    if (res != NULL) {
10511
0
        xmlRelaxNGFreeStates(ctxt, states);
10512
0
        ctxt->states = res;
10513
0
        ret = 0;
10514
0
    } else if (j > 1) {
10515
0
        states->nbState = j;
10516
0
        ctxt->states = states;
10517
0
        ret = 0;
10518
0
    } else if (j == 1) {
10519
0
        ctxt->state = states->tabState[0];
10520
0
        xmlRelaxNGFreeStates(ctxt, states);
10521
0
        ret = 0;
10522
0
    } else {
10523
0
        ret = -1;
10524
0
        xmlRelaxNGFreeStates(ctxt, states);
10525
0
        if (ctxt->states != NULL) {
10526
0
            xmlRelaxNGFreeStates(ctxt, ctxt->states);
10527
0
            ctxt->states = NULL;
10528
0
        }
10529
0
    }
10530
0
    if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10531
        /* TODO */
10532
0
        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10533
0
        ctxt->state = NULL;
10534
0
    }
10535
0
    return (ret);
10536
0
}
10537
10538
/**
10539
 * xmlRelaxNGValidateDocument:
10540
 * @ctxt:  a Relax-NG validation context
10541
 * @doc:  the document
10542
 *
10543
 * Validate the given document
10544
 *
10545
 * Returns 0 if the validation succeeded or an error code.
10546
 */
10547
static int
10548
xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10549
0
{
10550
0
    int ret;
10551
0
    xmlRelaxNGPtr schema;
10552
0
    xmlRelaxNGGrammarPtr grammar;
10553
0
    xmlRelaxNGValidStatePtr state;
10554
0
    xmlNodePtr node;
10555
10556
0
    if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10557
0
        return (-1);
10558
10559
0
    ctxt->errNo = XML_RELAXNG_OK;
10560
0
    schema = ctxt->schema;
10561
0
    grammar = schema->topgrammar;
10562
0
    if (grammar == NULL) {
10563
0
        VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10564
0
        return (-1);
10565
0
    }
10566
0
    state = xmlRelaxNGNewValidState(ctxt, NULL);
10567
0
    ctxt->state = state;
10568
0
    ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
10569
0
    if ((ctxt->state != NULL) && (state->seq != NULL)) {
10570
0
        state = ctxt->state;
10571
0
        node = state->seq;
10572
0
        node = xmlRelaxNGSkipIgnored(ctxt, node);
10573
0
        if (node != NULL) {
10574
0
            if (ret != -1) {
10575
0
                VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10576
0
                ret = -1;
10577
0
            }
10578
0
        }
10579
0
    } else if (ctxt->states != NULL) {
10580
0
        int i;
10581
0
        int tmp = -1;
10582
10583
0
        for (i = 0; i < ctxt->states->nbState; i++) {
10584
0
            state = ctxt->states->tabState[i];
10585
0
            node = state->seq;
10586
0
            node = xmlRelaxNGSkipIgnored(ctxt, node);
10587
0
            if (node == NULL)
10588
0
                tmp = 0;
10589
0
            xmlRelaxNGFreeValidState(ctxt, state);
10590
0
        }
10591
0
        if (tmp == -1) {
10592
0
            if (ret != -1) {
10593
0
                VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10594
0
                ret = -1;
10595
0
            }
10596
0
        }
10597
0
    }
10598
0
    if (ctxt->state != NULL) {
10599
0
        xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10600
0
        ctxt->state = NULL;
10601
0
    }
10602
0
    if (ret != 0)
10603
0
        xmlRelaxNGDumpValidError(ctxt);
10604
0
#ifdef LIBXML_VALID_ENABLED
10605
0
    if (ctxt->idref == 1) {
10606
0
        xmlValidCtxt vctxt;
10607
10608
0
        memset(&vctxt, 0, sizeof(xmlValidCtxt));
10609
0
        vctxt.valid = 1;
10610
10611
0
        if (ctxt->error == NULL) {
10612
0
            vctxt.error = xmlGenericError;
10613
0
            vctxt.warning = xmlGenericError;
10614
0
            vctxt.userData = xmlGenericErrorContext;
10615
0
        } else {
10616
0
            vctxt.error = ctxt->error;
10617
0
            vctxt.warning = ctxt->warning;
10618
0
            vctxt.userData = ctxt->userData;
10619
0
        }
10620
10621
0
        if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10622
0
            ret = -1;
10623
0
    }
10624
0
#endif /* LIBXML_VALID_ENABLED */
10625
0
    if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10626
0
        ret = -1;
10627
10628
0
    return (ret);
10629
0
}
10630
10631
/**
10632
 * xmlRelaxNGCleanPSVI:
10633
 * @node:  an input element or document
10634
 *
10635
 * Call this routine to speed up XPath computation on static documents.
10636
 * This stamps all the element nodes with the document order
10637
 * Like for line information, the order is kept in the element->content
10638
 * field, the value stored is actually - the node number (starting at -1)
10639
 * to be able to differentiate from line numbers.
10640
 *
10641
 * Returns the number of elements found in the document or -1 in case
10642
 *    of error.
10643
 */
10644
static void
10645
0
xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10646
0
    xmlNodePtr cur;
10647
10648
0
    if ((node == NULL) ||
10649
0
        ((node->type != XML_ELEMENT_NODE) &&
10650
0
         (node->type != XML_DOCUMENT_NODE) &&
10651
0
         (node->type != XML_HTML_DOCUMENT_NODE)))
10652
0
  return;
10653
0
    if (node->type == XML_ELEMENT_NODE)
10654
0
        node->psvi = NULL;
10655
10656
0
    cur = node->children;
10657
0
    while (cur != NULL) {
10658
0
  if (cur->type == XML_ELEMENT_NODE) {
10659
0
      cur->psvi = NULL;
10660
0
      if (cur->children != NULL) {
10661
0
    cur = cur->children;
10662
0
    continue;
10663
0
      }
10664
0
  }
10665
0
  if (cur->next != NULL) {
10666
0
      cur = cur->next;
10667
0
      continue;
10668
0
  }
10669
0
  do {
10670
0
      cur = cur->parent;
10671
0
      if (cur == NULL)
10672
0
    break;
10673
0
      if (cur == node) {
10674
0
    cur = NULL;
10675
0
    break;
10676
0
      }
10677
0
      if (cur->next != NULL) {
10678
0
    cur = cur->next;
10679
0
    break;
10680
0
      }
10681
0
  } while (cur != NULL);
10682
0
    }
10683
0
}
10684
/************************************************************************
10685
 *                  *
10686
 *      Validation interfaces       *
10687
 *                  *
10688
 ************************************************************************/
10689
10690
/**
10691
 * xmlRelaxNGNewValidCtxt:
10692
 * @schema:  a precompiled XML RelaxNGs
10693
 *
10694
 * Create an XML RelaxNGs validation context based on the given schema
10695
 *
10696
 * Returns the validation context or NULL in case of error
10697
 */
10698
xmlRelaxNGValidCtxtPtr
10699
xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10700
0
{
10701
0
    xmlRelaxNGValidCtxtPtr ret;
10702
10703
0
    ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10704
0
    if (ret == NULL) {
10705
0
        xmlRngVErrMemory(NULL);
10706
0
        return (NULL);
10707
0
    }
10708
0
    memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10709
0
    ret->schema = schema;
10710
0
    ret->errNr = 0;
10711
0
    ret->errMax = 0;
10712
0
    ret->err = NULL;
10713
0
    ret->errTab = NULL;
10714
0
    if (schema != NULL)
10715
0
  ret->idref = schema->idref;
10716
0
    ret->states = NULL;
10717
0
    ret->freeState = NULL;
10718
0
    ret->freeStates = NULL;
10719
0
    ret->errNo = XML_RELAXNG_OK;
10720
0
    return (ret);
10721
0
}
10722
10723
/**
10724
 * xmlRelaxNGFreeValidCtxt:
10725
 * @ctxt:  the schema validation context
10726
 *
10727
 * Free the resources associated to the schema validation context
10728
 */
10729
void
10730
xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10731
0
{
10732
0
    int k;
10733
10734
0
    if (ctxt == NULL)
10735
0
        return;
10736
0
    if (ctxt->states != NULL)
10737
0
        xmlRelaxNGFreeStates(NULL, ctxt->states);
10738
0
    if (ctxt->freeState != NULL) {
10739
0
        for (k = 0; k < ctxt->freeState->nbState; k++) {
10740
0
            xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10741
0
        }
10742
0
        xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10743
0
    }
10744
0
    if (ctxt->freeStates != NULL) {
10745
0
        for (k = 0; k < ctxt->freeStatesNr; k++) {
10746
0
            xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10747
0
        }
10748
0
        xmlFree(ctxt->freeStates);
10749
0
    }
10750
0
    if (ctxt->errTab != NULL)
10751
0
        xmlFree(ctxt->errTab);
10752
0
    if (ctxt->elemTab != NULL) {
10753
0
        xmlRegExecCtxtPtr exec;
10754
10755
0
        exec = xmlRelaxNGElemPop(ctxt);
10756
0
        while (exec != NULL) {
10757
0
            xmlRegFreeExecCtxt(exec);
10758
0
            exec = xmlRelaxNGElemPop(ctxt);
10759
0
        }
10760
0
        xmlFree(ctxt->elemTab);
10761
0
    }
10762
0
    xmlFree(ctxt);
10763
0
}
10764
10765
/**
10766
 * xmlRelaxNGSetValidErrors:
10767
 * @ctxt:  a Relax-NG validation context
10768
 * @err:  the error function
10769
 * @warn: the warning function
10770
 * @ctx: the functions context
10771
 *
10772
 * DEPRECATED: Use xmlRelaxNGSetValidStructuredErrors.
10773
 *
10774
 * Set the error and warning callback information
10775
 */
10776
void
10777
xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10778
                         xmlRelaxNGValidityErrorFunc err,
10779
                         xmlRelaxNGValidityWarningFunc warn, void *ctx)
10780
0
{
10781
0
    if (ctxt == NULL)
10782
0
        return;
10783
0
    ctxt->error = err;
10784
0
    ctxt->warning = warn;
10785
0
    ctxt->userData = ctx;
10786
0
    ctxt->serror = NULL;
10787
0
}
10788
10789
/**
10790
 * xmlRelaxNGSetValidStructuredErrors:
10791
 * @ctxt:  a Relax-NG validation context
10792
 * @serror:  the structured error function
10793
 * @ctx: the functions context
10794
 *
10795
 * Set the structured error callback
10796
 */
10797
void
10798
xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
10799
                                   xmlStructuredErrorFunc serror, void *ctx)
10800
0
{
10801
0
    if (ctxt == NULL)
10802
0
        return;
10803
0
    ctxt->serror = serror;
10804
0
    ctxt->error = NULL;
10805
0
    ctxt->warning = NULL;
10806
0
    ctxt->userData = ctx;
10807
0
}
10808
10809
/**
10810
 * xmlRelaxNGGetValidErrors:
10811
 * @ctxt:  a Relax-NG validation context
10812
 * @err:  the error function result
10813
 * @warn: the warning function result
10814
 * @ctx: the functions context result
10815
 *
10816
 * Get the error and warning callback information
10817
 *
10818
 * Returns -1 in case of error and 0 otherwise
10819
 */
10820
int
10821
xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10822
                         xmlRelaxNGValidityErrorFunc * err,
10823
                         xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10824
0
{
10825
0
    if (ctxt == NULL)
10826
0
        return (-1);
10827
0
    if (err != NULL)
10828
0
        *err = ctxt->error;
10829
0
    if (warn != NULL)
10830
0
        *warn = ctxt->warning;
10831
0
    if (ctx != NULL)
10832
0
        *ctx = ctxt->userData;
10833
0
    return (0);
10834
0
}
10835
10836
/**
10837
 * xmlRelaxNGValidateDoc:
10838
 * @ctxt:  a Relax-NG validation context
10839
 * @doc:  a parsed document tree
10840
 *
10841
 * Validate a document tree in memory.
10842
 *
10843
 * Returns 0 if the document is valid, a positive error code
10844
 *     number otherwise and -1 in case of internal or API error.
10845
 */
10846
int
10847
xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10848
0
{
10849
0
    int ret;
10850
10851
0
    if ((ctxt == NULL) || (doc == NULL))
10852
0
        return (-1);
10853
10854
0
    ctxt->doc = doc;
10855
10856
0
    ret = xmlRelaxNGValidateDocument(ctxt, doc);
10857
    /*
10858
     * Remove all left PSVI
10859
     */
10860
0
    xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10861
10862
    /*
10863
     * TODO: build error codes
10864
     */
10865
0
    if (ret == -1)
10866
0
        return (1);
10867
0
    return (ret);
10868
0
}
10869
10870
#endif /* LIBXML_SCHEMAS_ENABLED */