Coverage Report

Created: 2026-06-13 06:16

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