Coverage Report

Created: 2025-06-22 06:55

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