Coverage Report

Created: 2024-02-11 06:19

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