Coverage Report

Created: 2025-07-23 08:13

/src/fontconfig/subprojects/libxml2-2.12.6/valid.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * valid.c : part of the code use to do the DTD handling and the validity
3
 *           checking
4
 *
5
 * See Copyright for the status of this software.
6
 *
7
 * daniel@veillard.com
8
 */
9
10
#define IN_LIBXML
11
#include "libxml.h"
12
13
#include <string.h>
14
#include <stdlib.h>
15
16
#include <libxml/xmlmemory.h>
17
#include <libxml/hash.h>
18
#include <libxml/uri.h>
19
#include <libxml/valid.h>
20
#include <libxml/parser.h>
21
#include <libxml/parserInternals.h>
22
#include <libxml/xmlerror.h>
23
#include <libxml/list.h>
24
25
#include "private/error.h"
26
#include "private/parser.h"
27
28
static xmlElementPtr
29
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
30
                      int create);
31
32
#define TODO                \
33
    xmlGenericError(xmlGenericErrorContext,       \
34
      "Unimplemented block at %s:%d\n",       \
35
            __FILE__, __LINE__);
36
37
#ifdef LIBXML_VALID_ENABLED
38
static int
39
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
40
                                  const xmlChar *value);
41
#endif
42
/************************************************************************
43
 *                  *
44
 *      Error handling routines       *
45
 *                  *
46
 ************************************************************************/
47
48
/**
49
 * xmlVErrMemory:
50
 * @ctxt:  an XML validation parser context
51
 * @extra:  extra information
52
 *
53
 * Handle an out of memory error
54
 */
55
static void
56
xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
57
0
{
58
0
    xmlGenericErrorFunc channel = NULL;
59
0
    xmlParserCtxtPtr pctxt = NULL;
60
0
    void *data = NULL;
61
62
0
    if (ctxt != NULL) {
63
0
        channel = ctxt->error;
64
0
        data = ctxt->userData;
65
  /* Look up flag to detect if it is part of a parsing
66
     context */
67
0
  if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
68
0
      pctxt = ctxt->userData;
69
0
  }
70
0
    }
71
0
    if (extra)
72
0
        __xmlRaiseError(NULL, channel, data,
73
0
                        pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
74
0
                        XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
75
0
                        "Memory allocation failed : %s\n", extra);
76
0
    else
77
0
        __xmlRaiseError(NULL, channel, data,
78
0
                        pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
79
0
                        XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
80
0
                        "Memory allocation failed\n");
81
0
}
82
83
/**
84
 * xmlErrValid:
85
 * @ctxt:  an XML validation parser context
86
 * @error:  the error number
87
 * @extra:  extra information
88
 *
89
 * Handle a validation error
90
 */
91
static void LIBXML_ATTR_FORMAT(3,0)
92
xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
93
            const char *msg, const char *extra)
94
0
{
95
0
    xmlGenericErrorFunc channel = NULL;
96
0
    xmlParserCtxtPtr pctxt = NULL;
97
0
    void *data = NULL;
98
99
0
    if (ctxt != NULL) {
100
0
        channel = ctxt->error;
101
0
        data = ctxt->userData;
102
  /* Look up flag to detect if it is part of a parsing
103
     context */
104
0
  if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
105
0
      pctxt = ctxt->userData;
106
0
  }
107
0
    }
108
0
    if (extra)
109
0
        __xmlRaiseError(NULL, channel, data,
110
0
                        pctxt, NULL, XML_FROM_VALID, error,
111
0
                        XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
112
0
                        msg, extra);
113
0
    else
114
0
        __xmlRaiseError(NULL, channel, data,
115
0
                        pctxt, NULL, XML_FROM_VALID, error,
116
0
                        XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
117
0
                        "%s", msg);
118
0
}
119
120
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
121
/**
122
 * xmlErrValidNode:
123
 * @ctxt:  an XML validation parser context
124
 * @node:  the node raising the error
125
 * @error:  the error number
126
 * @str1:  extra information
127
 * @str2:  extra information
128
 * @str3:  extra information
129
 *
130
 * Handle a validation error, provide contextual information
131
 */
132
static void LIBXML_ATTR_FORMAT(4,0)
133
xmlErrValidNode(xmlValidCtxtPtr ctxt,
134
                xmlNodePtr node, xmlParserErrors error,
135
                const char *msg, const xmlChar * str1,
136
                const xmlChar * str2, const xmlChar * str3)
137
0
{
138
0
    xmlStructuredErrorFunc schannel = NULL;
139
0
    xmlGenericErrorFunc channel = NULL;
140
0
    xmlParserCtxtPtr pctxt = NULL;
141
0
    void *data = NULL;
142
143
0
    if (ctxt != NULL) {
144
0
        channel = ctxt->error;
145
0
        data = ctxt->userData;
146
  /* Look up flag to detect if it is part of a parsing
147
     context */
148
0
  if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
149
0
      pctxt = ctxt->userData;
150
0
  }
151
0
    }
152
0
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
153
0
                    XML_ERR_ERROR, NULL, 0,
154
0
                    (const char *) str1,
155
0
                    (const char *) str2,
156
0
                    (const char *) str3, 0, 0, msg, str1, str2, str3);
157
0
}
158
#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
159
160
#ifdef LIBXML_VALID_ENABLED
161
/**
162
 * xmlErrValidNodeNr:
163
 * @ctxt:  an XML validation parser context
164
 * @node:  the node raising the error
165
 * @error:  the error number
166
 * @str1:  extra information
167
 * @int2:  extra information
168
 * @str3:  extra information
169
 *
170
 * Handle a validation error, provide contextual information
171
 */
172
static void LIBXML_ATTR_FORMAT(4,0)
173
xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
174
                xmlNodePtr node, xmlParserErrors error,
175
                const char *msg, const xmlChar * str1,
176
                int int2, const xmlChar * str3)
177
0
{
178
0
    xmlStructuredErrorFunc schannel = NULL;
179
0
    xmlGenericErrorFunc channel = NULL;
180
0
    xmlParserCtxtPtr pctxt = NULL;
181
0
    void *data = NULL;
182
183
0
    if (ctxt != NULL) {
184
0
        channel = ctxt->error;
185
0
        data = ctxt->userData;
186
  /* Look up flag to detect if it is part of a parsing
187
     context */
188
0
  if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
189
0
      pctxt = ctxt->userData;
190
0
  }
191
0
    }
192
0
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
193
0
                    XML_ERR_ERROR, NULL, 0,
194
0
                    (const char *) str1,
195
0
                    (const char *) str3,
196
0
                    NULL, int2, 0, msg, str1, int2, str3);
197
0
}
198
199
/**
200
 * xmlErrValidWarning:
201
 * @ctxt:  an XML validation parser context
202
 * @node:  the node raising the error
203
 * @error:  the error number
204
 * @str1:  extra information
205
 * @str2:  extra information
206
 * @str3:  extra information
207
 *
208
 * Handle a validation error, provide contextual information
209
 */
210
static void LIBXML_ATTR_FORMAT(4,0)
211
xmlErrValidWarning(xmlValidCtxtPtr ctxt,
212
                xmlNodePtr node, xmlParserErrors error,
213
                const char *msg, const xmlChar * str1,
214
                const xmlChar * str2, const xmlChar * str3)
215
0
{
216
0
    xmlStructuredErrorFunc schannel = NULL;
217
0
    xmlGenericErrorFunc channel = NULL;
218
0
    xmlParserCtxtPtr pctxt = NULL;
219
0
    void *data = NULL;
220
221
0
    if (ctxt != NULL) {
222
0
        channel = ctxt->warning;
223
0
        data = ctxt->userData;
224
  /* Look up flag to detect if it is part of a parsing
225
     context */
226
0
  if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
227
0
      pctxt = ctxt->userData;
228
0
  }
229
0
    }
230
0
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
231
0
                    XML_ERR_WARNING, NULL, 0,
232
0
                    (const char *) str1,
233
0
                    (const char *) str2,
234
0
                    (const char *) str3, 0, 0, msg, str1, str2, str3);
235
0
}
236
237
238
239
#ifdef LIBXML_REGEXP_ENABLED
240
/*
241
 * If regexp are enabled we can do continuous validation without the
242
 * need of a tree to validate the content model. this is done in each
243
 * callbacks.
244
 * Each xmlValidState represent the validation state associated to the
245
 * set of nodes currently open from the document root to the current element.
246
 */
247
248
249
typedef struct _xmlValidState {
250
    xmlElementPtr  elemDecl;  /* pointer to the content model */
251
    xmlNodePtr           node;    /* pointer to the current node */
252
    xmlRegExecCtxtPtr    exec;    /* regexp runtime */
253
} _xmlValidState;
254
255
256
static int
257
0
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
258
0
    if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
259
0
  ctxt->vstateMax = 10;
260
0
  ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
261
0
                  sizeof(ctxt->vstateTab[0]));
262
0
        if (ctxt->vstateTab == NULL) {
263
0
      xmlVErrMemory(ctxt, "malloc failed");
264
0
      return(-1);
265
0
  }
266
0
    }
267
268
0
    if (ctxt->vstateNr >= ctxt->vstateMax) {
269
0
        xmlValidState *tmp;
270
271
0
  tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
272
0
               2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
273
0
        if (tmp == NULL) {
274
0
      xmlVErrMemory(ctxt, "realloc failed");
275
0
      return(-1);
276
0
  }
277
0
  ctxt->vstateMax *= 2;
278
0
  ctxt->vstateTab = tmp;
279
0
    }
280
0
    ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
281
0
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
282
0
    ctxt->vstateTab[ctxt->vstateNr].node = node;
283
0
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
284
0
  if (elemDecl->contModel == NULL)
285
0
      xmlValidBuildContentModel(ctxt, elemDecl);
286
0
  if (elemDecl->contModel != NULL) {
287
0
      ctxt->vstateTab[ctxt->vstateNr].exec =
288
0
    xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
289
0
  } else {
290
0
      ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
291
0
      xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
292
0
                      XML_ERR_INTERNAL_ERROR,
293
0
          "Failed to build content model regexp for %s\n",
294
0
          node->name, NULL, NULL);
295
0
  }
296
0
    }
297
0
    return(ctxt->vstateNr++);
298
0
}
299
300
static int
301
0
vstateVPop(xmlValidCtxtPtr ctxt) {
302
0
    xmlElementPtr elemDecl;
303
304
0
    if (ctxt->vstateNr < 1) return(-1);
305
0
    ctxt->vstateNr--;
306
0
    elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
307
0
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
308
0
    ctxt->vstateTab[ctxt->vstateNr].node = NULL;
309
0
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
310
0
  xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
311
0
    }
312
0
    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
313
0
    if (ctxt->vstateNr >= 1)
314
0
  ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
315
0
    else
316
0
  ctxt->vstate = NULL;
317
0
    return(ctxt->vstateNr);
318
0
}
319
320
#else /* not LIBXML_REGEXP_ENABLED */
321
/*
322
 * If regexp are not enabled, it uses a home made algorithm less
323
 * complex and easier to
324
 * debug/maintain than a generic NFA -> DFA state based algo. The
325
 * only restriction is on the deepness of the tree limited by the
326
 * size of the occurs bitfield
327
 *
328
 * this is the content of a saved state for rollbacks
329
 */
330
331
#define ROLLBACK_OR 0
332
#define ROLLBACK_PARENT 1
333
334
typedef struct _xmlValidState {
335
    xmlElementContentPtr cont;  /* pointer to the content model subtree */
336
    xmlNodePtr           node;  /* pointer to the current node in the list */
337
    long                 occurs;/* bitfield for multiple occurrences */
338
    unsigned char        depth; /* current depth in the overall tree */
339
    unsigned char        state; /* ROLLBACK_XXX */
340
} _xmlValidState;
341
342
#define MAX_RECURSE 25000
343
#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
344
#define CONT ctxt->vstate->cont
345
#define NODE ctxt->vstate->node
346
#define DEPTH ctxt->vstate->depth
347
#define OCCURS ctxt->vstate->occurs
348
#define STATE ctxt->vstate->state
349
350
#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
351
#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
352
353
#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
354
#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
355
356
static int
357
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
358
      xmlNodePtr node, unsigned char depth, long occurs,
359
      unsigned char state) {
360
    int i = ctxt->vstateNr - 1;
361
362
    if (ctxt->vstateNr > MAX_RECURSE) {
363
  return(-1);
364
    }
365
    if (ctxt->vstateTab == NULL) {
366
  ctxt->vstateMax = 8;
367
  ctxt->vstateTab = (xmlValidState *) xmlMalloc(
368
         ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
369
  if (ctxt->vstateTab == NULL) {
370
      xmlVErrMemory(ctxt, "malloc failed");
371
      return(-1);
372
  }
373
    }
374
    if (ctxt->vstateNr >= ctxt->vstateMax) {
375
        xmlValidState *tmp;
376
377
        tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
378
               2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
379
        if (tmp == NULL) {
380
      xmlVErrMemory(ctxt, "malloc failed");
381
      return(-1);
382
  }
383
  ctxt->vstateMax *= 2;
384
  ctxt->vstateTab = tmp;
385
  ctxt->vstate = &ctxt->vstateTab[0];
386
    }
387
    /*
388
     * Don't push on the stack a state already here
389
     */
390
    if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
391
  (ctxt->vstateTab[i].node == node) &&
392
  (ctxt->vstateTab[i].depth == depth) &&
393
  (ctxt->vstateTab[i].occurs == occurs) &&
394
  (ctxt->vstateTab[i].state == state))
395
  return(ctxt->vstateNr);
396
    ctxt->vstateTab[ctxt->vstateNr].cont = cont;
397
    ctxt->vstateTab[ctxt->vstateNr].node = node;
398
    ctxt->vstateTab[ctxt->vstateNr].depth = depth;
399
    ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
400
    ctxt->vstateTab[ctxt->vstateNr].state = state;
401
    return(ctxt->vstateNr++);
402
}
403
404
static int
405
vstateVPop(xmlValidCtxtPtr ctxt) {
406
    if (ctxt->vstateNr <= 1) return(-1);
407
    ctxt->vstateNr--;
408
    ctxt->vstate = &ctxt->vstateTab[0];
409
    ctxt->vstate->cont =  ctxt->vstateTab[ctxt->vstateNr].cont;
410
    ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
411
    ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
412
    ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
413
    ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
414
    return(ctxt->vstateNr);
415
}
416
417
#endif /* LIBXML_REGEXP_ENABLED */
418
419
static int
420
nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
421
0
{
422
0
    if (ctxt->nodeMax <= 0) {
423
0
        ctxt->nodeMax = 4;
424
0
        ctxt->nodeTab =
425
0
            (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
426
0
                                     sizeof(ctxt->nodeTab[0]));
427
0
        if (ctxt->nodeTab == NULL) {
428
0
      xmlVErrMemory(ctxt, "malloc failed");
429
0
            ctxt->nodeMax = 0;
430
0
            return (0);
431
0
        }
432
0
    }
433
0
    if (ctxt->nodeNr >= ctxt->nodeMax) {
434
0
        xmlNodePtr *tmp;
435
0
        tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
436
0
            ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
437
0
        if (tmp == NULL) {
438
0
      xmlVErrMemory(ctxt, "realloc failed");
439
0
            return (0);
440
0
        }
441
0
        ctxt->nodeMax *= 2;
442
0
  ctxt->nodeTab = tmp;
443
0
    }
444
0
    ctxt->nodeTab[ctxt->nodeNr] = value;
445
0
    ctxt->node = value;
446
0
    return (ctxt->nodeNr++);
447
0
}
448
static xmlNodePtr
449
nodeVPop(xmlValidCtxtPtr ctxt)
450
0
{
451
0
    xmlNodePtr ret;
452
453
0
    if (ctxt->nodeNr <= 0)
454
0
        return (NULL);
455
0
    ctxt->nodeNr--;
456
0
    if (ctxt->nodeNr > 0)
457
0
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
458
0
    else
459
0
        ctxt->node = NULL;
460
0
    ret = ctxt->nodeTab[ctxt->nodeNr];
461
0
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
462
0
    return (ret);
463
0
}
464
465
/* TODO: use hash table for accesses to elem and attribute definitions */
466
467
468
#define CHECK_DTD           \
469
0
   if (doc == NULL) return(0);         \
470
0
   else if ((doc->intSubset == NULL) &&       \
471
0
      (doc->extSubset == NULL)) return(0)
472
473
#ifdef LIBXML_REGEXP_ENABLED
474
475
/************************************************************************
476
 *                  *
477
 *    Content model validation based on the regexps   *
478
 *                  *
479
 ************************************************************************/
480
481
/**
482
 * xmlValidBuildAContentModel:
483
 * @content:  the content model
484
 * @ctxt:  the schema parser context
485
 * @name:  the element name whose content is being built
486
 *
487
 * Generate the automata sequence needed for that type
488
 *
489
 * Returns 1 if successful or 0 in case of error.
490
 */
491
static int
492
xmlValidBuildAContentModel(xmlElementContentPtr content,
493
               xmlValidCtxtPtr ctxt,
494
0
               const xmlChar *name) {
495
0
    if (content == NULL) {
496
0
  xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
497
0
      "Found NULL content in content model of %s\n",
498
0
      name, NULL, NULL);
499
0
  return(0);
500
0
    }
501
0
    switch (content->type) {
502
0
  case XML_ELEMENT_CONTENT_PCDATA:
503
0
      xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
504
0
          "Found PCDATA in content model of %s\n",
505
0
                name, NULL, NULL);
506
0
      return(0);
507
0
      break;
508
0
  case XML_ELEMENT_CONTENT_ELEMENT: {
509
0
      xmlAutomataStatePtr oldstate = ctxt->state;
510
0
      xmlChar fn[50];
511
0
      xmlChar *fullname;
512
513
0
      fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
514
0
      if (fullname == NULL) {
515
0
          xmlVErrMemory(ctxt, "Building content model");
516
0
    return(0);
517
0
      }
518
519
0
      switch (content->ocur) {
520
0
    case XML_ELEMENT_CONTENT_ONCE:
521
0
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
522
0
          ctxt->state, NULL, fullname, NULL);
523
0
        break;
524
0
    case XML_ELEMENT_CONTENT_OPT:
525
0
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
526
0
          ctxt->state, NULL, fullname, NULL);
527
0
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
528
0
        break;
529
0
    case XML_ELEMENT_CONTENT_PLUS:
530
0
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
531
0
          ctxt->state, NULL, fullname, NULL);
532
0
        xmlAutomataNewTransition(ctxt->am, ctxt->state,
533
0
                           ctxt->state, fullname, NULL);
534
0
        break;
535
0
    case XML_ELEMENT_CONTENT_MULT:
536
0
        ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
537
0
              ctxt->state, NULL);
538
0
        xmlAutomataNewTransition(ctxt->am,
539
0
          ctxt->state, ctxt->state, fullname, NULL);
540
0
        break;
541
0
      }
542
0
      if ((fullname != fn) && (fullname != content->name))
543
0
    xmlFree(fullname);
544
0
      break;
545
0
  }
546
0
  case XML_ELEMENT_CONTENT_SEQ: {
547
0
      xmlAutomataStatePtr oldstate, oldend;
548
0
      xmlElementContentOccur ocur;
549
550
      /*
551
       * Simply iterate over the content
552
       */
553
0
      oldstate = ctxt->state;
554
0
      ocur = content->ocur;
555
0
      if (ocur != XML_ELEMENT_CONTENT_ONCE) {
556
0
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
557
0
    oldstate = ctxt->state;
558
0
      }
559
0
      do {
560
0
    xmlValidBuildAContentModel(content->c1, ctxt, name);
561
0
    content = content->c2;
562
0
      } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
563
0
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
564
0
      xmlValidBuildAContentModel(content, ctxt, name);
565
0
      oldend = ctxt->state;
566
0
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
567
0
      switch (ocur) {
568
0
    case XML_ELEMENT_CONTENT_ONCE:
569
0
        break;
570
0
    case XML_ELEMENT_CONTENT_OPT:
571
0
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
572
0
        break;
573
0
    case XML_ELEMENT_CONTENT_MULT:
574
0
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
575
0
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
576
0
        break;
577
0
    case XML_ELEMENT_CONTENT_PLUS:
578
0
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
579
0
        break;
580
0
      }
581
0
      break;
582
0
  }
583
0
  case XML_ELEMENT_CONTENT_OR: {
584
0
      xmlAutomataStatePtr oldstate, oldend;
585
0
      xmlElementContentOccur ocur;
586
587
0
      ocur = content->ocur;
588
0
      if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
589
0
    (ocur == XML_ELEMENT_CONTENT_MULT)) {
590
0
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
591
0
      ctxt->state, NULL);
592
0
      }
593
0
      oldstate = ctxt->state;
594
0
      oldend = xmlAutomataNewState(ctxt->am);
595
596
      /*
597
       * iterate over the subtypes and remerge the end with an
598
       * epsilon transition
599
       */
600
0
      do {
601
0
    ctxt->state = oldstate;
602
0
    xmlValidBuildAContentModel(content->c1, ctxt, name);
603
0
    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
604
0
    content = content->c2;
605
0
      } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
606
0
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
607
0
      ctxt->state = oldstate;
608
0
      xmlValidBuildAContentModel(content, ctxt, name);
609
0
      xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
610
0
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
611
0
      switch (ocur) {
612
0
    case XML_ELEMENT_CONTENT_ONCE:
613
0
        break;
614
0
    case XML_ELEMENT_CONTENT_OPT:
615
0
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
616
0
        break;
617
0
    case XML_ELEMENT_CONTENT_MULT:
618
0
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
619
0
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
620
0
        break;
621
0
    case XML_ELEMENT_CONTENT_PLUS:
622
0
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
623
0
        break;
624
0
      }
625
0
      break;
626
0
  }
627
0
  default:
628
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
629
0
                  "ContentModel broken for element %s\n",
630
0
      (const char *) name);
631
0
      return(0);
632
0
    }
633
0
    return(1);
634
0
}
635
/**
636
 * xmlValidBuildContentModel:
637
 * @ctxt:  a validation context
638
 * @elem:  an element declaration node
639
 *
640
 * (Re)Build the automata associated to the content model of this
641
 * element
642
 *
643
 * Returns 1 in case of success, 0 in case of error
644
 */
645
int
646
0
xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
647
648
0
    if ((ctxt == NULL) || (elem == NULL))
649
0
  return(0);
650
0
    if (elem->type != XML_ELEMENT_DECL)
651
0
  return(0);
652
0
    if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
653
0
  return(1);
654
    /* TODO: should we rebuild in this case ? */
655
0
    if (elem->contModel != NULL) {
656
0
  if (!xmlRegexpIsDeterminist(elem->contModel)) {
657
0
      ctxt->valid = 0;
658
0
      return(0);
659
0
  }
660
0
  return(1);
661
0
    }
662
663
0
    ctxt->am = xmlNewAutomata();
664
0
    if (ctxt->am == NULL) {
665
0
  xmlErrValidNode(ctxt, (xmlNodePtr) elem,
666
0
                  XML_ERR_INTERNAL_ERROR,
667
0
                  "Cannot create automata for element %s\n",
668
0
            elem->name, NULL, NULL);
669
0
  return(0);
670
0
    }
671
0
    ctxt->state = xmlAutomataGetInitState(ctxt->am);
672
0
    xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
673
0
    xmlAutomataSetFinalState(ctxt->am, ctxt->state);
674
0
    elem->contModel = xmlAutomataCompile(ctxt->am);
675
0
    if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
676
0
  char expr[5000];
677
0
  expr[0] = 0;
678
0
  xmlSnprintfElementContent(expr, 5000, elem->content, 1);
679
0
  xmlErrValidNode(ctxt, (xmlNodePtr) elem,
680
0
                  XML_DTD_CONTENT_NOT_DETERMINIST,
681
0
         "Content model of %s is not deterministic: %s\n",
682
0
         elem->name, BAD_CAST expr, NULL);
683
0
        ctxt->valid = 0;
684
0
  ctxt->state = NULL;
685
0
  xmlFreeAutomata(ctxt->am);
686
0
  ctxt->am = NULL;
687
0
  return(0);
688
0
    }
689
0
    ctxt->state = NULL;
690
0
    xmlFreeAutomata(ctxt->am);
691
0
    ctxt->am = NULL;
692
0
    return(1);
693
0
}
694
695
#endif /* LIBXML_REGEXP_ENABLED */
696
697
/****************************************************************
698
 *                *
699
 *  Util functions for data allocation/deallocation   *
700
 *                *
701
 ****************************************************************/
702
703
/**
704
 * xmlNewValidCtxt:
705
 *
706
 * Allocate a validation context structure.
707
 *
708
 * Returns NULL if not, otherwise the new validation context structure
709
 */
710
0
xmlValidCtxtPtr xmlNewValidCtxt(void) {
711
0
    xmlValidCtxtPtr ret;
712
713
0
    if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
714
0
  xmlVErrMemory(NULL, "malloc failed");
715
0
  return (NULL);
716
0
    }
717
718
0
    (void) memset(ret, 0, sizeof (xmlValidCtxt));
719
720
0
    return (ret);
721
0
}
722
723
/**
724
 * xmlFreeValidCtxt:
725
 * @cur:  the validation context to free
726
 *
727
 * Free a validation context structure.
728
 */
729
void
730
0
xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
731
0
    if (cur == NULL)
732
0
        return;
733
0
    if (cur->vstateTab != NULL)
734
0
        xmlFree(cur->vstateTab);
735
0
    if (cur->nodeTab != NULL)
736
0
        xmlFree(cur->nodeTab);
737
0
    xmlFree(cur);
738
0
}
739
740
#endif /* LIBXML_VALID_ENABLED */
741
742
/**
743
 * xmlNewDocElementContent:
744
 * @doc:  the document
745
 * @name:  the subelement name or NULL
746
 * @type:  the type of element content decl
747
 *
748
 * Allocate an element content structure for the document.
749
 *
750
 * Returns NULL if not, otherwise the new element content structure
751
 */
752
xmlElementContentPtr
753
xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
754
0
                        xmlElementContentType type) {
755
0
    xmlElementContentPtr ret;
756
0
    xmlDictPtr dict = NULL;
757
758
0
    if (doc != NULL)
759
0
        dict = doc->dict;
760
761
0
    switch(type) {
762
0
  case XML_ELEMENT_CONTENT_ELEMENT:
763
0
      if (name == NULL) {
764
0
          xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
765
0
      "xmlNewElementContent : name == NULL !\n",
766
0
      NULL);
767
0
      }
768
0
      break;
769
0
        case XML_ELEMENT_CONTENT_PCDATA:
770
0
  case XML_ELEMENT_CONTENT_SEQ:
771
0
  case XML_ELEMENT_CONTENT_OR:
772
0
      if (name != NULL) {
773
0
          xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
774
0
      "xmlNewElementContent : name != NULL !\n",
775
0
      NULL);
776
0
      }
777
0
      break;
778
0
  default:
779
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
780
0
        "Internal: ELEMENT content corrupted invalid type\n",
781
0
        NULL);
782
0
      return(NULL);
783
0
    }
784
0
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
785
0
    if (ret == NULL) {
786
0
  xmlVErrMemory(NULL, "malloc failed");
787
0
  return(NULL);
788
0
    }
789
0
    memset(ret, 0, sizeof(xmlElementContent));
790
0
    ret->type = type;
791
0
    ret->ocur = XML_ELEMENT_CONTENT_ONCE;
792
0
    if (name != NULL) {
793
0
        int l;
794
0
  const xmlChar *tmp;
795
796
0
  tmp = xmlSplitQName3(name, &l);
797
0
  if (tmp == NULL) {
798
0
      if (dict == NULL)
799
0
    ret->name = xmlStrdup(name);
800
0
      else
801
0
          ret->name = xmlDictLookup(dict, name, -1);
802
0
  } else {
803
0
      if (dict == NULL) {
804
0
    ret->prefix = xmlStrndup(name, l);
805
0
    ret->name = xmlStrdup(tmp);
806
0
      } else {
807
0
          ret->prefix = xmlDictLookup(dict, name, l);
808
0
    ret->name = xmlDictLookup(dict, tmp, -1);
809
0
      }
810
0
  }
811
0
    }
812
0
    return(ret);
813
0
}
814
815
/**
816
 * xmlNewElementContent:
817
 * @name:  the subelement name or NULL
818
 * @type:  the type of element content decl
819
 *
820
 * Allocate an element content structure.
821
 * Deprecated in favor of xmlNewDocElementContent
822
 *
823
 * Returns NULL if not, otherwise the new element content structure
824
 */
825
xmlElementContentPtr
826
0
xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
827
0
    return(xmlNewDocElementContent(NULL, name, type));
828
0
}
829
830
/**
831
 * xmlCopyDocElementContent:
832
 * @doc:  the document owning the element declaration
833
 * @cur:  An element content pointer.
834
 *
835
 * Build a copy of an element content description.
836
 *
837
 * Returns the new xmlElementContentPtr or NULL in case of error.
838
 */
839
xmlElementContentPtr
840
0
xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
841
0
    xmlElementContentPtr ret = NULL, prev = NULL, tmp;
842
0
    xmlDictPtr dict = NULL;
843
844
0
    if (cur == NULL) return(NULL);
845
846
0
    if (doc != NULL)
847
0
        dict = doc->dict;
848
849
0
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
850
0
    if (ret == NULL) {
851
0
  xmlVErrMemory(NULL, "malloc failed");
852
0
  return(NULL);
853
0
    }
854
0
    memset(ret, 0, sizeof(xmlElementContent));
855
0
    ret->type = cur->type;
856
0
    ret->ocur = cur->ocur;
857
0
    if (cur->name != NULL) {
858
0
  if (dict)
859
0
      ret->name = xmlDictLookup(dict, cur->name, -1);
860
0
  else
861
0
      ret->name = xmlStrdup(cur->name);
862
0
    }
863
864
0
    if (cur->prefix != NULL) {
865
0
  if (dict)
866
0
      ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
867
0
  else
868
0
      ret->prefix = xmlStrdup(cur->prefix);
869
0
    }
870
0
    if (cur->c1 != NULL)
871
0
        ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
872
0
    if (ret->c1 != NULL)
873
0
  ret->c1->parent = ret;
874
0
    if (cur->c2 != NULL) {
875
0
        prev = ret;
876
0
  cur = cur->c2;
877
0
  while (cur != NULL) {
878
0
      tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
879
0
      if (tmp == NULL) {
880
0
    xmlVErrMemory(NULL, "malloc failed");
881
0
    return(ret);
882
0
      }
883
0
      memset(tmp, 0, sizeof(xmlElementContent));
884
0
      tmp->type = cur->type;
885
0
      tmp->ocur = cur->ocur;
886
0
      prev->c2 = tmp;
887
0
      tmp->parent = prev;
888
0
      if (cur->name != NULL) {
889
0
    if (dict)
890
0
        tmp->name = xmlDictLookup(dict, cur->name, -1);
891
0
    else
892
0
        tmp->name = xmlStrdup(cur->name);
893
0
      }
894
895
0
      if (cur->prefix != NULL) {
896
0
    if (dict)
897
0
        tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
898
0
    else
899
0
        tmp->prefix = xmlStrdup(cur->prefix);
900
0
      }
901
0
      if (cur->c1 != NULL)
902
0
          tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
903
0
      if (tmp->c1 != NULL)
904
0
    tmp->c1->parent = tmp;
905
0
      prev = tmp;
906
0
      cur = cur->c2;
907
0
  }
908
0
    }
909
0
    return(ret);
910
0
}
911
912
/**
913
 * xmlCopyElementContent:
914
 * @cur:  An element content pointer.
915
 *
916
 * Build a copy of an element content description.
917
 * Deprecated, use xmlCopyDocElementContent instead
918
 *
919
 * Returns the new xmlElementContentPtr or NULL in case of error.
920
 */
921
xmlElementContentPtr
922
0
xmlCopyElementContent(xmlElementContentPtr cur) {
923
0
    return(xmlCopyDocElementContent(NULL, cur));
924
0
}
925
926
/**
927
 * xmlFreeDocElementContent:
928
 * @doc: the document owning the element declaration
929
 * @cur:  the element content tree to free
930
 *
931
 * Free an element content structure. The whole subtree is removed.
932
 */
933
void
934
0
xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
935
0
    xmlDictPtr dict = NULL;
936
0
    size_t depth = 0;
937
938
0
    if (cur == NULL)
939
0
        return;
940
0
    if (doc != NULL)
941
0
        dict = doc->dict;
942
943
0
    while (1) {
944
0
        xmlElementContentPtr parent;
945
946
0
        while ((cur->c1 != NULL) || (cur->c2 != NULL)) {
947
0
            cur = (cur->c1 != NULL) ? cur->c1 : cur->c2;
948
0
            depth += 1;
949
0
        }
950
951
0
  switch (cur->type) {
952
0
      case XML_ELEMENT_CONTENT_PCDATA:
953
0
      case XML_ELEMENT_CONTENT_ELEMENT:
954
0
      case XML_ELEMENT_CONTENT_SEQ:
955
0
      case XML_ELEMENT_CONTENT_OR:
956
0
    break;
957
0
      default:
958
0
    xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
959
0
      "Internal: ELEMENT content corrupted invalid type\n",
960
0
      NULL);
961
0
    return;
962
0
  }
963
0
  if (dict) {
964
0
      if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
965
0
          xmlFree((xmlChar *) cur->name);
966
0
      if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
967
0
          xmlFree((xmlChar *) cur->prefix);
968
0
  } else {
969
0
      if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
970
0
      if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
971
0
  }
972
0
        parent = cur->parent;
973
0
        if ((depth == 0) || (parent == NULL)) {
974
0
            xmlFree(cur);
975
0
            break;
976
0
        }
977
0
        if (cur == parent->c1)
978
0
            parent->c1 = NULL;
979
0
        else
980
0
            parent->c2 = NULL;
981
0
  xmlFree(cur);
982
983
0
        if (parent->c2 != NULL) {
984
0
      cur = parent->c2;
985
0
        } else {
986
0
            depth -= 1;
987
0
            cur = parent;
988
0
        }
989
0
    }
990
0
}
991
992
/**
993
 * xmlFreeElementContent:
994
 * @cur:  the element content tree to free
995
 *
996
 * Free an element content structure. The whole subtree is removed.
997
 * Deprecated, use xmlFreeDocElementContent instead
998
 */
999
void
1000
0
xmlFreeElementContent(xmlElementContentPtr cur) {
1001
0
    xmlFreeDocElementContent(NULL, cur);
1002
0
}
1003
1004
#ifdef LIBXML_OUTPUT_ENABLED
1005
/**
1006
 * xmlDumpElementOccur:
1007
 * @buf:  An XML buffer
1008
 * @cur:  An element table
1009
 *
1010
 * Dump the occurrence operator of an element.
1011
 */
1012
static void
1013
0
xmlDumpElementOccur(xmlBufferPtr buf, xmlElementContentPtr cur) {
1014
0
    switch (cur->ocur) {
1015
0
        case XML_ELEMENT_CONTENT_ONCE:
1016
0
            break;
1017
0
        case XML_ELEMENT_CONTENT_OPT:
1018
0
            xmlBufferWriteChar(buf, "?");
1019
0
            break;
1020
0
        case XML_ELEMENT_CONTENT_MULT:
1021
0
            xmlBufferWriteChar(buf, "*");
1022
0
            break;
1023
0
        case XML_ELEMENT_CONTENT_PLUS:
1024
0
            xmlBufferWriteChar(buf, "+");
1025
0
            break;
1026
0
    }
1027
0
}
1028
1029
/**
1030
 * xmlDumpElementContent:
1031
 * @buf:  An XML buffer
1032
 * @content:  An element table
1033
 *
1034
 * This will dump the content of the element table as an XML DTD definition
1035
 */
1036
static void
1037
0
xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content) {
1038
0
    xmlElementContentPtr cur;
1039
1040
0
    if (content == NULL) return;
1041
1042
0
    xmlBufferWriteChar(buf, "(");
1043
0
    cur = content;
1044
1045
0
    do {
1046
0
        if (cur == NULL) return;
1047
1048
0
        switch (cur->type) {
1049
0
            case XML_ELEMENT_CONTENT_PCDATA:
1050
0
                xmlBufferWriteChar(buf, "#PCDATA");
1051
0
                break;
1052
0
            case XML_ELEMENT_CONTENT_ELEMENT:
1053
0
                if (cur->prefix != NULL) {
1054
0
                    xmlBufferWriteCHAR(buf, cur->prefix);
1055
0
                    xmlBufferWriteChar(buf, ":");
1056
0
                }
1057
0
                xmlBufferWriteCHAR(buf, cur->name);
1058
0
                break;
1059
0
            case XML_ELEMENT_CONTENT_SEQ:
1060
0
            case XML_ELEMENT_CONTENT_OR:
1061
0
                if ((cur != content) &&
1062
0
                    (cur->parent != NULL) &&
1063
0
                    ((cur->type != cur->parent->type) ||
1064
0
                     (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
1065
0
                    xmlBufferWriteChar(buf, "(");
1066
0
                cur = cur->c1;
1067
0
                continue;
1068
0
            default:
1069
0
                xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1070
0
                        "Internal: ELEMENT cur corrupted invalid type\n",
1071
0
                        NULL);
1072
0
        }
1073
1074
0
        while (cur != content) {
1075
0
            xmlElementContentPtr parent = cur->parent;
1076
1077
0
            if (parent == NULL) return;
1078
1079
0
            if (((cur->type == XML_ELEMENT_CONTENT_OR) ||
1080
0
                 (cur->type == XML_ELEMENT_CONTENT_SEQ)) &&
1081
0
                ((cur->type != parent->type) ||
1082
0
                 (cur->ocur != XML_ELEMENT_CONTENT_ONCE)))
1083
0
                xmlBufferWriteChar(buf, ")");
1084
0
            xmlDumpElementOccur(buf, cur);
1085
1086
0
            if (cur == parent->c1) {
1087
0
                if (parent->type == XML_ELEMENT_CONTENT_SEQ)
1088
0
                    xmlBufferWriteChar(buf, " , ");
1089
0
                else if (parent->type == XML_ELEMENT_CONTENT_OR)
1090
0
                    xmlBufferWriteChar(buf, " | ");
1091
1092
0
                cur = parent->c2;
1093
0
                break;
1094
0
            }
1095
1096
0
            cur = parent;
1097
0
        }
1098
0
    } while (cur != content);
1099
1100
0
    xmlBufferWriteChar(buf, ")");
1101
0
    xmlDumpElementOccur(buf, content);
1102
0
}
1103
1104
/**
1105
 * xmlSprintfElementContent:
1106
 * @buf:  an output buffer
1107
 * @content:  An element table
1108
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1109
 *
1110
 * Deprecated, unsafe, use xmlSnprintfElementContent
1111
 */
1112
void
1113
xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
1114
                   xmlElementContentPtr content ATTRIBUTE_UNUSED,
1115
0
       int englob ATTRIBUTE_UNUSED) {
1116
0
}
1117
#endif /* LIBXML_OUTPUT_ENABLED */
1118
1119
/**
1120
 * xmlSnprintfElementContent:
1121
 * @buf:  an output buffer
1122
 * @size:  the buffer size
1123
 * @content:  An element table
1124
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1125
 *
1126
 * This will dump the content of the element content definition
1127
 * Intended just for the debug routine
1128
 */
1129
void
1130
0
xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
1131
0
    int len;
1132
1133
0
    if (content == NULL) return;
1134
0
    len = strlen(buf);
1135
0
    if (size - len < 50) {
1136
0
  if ((size - len > 4) && (buf[len - 1] != '.'))
1137
0
      strcat(buf, " ...");
1138
0
  return;
1139
0
    }
1140
0
    if (englob) strcat(buf, "(");
1141
0
    switch (content->type) {
1142
0
        case XML_ELEMENT_CONTENT_PCDATA:
1143
0
            strcat(buf, "#PCDATA");
1144
0
      break;
1145
0
  case XML_ELEMENT_CONTENT_ELEMENT: {
1146
0
            int qnameLen = xmlStrlen(content->name);
1147
1148
0
      if (content->prefix != NULL)
1149
0
                qnameLen += xmlStrlen(content->prefix) + 1;
1150
0
      if (size - len < qnameLen + 10) {
1151
0
    strcat(buf, " ...");
1152
0
    return;
1153
0
      }
1154
0
      if (content->prefix != NULL) {
1155
0
    strcat(buf, (char *) content->prefix);
1156
0
    strcat(buf, ":");
1157
0
      }
1158
0
      if (content->name != NULL)
1159
0
    strcat(buf, (char *) content->name);
1160
0
      break;
1161
0
        }
1162
0
  case XML_ELEMENT_CONTENT_SEQ:
1163
0
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1164
0
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1165
0
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1166
0
      else
1167
0
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1168
0
      len = strlen(buf);
1169
0
      if (size - len < 50) {
1170
0
    if ((size - len > 4) && (buf[len - 1] != '.'))
1171
0
        strcat(buf, " ...");
1172
0
    return;
1173
0
      }
1174
0
            strcat(buf, " , ");
1175
0
      if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1176
0
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1177
0
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1178
0
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1179
0
      else
1180
0
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1181
0
      break;
1182
0
  case XML_ELEMENT_CONTENT_OR:
1183
0
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1184
0
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1185
0
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1186
0
      else
1187
0
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1188
0
      len = strlen(buf);
1189
0
      if (size - len < 50) {
1190
0
    if ((size - len > 4) && (buf[len - 1] != '.'))
1191
0
        strcat(buf, " ...");
1192
0
    return;
1193
0
      }
1194
0
            strcat(buf, " | ");
1195
0
      if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1196
0
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1197
0
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1198
0
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1199
0
      else
1200
0
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1201
0
      break;
1202
0
    }
1203
0
    if (size - strlen(buf) <= 2) return;
1204
0
    if (englob)
1205
0
        strcat(buf, ")");
1206
0
    switch (content->ocur) {
1207
0
        case XML_ELEMENT_CONTENT_ONCE:
1208
0
      break;
1209
0
        case XML_ELEMENT_CONTENT_OPT:
1210
0
      strcat(buf, "?");
1211
0
      break;
1212
0
        case XML_ELEMENT_CONTENT_MULT:
1213
0
      strcat(buf, "*");
1214
0
      break;
1215
0
        case XML_ELEMENT_CONTENT_PLUS:
1216
0
      strcat(buf, "+");
1217
0
      break;
1218
0
    }
1219
0
}
1220
1221
/****************************************************************
1222
 *                *
1223
 *  Registration of DTD declarations      *
1224
 *                *
1225
 ****************************************************************/
1226
1227
/**
1228
 * xmlFreeElement:
1229
 * @elem:  An element
1230
 *
1231
 * Deallocate the memory used by an element definition
1232
 */
1233
static void
1234
0
xmlFreeElement(xmlElementPtr elem) {
1235
0
    if (elem == NULL) return;
1236
0
    xmlUnlinkNode((xmlNodePtr) elem);
1237
0
    xmlFreeDocElementContent(elem->doc, elem->content);
1238
0
    if (elem->name != NULL)
1239
0
  xmlFree((xmlChar *) elem->name);
1240
0
    if (elem->prefix != NULL)
1241
0
  xmlFree((xmlChar *) elem->prefix);
1242
0
#ifdef LIBXML_REGEXP_ENABLED
1243
0
    if (elem->contModel != NULL)
1244
0
  xmlRegFreeRegexp(elem->contModel);
1245
0
#endif
1246
0
    xmlFree(elem);
1247
0
}
1248
1249
1250
/**
1251
 * xmlAddElementDecl:
1252
 * @ctxt:  the validation context
1253
 * @dtd:  pointer to the DTD
1254
 * @name:  the entity name
1255
 * @type:  the element type
1256
 * @content:  the element content tree or NULL
1257
 *
1258
 * Register a new element declaration
1259
 *
1260
 * Returns NULL if not, otherwise the entity
1261
 */
1262
xmlElementPtr
1263
xmlAddElementDecl(xmlValidCtxtPtr ctxt,
1264
                  xmlDtdPtr dtd, const xmlChar *name,
1265
                  xmlElementTypeVal type,
1266
0
      xmlElementContentPtr content) {
1267
0
    xmlElementPtr ret;
1268
0
    xmlElementTablePtr table;
1269
0
    xmlAttributePtr oldAttributes = NULL;
1270
0
    xmlChar *ns, *uqname;
1271
1272
0
    if (dtd == NULL) {
1273
0
  return(NULL);
1274
0
    }
1275
0
    if (name == NULL) {
1276
0
  return(NULL);
1277
0
    }
1278
1279
0
    switch (type) {
1280
0
        case XML_ELEMENT_TYPE_EMPTY:
1281
0
      if (content != NULL) {
1282
0
    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1283
0
            "xmlAddElementDecl: content != NULL for EMPTY\n",
1284
0
      NULL);
1285
0
    return(NULL);
1286
0
      }
1287
0
      break;
1288
0
  case XML_ELEMENT_TYPE_ANY:
1289
0
      if (content != NULL) {
1290
0
    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1291
0
            "xmlAddElementDecl: content != NULL for ANY\n",
1292
0
      NULL);
1293
0
    return(NULL);
1294
0
      }
1295
0
      break;
1296
0
  case XML_ELEMENT_TYPE_MIXED:
1297
0
      if (content == NULL) {
1298
0
    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1299
0
            "xmlAddElementDecl: content == NULL for MIXED\n",
1300
0
      NULL);
1301
0
    return(NULL);
1302
0
      }
1303
0
      break;
1304
0
  case XML_ELEMENT_TYPE_ELEMENT:
1305
0
      if (content == NULL) {
1306
0
    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1307
0
            "xmlAddElementDecl: content == NULL for ELEMENT\n",
1308
0
      NULL);
1309
0
    return(NULL);
1310
0
      }
1311
0
      break;
1312
0
  default:
1313
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1314
0
        "Internal: ELEMENT decl corrupted invalid type\n",
1315
0
        NULL);
1316
0
      return(NULL);
1317
0
    }
1318
1319
    /*
1320
     * check if name is a QName
1321
     */
1322
0
    uqname = xmlSplitQName2(name, &ns);
1323
0
    if (uqname != NULL)
1324
0
  name = uqname;
1325
1326
    /*
1327
     * Create the Element table if needed.
1328
     */
1329
0
    table = (xmlElementTablePtr) dtd->elements;
1330
0
    if (table == NULL) {
1331
0
  xmlDictPtr dict = NULL;
1332
1333
0
  if (dtd->doc != NULL)
1334
0
      dict = dtd->doc->dict;
1335
0
        table = xmlHashCreateDict(0, dict);
1336
0
  dtd->elements = (void *) table;
1337
0
    }
1338
0
    if (table == NULL) {
1339
0
  xmlVErrMemory(ctxt,
1340
0
            "xmlAddElementDecl: Table creation failed!\n");
1341
0
  if (uqname != NULL)
1342
0
      xmlFree(uqname);
1343
0
  if (ns != NULL)
1344
0
      xmlFree(ns);
1345
0
        return(NULL);
1346
0
    }
1347
1348
    /*
1349
     * lookup old attributes inserted on an undefined element in the
1350
     * internal subset.
1351
     */
1352
0
    if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1353
0
  ret = xmlHashLookup2(dtd->doc->intSubset->elements, name, ns);
1354
0
  if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1355
0
      oldAttributes = ret->attributes;
1356
0
      ret->attributes = NULL;
1357
0
      xmlHashRemoveEntry2(dtd->doc->intSubset->elements, name, ns, NULL);
1358
0
      xmlFreeElement(ret);
1359
0
  }
1360
0
    }
1361
1362
    /*
1363
     * The element may already be present if one of its attribute
1364
     * was registered first
1365
     */
1366
0
    ret = xmlHashLookup2(table, name, ns);
1367
0
    if (ret != NULL) {
1368
0
  if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1369
0
#ifdef LIBXML_VALID_ENABLED
1370
      /*
1371
       * The element is already defined in this DTD.
1372
       */
1373
0
      xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1374
0
                      "Redefinition of element %s\n",
1375
0
          name, NULL, NULL);
1376
0
#endif /* LIBXML_VALID_ENABLED */
1377
0
      if (uqname != NULL)
1378
0
    xmlFree(uqname);
1379
0
            if (ns != NULL)
1380
0
          xmlFree(ns);
1381
0
      return(NULL);
1382
0
  }
1383
0
  if (ns != NULL) {
1384
0
      xmlFree(ns);
1385
0
      ns = NULL;
1386
0
  }
1387
0
    } else {
1388
0
  ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1389
0
  if (ret == NULL) {
1390
0
      xmlVErrMemory(ctxt, "malloc failed");
1391
0
      if (uqname != NULL)
1392
0
    xmlFree(uqname);
1393
0
            if (ns != NULL)
1394
0
          xmlFree(ns);
1395
0
      return(NULL);
1396
0
  }
1397
0
  memset(ret, 0, sizeof(xmlElement));
1398
0
  ret->type = XML_ELEMENT_DECL;
1399
1400
  /*
1401
   * fill the structure.
1402
   */
1403
0
  ret->name = xmlStrdup(name);
1404
0
  if (ret->name == NULL) {
1405
0
      xmlVErrMemory(ctxt, "malloc failed");
1406
0
      if (uqname != NULL)
1407
0
    xmlFree(uqname);
1408
0
            if (ns != NULL)
1409
0
          xmlFree(ns);
1410
0
      xmlFree(ret);
1411
0
      return(NULL);
1412
0
  }
1413
0
  ret->prefix = ns;
1414
1415
  /*
1416
   * Validity Check:
1417
   * Insertion must not fail
1418
   */
1419
0
  if (xmlHashAddEntry2(table, name, ns, ret)) {
1420
0
#ifdef LIBXML_VALID_ENABLED
1421
      /*
1422
       * The element is already defined in this DTD.
1423
       */
1424
0
      xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1425
0
                      "Redefinition of element %s\n",
1426
0
          name, NULL, NULL);
1427
0
#endif /* LIBXML_VALID_ENABLED */
1428
0
      xmlFreeElement(ret);
1429
0
      if (uqname != NULL)
1430
0
    xmlFree(uqname);
1431
0
      return(NULL);
1432
0
  }
1433
  /*
1434
   * For new element, may have attributes from earlier
1435
   * definition in internal subset
1436
   */
1437
0
  ret->attributes = oldAttributes;
1438
0
    }
1439
1440
    /*
1441
     * Finish to fill the structure.
1442
     */
1443
0
    ret->etype = type;
1444
    /*
1445
     * Avoid a stupid copy when called by the parser
1446
     * and flag it by setting a special parent value
1447
     * so the parser doesn't unallocate it.
1448
     */
1449
0
    if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT)) {
1450
0
  ret->content = content;
1451
0
  if (content != NULL)
1452
0
      content->parent = (xmlElementContentPtr) 1;
1453
0
    } else {
1454
0
  ret->content = xmlCopyDocElementContent(dtd->doc, content);
1455
0
    }
1456
1457
    /*
1458
     * Link it to the DTD
1459
     */
1460
0
    ret->parent = dtd;
1461
0
    ret->doc = dtd->doc;
1462
0
    if (dtd->last == NULL) {
1463
0
  dtd->children = dtd->last = (xmlNodePtr) ret;
1464
0
    } else {
1465
0
        dtd->last->next = (xmlNodePtr) ret;
1466
0
  ret->prev = dtd->last;
1467
0
  dtd->last = (xmlNodePtr) ret;
1468
0
    }
1469
0
    if (uqname != NULL)
1470
0
  xmlFree(uqname);
1471
0
    return(ret);
1472
0
}
1473
1474
static void
1475
0
xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) {
1476
0
    xmlFreeElement((xmlElementPtr) elem);
1477
0
}
1478
1479
/**
1480
 * xmlFreeElementTable:
1481
 * @table:  An element table
1482
 *
1483
 * Deallocate the memory used by an element hash table.
1484
 */
1485
void
1486
0
xmlFreeElementTable(xmlElementTablePtr table) {
1487
0
    xmlHashFree(table, xmlFreeElementTableEntry);
1488
0
}
1489
1490
#ifdef LIBXML_TREE_ENABLED
1491
/**
1492
 * xmlCopyElement:
1493
 * @elem:  An element
1494
 *
1495
 * Build a copy of an element.
1496
 *
1497
 * Returns the new xmlElementPtr or NULL in case of error.
1498
 */
1499
static void *
1500
0
xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
1501
0
    xmlElementPtr elem = (xmlElementPtr) payload;
1502
0
    xmlElementPtr cur;
1503
1504
0
    cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1505
0
    if (cur == NULL) {
1506
0
  xmlVErrMemory(NULL, "malloc failed");
1507
0
  return(NULL);
1508
0
    }
1509
0
    memset(cur, 0, sizeof(xmlElement));
1510
0
    cur->type = XML_ELEMENT_DECL;
1511
0
    cur->etype = elem->etype;
1512
0
    if (elem->name != NULL)
1513
0
  cur->name = xmlStrdup(elem->name);
1514
0
    else
1515
0
  cur->name = NULL;
1516
0
    if (elem->prefix != NULL)
1517
0
  cur->prefix = xmlStrdup(elem->prefix);
1518
0
    else
1519
0
  cur->prefix = NULL;
1520
0
    cur->content = xmlCopyElementContent(elem->content);
1521
    /* TODO : rebuild the attribute list on the copy */
1522
0
    cur->attributes = NULL;
1523
0
    return(cur);
1524
0
}
1525
1526
/**
1527
 * xmlCopyElementTable:
1528
 * @table:  An element table
1529
 *
1530
 * Build a copy of an element table.
1531
 *
1532
 * Returns the new xmlElementTablePtr or NULL in case of error.
1533
 */
1534
xmlElementTablePtr
1535
0
xmlCopyElementTable(xmlElementTablePtr table) {
1536
0
    return((xmlElementTablePtr) xmlHashCopy(table, xmlCopyElement));
1537
0
}
1538
#endif /* LIBXML_TREE_ENABLED */
1539
1540
#ifdef LIBXML_OUTPUT_ENABLED
1541
/**
1542
 * xmlDumpElementDecl:
1543
 * @buf:  the XML buffer output
1544
 * @elem:  An element table
1545
 *
1546
 * This will dump the content of the element declaration as an XML
1547
 * DTD definition
1548
 */
1549
void
1550
0
xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1551
0
    if ((buf == NULL) || (elem == NULL))
1552
0
        return;
1553
0
    switch (elem->etype) {
1554
0
  case XML_ELEMENT_TYPE_EMPTY:
1555
0
      xmlBufferWriteChar(buf, "<!ELEMENT ");
1556
0
      if (elem->prefix != NULL) {
1557
0
    xmlBufferWriteCHAR(buf, elem->prefix);
1558
0
    xmlBufferWriteChar(buf, ":");
1559
0
      }
1560
0
      xmlBufferWriteCHAR(buf, elem->name);
1561
0
      xmlBufferWriteChar(buf, " EMPTY>\n");
1562
0
      break;
1563
0
  case XML_ELEMENT_TYPE_ANY:
1564
0
      xmlBufferWriteChar(buf, "<!ELEMENT ");
1565
0
      if (elem->prefix != NULL) {
1566
0
    xmlBufferWriteCHAR(buf, elem->prefix);
1567
0
    xmlBufferWriteChar(buf, ":");
1568
0
      }
1569
0
      xmlBufferWriteCHAR(buf, elem->name);
1570
0
      xmlBufferWriteChar(buf, " ANY>\n");
1571
0
      break;
1572
0
  case XML_ELEMENT_TYPE_MIXED:
1573
0
      xmlBufferWriteChar(buf, "<!ELEMENT ");
1574
0
      if (elem->prefix != NULL) {
1575
0
    xmlBufferWriteCHAR(buf, elem->prefix);
1576
0
    xmlBufferWriteChar(buf, ":");
1577
0
      }
1578
0
      xmlBufferWriteCHAR(buf, elem->name);
1579
0
      xmlBufferWriteChar(buf, " ");
1580
0
      xmlDumpElementContent(buf, elem->content);
1581
0
      xmlBufferWriteChar(buf, ">\n");
1582
0
      break;
1583
0
  case XML_ELEMENT_TYPE_ELEMENT:
1584
0
      xmlBufferWriteChar(buf, "<!ELEMENT ");
1585
0
      if (elem->prefix != NULL) {
1586
0
    xmlBufferWriteCHAR(buf, elem->prefix);
1587
0
    xmlBufferWriteChar(buf, ":");
1588
0
      }
1589
0
      xmlBufferWriteCHAR(buf, elem->name);
1590
0
      xmlBufferWriteChar(buf, " ");
1591
0
      xmlDumpElementContent(buf, elem->content);
1592
0
      xmlBufferWriteChar(buf, ">\n");
1593
0
      break;
1594
0
  default:
1595
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1596
0
        "Internal: ELEMENT struct corrupted invalid type\n",
1597
0
        NULL);
1598
0
    }
1599
0
}
1600
1601
/**
1602
 * xmlDumpElementDeclScan:
1603
 * @elem:  An element table
1604
 * @buf:  the XML buffer output
1605
 *
1606
 * This routine is used by the hash scan function.  It just reverses
1607
 * the arguments.
1608
 */
1609
static void
1610
xmlDumpElementDeclScan(void *elem, void *buf,
1611
0
                       const xmlChar *name ATTRIBUTE_UNUSED) {
1612
0
    xmlDumpElementDecl((xmlBufferPtr) buf, (xmlElementPtr) elem);
1613
0
}
1614
1615
/**
1616
 * xmlDumpElementTable:
1617
 * @buf:  the XML buffer output
1618
 * @table:  An element table
1619
 *
1620
 * This will dump the content of the element table as an XML DTD definition
1621
 */
1622
void
1623
0
xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1624
0
    if ((buf == NULL) || (table == NULL))
1625
0
        return;
1626
0
    xmlHashScan(table, xmlDumpElementDeclScan, buf);
1627
0
}
1628
#endif /* LIBXML_OUTPUT_ENABLED */
1629
1630
/**
1631
 * xmlCreateEnumeration:
1632
 * @name:  the enumeration name or NULL
1633
 *
1634
 * create and initialize an enumeration attribute node.
1635
 *
1636
 * Returns the xmlEnumerationPtr just created or NULL in case
1637
 *                of error.
1638
 */
1639
xmlEnumerationPtr
1640
0
xmlCreateEnumeration(const xmlChar *name) {
1641
0
    xmlEnumerationPtr ret;
1642
1643
0
    ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1644
0
    if (ret == NULL) {
1645
0
  xmlVErrMemory(NULL, "malloc failed");
1646
0
        return(NULL);
1647
0
    }
1648
0
    memset(ret, 0, sizeof(xmlEnumeration));
1649
1650
0
    if (name != NULL)
1651
0
        ret->name = xmlStrdup(name);
1652
0
    return(ret);
1653
0
}
1654
1655
/**
1656
 * xmlFreeEnumeration:
1657
 * @cur:  the tree to free.
1658
 *
1659
 * free an enumeration attribute node (recursive).
1660
 */
1661
void
1662
0
xmlFreeEnumeration(xmlEnumerationPtr cur) {
1663
0
    if (cur == NULL) return;
1664
1665
0
    if (cur->next != NULL) xmlFreeEnumeration(cur->next);
1666
1667
0
    if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
1668
0
    xmlFree(cur);
1669
0
}
1670
1671
#ifdef LIBXML_TREE_ENABLED
1672
/**
1673
 * xmlCopyEnumeration:
1674
 * @cur:  the tree to copy.
1675
 *
1676
 * Copy an enumeration attribute node (recursive).
1677
 *
1678
 * Returns the xmlEnumerationPtr just created or NULL in case
1679
 *                of error.
1680
 */
1681
xmlEnumerationPtr
1682
0
xmlCopyEnumeration(xmlEnumerationPtr cur) {
1683
0
    xmlEnumerationPtr ret;
1684
1685
0
    if (cur == NULL) return(NULL);
1686
0
    ret = xmlCreateEnumeration((xmlChar *) cur->name);
1687
0
    if (ret == NULL) return(NULL);
1688
1689
0
    if (cur->next != NULL) ret->next = xmlCopyEnumeration(cur->next);
1690
0
    else ret->next = NULL;
1691
1692
0
    return(ret);
1693
0
}
1694
#endif /* LIBXML_TREE_ENABLED */
1695
1696
#ifdef LIBXML_OUTPUT_ENABLED
1697
/**
1698
 * xmlDumpEnumeration:
1699
 * @buf:  the XML buffer output
1700
 * @enum:  An enumeration
1701
 *
1702
 * This will dump the content of the enumeration
1703
 */
1704
static void
1705
0
xmlDumpEnumeration(xmlBufferPtr buf, xmlEnumerationPtr cur) {
1706
0
    if ((buf == NULL) || (cur == NULL))
1707
0
        return;
1708
1709
0
    xmlBufferWriteCHAR(buf, cur->name);
1710
0
    if (cur->next == NULL)
1711
0
  xmlBufferWriteChar(buf, ")");
1712
0
    else {
1713
0
  xmlBufferWriteChar(buf, " | ");
1714
0
  xmlDumpEnumeration(buf, cur->next);
1715
0
    }
1716
0
}
1717
#endif /* LIBXML_OUTPUT_ENABLED */
1718
1719
#ifdef LIBXML_VALID_ENABLED
1720
/**
1721
 * xmlScanIDAttributeDecl:
1722
 * @ctxt:  the validation context
1723
 * @elem:  the element name
1724
 * @err: whether to raise errors here
1725
 *
1726
 * Verify that the element don't have too many ID attributes
1727
 * declared.
1728
 *
1729
 * Returns the number of ID attributes found.
1730
 */
1731
static int
1732
0
xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
1733
0
    xmlAttributePtr cur;
1734
0
    int ret = 0;
1735
1736
0
    if (elem == NULL) return(0);
1737
0
    cur = elem->attributes;
1738
0
    while (cur != NULL) {
1739
0
        if (cur->atype == XML_ATTRIBUTE_ID) {
1740
0
      ret ++;
1741
0
      if ((ret > 1) && (err))
1742
0
    xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
1743
0
         "Element %s has too many ID attributes defined : %s\n",
1744
0
           elem->name, cur->name, NULL);
1745
0
  }
1746
0
  cur = cur->nexth;
1747
0
    }
1748
0
    return(ret);
1749
0
}
1750
#endif /* LIBXML_VALID_ENABLED */
1751
1752
/**
1753
 * xmlFreeAttribute:
1754
 * @elem:  An attribute
1755
 *
1756
 * Deallocate the memory used by an attribute definition
1757
 */
1758
static void
1759
0
xmlFreeAttribute(xmlAttributePtr attr) {
1760
0
    xmlDictPtr dict;
1761
1762
0
    if (attr == NULL) return;
1763
0
    if (attr->doc != NULL)
1764
0
  dict = attr->doc->dict;
1765
0
    else
1766
0
  dict = NULL;
1767
0
    xmlUnlinkNode((xmlNodePtr) attr);
1768
0
    if (attr->tree != NULL)
1769
0
        xmlFreeEnumeration(attr->tree);
1770
0
    if (dict) {
1771
0
        if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1772
0
      xmlFree((xmlChar *) attr->elem);
1773
0
        if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1774
0
      xmlFree((xmlChar *) attr->name);
1775
0
        if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1776
0
      xmlFree((xmlChar *) attr->prefix);
1777
0
        if ((attr->defaultValue != NULL) &&
1778
0
      (!xmlDictOwns(dict, attr->defaultValue)))
1779
0
      xmlFree((xmlChar *) attr->defaultValue);
1780
0
    } else {
1781
0
  if (attr->elem != NULL)
1782
0
      xmlFree((xmlChar *) attr->elem);
1783
0
  if (attr->name != NULL)
1784
0
      xmlFree((xmlChar *) attr->name);
1785
0
  if (attr->defaultValue != NULL)
1786
0
      xmlFree((xmlChar *) attr->defaultValue);
1787
0
  if (attr->prefix != NULL)
1788
0
      xmlFree((xmlChar *) attr->prefix);
1789
0
    }
1790
0
    xmlFree(attr);
1791
0
}
1792
1793
1794
/**
1795
 * xmlAddAttributeDecl:
1796
 * @ctxt:  the validation context
1797
 * @dtd:  pointer to the DTD
1798
 * @elem:  the element name
1799
 * @name:  the attribute name
1800
 * @ns:  the attribute namespace prefix
1801
 * @type:  the attribute type
1802
 * @def:  the attribute default type
1803
 * @defaultValue:  the attribute default value
1804
 * @tree:  if it's an enumeration, the associated list
1805
 *
1806
 * Register a new attribute declaration
1807
 * Note that @tree becomes the ownership of the DTD
1808
 *
1809
 * Returns NULL if not new, otherwise the attribute decl
1810
 */
1811
xmlAttributePtr
1812
xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
1813
                    xmlDtdPtr dtd, const xmlChar *elem,
1814
                    const xmlChar *name, const xmlChar *ns,
1815
        xmlAttributeType type, xmlAttributeDefault def,
1816
0
        const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1817
0
    xmlAttributePtr ret;
1818
0
    xmlAttributeTablePtr table;
1819
0
    xmlElementPtr elemDef;
1820
0
    xmlDictPtr dict = NULL;
1821
1822
0
    if (dtd == NULL) {
1823
0
  xmlFreeEnumeration(tree);
1824
0
  return(NULL);
1825
0
    }
1826
0
    if (name == NULL) {
1827
0
  xmlFreeEnumeration(tree);
1828
0
  return(NULL);
1829
0
    }
1830
0
    if (elem == NULL) {
1831
0
  xmlFreeEnumeration(tree);
1832
0
  return(NULL);
1833
0
    }
1834
0
    if (dtd->doc != NULL)
1835
0
  dict = dtd->doc->dict;
1836
1837
0
#ifdef LIBXML_VALID_ENABLED
1838
    /*
1839
     * Check the type and possibly the default value.
1840
     */
1841
0
    switch (type) {
1842
0
        case XML_ATTRIBUTE_CDATA:
1843
0
      break;
1844
0
        case XML_ATTRIBUTE_ID:
1845
0
      break;
1846
0
        case XML_ATTRIBUTE_IDREF:
1847
0
      break;
1848
0
        case XML_ATTRIBUTE_IDREFS:
1849
0
      break;
1850
0
        case XML_ATTRIBUTE_ENTITY:
1851
0
      break;
1852
0
        case XML_ATTRIBUTE_ENTITIES:
1853
0
      break;
1854
0
        case XML_ATTRIBUTE_NMTOKEN:
1855
0
      break;
1856
0
        case XML_ATTRIBUTE_NMTOKENS:
1857
0
      break;
1858
0
        case XML_ATTRIBUTE_ENUMERATION:
1859
0
      break;
1860
0
        case XML_ATTRIBUTE_NOTATION:
1861
0
      break;
1862
0
  default:
1863
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1864
0
        "Internal: ATTRIBUTE struct corrupted invalid type\n",
1865
0
        NULL);
1866
0
      xmlFreeEnumeration(tree);
1867
0
      return(NULL);
1868
0
    }
1869
0
    if ((defaultValue != NULL) &&
1870
0
        (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
1871
0
  xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1872
0
                  "Attribute %s of %s: invalid default value\n",
1873
0
                  elem, name, defaultValue);
1874
0
  defaultValue = NULL;
1875
0
  if (ctxt != NULL)
1876
0
      ctxt->valid = 0;
1877
0
    }
1878
0
#endif /* LIBXML_VALID_ENABLED */
1879
1880
    /*
1881
     * Check first that an attribute defined in the external subset wasn't
1882
     * already defined in the internal subset
1883
     */
1884
0
    if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1885
0
  (dtd->doc->intSubset != NULL) &&
1886
0
  (dtd->doc->intSubset->attributes != NULL)) {
1887
0
        ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1888
0
  if (ret != NULL) {
1889
0
      xmlFreeEnumeration(tree);
1890
0
      return(NULL);
1891
0
  }
1892
0
    }
1893
1894
    /*
1895
     * Create the Attribute table if needed.
1896
     */
1897
0
    table = (xmlAttributeTablePtr) dtd->attributes;
1898
0
    if (table == NULL) {
1899
0
        table = xmlHashCreateDict(0, dict);
1900
0
  dtd->attributes = (void *) table;
1901
0
    }
1902
0
    if (table == NULL) {
1903
0
  xmlVErrMemory(ctxt,
1904
0
            "xmlAddAttributeDecl: Table creation failed!\n");
1905
0
  xmlFreeEnumeration(tree);
1906
0
        return(NULL);
1907
0
    }
1908
1909
1910
0
    ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1911
0
    if (ret == NULL) {
1912
0
  xmlVErrMemory(ctxt, "malloc failed");
1913
0
  xmlFreeEnumeration(tree);
1914
0
  return(NULL);
1915
0
    }
1916
0
    memset(ret, 0, sizeof(xmlAttribute));
1917
0
    ret->type = XML_ATTRIBUTE_DECL;
1918
1919
    /*
1920
     * fill the structure.
1921
     */
1922
0
    ret->atype = type;
1923
    /*
1924
     * doc must be set before possible error causes call
1925
     * to xmlFreeAttribute (because it's used to check on
1926
     * dict use)
1927
     */
1928
0
    ret->doc = dtd->doc;
1929
0
    if (dict) {
1930
0
  ret->name = xmlDictLookup(dict, name, -1);
1931
0
  ret->prefix = xmlDictLookup(dict, ns, -1);
1932
0
  ret->elem = xmlDictLookup(dict, elem, -1);
1933
0
    } else {
1934
0
  ret->name = xmlStrdup(name);
1935
0
  ret->prefix = xmlStrdup(ns);
1936
0
  ret->elem = xmlStrdup(elem);
1937
0
    }
1938
0
    ret->def = def;
1939
0
    ret->tree = tree;
1940
0
    if (defaultValue != NULL) {
1941
0
        if (dict)
1942
0
      ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
1943
0
  else
1944
0
      ret->defaultValue = xmlStrdup(defaultValue);
1945
0
    }
1946
1947
    /*
1948
     * Validity Check:
1949
     * Search the DTD for previous declarations of the ATTLIST
1950
     */
1951
0
    if (xmlHashAddEntry3(table, ret->name, ret->prefix, ret->elem, ret) < 0) {
1952
0
#ifdef LIBXML_VALID_ENABLED
1953
  /*
1954
   * The attribute is already defined in this DTD.
1955
   */
1956
0
  xmlErrValidWarning(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_REDEFINED,
1957
0
     "Attribute %s of element %s: already defined\n",
1958
0
     name, elem, NULL);
1959
0
#endif /* LIBXML_VALID_ENABLED */
1960
0
  xmlFreeAttribute(ret);
1961
0
  return(NULL);
1962
0
    }
1963
1964
    /*
1965
     * Validity Check:
1966
     * Multiple ID per element
1967
     */
1968
0
    elemDef = xmlGetDtdElementDesc2(ctxt, dtd, elem, 1);
1969
0
    if (elemDef != NULL) {
1970
1971
0
#ifdef LIBXML_VALID_ENABLED
1972
0
        if ((type == XML_ATTRIBUTE_ID) &&
1973
0
      (xmlScanIDAttributeDecl(NULL, elemDef, 1) != 0)) {
1974
0
      xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
1975
0
     "Element %s has too may ID attributes defined : %s\n",
1976
0
       elem, name, NULL);
1977
0
      if (ctxt != NULL)
1978
0
    ctxt->valid = 0;
1979
0
  }
1980
0
#endif /* LIBXML_VALID_ENABLED */
1981
1982
  /*
1983
   * Insert namespace default def first they need to be
1984
   * processed first.
1985
   */
1986
0
  if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1987
0
      ((ret->prefix != NULL &&
1988
0
       (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1989
0
      ret->nexth = elemDef->attributes;
1990
0
      elemDef->attributes = ret;
1991
0
  } else {
1992
0
      xmlAttributePtr tmp = elemDef->attributes;
1993
1994
0
      while ((tmp != NULL) &&
1995
0
       ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1996
0
        ((ret->prefix != NULL &&
1997
0
         (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1998
0
    if (tmp->nexth == NULL)
1999
0
        break;
2000
0
    tmp = tmp->nexth;
2001
0
      }
2002
0
      if (tmp != NULL) {
2003
0
    ret->nexth = tmp->nexth;
2004
0
          tmp->nexth = ret;
2005
0
      } else {
2006
0
    ret->nexth = elemDef->attributes;
2007
0
    elemDef->attributes = ret;
2008
0
      }
2009
0
  }
2010
0
    }
2011
2012
    /*
2013
     * Link it to the DTD
2014
     */
2015
0
    ret->parent = dtd;
2016
0
    if (dtd->last == NULL) {
2017
0
  dtd->children = dtd->last = (xmlNodePtr) ret;
2018
0
    } else {
2019
0
        dtd->last->next = (xmlNodePtr) ret;
2020
0
  ret->prev = dtd->last;
2021
0
  dtd->last = (xmlNodePtr) ret;
2022
0
    }
2023
0
    return(ret);
2024
0
}
2025
2026
static void
2027
0
xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) {
2028
0
    xmlFreeAttribute((xmlAttributePtr) attr);
2029
0
}
2030
2031
/**
2032
 * xmlFreeAttributeTable:
2033
 * @table:  An attribute table
2034
 *
2035
 * Deallocate the memory used by an entities hash table.
2036
 */
2037
void
2038
0
xmlFreeAttributeTable(xmlAttributeTablePtr table) {
2039
0
    xmlHashFree(table, xmlFreeAttributeTableEntry);
2040
0
}
2041
2042
#ifdef LIBXML_TREE_ENABLED
2043
/**
2044
 * xmlCopyAttribute:
2045
 * @attr:  An attribute
2046
 *
2047
 * Build a copy of an attribute.
2048
 *
2049
 * Returns the new xmlAttributePtr or NULL in case of error.
2050
 */
2051
static void *
2052
0
xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2053
0
    xmlAttributePtr attr = (xmlAttributePtr) payload;
2054
0
    xmlAttributePtr cur;
2055
2056
0
    cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
2057
0
    if (cur == NULL) {
2058
0
  xmlVErrMemory(NULL, "malloc failed");
2059
0
  return(NULL);
2060
0
    }
2061
0
    memset(cur, 0, sizeof(xmlAttribute));
2062
0
    cur->type = XML_ATTRIBUTE_DECL;
2063
0
    cur->atype = attr->atype;
2064
0
    cur->def = attr->def;
2065
0
    cur->tree = xmlCopyEnumeration(attr->tree);
2066
0
    if (attr->elem != NULL)
2067
0
  cur->elem = xmlStrdup(attr->elem);
2068
0
    if (attr->name != NULL)
2069
0
  cur->name = xmlStrdup(attr->name);
2070
0
    if (attr->prefix != NULL)
2071
0
  cur->prefix = xmlStrdup(attr->prefix);
2072
0
    if (attr->defaultValue != NULL)
2073
0
  cur->defaultValue = xmlStrdup(attr->defaultValue);
2074
0
    return(cur);
2075
0
}
2076
2077
/**
2078
 * xmlCopyAttributeTable:
2079
 * @table:  An attribute table
2080
 *
2081
 * Build a copy of an attribute table.
2082
 *
2083
 * Returns the new xmlAttributeTablePtr or NULL in case of error.
2084
 */
2085
xmlAttributeTablePtr
2086
0
xmlCopyAttributeTable(xmlAttributeTablePtr table) {
2087
0
    return((xmlAttributeTablePtr) xmlHashCopy(table, xmlCopyAttribute));
2088
0
}
2089
#endif /* LIBXML_TREE_ENABLED */
2090
2091
#ifdef LIBXML_OUTPUT_ENABLED
2092
/**
2093
 * xmlDumpAttributeDecl:
2094
 * @buf:  the XML buffer output
2095
 * @attr:  An attribute declaration
2096
 *
2097
 * This will dump the content of the attribute declaration as an XML
2098
 * DTD definition
2099
 */
2100
void
2101
0
xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
2102
0
    if ((buf == NULL) || (attr == NULL))
2103
0
        return;
2104
0
    xmlBufferWriteChar(buf, "<!ATTLIST ");
2105
0
    xmlBufferWriteCHAR(buf, attr->elem);
2106
0
    xmlBufferWriteChar(buf, " ");
2107
0
    if (attr->prefix != NULL) {
2108
0
  xmlBufferWriteCHAR(buf, attr->prefix);
2109
0
  xmlBufferWriteChar(buf, ":");
2110
0
    }
2111
0
    xmlBufferWriteCHAR(buf, attr->name);
2112
0
    switch (attr->atype) {
2113
0
  case XML_ATTRIBUTE_CDATA:
2114
0
      xmlBufferWriteChar(buf, " CDATA");
2115
0
      break;
2116
0
  case XML_ATTRIBUTE_ID:
2117
0
      xmlBufferWriteChar(buf, " ID");
2118
0
      break;
2119
0
  case XML_ATTRIBUTE_IDREF:
2120
0
      xmlBufferWriteChar(buf, " IDREF");
2121
0
      break;
2122
0
  case XML_ATTRIBUTE_IDREFS:
2123
0
      xmlBufferWriteChar(buf, " IDREFS");
2124
0
      break;
2125
0
  case XML_ATTRIBUTE_ENTITY:
2126
0
      xmlBufferWriteChar(buf, " ENTITY");
2127
0
      break;
2128
0
  case XML_ATTRIBUTE_ENTITIES:
2129
0
      xmlBufferWriteChar(buf, " ENTITIES");
2130
0
      break;
2131
0
  case XML_ATTRIBUTE_NMTOKEN:
2132
0
      xmlBufferWriteChar(buf, " NMTOKEN");
2133
0
      break;
2134
0
  case XML_ATTRIBUTE_NMTOKENS:
2135
0
      xmlBufferWriteChar(buf, " NMTOKENS");
2136
0
      break;
2137
0
  case XML_ATTRIBUTE_ENUMERATION:
2138
0
      xmlBufferWriteChar(buf, " (");
2139
0
      xmlDumpEnumeration(buf, attr->tree);
2140
0
      break;
2141
0
  case XML_ATTRIBUTE_NOTATION:
2142
0
      xmlBufferWriteChar(buf, " NOTATION (");
2143
0
      xmlDumpEnumeration(buf, attr->tree);
2144
0
      break;
2145
0
  default:
2146
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2147
0
        "Internal: ATTRIBUTE struct corrupted invalid type\n",
2148
0
        NULL);
2149
0
    }
2150
0
    switch (attr->def) {
2151
0
  case XML_ATTRIBUTE_NONE:
2152
0
      break;
2153
0
  case XML_ATTRIBUTE_REQUIRED:
2154
0
      xmlBufferWriteChar(buf, " #REQUIRED");
2155
0
      break;
2156
0
  case XML_ATTRIBUTE_IMPLIED:
2157
0
      xmlBufferWriteChar(buf, " #IMPLIED");
2158
0
      break;
2159
0
  case XML_ATTRIBUTE_FIXED:
2160
0
      xmlBufferWriteChar(buf, " #FIXED");
2161
0
      break;
2162
0
  default:
2163
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2164
0
        "Internal: ATTRIBUTE struct corrupted invalid def\n",
2165
0
        NULL);
2166
0
    }
2167
0
    if (attr->defaultValue != NULL) {
2168
0
  xmlBufferWriteChar(buf, " ");
2169
0
  xmlBufferWriteQuotedString(buf, attr->defaultValue);
2170
0
    }
2171
0
    xmlBufferWriteChar(buf, ">\n");
2172
0
}
2173
2174
/**
2175
 * xmlDumpAttributeDeclScan:
2176
 * @attr:  An attribute declaration
2177
 * @buf:  the XML buffer output
2178
 *
2179
 * This is used with the hash scan function - just reverses arguments
2180
 */
2181
static void
2182
xmlDumpAttributeDeclScan(void *attr, void *buf,
2183
0
                         const xmlChar *name ATTRIBUTE_UNUSED) {
2184
0
    xmlDumpAttributeDecl((xmlBufferPtr) buf, (xmlAttributePtr) attr);
2185
0
}
2186
2187
/**
2188
 * xmlDumpAttributeTable:
2189
 * @buf:  the XML buffer output
2190
 * @table:  An attribute table
2191
 *
2192
 * This will dump the content of the attribute table as an XML DTD definition
2193
 */
2194
void
2195
0
xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
2196
0
    if ((buf == NULL) || (table == NULL))
2197
0
        return;
2198
0
    xmlHashScan(table, xmlDumpAttributeDeclScan, buf);
2199
0
}
2200
#endif /* LIBXML_OUTPUT_ENABLED */
2201
2202
/************************************************************************
2203
 *                  *
2204
 *        NOTATIONs       *
2205
 *                  *
2206
 ************************************************************************/
2207
/**
2208
 * xmlFreeNotation:
2209
 * @not:  A notation
2210
 *
2211
 * Deallocate the memory used by an notation definition
2212
 */
2213
static void
2214
0
xmlFreeNotation(xmlNotationPtr nota) {
2215
0
    if (nota == NULL) return;
2216
0
    if (nota->name != NULL)
2217
0
  xmlFree((xmlChar *) nota->name);
2218
0
    if (nota->PublicID != NULL)
2219
0
  xmlFree((xmlChar *) nota->PublicID);
2220
0
    if (nota->SystemID != NULL)
2221
0
  xmlFree((xmlChar *) nota->SystemID);
2222
0
    xmlFree(nota);
2223
0
}
2224
2225
2226
/**
2227
 * xmlAddNotationDecl:
2228
 * @dtd:  pointer to the DTD
2229
 * @ctxt:  the validation context
2230
 * @name:  the entity name
2231
 * @PublicID:  the public identifier or NULL
2232
 * @SystemID:  the system identifier or NULL
2233
 *
2234
 * Register a new notation declaration
2235
 *
2236
 * Returns NULL if not, otherwise the entity
2237
 */
2238
xmlNotationPtr
2239
xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
2240
             const xmlChar *name,
2241
0
                   const xmlChar *PublicID, const xmlChar *SystemID) {
2242
0
    xmlNotationPtr ret;
2243
0
    xmlNotationTablePtr table;
2244
2245
0
    if (dtd == NULL) {
2246
0
  return(NULL);
2247
0
    }
2248
0
    if (name == NULL) {
2249
0
  return(NULL);
2250
0
    }
2251
0
    if ((PublicID == NULL) && (SystemID == NULL)) {
2252
0
  return(NULL);
2253
0
    }
2254
2255
    /*
2256
     * Create the Notation table if needed.
2257
     */
2258
0
    table = (xmlNotationTablePtr) dtd->notations;
2259
0
    if (table == NULL) {
2260
0
  xmlDictPtr dict = NULL;
2261
0
  if (dtd->doc != NULL)
2262
0
      dict = dtd->doc->dict;
2263
2264
0
        dtd->notations = table = xmlHashCreateDict(0, dict);
2265
0
    }
2266
0
    if (table == NULL) {
2267
0
  xmlVErrMemory(ctxt,
2268
0
    "xmlAddNotationDecl: Table creation failed!\n");
2269
0
        return(NULL);
2270
0
    }
2271
2272
0
    ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2273
0
    if (ret == NULL) {
2274
0
  xmlVErrMemory(ctxt, "malloc failed");
2275
0
  return(NULL);
2276
0
    }
2277
0
    memset(ret, 0, sizeof(xmlNotation));
2278
2279
    /*
2280
     * fill the structure.
2281
     */
2282
0
    ret->name = xmlStrdup(name);
2283
0
    if (SystemID != NULL)
2284
0
        ret->SystemID = xmlStrdup(SystemID);
2285
0
    if (PublicID != NULL)
2286
0
        ret->PublicID = xmlStrdup(PublicID);
2287
2288
    /*
2289
     * Validity Check:
2290
     * Check the DTD for previous declarations of the ATTLIST
2291
     */
2292
0
    if (xmlHashAddEntry(table, name, ret)) {
2293
0
#ifdef LIBXML_VALID_ENABLED
2294
0
  xmlErrValid(NULL, XML_DTD_NOTATION_REDEFINED,
2295
0
        "xmlAddNotationDecl: %s already defined\n",
2296
0
        (const char *) name);
2297
0
#endif /* LIBXML_VALID_ENABLED */
2298
0
  xmlFreeNotation(ret);
2299
0
  return(NULL);
2300
0
    }
2301
0
    return(ret);
2302
0
}
2303
2304
static void
2305
0
xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) {
2306
0
    xmlFreeNotation((xmlNotationPtr) nota);
2307
0
}
2308
2309
/**
2310
 * xmlFreeNotationTable:
2311
 * @table:  An notation table
2312
 *
2313
 * Deallocate the memory used by an entities hash table.
2314
 */
2315
void
2316
0
xmlFreeNotationTable(xmlNotationTablePtr table) {
2317
0
    xmlHashFree(table, xmlFreeNotationTableEntry);
2318
0
}
2319
2320
#ifdef LIBXML_TREE_ENABLED
2321
/**
2322
 * xmlCopyNotation:
2323
 * @nota:  A notation
2324
 *
2325
 * Build a copy of a notation.
2326
 *
2327
 * Returns the new xmlNotationPtr or NULL in case of error.
2328
 */
2329
static void *
2330
0
xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2331
0
    xmlNotationPtr nota = (xmlNotationPtr) payload;
2332
0
    xmlNotationPtr cur;
2333
2334
0
    cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2335
0
    if (cur == NULL) {
2336
0
  xmlVErrMemory(NULL, "malloc failed");
2337
0
  return(NULL);
2338
0
    }
2339
0
    if (nota->name != NULL)
2340
0
  cur->name = xmlStrdup(nota->name);
2341
0
    else
2342
0
  cur->name = NULL;
2343
0
    if (nota->PublicID != NULL)
2344
0
  cur->PublicID = xmlStrdup(nota->PublicID);
2345
0
    else
2346
0
  cur->PublicID = NULL;
2347
0
    if (nota->SystemID != NULL)
2348
0
  cur->SystemID = xmlStrdup(nota->SystemID);
2349
0
    else
2350
0
  cur->SystemID = NULL;
2351
0
    return(cur);
2352
0
}
2353
2354
/**
2355
 * xmlCopyNotationTable:
2356
 * @table:  A notation table
2357
 *
2358
 * Build a copy of a notation table.
2359
 *
2360
 * Returns the new xmlNotationTablePtr or NULL in case of error.
2361
 */
2362
xmlNotationTablePtr
2363
0
xmlCopyNotationTable(xmlNotationTablePtr table) {
2364
0
    return((xmlNotationTablePtr) xmlHashCopy(table, xmlCopyNotation));
2365
0
}
2366
#endif /* LIBXML_TREE_ENABLED */
2367
2368
#ifdef LIBXML_OUTPUT_ENABLED
2369
/**
2370
 * xmlDumpNotationDecl:
2371
 * @buf:  the XML buffer output
2372
 * @nota:  A notation declaration
2373
 *
2374
 * This will dump the content the notation declaration as an XML DTD definition
2375
 */
2376
void
2377
0
xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2378
0
    if ((buf == NULL) || (nota == NULL))
2379
0
        return;
2380
0
    xmlBufferWriteChar(buf, "<!NOTATION ");
2381
0
    xmlBufferWriteCHAR(buf, nota->name);
2382
0
    if (nota->PublicID != NULL) {
2383
0
  xmlBufferWriteChar(buf, " PUBLIC ");
2384
0
  xmlBufferWriteQuotedString(buf, nota->PublicID);
2385
0
  if (nota->SystemID != NULL) {
2386
0
      xmlBufferWriteChar(buf, " ");
2387
0
      xmlBufferWriteQuotedString(buf, nota->SystemID);
2388
0
  }
2389
0
    } else {
2390
0
  xmlBufferWriteChar(buf, " SYSTEM ");
2391
0
  xmlBufferWriteQuotedString(buf, nota->SystemID);
2392
0
    }
2393
0
    xmlBufferWriteChar(buf, " >\n");
2394
0
}
2395
2396
/**
2397
 * xmlDumpNotationDeclScan:
2398
 * @nota:  A notation declaration
2399
 * @buf:  the XML buffer output
2400
 *
2401
 * This is called with the hash scan function, and just reverses args
2402
 */
2403
static void
2404
xmlDumpNotationDeclScan(void *nota, void *buf,
2405
0
                        const xmlChar *name ATTRIBUTE_UNUSED) {
2406
0
    xmlDumpNotationDecl((xmlBufferPtr) buf, (xmlNotationPtr) nota);
2407
0
}
2408
2409
/**
2410
 * xmlDumpNotationTable:
2411
 * @buf:  the XML buffer output
2412
 * @table:  A notation table
2413
 *
2414
 * This will dump the content of the notation table as an XML DTD definition
2415
 */
2416
void
2417
0
xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2418
0
    if ((buf == NULL) || (table == NULL))
2419
0
        return;
2420
0
    xmlHashScan(table, xmlDumpNotationDeclScan, buf);
2421
0
}
2422
#endif /* LIBXML_OUTPUT_ENABLED */
2423
2424
/************************************************************************
2425
 *                  *
2426
 *        IDs         *
2427
 *                  *
2428
 ************************************************************************/
2429
/**
2430
 * DICT_FREE:
2431
 * @str:  a string
2432
 *
2433
 * Free a string if it is not owned by the "dict" dictionary in the
2434
 * current scope
2435
 */
2436
#define DICT_FREE(str)            \
2437
0
  if ((str) && ((!dict) ||       \
2438
0
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
2439
0
      xmlFree((char *)(str));
2440
2441
/**
2442
 * xmlValidNormalizeString:
2443
 * @str: a string
2444
 *
2445
 * Normalize a string in-place.
2446
 */
2447
static void
2448
0
xmlValidNormalizeString(xmlChar *str) {
2449
0
    xmlChar *dst;
2450
0
    const xmlChar *src;
2451
2452
0
    if (str == NULL)
2453
0
        return;
2454
0
    src = str;
2455
0
    dst = str;
2456
2457
0
    while (*src == 0x20) src++;
2458
0
    while (*src != 0) {
2459
0
  if (*src == 0x20) {
2460
0
      while (*src == 0x20) src++;
2461
0
      if (*src != 0)
2462
0
    *dst++ = 0x20;
2463
0
  } else {
2464
0
      *dst++ = *src++;
2465
0
  }
2466
0
    }
2467
0
    *dst = 0;
2468
0
}
2469
2470
static int
2471
0
xmlIsStreaming(xmlValidCtxtPtr ctxt) {
2472
0
    xmlParserCtxtPtr pctxt;
2473
2474
0
    if (ctxt == NULL)
2475
0
        return(0);
2476
0
    if ((ctxt->flags & XML_VCTXT_USE_PCTXT) == 0)
2477
0
        return(0);
2478
0
    pctxt = ctxt->userData;
2479
0
    return(pctxt->parseMode == XML_PARSE_READER);
2480
0
}
2481
2482
/**
2483
 * xmlFreeID:
2484
 * @not:  A id
2485
 *
2486
 * Deallocate the memory used by an id definition
2487
 */
2488
static void
2489
0
xmlFreeID(xmlIDPtr id) {
2490
0
    xmlDictPtr dict = NULL;
2491
2492
0
    if (id == NULL) return;
2493
2494
0
    if (id->doc != NULL)
2495
0
        dict = id->doc->dict;
2496
2497
0
    if (id->value != NULL)
2498
0
  DICT_FREE(id->value)
2499
0
    if (id->name != NULL)
2500
0
  DICT_FREE(id->name)
2501
0
    xmlFree(id);
2502
0
}
2503
2504
2505
/**
2506
 * xmlAddID:
2507
 * @ctxt:  the validation context
2508
 * @doc:  pointer to the document
2509
 * @value:  the value name
2510
 * @attr:  the attribute holding the ID
2511
 *
2512
 * Register a new id declaration
2513
 *
2514
 * Returns NULL if not, otherwise the new xmlIDPtr
2515
 */
2516
xmlIDPtr
2517
xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2518
0
         xmlAttrPtr attr) {
2519
0
    xmlIDPtr ret;
2520
0
    xmlIDTablePtr table;
2521
2522
0
    if (doc == NULL) {
2523
0
  return(NULL);
2524
0
    }
2525
0
    if ((value == NULL) || (value[0] == 0)) {
2526
0
  return(NULL);
2527
0
    }
2528
0
    if (attr == NULL) {
2529
0
  return(NULL);
2530
0
    }
2531
2532
    /*
2533
     * Create the ID table if needed.
2534
     */
2535
0
    table = (xmlIDTablePtr) doc->ids;
2536
0
    if (table == NULL)  {
2537
0
        doc->ids = table = xmlHashCreateDict(0, doc->dict);
2538
0
    }
2539
0
    if (table == NULL) {
2540
0
  xmlVErrMemory(ctxt,
2541
0
    "xmlAddID: Table creation failed!\n");
2542
0
        return(NULL);
2543
0
    }
2544
2545
0
    ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2546
0
    if (ret == NULL) {
2547
0
  xmlVErrMemory(ctxt, "malloc failed");
2548
0
  return(NULL);
2549
0
    }
2550
2551
    /*
2552
     * fill the structure.
2553
     */
2554
0
    ret->value = xmlStrdup(value);
2555
0
    ret->doc = doc;
2556
0
    if (xmlIsStreaming(ctxt)) {
2557
  /*
2558
   * Operating in streaming mode, attr is gonna disappear
2559
   */
2560
0
  if (doc->dict != NULL)
2561
0
      ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2562
0
  else
2563
0
      ret->name = xmlStrdup(attr->name);
2564
0
  ret->attr = NULL;
2565
0
    } else {
2566
0
  ret->attr = attr;
2567
0
  ret->name = NULL;
2568
0
    }
2569
0
    ret->lineno = xmlGetLineNo(attr->parent);
2570
2571
0
    if (xmlHashAddEntry(table, value, ret) < 0) {
2572
0
#ifdef LIBXML_VALID_ENABLED
2573
  /*
2574
   * The id is already defined in this DTD.
2575
   */
2576
0
  if (ctxt != NULL) {
2577
0
      xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2578
0
          "ID %s already defined\n", value, NULL, NULL);
2579
0
  }
2580
0
#endif /* LIBXML_VALID_ENABLED */
2581
0
  xmlFreeID(ret);
2582
0
  return(NULL);
2583
0
    }
2584
0
    if (attr != NULL)
2585
0
  attr->atype = XML_ATTRIBUTE_ID;
2586
0
    return(ret);
2587
0
}
2588
2589
static void
2590
0
xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) {
2591
0
    xmlFreeID((xmlIDPtr) id);
2592
0
}
2593
2594
/**
2595
 * xmlFreeIDTable:
2596
 * @table:  An id table
2597
 *
2598
 * Deallocate the memory used by an ID hash table.
2599
 */
2600
void
2601
0
xmlFreeIDTable(xmlIDTablePtr table) {
2602
0
    xmlHashFree(table, xmlFreeIDTableEntry);
2603
0
}
2604
2605
/**
2606
 * xmlIsID:
2607
 * @doc:  the document
2608
 * @elem:  the element carrying the attribute
2609
 * @attr:  the attribute
2610
 *
2611
 * Determine whether an attribute is of type ID. In case we have DTD(s)
2612
 * then this is done if DTD loading has been requested. In the case
2613
 * of HTML documents parsed with the HTML parser, then ID detection is
2614
 * done systematically.
2615
 *
2616
 * Returns 0 or 1 depending on the lookup result
2617
 */
2618
int
2619
0
xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2620
0
    if ((attr == NULL) || (attr->name == NULL)) return(0);
2621
0
    if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2622
0
        (!strcmp((char *) attr->name, "id")) &&
2623
0
        (!strcmp((char *) attr->ns->prefix, "xml")))
2624
0
  return(1);
2625
0
    if (doc == NULL) return(0);
2626
0
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
2627
0
        (doc->type != XML_HTML_DOCUMENT_NODE)) {
2628
0
  return(0);
2629
0
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2630
0
        if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2631
0
      ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
2632
0
      ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
2633
0
      return(1);
2634
0
  return(0);
2635
0
    } else if (elem == NULL) {
2636
0
  return(0);
2637
0
    } else {
2638
0
  xmlAttributePtr attrDecl = NULL;
2639
2640
0
  xmlChar felem[50], fattr[50];
2641
0
  xmlChar *fullelemname, *fullattrname;
2642
2643
0
  fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2644
0
      xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2645
0
      (xmlChar *)elem->name;
2646
2647
0
  fullattrname = (attr->ns != NULL && attr->ns->prefix != NULL) ?
2648
0
      xmlBuildQName(attr->name, attr->ns->prefix, fattr, 50) :
2649
0
      (xmlChar *)attr->name;
2650
2651
0
  if (fullelemname != NULL && fullattrname != NULL) {
2652
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullelemname,
2653
0
                             fullattrname);
2654
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
2655
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullelemname,
2656
0
               fullattrname);
2657
0
  }
2658
2659
0
  if ((fullattrname != fattr) && (fullattrname != attr->name))
2660
0
      xmlFree(fullattrname);
2661
0
  if ((fullelemname != felem) && (fullelemname != elem->name))
2662
0
      xmlFree(fullelemname);
2663
2664
0
        if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2665
0
      return(1);
2666
0
    }
2667
0
    return(0);
2668
0
}
2669
2670
/**
2671
 * xmlRemoveID:
2672
 * @doc:  the document
2673
 * @attr:  the attribute
2674
 *
2675
 * Remove the given attribute from the ID table maintained internally.
2676
 *
2677
 * Returns -1 if the lookup failed and 0 otherwise
2678
 */
2679
int
2680
0
xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2681
0
    xmlIDTablePtr table;
2682
0
    xmlIDPtr id;
2683
0
    xmlChar *ID;
2684
2685
0
    if (doc == NULL) return(-1);
2686
0
    if (attr == NULL) return(-1);
2687
2688
0
    table = (xmlIDTablePtr) doc->ids;
2689
0
    if (table == NULL)
2690
0
        return(-1);
2691
2692
0
    ID = xmlNodeListGetString(doc, attr->children, 1);
2693
0
    if (ID == NULL)
2694
0
        return(-1);
2695
0
    xmlValidNormalizeString(ID);
2696
2697
0
    id = xmlHashLookup(table, ID);
2698
0
    if (id == NULL || id->attr != attr) {
2699
0
        xmlFree(ID);
2700
0
        return(-1);
2701
0
    }
2702
2703
0
    xmlHashRemoveEntry(table, ID, xmlFreeIDTableEntry);
2704
0
    xmlFree(ID);
2705
0
    attr->atype = 0;
2706
0
    return(0);
2707
0
}
2708
2709
/**
2710
 * xmlGetID:
2711
 * @doc:  pointer to the document
2712
 * @ID:  the ID value
2713
 *
2714
 * Search the attribute declaring the given ID
2715
 *
2716
 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2717
 */
2718
xmlAttrPtr
2719
0
xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2720
0
    xmlIDTablePtr table;
2721
0
    xmlIDPtr id;
2722
2723
0
    if (doc == NULL) {
2724
0
  return(NULL);
2725
0
    }
2726
2727
0
    if (ID == NULL) {
2728
0
  return(NULL);
2729
0
    }
2730
2731
0
    table = (xmlIDTablePtr) doc->ids;
2732
0
    if (table == NULL)
2733
0
        return(NULL);
2734
2735
0
    id = xmlHashLookup(table, ID);
2736
0
    if (id == NULL)
2737
0
  return(NULL);
2738
0
    if (id->attr == NULL) {
2739
  /*
2740
   * We are operating on a stream, return a well known reference
2741
   * since the attribute node doesn't exist anymore
2742
   */
2743
0
  return((xmlAttrPtr) doc);
2744
0
    }
2745
0
    return(id->attr);
2746
0
}
2747
2748
/************************************************************************
2749
 *                  *
2750
 *        Refs          *
2751
 *                  *
2752
 ************************************************************************/
2753
typedef struct xmlRemoveMemo_t
2754
{
2755
  xmlListPtr l;
2756
  xmlAttrPtr ap;
2757
} xmlRemoveMemo;
2758
2759
typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2760
2761
typedef struct xmlValidateMemo_t
2762
{
2763
    xmlValidCtxtPtr ctxt;
2764
    const xmlChar *name;
2765
} xmlValidateMemo;
2766
2767
typedef xmlValidateMemo *xmlValidateMemoPtr;
2768
2769
/**
2770
 * xmlFreeRef:
2771
 * @lk:  A list link
2772
 *
2773
 * Deallocate the memory used by a ref definition
2774
 */
2775
static void
2776
0
xmlFreeRef(xmlLinkPtr lk) {
2777
0
    xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2778
0
    if (ref == NULL) return;
2779
0
    if (ref->value != NULL)
2780
0
        xmlFree((xmlChar *)ref->value);
2781
0
    if (ref->name != NULL)
2782
0
        xmlFree((xmlChar *)ref->name);
2783
0
    xmlFree(ref);
2784
0
}
2785
2786
/**
2787
 * xmlFreeRefTableEntry:
2788
 * @list_ref:  A list of references.
2789
 *
2790
 * Deallocate the memory used by a list of references
2791
 */
2792
static void
2793
0
xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2794
0
    xmlListPtr list_ref = (xmlListPtr) payload;
2795
0
    if (list_ref == NULL) return;
2796
0
    xmlListDelete(list_ref);
2797
0
}
2798
2799
/**
2800
 * xmlWalkRemoveRef:
2801
 * @data:  Contents of current link
2802
 * @user:  Value supplied by the user
2803
 *
2804
 * Returns 0 to abort the walk or 1 to continue
2805
 */
2806
static int
2807
xmlWalkRemoveRef(const void *data, void *user)
2808
0
{
2809
0
    xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2810
0
    xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2811
0
    xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
2812
2813
0
    if (attr0 == attr1) { /* Matched: remove and terminate walk */
2814
0
        xmlListRemoveFirst(ref_list, (void *)data);
2815
0
        return 0;
2816
0
    }
2817
0
    return 1;
2818
0
}
2819
2820
/**
2821
 * xmlDummyCompare
2822
 * @data0:  Value supplied by the user
2823
 * @data1:  Value supplied by the user
2824
 *
2825
 * Do nothing, return 0. Used to create unordered lists.
2826
 */
2827
static int
2828
xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2829
                const void *data1 ATTRIBUTE_UNUSED)
2830
0
{
2831
0
    return (0);
2832
0
}
2833
2834
/**
2835
 * xmlAddRef:
2836
 * @ctxt:  the validation context
2837
 * @doc:  pointer to the document
2838
 * @value:  the value name
2839
 * @attr:  the attribute holding the Ref
2840
 *
2841
 * DEPRECATED, do not use. This function will be removed from the public API.
2842
 *
2843
 * Register a new ref declaration
2844
 *
2845
 * Returns NULL if not, otherwise the new xmlRefPtr
2846
 */
2847
xmlRefPtr
2848
xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2849
0
    xmlAttrPtr attr) {
2850
0
    xmlRefPtr ret;
2851
0
    xmlRefTablePtr table;
2852
0
    xmlListPtr ref_list;
2853
2854
0
    if (doc == NULL) {
2855
0
        return(NULL);
2856
0
    }
2857
0
    if (value == NULL) {
2858
0
        return(NULL);
2859
0
    }
2860
0
    if (attr == NULL) {
2861
0
        return(NULL);
2862
0
    }
2863
2864
    /*
2865
     * Create the Ref table if needed.
2866
     */
2867
0
    table = (xmlRefTablePtr) doc->refs;
2868
0
    if (table == NULL) {
2869
0
        doc->refs = table = xmlHashCreateDict(0, doc->dict);
2870
0
    }
2871
0
    if (table == NULL) {
2872
0
  xmlVErrMemory(ctxt,
2873
0
            "xmlAddRef: Table creation failed!\n");
2874
0
        return(NULL);
2875
0
    }
2876
2877
0
    ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2878
0
    if (ret == NULL) {
2879
0
  xmlVErrMemory(ctxt, "malloc failed");
2880
0
        return(NULL);
2881
0
    }
2882
2883
    /*
2884
     * fill the structure.
2885
     */
2886
0
    ret->value = xmlStrdup(value);
2887
0
    if (xmlIsStreaming(ctxt)) {
2888
  /*
2889
   * Operating in streaming mode, attr is gonna disappear
2890
   */
2891
0
  ret->name = xmlStrdup(attr->name);
2892
0
  ret->attr = NULL;
2893
0
    } else {
2894
0
  ret->name = NULL;
2895
0
  ret->attr = attr;
2896
0
    }
2897
0
    ret->lineno = xmlGetLineNo(attr->parent);
2898
2899
    /* To add a reference :-
2900
     * References are maintained as a list of references,
2901
     * Lookup the entry, if no entry create new nodelist
2902
     * Add the owning node to the NodeList
2903
     * Return the ref
2904
     */
2905
2906
0
    if (NULL == (ref_list = xmlHashLookup(table, value))) {
2907
0
        if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare))) {
2908
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2909
0
        "xmlAddRef: Reference list creation failed!\n",
2910
0
        NULL);
2911
0
      goto failed;
2912
0
        }
2913
0
        if (xmlHashAddEntry(table, value, ref_list) < 0) {
2914
0
            xmlListDelete(ref_list);
2915
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2916
0
        "xmlAddRef: Reference list insertion failed!\n",
2917
0
        NULL);
2918
0
      goto failed;
2919
0
        }
2920
0
    }
2921
0
    if (xmlListAppend(ref_list, ret) != 0) {
2922
0
  xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
2923
0
        "xmlAddRef: Reference list insertion failed!\n",
2924
0
        NULL);
2925
0
        goto failed;
2926
0
    }
2927
0
    return(ret);
2928
0
failed:
2929
0
    if (ret != NULL) {
2930
0
        if (ret->value != NULL)
2931
0
      xmlFree((char *)ret->value);
2932
0
        if (ret->name != NULL)
2933
0
      xmlFree((char *)ret->name);
2934
0
        xmlFree(ret);
2935
0
    }
2936
0
    return(NULL);
2937
0
}
2938
2939
/**
2940
 * xmlFreeRefTable:
2941
 * @table:  An ref table
2942
 *
2943
 * DEPRECATED, do not use. This function will be removed from the public API.
2944
 *
2945
 * Deallocate the memory used by an Ref hash table.
2946
 */
2947
void
2948
0
xmlFreeRefTable(xmlRefTablePtr table) {
2949
0
    xmlHashFree(table, xmlFreeRefTableEntry);
2950
0
}
2951
2952
/**
2953
 * xmlIsRef:
2954
 * @doc:  the document
2955
 * @elem:  the element carrying the attribute
2956
 * @attr:  the attribute
2957
 *
2958
 * DEPRECATED, do not use. This function will be removed from the public API.
2959
 *
2960
 * Determine whether an attribute is of type Ref. In case we have DTD(s)
2961
 * then this is simple, otherwise we use an heuristic: name Ref (upper
2962
 * or lowercase).
2963
 *
2964
 * Returns 0 or 1 depending on the lookup result
2965
 */
2966
int
2967
0
xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2968
0
    if (attr == NULL)
2969
0
        return(0);
2970
0
    if (doc == NULL) {
2971
0
        doc = attr->doc;
2972
0
  if (doc == NULL) return(0);
2973
0
    }
2974
2975
0
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2976
0
        return(0);
2977
0
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2978
        /* TODO @@@ */
2979
0
        return(0);
2980
0
    } else {
2981
0
        xmlAttributePtr attrDecl;
2982
2983
0
        if (elem == NULL) return(0);
2984
0
        attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, attr->name);
2985
0
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
2986
0
            attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
2987
0
                             elem->name, attr->name);
2988
2989
0
  if ((attrDecl != NULL) &&
2990
0
      (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2991
0
       attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2992
0
  return(1);
2993
0
    }
2994
0
    return(0);
2995
0
}
2996
2997
/**
2998
 * xmlRemoveRef:
2999
 * @doc:  the document
3000
 * @attr:  the attribute
3001
 *
3002
 * DEPRECATED, do not use. This function will be removed from the public API.
3003
 *
3004
 * Remove the given attribute from the Ref table maintained internally.
3005
 *
3006
 * Returns -1 if the lookup failed and 0 otherwise
3007
 */
3008
int
3009
0
xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
3010
0
    xmlListPtr ref_list;
3011
0
    xmlRefTablePtr table;
3012
0
    xmlChar *ID;
3013
0
    xmlRemoveMemo target;
3014
3015
0
    if (doc == NULL) return(-1);
3016
0
    if (attr == NULL) return(-1);
3017
3018
0
    table = (xmlRefTablePtr) doc->refs;
3019
0
    if (table == NULL)
3020
0
        return(-1);
3021
3022
0
    ID = xmlNodeListGetString(doc, attr->children, 1);
3023
0
    if (ID == NULL)
3024
0
        return(-1);
3025
3026
0
    ref_list = xmlHashLookup(table, ID);
3027
0
    if(ref_list == NULL) {
3028
0
        xmlFree(ID);
3029
0
        return (-1);
3030
0
    }
3031
3032
    /* At this point, ref_list refers to a list of references which
3033
     * have the same key as the supplied attr. Our list of references
3034
     * is ordered by reference address and we don't have that information
3035
     * here to use when removing. We'll have to walk the list and
3036
     * check for a matching attribute, when we find one stop the walk
3037
     * and remove the entry.
3038
     * The list is ordered by reference, so that means we don't have the
3039
     * key. Passing the list and the reference to the walker means we
3040
     * will have enough data to be able to remove the entry.
3041
     */
3042
0
    target.l = ref_list;
3043
0
    target.ap = attr;
3044
3045
    /* Remove the supplied attr from our list */
3046
0
    xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
3047
3048
    /*If the list is empty then remove the list entry in the hash */
3049
0
    if (xmlListEmpty(ref_list))
3050
0
        xmlHashUpdateEntry(table, ID, NULL, xmlFreeRefTableEntry);
3051
0
    xmlFree(ID);
3052
0
    return(0);
3053
0
}
3054
3055
/**
3056
 * xmlGetRefs:
3057
 * @doc:  pointer to the document
3058
 * @ID:  the ID value
3059
 *
3060
 * DEPRECATED, do not use. This function will be removed from the public API.
3061
 *
3062
 * Find the set of references for the supplied ID.
3063
 *
3064
 * Returns NULL if not found, otherwise node set for the ID.
3065
 */
3066
xmlListPtr
3067
0
xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
3068
0
    xmlRefTablePtr table;
3069
3070
0
    if (doc == NULL) {
3071
0
        return(NULL);
3072
0
    }
3073
3074
0
    if (ID == NULL) {
3075
0
        return(NULL);
3076
0
    }
3077
3078
0
    table = (xmlRefTablePtr) doc->refs;
3079
0
    if (table == NULL)
3080
0
        return(NULL);
3081
3082
0
    return (xmlHashLookup(table, ID));
3083
0
}
3084
3085
/************************************************************************
3086
 *                  *
3087
 *    Routines for validity checking        *
3088
 *                  *
3089
 ************************************************************************/
3090
3091
/**
3092
 * xmlGetDtdElementDesc:
3093
 * @dtd:  a pointer to the DtD to search
3094
 * @name:  the element name
3095
 *
3096
 * Search the DTD for the description of this element
3097
 *
3098
 * returns the xmlElementPtr if found or NULL
3099
 */
3100
3101
xmlElementPtr
3102
0
xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
3103
0
    xmlElementTablePtr table;
3104
0
    xmlElementPtr cur;
3105
0
    xmlChar *uqname = NULL, *prefix = NULL;
3106
3107
0
    if ((dtd == NULL) || (name == NULL)) return(NULL);
3108
0
    if (dtd->elements == NULL)
3109
0
  return(NULL);
3110
0
    table = (xmlElementTablePtr) dtd->elements;
3111
3112
0
    uqname = xmlSplitQName2(name, &prefix);
3113
0
    if (uqname != NULL)
3114
0
        name = uqname;
3115
0
    cur = xmlHashLookup2(table, name, prefix);
3116
0
    if (prefix != NULL) xmlFree(prefix);
3117
0
    if (uqname != NULL) xmlFree(uqname);
3118
0
    return(cur);
3119
0
}
3120
/**
3121
 * xmlGetDtdElementDesc2:
3122
 * @dtd:  a pointer to the DtD to search
3123
 * @name:  the element name
3124
 * @create:  create an empty description if not found
3125
 *
3126
 * Search the DTD for the description of this element
3127
 *
3128
 * returns the xmlElementPtr if found or NULL
3129
 */
3130
3131
static xmlElementPtr
3132
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name,
3133
0
                      int create) {
3134
0
    xmlElementTablePtr table;
3135
0
    xmlElementPtr cur;
3136
0
    xmlChar *uqname = NULL, *prefix = NULL;
3137
3138
0
    if (dtd == NULL) return(NULL);
3139
0
    if (dtd->elements == NULL) {
3140
0
  xmlDictPtr dict = NULL;
3141
3142
0
  if (dtd->doc != NULL)
3143
0
      dict = dtd->doc->dict;
3144
3145
0
  if (!create)
3146
0
      return(NULL);
3147
  /*
3148
   * Create the Element table if needed.
3149
   */
3150
0
  table = (xmlElementTablePtr) dtd->elements;
3151
0
  if (table == NULL) {
3152
0
      table = xmlHashCreateDict(0, dict);
3153
0
      dtd->elements = (void *) table;
3154
0
  }
3155
0
  if (table == NULL) {
3156
0
      xmlVErrMemory(ctxt, "element table allocation failed");
3157
0
      return(NULL);
3158
0
  }
3159
0
    }
3160
0
    table = (xmlElementTablePtr) dtd->elements;
3161
3162
0
    uqname = xmlSplitQName2(name, &prefix);
3163
0
    if (uqname != NULL)
3164
0
        name = uqname;
3165
0
    cur = xmlHashLookup2(table, name, prefix);
3166
0
    if ((cur == NULL) && (create)) {
3167
0
  cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
3168
0
  if (cur == NULL) {
3169
0
      xmlVErrMemory(ctxt, "malloc failed");
3170
0
      goto error;
3171
0
  }
3172
0
  memset(cur, 0, sizeof(xmlElement));
3173
0
  cur->type = XML_ELEMENT_DECL;
3174
3175
  /*
3176
   * fill the structure.
3177
   */
3178
0
  cur->name = xmlStrdup(name);
3179
0
  cur->prefix = xmlStrdup(prefix);
3180
0
  cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
3181
3182
0
  if (xmlHashAddEntry2(table, name, prefix, cur) < 0) {
3183
0
      xmlVErrMemory(ctxt, "adding entry failed");
3184
0
            xmlFreeElement(cur);
3185
0
            cur = NULL;
3186
0
        }
3187
0
    }
3188
0
error:
3189
0
    if (prefix != NULL) xmlFree(prefix);
3190
0
    if (uqname != NULL) xmlFree(uqname);
3191
0
    return(cur);
3192
0
}
3193
3194
/**
3195
 * xmlGetDtdQElementDesc:
3196
 * @dtd:  a pointer to the DtD to search
3197
 * @name:  the element name
3198
 * @prefix:  the element namespace prefix
3199
 *
3200
 * Search the DTD for the description of this element
3201
 *
3202
 * returns the xmlElementPtr if found or NULL
3203
 */
3204
3205
xmlElementPtr
3206
xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3207
0
                const xmlChar *prefix) {
3208
0
    xmlElementTablePtr table;
3209
3210
0
    if (dtd == NULL) return(NULL);
3211
0
    if (dtd->elements == NULL) return(NULL);
3212
0
    table = (xmlElementTablePtr) dtd->elements;
3213
3214
0
    return(xmlHashLookup2(table, name, prefix));
3215
0
}
3216
3217
/**
3218
 * xmlGetDtdAttrDesc:
3219
 * @dtd:  a pointer to the DtD to search
3220
 * @elem:  the element name
3221
 * @name:  the attribute name
3222
 *
3223
 * Search the DTD for the description of this attribute on
3224
 * this element.
3225
 *
3226
 * returns the xmlAttributePtr if found or NULL
3227
 */
3228
3229
xmlAttributePtr
3230
0
xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3231
0
    xmlAttributeTablePtr table;
3232
0
    xmlAttributePtr cur;
3233
0
    xmlChar *uqname = NULL, *prefix = NULL;
3234
3235
0
    if (dtd == NULL) return(NULL);
3236
0
    if (dtd->attributes == NULL) return(NULL);
3237
3238
0
    table = (xmlAttributeTablePtr) dtd->attributes;
3239
0
    if (table == NULL)
3240
0
  return(NULL);
3241
3242
0
    uqname = xmlSplitQName2(name, &prefix);
3243
3244
0
    if (uqname != NULL) {
3245
0
  cur = xmlHashLookup3(table, uqname, prefix, elem);
3246
0
  if (prefix != NULL) xmlFree(prefix);
3247
0
  if (uqname != NULL) xmlFree(uqname);
3248
0
    } else
3249
0
  cur = xmlHashLookup3(table, name, NULL, elem);
3250
0
    return(cur);
3251
0
}
3252
3253
/**
3254
 * xmlGetDtdQAttrDesc:
3255
 * @dtd:  a pointer to the DtD to search
3256
 * @elem:  the element name
3257
 * @name:  the attribute name
3258
 * @prefix:  the attribute namespace prefix
3259
 *
3260
 * Search the DTD for the description of this qualified attribute on
3261
 * this element.
3262
 *
3263
 * returns the xmlAttributePtr if found or NULL
3264
 */
3265
3266
xmlAttributePtr
3267
xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3268
0
            const xmlChar *prefix) {
3269
0
    xmlAttributeTablePtr table;
3270
3271
0
    if (dtd == NULL) return(NULL);
3272
0
    if (dtd->attributes == NULL) return(NULL);
3273
0
    table = (xmlAttributeTablePtr) dtd->attributes;
3274
3275
0
    return(xmlHashLookup3(table, name, prefix, elem));
3276
0
}
3277
3278
/**
3279
 * xmlGetDtdNotationDesc:
3280
 * @dtd:  a pointer to the DtD to search
3281
 * @name:  the notation name
3282
 *
3283
 * Search the DTD for the description of this notation
3284
 *
3285
 * returns the xmlNotationPtr if found or NULL
3286
 */
3287
3288
xmlNotationPtr
3289
0
xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3290
0
    xmlNotationTablePtr table;
3291
3292
0
    if (dtd == NULL) return(NULL);
3293
0
    if (dtd->notations == NULL) return(NULL);
3294
0
    table = (xmlNotationTablePtr) dtd->notations;
3295
3296
0
    return(xmlHashLookup(table, name));
3297
0
}
3298
3299
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
3300
/**
3301
 * xmlValidateNotationUse:
3302
 * @ctxt:  the validation context
3303
 * @doc:  the document
3304
 * @notationName:  the notation name to check
3305
 *
3306
 * Validate that the given name match a notation declaration.
3307
 * - [ VC: Notation Declared ]
3308
 *
3309
 * returns 1 if valid or 0 otherwise
3310
 */
3311
3312
int
3313
xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3314
0
                       const xmlChar *notationName) {
3315
0
    xmlNotationPtr notaDecl;
3316
0
    if ((doc == NULL) || (doc->intSubset == NULL) ||
3317
0
        (notationName == NULL)) return(-1);
3318
3319
0
    notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3320
0
    if ((notaDecl == NULL) && (doc->extSubset != NULL))
3321
0
  notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3322
3323
0
    if ((notaDecl == NULL) && (ctxt != NULL)) {
3324
0
  xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3325
0
                  "NOTATION %s is not declared\n",
3326
0
            notationName, NULL, NULL);
3327
0
  return(0);
3328
0
    }
3329
0
    return(1);
3330
0
}
3331
#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
3332
3333
/**
3334
 * xmlIsMixedElement:
3335
 * @doc:  the document
3336
 * @name:  the element name
3337
 *
3338
 * Search in the DtDs whether an element accept Mixed content (or ANY)
3339
 * basically if it is supposed to accept text childs
3340
 *
3341
 * returns 0 if no, 1 if yes, and -1 if no element description is available
3342
 */
3343
3344
int
3345
0
xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3346
0
    xmlElementPtr elemDecl;
3347
3348
0
    if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3349
3350
0
    elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3351
0
    if ((elemDecl == NULL) && (doc->extSubset != NULL))
3352
0
  elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3353
0
    if (elemDecl == NULL) return(-1);
3354
0
    switch (elemDecl->etype) {
3355
0
  case XML_ELEMENT_TYPE_UNDEFINED:
3356
0
      return(-1);
3357
0
  case XML_ELEMENT_TYPE_ELEMENT:
3358
0
      return(0);
3359
0
        case XML_ELEMENT_TYPE_EMPTY:
3360
      /*
3361
       * return 1 for EMPTY since we want VC error to pop up
3362
       * on <empty>     </empty> for example
3363
       */
3364
0
  case XML_ELEMENT_TYPE_ANY:
3365
0
  case XML_ELEMENT_TYPE_MIXED:
3366
0
      return(1);
3367
0
    }
3368
0
    return(1);
3369
0
}
3370
3371
#ifdef LIBXML_VALID_ENABLED
3372
3373
static int
3374
0
xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
3375
0
    if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3376
        /*
3377
   * Use the new checks of production [4] [4a] amd [5] of the
3378
   * Update 5 of XML-1.0
3379
   */
3380
0
  if (((c >= 'a') && (c <= 'z')) ||
3381
0
      ((c >= 'A') && (c <= 'Z')) ||
3382
0
      (c == '_') || (c == ':') ||
3383
0
      ((c >= 0xC0) && (c <= 0xD6)) ||
3384
0
      ((c >= 0xD8) && (c <= 0xF6)) ||
3385
0
      ((c >= 0xF8) && (c <= 0x2FF)) ||
3386
0
      ((c >= 0x370) && (c <= 0x37D)) ||
3387
0
      ((c >= 0x37F) && (c <= 0x1FFF)) ||
3388
0
      ((c >= 0x200C) && (c <= 0x200D)) ||
3389
0
      ((c >= 0x2070) && (c <= 0x218F)) ||
3390
0
      ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3391
0
      ((c >= 0x3001) && (c <= 0xD7FF)) ||
3392
0
      ((c >= 0xF900) && (c <= 0xFDCF)) ||
3393
0
      ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3394
0
      ((c >= 0x10000) && (c <= 0xEFFFF)))
3395
0
      return(1);
3396
0
    } else {
3397
0
        if (IS_LETTER(c) || (c == '_') || (c == ':'))
3398
0
      return(1);
3399
0
    }
3400
0
    return(0);
3401
0
}
3402
3403
static int
3404
0
xmlIsDocNameChar(xmlDocPtr doc, int c) {
3405
0
    if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3406
        /*
3407
   * Use the new checks of production [4] [4a] amd [5] of the
3408
   * Update 5 of XML-1.0
3409
   */
3410
0
  if (((c >= 'a') && (c <= 'z')) ||
3411
0
      ((c >= 'A') && (c <= 'Z')) ||
3412
0
      ((c >= '0') && (c <= '9')) || /* !start */
3413
0
      (c == '_') || (c == ':') ||
3414
0
      (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3415
0
      ((c >= 0xC0) && (c <= 0xD6)) ||
3416
0
      ((c >= 0xD8) && (c <= 0xF6)) ||
3417
0
      ((c >= 0xF8) && (c <= 0x2FF)) ||
3418
0
      ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3419
0
      ((c >= 0x370) && (c <= 0x37D)) ||
3420
0
      ((c >= 0x37F) && (c <= 0x1FFF)) ||
3421
0
      ((c >= 0x200C) && (c <= 0x200D)) ||
3422
0
      ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3423
0
      ((c >= 0x2070) && (c <= 0x218F)) ||
3424
0
      ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3425
0
      ((c >= 0x3001) && (c <= 0xD7FF)) ||
3426
0
      ((c >= 0xF900) && (c <= 0xFDCF)) ||
3427
0
      ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3428
0
      ((c >= 0x10000) && (c <= 0xEFFFF)))
3429
0
       return(1);
3430
0
    } else {
3431
0
        if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3432
0
            (c == '.') || (c == '-') ||
3433
0
      (c == '_') || (c == ':') ||
3434
0
      (IS_COMBINING(c)) ||
3435
0
      (IS_EXTENDER(c)))
3436
0
      return(1);
3437
0
    }
3438
0
    return(0);
3439
0
}
3440
3441
/**
3442
 * xmlValidateNameValue:
3443
 * @doc:  pointer to the document or NULL
3444
 * @value:  an Name value
3445
 *
3446
 * Validate that the given value match Name production
3447
 *
3448
 * returns 1 if valid or 0 otherwise
3449
 */
3450
3451
static int
3452
0
xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
3453
0
    const xmlChar *cur;
3454
0
    int val, len;
3455
3456
0
    if (value == NULL) return(0);
3457
0
    cur = value;
3458
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3459
0
    cur += len;
3460
0
    if (!xmlIsDocNameStartChar(doc, val))
3461
0
  return(0);
3462
3463
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3464
0
    cur += len;
3465
0
    while (xmlIsDocNameChar(doc, val)) {
3466
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3467
0
  cur += len;
3468
0
    }
3469
3470
0
    if (val != 0) return(0);
3471
3472
0
    return(1);
3473
0
}
3474
3475
/**
3476
 * xmlValidateNameValue:
3477
 * @value:  an Name value
3478
 *
3479
 * Validate that the given value match Name production
3480
 *
3481
 * returns 1 if valid or 0 otherwise
3482
 */
3483
3484
int
3485
0
xmlValidateNameValue(const xmlChar *value) {
3486
0
    return(xmlValidateNameValueInternal(NULL, value));
3487
0
}
3488
3489
/**
3490
 * xmlValidateNamesValueInternal:
3491
 * @doc:  pointer to the document or NULL
3492
 * @value:  an Names value
3493
 *
3494
 * Validate that the given value match Names production
3495
 *
3496
 * returns 1 if valid or 0 otherwise
3497
 */
3498
3499
static int
3500
0
xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
3501
0
    const xmlChar *cur;
3502
0
    int val, len;
3503
3504
0
    if (value == NULL) return(0);
3505
0
    cur = value;
3506
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3507
0
    cur += len;
3508
3509
0
    if (!xmlIsDocNameStartChar(doc, val))
3510
0
  return(0);
3511
3512
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3513
0
    cur += len;
3514
0
    while (xmlIsDocNameChar(doc, val)) {
3515
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3516
0
  cur += len;
3517
0
    }
3518
3519
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3520
0
    while (val == 0x20) {
3521
0
  while (val == 0x20) {
3522
0
      val = xmlStringCurrentChar(NULL, cur, &len);
3523
0
      cur += len;
3524
0
  }
3525
3526
0
  if (!xmlIsDocNameStartChar(doc, val))
3527
0
      return(0);
3528
3529
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3530
0
  cur += len;
3531
3532
0
  while (xmlIsDocNameChar(doc, val)) {
3533
0
      val = xmlStringCurrentChar(NULL, cur, &len);
3534
0
      cur += len;
3535
0
  }
3536
0
    }
3537
3538
0
    if (val != 0) return(0);
3539
3540
0
    return(1);
3541
0
}
3542
3543
/**
3544
 * xmlValidateNamesValue:
3545
 * @value:  an Names value
3546
 *
3547
 * Validate that the given value match Names production
3548
 *
3549
 * returns 1 if valid or 0 otherwise
3550
 */
3551
3552
int
3553
0
xmlValidateNamesValue(const xmlChar *value) {
3554
0
    return(xmlValidateNamesValueInternal(NULL, value));
3555
0
}
3556
3557
/**
3558
 * xmlValidateNmtokenValueInternal:
3559
 * @doc:  pointer to the document or NULL
3560
 * @value:  an Nmtoken value
3561
 *
3562
 * Validate that the given value match Nmtoken production
3563
 *
3564
 * [ VC: Name Token ]
3565
 *
3566
 * returns 1 if valid or 0 otherwise
3567
 */
3568
3569
static int
3570
0
xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
3571
0
    const xmlChar *cur;
3572
0
    int val, len;
3573
3574
0
    if (value == NULL) return(0);
3575
0
    cur = value;
3576
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3577
0
    cur += len;
3578
3579
0
    if (!xmlIsDocNameChar(doc, val))
3580
0
  return(0);
3581
3582
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3583
0
    cur += len;
3584
0
    while (xmlIsDocNameChar(doc, val)) {
3585
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3586
0
  cur += len;
3587
0
    }
3588
3589
0
    if (val != 0) return(0);
3590
3591
0
    return(1);
3592
0
}
3593
3594
/**
3595
 * xmlValidateNmtokenValue:
3596
 * @value:  an Nmtoken value
3597
 *
3598
 * Validate that the given value match Nmtoken production
3599
 *
3600
 * [ VC: Name Token ]
3601
 *
3602
 * returns 1 if valid or 0 otherwise
3603
 */
3604
3605
int
3606
0
xmlValidateNmtokenValue(const xmlChar *value) {
3607
0
    return(xmlValidateNmtokenValueInternal(NULL, value));
3608
0
}
3609
3610
/**
3611
 * xmlValidateNmtokensValueInternal:
3612
 * @doc:  pointer to the document or NULL
3613
 * @value:  an Nmtokens value
3614
 *
3615
 * Validate that the given value match Nmtokens production
3616
 *
3617
 * [ VC: Name Token ]
3618
 *
3619
 * returns 1 if valid or 0 otherwise
3620
 */
3621
3622
static int
3623
0
xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
3624
0
    const xmlChar *cur;
3625
0
    int val, len;
3626
3627
0
    if (value == NULL) return(0);
3628
0
    cur = value;
3629
0
    val = xmlStringCurrentChar(NULL, cur, &len);
3630
0
    cur += len;
3631
3632
0
    while (IS_BLANK(val)) {
3633
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3634
0
  cur += len;
3635
0
    }
3636
3637
0
    if (!xmlIsDocNameChar(doc, val))
3638
0
  return(0);
3639
3640
0
    while (xmlIsDocNameChar(doc, val)) {
3641
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3642
0
  cur += len;
3643
0
    }
3644
3645
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3646
0
    while (val == 0x20) {
3647
0
  while (val == 0x20) {
3648
0
      val = xmlStringCurrentChar(NULL, cur, &len);
3649
0
      cur += len;
3650
0
  }
3651
0
  if (val == 0) return(1);
3652
3653
0
  if (!xmlIsDocNameChar(doc, val))
3654
0
      return(0);
3655
3656
0
  val = xmlStringCurrentChar(NULL, cur, &len);
3657
0
  cur += len;
3658
3659
0
  while (xmlIsDocNameChar(doc, val)) {
3660
0
      val = xmlStringCurrentChar(NULL, cur, &len);
3661
0
      cur += len;
3662
0
  }
3663
0
    }
3664
3665
0
    if (val != 0) return(0);
3666
3667
0
    return(1);
3668
0
}
3669
3670
/**
3671
 * xmlValidateNmtokensValue:
3672
 * @value:  an Nmtokens value
3673
 *
3674
 * Validate that the given value match Nmtokens production
3675
 *
3676
 * [ VC: Name Token ]
3677
 *
3678
 * returns 1 if valid or 0 otherwise
3679
 */
3680
3681
int
3682
0
xmlValidateNmtokensValue(const xmlChar *value) {
3683
0
    return(xmlValidateNmtokensValueInternal(NULL, value));
3684
0
}
3685
3686
/**
3687
 * xmlValidateNotationDecl:
3688
 * @ctxt:  the validation context
3689
 * @doc:  a document instance
3690
 * @nota:  a notation definition
3691
 *
3692
 * Try to validate a single notation definition
3693
 * basically it does the following checks as described by the
3694
 * XML-1.0 recommendation:
3695
 *  - it seems that no validity constraint exists on notation declarations
3696
 * But this function get called anyway ...
3697
 *
3698
 * returns 1 if valid or 0 otherwise
3699
 */
3700
3701
int
3702
xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3703
0
                         xmlNotationPtr nota ATTRIBUTE_UNUSED) {
3704
0
    int ret = 1;
3705
3706
0
    return(ret);
3707
0
}
3708
3709
/**
3710
 * xmlValidateAttributeValueInternal:
3711
 * @doc: the document
3712
 * @type:  an attribute type
3713
 * @value:  an attribute value
3714
 *
3715
 * Validate that the given attribute value match  the proper production
3716
 *
3717
 * returns 1 if valid or 0 otherwise
3718
 */
3719
3720
static int
3721
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
3722
0
                                  const xmlChar *value) {
3723
0
    switch (type) {
3724
0
  case XML_ATTRIBUTE_ENTITIES:
3725
0
  case XML_ATTRIBUTE_IDREFS:
3726
0
      return(xmlValidateNamesValueInternal(doc, value));
3727
0
  case XML_ATTRIBUTE_ENTITY:
3728
0
  case XML_ATTRIBUTE_IDREF:
3729
0
  case XML_ATTRIBUTE_ID:
3730
0
  case XML_ATTRIBUTE_NOTATION:
3731
0
      return(xmlValidateNameValueInternal(doc, value));
3732
0
  case XML_ATTRIBUTE_NMTOKENS:
3733
0
  case XML_ATTRIBUTE_ENUMERATION:
3734
0
      return(xmlValidateNmtokensValueInternal(doc, value));
3735
0
  case XML_ATTRIBUTE_NMTOKEN:
3736
0
      return(xmlValidateNmtokenValueInternal(doc, value));
3737
0
        case XML_ATTRIBUTE_CDATA:
3738
0
      break;
3739
0
    }
3740
0
    return(1);
3741
0
}
3742
3743
/**
3744
 * xmlValidateAttributeValue:
3745
 * @type:  an attribute type
3746
 * @value:  an attribute value
3747
 *
3748
 * Validate that the given attribute value match  the proper production
3749
 *
3750
 * [ VC: ID ]
3751
 * Values of type ID must match the Name production....
3752
 *
3753
 * [ VC: IDREF ]
3754
 * Values of type IDREF must match the Name production, and values
3755
 * of type IDREFS must match Names ...
3756
 *
3757
 * [ VC: Entity Name ]
3758
 * Values of type ENTITY must match the Name production, values
3759
 * of type ENTITIES must match Names ...
3760
 *
3761
 * [ VC: Name Token ]
3762
 * Values of type NMTOKEN must match the Nmtoken production; values
3763
 * of type NMTOKENS must match Nmtokens.
3764
 *
3765
 * returns 1 if valid or 0 otherwise
3766
 */
3767
int
3768
0
xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3769
0
    return(xmlValidateAttributeValueInternal(NULL, type, value));
3770
0
}
3771
3772
/**
3773
 * xmlValidateAttributeValue2:
3774
 * @ctxt:  the validation context
3775
 * @doc:  the document
3776
 * @name:  the attribute name (used for error reporting only)
3777
 * @type:  the attribute type
3778
 * @value:  the attribute value
3779
 *
3780
 * Validate that the given attribute value match a given type.
3781
 * This typically cannot be done before having finished parsing
3782
 * the subsets.
3783
 *
3784
 * [ VC: IDREF ]
3785
 * Values of type IDREF must match one of the declared IDs
3786
 * Values of type IDREFS must match a sequence of the declared IDs
3787
 * each Name must match the value of an ID attribute on some element
3788
 * in the XML document; i.e. IDREF values must match the value of
3789
 * some ID attribute
3790
 *
3791
 * [ VC: Entity Name ]
3792
 * Values of type ENTITY must match one declared entity
3793
 * Values of type ENTITIES must match a sequence of declared entities
3794
 *
3795
 * [ VC: Notation Attributes ]
3796
 * all notation names in the declaration must be declared.
3797
 *
3798
 * returns 1 if valid or 0 otherwise
3799
 */
3800
3801
static int
3802
xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3803
0
      const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3804
0
    int ret = 1;
3805
0
    switch (type) {
3806
0
  case XML_ATTRIBUTE_IDREFS:
3807
0
  case XML_ATTRIBUTE_IDREF:
3808
0
  case XML_ATTRIBUTE_ID:
3809
0
  case XML_ATTRIBUTE_NMTOKENS:
3810
0
  case XML_ATTRIBUTE_ENUMERATION:
3811
0
  case XML_ATTRIBUTE_NMTOKEN:
3812
0
        case XML_ATTRIBUTE_CDATA:
3813
0
      break;
3814
0
  case XML_ATTRIBUTE_ENTITY: {
3815
0
      xmlEntityPtr ent;
3816
3817
0
      ent = xmlGetDocEntity(doc, value);
3818
      /* yeah it's a bit messy... */
3819
0
      if ((ent == NULL) && (doc->standalone == 1)) {
3820
0
    doc->standalone = 0;
3821
0
    ent = xmlGetDocEntity(doc, value);
3822
0
      }
3823
0
      if (ent == NULL) {
3824
0
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3825
0
        XML_DTD_UNKNOWN_ENTITY,
3826
0
   "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3827
0
           name, value, NULL);
3828
0
    ret = 0;
3829
0
      } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3830
0
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3831
0
        XML_DTD_ENTITY_TYPE,
3832
0
   "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3833
0
           name, value, NULL);
3834
0
    ret = 0;
3835
0
      }
3836
0
      break;
3837
0
        }
3838
0
  case XML_ATTRIBUTE_ENTITIES: {
3839
0
      xmlChar *dup, *nam = NULL, *cur, save;
3840
0
      xmlEntityPtr ent;
3841
3842
0
      dup = xmlStrdup(value);
3843
0
      if (dup == NULL)
3844
0
    return(0);
3845
0
      cur = dup;
3846
0
      while (*cur != 0) {
3847
0
    nam = cur;
3848
0
    while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
3849
0
    save = *cur;
3850
0
    *cur = 0;
3851
0
    ent = xmlGetDocEntity(doc, nam);
3852
0
    if (ent == NULL) {
3853
0
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3854
0
            XML_DTD_UNKNOWN_ENTITY,
3855
0
       "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3856
0
         name, nam, NULL);
3857
0
        ret = 0;
3858
0
    } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3859
0
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3860
0
            XML_DTD_ENTITY_TYPE,
3861
0
       "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3862
0
         name, nam, NULL);
3863
0
        ret = 0;
3864
0
    }
3865
0
    if (save == 0)
3866
0
        break;
3867
0
    *cur = save;
3868
0
    while (IS_BLANK_CH(*cur)) cur++;
3869
0
      }
3870
0
      xmlFree(dup);
3871
0
      break;
3872
0
  }
3873
0
  case XML_ATTRIBUTE_NOTATION: {
3874
0
      xmlNotationPtr nota;
3875
3876
0
      nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3877
0
      if ((nota == NULL) && (doc->extSubset != NULL))
3878
0
    nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3879
3880
0
      if (nota == NULL) {
3881
0
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3882
0
                    XML_DTD_UNKNOWN_NOTATION,
3883
0
       "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3884
0
           name, value, NULL);
3885
0
    ret = 0;
3886
0
      }
3887
0
      break;
3888
0
        }
3889
0
    }
3890
0
    return(ret);
3891
0
}
3892
3893
/**
3894
 * xmlValidCtxtNormalizeAttributeValue:
3895
 * @ctxt: the validation context
3896
 * @doc:  the document
3897
 * @elem:  the parent
3898
 * @name:  the attribute name
3899
 * @value:  the attribute value
3900
 * @ctxt:  the validation context or NULL
3901
 *
3902
 * Does the validation related extra step of the normalization of attribute
3903
 * values:
3904
 *
3905
 * If the declared value is not CDATA, then the XML processor must further
3906
 * process the normalized attribute value by discarding any leading and
3907
 * trailing space (#x20) characters, and by replacing sequences of space
3908
 * (#x20) characters by single space (#x20) character.
3909
 *
3910
 * Also  check VC: Standalone Document Declaration in P32, and update
3911
 *  ctxt->valid accordingly
3912
 *
3913
 * returns a new normalized string if normalization is needed, NULL otherwise
3914
 *      the caller must free the returned value.
3915
 */
3916
3917
xmlChar *
3918
xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3919
0
       xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3920
0
    xmlChar *ret;
3921
0
    xmlAttributePtr attrDecl = NULL;
3922
0
    int extsubset = 0;
3923
3924
0
    if (doc == NULL) return(NULL);
3925
0
    if (elem == NULL) return(NULL);
3926
0
    if (name == NULL) return(NULL);
3927
0
    if (value == NULL) return(NULL);
3928
3929
0
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3930
0
  xmlChar fn[50];
3931
0
  xmlChar *fullname;
3932
3933
0
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3934
0
  if (fullname == NULL)
3935
0
      return(NULL);
3936
0
  attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, name);
3937
0
  if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3938
0
      attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname, name);
3939
0
      if (attrDecl != NULL)
3940
0
    extsubset = 1;
3941
0
  }
3942
0
  if ((fullname != fn) && (fullname != elem->name))
3943
0
      xmlFree(fullname);
3944
0
    }
3945
0
    if ((attrDecl == NULL) && (doc->intSubset != NULL))
3946
0
  attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3947
0
    if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3948
0
  attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3949
0
  if (attrDecl != NULL)
3950
0
      extsubset = 1;
3951
0
    }
3952
3953
0
    if (attrDecl == NULL)
3954
0
  return(NULL);
3955
0
    if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3956
0
  return(NULL);
3957
3958
0
    ret = xmlStrdup(value);
3959
0
    if (ret == NULL)
3960
0
  return(NULL);
3961
0
    xmlValidNormalizeString(ret);
3962
0
    if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3963
0
  xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
3964
0
"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3965
0
         name, elem->name, NULL);
3966
0
  ctxt->valid = 0;
3967
0
    }
3968
0
    return(ret);
3969
0
}
3970
3971
/**
3972
 * xmlValidNormalizeAttributeValue:
3973
 * @doc:  the document
3974
 * @elem:  the parent
3975
 * @name:  the attribute name
3976
 * @value:  the attribute value
3977
 *
3978
 * Does the validation related extra step of the normalization of attribute
3979
 * values:
3980
 *
3981
 * If the declared value is not CDATA, then the XML processor must further
3982
 * process the normalized attribute value by discarding any leading and
3983
 * trailing space (#x20) characters, and by replacing sequences of space
3984
 * (#x20) characters by single space (#x20) character.
3985
 *
3986
 * Returns a new normalized string if normalization is needed, NULL otherwise
3987
 *      the caller must free the returned value.
3988
 */
3989
3990
xmlChar *
3991
xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3992
0
              const xmlChar *name, const xmlChar *value) {
3993
0
    xmlChar *ret;
3994
0
    xmlAttributePtr attrDecl = NULL;
3995
3996
0
    if (doc == NULL) return(NULL);
3997
0
    if (elem == NULL) return(NULL);
3998
0
    if (name == NULL) return(NULL);
3999
0
    if (value == NULL) return(NULL);
4000
4001
0
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4002
0
  xmlChar fn[50];
4003
0
  xmlChar *fullname;
4004
4005
0
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4006
0
  if (fullname == NULL)
4007
0
      return(NULL);
4008
0
  if ((fullname != fn) && (fullname != elem->name))
4009
0
      xmlFree(fullname);
4010
0
    }
4011
0
    attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
4012
0
    if ((attrDecl == NULL) && (doc->extSubset != NULL))
4013
0
  attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
4014
4015
0
    if (attrDecl == NULL)
4016
0
  return(NULL);
4017
0
    if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
4018
0
  return(NULL);
4019
4020
0
    ret = xmlStrdup(value);
4021
0
    if (ret == NULL)
4022
0
  return(NULL);
4023
0
    xmlValidNormalizeString(ret);
4024
0
    return(ret);
4025
0
}
4026
4027
static void
4028
xmlValidateAttributeIdCallback(void *payload, void *data,
4029
0
                         const xmlChar *name ATTRIBUTE_UNUSED) {
4030
0
    xmlAttributePtr attr = (xmlAttributePtr) payload;
4031
0
    int *count = (int *) data;
4032
0
    if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
4033
0
}
4034
4035
/**
4036
 * xmlValidateAttributeDecl:
4037
 * @ctxt:  the validation context
4038
 * @doc:  a document instance
4039
 * @attr:  an attribute definition
4040
 *
4041
 * Try to validate a single attribute definition
4042
 * basically it does the following checks as described by the
4043
 * XML-1.0 recommendation:
4044
 *  - [ VC: Attribute Default Legal ]
4045
 *  - [ VC: Enumeration ]
4046
 *  - [ VC: ID Attribute Default ]
4047
 *
4048
 * The ID/IDREF uniqueness and matching are done separately
4049
 *
4050
 * returns 1 if valid or 0 otherwise
4051
 */
4052
4053
int
4054
xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4055
0
                         xmlAttributePtr attr) {
4056
0
    int ret = 1;
4057
0
    int val;
4058
0
    CHECK_DTD;
4059
0
    if(attr == NULL) return(1);
4060
4061
    /* Attribute Default Legal */
4062
    /* Enumeration */
4063
0
    if (attr->defaultValue != NULL) {
4064
0
  val = xmlValidateAttributeValueInternal(doc, attr->atype,
4065
0
                                          attr->defaultValue);
4066
0
  if (val == 0) {
4067
0
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
4068
0
         "Syntax of default value for attribute %s of %s is not valid\n",
4069
0
             attr->name, attr->elem, NULL);
4070
0
  }
4071
0
        ret &= val;
4072
0
    }
4073
4074
    /* ID Attribute Default */
4075
0
    if ((attr->atype == XML_ATTRIBUTE_ID)&&
4076
0
        (attr->def != XML_ATTRIBUTE_IMPLIED) &&
4077
0
  (attr->def != XML_ATTRIBUTE_REQUIRED)) {
4078
0
  xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
4079
0
          "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
4080
0
         attr->name, attr->elem, NULL);
4081
0
  ret = 0;
4082
0
    }
4083
4084
    /* One ID per Element Type */
4085
0
    if (attr->atype == XML_ATTRIBUTE_ID) {
4086
0
        int nbId;
4087
4088
  /* the trick is that we parse DtD as their own internal subset */
4089
0
        xmlElementPtr elem = xmlGetDtdElementDesc(doc->intSubset,
4090
0
                                            attr->elem);
4091
0
  if (elem != NULL) {
4092
0
      nbId = xmlScanIDAttributeDecl(NULL, elem, 0);
4093
0
  } else {
4094
0
      xmlAttributeTablePtr table;
4095
4096
      /*
4097
       * The attribute may be declared in the internal subset and the
4098
       * element in the external subset.
4099
       */
4100
0
      nbId = 0;
4101
0
      if (doc->intSubset != NULL) {
4102
0
    table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4103
0
    xmlHashScan3(table, NULL, NULL, attr->elem,
4104
0
           xmlValidateAttributeIdCallback, &nbId);
4105
0
      }
4106
0
  }
4107
0
  if (nbId > 1) {
4108
4109
0
      xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4110
0
       "Element %s has %d ID attribute defined in the internal subset : %s\n",
4111
0
       attr->elem, nbId, attr->name);
4112
0
  } else if (doc->extSubset != NULL) {
4113
0
      int extId = 0;
4114
0
      elem = xmlGetDtdElementDesc(doc->extSubset, attr->elem);
4115
0
      if (elem != NULL) {
4116
0
    extId = xmlScanIDAttributeDecl(NULL, elem, 0);
4117
0
      }
4118
0
      if (extId > 1) {
4119
0
    xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4120
0
       "Element %s has %d ID attribute defined in the external subset : %s\n",
4121
0
           attr->elem, extId, attr->name);
4122
0
      } else if (extId + nbId > 1) {
4123
0
    xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4124
0
"Element %s has ID attributes defined in the internal and external subset : %s\n",
4125
0
           attr->elem, attr->name, NULL);
4126
0
      }
4127
0
  }
4128
0
    }
4129
4130
    /* Validity Constraint: Enumeration */
4131
0
    if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4132
0
        xmlEnumerationPtr tree = attr->tree;
4133
0
  while (tree != NULL) {
4134
0
      if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4135
0
      tree = tree->next;
4136
0
  }
4137
0
  if (tree == NULL) {
4138
0
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
4139
0
"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4140
0
       attr->defaultValue, attr->name, attr->elem);
4141
0
      ret = 0;
4142
0
  }
4143
0
    }
4144
4145
0
    return(ret);
4146
0
}
4147
4148
/**
4149
 * xmlValidateElementDecl:
4150
 * @ctxt:  the validation context
4151
 * @doc:  a document instance
4152
 * @elem:  an element definition
4153
 *
4154
 * Try to validate a single element definition
4155
 * basically it does the following checks as described by the
4156
 * XML-1.0 recommendation:
4157
 *  - [ VC: One ID per Element Type ]
4158
 *  - [ VC: No Duplicate Types ]
4159
 *  - [ VC: Unique Element Type Declaration ]
4160
 *
4161
 * returns 1 if valid or 0 otherwise
4162
 */
4163
4164
int
4165
xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4166
0
                       xmlElementPtr elem) {
4167
0
    int ret = 1;
4168
0
    xmlElementPtr tst;
4169
4170
0
    CHECK_DTD;
4171
4172
0
    if (elem == NULL) return(1);
4173
4174
#if 0
4175
#ifdef LIBXML_REGEXP_ENABLED
4176
    /* Build the regexp associated to the content model */
4177
    ret = xmlValidBuildContentModel(ctxt, elem);
4178
#endif
4179
#endif
4180
4181
    /* No Duplicate Types */
4182
0
    if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4183
0
  xmlElementContentPtr cur, next;
4184
0
        const xmlChar *name;
4185
4186
0
  cur = elem->content;
4187
0
  while (cur != NULL) {
4188
0
      if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4189
0
      if (cur->c1 == NULL) break;
4190
0
      if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4191
0
    name = cur->c1->name;
4192
0
    next = cur->c2;
4193
0
    while (next != NULL) {
4194
0
        if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
4195
0
            if ((xmlStrEqual(next->name, name)) &&
4196
0
          (xmlStrEqual(next->prefix, cur->c1->prefix))) {
4197
0
          if (cur->c1->prefix == NULL) {
4198
0
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4199
0
       "Definition of %s has duplicate references of %s\n",
4200
0
               elem->name, name, NULL);
4201
0
          } else {
4202
0
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4203
0
       "Definition of %s has duplicate references of %s:%s\n",
4204
0
               elem->name, cur->c1->prefix, name);
4205
0
          }
4206
0
          ret = 0;
4207
0
      }
4208
0
      break;
4209
0
        }
4210
0
        if (next->c1 == NULL) break;
4211
0
        if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
4212
0
        if ((xmlStrEqual(next->c1->name, name)) &&
4213
0
            (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
4214
0
      if (cur->c1->prefix == NULL) {
4215
0
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4216
0
         "Definition of %s has duplicate references to %s\n",
4217
0
           elem->name, name, NULL);
4218
0
      } else {
4219
0
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4220
0
         "Definition of %s has duplicate references to %s:%s\n",
4221
0
           elem->name, cur->c1->prefix, name);
4222
0
      }
4223
0
      ret = 0;
4224
0
        }
4225
0
        next = next->c2;
4226
0
    }
4227
0
      }
4228
0
      cur = cur->c2;
4229
0
  }
4230
0
    }
4231
4232
    /* VC: Unique Element Type Declaration */
4233
0
    tst = xmlGetDtdElementDesc(doc->intSubset, elem->name);
4234
0
    if ((tst != NULL ) && (tst != elem) &&
4235
0
  ((tst->prefix == elem->prefix) ||
4236
0
   (xmlStrEqual(tst->prefix, elem->prefix))) &&
4237
0
  (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4238
0
  xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4239
0
                  "Redefinition of element %s\n",
4240
0
           elem->name, NULL, NULL);
4241
0
  ret = 0;
4242
0
    }
4243
0
    tst = xmlGetDtdElementDesc(doc->extSubset, elem->name);
4244
0
    if ((tst != NULL ) && (tst != elem) &&
4245
0
  ((tst->prefix == elem->prefix) ||
4246
0
   (xmlStrEqual(tst->prefix, elem->prefix))) &&
4247
0
  (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4248
0
  xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4249
0
                  "Redefinition of element %s\n",
4250
0
           elem->name, NULL, NULL);
4251
0
  ret = 0;
4252
0
    }
4253
    /* One ID per Element Type
4254
     * already done when registering the attribute
4255
    if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4256
  ret = 0;
4257
    } */
4258
0
    return(ret);
4259
0
}
4260
4261
/**
4262
 * xmlValidateOneAttribute:
4263
 * @ctxt:  the validation context
4264
 * @doc:  a document instance
4265
 * @elem:  an element instance
4266
 * @attr:  an attribute instance
4267
 * @value:  the attribute value (without entities processing)
4268
 *
4269
 * Try to validate a single attribute for an element
4270
 * basically it does the following checks as described by the
4271
 * XML-1.0 recommendation:
4272
 *  - [ VC: Attribute Value Type ]
4273
 *  - [ VC: Fixed Attribute Default ]
4274
 *  - [ VC: Entity Name ]
4275
 *  - [ VC: Name Token ]
4276
 *  - [ VC: ID ]
4277
 *  - [ VC: IDREF ]
4278
 *  - [ VC: Entity Name ]
4279
 *  - [ VC: Notation Attributes ]
4280
 *
4281
 * The ID/IDREF uniqueness and matching are done separately
4282
 *
4283
 * returns 1 if valid or 0 otherwise
4284
 */
4285
4286
int
4287
xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4288
                        xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4289
0
{
4290
0
    xmlAttributePtr attrDecl =  NULL;
4291
0
    int val;
4292
0
    int ret = 1;
4293
4294
0
    CHECK_DTD;
4295
0
    if ((elem == NULL) || (elem->name == NULL)) return(0);
4296
0
    if ((attr == NULL) || (attr->name == NULL)) return(0);
4297
4298
0
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4299
0
  xmlChar fn[50];
4300
0
  xmlChar *fullname;
4301
4302
0
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4303
0
  if (fullname == NULL)
4304
0
      return(0);
4305
0
  if (attr->ns != NULL) {
4306
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4307
0
                              attr->name, attr->ns->prefix);
4308
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4309
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4310
0
                attr->name, attr->ns->prefix);
4311
0
  } else {
4312
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname, attr->name);
4313
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4314
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4315
0
               fullname, attr->name);
4316
0
  }
4317
0
  if ((fullname != fn) && (fullname != elem->name))
4318
0
      xmlFree(fullname);
4319
0
    }
4320
0
    if (attrDecl == NULL) {
4321
0
  if (attr->ns != NULL) {
4322
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4323
0
                              attr->name, attr->ns->prefix);
4324
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4325
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4326
0
                attr->name, attr->ns->prefix);
4327
0
  } else {
4328
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4329
0
                             elem->name, attr->name);
4330
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4331
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4332
0
               elem->name, attr->name);
4333
0
  }
4334
0
    }
4335
4336
4337
    /* Validity Constraint: Attribute Value Type */
4338
0
    if (attrDecl == NULL) {
4339
0
  xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4340
0
         "No declaration for attribute %s of element %s\n",
4341
0
         attr->name, elem->name, NULL);
4342
0
  return(0);
4343
0
    }
4344
0
    attr->atype = attrDecl->atype;
4345
4346
0
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4347
0
    if (val == 0) {
4348
0
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4349
0
     "Syntax of value for attribute %s of %s is not valid\n",
4350
0
         attr->name, elem->name, NULL);
4351
0
        ret = 0;
4352
0
    }
4353
4354
    /* Validity constraint: Fixed Attribute Default */
4355
0
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4356
0
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4357
0
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4358
0
     "Value for attribute %s of %s is different from default \"%s\"\n",
4359
0
       attr->name, elem->name, attrDecl->defaultValue);
4360
0
      ret = 0;
4361
0
  }
4362
0
    }
4363
4364
    /* Validity Constraint: ID uniqueness */
4365
0
    if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4366
0
        if (xmlAddID(ctxt, doc, value, attr) == NULL)
4367
0
      ret = 0;
4368
0
    }
4369
4370
0
    if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4371
0
  (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4372
0
        if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4373
0
      ret = 0;
4374
0
    }
4375
4376
    /* Validity Constraint: Notation Attributes */
4377
0
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4378
0
        xmlEnumerationPtr tree = attrDecl->tree;
4379
0
        xmlNotationPtr nota;
4380
4381
        /* First check that the given NOTATION was declared */
4382
0
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4383
0
  if (nota == NULL)
4384
0
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4385
4386
0
  if (nota == NULL) {
4387
0
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4388
0
       "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
4389
0
       value, attr->name, elem->name);
4390
0
      ret = 0;
4391
0
        }
4392
4393
  /* Second, verify that it's among the list */
4394
0
  while (tree != NULL) {
4395
0
      if (xmlStrEqual(tree->name, value)) break;
4396
0
      tree = tree->next;
4397
0
  }
4398
0
  if (tree == NULL) {
4399
0
      xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4400
0
"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
4401
0
       value, attr->name, elem->name);
4402
0
      ret = 0;
4403
0
  }
4404
0
    }
4405
4406
    /* Validity Constraint: Enumeration */
4407
0
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4408
0
        xmlEnumerationPtr tree = attrDecl->tree;
4409
0
  while (tree != NULL) {
4410
0
      if (xmlStrEqual(tree->name, value)) break;
4411
0
      tree = tree->next;
4412
0
  }
4413
0
  if (tree == NULL) {
4414
0
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4415
0
       "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4416
0
       value, attr->name, elem->name);
4417
0
      ret = 0;
4418
0
  }
4419
0
    }
4420
4421
    /* Fixed Attribute Default */
4422
0
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4423
0
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4424
0
  xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4425
0
     "Value for attribute %s of %s must be \"%s\"\n",
4426
0
         attr->name, elem->name, attrDecl->defaultValue);
4427
0
        ret = 0;
4428
0
    }
4429
4430
    /* Extra check for the attribute value */
4431
0
    ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4432
0
              attrDecl->atype, value);
4433
4434
0
    return(ret);
4435
0
}
4436
4437
/**
4438
 * xmlValidateOneNamespace:
4439
 * @ctxt:  the validation context
4440
 * @doc:  a document instance
4441
 * @elem:  an element instance
4442
 * @prefix:  the namespace prefix
4443
 * @ns:  an namespace declaration instance
4444
 * @value:  the attribute value (without entities processing)
4445
 *
4446
 * Try to validate a single namespace declaration for an element
4447
 * basically it does the following checks as described by the
4448
 * XML-1.0 recommendation:
4449
 *  - [ VC: Attribute Value Type ]
4450
 *  - [ VC: Fixed Attribute Default ]
4451
 *  - [ VC: Entity Name ]
4452
 *  - [ VC: Name Token ]
4453
 *  - [ VC: ID ]
4454
 *  - [ VC: IDREF ]
4455
 *  - [ VC: Entity Name ]
4456
 *  - [ VC: Notation Attributes ]
4457
 *
4458
 * The ID/IDREF uniqueness and matching are done separately
4459
 *
4460
 * returns 1 if valid or 0 otherwise
4461
 */
4462
4463
int
4464
xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4465
0
xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4466
    /* xmlElementPtr elemDecl; */
4467
0
    xmlAttributePtr attrDecl =  NULL;
4468
0
    int val;
4469
0
    int ret = 1;
4470
4471
0
    CHECK_DTD;
4472
0
    if ((elem == NULL) || (elem->name == NULL)) return(0);
4473
0
    if ((ns == NULL) || (ns->href == NULL)) return(0);
4474
4475
0
    if (prefix != NULL) {
4476
0
  xmlChar fn[50];
4477
0
  xmlChar *fullname;
4478
4479
0
  fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4480
0
  if (fullname == NULL) {
4481
0
      xmlVErrMemory(ctxt, "Validating namespace");
4482
0
      return(0);
4483
0
  }
4484
0
  if (ns->prefix != NULL) {
4485
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4486
0
                              ns->prefix, BAD_CAST "xmlns");
4487
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4488
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4489
0
            ns->prefix, BAD_CAST "xmlns");
4490
0
  } else {
4491
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset, fullname,
4492
0
                             BAD_CAST "xmlns");
4493
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4494
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset, fullname,
4495
0
                       BAD_CAST "xmlns");
4496
0
  }
4497
0
  if ((fullname != fn) && (fullname != elem->name))
4498
0
      xmlFree(fullname);
4499
0
    }
4500
0
    if (attrDecl == NULL) {
4501
0
  if (ns->prefix != NULL) {
4502
0
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4503
0
                              ns->prefix, BAD_CAST "xmlns");
4504
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4505
0
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4506
0
                ns->prefix, BAD_CAST "xmlns");
4507
0
  } else {
4508
0
      attrDecl = xmlGetDtdAttrDesc(doc->intSubset,
4509
0
                             elem->name, BAD_CAST "xmlns");
4510
0
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4511
0
    attrDecl = xmlGetDtdAttrDesc(doc->extSubset,
4512
0
               elem->name, BAD_CAST "xmlns");
4513
0
  }
4514
0
    }
4515
4516
4517
    /* Validity Constraint: Attribute Value Type */
4518
0
    if (attrDecl == NULL) {
4519
0
  if (ns->prefix != NULL) {
4520
0
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4521
0
       "No declaration for attribute xmlns:%s of element %s\n",
4522
0
       ns->prefix, elem->name, NULL);
4523
0
  } else {
4524
0
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4525
0
       "No declaration for attribute xmlns of element %s\n",
4526
0
       elem->name, NULL, NULL);
4527
0
  }
4528
0
  return(0);
4529
0
    }
4530
4531
0
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4532
0
    if (val == 0) {
4533
0
  if (ns->prefix != NULL) {
4534
0
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4535
0
         "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4536
0
       ns->prefix, elem->name, NULL);
4537
0
  } else {
4538
0
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4539
0
         "Syntax of value for attribute xmlns of %s is not valid\n",
4540
0
       elem->name, NULL, NULL);
4541
0
  }
4542
0
        ret = 0;
4543
0
    }
4544
4545
    /* Validity constraint: Fixed Attribute Default */
4546
0
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4547
0
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4548
0
      if (ns->prefix != NULL) {
4549
0
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4550
0
       "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4551
0
           ns->prefix, elem->name, attrDecl->defaultValue);
4552
0
      } else {
4553
0
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4554
0
       "Value for attribute xmlns of %s is different from default \"%s\"\n",
4555
0
           elem->name, attrDecl->defaultValue, NULL);
4556
0
      }
4557
0
      ret = 0;
4558
0
  }
4559
0
    }
4560
4561
    /*
4562
     * Casting ns to xmlAttrPtr is wrong. We'd need separate functions
4563
     * xmlAddID and xmlAddRef for namespace declarations, but it makes
4564
     * no practical sense to use ID types anyway.
4565
     */
4566
#if 0
4567
    /* Validity Constraint: ID uniqueness */
4568
    if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4569
        if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4570
      ret = 0;
4571
    }
4572
4573
    if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4574
  (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4575
        if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4576
      ret = 0;
4577
    }
4578
#endif
4579
4580
    /* Validity Constraint: Notation Attributes */
4581
0
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4582
0
        xmlEnumerationPtr tree = attrDecl->tree;
4583
0
        xmlNotationPtr nota;
4584
4585
        /* First check that the given NOTATION was declared */
4586
0
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4587
0
  if (nota == NULL)
4588
0
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4589
4590
0
  if (nota == NULL) {
4591
0
      if (ns->prefix != NULL) {
4592
0
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4593
0
       "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4594
0
           value, ns->prefix, elem->name);
4595
0
      } else {
4596
0
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4597
0
       "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4598
0
           value, elem->name, NULL);
4599
0
      }
4600
0
      ret = 0;
4601
0
        }
4602
4603
  /* Second, verify that it's among the list */
4604
0
  while (tree != NULL) {
4605
0
      if (xmlStrEqual(tree->name, value)) break;
4606
0
      tree = tree->next;
4607
0
  }
4608
0
  if (tree == NULL) {
4609
0
      if (ns->prefix != NULL) {
4610
0
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4611
0
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4612
0
           value, ns->prefix, elem->name);
4613
0
      } else {
4614
0
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4615
0
"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4616
0
           value, elem->name, NULL);
4617
0
      }
4618
0
      ret = 0;
4619
0
  }
4620
0
    }
4621
4622
    /* Validity Constraint: Enumeration */
4623
0
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4624
0
        xmlEnumerationPtr tree = attrDecl->tree;
4625
0
  while (tree != NULL) {
4626
0
      if (xmlStrEqual(tree->name, value)) break;
4627
0
      tree = tree->next;
4628
0
  }
4629
0
  if (tree == NULL) {
4630
0
      if (ns->prefix != NULL) {
4631
0
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4632
0
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4633
0
           value, ns->prefix, elem->name);
4634
0
      } else {
4635
0
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4636
0
"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4637
0
           value, elem->name, NULL);
4638
0
      }
4639
0
      ret = 0;
4640
0
  }
4641
0
    }
4642
4643
    /* Fixed Attribute Default */
4644
0
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4645
0
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4646
0
  if (ns->prefix != NULL) {
4647
0
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4648
0
       "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4649
0
       ns->prefix, elem->name, attrDecl->defaultValue);
4650
0
  } else {
4651
0
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4652
0
       "Value for attribute xmlns of %s must be \"%s\"\n",
4653
0
       elem->name, attrDecl->defaultValue, NULL);
4654
0
  }
4655
0
        ret = 0;
4656
0
    }
4657
4658
    /* Extra check for the attribute value */
4659
0
    if (ns->prefix != NULL) {
4660
0
  ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4661
0
            attrDecl->atype, value);
4662
0
    } else {
4663
0
  ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4664
0
            attrDecl->atype, value);
4665
0
    }
4666
4667
0
    return(ret);
4668
0
}
4669
4670
#ifndef  LIBXML_REGEXP_ENABLED
4671
/**
4672
 * xmlValidateSkipIgnorable:
4673
 * @ctxt:  the validation context
4674
 * @child:  the child list
4675
 *
4676
 * Skip ignorable elements w.r.t. the validation process
4677
 *
4678
 * returns the first element to consider for validation of the content model
4679
 */
4680
4681
static xmlNodePtr
4682
xmlValidateSkipIgnorable(xmlNodePtr child) {
4683
    while (child != NULL) {
4684
  switch (child->type) {
4685
      /* These things are ignored (skipped) during validation.  */
4686
      case XML_PI_NODE:
4687
      case XML_COMMENT_NODE:
4688
      case XML_XINCLUDE_START:
4689
      case XML_XINCLUDE_END:
4690
    child = child->next;
4691
    break;
4692
      case XML_TEXT_NODE:
4693
    if (xmlIsBlankNode(child))
4694
        child = child->next;
4695
    else
4696
        return(child);
4697
    break;
4698
      /* keep current node */
4699
      default:
4700
    return(child);
4701
  }
4702
    }
4703
    return(child);
4704
}
4705
4706
/**
4707
 * xmlValidateElementType:
4708
 * @ctxt:  the validation context
4709
 *
4710
 * Try to validate the content model of an element internal function
4711
 *
4712
 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4713
 *           reference is found and -3 if the validation succeeded but
4714
 *           the content model is not determinist.
4715
 */
4716
4717
static int
4718
xmlValidateElementType(xmlValidCtxtPtr ctxt) {
4719
    int ret = -1;
4720
    int determinist = 1;
4721
4722
    NODE = xmlValidateSkipIgnorable(NODE);
4723
    if ((NODE == NULL) && (CONT == NULL))
4724
  return(1);
4725
    if ((NODE == NULL) &&
4726
  ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4727
   (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4728
  return(1);
4729
    }
4730
    if (CONT == NULL) return(-1);
4731
    if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
4732
  return(-2);
4733
4734
    /*
4735
     * We arrive here when more states need to be examined
4736
     */
4737
cont:
4738
4739
    /*
4740
     * We just recovered from a rollback generated by a possible
4741
     * epsilon transition, go directly to the analysis phase
4742
     */
4743
    if (STATE == ROLLBACK_PARENT) {
4744
  ret = 1;
4745
  goto analyze;
4746
    }
4747
4748
    /*
4749
     * we may have to save a backup state here. This is the equivalent
4750
     * of handling epsilon transition in NFAs.
4751
     */
4752
    if ((CONT != NULL) &&
4753
  ((CONT->parent == NULL) ||
4754
   (CONT->parent == (xmlElementContentPtr) 1) ||
4755
   (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
4756
  ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4757
   (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
4758
   ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
4759
  if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4760
      return(0);
4761
    }
4762
4763
4764
    /*
4765
     * Check first if the content matches
4766
     */
4767
    switch (CONT->type) {
4768
  case XML_ELEMENT_CONTENT_PCDATA:
4769
      if (NODE == NULL) {
4770
    ret = 0;
4771
    break;
4772
      }
4773
      if (NODE->type == XML_TEXT_NODE) {
4774
    /*
4775
     * go to next element in the content model
4776
     * skipping ignorable elems
4777
     */
4778
    do {
4779
        NODE = NODE->next;
4780
        NODE = xmlValidateSkipIgnorable(NODE);
4781
        if ((NODE != NULL) &&
4782
      (NODE->type == XML_ENTITY_REF_NODE))
4783
      return(-2);
4784
    } while ((NODE != NULL) &&
4785
       ((NODE->type != XML_ELEMENT_NODE) &&
4786
        (NODE->type != XML_TEXT_NODE) &&
4787
        (NODE->type != XML_CDATA_SECTION_NODE)));
4788
                ret = 1;
4789
    break;
4790
      } else {
4791
    ret = 0;
4792
    break;
4793
      }
4794
      break;
4795
  case XML_ELEMENT_CONTENT_ELEMENT:
4796
      if (NODE == NULL) {
4797
    ret = 0;
4798
    break;
4799
      }
4800
      ret = ((NODE->type == XML_ELEMENT_NODE) &&
4801
       (xmlStrEqual(NODE->name, CONT->name)));
4802
      if (ret == 1) {
4803
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4804
        ret = (CONT->prefix == NULL);
4805
    } else if (CONT->prefix == NULL) {
4806
        ret = 0;
4807
    } else {
4808
        ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4809
    }
4810
      }
4811
      if (ret == 1) {
4812
    /*
4813
     * go to next element in the content model
4814
     * skipping ignorable elems
4815
     */
4816
    do {
4817
        NODE = NODE->next;
4818
        NODE = xmlValidateSkipIgnorable(NODE);
4819
        if ((NODE != NULL) &&
4820
      (NODE->type == XML_ENTITY_REF_NODE))
4821
      return(-2);
4822
    } while ((NODE != NULL) &&
4823
       ((NODE->type != XML_ELEMENT_NODE) &&
4824
        (NODE->type != XML_TEXT_NODE) &&
4825
        (NODE->type != XML_CDATA_SECTION_NODE)));
4826
      } else {
4827
    ret = 0;
4828
    break;
4829
      }
4830
      break;
4831
  case XML_ELEMENT_CONTENT_OR:
4832
      /*
4833
       * Small optimization.
4834
       */
4835
      if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4836
    if ((NODE == NULL) ||
4837
        (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4838
        DEPTH++;
4839
        CONT = CONT->c2;
4840
        goto cont;
4841
    }
4842
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4843
        ret = (CONT->c1->prefix == NULL);
4844
    } else if (CONT->c1->prefix == NULL) {
4845
        ret = 0;
4846
    } else {
4847
        ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4848
    }
4849
    if (ret == 0) {
4850
        DEPTH++;
4851
        CONT = CONT->c2;
4852
        goto cont;
4853
    }
4854
      }
4855
4856
      /*
4857
       * save the second branch 'or' branch
4858
       */
4859
      if (vstateVPush(ctxt, CONT->c2, NODE, DEPTH + 1,
4860
          OCCURS, ROLLBACK_OR) < 0)
4861
    return(-1);
4862
      DEPTH++;
4863
      CONT = CONT->c1;
4864
      goto cont;
4865
  case XML_ELEMENT_CONTENT_SEQ:
4866
      /*
4867
       * Small optimization.
4868
       */
4869
      if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4870
    ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4871
     (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4872
    if ((NODE == NULL) ||
4873
        (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4874
        DEPTH++;
4875
        CONT = CONT->c2;
4876
        goto cont;
4877
    }
4878
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4879
        ret = (CONT->c1->prefix == NULL);
4880
    } else if (CONT->c1->prefix == NULL) {
4881
        ret = 0;
4882
    } else {
4883
        ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4884
    }
4885
    if (ret == 0) {
4886
        DEPTH++;
4887
        CONT = CONT->c2;
4888
        goto cont;
4889
    }
4890
      }
4891
      DEPTH++;
4892
      CONT = CONT->c1;
4893
      goto cont;
4894
    }
4895
4896
    /*
4897
     * At this point handle going up in the tree
4898
     */
4899
    if (ret == -1) {
4900
  return(ret);
4901
    }
4902
analyze:
4903
    while (CONT != NULL) {
4904
  /*
4905
   * First do the analysis depending on the occurrence model at
4906
   * this level.
4907
   */
4908
  if (ret == 0) {
4909
      switch (CONT->ocur) {
4910
    xmlNodePtr cur;
4911
4912
    case XML_ELEMENT_CONTENT_ONCE:
4913
        cur = ctxt->vstate->node;
4914
        if (vstateVPop(ctxt) < 0 ) {
4915
      return(0);
4916
        }
4917
        if (cur != ctxt->vstate->node)
4918
      determinist = -3;
4919
        goto cont;
4920
    case XML_ELEMENT_CONTENT_PLUS:
4921
        if (OCCURRENCE == 0) {
4922
      cur = ctxt->vstate->node;
4923
      if (vstateVPop(ctxt) < 0 ) {
4924
          return(0);
4925
      }
4926
      if (cur != ctxt->vstate->node)
4927
          determinist = -3;
4928
      goto cont;
4929
        }
4930
        ret = 1;
4931
        break;
4932
    case XML_ELEMENT_CONTENT_MULT:
4933
        ret = 1;
4934
        break;
4935
    case XML_ELEMENT_CONTENT_OPT:
4936
        ret = 1;
4937
        break;
4938
      }
4939
  } else {
4940
      switch (CONT->ocur) {
4941
    case XML_ELEMENT_CONTENT_OPT:
4942
        ret = 1;
4943
        break;
4944
    case XML_ELEMENT_CONTENT_ONCE:
4945
        ret = 1;
4946
        break;
4947
    case XML_ELEMENT_CONTENT_PLUS:
4948
        if (STATE == ROLLBACK_PARENT) {
4949
      ret = 1;
4950
      break;
4951
        }
4952
        if (NODE == NULL) {
4953
      ret = 1;
4954
      break;
4955
        }
4956
        SET_OCCURRENCE;
4957
        goto cont;
4958
    case XML_ELEMENT_CONTENT_MULT:
4959
        if (STATE == ROLLBACK_PARENT) {
4960
      ret = 1;
4961
      break;
4962
        }
4963
        if (NODE == NULL) {
4964
      ret = 1;
4965
      break;
4966
        }
4967
        /* SET_OCCURRENCE; */
4968
        goto cont;
4969
      }
4970
  }
4971
  STATE = 0;
4972
4973
  /*
4974
   * Then act accordingly at the parent level
4975
   */
4976
  RESET_OCCURRENCE;
4977
  if ((CONT->parent == NULL) ||
4978
            (CONT->parent == (xmlElementContentPtr) 1))
4979
      break;
4980
4981
  switch (CONT->parent->type) {
4982
      case XML_ELEMENT_CONTENT_PCDATA:
4983
    return(-1);
4984
      case XML_ELEMENT_CONTENT_ELEMENT:
4985
    return(-1);
4986
      case XML_ELEMENT_CONTENT_OR:
4987
    if (ret == 1) {
4988
        CONT = CONT->parent;
4989
        DEPTH--;
4990
    } else {
4991
        CONT = CONT->parent;
4992
        DEPTH--;
4993
    }
4994
    break;
4995
      case XML_ELEMENT_CONTENT_SEQ:
4996
    if (ret == 0) {
4997
        CONT = CONT->parent;
4998
        DEPTH--;
4999
    } else if (CONT == CONT->parent->c1) {
5000
        CONT = CONT->parent->c2;
5001
        goto cont;
5002
    } else {
5003
        CONT = CONT->parent;
5004
        DEPTH--;
5005
    }
5006
  }
5007
    }
5008
    if (NODE != NULL) {
5009
  xmlNodePtr cur;
5010
5011
  cur = ctxt->vstate->node;
5012
  if (vstateVPop(ctxt) < 0 ) {
5013
      return(0);
5014
  }
5015
  if (cur != ctxt->vstate->node)
5016
      determinist = -3;
5017
  goto cont;
5018
    }
5019
    if (ret == 0) {
5020
  xmlNodePtr cur;
5021
5022
  cur = ctxt->vstate->node;
5023
  if (vstateVPop(ctxt) < 0 ) {
5024
      return(0);
5025
  }
5026
  if (cur != ctxt->vstate->node)
5027
      determinist = -3;
5028
  goto cont;
5029
    }
5030
    return(determinist);
5031
}
5032
#endif
5033
5034
/**
5035
 * xmlSnprintfElements:
5036
 * @buf:  an output buffer
5037
 * @size:  the size of the buffer
5038
 * @content:  An element
5039
 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
5040
 *
5041
 * This will dump the list of elements to the buffer
5042
 * Intended just for the debug routine
5043
 */
5044
static void
5045
0
xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
5046
0
    xmlNodePtr cur;
5047
0
    int len;
5048
5049
0
    if (node == NULL) return;
5050
0
    if (glob) strcat(buf, "(");
5051
0
    cur = node;
5052
0
    while (cur != NULL) {
5053
0
  len = strlen(buf);
5054
0
  if (size - len < 50) {
5055
0
      if ((size - len > 4) && (buf[len - 1] != '.'))
5056
0
    strcat(buf, " ...");
5057
0
      return;
5058
0
  }
5059
0
        switch (cur->type) {
5060
0
            case XML_ELEMENT_NODE:
5061
0
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5062
0
        if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
5063
0
      if ((size - len > 4) && (buf[len - 1] != '.'))
5064
0
          strcat(buf, " ...");
5065
0
      return;
5066
0
        }
5067
0
        strcat(buf, (char *) cur->ns->prefix);
5068
0
        strcat(buf, ":");
5069
0
    }
5070
0
                if (size - len < xmlStrlen(cur->name) + 10) {
5071
0
        if ((size - len > 4) && (buf[len - 1] != '.'))
5072
0
      strcat(buf, " ...");
5073
0
        return;
5074
0
    }
5075
0
          strcat(buf, (char *) cur->name);
5076
0
    if (cur->next != NULL)
5077
0
        strcat(buf, " ");
5078
0
    break;
5079
0
            case XML_TEXT_NODE:
5080
0
    if (xmlIsBlankNode(cur))
5081
0
        break;
5082
                /* Falls through. */
5083
0
            case XML_CDATA_SECTION_NODE:
5084
0
            case XML_ENTITY_REF_NODE:
5085
0
          strcat(buf, "CDATA");
5086
0
    if (cur->next != NULL)
5087
0
        strcat(buf, " ");
5088
0
    break;
5089
0
            case XML_ATTRIBUTE_NODE:
5090
0
            case XML_DOCUMENT_NODE:
5091
0
      case XML_HTML_DOCUMENT_NODE:
5092
0
            case XML_DOCUMENT_TYPE_NODE:
5093
0
            case XML_DOCUMENT_FRAG_NODE:
5094
0
            case XML_NOTATION_NODE:
5095
0
      case XML_NAMESPACE_DECL:
5096
0
          strcat(buf, "???");
5097
0
    if (cur->next != NULL)
5098
0
        strcat(buf, " ");
5099
0
    break;
5100
0
            case XML_ENTITY_NODE:
5101
0
            case XML_PI_NODE:
5102
0
            case XML_DTD_NODE:
5103
0
            case XML_COMMENT_NODE:
5104
0
      case XML_ELEMENT_DECL:
5105
0
      case XML_ATTRIBUTE_DECL:
5106
0
      case XML_ENTITY_DECL:
5107
0
      case XML_XINCLUDE_START:
5108
0
      case XML_XINCLUDE_END:
5109
0
    break;
5110
0
  }
5111
0
  cur = cur->next;
5112
0
    }
5113
0
    if (glob) strcat(buf, ")");
5114
0
}
5115
5116
/**
5117
 * xmlValidateElementContent:
5118
 * @ctxt:  the validation context
5119
 * @child:  the child list
5120
 * @elemDecl:  pointer to the element declaration
5121
 * @warn:  emit the error message
5122
 * @parent: the parent element (for error reporting)
5123
 *
5124
 * Try to validate the content model of an element
5125
 *
5126
 * returns 1 if valid or 0 if not and -1 in case of error
5127
 */
5128
5129
static int
5130
xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
5131
0
       xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
5132
0
    int ret = 1;
5133
#ifndef  LIBXML_REGEXP_ENABLED
5134
    xmlNodePtr repl = NULL, last = NULL, tmp;
5135
#endif
5136
0
    xmlNodePtr cur;
5137
0
    xmlElementContentPtr cont;
5138
0
    const xmlChar *name;
5139
5140
0
    if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
5141
0
  return(-1);
5142
0
    cont = elemDecl->content;
5143
0
    name = elemDecl->name;
5144
5145
0
#ifdef LIBXML_REGEXP_ENABLED
5146
    /* Build the regexp associated to the content model */
5147
0
    if (elemDecl->contModel == NULL)
5148
0
  ret = xmlValidBuildContentModel(ctxt, elemDecl);
5149
0
    if (elemDecl->contModel == NULL) {
5150
0
  return(-1);
5151
0
    } else {
5152
0
  xmlRegExecCtxtPtr exec;
5153
5154
0
  if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5155
0
      return(-1);
5156
0
  }
5157
0
  ctxt->nodeMax = 0;
5158
0
  ctxt->nodeNr = 0;
5159
0
  ctxt->nodeTab = NULL;
5160
0
  exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5161
0
  if (exec != NULL) {
5162
0
      cur = child;
5163
0
      while (cur != NULL) {
5164
0
    switch (cur->type) {
5165
0
        case XML_ENTITY_REF_NODE:
5166
      /*
5167
       * Push the current node to be able to roll back
5168
       * and process within the entity
5169
       */
5170
0
      if ((cur->children != NULL) &&
5171
0
          (cur->children->children != NULL)) {
5172
0
          nodeVPush(ctxt, cur);
5173
0
          cur = cur->children->children;
5174
0
          continue;
5175
0
      }
5176
0
      break;
5177
0
        case XML_TEXT_NODE:
5178
0
      if (xmlIsBlankNode(cur))
5179
0
          break;
5180
0
      ret = 0;
5181
0
      goto fail;
5182
0
        case XML_CDATA_SECTION_NODE:
5183
      /* TODO */
5184
0
      ret = 0;
5185
0
      goto fail;
5186
0
        case XML_ELEMENT_NODE:
5187
0
      if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5188
0
          xmlChar fn[50];
5189
0
          xmlChar *fullname;
5190
5191
0
          fullname = xmlBuildQName(cur->name,
5192
0
                             cur->ns->prefix, fn, 50);
5193
0
          if (fullname == NULL) {
5194
0
        ret = -1;
5195
0
        goto fail;
5196
0
          }
5197
0
                            ret = xmlRegExecPushString(exec, fullname, NULL);
5198
0
          if ((fullname != fn) && (fullname != cur->name))
5199
0
        xmlFree(fullname);
5200
0
      } else {
5201
0
          ret = xmlRegExecPushString(exec, cur->name, NULL);
5202
0
      }
5203
0
      break;
5204
0
        default:
5205
0
      break;
5206
0
    }
5207
    /*
5208
     * Switch to next element
5209
     */
5210
0
    cur = cur->next;
5211
0
    while (cur == NULL) {
5212
0
        cur = nodeVPop(ctxt);
5213
0
        if (cur == NULL)
5214
0
      break;
5215
0
        cur = cur->next;
5216
0
    }
5217
0
      }
5218
0
      ret = xmlRegExecPushString(exec, NULL, NULL);
5219
0
fail:
5220
0
      xmlRegFreeExecCtxt(exec);
5221
0
  }
5222
0
    }
5223
#else  /* LIBXML_REGEXP_ENABLED */
5224
    /*
5225
     * Allocate the stack
5226
     */
5227
    ctxt->vstateMax = 8;
5228
    ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5229
     ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5230
    if (ctxt->vstateTab == NULL) {
5231
  xmlVErrMemory(ctxt, "malloc failed");
5232
  return(-1);
5233
    }
5234
    /*
5235
     * The first entry in the stack is reserved to the current state
5236
     */
5237
    ctxt->nodeMax = 0;
5238
    ctxt->nodeNr = 0;
5239
    ctxt->nodeTab = NULL;
5240
    ctxt->vstate = &ctxt->vstateTab[0];
5241
    ctxt->vstateNr = 1;
5242
    CONT = cont;
5243
    NODE = child;
5244
    DEPTH = 0;
5245
    OCCURS = 0;
5246
    STATE = 0;
5247
    ret = xmlValidateElementType(ctxt);
5248
    if ((ret == -3) && (warn)) {
5249
  char expr[5000];
5250
  expr[0] = 0;
5251
  xmlSnprintfElementContent(expr, 5000, elemDecl->content, 1);
5252
  xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
5253
                XML_DTD_CONTENT_NOT_DETERMINIST,
5254
          "Content model of %s is not deterministic: %s\n",
5255
          name, BAD_CAST expr, NULL);
5256
    } else if (ret == -2) {
5257
  /*
5258
   * An entities reference appeared at this level.
5259
   * Build a minimal representation of this node content
5260
   * sufficient to run the validation process on it
5261
   */
5262
  cur = child;
5263
  while (cur != NULL) {
5264
      switch (cur->type) {
5265
    case XML_ENTITY_REF_NODE:
5266
        /*
5267
         * Push the current node to be able to roll back
5268
         * and process within the entity
5269
         */
5270
        if ((cur->children != NULL) &&
5271
      (cur->children->children != NULL)) {
5272
      nodeVPush(ctxt, cur);
5273
      cur = cur->children->children;
5274
      continue;
5275
        }
5276
        break;
5277
    case XML_TEXT_NODE:
5278
        if (xmlIsBlankNode(cur))
5279
      break;
5280
        /* no break on purpose */
5281
    case XML_CDATA_SECTION_NODE:
5282
        /* no break on purpose */
5283
    case XML_ELEMENT_NODE:
5284
        /*
5285
         * Allocate a new node and minimally fills in
5286
         * what's required
5287
         */
5288
        tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5289
        if (tmp == NULL) {
5290
      xmlVErrMemory(ctxt, "malloc failed");
5291
      xmlFreeNodeList(repl);
5292
      ret = -1;
5293
      goto done;
5294
        }
5295
        tmp->type = cur->type;
5296
        tmp->name = cur->name;
5297
        tmp->ns = cur->ns;
5298
        tmp->next = NULL;
5299
        tmp->content = NULL;
5300
        if (repl == NULL)
5301
      repl = last = tmp;
5302
        else {
5303
      last->next = tmp;
5304
      last = tmp;
5305
        }
5306
        if (cur->type == XML_CDATA_SECTION_NODE) {
5307
      /*
5308
       * E59 spaces in CDATA does not match the
5309
       * nonterminal S
5310
       */
5311
      tmp->content = xmlStrdup(BAD_CAST "CDATA");
5312
        }
5313
        break;
5314
    default:
5315
        break;
5316
      }
5317
      /*
5318
       * Switch to next element
5319
       */
5320
      cur = cur->next;
5321
      while (cur == NULL) {
5322
    cur = nodeVPop(ctxt);
5323
    if (cur == NULL)
5324
        break;
5325
    cur = cur->next;
5326
      }
5327
  }
5328
5329
  /*
5330
   * Relaunch the validation
5331
   */
5332
  ctxt->vstate = &ctxt->vstateTab[0];
5333
  ctxt->vstateNr = 1;
5334
  CONT = cont;
5335
  NODE = repl;
5336
  DEPTH = 0;
5337
  OCCURS = 0;
5338
  STATE = 0;
5339
  ret = xmlValidateElementType(ctxt);
5340
    }
5341
#endif /* LIBXML_REGEXP_ENABLED */
5342
0
    if ((warn) && ((ret != 1) && (ret != -3))) {
5343
0
  if (ctxt != NULL) {
5344
0
      char expr[5000];
5345
0
      char list[5000];
5346
5347
0
      expr[0] = 0;
5348
0
      xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
5349
0
      list[0] = 0;
5350
#ifndef LIBXML_REGEXP_ENABLED
5351
      if (repl != NULL)
5352
    xmlSnprintfElements(&list[0], 5000, repl, 1);
5353
      else
5354
#endif /* LIBXML_REGEXP_ENABLED */
5355
0
    xmlSnprintfElements(&list[0], 5000, child, 1);
5356
5357
0
      if (name != NULL) {
5358
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5359
0
     "Element %s content does not follow the DTD, expecting %s, got %s\n",
5360
0
           name, BAD_CAST expr, BAD_CAST list);
5361
0
      } else {
5362
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5363
0
     "Element content does not follow the DTD, expecting %s, got %s\n",
5364
0
           BAD_CAST expr, BAD_CAST list, NULL);
5365
0
      }
5366
0
  } else {
5367
0
      if (name != NULL) {
5368
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5369
0
           "Element %s content does not follow the DTD\n",
5370
0
           name, NULL, NULL);
5371
0
      } else {
5372
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5373
0
           "Element content does not follow the DTD\n",
5374
0
                    NULL, NULL, NULL);
5375
0
      }
5376
0
  }
5377
0
  ret = 0;
5378
0
    }
5379
0
    if (ret == -3)
5380
0
  ret = 1;
5381
5382
#ifndef  LIBXML_REGEXP_ENABLED
5383
done:
5384
    /*
5385
     * Deallocate the copy if done, and free up the validation stack
5386
     */
5387
    while (repl != NULL) {
5388
  tmp = repl->next;
5389
  xmlFree(repl);
5390
  repl = tmp;
5391
    }
5392
    ctxt->vstateMax = 0;
5393
    if (ctxt->vstateTab != NULL) {
5394
  xmlFree(ctxt->vstateTab);
5395
  ctxt->vstateTab = NULL;
5396
    }
5397
#endif
5398
0
    ctxt->nodeMax = 0;
5399
0
    ctxt->nodeNr = 0;
5400
0
    if (ctxt->nodeTab != NULL) {
5401
0
  xmlFree(ctxt->nodeTab);
5402
0
  ctxt->nodeTab = NULL;
5403
0
    }
5404
0
    return(ret);
5405
5406
0
}
5407
5408
/**
5409
 * xmlValidateCdataElement:
5410
 * @ctxt:  the validation context
5411
 * @doc:  a document instance
5412
 * @elem:  an element instance
5413
 *
5414
 * Check that an element follows #CDATA
5415
 *
5416
 * returns 1 if valid or 0 otherwise
5417
 */
5418
static int
5419
xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5420
0
                           xmlNodePtr elem) {
5421
0
    int ret = 1;
5422
0
    xmlNodePtr cur, child;
5423
5424
0
    if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5425
0
        (elem->type != XML_ELEMENT_NODE))
5426
0
  return(0);
5427
5428
0
    child = elem->children;
5429
5430
0
    cur = child;
5431
0
    while (cur != NULL) {
5432
0
  switch (cur->type) {
5433
0
      case XML_ENTITY_REF_NODE:
5434
    /*
5435
     * Push the current node to be able to roll back
5436
     * and process within the entity
5437
     */
5438
0
    if ((cur->children != NULL) &&
5439
0
        (cur->children->children != NULL)) {
5440
0
        nodeVPush(ctxt, cur);
5441
0
        cur = cur->children->children;
5442
0
        continue;
5443
0
    }
5444
0
    break;
5445
0
      case XML_COMMENT_NODE:
5446
0
      case XML_PI_NODE:
5447
0
      case XML_TEXT_NODE:
5448
0
      case XML_CDATA_SECTION_NODE:
5449
0
    break;
5450
0
      default:
5451
0
    ret = 0;
5452
0
    goto done;
5453
0
  }
5454
  /*
5455
   * Switch to next element
5456
   */
5457
0
  cur = cur->next;
5458
0
  while (cur == NULL) {
5459
0
      cur = nodeVPop(ctxt);
5460
0
      if (cur == NULL)
5461
0
    break;
5462
0
      cur = cur->next;
5463
0
  }
5464
0
    }
5465
0
done:
5466
0
    ctxt->nodeMax = 0;
5467
0
    ctxt->nodeNr = 0;
5468
0
    if (ctxt->nodeTab != NULL) {
5469
0
  xmlFree(ctxt->nodeTab);
5470
0
  ctxt->nodeTab = NULL;
5471
0
    }
5472
0
    return(ret);
5473
0
}
5474
5475
#ifdef LIBXML_REGEXP_ENABLED
5476
/**
5477
 * xmlValidateCheckMixed:
5478
 * @ctxt:  the validation context
5479
 * @cont:  the mixed content model
5480
 * @qname:  the qualified name as appearing in the serialization
5481
 *
5482
 * Check if the given node is part of the content model.
5483
 *
5484
 * Returns 1 if yes, 0 if no, -1 in case of error
5485
 */
5486
static int
5487
xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
5488
0
                xmlElementContentPtr cont, const xmlChar *qname) {
5489
0
    const xmlChar *name;
5490
0
    int plen;
5491
0
    name = xmlSplitQName3(qname, &plen);
5492
5493
0
    if (name == NULL) {
5494
0
  while (cont != NULL) {
5495
0
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5496
0
    if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5497
0
        return(1);
5498
0
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5499
0
         (cont->c1 != NULL) &&
5500
0
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5501
0
    if ((cont->c1->prefix == NULL) &&
5502
0
        (xmlStrEqual(cont->c1->name, qname)))
5503
0
        return(1);
5504
0
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5505
0
    (cont->c1 == NULL) ||
5506
0
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5507
0
    xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5508
0
      "Internal: MIXED struct corrupted\n",
5509
0
      NULL);
5510
0
    break;
5511
0
      }
5512
0
      cont = cont->c2;
5513
0
  }
5514
0
    } else {
5515
0
  while (cont != NULL) {
5516
0
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5517
0
    if ((cont->prefix != NULL) &&
5518
0
        (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5519
0
        (xmlStrEqual(cont->name, name)))
5520
0
        return(1);
5521
0
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5522
0
         (cont->c1 != NULL) &&
5523
0
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5524
0
    if ((cont->c1->prefix != NULL) &&
5525
0
        (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5526
0
        (xmlStrEqual(cont->c1->name, name)))
5527
0
        return(1);
5528
0
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5529
0
    (cont->c1 == NULL) ||
5530
0
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5531
0
    xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5532
0
      "Internal: MIXED struct corrupted\n",
5533
0
      NULL);
5534
0
    break;
5535
0
      }
5536
0
      cont = cont->c2;
5537
0
  }
5538
0
    }
5539
0
    return(0);
5540
0
}
5541
#endif /* LIBXML_REGEXP_ENABLED */
5542
5543
/**
5544
 * xmlValidGetElemDecl:
5545
 * @ctxt:  the validation context
5546
 * @doc:  a document instance
5547
 * @elem:  an element instance
5548
 * @extsubset:  pointer, (out) indicate if the declaration was found
5549
 *              in the external subset.
5550
 *
5551
 * Finds a declaration associated to an element in the document.
5552
 *
5553
 * returns the pointer to the declaration or NULL if not found.
5554
 */
5555
static xmlElementPtr
5556
xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5557
0
              xmlNodePtr elem, int *extsubset) {
5558
0
    xmlElementPtr elemDecl = NULL;
5559
0
    const xmlChar *prefix = NULL;
5560
5561
0
    if ((ctxt == NULL) || (doc == NULL) ||
5562
0
        (elem == NULL) || (elem->name == NULL))
5563
0
        return(NULL);
5564
0
    if (extsubset != NULL)
5565
0
  *extsubset = 0;
5566
5567
    /*
5568
     * Fetch the declaration for the qualified name
5569
     */
5570
0
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5571
0
  prefix = elem->ns->prefix;
5572
5573
0
    if (prefix != NULL) {
5574
0
  elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5575
0
                             elem->name, prefix);
5576
0
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5577
0
      elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5578
0
                                 elem->name, prefix);
5579
0
      if ((elemDecl != NULL) && (extsubset != NULL))
5580
0
    *extsubset = 1;
5581
0
  }
5582
0
    }
5583
5584
    /*
5585
     * Fetch the declaration for the non qualified name
5586
     * This is "non-strict" validation should be done on the
5587
     * full QName but in that case being flexible makes sense.
5588
     */
5589
0
    if (elemDecl == NULL) {
5590
0
  elemDecl = xmlGetDtdElementDesc(doc->intSubset, elem->name);
5591
0
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5592
0
      elemDecl = xmlGetDtdElementDesc(doc->extSubset, elem->name);
5593
0
      if ((elemDecl != NULL) && (extsubset != NULL))
5594
0
    *extsubset = 1;
5595
0
  }
5596
0
    }
5597
0
    if (elemDecl == NULL) {
5598
0
  xmlErrValidNode(ctxt, elem,
5599
0
      XML_DTD_UNKNOWN_ELEM,
5600
0
         "No declaration for element %s\n",
5601
0
         elem->name, NULL, NULL);
5602
0
    }
5603
0
    return(elemDecl);
5604
0
}
5605
5606
#ifdef LIBXML_REGEXP_ENABLED
5607
/**
5608
 * xmlValidatePushElement:
5609
 * @ctxt:  the validation context
5610
 * @doc:  a document instance
5611
 * @elem:  an element instance
5612
 * @qname:  the qualified name as appearing in the serialization
5613
 *
5614
 * Push a new element start on the validation stack.
5615
 *
5616
 * returns 1 if no validation problem was found or 0 otherwise
5617
 */
5618
int
5619
xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5620
0
                       xmlNodePtr elem, const xmlChar *qname) {
5621
0
    int ret = 1;
5622
0
    xmlElementPtr eDecl;
5623
0
    int extsubset = 0;
5624
5625
0
    if (ctxt == NULL)
5626
0
        return(0);
5627
/* printf("PushElem %s\n", qname); */
5628
0
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5629
0
  xmlValidStatePtr state = ctxt->vstate;
5630
0
  xmlElementPtr elemDecl;
5631
5632
  /*
5633
   * Check the new element against the content model of the new elem.
5634
   */
5635
0
  if (state->elemDecl != NULL) {
5636
0
      elemDecl = state->elemDecl;
5637
5638
0
      switch(elemDecl->etype) {
5639
0
    case XML_ELEMENT_TYPE_UNDEFINED:
5640
0
        ret = 0;
5641
0
        break;
5642
0
    case XML_ELEMENT_TYPE_EMPTY:
5643
0
        xmlErrValidNode(ctxt, state->node,
5644
0
            XML_DTD_NOT_EMPTY,
5645
0
         "Element %s was declared EMPTY this one has content\n",
5646
0
         state->node->name, NULL, NULL);
5647
0
        ret = 0;
5648
0
        break;
5649
0
    case XML_ELEMENT_TYPE_ANY:
5650
        /* I don't think anything is required then */
5651
0
        break;
5652
0
    case XML_ELEMENT_TYPE_MIXED:
5653
        /* simple case of declared as #PCDATA */
5654
0
        if ((elemDecl->content != NULL) &&
5655
0
      (elemDecl->content->type ==
5656
0
       XML_ELEMENT_CONTENT_PCDATA)) {
5657
0
      xmlErrValidNode(ctxt, state->node,
5658
0
          XML_DTD_NOT_PCDATA,
5659
0
         "Element %s was declared #PCDATA but contains non text nodes\n",
5660
0
        state->node->name, NULL, NULL);
5661
0
      ret = 0;
5662
0
        } else {
5663
0
      ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5664
0
                            qname);
5665
0
      if (ret != 1) {
5666
0
          xmlErrValidNode(ctxt, state->node,
5667
0
              XML_DTD_INVALID_CHILD,
5668
0
         "Element %s is not declared in %s list of possible children\n",
5669
0
            qname, state->node->name, NULL);
5670
0
      }
5671
0
        }
5672
0
        break;
5673
0
    case XML_ELEMENT_TYPE_ELEMENT:
5674
        /*
5675
         * TODO:
5676
         * VC: Standalone Document Declaration
5677
         *     - element types with element content, if white space
5678
         *       occurs directly within any instance of those types.
5679
         */
5680
0
        if (state->exec != NULL) {
5681
0
      ret = xmlRegExecPushString(state->exec, qname, NULL);
5682
0
      if (ret < 0) {
5683
0
          xmlErrValidNode(ctxt, state->node,
5684
0
              XML_DTD_CONTENT_MODEL,
5685
0
         "Element %s content does not follow the DTD, Misplaced %s\n",
5686
0
           state->node->name, qname, NULL);
5687
0
          ret = 0;
5688
0
      } else {
5689
0
          ret = 1;
5690
0
      }
5691
0
        }
5692
0
        break;
5693
0
      }
5694
0
  }
5695
0
    }
5696
0
    eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5697
0
    vstateVPush(ctxt, eDecl, elem);
5698
0
    return(ret);
5699
0
}
5700
5701
/**
5702
 * xmlValidatePushCData:
5703
 * @ctxt:  the validation context
5704
 * @data:  some character data read
5705
 * @len:  the length of the data
5706
 *
5707
 * check the CData parsed for validation in the current stack
5708
 *
5709
 * returns 1 if no validation problem was found or 0 otherwise
5710
 */
5711
int
5712
0
xmlValidatePushCData(xmlValidCtxtPtr ctxt, const xmlChar *data, int len) {
5713
0
    int ret = 1;
5714
5715
/* printf("CDATA %s %d\n", data, len); */
5716
0
    if (ctxt == NULL)
5717
0
        return(0);
5718
0
    if (len <= 0)
5719
0
  return(ret);
5720
0
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5721
0
  xmlValidStatePtr state = ctxt->vstate;
5722
0
  xmlElementPtr elemDecl;
5723
5724
  /*
5725
   * Check the new element against the content model of the new elem.
5726
   */
5727
0
  if (state->elemDecl != NULL) {
5728
0
      elemDecl = state->elemDecl;
5729
5730
0
      switch(elemDecl->etype) {
5731
0
    case XML_ELEMENT_TYPE_UNDEFINED:
5732
0
        ret = 0;
5733
0
        break;
5734
0
    case XML_ELEMENT_TYPE_EMPTY:
5735
0
        xmlErrValidNode(ctxt, state->node,
5736
0
            XML_DTD_NOT_EMPTY,
5737
0
         "Element %s was declared EMPTY this one has content\n",
5738
0
         state->node->name, NULL, NULL);
5739
0
        ret = 0;
5740
0
        break;
5741
0
    case XML_ELEMENT_TYPE_ANY:
5742
0
        break;
5743
0
    case XML_ELEMENT_TYPE_MIXED:
5744
0
        break;
5745
0
    case XML_ELEMENT_TYPE_ELEMENT: {
5746
0
                    int i;
5747
5748
0
                    for (i = 0;i < len;i++) {
5749
0
                        if (!IS_BLANK_CH(data[i])) {
5750
0
                            xmlErrValidNode(ctxt, state->node,
5751
0
                                            XML_DTD_CONTENT_MODEL,
5752
0
       "Element %s content does not follow the DTD, Text not allowed\n",
5753
0
                                   state->node->name, NULL, NULL);
5754
0
                            ret = 0;
5755
0
                            goto done;
5756
0
                        }
5757
0
                    }
5758
                    /*
5759
                     * TODO:
5760
                     * VC: Standalone Document Declaration
5761
                     *  element types with element content, if white space
5762
                     *  occurs directly within any instance of those types.
5763
                     */
5764
0
                    break;
5765
0
                }
5766
0
      }
5767
0
  }
5768
0
    }
5769
0
done:
5770
0
    return(ret);
5771
0
}
5772
5773
/**
5774
 * xmlValidatePopElement:
5775
 * @ctxt:  the validation context
5776
 * @doc:  a document instance
5777
 * @elem:  an element instance
5778
 * @qname:  the qualified name as appearing in the serialization
5779
 *
5780
 * Pop the element end from the validation stack.
5781
 *
5782
 * returns 1 if no validation problem was found or 0 otherwise
5783
 */
5784
int
5785
xmlValidatePopElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc ATTRIBUTE_UNUSED,
5786
                      xmlNodePtr elem ATTRIBUTE_UNUSED,
5787
0
          const xmlChar *qname ATTRIBUTE_UNUSED) {
5788
0
    int ret = 1;
5789
5790
0
    if (ctxt == NULL)
5791
0
        return(0);
5792
/* printf("PopElem %s\n", qname); */
5793
0
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5794
0
  xmlValidStatePtr state = ctxt->vstate;
5795
0
  xmlElementPtr elemDecl;
5796
5797
  /*
5798
   * Check the new element against the content model of the new elem.
5799
   */
5800
0
  if (state->elemDecl != NULL) {
5801
0
      elemDecl = state->elemDecl;
5802
5803
0
      if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5804
0
    if (state->exec != NULL) {
5805
0
        ret = xmlRegExecPushString(state->exec, NULL, NULL);
5806
0
        if (ret <= 0) {
5807
0
      xmlErrValidNode(ctxt, state->node,
5808
0
                      XML_DTD_CONTENT_MODEL,
5809
0
     "Element %s content does not follow the DTD, Expecting more children\n",
5810
0
             state->node->name, NULL,NULL);
5811
0
      ret = 0;
5812
0
        } else {
5813
      /*
5814
       * previous validation errors should not generate
5815
       * a new one here
5816
       */
5817
0
      ret = 1;
5818
0
        }
5819
0
    }
5820
0
      }
5821
0
  }
5822
0
  vstateVPop(ctxt);
5823
0
    }
5824
0
    return(ret);
5825
0
}
5826
#endif /* LIBXML_REGEXP_ENABLED */
5827
5828
/**
5829
 * xmlValidateOneElement:
5830
 * @ctxt:  the validation context
5831
 * @doc:  a document instance
5832
 * @elem:  an element instance
5833
 *
5834
 * Try to validate a single element and it's attributes,
5835
 * basically it does the following checks as described by the
5836
 * XML-1.0 recommendation:
5837
 *  - [ VC: Element Valid ]
5838
 *  - [ VC: Required Attribute ]
5839
 * Then call xmlValidateOneAttribute() for each attribute present.
5840
 *
5841
 * The ID/IDREF checkings are done separately
5842
 *
5843
 * returns 1 if valid or 0 otherwise
5844
 */
5845
5846
int
5847
xmlValidateOneElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5848
0
                      xmlNodePtr elem) {
5849
0
    xmlElementPtr elemDecl = NULL;
5850
0
    xmlElementContentPtr cont;
5851
0
    xmlAttributePtr attr;
5852
0
    xmlNodePtr child;
5853
0
    int ret = 1, tmp;
5854
0
    const xmlChar *name;
5855
0
    int extsubset = 0;
5856
5857
0
    CHECK_DTD;
5858
5859
0
    if (elem == NULL) return(0);
5860
0
    switch (elem->type) {
5861
0
        case XML_ATTRIBUTE_NODE:
5862
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5863
0
       "Attribute element not expected\n", NULL, NULL ,NULL);
5864
0
      return(0);
5865
0
        case XML_TEXT_NODE:
5866
0
      if (elem->children != NULL) {
5867
0
    xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5868
0
                    "Text element has children !\n",
5869
0
        NULL,NULL,NULL);
5870
0
    return(0);
5871
0
      }
5872
0
      if (elem->ns != NULL) {
5873
0
    xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5874
0
                    "Text element has namespace !\n",
5875
0
        NULL,NULL,NULL);
5876
0
    return(0);
5877
0
      }
5878
0
      if (elem->content == NULL) {
5879
0
    xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5880
0
                    "Text element has no content !\n",
5881
0
        NULL,NULL,NULL);
5882
0
    return(0);
5883
0
      }
5884
0
      return(1);
5885
0
        case XML_XINCLUDE_START:
5886
0
        case XML_XINCLUDE_END:
5887
0
            return(1);
5888
0
        case XML_CDATA_SECTION_NODE:
5889
0
        case XML_ENTITY_REF_NODE:
5890
0
        case XML_PI_NODE:
5891
0
        case XML_COMMENT_NODE:
5892
0
      return(1);
5893
0
        case XML_ENTITY_NODE:
5894
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5895
0
       "Entity element not expected\n", NULL, NULL ,NULL);
5896
0
      return(0);
5897
0
        case XML_NOTATION_NODE:
5898
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5899
0
       "Notation element not expected\n", NULL, NULL ,NULL);
5900
0
      return(0);
5901
0
        case XML_DOCUMENT_NODE:
5902
0
        case XML_DOCUMENT_TYPE_NODE:
5903
0
        case XML_DOCUMENT_FRAG_NODE:
5904
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5905
0
       "Document element not expected\n", NULL, NULL ,NULL);
5906
0
      return(0);
5907
0
        case XML_HTML_DOCUMENT_NODE:
5908
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5909
0
       "HTML Document not expected\n", NULL, NULL ,NULL);
5910
0
      return(0);
5911
0
        case XML_ELEMENT_NODE:
5912
0
      break;
5913
0
  default:
5914
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5915
0
       "unknown element type\n", NULL, NULL ,NULL);
5916
0
      return(0);
5917
0
    }
5918
5919
    /*
5920
     * Fetch the declaration
5921
     */
5922
0
    elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5923
0
    if (elemDecl == NULL)
5924
0
  return(0);
5925
5926
    /*
5927
     * If vstateNr is not zero that means continuous validation is
5928
     * activated, do not try to check the content model at that level.
5929
     */
5930
0
    if (ctxt->vstateNr == 0) {
5931
    /* Check that the element content matches the definition */
5932
0
    switch (elemDecl->etype) {
5933
0
        case XML_ELEMENT_TYPE_UNDEFINED:
5934
0
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5935
0
                      "No declaration for element %s\n",
5936
0
       elem->name, NULL, NULL);
5937
0
      return(0);
5938
0
        case XML_ELEMENT_TYPE_EMPTY:
5939
0
      if (elem->children != NULL) {
5940
0
    xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
5941
0
         "Element %s was declared EMPTY this one has content\n",
5942
0
                 elem->name, NULL, NULL);
5943
0
    ret = 0;
5944
0
      }
5945
0
      break;
5946
0
        case XML_ELEMENT_TYPE_ANY:
5947
      /* I don't think anything is required then */
5948
0
      break;
5949
0
        case XML_ELEMENT_TYPE_MIXED:
5950
5951
      /* simple case of declared as #PCDATA */
5952
0
      if ((elemDecl->content != NULL) &&
5953
0
    (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5954
0
    ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5955
0
    if (!ret) {
5956
0
        xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
5957
0
         "Element %s was declared #PCDATA but contains non text nodes\n",
5958
0
         elem->name, NULL, NULL);
5959
0
    }
5960
0
    break;
5961
0
      }
5962
0
      child = elem->children;
5963
      /* Hum, this start to get messy */
5964
0
      while (child != NULL) {
5965
0
          if (child->type == XML_ELEMENT_NODE) {
5966
0
        name = child->name;
5967
0
        if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
5968
0
      xmlChar fn[50];
5969
0
      xmlChar *fullname;
5970
5971
0
      fullname = xmlBuildQName(child->name, child->ns->prefix,
5972
0
                         fn, 50);
5973
0
      if (fullname == NULL)
5974
0
          return(0);
5975
0
      cont = elemDecl->content;
5976
0
      while (cont != NULL) {
5977
0
          if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5978
0
        if (xmlStrEqual(cont->name, fullname))
5979
0
            break;
5980
0
          } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5981
0
             (cont->c1 != NULL) &&
5982
0
             (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5983
0
        if (xmlStrEqual(cont->c1->name, fullname))
5984
0
            break;
5985
0
          } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5986
0
        (cont->c1 == NULL) ||
5987
0
        (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5988
0
        xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5989
0
          "Internal: MIXED struct corrupted\n",
5990
0
          NULL);
5991
0
        break;
5992
0
          }
5993
0
          cont = cont->c2;
5994
0
      }
5995
0
      if ((fullname != fn) && (fullname != child->name))
5996
0
          xmlFree(fullname);
5997
0
      if (cont != NULL)
5998
0
          goto child_ok;
5999
0
        }
6000
0
        cont = elemDecl->content;
6001
0
        while (cont != NULL) {
6002
0
            if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
6003
0
          if (xmlStrEqual(cont->name, name)) break;
6004
0
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
6005
0
         (cont->c1 != NULL) &&
6006
0
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
6007
0
          if (xmlStrEqual(cont->c1->name, name)) break;
6008
0
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
6009
0
          (cont->c1 == NULL) ||
6010
0
          (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
6011
0
          xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
6012
0
            "Internal: MIXED struct corrupted\n",
6013
0
            NULL);
6014
0
          break;
6015
0
      }
6016
0
      cont = cont->c2;
6017
0
        }
6018
0
        if (cont == NULL) {
6019
0
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
6020
0
         "Element %s is not declared in %s list of possible children\n",
6021
0
             name, elem->name, NULL);
6022
0
      ret = 0;
6023
0
        }
6024
0
    }
6025
0
child_ok:
6026
0
          child = child->next;
6027
0
      }
6028
0
      break;
6029
0
        case XML_ELEMENT_TYPE_ELEMENT:
6030
0
      if ((doc->standalone == 1) && (extsubset == 1)) {
6031
    /*
6032
     * VC: Standalone Document Declaration
6033
     *     - element types with element content, if white space
6034
     *       occurs directly within any instance of those types.
6035
     */
6036
0
    child = elem->children;
6037
0
    while (child != NULL) {
6038
0
        if (child->type == XML_TEXT_NODE) {
6039
0
      const xmlChar *content = child->content;
6040
6041
0
      while (IS_BLANK_CH(*content))
6042
0
          content++;
6043
0
      if (*content == 0) {
6044
0
          xmlErrValidNode(ctxt, elem,
6045
0
                          XML_DTD_STANDALONE_WHITE_SPACE,
6046
0
"standalone: %s declared in the external subset contains white spaces nodes\n",
6047
0
           elem->name, NULL, NULL);
6048
0
          ret = 0;
6049
0
          break;
6050
0
      }
6051
0
        }
6052
0
        child =child->next;
6053
0
    }
6054
0
      }
6055
0
      child = elem->children;
6056
0
      cont = elemDecl->content;
6057
0
      tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
6058
0
      if (tmp <= 0)
6059
0
    ret = tmp;
6060
0
      break;
6061
0
    }
6062
0
    } /* not continuous */
6063
6064
    /* [ VC: Required Attribute ] */
6065
0
    attr = elemDecl->attributes;
6066
0
    while (attr != NULL) {
6067
0
  if (attr->def == XML_ATTRIBUTE_REQUIRED) {
6068
0
      int qualified = -1;
6069
6070
0
      if ((attr->prefix == NULL) &&
6071
0
    (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6072
0
    xmlNsPtr ns;
6073
6074
0
    ns = elem->nsDef;
6075
0
    while (ns != NULL) {
6076
0
        if (ns->prefix == NULL)
6077
0
      goto found;
6078
0
        ns = ns->next;
6079
0
    }
6080
0
      } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6081
0
    xmlNsPtr ns;
6082
6083
0
    ns = elem->nsDef;
6084
0
    while (ns != NULL) {
6085
0
        if (xmlStrEqual(attr->name, ns->prefix))
6086
0
      goto found;
6087
0
        ns = ns->next;
6088
0
    }
6089
0
      } else {
6090
0
    xmlAttrPtr attrib;
6091
6092
0
    attrib = elem->properties;
6093
0
    while (attrib != NULL) {
6094
0
        if (xmlStrEqual(attrib->name, attr->name)) {
6095
0
      if (attr->prefix != NULL) {
6096
0
          xmlNsPtr nameSpace = attrib->ns;
6097
6098
0
          if (nameSpace == NULL)
6099
0
        nameSpace = elem->ns;
6100
          /*
6101
           * qualified names handling is problematic, having a
6102
           * different prefix should be possible but DTDs don't
6103
           * allow to define the URI instead of the prefix :-(
6104
           */
6105
0
          if (nameSpace == NULL) {
6106
0
        if (qualified < 0)
6107
0
            qualified = 0;
6108
0
          } else if (!xmlStrEqual(nameSpace->prefix,
6109
0
                attr->prefix)) {
6110
0
        if (qualified < 1)
6111
0
            qualified = 1;
6112
0
          } else
6113
0
        goto found;
6114
0
      } else {
6115
          /*
6116
           * We should allow applications to define namespaces
6117
           * for their application even if the DTD doesn't
6118
           * carry one, otherwise, basically we would always
6119
           * break.
6120
           */
6121
0
          goto found;
6122
0
      }
6123
0
        }
6124
0
        attrib = attrib->next;
6125
0
    }
6126
0
      }
6127
0
      if (qualified == -1) {
6128
0
    if (attr->prefix == NULL) {
6129
0
        xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6130
0
           "Element %s does not carry attribute %s\n",
6131
0
         elem->name, attr->name, NULL);
6132
0
        ret = 0;
6133
0
          } else {
6134
0
        xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
6135
0
           "Element %s does not carry attribute %s:%s\n",
6136
0
         elem->name, attr->prefix,attr->name);
6137
0
        ret = 0;
6138
0
    }
6139
0
      } else if (qualified == 0) {
6140
0
    xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
6141
0
       "Element %s required attribute %s:%s has no prefix\n",
6142
0
           elem->name, attr->prefix, attr->name);
6143
0
      } else if (qualified == 1) {
6144
0
    xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
6145
0
       "Element %s required attribute %s:%s has different prefix\n",
6146
0
           elem->name, attr->prefix, attr->name);
6147
0
      }
6148
0
  } else if (attr->def == XML_ATTRIBUTE_FIXED) {
6149
      /*
6150
       * Special tests checking #FIXED namespace declarations
6151
       * have the right value since this is not done as an
6152
       * attribute checking
6153
       */
6154
0
      if ((attr->prefix == NULL) &&
6155
0
    (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
6156
0
    xmlNsPtr ns;
6157
6158
0
    ns = elem->nsDef;
6159
0
    while (ns != NULL) {
6160
0
        if (ns->prefix == NULL) {
6161
0
      if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6162
0
          xmlErrValidNode(ctxt, elem,
6163
0
                 XML_DTD_ELEM_DEFAULT_NAMESPACE,
6164
0
   "Element %s namespace name for default namespace does not match the DTD\n",
6165
0
           elem->name, NULL, NULL);
6166
0
          ret = 0;
6167
0
      }
6168
0
      goto found;
6169
0
        }
6170
0
        ns = ns->next;
6171
0
    }
6172
0
      } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
6173
0
    xmlNsPtr ns;
6174
6175
0
    ns = elem->nsDef;
6176
0
    while (ns != NULL) {
6177
0
        if (xmlStrEqual(attr->name, ns->prefix)) {
6178
0
      if (!xmlStrEqual(attr->defaultValue, ns->href)) {
6179
0
          xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
6180
0
       "Element %s namespace name for %s does not match the DTD\n",
6181
0
           elem->name, ns->prefix, NULL);
6182
0
          ret = 0;
6183
0
      }
6184
0
      goto found;
6185
0
        }
6186
0
        ns = ns->next;
6187
0
    }
6188
0
      }
6189
0
  }
6190
0
found:
6191
0
        attr = attr->nexth;
6192
0
    }
6193
0
    return(ret);
6194
0
}
6195
6196
/**
6197
 * xmlValidateRoot:
6198
 * @ctxt:  the validation context
6199
 * @doc:  a document instance
6200
 *
6201
 * Try to validate a the root element
6202
 * basically it does the following check as described by the
6203
 * XML-1.0 recommendation:
6204
 *  - [ VC: Root Element Type ]
6205
 * it doesn't try to recurse or apply other check to the element
6206
 *
6207
 * returns 1 if valid or 0 otherwise
6208
 */
6209
6210
int
6211
0
xmlValidateRoot(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6212
0
    xmlNodePtr root;
6213
0
    int ret;
6214
6215
0
    if (doc == NULL) return(0);
6216
6217
0
    root = xmlDocGetRootElement(doc);
6218
0
    if ((root == NULL) || (root->name == NULL)) {
6219
0
  xmlErrValid(ctxt, XML_DTD_NO_ROOT,
6220
0
              "no root element\n", NULL);
6221
0
        return(0);
6222
0
    }
6223
6224
    /*
6225
     * When doing post validation against a separate DTD, those may
6226
     * no internal subset has been generated
6227
     */
6228
0
    if ((doc->intSubset != NULL) &&
6229
0
  (doc->intSubset->name != NULL)) {
6230
  /*
6231
   * Check first the document root against the NQName
6232
   */
6233
0
  if (!xmlStrEqual(doc->intSubset->name, root->name)) {
6234
0
      if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
6235
0
    xmlChar fn[50];
6236
0
    xmlChar *fullname;
6237
6238
0
    fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
6239
0
    if (fullname == NULL) {
6240
0
        xmlVErrMemory(ctxt, NULL);
6241
0
        return(0);
6242
0
    }
6243
0
    ret = xmlStrEqual(doc->intSubset->name, fullname);
6244
0
    if ((fullname != fn) && (fullname != root->name))
6245
0
        xmlFree(fullname);
6246
0
    if (ret == 1)
6247
0
        goto name_ok;
6248
0
      }
6249
0
      if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
6250
0
    (xmlStrEqual(root->name, BAD_CAST "html")))
6251
0
    goto name_ok;
6252
0
      xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
6253
0
       "root and DTD name do not match '%s' and '%s'\n",
6254
0
       root->name, doc->intSubset->name, NULL);
6255
0
      return(0);
6256
0
  }
6257
0
    }
6258
0
name_ok:
6259
0
    return(1);
6260
0
}
6261
6262
6263
/**
6264
 * xmlValidateElement:
6265
 * @ctxt:  the validation context
6266
 * @doc:  a document instance
6267
 * @root:  an element instance
6268
 *
6269
 * Try to validate the subtree under an element
6270
 *
6271
 * returns 1 if valid or 0 otherwise
6272
 */
6273
6274
int
6275
0
xmlValidateElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlNodePtr root) {
6276
0
    xmlNodePtr elem;
6277
0
    xmlAttrPtr attr;
6278
0
    xmlNsPtr ns;
6279
0
    const xmlChar *value;
6280
0
    int ret = 1;
6281
6282
0
    if (root == NULL) return(0);
6283
6284
0
    CHECK_DTD;
6285
6286
0
    elem = root;
6287
0
    while (1) {
6288
0
        ret &= xmlValidateOneElement(ctxt, doc, elem);
6289
6290
0
        if (elem->type == XML_ELEMENT_NODE) {
6291
0
            attr = elem->properties;
6292
0
            while (attr != NULL) {
6293
0
                value = xmlNodeListGetString(doc, attr->children, 0);
6294
0
                ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
6295
0
                if (value != NULL)
6296
0
                    xmlFree((char *)value);
6297
0
                attr= attr->next;
6298
0
            }
6299
6300
0
            ns = elem->nsDef;
6301
0
            while (ns != NULL) {
6302
0
                if (elem->ns == NULL)
6303
0
                    ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
6304
0
                                                   ns, ns->href);
6305
0
                else
6306
0
                    ret &= xmlValidateOneNamespace(ctxt, doc, elem,
6307
0
                                                   elem->ns->prefix, ns,
6308
0
                                                   ns->href);
6309
0
                ns = ns->next;
6310
0
            }
6311
6312
0
            if (elem->children != NULL) {
6313
0
                elem = elem->children;
6314
0
                continue;
6315
0
            }
6316
0
        }
6317
6318
0
        while (1) {
6319
0
            if (elem == root)
6320
0
                goto done;
6321
0
            if (elem->next != NULL)
6322
0
                break;
6323
0
            elem = elem->parent;
6324
0
        }
6325
0
        elem = elem->next;
6326
0
    }
6327
6328
0
done:
6329
0
    return(ret);
6330
0
}
6331
6332
/**
6333
 * xmlValidateRef:
6334
 * @ref:   A reference to be validated
6335
 * @ctxt:  Validation context
6336
 * @name:  Name of ID we are searching for
6337
 *
6338
 */
6339
static void
6340
xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
6341
0
                     const xmlChar *name) {
6342
0
    xmlAttrPtr id;
6343
0
    xmlAttrPtr attr;
6344
6345
0
    if (ref == NULL)
6346
0
  return;
6347
0
    if ((ref->attr == NULL) && (ref->name == NULL))
6348
0
  return;
6349
0
    attr = ref->attr;
6350
0
    if (attr == NULL) {
6351
0
  xmlChar *dup, *str = NULL, *cur, save;
6352
6353
0
  dup = xmlStrdup(name);
6354
0
  if (dup == NULL) {
6355
0
      ctxt->valid = 0;
6356
0
      return;
6357
0
  }
6358
0
  cur = dup;
6359
0
  while (*cur != 0) {
6360
0
      str = cur;
6361
0
      while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6362
0
      save = *cur;
6363
0
      *cur = 0;
6364
0
      id = xmlGetID(ctxt->doc, str);
6365
0
      if (id == NULL) {
6366
0
    xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
6367
0
     "attribute %s line %d references an unknown ID \"%s\"\n",
6368
0
           ref->name, ref->lineno, str);
6369
0
    ctxt->valid = 0;
6370
0
      }
6371
0
      if (save == 0)
6372
0
    break;
6373
0
      *cur = save;
6374
0
      while (IS_BLANK_CH(*cur)) cur++;
6375
0
  }
6376
0
  xmlFree(dup);
6377
0
    } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
6378
0
  id = xmlGetID(ctxt->doc, name);
6379
0
  if (id == NULL) {
6380
0
      xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6381
0
     "IDREF attribute %s references an unknown ID \"%s\"\n",
6382
0
       attr->name, name, NULL);
6383
0
      ctxt->valid = 0;
6384
0
  }
6385
0
    } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
6386
0
  xmlChar *dup, *str = NULL, *cur, save;
6387
6388
0
  dup = xmlStrdup(name);
6389
0
  if (dup == NULL) {
6390
0
      xmlVErrMemory(ctxt, "IDREFS split");
6391
0
      ctxt->valid = 0;
6392
0
      return;
6393
0
  }
6394
0
  cur = dup;
6395
0
  while (*cur != 0) {
6396
0
      str = cur;
6397
0
      while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
6398
0
      save = *cur;
6399
0
      *cur = 0;
6400
0
      id = xmlGetID(ctxt->doc, str);
6401
0
      if (id == NULL) {
6402
0
    xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
6403
0
     "IDREFS attribute %s references an unknown ID \"%s\"\n",
6404
0
           attr->name, str, NULL);
6405
0
    ctxt->valid = 0;
6406
0
      }
6407
0
      if (save == 0)
6408
0
    break;
6409
0
      *cur = save;
6410
0
      while (IS_BLANK_CH(*cur)) cur++;
6411
0
  }
6412
0
  xmlFree(dup);
6413
0
    }
6414
0
}
6415
6416
/**
6417
 * xmlWalkValidateList:
6418
 * @data:  Contents of current link
6419
 * @user:  Value supplied by the user
6420
 *
6421
 * Returns 0 to abort the walk or 1 to continue
6422
 */
6423
static int
6424
xmlWalkValidateList(const void *data, void *user)
6425
0
{
6426
0
  xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
6427
0
  xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
6428
0
  return 1;
6429
0
}
6430
6431
/**
6432
 * xmlValidateCheckRefCallback:
6433
 * @ref_list:  List of references
6434
 * @ctxt:  Validation context
6435
 * @name:  Name of ID we are searching for
6436
 *
6437
 */
6438
static void
6439
0
xmlValidateCheckRefCallback(void *payload, void *data, const xmlChar *name) {
6440
0
    xmlListPtr ref_list = (xmlListPtr) payload;
6441
0
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
6442
0
    xmlValidateMemo memo;
6443
6444
0
    if (ref_list == NULL)
6445
0
  return;
6446
0
    memo.ctxt = ctxt;
6447
0
    memo.name = name;
6448
6449
0
    xmlListWalk(ref_list, xmlWalkValidateList, &memo);
6450
6451
0
}
6452
6453
/**
6454
 * xmlValidateDocumentFinal:
6455
 * @ctxt:  the validation context
6456
 * @doc:  a document instance
6457
 *
6458
 * Does the final step for the document validation once all the
6459
 * incremental validation steps have been completed
6460
 *
6461
 * basically it does the following checks described by the XML Rec
6462
 *
6463
 * Check all the IDREF/IDREFS attributes definition for validity
6464
 *
6465
 * returns 1 if valid or 0 otherwise
6466
 */
6467
6468
int
6469
0
xmlValidateDocumentFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6470
0
    xmlRefTablePtr table;
6471
0
    unsigned int save;
6472
6473
0
    if (ctxt == NULL)
6474
0
        return(0);
6475
0
    if (doc == NULL) {
6476
0
        xmlErrValid(ctxt, XML_DTD_NO_DOC,
6477
0
    "xmlValidateDocumentFinal: doc == NULL\n", NULL);
6478
0
  return(0);
6479
0
    }
6480
6481
    /* trick to get correct line id report */
6482
0
    save = ctxt->flags;
6483
0
    ctxt->flags &= ~XML_VCTXT_USE_PCTXT;
6484
6485
    /*
6486
     * Check all the NOTATION/NOTATIONS attributes
6487
     */
6488
    /*
6489
     * Check all the ENTITY/ENTITIES attributes definition for validity
6490
     */
6491
    /*
6492
     * Check all the IDREF/IDREFS attributes definition for validity
6493
     */
6494
0
    table = (xmlRefTablePtr) doc->refs;
6495
0
    ctxt->doc = doc;
6496
0
    ctxt->valid = 1;
6497
0
    xmlHashScan(table, xmlValidateCheckRefCallback, ctxt);
6498
6499
0
    ctxt->flags = save;
6500
0
    return(ctxt->valid);
6501
0
}
6502
6503
/**
6504
 * xmlValidateDtd:
6505
 * @ctxt:  the validation context
6506
 * @doc:  a document instance
6507
 * @dtd:  a dtd instance
6508
 *
6509
 * Try to validate the document against the dtd instance
6510
 *
6511
 * Basically it does check all the definitions in the DtD.
6512
 * Note the the internal subset (if present) is de-coupled
6513
 * (i.e. not used), which could give problems if ID or IDREF
6514
 * is present.
6515
 *
6516
 * returns 1 if valid or 0 otherwise
6517
 */
6518
6519
int
6520
0
xmlValidateDtd(xmlValidCtxtPtr ctxt, xmlDocPtr doc, xmlDtdPtr dtd) {
6521
0
    int ret;
6522
0
    xmlDtdPtr oldExt, oldInt;
6523
0
    xmlNodePtr root;
6524
6525
0
    if (dtd == NULL) return(0);
6526
0
    if (doc == NULL) return(0);
6527
0
    oldExt = doc->extSubset;
6528
0
    oldInt = doc->intSubset;
6529
0
    doc->extSubset = dtd;
6530
0
    doc->intSubset = NULL;
6531
0
    ret = xmlValidateRoot(ctxt, doc);
6532
0
    if (ret == 0) {
6533
0
  doc->extSubset = oldExt;
6534
0
  doc->intSubset = oldInt;
6535
0
  return(ret);
6536
0
    }
6537
0
    if (doc->ids != NULL) {
6538
0
          xmlFreeIDTable(doc->ids);
6539
0
          doc->ids = NULL;
6540
0
    }
6541
0
    if (doc->refs != NULL) {
6542
0
          xmlFreeRefTable(doc->refs);
6543
0
          doc->refs = NULL;
6544
0
    }
6545
0
    root = xmlDocGetRootElement(doc);
6546
0
    ret = xmlValidateElement(ctxt, doc, root);
6547
0
    ret &= xmlValidateDocumentFinal(ctxt, doc);
6548
0
    doc->extSubset = oldExt;
6549
0
    doc->intSubset = oldInt;
6550
0
    return(ret);
6551
0
}
6552
6553
static void
6554
xmlValidateNotationCallback(void *payload, void *data,
6555
0
                      const xmlChar *name ATTRIBUTE_UNUSED) {
6556
0
    xmlEntityPtr cur = (xmlEntityPtr) payload;
6557
0
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
6558
0
    if (cur == NULL)
6559
0
  return;
6560
0
    if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6561
0
  xmlChar *notation = cur->content;
6562
6563
0
  if (notation != NULL) {
6564
0
      int ret;
6565
6566
0
      ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6567
0
      if (ret != 1) {
6568
0
    ctxt->valid = 0;
6569
0
      }
6570
0
  }
6571
0
    }
6572
0
}
6573
6574
static void
6575
xmlValidateAttributeCallback(void *payload, void *data,
6576
0
                       const xmlChar *name ATTRIBUTE_UNUSED) {
6577
0
    xmlAttributePtr cur = (xmlAttributePtr) payload;
6578
0
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
6579
0
    int ret;
6580
0
    xmlDocPtr doc;
6581
0
    xmlElementPtr elem = NULL;
6582
6583
0
    if (cur == NULL)
6584
0
  return;
6585
0
    switch (cur->atype) {
6586
0
  case XML_ATTRIBUTE_CDATA:
6587
0
  case XML_ATTRIBUTE_ID:
6588
0
  case XML_ATTRIBUTE_IDREF  :
6589
0
  case XML_ATTRIBUTE_IDREFS:
6590
0
  case XML_ATTRIBUTE_NMTOKEN:
6591
0
  case XML_ATTRIBUTE_NMTOKENS:
6592
0
  case XML_ATTRIBUTE_ENUMERATION:
6593
0
      break;
6594
0
  case XML_ATTRIBUTE_ENTITY:
6595
0
  case XML_ATTRIBUTE_ENTITIES:
6596
0
  case XML_ATTRIBUTE_NOTATION:
6597
0
      if (cur->defaultValue != NULL) {
6598
6599
0
    ret = xmlValidateAttributeValue2(ctxt, ctxt->doc, cur->name,
6600
0
                               cur->atype, cur->defaultValue);
6601
0
    if ((ret == 0) && (ctxt->valid == 1))
6602
0
        ctxt->valid = 0;
6603
0
      }
6604
0
      if (cur->tree != NULL) {
6605
0
    xmlEnumerationPtr tree = cur->tree;
6606
0
    while (tree != NULL) {
6607
0
        ret = xmlValidateAttributeValue2(ctxt, ctxt->doc,
6608
0
            cur->name, cur->atype, tree->name);
6609
0
        if ((ret == 0) && (ctxt->valid == 1))
6610
0
      ctxt->valid = 0;
6611
0
        tree = tree->next;
6612
0
    }
6613
0
      }
6614
0
    }
6615
0
    if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6616
0
  doc = cur->doc;
6617
0
  if (cur->elem == NULL) {
6618
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
6619
0
       "xmlValidateAttributeCallback(%s): internal error\n",
6620
0
       (const char *) cur->name);
6621
0
      return;
6622
0
  }
6623
6624
0
  if (doc != NULL)
6625
0
      elem = xmlGetDtdElementDesc(doc->intSubset, cur->elem);
6626
0
  if ((elem == NULL) && (doc != NULL))
6627
0
      elem = xmlGetDtdElementDesc(doc->extSubset, cur->elem);
6628
0
  if ((elem == NULL) && (cur->parent != NULL) &&
6629
0
      (cur->parent->type == XML_DTD_NODE))
6630
0
      elem = xmlGetDtdElementDesc((xmlDtdPtr) cur->parent, cur->elem);
6631
0
  if (elem == NULL) {
6632
0
      xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
6633
0
       "attribute %s: could not find decl for element %s\n",
6634
0
       cur->name, cur->elem, NULL);
6635
0
      return;
6636
0
  }
6637
0
  if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6638
0
      xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
6639
0
       "NOTATION attribute %s declared for EMPTY element %s\n",
6640
0
       cur->name, cur->elem, NULL);
6641
0
      ctxt->valid = 0;
6642
0
  }
6643
0
    }
6644
0
}
6645
6646
/**
6647
 * xmlValidateDtdFinal:
6648
 * @ctxt:  the validation context
6649
 * @doc:  a document instance
6650
 *
6651
 * Does the final step for the dtds validation once all the
6652
 * subsets have been parsed
6653
 *
6654
 * basically it does the following checks described by the XML Rec
6655
 * - check that ENTITY and ENTITIES type attributes default or
6656
 *   possible values matches one of the defined entities.
6657
 * - check that NOTATION type attributes default or
6658
 *   possible values matches one of the defined notations.
6659
 *
6660
 * returns 1 if valid or 0 if invalid and -1 if not well-formed
6661
 */
6662
6663
int
6664
0
xmlValidateDtdFinal(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6665
0
    xmlDtdPtr dtd;
6666
0
    xmlAttributeTablePtr table;
6667
0
    xmlEntitiesTablePtr entities;
6668
6669
0
    if ((doc == NULL) || (ctxt == NULL)) return(0);
6670
0
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6671
0
  return(0);
6672
0
    ctxt->doc = doc;
6673
0
    ctxt->valid = 1;
6674
0
    dtd = doc->intSubset;
6675
0
    if ((dtd != NULL) && (dtd->attributes != NULL)) {
6676
0
  table = (xmlAttributeTablePtr) dtd->attributes;
6677
0
  xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
6678
0
    }
6679
0
    if ((dtd != NULL) && (dtd->entities != NULL)) {
6680
0
  entities = (xmlEntitiesTablePtr) dtd->entities;
6681
0
  xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
6682
0
    }
6683
0
    dtd = doc->extSubset;
6684
0
    if ((dtd != NULL) && (dtd->attributes != NULL)) {
6685
0
  table = (xmlAttributeTablePtr) dtd->attributes;
6686
0
  xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
6687
0
    }
6688
0
    if ((dtd != NULL) && (dtd->entities != NULL)) {
6689
0
  entities = (xmlEntitiesTablePtr) dtd->entities;
6690
0
  xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
6691
0
    }
6692
0
    return(ctxt->valid);
6693
0
}
6694
6695
/**
6696
 * xmlValidateDocument:
6697
 * @ctxt:  the validation context
6698
 * @doc:  a document instance
6699
 *
6700
 * Try to validate the document instance
6701
 *
6702
 * basically it does the all the checks described by the XML Rec
6703
 * i.e. validates the internal and external subset (if present)
6704
 * and validate the document tree.
6705
 *
6706
 * returns 1 if valid or 0 otherwise
6707
 */
6708
6709
int
6710
0
xmlValidateDocument(xmlValidCtxtPtr ctxt, xmlDocPtr doc) {
6711
0
    int ret;
6712
0
    xmlNodePtr root;
6713
6714
0
    if (doc == NULL)
6715
0
        return(0);
6716
0
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6717
0
        xmlErrValid(ctxt, XML_DTD_NO_DTD,
6718
0
              "no DTD found!\n", NULL);
6719
0
  return(0);
6720
0
    }
6721
0
    if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6722
0
  (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6723
0
  xmlChar *sysID;
6724
0
  if (doc->intSubset->SystemID != NULL) {
6725
0
      sysID = xmlBuildURI(doc->intSubset->SystemID,
6726
0
      doc->URL);
6727
0
      if (sysID == NULL) {
6728
0
          xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6729
0
      "Could not build URI for external subset \"%s\"\n",
6730
0
      (const char *) doc->intSubset->SystemID);
6731
0
    return 0;
6732
0
      }
6733
0
  } else
6734
0
      sysID = NULL;
6735
0
        doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID,
6736
0
      (const xmlChar *)sysID);
6737
0
  if (sysID != NULL)
6738
0
      xmlFree(sysID);
6739
0
        if (doc->extSubset == NULL) {
6740
0
      if (doc->intSubset->SystemID != NULL) {
6741
0
    xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6742
0
           "Could not load the external subset \"%s\"\n",
6743
0
           (const char *) doc->intSubset->SystemID);
6744
0
      } else {
6745
0
    xmlErrValid(ctxt, XML_DTD_LOAD_ERROR,
6746
0
           "Could not load the external subset \"%s\"\n",
6747
0
           (const char *) doc->intSubset->ExternalID);
6748
0
      }
6749
0
      return(0);
6750
0
  }
6751
0
    }
6752
6753
0
    if (doc->ids != NULL) {
6754
0
          xmlFreeIDTable(doc->ids);
6755
0
          doc->ids = NULL;
6756
0
    }
6757
0
    if (doc->refs != NULL) {
6758
0
          xmlFreeRefTable(doc->refs);
6759
0
          doc->refs = NULL;
6760
0
    }
6761
0
    ret = xmlValidateDtdFinal(ctxt, doc);
6762
0
    if (!xmlValidateRoot(ctxt, doc)) return(0);
6763
6764
0
    root = xmlDocGetRootElement(doc);
6765
0
    ret &= xmlValidateElement(ctxt, doc, root);
6766
0
    ret &= xmlValidateDocumentFinal(ctxt, doc);
6767
0
    return(ret);
6768
0
}
6769
6770
/************************************************************************
6771
 *                  *
6772
 *    Routines for dynamic validation editing     *
6773
 *                  *
6774
 ************************************************************************/
6775
6776
/**
6777
 * xmlValidGetPotentialChildren:
6778
 * @ctree:  an element content tree
6779
 * @names:  an array to store the list of child names
6780
 * @len:  a pointer to the number of element in the list
6781
 * @max:  the size of the array
6782
 *
6783
 * Build/extend a list of  potential children allowed by the content tree
6784
 *
6785
 * returns the number of element in the list, or -1 in case of error.
6786
 */
6787
6788
int
6789
xmlValidGetPotentialChildren(xmlElementContent *ctree,
6790
                             const xmlChar **names,
6791
0
                             int *len, int max) {
6792
0
    int i;
6793
6794
0
    if ((ctree == NULL) || (names == NULL) || (len == NULL))
6795
0
        return(-1);
6796
0
    if (*len >= max) return(*len);
6797
6798
0
    switch (ctree->type) {
6799
0
  case XML_ELEMENT_CONTENT_PCDATA:
6800
0
      for (i = 0; i < *len;i++)
6801
0
    if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
6802
0
      names[(*len)++] = BAD_CAST "#PCDATA";
6803
0
      break;
6804
0
  case XML_ELEMENT_CONTENT_ELEMENT:
6805
0
      for (i = 0; i < *len;i++)
6806
0
    if (xmlStrEqual(ctree->name, names[i])) return(*len);
6807
0
      names[(*len)++] = ctree->name;
6808
0
      break;
6809
0
  case XML_ELEMENT_CONTENT_SEQ:
6810
0
      xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6811
0
      xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6812
0
      break;
6813
0
  case XML_ELEMENT_CONTENT_OR:
6814
0
      xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6815
0
      xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6816
0
      break;
6817
0
   }
6818
6819
0
   return(*len);
6820
0
}
6821
6822
/*
6823
 * Dummy function to suppress messages while we try out valid elements
6824
 */
6825
static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6826
0
                                const char *msg ATTRIBUTE_UNUSED, ...) {
6827
0
    return;
6828
0
}
6829
6830
/**
6831
 * xmlValidGetValidElements:
6832
 * @prev:  an element to insert after
6833
 * @next:  an element to insert next
6834
 * @names:  an array to store the list of child names
6835
 * @max:  the size of the array
6836
 *
6837
 * This function returns the list of authorized children to insert
6838
 * within an existing tree while respecting the validity constraints
6839
 * forced by the Dtd. The insertion point is defined using @prev and
6840
 * @next in the following ways:
6841
 *  to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6842
 *  to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6843
 *  to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6844
 *  to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6845
 *  to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6846
 *
6847
 * pointers to the element names are inserted at the beginning of the array
6848
 * and do not need to be freed.
6849
 *
6850
 * returns the number of element in the list, or -1 in case of error. If
6851
 *    the function returns the value @max the caller is invited to grow the
6852
 *    receiving array and retry.
6853
 */
6854
6855
int
6856
xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
6857
0
                         int max) {
6858
0
    xmlValidCtxt vctxt;
6859
0
    int nb_valid_elements = 0;
6860
0
    const xmlChar *elements[256]={0};
6861
0
    int nb_elements = 0, i;
6862
0
    const xmlChar *name;
6863
6864
0
    xmlNode *ref_node;
6865
0
    xmlNode *parent;
6866
0
    xmlNode *test_node;
6867
6868
0
    xmlNode *prev_next;
6869
0
    xmlNode *next_prev;
6870
0
    xmlNode *parent_childs;
6871
0
    xmlNode *parent_last;
6872
6873
0
    xmlElement *element_desc;
6874
6875
0
    if (prev == NULL && next == NULL)
6876
0
        return(-1);
6877
6878
0
    if (names == NULL) return(-1);
6879
0
    if (max <= 0) return(-1);
6880
6881
0
    memset(&vctxt, 0, sizeof (xmlValidCtxt));
6882
0
    vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6883
6884
0
    nb_valid_elements = 0;
6885
0
    ref_node = prev ? prev : next;
6886
0
    parent = ref_node->parent;
6887
6888
    /*
6889
     * Retrieves the parent element declaration
6890
     */
6891
0
    element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6892
0
                                         parent->name);
6893
0
    if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6894
0
        element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6895
0
                                             parent->name);
6896
0
    if (element_desc == NULL) return(-1);
6897
6898
    /*
6899
     * Do a backup of the current tree structure
6900
     */
6901
0
    prev_next = prev ? prev->next : NULL;
6902
0
    next_prev = next ? next->prev : NULL;
6903
0
    parent_childs = parent->children;
6904
0
    parent_last = parent->last;
6905
6906
    /*
6907
     * Creates a dummy node and insert it into the tree
6908
     */
6909
0
    test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
6910
0
    if (test_node == NULL)
6911
0
        return(-1);
6912
6913
0
    test_node->parent = parent;
6914
0
    test_node->prev = prev;
6915
0
    test_node->next = next;
6916
0
    name = test_node->name;
6917
6918
0
    if (prev) prev->next = test_node;
6919
0
    else parent->children = test_node;
6920
6921
0
    if (next) next->prev = test_node;
6922
0
    else parent->last = test_node;
6923
6924
    /*
6925
     * Insert each potential child node and check if the parent is
6926
     * still valid
6927
     */
6928
0
    nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6929
0
           elements, &nb_elements, 256);
6930
6931
0
    for (i = 0;i < nb_elements;i++) {
6932
0
  test_node->name = elements[i];
6933
0
  if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
6934
0
      int j;
6935
6936
0
      for (j = 0; j < nb_valid_elements;j++)
6937
0
    if (xmlStrEqual(elements[i], names[j])) break;
6938
0
      names[nb_valid_elements++] = elements[i];
6939
0
      if (nb_valid_elements >= max) break;
6940
0
  }
6941
0
    }
6942
6943
    /*
6944
     * Restore the tree structure
6945
     */
6946
0
    if (prev) prev->next = prev_next;
6947
0
    if (next) next->prev = next_prev;
6948
0
    parent->children = parent_childs;
6949
0
    parent->last = parent_last;
6950
6951
    /*
6952
     * Free up the dummy node
6953
     */
6954
0
    test_node->name = name;
6955
0
    xmlFreeNode(test_node);
6956
6957
0
    return(nb_valid_elements);
6958
0
}
6959
#endif /* LIBXML_VALID_ENABLED */
6960