Coverage Report

Created: 2026-06-13 06:16

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