Coverage Report

Created: 2025-11-16 09:57

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