Coverage Report

Created: 2025-07-07 10:01

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