Coverage Report

Created: 2025-07-18 06:08

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