Coverage Report

Created: 2024-09-06 07:53

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