Coverage Report

Created: 2026-03-06 07:01

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
5.04k
{
55
5.04k
    if (ctxt != NULL) {
56
4.92k
        if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
57
3.97k
            xmlCtxtErrMemory(ctxt->userData);
58
3.97k
        } else {
59
945
            xmlRaiseMemoryError(NULL, ctxt->error, ctxt->userData,
60
945
                                XML_FROM_VALID, NULL);
61
945
        }
62
4.92k
    } else {
63
124
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_VALID, NULL);
64
124
    }
65
5.04k
}
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
1.36M
              const char *msg, ...) {
76
1.36M
    xmlParserCtxtPtr pctxt = NULL;
77
1.36M
    va_list ap;
78
1.36M
    int ret = 0;
79
80
1.36M
    if (ctxt == NULL)
81
332
        return -1;
82
1.36M
    if (ctxt->flags & XML_VCTXT_USE_PCTXT)
83
1.02M
        pctxt = ctxt->userData;
84
85
1.36M
    va_start(ap, msg);
86
1.36M
    if (pctxt != NULL) {
87
1.02M
        xmlCtxtVErr(pctxt, node, XML_FROM_VALID, code, level,
88
1.02M
                    str1, str2, str3, int1, msg, ap);
89
1.02M
        if (pctxt->errNo == XML_ERR_NO_MEMORY)
90
57.7k
            ret = -1;
91
1.02M
    } else {
92
338k
        xmlGenericErrorFunc channel = NULL;
93
338k
        void *data = NULL;
94
338k
        int res;
95
96
338k
        if (ctxt != NULL) {
97
338k
            channel = ctxt->error;
98
338k
            data = ctxt->userData;
99
338k
        }
100
338k
        res = xmlVRaiseError(NULL, channel, data, NULL, node,
101
338k
                             XML_FROM_VALID, code, level, NULL, 0,
102
338k
                             (const char *) str1, (const char *) str2,
103
338k
                             (const char *) str2, int1, 0,
104
338k
                             msg, ap);
105
338k
        if (res < 0) {
106
164
            xmlVErrMemory(ctxt);
107
164
            ret = -1;
108
164
        }
109
338k
    }
110
1.36M
    va_end(ap);
111
112
1.36M
    return ret;
113
1.36M
}
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
1.29M
{
132
1.29M
    xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str2, str3, 0,
133
1.29M
                  msg, str1, str2, str3);
134
1.29M
}
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
29.1k
{
149
29.1k
    xmlDoErrValid(ctxt, NULL, error, XML_ERR_ERROR, (const xmlChar *) extra,
150
29.1k
                  NULL, NULL, 0, msg, extra);
151
29.1k
}
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
2.69k
{
170
2.69k
    xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str3, NULL, int2,
171
2.69k
                  msg, str1, int2, str3);
172
2.69k
}
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
33.4k
{
192
33.4k
    return xmlDoErrValid(ctxt, node, error, XML_ERR_WARNING, str1, str2, str3,
193
33.4k
                         0, msg, str1, str2, str3);
194
33.4k
}
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
96.4k
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
217
96.4k
    if (ctxt->vstateNr >= ctxt->vstateMax) {
218
7.40k
        xmlValidState *tmp;
219
7.40k
        int newSize;
220
221
7.40k
        newSize = xmlGrowCapacity(ctxt->vstateMax, sizeof(tmp[0]),
222
7.40k
                                  10, XML_MAX_ITEMS);
223
7.40k
        if (newSize < 0) {
224
0
      xmlVErrMemory(ctxt);
225
0
      return(-1);
226
0
  }
227
7.40k
  tmp = xmlRealloc(ctxt->vstateTab, newSize * sizeof(tmp[0]));
228
7.40k
        if (tmp == NULL) {
229
84
      xmlVErrMemory(ctxt);
230
84
      return(-1);
231
84
  }
232
7.31k
  ctxt->vstateTab = tmp;
233
7.31k
  ctxt->vstateMax = newSize;
234
7.31k
    }
235
96.3k
    ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
236
96.3k
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
237
96.3k
    ctxt->vstateTab[ctxt->vstateNr].node = node;
238
96.3k
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
239
7.20k
  if (elemDecl->contModel == NULL)
240
1.36k
      xmlValidBuildContentModel(ctxt, elemDecl);
241
7.20k
  if (elemDecl->contModel != NULL) {
242
6.63k
      ctxt->vstateTab[ctxt->vstateNr].exec =
243
6.63k
    xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
244
6.63k
            if (ctxt->vstateTab[ctxt->vstateNr].exec == NULL) {
245
19
                xmlVErrMemory(ctxt);
246
19
                return(-1);
247
19
            }
248
6.63k
  } else {
249
568
      ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
250
568
      xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
251
568
                      XML_ERR_INTERNAL_ERROR,
252
568
          "Failed to build content model regexp for %s\n",
253
568
          node->name, NULL, NULL);
254
568
  }
255
7.20k
    }
256
96.3k
    return(ctxt->vstateNr++);
257
96.3k
}
258
259
static int
260
96.3k
vstateVPop(xmlValidCtxtPtr ctxt) {
261
96.3k
    xmlElementPtr elemDecl;
262
263
96.3k
    if (ctxt->vstateNr < 1) return(-1);
264
96.3k
    ctxt->vstateNr--;
265
96.3k
    elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
266
96.3k
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
267
96.3k
    ctxt->vstateTab[ctxt->vstateNr].node = NULL;
268
96.3k
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
269
7.18k
  xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
270
7.18k
    }
271
96.3k
    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
272
96.3k
    if (ctxt->vstateNr >= 1)
273
93.5k
  ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
274
2.85k
    else
275
2.85k
  ctxt->vstate = NULL;
276
96.3k
    return(ctxt->vstateNr);
277
96.3k
}
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
49.6k
{
375
49.6k
    if (ctxt->nodeNr >= ctxt->nodeMax) {
376
3.17k
        xmlNodePtr *tmp;
377
3.17k
        int newSize;
378
379
3.17k
        newSize = xmlGrowCapacity(ctxt->nodeMax, sizeof(tmp[0]),
380
3.17k
                                  4, XML_MAX_ITEMS);
381
3.17k
        if (newSize < 0) {
382
0
      xmlVErrMemory(ctxt);
383
0
            return (-1);
384
0
        }
385
3.17k
        tmp = xmlRealloc(ctxt->nodeTab, newSize * sizeof(tmp[0]));
386
3.17k
        if (tmp == NULL) {
387
18
      xmlVErrMemory(ctxt);
388
18
            return (-1);
389
18
        }
390
3.15k
  ctxt->nodeTab = tmp;
391
3.15k
        ctxt->nodeMax = newSize;
392
3.15k
    }
393
49.5k
    ctxt->nodeTab[ctxt->nodeNr] = value;
394
49.5k
    ctxt->node = value;
395
49.5k
    return (ctxt->nodeNr++);
396
49.6k
}
397
static xmlNodePtr
398
nodeVPop(xmlValidCtxtPtr ctxt)
399
84.2k
{
400
84.2k
    xmlNodePtr ret;
401
402
84.2k
    if (ctxt->nodeNr <= 0)
403
35.0k
        return (NULL);
404
49.1k
    ctxt->nodeNr--;
405
49.1k
    if (ctxt->nodeNr > 0)
406
46.7k
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
407
2.41k
    else
408
2.41k
        ctxt->node = NULL;
409
49.1k
    ret = ctxt->nodeTab[ctxt->nodeNr];
410
49.1k
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
411
49.1k
    return (ret);
412
84.2k
}
413
414
/* TODO: use hash table for accesses to elem and attribute definitions */
415
416
417
#define CHECK_DTD           \
418
1.13M
   if (doc == NULL) return(0);         \
419
1.13M
   else if ((doc->intSubset == NULL) &&       \
420
1.13M
      (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
1.13M
               const xmlChar *name) {
442
1.13M
    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
1.13M
    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
1.07M
  case XML_ELEMENT_CONTENT_ELEMENT: {
456
1.07M
      xmlAutomataStatePtr oldstate = ctxt->state;
457
1.07M
      xmlChar fn[50];
458
1.07M
      xmlChar *fullname;
459
460
1.07M
      fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
461
1.07M
      if (fullname == NULL) {
462
54
          xmlVErrMemory(ctxt);
463
54
    return(0);
464
54
      }
465
466
1.07M
      switch (content->ocur) {
467
947k
    case XML_ELEMENT_CONTENT_ONCE:
468
947k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
469
947k
          ctxt->state, NULL, fullname, NULL);
470
947k
        break;
471
91.5k
    case XML_ELEMENT_CONTENT_OPT:
472
91.5k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
473
91.5k
          ctxt->state, NULL, fullname, NULL);
474
91.5k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
475
91.5k
        break;
476
14.7k
    case XML_ELEMENT_CONTENT_PLUS:
477
14.7k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
478
14.7k
          ctxt->state, NULL, fullname, NULL);
479
14.7k
        xmlAutomataNewTransition(ctxt->am, ctxt->state,
480
14.7k
                           ctxt->state, fullname, NULL);
481
14.7k
        break;
482
17.1k
    case XML_ELEMENT_CONTENT_MULT:
483
17.1k
        ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
484
17.1k
              ctxt->state, NULL);
485
17.1k
        xmlAutomataNewTransition(ctxt->am,
486
17.1k
          ctxt->state, ctxt->state, fullname, NULL);
487
17.1k
        break;
488
1.07M
      }
489
1.07M
      if ((fullname != fn) && (fullname != content->name))
490
84.1k
    xmlFree(fullname);
491
1.07M
      break;
492
1.07M
  }
493
17.9k
  case XML_ELEMENT_CONTENT_SEQ: {
494
17.9k
      xmlAutomataStatePtr oldstate, oldend;
495
17.9k
      xmlElementContentOccur ocur;
496
497
      /*
498
       * Simply iterate over the content
499
       */
500
17.9k
      oldstate = ctxt->state;
501
17.9k
      ocur = content->ocur;
502
17.9k
      if (ocur != XML_ELEMENT_CONTENT_ONCE) {
503
14.7k
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
504
14.7k
    oldstate = ctxt->state;
505
14.7k
      }
506
826k
      do {
507
826k
    if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0)
508
38
                    return(0);
509
826k
    content = content->c2;
510
826k
      } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
511
808k
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
512
17.8k
      if (xmlValidBuildAContentModel(content, ctxt, name) == 0)
513
9
                return(0);
514
17.8k
      oldend = ctxt->state;
515
17.8k
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
516
17.8k
      switch (ocur) {
517
3.14k
    case XML_ELEMENT_CONTENT_ONCE:
518
3.14k
        break;
519
11.7k
    case XML_ELEMENT_CONTENT_OPT:
520
11.7k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
521
11.7k
        break;
522
1.95k
    case XML_ELEMENT_CONTENT_MULT:
523
1.95k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
524
1.95k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
525
1.95k
        break;
526
1.05k
    case XML_ELEMENT_CONTENT_PLUS:
527
1.05k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
528
1.05k
        break;
529
17.8k
      }
530
17.8k
      break;
531
17.8k
  }
532
46.1k
  case XML_ELEMENT_CONTENT_OR: {
533
46.1k
      xmlAutomataStatePtr oldstate, oldend;
534
46.1k
      xmlElementContentOccur ocur;
535
536
46.1k
      ocur = content->ocur;
537
46.1k
      if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
538
37.5k
    (ocur == XML_ELEMENT_CONTENT_MULT)) {
539
34.1k
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
540
34.1k
      ctxt->state, NULL);
541
34.1k
      }
542
46.1k
      oldstate = ctxt->state;
543
46.1k
      oldend = xmlAutomataNewState(ctxt->am);
544
545
      /*
546
       * iterate over the subtypes and remerge the end with an
547
       * epsilon transition
548
       */
549
235k
      do {
550
235k
    ctxt->state = oldstate;
551
235k
    if (xmlValidBuildAContentModel(content->c1, ctxt, name) == 0)
552
21
                    return(0);
553
235k
    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
554
235k
    content = content->c2;
555
235k
      } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
556
189k
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
557
46.1k
      ctxt->state = oldstate;
558
46.1k
      if (xmlValidBuildAContentModel(content, ctxt, name) == 0)
559
11
                return(0);
560
46.1k
      xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
561
46.1k
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
562
46.1k
      switch (ocur) {
563
5.26k
    case XML_ELEMENT_CONTENT_ONCE:
564
5.26k
        break;
565
6.70k
    case XML_ELEMENT_CONTENT_OPT:
566
6.70k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
567
6.70k
        break;
568
25.5k
    case XML_ELEMENT_CONTENT_MULT:
569
25.5k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
570
25.5k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
571
25.5k
        break;
572
8.57k
    case XML_ELEMENT_CONTENT_PLUS:
573
8.57k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
574
8.57k
        break;
575
46.1k
      }
576
46.1k
      break;
577
46.1k
  }
578
46.1k
  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
1.13M
    }
584
1.13M
    return(1);
585
1.13M
}
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
9.96k
xmlValidBuildContentModel(xmlValidCtxt *ctxt, xmlElement *elem) {
598
9.96k
    int ret = 0;
599
600
9.96k
    if ((ctxt == NULL) || (elem == NULL))
601
0
  return(0);
602
9.96k
    if (elem->type != XML_ELEMENT_DECL)
603
0
  return(0);
604
9.96k
    if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
605
0
  return(1);
606
    /* TODO: should we rebuild in this case ? */
607
9.96k
    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
9.96k
    ctxt->am = xmlNewAutomata();
616
9.96k
    if (ctxt->am == NULL) {
617
49
        xmlVErrMemory(ctxt);
618
49
  return(0);
619
49
    }
620
9.91k
    ctxt->state = xmlAutomataGetInitState(ctxt->am);
621
9.91k
    if (xmlValidBuildAContentModel(elem->content, ctxt, elem->name) == 0)
622
54
        goto done;
623
9.85k
    xmlAutomataSetFinalState(ctxt->am, ctxt->state);
624
9.85k
    elem->contModel = xmlAutomataCompile(ctxt->am);
625
9.85k
    if (elem->contModel == NULL) {
626
1.37k
        xmlVErrMemory(ctxt);
627
1.37k
        goto done;
628
1.37k
    }
629
8.48k
    if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
630
1.71k
  char expr[5000];
631
1.71k
  expr[0] = 0;
632
1.71k
  xmlSnprintfElementContent(expr, 5000, elem->content, 1);
633
1.71k
  xmlErrValidNode(ctxt, (xmlNodePtr) elem,
634
1.71k
                  XML_DTD_CONTENT_NOT_DETERMINIST,
635
1.71k
         "Content model of %s is not deterministic: %s\n",
636
1.71k
         elem->name, BAD_CAST expr, NULL);
637
1.71k
        ctxt->valid = 0;
638
1.71k
  goto done;
639
1.71k
    }
640
641
6.76k
    ret = 1;
642
643
9.91k
done:
644
9.91k
    ctxt->state = NULL;
645
9.91k
    xmlFreeAutomata(ctxt->am);
646
9.91k
    ctxt->am = NULL;
647
9.91k
    return(ret);
648
6.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
47.8k
xmlValidCtxt *xmlNewValidCtxt(void) {
665
47.8k
    xmlValidCtxtPtr ret;
666
667
47.8k
    ret = xmlMalloc(sizeof (xmlValidCtxt));
668
47.8k
    if (ret == NULL)
669
60
  return (NULL);
670
671
47.8k
    (void) memset(ret, 0, sizeof (xmlValidCtxt));
672
47.8k
    ret->flags |= XML_VCTXT_VALIDATE;
673
674
47.8k
    return (ret);
675
47.8k
}
676
677
/**
678
 * Free a validation context structure.
679
 *
680
 * @param cur  the validation context to free
681
 */
682
void
683
47.8k
xmlFreeValidCtxt(xmlValidCtxt *cur) {
684
47.8k
    if (cur == NULL)
685
60
        return;
686
47.8k
    if (cur->vstateTab != NULL)
687
0
        xmlFree(cur->vstateTab);
688
47.8k
    if (cur->nodeTab != NULL)
689
0
        xmlFree(cur->nodeTab);
690
47.8k
    xmlFree(cur);
691
47.8k
}
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
2.64M
                        xmlElementContentType type) {
709
2.64M
    xmlElementContentPtr ret;
710
2.64M
    xmlDictPtr dict = NULL;
711
712
2.64M
    if (doc != NULL)
713
2.63M
        dict = doc->dict;
714
715
2.64M
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
716
2.64M
    if (ret == NULL)
717
277
  return(NULL);
718
2.64M
    memset(ret, 0, sizeof(xmlElementContent));
719
2.64M
    ret->type = type;
720
2.64M
    ret->ocur = XML_ELEMENT_CONTENT_ONCE;
721
2.64M
    if (name != NULL) {
722
1.34M
        int l;
723
1.34M
  const xmlChar *tmp;
724
725
1.34M
  tmp = xmlSplitQName3(name, &l);
726
1.34M
  if (tmp == NULL) {
727
895k
      if (dict == NULL)
728
287k
    ret->name = xmlStrdup(name);
729
607k
      else
730
607k
          ret->name = xmlDictLookup(dict, name, -1);
731
895k
  } else {
732
447k
      if (dict == NULL) {
733
186k
    ret->prefix = xmlStrndup(name, l);
734
186k
    ret->name = xmlStrdup(tmp);
735
261k
      } else {
736
261k
          ret->prefix = xmlDictLookup(dict, name, l);
737
261k
    ret->name = xmlDictLookup(dict, tmp, -1);
738
261k
      }
739
447k
            if (ret->prefix == NULL)
740
17
                goto error;
741
447k
  }
742
1.34M
        if (ret->name == NULL)
743
74
            goto error;
744
1.34M
    }
745
2.64M
    return(ret);
746
747
91
error:
748
91
    xmlFreeDocElementContent(doc, ret);
749
91
    return(NULL);
750
2.64M
}
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
16.5k
xmlCopyDocElementContent(xmlDoc *doc, xmlElementContent *cur) {
779
16.5k
    xmlElementContentPtr ret = NULL, prev = NULL, tmp;
780
16.5k
    xmlDictPtr dict = NULL;
781
782
16.5k
    if (cur == NULL) return(NULL);
783
784
16.5k
    if (doc != NULL)
785
0
        dict = doc->dict;
786
787
16.5k
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
788
16.5k
    if (ret == NULL)
789
17
  return(NULL);
790
16.5k
    memset(ret, 0, sizeof(xmlElementContent));
791
16.5k
    ret->type = cur->type;
792
16.5k
    ret->ocur = cur->ocur;
793
16.5k
    if (cur->name != NULL) {
794
12.8k
  if (dict)
795
0
      ret->name = xmlDictLookup(dict, cur->name, -1);
796
12.8k
  else
797
12.8k
      ret->name = xmlStrdup(cur->name);
798
12.8k
        if (ret->name == NULL)
799
13
            goto error;
800
12.8k
    }
801
802
16.5k
    if (cur->prefix != NULL) {
803
1.01k
  if (dict)
804
0
      ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
805
1.01k
  else
806
1.01k
      ret->prefix = xmlStrdup(cur->prefix);
807
1.01k
        if (ret->prefix == NULL)
808
2
            goto error;
809
1.01k
    }
810
16.5k
    if (cur->c1 != NULL) {
811
2.46k
        ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
812
2.46k
        if (ret->c1 == NULL)
813
53
            goto error;
814
2.40k
  ret->c1->parent = ret;
815
2.40k
    }
816
16.5k
    if (cur->c2 != NULL) {
817
2.40k
        prev = ret;
818
2.40k
  cur = cur->c2;
819
15.6k
  while (cur != NULL) {
820
13.4k
      tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
821
13.4k
      if (tmp == NULL)
822
16
                goto error;
823
13.4k
      memset(tmp, 0, sizeof(xmlElementContent));
824
13.4k
      tmp->type = cur->type;
825
13.4k
      tmp->ocur = cur->ocur;
826
13.4k
      prev->c2 = tmp;
827
13.4k
      tmp->parent = prev;
828
13.4k
      if (cur->name != NULL) {
829
2.29k
    if (dict)
830
0
        tmp->name = xmlDictLookup(dict, cur->name, -1);
831
2.29k
    else
832
2.29k
        tmp->name = xmlStrdup(cur->name);
833
2.29k
                if (tmp->name == NULL)
834
13
                    goto error;
835
2.29k
      }
836
837
13.3k
      if (cur->prefix != NULL) {
838
370
    if (dict)
839
0
        tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
840
370
    else
841
370
        tmp->prefix = xmlStrdup(cur->prefix);
842
370
                if (tmp->prefix == NULL)
843
3
                    goto error;
844
370
      }
845
13.3k
      if (cur->c1 != NULL) {
846
11.1k
          tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
847
11.1k
          if (tmp->c1 == NULL)
848
101
                    goto error;
849
11.0k
    tmp->c1->parent = tmp;
850
11.0k
            }
851
13.2k
      prev = tmp;
852
13.2k
      cur = cur->c2;
853
13.2k
  }
854
2.40k
    }
855
16.3k
    return(ret);
856
857
201
error:
858
201
    xmlFreeElementContent(ret);
859
201
    return(NULL);
860
16.5k
}
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
3.01k
xmlCopyElementContent(xmlElementContent *cur) {
873
3.01k
    return(xmlCopyDocElementContent(NULL, cur));
874
3.01k
}
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
243k
xmlFreeDocElementContent(xmlDoc *doc, xmlElementContent *cur) {
886
243k
    xmlDictPtr dict = NULL;
887
243k
    size_t depth = 0;
888
889
243k
    if (cur == NULL)
890
84.0k
        return;
891
159k
    if (doc != NULL)
892
155k
        dict = doc->dict;
893
894
2.67M
    while (1) {
895
2.67M
        xmlElementContentPtr parent;
896
897
3.94M
        while ((cur->c1 != NULL) || (cur->c2 != NULL)) {
898
1.27M
            cur = (cur->c1 != NULL) ? cur->c1 : cur->c2;
899
1.27M
            depth += 1;
900
1.27M
        }
901
902
2.67M
  if (dict) {
903
1.70M
      if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
904
110
          xmlFree((xmlChar *) cur->name);
905
1.70M
      if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
906
66
          xmlFree((xmlChar *) cur->prefix);
907
1.70M
  } else {
908
966k
      if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
909
966k
      if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
910
966k
  }
911
2.67M
        parent = cur->parent;
912
2.67M
        if ((depth == 0) || (parent == NULL)) {
913
159k
            xmlFree(cur);
914
159k
            break;
915
159k
        }
916
2.51M
        if (cur == parent->c1)
917
1.27M
            parent->c1 = NULL;
918
1.23M
        else
919
1.23M
            parent->c2 = NULL;
920
2.51M
  xmlFree(cur);
921
922
2.51M
        if (parent->c2 != NULL) {
923
1.23M
      cur = parent->c2;
924
1.27M
        } else {
925
1.27M
            depth -= 1;
926
1.27M
            cur = parent;
927
1.27M
        }
928
2.51M
    }
929
159k
}
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
201
xmlFreeElementContent(xmlElementContent *cur) {
941
201
    xmlFreeDocElementContent(NULL, cur);
942
201
}
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
1.40M
xmlSnprintfElementContent(char *buf, int size, xmlElementContent *content, int englob) {
974
1.40M
    int len;
975
976
1.40M
    if (content == NULL) return;
977
1.40M
    len = strlen(buf);
978
1.40M
    if (size - len < 50) {
979
374
  if ((size - len > 4) && (buf[len - 1] != '.'))
980
374
      strcat(buf, " ...");
981
374
  return;
982
374
    }
983
1.40M
    if (englob) strcat(buf, "(");
984
1.40M
    switch (content->type) {
985
0
        case XML_ELEMENT_CONTENT_PCDATA:
986
0
            strcat(buf, "#PCDATA");
987
0
      break;
988
722k
  case XML_ELEMENT_CONTENT_ELEMENT: {
989
722k
            int qnameLen = xmlStrlen(content->name);
990
991
722k
      if (content->prefix != NULL)
992
283k
                qnameLen += xmlStrlen(content->prefix) + 1;
993
722k
      if (size - len < qnameLen + 10) {
994
596
    strcat(buf, " ...");
995
596
    return;
996
596
      }
997
721k
      if (content->prefix != NULL) {
998
283k
    strcat(buf, (char *) content->prefix);
999
283k
    strcat(buf, ":");
1000
283k
      }
1001
721k
      if (content->name != NULL)
1002
721k
    strcat(buf, (char *) content->name);
1003
721k
      break;
1004
722k
        }
1005
221k
  case XML_ELEMENT_CONTENT_SEQ:
1006
221k
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1007
215k
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1008
9.79k
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1009
211k
      else
1010
211k
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1011
221k
      len = strlen(buf);
1012
221k
      if (size - len < 50) {
1013
730
    if ((size - len > 4) && (buf[len - 1] != '.'))
1014
413
        strcat(buf, " ...");
1015
730
    return;
1016
730
      }
1017
220k
            strcat(buf, " , ");
1018
220k
      if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1019
219k
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1020
11.2k
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1021
1.55k
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1022
219k
      else
1023
219k
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1024
220k
      break;
1025
457k
  case XML_ELEMENT_CONTENT_OR:
1026
457k
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1027
456k
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1028
2.20k
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1029
455k
      else
1030
455k
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1031
457k
      len = strlen(buf);
1032
457k
      if (size - len < 50) {
1033
896
    if ((size - len > 4) && (buf[len - 1] != '.'))
1034
517
        strcat(buf, " ...");
1035
896
    return;
1036
896
      }
1037
456k
            strcat(buf, " | ");
1038
456k
      if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1039
456k
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1040
10.1k
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1041
361
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1042
456k
      else
1043
456k
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1044
456k
      break;
1045
1.40M
    }
1046
1.39M
    if (size - strlen(buf) <= 2) return;
1047
1.39M
    if (englob)
1048
58.5k
        strcat(buf, ")");
1049
1.39M
    switch (content->ocur) {
1050
1.13M
        case XML_ELEMENT_CONTENT_ONCE:
1051
1.13M
      break;
1052
190k
        case XML_ELEMENT_CONTENT_OPT:
1053
190k
      strcat(buf, "?");
1054
190k
      break;
1055
27.5k
        case XML_ELEMENT_CONTENT_MULT:
1056
27.5k
      strcat(buf, "*");
1057
27.5k
      break;
1058
45.3k
        case XML_ELEMENT_CONTENT_PLUS:
1059
45.3k
      strcat(buf, "+");
1060
45.3k
      break;
1061
1.39M
    }
1062
1.39M
}
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
128k
xmlFreeElement(xmlElementPtr elem) {
1077
128k
    if (elem == NULL) return;
1078
128k
    xmlUnlinkNode((xmlNodePtr) elem);
1079
128k
    xmlFreeDocElementContent(elem->doc, elem->content);
1080
128k
    if (elem->name != NULL)
1081
128k
  xmlFree((xmlChar *) elem->name);
1082
128k
    if (elem->prefix != NULL)
1083
15.3k
  xmlFree((xmlChar *) elem->prefix);
1084
128k
#ifdef LIBXML_REGEXP_ENABLED
1085
128k
    if (elem->contModel != NULL)
1086
8.48k
  xmlRegFreeRegexp(elem->contModel);
1087
128k
#endif
1088
128k
    xmlFree(elem);
1089
128k
}
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
108k
      xmlElementContent *content) {
1107
108k
    xmlElementPtr ret;
1108
108k
    xmlElementTablePtr table;
1109
108k
    xmlAttributePtr oldAttributes = NULL;
1110
108k
    const xmlChar *localName;
1111
108k
    xmlChar *prefix = NULL;
1112
1113
108k
    if ((dtd == NULL) || (name == NULL))
1114
1.44k
  return(NULL);
1115
1116
106k
    switch (type) {
1117
18.3k
        case XML_ELEMENT_TYPE_EMPTY:
1118
26.8k
        case XML_ELEMENT_TYPE_ANY:
1119
26.8k
            if (content != NULL)
1120
0
                return(NULL);
1121
26.8k
            break;
1122
26.8k
        case XML_ELEMENT_TYPE_MIXED:
1123
79.6k
        case XML_ELEMENT_TYPE_ELEMENT:
1124
79.6k
            if (content == NULL)
1125
207
                return(NULL);
1126
79.4k
            break;
1127
79.4k
        default:
1128
345
            return(NULL);
1129
106k
    }
1130
1131
    /*
1132
     * check if name is a QName
1133
     */
1134
106k
    localName = xmlSplitQName4(name, &prefix);
1135
106k
    if (localName == NULL)
1136
27
        goto mem_error;
1137
1138
    /*
1139
     * Create the Element table if needed.
1140
     */
1141
106k
    table = (xmlElementTablePtr) dtd->elements;
1142
106k
    if (table == NULL) {
1143
30.2k
  xmlDictPtr dict = NULL;
1144
1145
30.2k
  if (dtd->doc != NULL)
1146
29.9k
      dict = dtd->doc->dict;
1147
30.2k
        table = xmlHashCreateDict(0, dict);
1148
30.2k
        if (table == NULL)
1149
33
            goto mem_error;
1150
30.2k
  dtd->elements = (void *) table;
1151
30.2k
    }
1152
1153
    /*
1154
     * lookup old attributes inserted on an undefined element in the
1155
     * internal subset.
1156
     */
1157
106k
    if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1158
105k
  ret = xmlHashLookup2(dtd->doc->intSubset->elements, localName, prefix);
1159
105k
  if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1160
2.24k
      oldAttributes = ret->attributes;
1161
2.24k
      ret->attributes = NULL;
1162
2.24k
      xmlHashRemoveEntry2(dtd->doc->intSubset->elements, localName, prefix,
1163
2.24k
                                NULL);
1164
2.24k
      xmlFreeElement(ret);
1165
2.24k
  }
1166
105k
    }
1167
1168
    /*
1169
     * The element may already be present if one of its attribute
1170
     * was registered first
1171
     */
1172
106k
    ret = xmlHashLookup2(table, localName, prefix);
1173
106k
    if (ret != NULL) {
1174
43.7k
  if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1175
43.3k
#ifdef LIBXML_VALID_ENABLED
1176
      /*
1177
       * The element is already defined in this DTD.
1178
       */
1179
43.3k
            if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE))
1180
25.6k
                xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1181
25.6k
                                "Redefinition of element %s\n",
1182
25.6k
                                name, NULL, NULL);
1183
43.3k
#endif /* LIBXML_VALID_ENABLED */
1184
43.3k
            if (prefix != NULL)
1185
2.72k
          xmlFree(prefix);
1186
43.3k
      return(NULL);
1187
43.3k
  }
1188
363
  if (prefix != NULL) {
1189
222
      xmlFree(prefix);
1190
222
      prefix = NULL;
1191
222
  }
1192
62.5k
    } else {
1193
62.5k
        int res;
1194
1195
62.5k
  ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1196
62.5k
  if (ret == NULL)
1197
30
            goto mem_error;
1198
62.4k
  memset(ret, 0, sizeof(xmlElement));
1199
62.4k
  ret->type = XML_ELEMENT_DECL;
1200
1201
  /*
1202
   * fill the structure.
1203
   */
1204
62.4k
  ret->name = xmlStrdup(localName);
1205
62.4k
  if (ret->name == NULL) {
1206
25
      xmlFree(ret);
1207
25
      goto mem_error;
1208
25
  }
1209
62.4k
  ret->prefix = prefix;
1210
62.4k
        prefix = NULL;
1211
1212
  /*
1213
   * Validity Check:
1214
   * Insertion must not fail
1215
   */
1216
62.4k
        res = xmlHashAdd2(table, localName, ret->prefix, ret);
1217
62.4k
        if (res <= 0) {
1218
102
      xmlFreeElement(ret);
1219
102
            goto mem_error;
1220
102
  }
1221
  /*
1222
   * For new element, may have attributes from earlier
1223
   * definition in internal subset
1224
   */
1225
62.3k
  ret->attributes = oldAttributes;
1226
62.3k
    }
1227
1228
    /*
1229
     * Finish to fill the structure.
1230
     */
1231
62.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
62.7k
    if (content != NULL) {
1238
44.7k
        if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT)) {
1239
44.7k
            ret->content = content;
1240
44.7k
            content->parent = (xmlElementContentPtr) 1;
1241
44.7k
        } 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
44.7k
    }
1247
1248
    /*
1249
     * Link it to the DTD
1250
     */
1251
62.7k
    ret->parent = dtd;
1252
62.7k
    ret->doc = dtd->doc;
1253
62.7k
    if (dtd->last == NULL) {
1254
27.1k
  dtd->children = dtd->last = (xmlNodePtr) ret;
1255
35.5k
    } else {
1256
35.5k
        dtd->last->next = (xmlNodePtr) ret;
1257
35.5k
  ret->prev = dtd->last;
1258
35.5k
  dtd->last = (xmlNodePtr) ret;
1259
35.5k
    }
1260
62.7k
    if (prefix != NULL)
1261
0
  xmlFree(prefix);
1262
62.7k
    return(ret);
1263
1264
217
mem_error:
1265
217
    xmlVErrMemory(ctxt);
1266
217
    if (prefix != NULL)
1267
18
        xmlFree(prefix);
1268
217
    return(NULL);
1269
62.7k
}
1270
1271
static void
1272
126k
xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) {
1273
126k
    xmlFreeElement((xmlElementPtr) elem);
1274
126k
}
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
80.5k
xmlFreeElementTable(xmlElementTable *table) {
1285
80.5k
    xmlHashFree(table, xmlFreeElementTableEntry);
1286
80.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
12.7k
xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
1297
12.7k
    xmlElementPtr elem = (xmlElementPtr) payload;
1298
12.7k
    xmlElementPtr cur;
1299
1300
12.7k
    cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1301
12.7k
    if (cur == NULL)
1302
11
  return(NULL);
1303
12.7k
    memset(cur, 0, sizeof(xmlElement));
1304
12.7k
    cur->type = XML_ELEMENT_DECL;
1305
12.7k
    cur->etype = elem->etype;
1306
12.7k
    if (elem->name != NULL) {
1307
12.7k
  cur->name = xmlStrdup(elem->name);
1308
12.7k
        if (cur->name == NULL)
1309
5
            goto error;
1310
12.7k
    }
1311
12.7k
    if (elem->prefix != NULL) {
1312
4.18k
  cur->prefix = xmlStrdup(elem->prefix);
1313
4.18k
        if (cur->prefix == NULL)
1314
3
            goto error;
1315
4.18k
    }
1316
12.7k
    if (elem->content != NULL) {
1317
3.01k
        cur->content = xmlCopyElementContent(elem->content);
1318
3.01k
        if (cur->content == NULL)
1319
64
            goto error;
1320
3.01k
    }
1321
    /* TODO : rebuild the attribute list on the copy */
1322
12.6k
    cur->attributes = NULL;
1323
12.6k
    return(cur);
1324
1325
72
error:
1326
72
    xmlFreeElement(cur);
1327
72
    return(NULL);
1328
12.7k
}
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
5.26k
xmlCopyElementTable(xmlElementTable *table) {
1340
5.26k
    return(xmlHashCopySafe(table, xmlCopyElement, xmlFreeElementTableEntry));
1341
5.26k
}
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.18k
xmlDumpElementDecl(xmlBuffer *buf, xmlElement *elem) {
1355
2.18k
    xmlSaveCtxtPtr save;
1356
1357
2.18k
    if ((buf == NULL) || (elem == NULL))
1358
1.79k
        return;
1359
1360
396
    save = xmlSaveToBuffer(buf, NULL, 0);
1361
396
    xmlSaveTree(save, (xmlNodePtr) elem);
1362
396
    if (xmlSaveFinish(save) != XML_ERR_OK)
1363
1
        xmlFree(xmlBufferDetach(buf));
1364
396
}
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
706
                       const xmlChar *name ATTRIBUTE_UNUSED) {
1377
706
    xmlSaveTree(save, elem);
1378
706
}
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.80k
xmlDumpElementTable(xmlBuffer *buf, xmlElementTable *table) {
1391
2.80k
    xmlSaveCtxtPtr save;
1392
1393
2.80k
    if ((buf == NULL) || (table == NULL))
1394
2.47k
        return;
1395
1396
323
    save = xmlSaveToBuffer(buf, NULL, 0);
1397
323
    xmlHashScan(table, xmlDumpElementDeclScan, save);
1398
323
    if (xmlSaveFinish(save) != XML_ERR_OK)
1399
3
        xmlFree(xmlBufferDetach(buf));
1400
323
}
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
136k
xmlCreateEnumeration(const xmlChar *name) {
1414
136k
    xmlEnumerationPtr ret;
1415
1416
136k
    ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1417
136k
    if (ret == NULL)
1418
52
        return(NULL);
1419
136k
    memset(ret, 0, sizeof(xmlEnumeration));
1420
1421
136k
    if (name != NULL) {
1422
136k
        ret->name = xmlStrdup(name);
1423
136k
        if (ret->name == NULL) {
1424
77
            xmlFree(ret);
1425
77
            return(NULL);
1426
77
        }
1427
136k
    }
1428
1429
136k
    return(ret);
1430
136k
}
1431
1432
/**
1433
 * Free an enumeration attribute node (recursive).
1434
 *
1435
 * @param cur  the tree to free.
1436
 */
1437
void
1438
104k
xmlFreeEnumeration(xmlEnumeration *cur) {
1439
240k
    while (cur != NULL) {
1440
136k
        xmlEnumerationPtr next = cur->next;
1441
1442
136k
        xmlFree((xmlChar *) cur->name);
1443
136k
        xmlFree(cur);
1444
1445
136k
        cur = next;
1446
136k
    }
1447
104k
}
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
2.64k
xmlCopyEnumeration(xmlEnumeration *cur) {
1460
2.64k
    xmlEnumerationPtr ret = NULL;
1461
2.64k
    xmlEnumerationPtr last = NULL;
1462
1463
8.73k
    while (cur != NULL) {
1464
6.11k
        xmlEnumerationPtr copy = xmlCreateEnumeration(cur->name);
1465
1466
6.11k
        if (copy == NULL) {
1467
22
            xmlFreeEnumeration(ret);
1468
22
            return(NULL);
1469
22
        }
1470
1471
6.09k
        if (ret == NULL) {
1472
2.63k
            ret = last = copy;
1473
3.46k
        } else {
1474
3.46k
            last->next = copy;
1475
3.46k
            last = copy;
1476
3.46k
        }
1477
1478
6.09k
        cur = cur->next;
1479
6.09k
    }
1480
1481
2.61k
    return(ret);
1482
2.64k
}
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
16.6k
xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
1496
16.6k
    xmlAttributePtr cur;
1497
16.6k
    int ret = 0;
1498
1499
16.6k
    if (elem == NULL) return(0);
1500
16.6k
    cur = elem->attributes;
1501
39.7k
    while (cur != NULL) {
1502
23.1k
        if (cur->atype == XML_ATTRIBUTE_ID) {
1503
18.9k
      ret ++;
1504
18.9k
      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
18.9k
  }
1509
23.1k
  cur = cur->nexth;
1510
23.1k
    }
1511
16.6k
    return(ret);
1512
16.6k
}
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
318k
xmlFreeAttribute(xmlAttributePtr attr) {
1522
318k
    xmlDictPtr dict;
1523
1524
318k
    if (attr == NULL) return;
1525
318k
    if (attr->doc != NULL)
1526
312k
  dict = attr->doc->dict;
1527
5.43k
    else
1528
5.43k
  dict = NULL;
1529
318k
    xmlUnlinkNode((xmlNodePtr) attr);
1530
318k
    if (attr->tree != NULL)
1531
66.0k
        xmlFreeEnumeration(attr->tree);
1532
318k
    if (dict) {
1533
228k
        if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1534
111
      xmlFree((xmlChar *) attr->elem);
1535
228k
        if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1536
111
      xmlFree((xmlChar *) attr->name);
1537
228k
        if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1538
93
      xmlFree((xmlChar *) attr->prefix);
1539
228k
    } else {
1540
89.6k
  if (attr->elem != NULL)
1541
89.5k
      xmlFree((xmlChar *) attr->elem);
1542
89.6k
  if (attr->name != NULL)
1543
89.5k
      xmlFree((xmlChar *) attr->name);
1544
89.6k
  if (attr->prefix != NULL)
1545
23.5k
      xmlFree((xmlChar *) attr->prefix);
1546
89.6k
    }
1547
318k
    if (attr->defaultValue != NULL)
1548
191k
        xmlFree(attr->defaultValue);
1549
318k
    xmlFree(attr);
1550
318k
}
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
308k
        const xmlChar *defaultValue, xmlEnumeration *tree) {
1573
308k
    xmlAttributePtr ret = NULL;
1574
308k
    xmlAttributeTablePtr table;
1575
308k
    xmlElementPtr elemDef;
1576
308k
    xmlDictPtr dict = NULL;
1577
308k
    int res;
1578
1579
308k
    if (dtd == NULL) {
1580
1.13k
  xmlFreeEnumeration(tree);
1581
1.13k
  return(NULL);
1582
1.13k
    }
1583
307k
    if (name == NULL) {
1584
394
  xmlFreeEnumeration(tree);
1585
394
  return(NULL);
1586
394
    }
1587
307k
    if (elem == NULL) {
1588
213
  xmlFreeEnumeration(tree);
1589
213
  return(NULL);
1590
213
    }
1591
306k
    if (dtd->doc != NULL)
1592
306k
  dict = dtd->doc->dict;
1593
1594
306k
#ifdef LIBXML_VALID_ENABLED
1595
306k
    if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE) &&
1596
105k
        (defaultValue != NULL) &&
1597
58.5k
        (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
1598
14.3k
  xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1599
14.3k
                  "Attribute %s of %s: invalid default value\n",
1600
14.3k
                  elem, name, defaultValue);
1601
14.3k
  defaultValue = NULL;
1602
14.3k
  if (ctxt != NULL)
1603
14.3k
      ctxt->valid = 0;
1604
14.3k
    }
1605
306k
#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
306k
    if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1612
100k
  (dtd->doc->intSubset != NULL) &&
1613
100k
  (dtd->doc->intSubset->attributes != NULL)) {
1614
17.2k
        ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1615
17.2k
  if (ret != NULL) {
1616
6.90k
      xmlFreeEnumeration(tree);
1617
6.90k
      return(NULL);
1618
6.90k
  }
1619
17.2k
    }
1620
1621
    /*
1622
     * Create the Attribute table if needed.
1623
     */
1624
300k
    table = (xmlAttributeTablePtr) dtd->attributes;
1625
300k
    if (table == NULL) {
1626
50.8k
        table = xmlHashCreateDict(0, dict);
1627
50.8k
  dtd->attributes = (void *) table;
1628
50.8k
    }
1629
300k
    if (table == NULL)
1630
67
        goto mem_error;
1631
1632
299k
    ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1633
299k
    if (ret == NULL)
1634
98
        goto mem_error;
1635
299k
    memset(ret, 0, sizeof(xmlAttribute));
1636
299k
    ret->type = XML_ATTRIBUTE_DECL;
1637
1638
    /*
1639
     * fill the structure.
1640
     */
1641
299k
    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
299k
    ret->doc = dtd->doc;
1648
299k
    if (dict) {
1649
228k
  ret->name = xmlDictLookup(dict, name, -1);
1650
228k
  ret->elem = xmlDictLookup(dict, elem, -1);
1651
228k
    } else {
1652
71.4k
  ret->name = xmlStrdup(name);
1653
71.4k
  ret->elem = xmlStrdup(elem);
1654
71.4k
    }
1655
299k
    if ((ret->name == NULL) || (ret->elem == NULL))
1656
74
        goto mem_error;
1657
299k
    if (ns != NULL) {
1658
44.8k
        if (dict)
1659
29.9k
            ret->prefix = xmlDictLookup(dict, ns, -1);
1660
14.9k
        else
1661
14.9k
            ret->prefix = xmlStrdup(ns);
1662
44.8k
        if (ret->prefix == NULL)
1663
22
            goto mem_error;
1664
44.8k
    }
1665
299k
    ret->def = def;
1666
299k
    ret->tree = tree;
1667
299k
    tree = NULL;
1668
299k
    if (defaultValue != NULL) {
1669
180k
  ret->defaultValue = xmlStrdup(defaultValue);
1670
180k
        if (ret->defaultValue == NULL)
1671
50
            goto mem_error;
1672
180k
    }
1673
1674
299k
    elemDef = xmlGetDtdElementDesc2(ctxt, dtd, elem);
1675
299k
    if (elemDef == NULL)
1676
180
        goto mem_error;
1677
1678
    /*
1679
     * Validity Check:
1680
     * Search the DTD for previous declarations of the ATTLIST
1681
     */
1682
299k
    res = xmlHashAdd3(table, ret->name, ret->prefix, ret->elem, ret);
1683
299k
    if (res <= 0) {
1684
110k
        if (res < 0)
1685
128
            goto mem_error;
1686
110k
#ifdef LIBXML_VALID_ENABLED
1687
        /*
1688
         * The attribute is already defined in this DTD.
1689
         */
1690
110k
        if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE))
1691
31.3k
            xmlErrValidWarning(ctxt, (xmlNodePtr) dtd,
1692
31.3k
                    XML_DTD_ATTRIBUTE_REDEFINED,
1693
31.3k
                    "Attribute %s of element %s: already defined\n",
1694
31.3k
                    name, elem, NULL);
1695
110k
#endif /* LIBXML_VALID_ENABLED */
1696
110k
  xmlFreeAttribute(ret);
1697
110k
  return(NULL);
1698
110k
    }
1699
1700
    /*
1701
     * Insert namespace default def first they need to be
1702
     * processed first.
1703
     */
1704
188k
    if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1705
183k
        ((ret->prefix != NULL &&
1706
35.8k
         (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1707
16.3k
        ret->nexth = elemDef->attributes;
1708
16.3k
        elemDef->attributes = ret;
1709
172k
    } else {
1710
172k
        xmlAttributePtr tmp = elemDef->attributes;
1711
1712
176k
        while ((tmp != NULL) &&
1713
109k
               ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1714
105k
                ((ret->prefix != NULL &&
1715
9.34k
                 (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1716
4.93k
            if (tmp->nexth == NULL)
1717
878
                break;
1718
4.06k
            tmp = tmp->nexth;
1719
4.06k
        }
1720
172k
        if (tmp != NULL) {
1721
105k
            ret->nexth = tmp->nexth;
1722
105k
            tmp->nexth = ret;
1723
105k
        } else {
1724
66.5k
            ret->nexth = elemDef->attributes;
1725
66.5k
            elemDef->attributes = ret;
1726
66.5k
        }
1727
172k
    }
1728
1729
    /*
1730
     * Link it to the DTD
1731
     */
1732
188k
    ret->parent = dtd;
1733
188k
    if (dtd->last == NULL) {
1734
40.2k
  dtd->children = dtd->last = (xmlNodePtr) ret;
1735
148k
    } else {
1736
148k
        dtd->last->next = (xmlNodePtr) ret;
1737
148k
  ret->prev = dtd->last;
1738
148k
  dtd->last = (xmlNodePtr) ret;
1739
148k
    }
1740
188k
    return(ret);
1741
1742
619
mem_error:
1743
619
    xmlVErrMemory(ctxt);
1744
619
    xmlFreeEnumeration(tree);
1745
619
    xmlFreeAttribute(ret);
1746
619
    return(NULL);
1747
299k
}
1748
1749
static void
1750
207k
xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) {
1751
207k
    xmlFreeAttribute((xmlAttributePtr) attr);
1752
207k
}
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
54.3k
xmlFreeAttributeTable(xmlAttributeTable *table) {
1763
54.3k
    xmlHashFree(table, xmlFreeAttributeTableEntry);
1764
54.3k
}
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
18.3k
xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
1775
18.3k
    xmlAttributePtr attr = (xmlAttributePtr) payload;
1776
18.3k
    xmlAttributePtr cur;
1777
1778
18.3k
    cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1779
18.3k
    if (cur == NULL)
1780
9
  return(NULL);
1781
18.3k
    memset(cur, 0, sizeof(xmlAttribute));
1782
18.3k
    cur->type = XML_ATTRIBUTE_DECL;
1783
18.3k
    cur->atype = attr->atype;
1784
18.3k
    cur->def = attr->def;
1785
18.3k
    if (attr->tree != NULL) {
1786
2.64k
        cur->tree = xmlCopyEnumeration(attr->tree);
1787
2.64k
        if (cur->tree == NULL)
1788
22
            goto error;
1789
2.64k
    }
1790
18.3k
    if (attr->elem != NULL) {
1791
18.3k
  cur->elem = xmlStrdup(attr->elem);
1792
18.3k
        if (cur->elem == NULL)
1793
9
            goto error;
1794
18.3k
    }
1795
18.3k
    if (attr->name != NULL) {
1796
18.3k
  cur->name = xmlStrdup(attr->name);
1797
18.3k
        if (cur->name == NULL)
1798
9
            goto error;
1799
18.3k
    }
1800
18.3k
    if (attr->prefix != NULL) {
1801
8.72k
  cur->prefix = xmlStrdup(attr->prefix);
1802
8.72k
        if (cur->prefix == NULL)
1803
3
            goto error;
1804
8.72k
    }
1805
18.3k
    if (attr->defaultValue != NULL) {
1806
10.9k
  cur->defaultValue = xmlStrdup(attr->defaultValue);
1807
10.9k
        if (cur->defaultValue == NULL)
1808
12
            goto error;
1809
10.9k
    }
1810
18.3k
    return(cur);
1811
1812
55
error:
1813
55
    xmlFreeAttribute(cur);
1814
55
    return(NULL);
1815
18.3k
}
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
3.63k
xmlCopyAttributeTable(xmlAttributeTable *table) {
1827
3.63k
    return(xmlHashCopySafe(table, xmlCopyAttribute,
1828
3.63k
                           xmlFreeAttributeTableEntry));
1829
3.63k
}
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.59k
xmlDumpAttributeDecl(xmlBuffer *buf, xmlAttribute *attr) {
1843
3.59k
    xmlSaveCtxtPtr save;
1844
1845
3.59k
    if ((buf == NULL) || (attr == NULL))
1846
2.75k
        return;
1847
1848
846
    save = xmlSaveToBuffer(buf, NULL, 0);
1849
846
    xmlSaveTree(save, (xmlNodePtr) attr);
1850
846
    if (xmlSaveFinish(save) != XML_ERR_OK)
1851
1
        xmlFree(xmlBufferDetach(buf));
1852
846
}
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
6.62k
                         const xmlChar *name ATTRIBUTE_UNUSED) {
1865
6.62k
    xmlSaveTree(save, attr);
1866
6.62k
}
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.99k
xmlDumpAttributeTable(xmlBuffer *buf, xmlAttributeTable *table) {
1879
3.99k
    xmlSaveCtxtPtr save;
1880
1881
3.99k
    if ((buf == NULL) || (table == NULL))
1882
3.33k
        return;
1883
1884
659
    save = xmlSaveToBuffer(buf, NULL, 0);
1885
659
    xmlHashScan(table, xmlDumpAttributeDeclScan, save);
1886
659
    if (xmlSaveFinish(save) != XML_ERR_OK)
1887
6
        xmlFree(xmlBufferDetach(buf));
1888
659
}
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
19.1k
xmlFreeNotation(xmlNotationPtr nota) {
1903
19.1k
    if (nota == NULL) return;
1904
19.0k
    if (nota->name != NULL)
1905
19.0k
  xmlFree((xmlChar *) nota->name);
1906
19.0k
    if (nota->PublicID != NULL)
1907
10.7k
  xmlFree((xmlChar *) nota->PublicID);
1908
19.0k
    if (nota->SystemID != NULL)
1909
10.7k
  xmlFree((xmlChar *) nota->SystemID);
1910
19.0k
    xmlFree(nota);
1911
19.0k
}
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
19.1k
                   const xmlChar *publicId, const xmlChar *systemId) {
1927
19.1k
    xmlNotationPtr ret = NULL;
1928
19.1k
    xmlNotationTablePtr table;
1929
19.1k
    int res;
1930
1931
19.1k
    if (dtd == NULL) {
1932
1.00k
  return(NULL);
1933
1.00k
    }
1934
18.1k
    if (name == NULL) {
1935
369
  return(NULL);
1936
369
    }
1937
17.7k
    if ((publicId == NULL) && (systemId == NULL)) {
1938
315
  return(NULL);
1939
315
    }
1940
1941
    /*
1942
     * Create the Notation table if needed.
1943
     */
1944
17.4k
    table = (xmlNotationTablePtr) dtd->notations;
1945
17.4k
    if (table == NULL) {
1946
4.45k
  xmlDictPtr dict = NULL;
1947
4.45k
  if (dtd->doc != NULL)
1948
4.24k
      dict = dtd->doc->dict;
1949
1950
4.45k
        dtd->notations = table = xmlHashCreateDict(0, dict);
1951
4.45k
        if (table == NULL)
1952
22
            goto mem_error;
1953
4.45k
    }
1954
1955
17.3k
    ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
1956
17.3k
    if (ret == NULL)
1957
24
        goto mem_error;
1958
17.3k
    memset(ret, 0, sizeof(xmlNotation));
1959
1960
    /*
1961
     * fill the structure.
1962
     */
1963
17.3k
    ret->name = xmlStrdup(name);
1964
17.3k
    if (ret->name == NULL)
1965
20
        goto mem_error;
1966
17.3k
    if (systemId != NULL) {
1967
9.78k
        ret->SystemID = xmlStrdup(systemId);
1968
9.78k
        if (ret->SystemID == NULL)
1969
28
            goto mem_error;
1970
9.78k
    }
1971
17.3k
    if (publicId != NULL) {
1972
9.54k
        ret->PublicID = xmlStrdup(publicId);
1973
9.54k
        if (ret->PublicID == NULL)
1974
26
            goto mem_error;
1975
9.54k
    }
1976
1977
    /*
1978
     * Validity Check:
1979
     * Check the DTD for previous declarations of the ATTLIST
1980
     */
1981
17.2k
    res = xmlHashAdd(table, name, ret);
1982
17.2k
    if (res <= 0) {
1983
11.9k
        if (res < 0)
1984
39
            goto mem_error;
1985
11.8k
#ifdef LIBXML_VALID_ENABLED
1986
11.8k
        if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_VALIDATE))
1987
2.31k
            xmlErrValid(ctxt, XML_DTD_NOTATION_REDEFINED,
1988
2.31k
                        "xmlAddNotationDecl: %s already defined\n",
1989
2.31k
                        (const char *) name);
1990
11.8k
#endif /* LIBXML_VALID_ENABLED */
1991
11.8k
  xmlFreeNotation(ret);
1992
11.8k
  return(NULL);
1993
11.9k
    }
1994
5.36k
    return(ret);
1995
1996
159
mem_error:
1997
159
    xmlVErrMemory(ctxt);
1998
159
    xmlFreeNotation(ret);
1999
159
    return(NULL);
2000
17.2k
}
2001
2002
static void
2003
7.03k
xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) {
2004
7.03k
    xmlFreeNotation((xmlNotationPtr) nota);
2005
7.03k
}
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
5.13k
xmlFreeNotationTable(xmlNotationTable *table) {
2016
5.13k
    xmlHashFree(table, xmlFreeNotationTableEntry);
2017
5.13k
}
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.68k
xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2028
1.68k
    xmlNotationPtr nota = (xmlNotationPtr) payload;
2029
1.68k
    xmlNotationPtr cur;
2030
2031
1.68k
    cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2032
1.68k
    if (cur == NULL)
2033
2
  return(NULL);
2034
1.68k
    memset(cur, 0, sizeof(*cur));
2035
1.68k
    if (nota->name != NULL) {
2036
1.68k
  cur->name = xmlStrdup(nota->name);
2037
1.68k
        if (cur->name == NULL)
2038
4
            goto error;
2039
1.68k
    }
2040
1.67k
    if (nota->PublicID != NULL) {
2041
1.26k
  cur->PublicID = xmlStrdup(nota->PublicID);
2042
1.26k
        if (cur->PublicID == NULL)
2043
3
            goto error;
2044
1.26k
    }
2045
1.67k
    if (nota->SystemID != NULL) {
2046
964
  cur->SystemID = xmlStrdup(nota->SystemID);
2047
964
        if (cur->SystemID == NULL)
2048
4
            goto error;
2049
964
    }
2050
1.67k
    return(cur);
2051
2052
11
error:
2053
11
    xmlFreeNotation(cur);
2054
11
    return(NULL);
2055
1.67k
}
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
712
xmlCopyNotationTable(xmlNotationTable *table) {
2067
712
    return(xmlHashCopySafe(table, xmlCopyNotation, xmlFreeNotationTableEntry));
2068
712
}
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.55k
xmlDumpNotationTable(xmlBuffer *buf, xmlNotationTable *table) {
2104
4.55k
    xmlSaveCtxtPtr save;
2105
2106
4.55k
    if ((buf == NULL) || (table == NULL))
2107
4.13k
        return;
2108
2109
416
    save = xmlSaveToBuffer(buf, NULL, 0);
2110
416
    xmlSaveNotationTable(save, table);
2111
416
    if (xmlSaveFinish(save) != XML_ERR_OK)
2112
1
        xmlFree(xmlBufferDetach(buf));
2113
416
}
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
143
  if ((str) && ((!dict) ||       \
2130
143
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
2131
143
      xmlFree((char *)(str));
2132
2133
static int
2134
118k
xmlIsStreaming(xmlValidCtxtPtr ctxt) {
2135
118k
    xmlParserCtxtPtr pctxt;
2136
2137
118k
    if (ctxt == NULL)
2138
934
        return(0);
2139
117k
    if ((ctxt->flags & XML_VCTXT_USE_PCTXT) == 0)
2140
921
        return(0);
2141
116k
    pctxt = ctxt->userData;
2142
116k
    return(pctxt->parseMode == XML_PARSE_READER);
2143
117k
}
2144
2145
/**
2146
 * Deallocate the memory used by an id definition.
2147
 *
2148
 * @param id  an id
2149
 */
2150
static void
2151
63.9k
xmlFreeID(xmlIDPtr id) {
2152
63.9k
    xmlDictPtr dict = NULL;
2153
2154
63.9k
    if (id == NULL) return;
2155
2156
63.9k
    if (id->doc != NULL)
2157
63.9k
        dict = id->doc->dict;
2158
2159
63.9k
    if (id->value != NULL)
2160
63.8k
  xmlFree(id->value);
2161
63.9k
    if (id->name != NULL)
2162
143
  DICT_FREE(id->name)
2163
63.9k
    if (id->attr != NULL) {
2164
63.5k
        id->attr->id = NULL;
2165
63.5k
        id->attr->atype = 0;
2166
63.5k
    }
2167
2168
63.9k
    xmlFree(id);
2169
63.9k
}
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
245k
xmlAddIDInternal(xmlAttrPtr attr, const xmlChar *value, xmlIDPtr *idPtr) {
2183
245k
    xmlDocPtr doc;
2184
245k
    xmlIDPtr id;
2185
245k
    xmlIDTablePtr table;
2186
245k
    int ret;
2187
2188
245k
    if (idPtr != NULL)
2189
168k
        *idPtr = NULL;
2190
245k
    if ((value == NULL) || (value[0] == 0))
2191
18.8k
  return(0);
2192
226k
    if (attr == NULL)
2193
483
  return(0);
2194
2195
225k
    doc = attr->doc;
2196
225k
    if (doc == NULL)
2197
243
        return(0);
2198
2199
    /*
2200
     * Create the ID table if needed.
2201
     */
2202
225k
    table = (xmlIDTablePtr) doc->ids;
2203
225k
    if (table == NULL)  {
2204
11.8k
        doc->ids = table = xmlHashCreate(0);
2205
11.8k
        if (table == NULL)
2206
31
            return(-1);
2207
213k
    } else {
2208
213k
        id = xmlHashLookup(table, value);
2209
213k
        if (id != NULL)
2210
161k
            return(0);
2211
213k
    }
2212
2213
63.9k
    id = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2214
63.9k
    if (id == NULL)
2215
61
  return(-1);
2216
63.9k
    memset(id, 0, sizeof(*id));
2217
2218
    /*
2219
     * fill the structure.
2220
     */
2221
63.9k
    id->doc = doc;
2222
63.9k
    id->value = xmlStrdup(value);
2223
63.9k
    if (id->value == NULL) {
2224
69
        xmlFreeID(id);
2225
69
        return(-1);
2226
69
    }
2227
2228
63.8k
    if (attr->id != NULL)
2229
217
        xmlRemoveID(doc, attr);
2230
2231
63.8k
    if (xmlHashAddEntry(table, value, id) < 0) {
2232
123
  xmlFreeID(id);
2233
123
  return(-1);
2234
123
    }
2235
2236
63.7k
    ret = 1;
2237
63.7k
    if (idPtr != NULL)
2238
40.3k
        *idPtr = id;
2239
2240
63.7k
    id->attr = attr;
2241
63.7k
    id->lineno = xmlGetLineNo(attr->parent);
2242
63.7k
    attr->atype = XML_ATTRIBUTE_ID;
2243
63.7k
    attr->id = id;
2244
2245
63.7k
    return(ret);
2246
63.8k
}
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
76.6k
xmlAddIDSafe(xmlAttr *attr, const xmlChar *value) {
2260
76.6k
    return(xmlAddIDInternal(attr, value, NULL));
2261
76.6k
}
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
169k
         xmlAttr *attr) {
2275
169k
    xmlIDPtr id;
2276
169k
    int res;
2277
2278
169k
    if ((attr == NULL) || (doc != attr->doc))
2279
1.04k
        return(NULL);
2280
2281
168k
    res = xmlAddIDInternal(attr, value, &id);
2282
168k
    if (res < 0) {
2283
165
        xmlVErrMemory(ctxt);
2284
165
    }
2285
168k
    else if (res == 0) {
2286
127k
        if (ctxt != NULL) {
2287
            /*
2288
             * The id is already defined in this DTD.
2289
             */
2290
127k
            xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2291
127k
                            "ID %s already defined\n", value, NULL, NULL);
2292
127k
        }
2293
127k
    }
2294
2295
168k
    return(id);
2296
169k
}
2297
2298
static void
2299
63.7k
xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) {
2300
63.7k
    xmlFreeID((xmlIDPtr) id);
2301
63.7k
}
2302
2303
/**
2304
 * Deallocate the memory used by an ID hash table.
2305
 *
2306
 * @param table  An id table
2307
 */
2308
void
2309
11.8k
xmlFreeIDTable(xmlIDTable *table) {
2310
11.8k
    xmlHashFree(table, xmlFreeIDTableEntry);
2311
11.8k
}
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
3.22M
xmlIsID(xmlDoc *doc, xmlNode *elem, xmlAttr *attr) {
2327
3.22M
    if ((attr == NULL) || (attr->name == NULL))
2328
2.18k
        return(0);
2329
2330
3.21M
    if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
2331
142k
        if (xmlStrEqual(BAD_CAST "id", attr->name))
2332
52.4k
            return(1);
2333
2334
90.1k
        if ((elem == NULL) || (elem->type != XML_ELEMENT_NODE))
2335
430
            return(0);
2336
2337
89.7k
        if ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
2338
3.33k
      (xmlStrEqual(elem->name, BAD_CAST "a")))
2339
1.03k
      return(1);
2340
3.07M
    } else {
2341
3.07M
  xmlAttributePtr attrDecl = NULL;
2342
3.07M
  xmlChar felem[50];
2343
3.07M
  xmlChar *fullelemname;
2344
3.07M
        const xmlChar *aprefix;
2345
2346
3.07M
        if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2347
624k
            (!strcmp((char *) attr->name, "id")) &&
2348
19.9k
            (!strcmp((char *) attr->ns->prefix, "xml")))
2349
13.7k
            return(1);
2350
2351
3.06M
        if ((doc == NULL) ||
2352
3.06M
            ((doc->intSubset == NULL) && (doc->extSubset == NULL)))
2353
2.18M
            return(0);
2354
2355
881k
        if ((elem == NULL) ||
2356
881k
            (elem->type != XML_ELEMENT_NODE) ||
2357
881k
            (elem->name == NULL))
2358
741
            return(0);
2359
2360
880k
  fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2361
39.2k
      xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2362
880k
      (xmlChar *)elem->name;
2363
880k
        if (fullelemname == NULL)
2364
41
            return(-1);
2365
2366
880k
        aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
2367
2368
880k
  if (fullelemname != NULL) {
2369
880k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullelemname,
2370
880k
                              attr->name, aprefix);
2371
880k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
2372
19.9k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullelemname,
2373
19.9k
                attr->name, aprefix);
2374
880k
  }
2375
2376
880k
  if ((fullelemname != felem) && (fullelemname != elem->name))
2377
26.3k
      xmlFree(fullelemname);
2378
2379
880k
        if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2380
114k
      return(1);
2381
880k
    }
2382
2383
855k
    return(0);
2384
3.21M
}
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
10.3k
xmlRemoveID(xmlDoc *doc, xmlAttr *attr) {
2395
10.3k
    xmlIDTablePtr table;
2396
2397
10.3k
    if (doc == NULL) return(-1);
2398
9.65k
    if ((attr == NULL) || (attr->id == NULL)) return(-1);
2399
2400
8.84k
    table = (xmlIDTablePtr) doc->ids;
2401
8.84k
    if (table == NULL)
2402
199
        return(-1);
2403
2404
8.64k
    if (xmlHashRemoveEntry(table, attr->id->value, xmlFreeIDTableEntry) < 0)
2405
493
        return(-1);
2406
2407
8.15k
    return(0);
2408
8.64k
}
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
20.6M
xmlGetID(xmlDoc *doc, const xmlChar *ID) {
2420
20.6M
    xmlIDTablePtr table;
2421
20.6M
    xmlIDPtr id;
2422
2423
20.6M
    if (doc == NULL) {
2424
332
  return(NULL);
2425
332
    }
2426
2427
20.6M
    if (ID == NULL) {
2428
214
  return(NULL);
2429
214
    }
2430
2431
20.6M
    table = (xmlIDTablePtr) doc->ids;
2432
20.6M
    if (table == NULL)
2433
10.1M
        return(NULL);
2434
2435
10.4M
    id = xmlHashLookup(table, ID);
2436
10.4M
    if (id == NULL)
2437
10.4M
  return(NULL);
2438
29.4k
    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
149
  return((xmlAttrPtr) doc);
2444
149
    }
2445
29.2k
    return(id->attr);
2446
29.4k
}
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
118k
xmlFreeRef(xmlLinkPtr lk) {
2476
118k
    xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2477
118k
    if (ref == NULL) return;
2478
118k
    if (ref->value != NULL)
2479
118k
        xmlFree((xmlChar *)ref->value);
2480
118k
    if (ref->name != NULL)
2481
4.42k
        xmlFree((xmlChar *)ref->name);
2482
118k
    xmlFree(ref);
2483
118k
}
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
9.35k
xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2493
9.35k
    xmlListPtr list_ref = (xmlListPtr) payload;
2494
9.35k
    if (list_ref == NULL) return;
2495
9.35k
    xmlListDelete(list_ref);
2496
9.35k
}
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
779
{
2506
779
    xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2507
779
    xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2508
779
    xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
2509
2510
779
    if (attr0 == attr1) { /* Matched: remove and terminate walk */
2511
341
        xmlListRemoveFirst(ref_list, (void *)data);
2512
341
        return 0;
2513
341
    }
2514
438
    return 1;
2515
779
}
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
109k
{
2528
109k
    return (0);
2529
109k
}
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
119k
    xmlAttr *attr) {
2546
119k
    xmlRefPtr ret = NULL;
2547
119k
    xmlRefTablePtr table;
2548
119k
    xmlListPtr ref_list;
2549
2550
119k
    if (doc == NULL) {
2551
536
        return(NULL);
2552
536
    }
2553
119k
    if (value == NULL) {
2554
235
        return(NULL);
2555
235
    }
2556
119k
    if (attr == NULL) {
2557
461
        return(NULL);
2558
461
    }
2559
2560
    /*
2561
     * Create the Ref table if needed.
2562
     */
2563
118k
    table = (xmlRefTablePtr) doc->refs;
2564
118k
    if (table == NULL) {
2565
4.22k
        doc->refs = table = xmlHashCreate(0);
2566
4.22k
        if (table == NULL)
2567
16
            goto failed;
2568
4.22k
    }
2569
2570
118k
    ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2571
118k
    if (ret == NULL)
2572
24
        goto failed;
2573
118k
    memset(ret, 0, sizeof(*ret));
2574
2575
    /*
2576
     * fill the structure.
2577
     */
2578
118k
    ret->value = xmlStrdup(value);
2579
118k
    if (ret->value == NULL)
2580
26
        goto failed;
2581
118k
    if (xmlIsStreaming(ctxt)) {
2582
  /*
2583
   * Operating in streaming mode, attr is gonna disappear
2584
   */
2585
4.43k
  ret->name = xmlStrdup(attr->name);
2586
4.43k
        if (ret->name == NULL)
2587
3
            goto failed;
2588
4.43k
  ret->attr = NULL;
2589
114k
    } else {
2590
114k
  ret->name = NULL;
2591
114k
  ret->attr = attr;
2592
114k
    }
2593
118k
    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
118k
    ref_list = xmlHashLookup(table, value);
2603
118k
    if (ref_list == NULL) {
2604
9.41k
        int res;
2605
2606
9.41k
        ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare);
2607
9.41k
        if (ref_list == NULL)
2608
37
      goto failed;
2609
9.37k
        res = xmlHashAdd(table, value, ref_list);
2610
9.37k
        if (res <= 0) {
2611
20
            xmlListDelete(ref_list);
2612
20
      goto failed;
2613
20
        }
2614
9.37k
    }
2615
118k
    if (xmlListAppend(ref_list, ret) != 0)
2616
25
        goto failed;
2617
118k
    return(ret);
2618
2619
151
failed:
2620
151
    xmlVErrMemory(ctxt);
2621
151
    if (ret != NULL) {
2622
111
        if (ret->value != NULL)
2623
85
      xmlFree((char *)ret->value);
2624
111
        if (ret->name != NULL)
2625
8
      xmlFree((char *)ret->name);
2626
111
        xmlFree(ret);
2627
111
    }
2628
151
    return(NULL);
2629
118k
}
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
4.20k
xmlFreeRefTable(xmlRefTable *table) {
2641
4.20k
    xmlHashFree(table, xmlFreeRefTableEntry);
2642
4.20k
}
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
1.38M
xmlIsRef(xmlDoc *doc, xmlNode *elem, xmlAttr *attr) {
2659
1.38M
    if (attr == NULL)
2660
1.12k
        return(0);
2661
1.37M
    if (doc == NULL) {
2662
400
        doc = attr->doc;
2663
400
  if (doc == NULL) return(0);
2664
400
    }
2665
2666
1.37M
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2667
913k
        return(0);
2668
913k
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2669
        /* TODO @@@ */
2670
210
        return(0);
2671
465k
    } else {
2672
465k
        xmlAttributePtr attrDecl;
2673
465k
        const xmlChar *aprefix;
2674
2675
465k
        if (elem == NULL) return(0);
2676
465k
        aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
2677
465k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, attr->name,
2678
465k
                                      aprefix);
2679
465k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
2680
12.8k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, attr->name,
2681
12.8k
                                          aprefix);
2682
2683
465k
  if ((attrDecl != NULL) &&
2684
316k
      (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2685
281k
       attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2686
100k
  return(1);
2687
465k
    }
2688
364k
    return(0);
2689
1.37M
}
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.40k
xmlRemoveRef(xmlDoc *doc, xmlAttr *attr) {
2703
2.40k
    xmlListPtr ref_list;
2704
2.40k
    xmlRefTablePtr table;
2705
2.40k
    xmlChar *ID;
2706
2.40k
    xmlRemoveMemo target;
2707
2708
2.40k
    if (doc == NULL) return(-1);
2709
1.63k
    if (attr == NULL) return(-1);
2710
2711
1.28k
    table = (xmlRefTablePtr) doc->refs;
2712
1.28k
    if (table == NULL)
2713
336
        return(-1);
2714
2715
950
    ID = xmlNodeListGetString(doc, attr->children, 1);
2716
950
    if (ID == NULL)
2717
199
        return(-1);
2718
2719
751
    ref_list = xmlHashLookup(table, ID);
2720
751
    if(ref_list == NULL) {
2721
201
        xmlFree(ID);
2722
201
        return (-1);
2723
201
    }
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
550
    target.l = ref_list;
2736
550
    target.ap = attr;
2737
2738
    /* Remove the supplied attr from our list */
2739
550
    xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
2740
2741
    /*If the list is empty then remove the list entry in the hash */
2742
550
    if (xmlListEmpty(ref_list))
2743
231
        xmlHashRemoveEntry(table, ID, xmlFreeRefTableEntry);
2744
550
    xmlFree(ID);
2745
550
    return(0);
2746
751
}
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.19k
xmlGetRefs(xmlDoc *doc, const xmlChar *ID) {
2760
1.19k
    xmlRefTablePtr table;
2761
2762
1.19k
    if (doc == NULL) {
2763
334
        return(NULL);
2764
334
    }
2765
2766
857
    if (ID == NULL) {
2767
250
        return(NULL);
2768
250
    }
2769
2770
607
    table = (xmlRefTablePtr) doc->refs;
2771
607
    if (table == NULL)
2772
410
        return(NULL);
2773
2774
197
    return (xmlHashLookup(table, ID));
2775
607
}
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
5.90k
xmlGetDtdElementDesc(xmlDtd *dtd, const xmlChar *name) {
2795
5.90k
    xmlElementTablePtr table;
2796
5.90k
    xmlElementPtr cur;
2797
5.90k
    const xmlChar *localname;
2798
5.90k
    xmlChar *prefix;
2799
2800
5.90k
    if ((dtd == NULL) || (dtd->elements == NULL) ||
2801
954
        (name == NULL))
2802
5.14k
        return(NULL);
2803
2804
759
    table = (xmlElementTablePtr) dtd->elements;
2805
759
    if (table == NULL)
2806
0
  return(NULL);
2807
2808
759
    localname = xmlSplitQName4(name, &prefix);
2809
759
    if (localname == NULL)
2810
1
        return(NULL);
2811
758
    cur = xmlHashLookup2(table, localname, prefix);
2812
758
    if (prefix != NULL)
2813
366
        xmlFree(prefix);
2814
758
    return(cur);
2815
759
}
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
299k
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name) {
2828
299k
    xmlElementTablePtr table;
2829
299k
    xmlElementPtr cur = NULL;
2830
299k
    const xmlChar *localName;
2831
299k
    xmlChar *prefix = NULL;
2832
2833
299k
    if (dtd == NULL) return(NULL);
2834
2835
    /*
2836
     * Create the Element table if needed.
2837
     */
2838
299k
    if (dtd->elements == NULL) {
2839
45.2k
  xmlDictPtr dict = NULL;
2840
2841
45.2k
  if (dtd->doc != NULL)
2842
45.0k
      dict = dtd->doc->dict;
2843
2844
45.2k
  dtd->elements = xmlHashCreateDict(0, dict);
2845
45.2k
  if (dtd->elements == NULL)
2846
36
            goto mem_error;
2847
45.2k
    }
2848
299k
    table = (xmlElementTablePtr) dtd->elements;
2849
2850
299k
    localName = xmlSplitQName4(name, &prefix);
2851
299k
    if (localName == NULL)
2852
16
        goto mem_error;
2853
299k
    cur = xmlHashLookup2(table, localName, prefix);
2854
299k
    if (cur == NULL) {
2855
53.5k
  cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2856
53.5k
  if (cur == NULL)
2857
27
            goto mem_error;
2858
53.5k
  memset(cur, 0, sizeof(xmlElement));
2859
53.5k
  cur->type = XML_ELEMENT_DECL;
2860
53.5k
        cur->doc = dtd->doc;
2861
2862
  /*
2863
   * fill the structure.
2864
   */
2865
53.5k
  cur->name = xmlStrdup(localName);
2866
53.5k
        if (cur->name == NULL)
2867
41
            goto mem_error;
2868
53.5k
  cur->prefix = prefix;
2869
53.5k
        prefix = NULL;
2870
53.5k
  cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2871
2872
53.5k
  if (xmlHashAdd2(table, localName, cur->prefix, cur) <= 0)
2873
60
            goto mem_error;
2874
53.5k
    }
2875
2876
299k
    if (prefix != NULL)
2877
7.65k
        xmlFree(prefix);
2878
299k
    return(cur);
2879
2880
180
mem_error:
2881
180
    xmlVErrMemory(ctxt);
2882
180
    xmlFree(prefix);
2883
180
    xmlFreeElement(cur);
2884
180
    return(NULL);
2885
299k
}
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
1.37M
                const xmlChar *prefix) {
2899
1.37M
    xmlElementTablePtr table;
2900
2901
1.37M
    if (dtd == NULL) return(NULL);
2902
942k
    if (dtd->elements == NULL) return(NULL);
2903
597k
    table = (xmlElementTablePtr) dtd->elements;
2904
2905
597k
    return(xmlHashLookup2(table, name, prefix));
2906
942k
}
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.44k
xmlGetDtdAttrDesc(xmlDtd *dtd, const xmlChar *elem, const xmlChar *name) {
2920
3.44k
    xmlAttributeTablePtr table;
2921
3.44k
    xmlAttributePtr cur;
2922
3.44k
    const xmlChar *localname;
2923
3.44k
    xmlChar *prefix = NULL;
2924
2925
3.44k
    if ((dtd == NULL) || (dtd->attributes == NULL) ||
2926
1.84k
        (elem == NULL) || (name == NULL))
2927
1.86k
        return(NULL);
2928
2929
1.58k
    table = (xmlAttributeTablePtr) dtd->attributes;
2930
1.58k
    if (table == NULL)
2931
0
  return(NULL);
2932
2933
1.58k
    localname = xmlSplitQName4(name, &prefix);
2934
1.58k
    if (localname == NULL)
2935
1
        return(NULL);
2936
1.58k
    cur = xmlHashLookup3(table, localname, prefix, elem);
2937
1.58k
    if (prefix != NULL)
2938
213
        xmlFree(prefix);
2939
1.58k
    return(cur);
2940
1.58k
}
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
2.24M
            const xmlChar *prefix) {
2956
2.24M
    xmlAttributeTablePtr table;
2957
2958
2.24M
    if (dtd == NULL) return(NULL);
2959
2.09M
    if (dtd->attributes == NULL) return(NULL);
2960
1.60M
    table = (xmlAttributeTablePtr) dtd->attributes;
2961
2962
1.60M
    return(xmlHashLookup3(table, name, prefix, elem));
2963
2.09M
}
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
28.1k
xmlGetDtdNotationDesc(xmlDtd *dtd, const xmlChar *name) {
2975
28.1k
    xmlNotationTablePtr table;
2976
2977
28.1k
    if (dtd == NULL) return(NULL);
2978
21.0k
    if (dtd->notations == NULL) return(NULL);
2979
7.26k
    table = (xmlNotationTablePtr) dtd->notations;
2980
2981
7.26k
    return(xmlHashLookup(table, name));
2982
21.0k
}
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.58k
                       const xmlChar *notationName) {
3000
1.58k
    xmlNotationPtr notaDecl;
3001
1.58k
    if ((doc == NULL) || (doc->intSubset == NULL) ||
3002
1.39k
        (notationName == NULL)) return(-1);
3003
3004
1.39k
    notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3005
1.39k
    if ((notaDecl == NULL) && (doc->extSubset != NULL))
3006
476
  notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3007
3008
1.39k
    if (notaDecl == NULL) {
3009
1.12k
  xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3010
1.12k
                  "NOTATION %s is not declared\n",
3011
1.12k
            notationName, NULL, NULL);
3012
1.12k
  return(0);
3013
1.12k
    }
3014
262
    return(1);
3015
1.39k
}
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
11.4k
xmlValidNormalizeString(xmlChar *str) {
3066
11.4k
    xmlChar *dst;
3067
11.4k
    const xmlChar *src;
3068
3069
11.4k
    if (str == NULL)
3070
0
        return;
3071
11.4k
    src = str;
3072
11.4k
    dst = str;
3073
3074
12.7k
    while (*src == 0x20) src++;
3075
168k
    while (*src != 0) {
3076
157k
  if (*src == 0x20) {
3077
15.3k
      while (*src == 0x20) src++;
3078
7.05k
      if (*src != 0)
3079
6.12k
    *dst++ = 0x20;
3080
150k
  } else {
3081
150k
      *dst++ = *src++;
3082
150k
  }
3083
157k
    }
3084
11.4k
    *dst = 0;
3085
11.4k
}
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
195k
xmlValidateNameValueInternal(const xmlChar *value, int flags) {
3097
195k
    if ((value == NULL) || (value[0] == 0))
3098
85.3k
        return(0);
3099
3100
110k
    value = xmlScanName(value, SIZE_MAX, flags);
3101
110k
    return((value != NULL) && (*value == 0));
3102
195k
}
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
26.7k
xmlValidateNamesValueInternal(const xmlChar *value, int flags) {
3126
26.7k
    const xmlChar *cur;
3127
3128
26.7k
    if (value == NULL)
3129
11
        return(0);
3130
3131
26.7k
    cur = xmlScanName(value, SIZE_MAX, flags);
3132
26.7k
    if ((cur == NULL) || (cur == value))
3133
6.96k
        return(0);
3134
3135
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3136
21.7k
    while (*cur == 0x20) {
3137
5.86k
  while (*cur == 0x20)
3138
3.07k
      cur += 1;
3139
3140
2.78k
        value = cur;
3141
2.78k
        cur = xmlScanName(value, SIZE_MAX, flags);
3142
2.78k
        if ((cur == NULL) || (cur == value))
3143
820
            return(0);
3144
2.78k
    }
3145
3146
18.9k
    return(*cur == 0);
3147
19.7k
}
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
4.94k
xmlValidateNmtokenValueInternal(const xmlChar *value, int flags) {
3173
4.94k
    if ((value == NULL) || (value[0] == 0))
3174
790
        return(0);
3175
3176
4.15k
    value = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN);
3177
4.15k
    return((value != NULL) && (*value == 0));
3178
4.94k
}
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
121k
xmlValidateNmtokensValueInternal(const xmlChar *value, int flags) {
3206
121k
    const xmlChar *cur;
3207
3208
121k
    if (value == NULL)
3209
5
        return(0);
3210
3211
121k
    cur = value;
3212
121k
    while (IS_BLANK_CH(*cur))
3213
1.53k
  cur += 1;
3214
3215
121k
    value = cur;
3216
121k
    cur = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN);
3217
121k
    if ((cur == NULL) || (cur == value))
3218
8.89k
        return(0);
3219
3220
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3221
114k
    while (*cur == 0x20) {
3222
5.62k
  while (*cur == 0x20)
3223
2.86k
      cur += 1;
3224
2.76k
        if (*cur == 0)
3225
475
            return(1);
3226
3227
2.29k
        value = cur;
3228
2.29k
        cur = xmlScanName(value, SIZE_MAX, flags | XML_SCAN_NMTOKEN);
3229
2.29k
        if ((cur == NULL) || (cur == value))
3230
989
            return(0);
3231
2.29k
    }
3232
3233
111k
    return(*cur == 0);
3234
112k
}
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
2.00k
                         xmlNotation *nota ATTRIBUTE_UNUSED) {
3267
2.00k
    int ret = 1;
3268
3269
2.00k
    return(ret);
3270
2.00k
}
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
370k
                                  const xmlChar *value) {
3284
370k
    int flags = 0;
3285
3286
370k
    if ((doc != NULL) && (doc->properties & XML_DOC_OLD10))
3287
129k
        flags |= XML_SCAN_OLD10;
3288
3289
370k
    switch (type) {
3290
7.89k
  case XML_ATTRIBUTE_ENTITIES:
3291
26.7k
  case XML_ATTRIBUTE_IDREFS:
3292
26.7k
      return(xmlValidateNamesValueInternal(value, flags));
3293
24.8k
  case XML_ATTRIBUTE_ENTITY:
3294
37.0k
  case XML_ATTRIBUTE_IDREF:
3295
187k
  case XML_ATTRIBUTE_ID:
3296
195k
  case XML_ATTRIBUTE_NOTATION:
3297
195k
      return(xmlValidateNameValueInternal(value, flags));
3298
3.74k
  case XML_ATTRIBUTE_NMTOKENS:
3299
121k
  case XML_ATTRIBUTE_ENUMERATION:
3300
121k
      return(xmlValidateNmtokensValueInternal(value, flags));
3301
4.94k
  case XML_ATTRIBUTE_NMTOKEN:
3302
4.94k
      return(xmlValidateNmtokenValueInternal(value, flags));
3303
19.3k
        case XML_ATTRIBUTE_CDATA:
3304
19.3k
      break;
3305
370k
    }
3306
21.4k
    return(1);
3307
370k
}
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
291k
      const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3368
291k
    int ret = 1;
3369
291k
    switch (type) {
3370
8.75k
  case XML_ATTRIBUTE_IDREFS:
3371
18.4k
  case XML_ATTRIBUTE_IDREF:
3372
150k
  case XML_ATTRIBUTE_ID:
3373
151k
  case XML_ATTRIBUTE_NMTOKENS:
3374
245k
  case XML_ATTRIBUTE_ENUMERATION:
3375
246k
  case XML_ATTRIBUTE_NMTOKEN:
3376
256k
        case XML_ATTRIBUTE_CDATA:
3377
256k
      break;
3378
20.1k
  case XML_ATTRIBUTE_ENTITY: {
3379
20.1k
      xmlEntityPtr ent;
3380
3381
20.1k
      ent = xmlGetDocEntity(doc, value);
3382
      /* yeah it's a bit messy... */
3383
20.1k
      if ((ent == NULL) && (doc->standalone == 1)) {
3384
329
    doc->standalone = 0;
3385
329
    ent = xmlGetDocEntity(doc, value);
3386
329
      }
3387
20.1k
      if (ent == NULL) {
3388
18.9k
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3389
18.9k
        XML_DTD_UNKNOWN_ENTITY,
3390
18.9k
   "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3391
18.9k
           name, value, NULL);
3392
18.9k
    ret = 0;
3393
18.9k
      } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3394
532
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3395
532
        XML_DTD_ENTITY_TYPE,
3396
532
   "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3397
532
           name, value, NULL);
3398
532
    ret = 0;
3399
532
      }
3400
20.1k
      break;
3401
246k
        }
3402
5.18k
  case XML_ATTRIBUTE_ENTITIES: {
3403
5.18k
      xmlChar *dup, *nam = NULL, *cur, save;
3404
5.18k
      xmlEntityPtr ent;
3405
3406
5.18k
      dup = xmlStrdup(value);
3407
5.18k
      if (dup == NULL) {
3408
17
                xmlVErrMemory(ctxt);
3409
17
    return(0);
3410
17
            }
3411
5.16k
      cur = dup;
3412
11.3k
      while (*cur != 0) {
3413
9.43k
    nam = cur;
3414
121k
    while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
3415
9.43k
    save = *cur;
3416
9.43k
    *cur = 0;
3417
9.43k
    ent = xmlGetDocEntity(doc, nam);
3418
9.43k
    if (ent == NULL) {
3419
8.04k
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3420
8.04k
            XML_DTD_UNKNOWN_ENTITY,
3421
8.04k
       "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3422
8.04k
         name, nam, NULL);
3423
8.04k
        ret = 0;
3424
8.04k
    } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3425
673
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3426
673
            XML_DTD_ENTITY_TYPE,
3427
673
       "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3428
673
         name, nam, NULL);
3429
673
        ret = 0;
3430
673
    }
3431
9.43k
    if (save == 0)
3432
3.29k
        break;
3433
6.14k
    *cur = save;
3434
6.44k
    while (IS_BLANK_CH(*cur)) cur++;
3435
6.14k
      }
3436
5.16k
      xmlFree(dup);
3437
5.16k
      break;
3438
5.18k
  }
3439
7.54k
  case XML_ATTRIBUTE_NOTATION: {
3440
7.54k
      xmlNotationPtr nota;
3441
3442
7.54k
      nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3443
7.54k
      if ((nota == NULL) && (doc->extSubset != NULL))
3444
2.87k
    nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3445
3446
7.54k
      if (nota == NULL) {
3447
5.64k
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3448
5.64k
                    XML_DTD_UNKNOWN_NOTATION,
3449
5.64k
       "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3450
5.64k
           name, value, NULL);
3451
5.64k
    ret = 0;
3452
5.64k
      }
3453
7.54k
      break;
3454
5.18k
        }
3455
291k
    }
3456
290k
    return(ret);
3457
291k
}
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
16.8k
       xmlNode *elem, const xmlChar *name, const xmlChar *value) {
3485
16.8k
    xmlChar *ret;
3486
16.8k
    xmlAttributePtr attrDecl = NULL;
3487
16.8k
    const xmlChar *localName;
3488
16.8k
    xmlChar *prefix = NULL;
3489
16.8k
    int extsubset = 0;
3490
3491
16.8k
    if (doc == NULL) return(NULL);
3492
16.8k
    if (elem == NULL) return(NULL);
3493
16.8k
    if (name == NULL) return(NULL);
3494
16.8k
    if (value == NULL) return(NULL);
3495
3496
16.8k
    localName = xmlSplitQName4(name, &prefix);
3497
16.8k
    if (localName == NULL)
3498
13
        goto mem_error;
3499
3500
16.8k
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3501
2.39k
  xmlChar buf[50];
3502
2.39k
  xmlChar *elemname;
3503
3504
2.39k
  elemname = xmlBuildQName(elem->name, elem->ns->prefix, buf, 50);
3505
2.39k
  if (elemname == NULL)
3506
8
      goto mem_error;
3507
2.38k
        if (doc->intSubset != NULL)
3508
2.38k
            attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName,
3509
2.38k
                                      prefix, elemname);
3510
2.38k
  if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3511
598
      attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName,
3512
598
                                      prefix, elemname);
3513
598
      if (attrDecl != NULL)
3514
68
    extsubset = 1;
3515
598
  }
3516
2.38k
  if ((elemname != buf) && (elemname != elem->name))
3517
1.25k
      xmlFree(elemname);
3518
2.38k
    }
3519
16.8k
    if ((attrDecl == NULL) && (doc->intSubset != NULL))
3520
16.0k
  attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName,
3521
16.0k
                                  prefix, elem->name);
3522
16.8k
    if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3523
3.20k
  attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName,
3524
3.20k
                                  prefix, elem->name);
3525
3.20k
  if (attrDecl != NULL)
3526
2.26k
      extsubset = 1;
3527
3.20k
    }
3528
3529
16.8k
    if (attrDecl == NULL)
3530
3.98k
  goto done;
3531
12.8k
    if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3532
1.36k
  goto done;
3533
3534
11.4k
    ret = xmlStrdup(value);
3535
11.4k
    if (ret == NULL)
3536
32
  goto mem_error;
3537
11.4k
    xmlValidNormalizeString(ret);
3538
11.4k
    if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3539
499
  xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
3540
499
"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3541
499
         name, elem->name, NULL);
3542
499
  ctxt->valid = 0;
3543
499
    }
3544
3545
11.4k
    xmlFree(prefix);
3546
11.4k
    return(ret);
3547
3548
53
mem_error:
3549
53
    xmlVErrMemory(ctxt);
3550
3551
5.40k
done:
3552
5.40k
    xmlFree(prefix);
3553
5.40k
    return(NULL);
3554
53
}
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
1.08k
                         const xmlChar *name ATTRIBUTE_UNUSED) {
3615
1.08k
    xmlAttributePtr attr = (xmlAttributePtr) payload;
3616
1.08k
    int *count = (int *) data;
3617
1.08k
    if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3618
1.08k
}
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
59.3k
                         xmlAttribute *attr) {
3642
59.3k
    int ret = 1;
3643
59.3k
    int val;
3644
59.3k
    CHECK_DTD;
3645
59.1k
    if(attr == NULL) return(1);
3646
3647
    /* Attribute Default Legal */
3648
    /* Enumeration */
3649
59.1k
    if (attr->defaultValue != NULL) {
3650
20.8k
  val = xmlValidateAttributeValueInternal(doc, attr->atype,
3651
20.8k
                                          attr->defaultValue);
3652
20.8k
  if (val == 0) {
3653
1.20k
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
3654
1.20k
         "Syntax of default value for attribute %s of %s is not valid\n",
3655
1.20k
             attr->name, attr->elem, NULL);
3656
1.20k
  }
3657
20.8k
        ret &= val;
3658
20.8k
    }
3659
3660
    /* ID Attribute Default */
3661
59.1k
    if ((attr->atype == XML_ATTRIBUTE_ID)&&
3662
15.6k
        (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3663
7.54k
  (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3664
6.72k
  xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
3665
6.72k
          "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
3666
6.72k
         attr->name, attr->elem, NULL);
3667
6.72k
  ret = 0;
3668
6.72k
    }
3669
3670
    /* One ID per Element Type */
3671
59.1k
    if (attr->atype == XML_ATTRIBUTE_ID) {
3672
15.6k
        xmlElementPtr elem = NULL;
3673
15.6k
        const xmlChar *elemLocalName;
3674
15.6k
        xmlChar *elemPrefix;
3675
15.6k
        int nbId;
3676
3677
15.6k
        elemLocalName = xmlSplitQName4(attr->elem, &elemPrefix);
3678
15.6k
        if (elemLocalName == NULL) {
3679
6
            xmlVErrMemory(ctxt);
3680
6
            return(0);
3681
6
        }
3682
3683
  /* the trick is that we parse DtD as their own internal subset */
3684
15.5k
        if (doc->intSubset != NULL)
3685
15.1k
            elem = xmlHashLookup2(doc->intSubset->elements,
3686
15.1k
                                  elemLocalName, elemPrefix);
3687
15.5k
  if (elem != NULL) {
3688
9.75k
      nbId = xmlScanIDAttributeDecl(ctxt, elem, 0);
3689
9.75k
  } else {
3690
5.84k
      xmlAttributeTablePtr table;
3691
3692
      /*
3693
       * The attribute may be declared in the internal subset and the
3694
       * element in the external subset.
3695
       */
3696
5.84k
      nbId = 0;
3697
5.84k
      if (doc->intSubset != NULL) {
3698
5.42k
    table = (xmlAttributeTablePtr) doc->intSubset->attributes;
3699
5.42k
    xmlHashScan3(table, NULL, NULL, attr->elem,
3700
5.42k
           xmlValidateAttributeIdCallback, &nbId);
3701
5.42k
      }
3702
5.84k
  }
3703
15.5k
  if (nbId > 1) {
3704
3705
1.08k
      xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
3706
1.08k
       "Element %s has %d ID attribute defined in the internal subset : %s\n",
3707
1.08k
       attr->elem, nbId, attr->name);
3708
1.08k
            ret = 0;
3709
14.5k
  } else if (doc->extSubset != NULL) {
3710
7.11k
      int extId = 0;
3711
7.11k
      elem = xmlHashLookup2(doc->extSubset->elements,
3712
7.11k
                                  elemLocalName, elemPrefix);
3713
7.11k
      if (elem != NULL) {
3714
6.91k
    extId = xmlScanIDAttributeDecl(ctxt, elem, 0);
3715
6.91k
      }
3716
7.11k
      if (extId > 1) {
3717
451
    xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
3718
451
       "Element %s has %d ID attribute defined in the external subset : %s\n",
3719
451
           attr->elem, extId, attr->name);
3720
451
                ret = 0;
3721
6.66k
      } else if (extId + nbId > 1) {
3722
503
    xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
3723
503
"Element %s has ID attributes defined in the internal and external subset : %s\n",
3724
503
           attr->elem, attr->name, NULL);
3725
503
                ret = 0;
3726
503
      }
3727
7.11k
  }
3728
3729
15.5k
        xmlFree(elemPrefix);
3730
15.5k
    }
3731
3732
    /* Validity Constraint: Enumeration */
3733
59.1k
    if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
3734
9.00k
        xmlEnumerationPtr tree = attr->tree;
3735
11.5k
  while (tree != NULL) {
3736
10.3k
      if (xmlStrEqual(tree->name, attr->defaultValue)) break;
3737
2.54k
      tree = tree->next;
3738
2.54k
  }
3739
9.00k
  if (tree == NULL) {
3740
1.23k
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
3741
1.23k
"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
3742
1.23k
       attr->defaultValue, attr->name, attr->elem);
3743
1.23k
      ret = 0;
3744
1.23k
  }
3745
9.00k
    }
3746
3747
59.1k
    return(ret);
3748
59.1k
}
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
31.7k
                       xmlElement *elem) {
3770
31.7k
    int ret = 1;
3771
31.7k
    xmlElementPtr tst;
3772
31.7k
    const xmlChar *localName;
3773
31.7k
    xmlChar *prefix;
3774
3775
31.7k
    CHECK_DTD;
3776
3777
31.5k
    if (elem == NULL) return(1);
3778
3779
    /* No Duplicate Types */
3780
29.7k
    if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
3781
8.47k
  xmlElementContentPtr cur, next;
3782
8.47k
        const xmlChar *name;
3783
3784
8.47k
  cur = elem->content;
3785
44.8k
  while (cur != NULL) {
3786
44.8k
      if (cur->type != XML_ELEMENT_CONTENT_OR) break;
3787
36.3k
      if (cur->c1 == NULL) break;
3788
36.3k
      if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
3789
32.3k
    name = cur->c1->name;
3790
32.3k
    next = cur->c2;
3791
876k
    while (next != NULL) {
3792
876k
        if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
3793
32.3k
            if ((xmlStrEqual(next->name, name)) &&
3794
3.33k
          (xmlStrEqual(next->prefix, cur->c1->prefix))) {
3795
2.08k
          if (cur->c1->prefix == NULL) {
3796
1.54k
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
3797
1.54k
       "Definition of %s has duplicate references of %s\n",
3798
1.54k
               elem->name, name, NULL);
3799
1.54k
          } else {
3800
540
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
3801
540
       "Definition of %s has duplicate references of %s:%s\n",
3802
540
               elem->name, cur->c1->prefix, name);
3803
540
          }
3804
2.08k
          ret = 0;
3805
2.08k
      }
3806
32.3k
      break;
3807
32.3k
        }
3808
844k
        if (next->c1 == NULL) break;
3809
844k
        if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
3810
844k
        if ((xmlStrEqual(next->c1->name, name)) &&
3811
257k
            (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
3812
168k
      if (cur->c1->prefix == NULL) {
3813
144k
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
3814
144k
         "Definition of %s has duplicate references to %s\n",
3815
144k
           elem->name, name, NULL);
3816
144k
      } else {
3817
24.1k
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
3818
24.1k
         "Definition of %s has duplicate references to %s:%s\n",
3819
24.1k
           elem->name, cur->c1->prefix, name);
3820
24.1k
      }
3821
168k
      ret = 0;
3822
168k
        }
3823
844k
        next = next->c2;
3824
844k
    }
3825
32.3k
      }
3826
36.3k
      cur = cur->c2;
3827
36.3k
  }
3828
8.47k
    }
3829
3830
29.7k
    localName = xmlSplitQName4(elem->name, &prefix);
3831
29.7k
    if (localName == NULL) {
3832
11
        xmlVErrMemory(ctxt);
3833
11
        return(0);
3834
11
    }
3835
3836
    /* VC: Unique Element Type Declaration */
3837
29.7k
    if (doc->intSubset != NULL) {
3838
29.5k
        tst = xmlHashLookup2(doc->intSubset->elements, localName, prefix);
3839
3840
29.5k
        if ((tst != NULL ) && (tst != elem) &&
3841
2.13k
            ((tst->prefix == elem->prefix) ||
3842
415
             (xmlStrEqual(tst->prefix, elem->prefix))) &&
3843
1.92k
            (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
3844
1.72k
            xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3845
1.72k
                            "Redefinition of element %s\n",
3846
1.72k
                           elem->name, NULL, NULL);
3847
1.72k
            ret = 0;
3848
1.72k
        }
3849
29.5k
    }
3850
29.7k
    if (doc->extSubset != NULL) {
3851
7.03k
        tst = xmlHashLookup2(doc->extSubset->elements, localName, prefix);
3852
3853
7.03k
        if ((tst != NULL ) && (tst != elem) &&
3854
599
            ((tst->prefix == elem->prefix) ||
3855
210
             (xmlStrEqual(tst->prefix, elem->prefix))) &&
3856
397
            (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
3857
199
            xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
3858
199
                            "Redefinition of element %s\n",
3859
199
                           elem->name, NULL, NULL);
3860
199
            ret = 0;
3861
199
        }
3862
7.03k
    }
3863
3864
29.7k
    xmlFree(prefix);
3865
29.7k
    return(ret);
3866
29.7k
}
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
259k
{
3898
259k
    xmlAttributePtr attrDecl =  NULL;
3899
259k
    const xmlChar *aprefix;
3900
259k
    int val;
3901
259k
    int ret = 1;
3902
3903
259k
    CHECK_DTD;
3904
259k
    if ((elem == NULL) || (elem->name == NULL)) return(0);
3905
258k
    if ((attr == NULL) || (attr->name == NULL)) return(0);
3906
3907
258k
    aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
3908
3909
258k
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3910
8.73k
  xmlChar fn[50];
3911
8.73k
  xmlChar *fullname;
3912
3913
8.73k
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3914
8.73k
  if (fullname == NULL) {
3915
14
            xmlVErrMemory(ctxt);
3916
14
      return(0);
3917
14
        }
3918
8.71k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
3919
8.71k
                                      attr->name, aprefix);
3920
8.71k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
3921
3.27k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
3922
3.27k
                                          attr->name, aprefix);
3923
8.71k
  if ((fullname != fn) && (fullname != elem->name))
3924
4.21k
      xmlFree(fullname);
3925
8.71k
    }
3926
258k
    if (attrDecl == NULL) {
3927
256k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
3928
256k
                                      attr->name, aprefix);
3929
256k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
3930
80.9k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
3931
80.9k
                                          attr->name, aprefix);
3932
256k
    }
3933
3934
3935
    /* Validity Constraint: Attribute Value Type */
3936
258k
    if (attrDecl == NULL) {
3937
104k
  xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
3938
104k
         "No declaration for attribute %s of element %s\n",
3939
104k
         attr->name, elem->name, NULL);
3940
104k
  return(0);
3941
104k
    }
3942
153k
    if (attr->id != NULL)
3943
196
        xmlRemoveID(doc, attr);
3944
153k
    attr->atype = attrDecl->atype;
3945
3946
153k
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
3947
153k
    if (val == 0) {
3948
34.5k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
3949
34.5k
     "Syntax of value for attribute %s of %s is not valid\n",
3950
34.5k
         attr->name, elem->name, NULL);
3951
34.5k
        ret = 0;
3952
34.5k
    }
3953
3954
    /* Validity constraint: Fixed Attribute Default */
3955
153k
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
3956
8.29k
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
3957
2.33k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
3958
2.33k
     "Value for attribute %s of %s is different from default \"%s\"\n",
3959
2.33k
       attr->name, elem->name, attrDecl->defaultValue);
3960
2.33k
      ret = 0;
3961
2.33k
  }
3962
8.29k
    }
3963
3964
    /* Validity Constraint: ID uniqueness */
3965
153k
    if (attrDecl->atype == XML_ATTRIBUTE_ID &&
3966
29.4k
        (ctxt == NULL || (ctxt->flags & XML_VCTXT_IN_ENTITY) == 0)) {
3967
29.0k
        if (xmlAddID(ctxt, doc, value, attr) == NULL)
3968
24.6k
      ret = 0;
3969
29.0k
    }
3970
3971
153k
    if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
3972
144k
  (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
3973
17.3k
        if (xmlAddRef(ctxt, doc, value, attr) == NULL)
3974
58
      ret = 0;
3975
17.3k
    }
3976
3977
    /* Validity Constraint: Notation Attributes */
3978
153k
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
3979
4.19k
        xmlEnumerationPtr tree = attrDecl->tree;
3980
4.19k
        xmlNotationPtr nota;
3981
3982
        /* First check that the given NOTATION was declared */
3983
4.19k
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3984
4.19k
  if (nota == NULL)
3985
3.58k
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3986
3987
4.19k
  if (nota == NULL) {
3988
3.06k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
3989
3.06k
       "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
3990
3.06k
       value, attr->name, elem->name);
3991
3.06k
      ret = 0;
3992
3.06k
        }
3993
3994
  /* Second, verify that it's among the list */
3995
8.29k
  while (tree != NULL) {
3996
5.02k
      if (xmlStrEqual(tree->name, value)) break;
3997
4.09k
      tree = tree->next;
3998
4.09k
  }
3999
4.19k
  if (tree == NULL) {
4000
3.27k
      xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4001
3.27k
"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
4002
3.27k
       value, attr->name, elem->name);
4003
3.27k
      ret = 0;
4004
3.27k
  }
4005
4.19k
    }
4006
4007
    /* Validity Constraint: Enumeration */
4008
153k
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4009
86.5k
        xmlEnumerationPtr tree = attrDecl->tree;
4010
105k
  while (tree != NULL) {
4011
86.8k
      if (xmlStrEqual(tree->name, value)) break;
4012
19.0k
      tree = tree->next;
4013
19.0k
  }
4014
86.5k
  if (tree == NULL) {
4015
18.6k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4016
18.6k
       "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4017
18.6k
       value, attr->name, elem->name);
4018
18.6k
      ret = 0;
4019
18.6k
  }
4020
86.5k
    }
4021
4022
    /* Fixed Attribute Default */
4023
153k
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4024
8.29k
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4025
2.33k
  xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4026
2.33k
     "Value for attribute %s of %s must be \"%s\"\n",
4027
2.33k
         attr->name, elem->name, attrDecl->defaultValue);
4028
2.33k
        ret = 0;
4029
2.33k
    }
4030
4031
    /* Extra check for the attribute value */
4032
153k
    ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4033
153k
              attrDecl->atype, value);
4034
4035
153k
    return(ret);
4036
258k
}
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
253k
xmlNode *elem, const xmlChar *prefix, xmlNs *ns, const xmlChar *value) {
4068
    /* xmlElementPtr elemDecl; */
4069
253k
    xmlAttributePtr attrDecl =  NULL;
4070
253k
    int val;
4071
253k
    int ret = 1;
4072
4073
253k
    CHECK_DTD;
4074
253k
    if ((elem == NULL) || (elem->name == NULL)) return(0);
4075
252k
    if ((ns == NULL) || (ns->href == NULL)) return(0);
4076
4077
251k
    if (prefix != NULL) {
4078
40.4k
  xmlChar fn[50];
4079
40.4k
  xmlChar *fullname;
4080
4081
40.4k
  fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4082
40.4k
  if (fullname == NULL) {
4083
9
      xmlVErrMemory(ctxt);
4084
9
      return(0);
4085
9
  }
4086
40.4k
  if (ns->prefix != NULL) {
4087
33.5k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4088
33.5k
                              ns->prefix, BAD_CAST "xmlns");
4089
33.5k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4090
3.85k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4091
3.85k
            ns->prefix, BAD_CAST "xmlns");
4092
33.5k
  } else {
4093
6.81k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4094
6.81k
                                          BAD_CAST "xmlns", NULL);
4095
6.81k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4096
4.27k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4097
4.27k
                                              BAD_CAST "xmlns", NULL);
4098
6.81k
  }
4099
40.4k
  if ((fullname != fn) && (fullname != elem->name))
4100
30.7k
      xmlFree(fullname);
4101
40.4k
    }
4102
251k
    if (attrDecl == NULL) {
4103
250k
  if (ns->prefix != NULL) {
4104
222k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4105
222k
                              ns->prefix, BAD_CAST "xmlns");
4106
222k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4107
65.4k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4108
65.4k
                ns->prefix, BAD_CAST "xmlns");
4109
222k
  } else {
4110
27.9k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4111
27.9k
                                          BAD_CAST "xmlns", NULL);
4112
27.9k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4113
8.22k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4114
8.22k
                                              BAD_CAST "xmlns", NULL);
4115
27.9k
  }
4116
250k
    }
4117
4118
4119
    /* Validity Constraint: Attribute Value Type */
4120
251k
    if (attrDecl == NULL) {
4121
114k
  if (ns->prefix != NULL) {
4122
98.2k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4123
98.2k
       "No declaration for attribute xmlns:%s of element %s\n",
4124
98.2k
       ns->prefix, elem->name, NULL);
4125
98.2k
  } else {
4126
15.9k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4127
15.9k
       "No declaration for attribute xmlns of element %s\n",
4128
15.9k
       elem->name, NULL, NULL);
4129
15.9k
  }
4130
114k
  return(0);
4131
114k
    }
4132
4133
137k
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4134
137k
    if (val == 0) {
4135
99.3k
  if (ns->prefix != NULL) {
4136
96.8k
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4137
96.8k
         "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4138
96.8k
       ns->prefix, elem->name, NULL);
4139
96.8k
  } else {
4140
2.50k
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4141
2.50k
         "Syntax of value for attribute xmlns of %s is not valid\n",
4142
2.50k
       elem->name, NULL, NULL);
4143
2.50k
  }
4144
99.3k
        ret = 0;
4145
99.3k
    }
4146
4147
    /* Validity constraint: Fixed Attribute Default */
4148
137k
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4149
25.7k
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4150
1.54k
      if (ns->prefix != NULL) {
4151
729
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4152
729
       "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4153
729
           ns->prefix, elem->name, attrDecl->defaultValue);
4154
815
      } else {
4155
815
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4156
815
       "Value for attribute xmlns of %s is different from default \"%s\"\n",
4157
815
           elem->name, attrDecl->defaultValue, NULL);
4158
815
      }
4159
1.54k
      ret = 0;
4160
1.54k
  }
4161
25.7k
    }
4162
4163
    /* Validity Constraint: Notation Attributes */
4164
137k
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4165
3.34k
        xmlEnumerationPtr tree = attrDecl->tree;
4166
3.34k
        xmlNotationPtr nota;
4167
4168
        /* First check that the given NOTATION was declared */
4169
3.34k
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4170
3.34k
  if (nota == NULL)
4171
2.99k
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4172
4173
3.34k
  if (nota == NULL) {
4174
2.58k
      if (ns->prefix != NULL) {
4175
1.45k
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4176
1.45k
       "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4177
1.45k
           value, ns->prefix, elem->name);
4178
1.45k
      } else {
4179
1.12k
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4180
1.12k
       "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4181
1.12k
           value, elem->name, NULL);
4182
1.12k
      }
4183
2.58k
      ret = 0;
4184
2.58k
        }
4185
4186
  /* Second, verify that it's among the list */
4187
7.16k
  while (tree != NULL) {
4188
4.32k
      if (xmlStrEqual(tree->name, value)) break;
4189
3.81k
      tree = tree->next;
4190
3.81k
  }
4191
3.34k
  if (tree == NULL) {
4192
2.83k
      if (ns->prefix != NULL) {
4193
1.74k
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4194
1.74k
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4195
1.74k
           value, ns->prefix, elem->name);
4196
1.74k
      } else {
4197
1.09k
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4198
1.09k
"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4199
1.09k
           value, elem->name, NULL);
4200
1.09k
      }
4201
2.83k
      ret = 0;
4202
2.83k
  }
4203
3.34k
    }
4204
4205
    /* Validity Constraint: Enumeration */
4206
137k
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4207
7.49k
        xmlEnumerationPtr tree = attrDecl->tree;
4208
14.1k
  while (tree != NULL) {
4209
8.09k
      if (xmlStrEqual(tree->name, value)) break;
4210
6.64k
      tree = tree->next;
4211
6.64k
  }
4212
7.49k
  if (tree == NULL) {
4213
6.04k
      if (ns->prefix != NULL) {
4214
3.35k
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4215
3.35k
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4216
3.35k
           value, ns->prefix, elem->name);
4217
3.35k
      } else {
4218
2.69k
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4219
2.69k
"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4220
2.69k
           value, elem->name, NULL);
4221
2.69k
      }
4222
6.04k
      ret = 0;
4223
6.04k
  }
4224
7.49k
    }
4225
4226
    /* Fixed Attribute Default */
4227
137k
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4228
25.7k
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4229
1.54k
  if (ns->prefix != NULL) {
4230
729
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4231
729
       "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4232
729
       ns->prefix, elem->name, attrDecl->defaultValue);
4233
815
  } else {
4234
815
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4235
815
       "Value for attribute xmlns of %s must be \"%s\"\n",
4236
815
       elem->name, attrDecl->defaultValue, NULL);
4237
815
  }
4238
1.54k
        ret = 0;
4239
1.54k
    }
4240
4241
    /* Extra check for the attribute value */
4242
137k
    if (ns->prefix != NULL) {
4243
124k
  ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4244
124k
            attrDecl->atype, value);
4245
124k
    } else {
4246
12.5k
  ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4247
12.5k
            attrDecl->atype, value);
4248
12.5k
    }
4249
4250
137k
    return(ret);
4251
251k
}
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
43.3k
xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
4624
43.3k
    xmlNodePtr cur;
4625
43.3k
    int len;
4626
4627
43.3k
    if (node == NULL) return;
4628
39.4k
    if (glob) strcat(buf, "(");
4629
39.4k
    cur = node;
4630
145k
    while (cur != NULL) {
4631
107k
  len = strlen(buf);
4632
107k
  if (size - len < 50) {
4633
353
      if ((size - len > 4) && (buf[len - 1] != '.'))
4634
353
    strcat(buf, " ...");
4635
353
      return;
4636
353
  }
4637
106k
        switch (cur->type) {
4638
69.9k
            case XML_ELEMENT_NODE: {
4639
69.9k
                int qnameLen = xmlStrlen(cur->name);
4640
4641
69.9k
                if ((cur->ns != NULL) && (cur->ns->prefix != NULL))
4642
5.48k
                    qnameLen += xmlStrlen(cur->ns->prefix) + 1;
4643
69.9k
                if (size - len < qnameLen + 10) {
4644
438
                    if ((size - len > 4) && (buf[len - 1] != '.'))
4645
438
                        strcat(buf, " ...");
4646
438
                    return;
4647
438
                }
4648
69.5k
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4649
5.28k
        strcat(buf, (char *) cur->ns->prefix);
4650
5.28k
        strcat(buf, ":");
4651
5.28k
    }
4652
69.5k
                if (cur->name != NULL)
4653
68.9k
              strcat(buf, (char *) cur->name);
4654
69.5k
    if (cur->next != NULL)
4655
40.4k
        strcat(buf, " ");
4656
69.5k
    break;
4657
69.9k
            }
4658
29.0k
            case XML_TEXT_NODE:
4659
29.0k
    if (xmlIsBlankNode(cur))
4660
10.4k
        break;
4661
                /* Falls through. */
4662
20.8k
            case XML_CDATA_SECTION_NODE:
4663
23.9k
            case XML_ENTITY_REF_NODE:
4664
23.9k
          strcat(buf, "CDATA");
4665
23.9k
    if (cur->next != NULL)
4666
15.8k
        strcat(buf, " ");
4667
23.9k
    break;
4668
0
            case XML_ATTRIBUTE_NODE:
4669
0
            case XML_DOCUMENT_NODE:
4670
0
      case XML_HTML_DOCUMENT_NODE:
4671
0
            case XML_DOCUMENT_TYPE_NODE:
4672
0
            case XML_DOCUMENT_FRAG_NODE:
4673
0
            case XML_NOTATION_NODE:
4674
0
      case XML_NAMESPACE_DECL:
4675
0
          strcat(buf, "???");
4676
0
    if (cur->next != NULL)
4677
0
        strcat(buf, " ");
4678
0
    break;
4679
0
            case XML_ENTITY_NODE:
4680
822
            case XML_PI_NODE:
4681
822
            case XML_DTD_NODE:
4682
2.38k
            case XML_COMMENT_NODE:
4683
2.38k
      case XML_ELEMENT_DECL:
4684
2.38k
      case XML_ATTRIBUTE_DECL:
4685
2.38k
      case XML_ENTITY_DECL:
4686
2.38k
      case XML_XINCLUDE_START:
4687
2.38k
      case XML_XINCLUDE_END:
4688
2.38k
    break;
4689
106k
  }
4690
106k
  cur = cur->next;
4691
106k
    }
4692
38.6k
    if (glob) strcat(buf, ")");
4693
38.6k
}
4694
4695
/**
4696
 * Try to validate the content model of an element
4697
 *
4698
 * @param ctxt  the validation context
4699
 * @param child  the child list
4700
 * @param elemDecl  pointer to the element declaration
4701
 * @param warn  emit the error message
4702
 * @param parent  the parent element (for error reporting)
4703
 * @returns 1 if valid or 0 if not and -1 in case of error
4704
 */
4705
4706
static int
4707
xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
4708
57.7k
       xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
4709
57.7k
    int ret = 1;
4710
#ifndef  LIBXML_REGEXP_ENABLED
4711
    xmlNodePtr repl = NULL, last = NULL, tmp;
4712
#endif
4713
57.7k
    xmlNodePtr cur;
4714
57.7k
    xmlElementContentPtr cont;
4715
57.7k
    const xmlChar *name;
4716
4717
57.7k
    if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
4718
0
  return(-1);
4719
57.7k
    cont = elemDecl->content;
4720
57.7k
    name = elemDecl->name;
4721
4722
57.7k
#ifdef LIBXML_REGEXP_ENABLED
4723
    /* Build the regexp associated to the content model */
4724
57.7k
    if (elemDecl->contModel == NULL)
4725
8.59k
  ret = xmlValidBuildContentModel(ctxt, elemDecl);
4726
57.7k
    if (elemDecl->contModel == NULL) {
4727
911
  return(-1);
4728
56.8k
    } else {
4729
56.8k
  xmlRegExecCtxtPtr exec;
4730
4731
56.8k
  if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
4732
3.04k
      return(-1);
4733
3.04k
  }
4734
53.8k
  ctxt->nodeMax = 0;
4735
53.8k
  ctxt->nodeNr = 0;
4736
53.8k
  ctxt->nodeTab = NULL;
4737
53.8k
  exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
4738
53.8k
  if (exec == NULL) {
4739
26
            xmlVErrMemory(ctxt);
4740
26
            return(-1);
4741
26
        }
4742
53.7k
        cur = child;
4743
121k
        while (cur != NULL) {
4744
80.8k
            switch (cur->type) {
4745
8.74k
                case XML_ENTITY_REF_NODE:
4746
                    /*
4747
                     * Push the current node to be able to roll back
4748
                     * and process within the entity
4749
                     */
4750
8.74k
                    if ((cur->children != NULL) &&
4751
7.84k
                        (cur->children->children != NULL)) {
4752
2.62k
                        if (nodeVPush(ctxt, cur) < 0) {
4753
9
                            ret = -1;
4754
9
                            goto fail;
4755
9
                        }
4756
2.61k
                        cur = cur->children->children;
4757
2.61k
                        continue;
4758
2.62k
                    }
4759
6.11k
                    break;
4760
22.8k
                case XML_TEXT_NODE:
4761
22.8k
                    if (xmlIsBlankNode(cur))
4762
10.6k
                        break;
4763
12.1k
                    ret = 0;
4764
12.1k
                    goto fail;
4765
658
                case XML_CDATA_SECTION_NODE:
4766
                    /* TODO */
4767
658
                    ret = 0;
4768
658
                    goto fail;
4769
47.6k
                case XML_ELEMENT_NODE:
4770
47.6k
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4771
2.97k
                        xmlChar fn[50];
4772
2.97k
                        xmlChar *fullname;
4773
4774
2.97k
                        fullname = xmlBuildQName(cur->name,
4775
2.97k
                                                 cur->ns->prefix, fn, 50);
4776
2.97k
                        if (fullname == NULL) {
4777
557
                            xmlVErrMemory(ctxt);
4778
557
                            ret = -1;
4779
557
                            goto fail;
4780
557
                        }
4781
2.41k
                        ret = xmlRegExecPushString(exec, fullname, NULL);
4782
2.41k
                        if ((fullname != fn) && (fullname != cur->name))
4783
1.11k
                            xmlFree(fullname);
4784
44.6k
                    } else {
4785
44.6k
                        ret = xmlRegExecPushString(exec, cur->name, NULL);
4786
44.6k
                    }
4787
47.0k
                    break;
4788
47.0k
                default:
4789
1.02k
                    break;
4790
80.8k
            }
4791
64.8k
            if (ret == XML_REGEXP_OUT_OF_MEMORY)
4792
523
                xmlVErrMemory(ctxt);
4793
            /*
4794
             * Switch to next element
4795
             */
4796
64.8k
            cur = cur->next;
4797
67.0k
            while (cur == NULL) {
4798
32.6k
                cur = nodeVPop(ctxt);
4799
32.6k
                if (cur == NULL)
4800
30.4k
                    break;
4801
2.19k
                cur = cur->next;
4802
2.19k
            }
4803
64.8k
        }
4804
40.4k
        ret = xmlRegExecPushString(exec, NULL, NULL);
4805
40.4k
        if (ret == XML_REGEXP_OUT_OF_MEMORY)
4806
40
            xmlVErrMemory(ctxt);
4807
53.7k
fail:
4808
53.7k
        xmlRegFreeExecCtxt(exec);
4809
53.7k
    }
4810
#else  /* LIBXML_REGEXP_ENABLED */
4811
    /*
4812
     * Allocate the stack
4813
     */
4814
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
4815
    ctxt->vstateMax = 8;
4816
#else
4817
    ctxt->vstateMax = 1;
4818
#endif
4819
    ctxt->vstateTab = xmlMalloc(ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
4820
    if (ctxt->vstateTab == NULL) {
4821
  xmlVErrMemory(ctxt);
4822
  return(-1);
4823
    }
4824
    /*
4825
     * The first entry in the stack is reserved to the current state
4826
     */
4827
    ctxt->nodeMax = 0;
4828
    ctxt->nodeNr = 0;
4829
    ctxt->nodeTab = NULL;
4830
    ctxt->vstate = &ctxt->vstateTab[0];
4831
    ctxt->vstateNr = 1;
4832
    CONT = cont;
4833
    NODE = child;
4834
    DEPTH = 0;
4835
    OCCURS = 0;
4836
    STATE = 0;
4837
    ret = xmlValidateElementType(ctxt);
4838
    if ((ret == -3) && (warn)) {
4839
  char expr[5000];
4840
  expr[0] = 0;
4841
  xmlSnprintfElementContent(expr, 5000, elemDecl->content, 1);
4842
  xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
4843
                XML_DTD_CONTENT_NOT_DETERMINIST,
4844
          "Content model of %s is not deterministic: %s\n",
4845
          name, BAD_CAST expr, NULL);
4846
    } else if (ret == -2) {
4847
  /*
4848
   * An entities reference appeared at this level.
4849
   * Build a minimal representation of this node content
4850
   * sufficient to run the validation process on it
4851
   */
4852
  cur = child;
4853
  while (cur != NULL) {
4854
      switch (cur->type) {
4855
    case XML_ENTITY_REF_NODE:
4856
        /*
4857
         * Push the current node to be able to roll back
4858
         * and process within the entity
4859
         */
4860
        if ((cur->children != NULL) &&
4861
      (cur->children->children != NULL)) {
4862
      if (nodeVPush(ctxt, cur) < 0) {
4863
                            xmlFreeNodeList(repl);
4864
                            ret = -1;
4865
                            goto done;
4866
                        }
4867
      cur = cur->children->children;
4868
      continue;
4869
        }
4870
        break;
4871
    case XML_TEXT_NODE:
4872
        if (xmlIsBlankNode(cur))
4873
      break;
4874
        /* falls through */
4875
    case XML_CDATA_SECTION_NODE:
4876
    case XML_ELEMENT_NODE:
4877
        /*
4878
         * Allocate a new node and minimally fills in
4879
         * what's required
4880
         */
4881
        tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
4882
        if (tmp == NULL) {
4883
      xmlVErrMemory(ctxt);
4884
      xmlFreeNodeList(repl);
4885
      ret = -1;
4886
      goto done;
4887
        }
4888
        tmp->type = cur->type;
4889
        tmp->name = cur->name;
4890
        tmp->ns = cur->ns;
4891
        tmp->next = NULL;
4892
        tmp->content = NULL;
4893
        if (repl == NULL)
4894
      repl = last = tmp;
4895
        else {
4896
      last->next = tmp;
4897
      last = tmp;
4898
        }
4899
        if (cur->type == XML_CDATA_SECTION_NODE) {
4900
      /*
4901
       * E59 spaces in CDATA does not match the
4902
       * nonterminal S
4903
       */
4904
      tmp->content = xmlStrdup(BAD_CAST "CDATA");
4905
        }
4906
        break;
4907
    default:
4908
        break;
4909
      }
4910
      /*
4911
       * Switch to next element
4912
       */
4913
      cur = cur->next;
4914
      while (cur == NULL) {
4915
    cur = nodeVPop(ctxt);
4916
    if (cur == NULL)
4917
        break;
4918
    cur = cur->next;
4919
      }
4920
  }
4921
4922
  /*
4923
   * Relaunch the validation
4924
   */
4925
  ctxt->vstate = &ctxt->vstateTab[0];
4926
  ctxt->vstateNr = 1;
4927
  CONT = cont;
4928
  NODE = repl;
4929
  DEPTH = 0;
4930
  OCCURS = 0;
4931
  STATE = 0;
4932
  ret = xmlValidateElementType(ctxt);
4933
    }
4934
#endif /* LIBXML_REGEXP_ENABLED */
4935
53.7k
    if ((warn) && ((ret != 1) && (ret != -3))) {
4936
43.3k
  if (ctxt != NULL) {
4937
43.3k
      char expr[5000];
4938
43.3k
      char list[5000];
4939
4940
43.3k
      expr[0] = 0;
4941
43.3k
      xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
4942
43.3k
      list[0] = 0;
4943
#ifndef LIBXML_REGEXP_ENABLED
4944
      if (repl != NULL)
4945
    xmlSnprintfElements(&list[0], 5000, repl, 1);
4946
      else
4947
#endif /* LIBXML_REGEXP_ENABLED */
4948
43.3k
    xmlSnprintfElements(&list[0], 5000, child, 1);
4949
4950
43.3k
      if (name != NULL) {
4951
43.3k
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
4952
43.3k
     "Element %s content does not follow the DTD, expecting %s, got %s\n",
4953
43.3k
           name, BAD_CAST expr, BAD_CAST list);
4954
43.3k
      } else {
4955
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
4956
0
     "Element content does not follow the DTD, expecting %s, got %s\n",
4957
0
           BAD_CAST expr, BAD_CAST list, NULL);
4958
0
      }
4959
43.3k
  } else {
4960
0
      if (name != NULL) {
4961
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
4962
0
           "Element %s content does not follow the DTD\n",
4963
0
           name, NULL, NULL);
4964
0
      } else {
4965
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
4966
0
           "Element content does not follow the DTD\n",
4967
0
                    NULL, NULL, NULL);
4968
0
      }
4969
0
  }
4970
43.3k
  ret = 0;
4971
43.3k
    }
4972
53.7k
    if (ret == -3)
4973
0
  ret = 1;
4974
4975
#ifndef  LIBXML_REGEXP_ENABLED
4976
done:
4977
    /*
4978
     * Deallocate the copy if done, and free up the validation stack
4979
     */
4980
    while (repl != NULL) {
4981
  tmp = repl->next;
4982
  xmlFree(repl);
4983
  repl = tmp;
4984
    }
4985
    ctxt->vstateMax = 0;
4986
    if (ctxt->vstateTab != NULL) {
4987
  xmlFree(ctxt->vstateTab);
4988
  ctxt->vstateTab = NULL;
4989
    }
4990
#endif
4991
53.7k
    ctxt->nodeMax = 0;
4992
53.7k
    ctxt->nodeNr = 0;
4993
53.7k
    if (ctxt->nodeTab != NULL) {
4994
995
  xmlFree(ctxt->nodeTab);
4995
995
  ctxt->nodeTab = NULL;
4996
995
    }
4997
53.7k
    return(ret);
4998
4999
57.7k
}
5000
5001
/**
5002
 * Check that an element follows \#CDATA.
5003
 *
5004
 * @param ctxt  the validation context
5005
 * @param doc  a document instance
5006
 * @param elem  an element instance
5007
 * @returns 1 if valid or 0 otherwise.
5008
 */
5009
static int
5010
xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5011
12.6k
                           xmlNodePtr elem) {
5012
12.6k
    int ret = 1;
5013
12.6k
    xmlNodePtr cur, child;
5014
5015
12.6k
    if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5016
12.6k
        (elem->type != XML_ELEMENT_NODE))
5017
0
  return(0);
5018
5019
12.6k
    child = elem->children;
5020
5021
12.6k
    cur = child;
5022
122k
    while (cur != NULL) {
5023
115k
  switch (cur->type) {
5024
50.8k
      case XML_ENTITY_REF_NODE:
5025
    /*
5026
     * Push the current node to be able to roll back
5027
     * and process within the entity
5028
     */
5029
50.8k
    if ((cur->children != NULL) &&
5030
48.0k
        (cur->children->children != NULL)) {
5031
46.9k
        if (nodeVPush(ctxt, cur) < 0) {
5032
9
                        ret = 0;
5033
9
                        goto done;
5034
9
                    }
5035
46.9k
        cur = cur->children->children;
5036
46.9k
        continue;
5037
46.9k
    }
5038
3.83k
    break;
5039
3.83k
      case XML_COMMENT_NODE:
5040
1.67k
      case XML_PI_NODE:
5041
58.3k
      case XML_TEXT_NODE:
5042
59.3k
      case XML_CDATA_SECTION_NODE:
5043
59.3k
    break;
5044
5.44k
      default:
5045
5.44k
    ret = 0;
5046
5.44k
    goto done;
5047
115k
  }
5048
  /*
5049
   * Switch to next element
5050
   */
5051
63.2k
  cur = cur->next;
5052
110k
  while (cur == NULL) {
5053
51.5k
      cur = nodeVPop(ctxt);
5054
51.5k
      if (cur == NULL)
5055
4.61k
    break;
5056
46.9k
      cur = cur->next;
5057
46.9k
  }
5058
63.2k
    }
5059
12.6k
done:
5060
12.6k
    ctxt->nodeMax = 0;
5061
12.6k
    ctxt->nodeNr = 0;
5062
12.6k
    if (ctxt->nodeTab != NULL) {
5063
1.04k
  xmlFree(ctxt->nodeTab);
5064
1.04k
  ctxt->nodeTab = NULL;
5065
1.04k
    }
5066
12.6k
    return(ret);
5067
12.6k
}
5068
5069
#ifdef LIBXML_REGEXP_ENABLED
5070
/**
5071
 * Check if the given node is part of the content model.
5072
 *
5073
 * @param ctxt  the validation context
5074
 * @param cont  the mixed content model
5075
 * @param qname  the qualified name as appearing in the serialization
5076
 * @returns 1 if yes, 0 if no, -1 in case of error
5077
 */
5078
static int
5079
xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
5080
4.04k
                xmlElementContentPtr cont, const xmlChar *qname) {
5081
4.04k
    const xmlChar *name;
5082
4.04k
    int plen;
5083
4.04k
    name = xmlSplitQName3(qname, &plen);
5084
5085
4.04k
    if (name == NULL) {
5086
11.4k
  while (cont != NULL) {
5087
9.89k
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5088
2.33k
    if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5089
801
        return(1);
5090
7.56k
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5091
7.56k
         (cont->c1 != NULL) &&
5092
7.56k
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5093
4.98k
    if ((cont->c1->prefix == NULL) &&
5094
2.43k
        (xmlStrEqual(cont->c1->name, qname)))
5095
242
        return(1);
5096
4.98k
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5097
2.57k
    (cont->c1 == NULL) ||
5098
2.57k
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5099
0
    xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5100
0
      "Internal: MIXED struct corrupted\n",
5101
0
      NULL);
5102
0
    break;
5103
0
      }
5104
8.85k
      cont = cont->c2;
5105
8.85k
  }
5106
2.57k
    } else {
5107
7.54k
  while (cont != NULL) {
5108
6.16k
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5109
1.45k
    if ((cont->prefix != NULL) &&
5110
474
        (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5111
302
        (xmlStrEqual(cont->name, name)))
5112
76
        return(1);
5113
4.71k
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5114
4.71k
         (cont->c1 != NULL) &&
5115
4.71k
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5116
3.24k
    if ((cont->c1->prefix != NULL) &&
5117
2.03k
        (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5118
736
        (xmlStrEqual(cont->c1->name, name)))
5119
10
        return(1);
5120
3.24k
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5121
1.46k
    (cont->c1 == NULL) ||
5122
1.46k
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5123
0
    xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5124
0
      "Internal: MIXED struct corrupted\n",
5125
0
      NULL);
5126
0
    break;
5127
0
      }
5128
6.07k
      cont = cont->c2;
5129
6.07k
  }
5130
1.46k
    }
5131
2.91k
    return(0);
5132
4.04k
}
5133
#endif /* LIBXML_REGEXP_ENABLED */
5134
5135
/**
5136
 * Finds a declaration associated to an element in the document.
5137
 *
5138
 * @param ctxt  the validation context
5139
 * @param doc  a document instance
5140
 * @param elem  an element instance
5141
 * @param extsubset  pointer, (out) indicate if the declaration was found
5142
 *              in the external subset.
5143
 * @returns the pointer to the declaration or NULL if not found.
5144
 */
5145
static xmlElementPtr
5146
xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5147
495k
              xmlNodePtr elem, int *extsubset) {
5148
495k
    xmlElementPtr elemDecl = NULL;
5149
495k
    const xmlChar *prefix = NULL;
5150
5151
495k
    if ((ctxt == NULL) || (doc == NULL) ||
5152
495k
        (elem == NULL) || (elem->name == NULL))
5153
1.31k
        return(NULL);
5154
494k
    if (extsubset != NULL)
5155
494k
  *extsubset = 0;
5156
5157
    /*
5158
     * Fetch the declaration for the qualified name
5159
     */
5160
494k
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5161
30.6k
  prefix = elem->ns->prefix;
5162
5163
494k
    if (prefix != NULL) {
5164
30.6k
  elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5165
30.6k
                             elem->name, prefix);
5166
30.6k
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5167
12.9k
      elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5168
12.9k
                                 elem->name, prefix);
5169
12.9k
      if ((elemDecl != NULL) && (extsubset != NULL))
5170
1.06k
    *extsubset = 1;
5171
12.9k
  }
5172
30.6k
    }
5173
5174
    /*
5175
     * Fetch the declaration for the non qualified name
5176
     * This is "non-strict" validation should be done on the
5177
     * full QName but in that case being flexible makes sense.
5178
     */
5179
494k
    if (elemDecl == NULL) {
5180
489k
  elemDecl = xmlGetDtdQElementDesc(doc->intSubset, elem->name, NULL);
5181
489k
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5182
155k
      elemDecl = xmlGetDtdQElementDesc(doc->extSubset, elem->name, NULL);
5183
155k
      if ((elemDecl != NULL) && (extsubset != NULL))
5184
39.3k
    *extsubset = 1;
5185
155k
  }
5186
489k
    }
5187
494k
    if (elemDecl == NULL) {
5188
298k
  xmlErrValidNode(ctxt, elem,
5189
298k
      XML_DTD_UNKNOWN_ELEM,
5190
298k
         "No declaration for element %s\n",
5191
298k
         elem->name, NULL, NULL);
5192
298k
    }
5193
494k
    return(elemDecl);
5194
495k
}
5195
5196
#ifdef LIBXML_REGEXP_ENABLED
5197
/**
5198
 * Push a new element start on the validation stack.
5199
 *
5200
 * @deprecated Internal function, don't use.
5201
 *
5202
 * @param ctxt  the validation context
5203
 * @param doc  a document instance
5204
 * @param elem  an element instance
5205
 * @param qname  the qualified name as appearing in the serialization
5206
 * @returns 1 if no validation problem was found or 0 otherwise.
5207
 */
5208
int
5209
xmlValidatePushElement(xmlValidCtxt *ctxt, xmlDoc *doc,
5210
96.7k
                       xmlNode *elem, const xmlChar *qname) {
5211
96.7k
    int ret = 1;
5212
96.7k
    xmlElementPtr eDecl;
5213
96.7k
    int extsubset = 0;
5214
5215
96.7k
    if (ctxt == NULL)
5216
0
        return(0);
5217
5218
96.7k
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5219
93.8k
  xmlValidStatePtr state = ctxt->vstate;
5220
93.8k
  xmlElementPtr elemDecl;
5221
5222
  /*
5223
   * Check the new element against the content model of the new elem.
5224
   */
5225
93.8k
  if (state->elemDecl != NULL) {
5226
65.3k
      elemDecl = state->elemDecl;
5227
5228
65.3k
      switch(elemDecl->etype) {
5229
2.41k
    case XML_ELEMENT_TYPE_UNDEFINED:
5230
2.41k
        ret = 0;
5231
2.41k
        break;
5232
354
    case XML_ELEMENT_TYPE_EMPTY:
5233
354
        xmlErrValidNode(ctxt, state->node,
5234
354
            XML_DTD_NOT_EMPTY,
5235
354
         "Element %s was declared EMPTY this one has content\n",
5236
354
         state->node->name, NULL, NULL);
5237
354
        ret = 0;
5238
354
        break;
5239
77
    case XML_ELEMENT_TYPE_ANY:
5240
        /* I don't think anything is required then */
5241
77
        break;
5242
5.58k
    case XML_ELEMENT_TYPE_MIXED:
5243
        /* simple case of declared as #PCDATA */
5244
5.58k
        if ((elemDecl->content != NULL) &&
5245
5.58k
      (elemDecl->content->type ==
5246
5.58k
       XML_ELEMENT_CONTENT_PCDATA)) {
5247
1.54k
      xmlErrValidNode(ctxt, state->node,
5248
1.54k
          XML_DTD_NOT_PCDATA,
5249
1.54k
         "Element %s was declared #PCDATA but contains non text nodes\n",
5250
1.54k
        state->node->name, NULL, NULL);
5251
1.54k
      ret = 0;
5252
4.04k
        } else {
5253
4.04k
      ret = xmlValidateCheckMixed(ctxt, elemDecl->content,
5254
4.04k
                            qname);
5255
4.04k
      if (ret != 1) {
5256
2.91k
          xmlErrValidNode(ctxt, state->node,
5257
2.91k
              XML_DTD_INVALID_CHILD,
5258
2.91k
         "Element %s is not declared in %s list of possible children\n",
5259
2.91k
            qname, state->node->name, NULL);
5260
2.91k
      }
5261
4.04k
        }
5262
5.58k
        break;
5263
56.9k
    case XML_ELEMENT_TYPE_ELEMENT:
5264
        /*
5265
         * TODO:
5266
         * VC: Standalone Document Declaration
5267
         *     - element types with element content, if white space
5268
         *       occurs directly within any instance of those types.
5269
         */
5270
56.9k
        if (state->exec != NULL) {
5271
56.4k
      ret = xmlRegExecPushString(state->exec, qname, NULL);
5272
56.4k
                        if (ret == XML_REGEXP_OUT_OF_MEMORY) {
5273
290
                            xmlVErrMemory(ctxt);
5274
290
                            return(0);
5275
290
                        }
5276
56.1k
      if (ret < 0) {
5277
6.05k
          xmlErrValidNode(ctxt, state->node,
5278
6.05k
              XML_DTD_CONTENT_MODEL,
5279
6.05k
         "Element %s content does not follow the DTD, Misplaced %s\n",
5280
6.05k
           state->node->name, qname, NULL);
5281
6.05k
          ret = 0;
5282
50.1k
      } else {
5283
50.1k
          ret = 1;
5284
50.1k
      }
5285
56.1k
        }
5286
56.6k
        break;
5287
65.3k
      }
5288
65.3k
  }
5289
93.8k
    }
5290
96.4k
    eDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5291
96.4k
    vstateVPush(ctxt, eDecl, elem);
5292
96.4k
    return(ret);
5293
96.7k
}
5294
5295
/**
5296
 * Check the CData parsed for validation in the current stack.
5297
 *
5298
 * @deprecated Internal function, don't use.
5299
 *
5300
 * @param ctxt  the validation context
5301
 * @param data  some character data read
5302
 * @param len  the length of the data
5303
 * @returns 1 if no validation problem was found or 0 otherwise.
5304
 */
5305
int
5306
39.5k
xmlValidatePushCData(xmlValidCtxt *ctxt, const xmlChar *data, int len) {
5307
39.5k
    int ret = 1;
5308
5309
39.5k
    if (ctxt == NULL)
5310
0
        return(0);
5311
39.5k
    if (len <= 0)
5312
85
  return(ret);
5313
39.4k
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5314
39.1k
  xmlValidStatePtr state = ctxt->vstate;
5315
39.1k
  xmlElementPtr elemDecl;
5316
5317
  /*
5318
   * Check the new element against the content model of the new elem.
5319
   */
5320
39.1k
  if (state->elemDecl != NULL) {
5321
14.8k
      elemDecl = state->elemDecl;
5322
5323
14.8k
      switch(elemDecl->etype) {
5324
635
    case XML_ELEMENT_TYPE_UNDEFINED:
5325
635
        ret = 0;
5326
635
        break;
5327
380
    case XML_ELEMENT_TYPE_EMPTY:
5328
380
        xmlErrValidNode(ctxt, state->node,
5329
380
            XML_DTD_NOT_EMPTY,
5330
380
         "Element %s was declared EMPTY this one has content\n",
5331
380
         state->node->name, NULL, NULL);
5332
380
        ret = 0;
5333
380
        break;
5334
20
    case XML_ELEMENT_TYPE_ANY:
5335
20
        break;
5336
10.5k
    case XML_ELEMENT_TYPE_MIXED:
5337
10.5k
        break;
5338
3.18k
    case XML_ELEMENT_TYPE_ELEMENT: {
5339
3.18k
                    int i;
5340
5341
5.68k
                    for (i = 0;i < len;i++) {
5342
4.72k
                        if (!IS_BLANK_CH(data[i])) {
5343
2.21k
                            xmlErrValidNode(ctxt, state->node,
5344
2.21k
                                            XML_DTD_CONTENT_MODEL,
5345
2.21k
       "Element %s content does not follow the DTD, Text not allowed\n",
5346
2.21k
                                   state->node->name, NULL, NULL);
5347
2.21k
                            ret = 0;
5348
2.21k
                            goto done;
5349
2.21k
                        }
5350
4.72k
                    }
5351
                    /*
5352
                     * TODO:
5353
                     * VC: Standalone Document Declaration
5354
                     *  element types with element content, if white space
5355
                     *  occurs directly within any instance of those types.
5356
                     */
5357
966
                    break;
5358
3.18k
                }
5359
14.8k
      }
5360
14.8k
  }
5361
39.1k
    }
5362
39.4k
done:
5363
39.4k
    return(ret);
5364
39.4k
}
5365
5366
/**
5367
 * Pop the element end from the validation stack.
5368
 *
5369
 * @deprecated Internal function, don't use.
5370
 *
5371
 * @param ctxt  the validation context
5372
 * @param doc  a document instance
5373
 * @param elem  an element instance
5374
 * @param qname  the qualified name as appearing in the serialization
5375
 * @returns 1 if no validation problem was found or 0 otherwise.
5376
 */
5377
int
5378
xmlValidatePopElement(xmlValidCtxt *ctxt, xmlDoc *doc ATTRIBUTE_UNUSED,
5379
                      xmlNode *elem ATTRIBUTE_UNUSED,
5380
96.7k
          const xmlChar *qname ATTRIBUTE_UNUSED) {
5381
96.7k
    int ret = 1;
5382
5383
96.7k
    if (ctxt == NULL)
5384
0
        return(0);
5385
5386
96.7k
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5387
96.3k
  xmlValidStatePtr state = ctxt->vstate;
5388
96.3k
  xmlElementPtr elemDecl;
5389
5390
  /*
5391
   * Check the new element against the content model of the new elem.
5392
   */
5393
96.3k
  if (state->elemDecl != NULL) {
5394
18.9k
      elemDecl = state->elemDecl;
5395
5396
18.9k
      if (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT) {
5397
7.19k
    if (state->exec != NULL) {
5398
6.61k
        ret = xmlRegExecPushString(state->exec, NULL, NULL);
5399
6.61k
        if (ret <= 0) {
5400
4.87k
                        if (ret == XML_REGEXP_OUT_OF_MEMORY)
5401
27
                            xmlVErrMemory(ctxt);
5402
4.84k
                        else
5403
4.84k
          xmlErrValidNode(ctxt, state->node,
5404
4.84k
                          XML_DTD_CONTENT_MODEL,
5405
4.84k
     "Element %s content does not follow the DTD, Expecting more children\n",
5406
4.84k
             state->node->name, NULL,NULL);
5407
4.87k
      ret = 0;
5408
4.87k
        } else {
5409
      /*
5410
       * previous validation errors should not generate
5411
       * a new one here
5412
       */
5413
1.74k
      ret = 1;
5414
1.74k
        }
5415
6.61k
    }
5416
7.19k
      }
5417
18.9k
  }
5418
96.3k
  vstateVPop(ctxt);
5419
96.3k
    }
5420
96.7k
    return(ret);
5421
96.7k
}
5422
#endif /* LIBXML_REGEXP_ENABLED */
5423
5424
/**
5425
 * Try to validate a single element and its attributes.
5426
 * Performs the following checks as described by the
5427
 * XML-1.0 recommendation:
5428
 *
5429
 * @deprecated Internal function, don't use.
5430
 *
5431
 * - [ VC: Element Valid ]
5432
 * - [ VC: Required Attribute ]
5433
 *
5434
 * Then calls #xmlValidateOneAttribute for each attribute present.
5435
 *
5436
 * ID/IDREF checks are handled separately.
5437
 *
5438
 * @param ctxt  the validation context
5439
 * @param doc  a document instance
5440
 * @param elem  an element instance
5441
 * @returns 1 if valid or 0 otherwise.
5442
 */
5443
5444
int
5445
xmlValidateOneElement(xmlValidCtxt *ctxt, xmlDoc *doc,
5446
517k
                      xmlNode *elem) {
5447
517k
    xmlElementPtr elemDecl = NULL;
5448
517k
    xmlElementContentPtr cont;
5449
517k
    xmlAttributePtr attr;
5450
517k
    xmlNodePtr child;
5451
517k
    int ret = 1, tmp;
5452
517k
    const xmlChar *name;
5453
517k
    int extsubset = 0;
5454
5455
517k
    CHECK_DTD;
5456
5457
517k
    if (elem == NULL) return(0);
5458
517k
    switch (elem->type) {
5459
89.0k
        case XML_TEXT_NODE:
5460
92.0k
        case XML_CDATA_SECTION_NODE:
5461
112k
        case XML_ENTITY_REF_NODE:
5462
115k
        case XML_PI_NODE:
5463
118k
        case XML_COMMENT_NODE:
5464
118k
        case XML_XINCLUDE_START:
5465
118k
        case XML_XINCLUDE_END:
5466
118k
      return(1);
5467
399k
        case XML_ELEMENT_NODE:
5468
399k
      break;
5469
0
  default:
5470
0
      xmlErrValidNode(ctxt, elem, XML_ERR_INTERNAL_ERROR,
5471
0
       "unexpected element type\n", NULL, NULL ,NULL);
5472
0
      return(0);
5473
517k
    }
5474
5475
    /*
5476
     * Fetch the declaration
5477
     */
5478
399k
    elemDecl = xmlValidGetElemDecl(ctxt, doc, elem, &extsubset);
5479
399k
    if (elemDecl == NULL)
5480
222k
  return(0);
5481
5482
    /*
5483
     * If vstateNr is not zero that means continuous validation is
5484
     * activated, do not try to check the content model at that level.
5485
     */
5486
177k
    if (ctxt->vstateNr == 0) {
5487
    /* Check that the element content matches the definition */
5488
169k
    switch (elemDecl->etype) {
5489
75.1k
        case XML_ELEMENT_TYPE_UNDEFINED:
5490
75.1k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ELEM,
5491
75.1k
                      "No declaration for element %s\n",
5492
75.1k
       elem->name, NULL, NULL);
5493
75.1k
      return(0);
5494
7.00k
        case XML_ELEMENT_TYPE_EMPTY:
5495
7.00k
      if (elem->children != NULL) {
5496
2.14k
    xmlErrValidNode(ctxt, elem, XML_DTD_NOT_EMPTY,
5497
2.14k
         "Element %s was declared EMPTY this one has content\n",
5498
2.14k
                 elem->name, NULL, NULL);
5499
2.14k
    ret = 0;
5500
2.14k
      }
5501
7.00k
      break;
5502
4.58k
        case XML_ELEMENT_TYPE_ANY:
5503
      /* I don't think anything is required then */
5504
4.58k
      break;
5505
25.5k
        case XML_ELEMENT_TYPE_MIXED:
5506
5507
      /* simple case of declared as #PCDATA */
5508
25.5k
      if ((elemDecl->content != NULL) &&
5509
25.5k
    (elemDecl->content->type == XML_ELEMENT_CONTENT_PCDATA)) {
5510
12.6k
    ret = xmlValidateOneCdataElement(ctxt, doc, elem);
5511
12.6k
    if (!ret) {
5512
5.44k
        xmlErrValidNode(ctxt, elem, XML_DTD_NOT_PCDATA,
5513
5.44k
         "Element %s was declared #PCDATA but contains non text nodes\n",
5514
5.44k
         elem->name, NULL, NULL);
5515
5.44k
    }
5516
12.6k
    break;
5517
12.6k
      }
5518
12.8k
      child = elem->children;
5519
      /* Hum, this start to get messy */
5520
50.1k
      while (child != NULL) {
5521
37.3k
          if (child->type == XML_ELEMENT_NODE) {
5522
13.9k
        name = child->name;
5523
13.9k
        if ((child->ns != NULL) && (child->ns->prefix != NULL)) {
5524
2.80k
      xmlChar fn[50];
5525
2.80k
      xmlChar *fullname;
5526
5527
2.80k
      fullname = xmlBuildQName(child->name, child->ns->prefix,
5528
2.80k
                         fn, 50);
5529
2.80k
      if (fullname == NULL) {
5530
10
                            xmlVErrMemory(ctxt);
5531
10
          return(0);
5532
10
                        }
5533
2.79k
      cont = elemDecl->content;
5534
9.24k
      while (cont != NULL) {
5535
6.83k
          if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5536
2.52k
        if (xmlStrEqual(cont->name, fullname))
5537
112
            break;
5538
4.30k
          } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5539
4.30k
             (cont->c1 != NULL) &&
5540
4.30k
             (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5541
1.51k
        if (xmlStrEqual(cont->c1->name, fullname))
5542
272
            break;
5543
2.79k
          } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5544
2.79k
        (cont->c1 == NULL) ||
5545
2.79k
        (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5546
0
        xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5547
0
          "Internal: MIXED struct corrupted\n",
5548
0
          NULL);
5549
0
        break;
5550
0
          }
5551
6.44k
          cont = cont->c2;
5552
6.44k
      }
5553
2.79k
      if ((fullname != fn) && (fullname != child->name))
5554
1.27k
          xmlFree(fullname);
5555
2.79k
      if (cont != NULL)
5556
384
          goto child_ok;
5557
2.79k
        }
5558
13.5k
        cont = elemDecl->content;
5559
59.7k
        while (cont != NULL) {
5560
54.3k
            if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5561
9.80k
          if (xmlStrEqual(cont->name, name)) break;
5562
44.5k
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5563
44.5k
         (cont->c1 != NULL) &&
5564
44.5k
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)) {
5565
30.9k
          if (xmlStrEqual(cont->c1->name, name)) break;
5566
30.9k
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5567
13.5k
          (cont->c1 == NULL) ||
5568
13.5k
          (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)) {
5569
0
          xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5570
0
            "Internal: MIXED struct corrupted\n",
5571
0
            NULL);
5572
0
          break;
5573
0
      }
5574
46.2k
      cont = cont->c2;
5575
46.2k
        }
5576
13.5k
        if (cont == NULL) {
5577
5.46k
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_CHILD,
5578
5.46k
         "Element %s is not declared in %s list of possible children\n",
5579
5.46k
             name, elem->name, NULL);
5580
5.46k
      ret = 0;
5581
5.46k
        }
5582
13.5k
    }
5583
37.3k
child_ok:
5584
37.3k
          child = child->next;
5585
37.3k
      }
5586
12.8k
      break;
5587
57.7k
        case XML_ELEMENT_TYPE_ELEMENT:
5588
57.7k
      if ((doc->standalone == 1) && (extsubset == 1)) {
5589
    /*
5590
     * VC: Standalone Document Declaration
5591
     *     - element types with element content, if white space
5592
     *       occurs directly within any instance of those types.
5593
     */
5594
9.21k
    child = elem->children;
5595
20.3k
    while (child != NULL) {
5596
14.5k
        if ((child->type == XML_TEXT_NODE) &&
5597
7.44k
                        (child->content != NULL)) {
5598
7.15k
      const xmlChar *content = child->content;
5599
5600
7.15k
      while (IS_BLANK_CH(*content))
5601
4.36k
          content++;
5602
7.15k
      if (*content == 0) {
5603
3.40k
          xmlErrValidNode(ctxt, elem,
5604
3.40k
                          XML_DTD_STANDALONE_WHITE_SPACE,
5605
3.40k
"standalone: %s declared in the external subset contains white spaces nodes\n",
5606
3.40k
           elem->name, NULL, NULL);
5607
3.40k
          ret = 0;
5608
3.40k
          break;
5609
3.40k
      }
5610
7.15k
        }
5611
11.1k
        child =child->next;
5612
11.1k
    }
5613
9.21k
      }
5614
57.7k
      child = elem->children;
5615
57.7k
      cont = elemDecl->content;
5616
57.7k
      tmp = xmlValidateElementContent(ctxt, child, elemDecl, 1, elem);
5617
57.7k
      if (tmp <= 0)
5618
47.2k
    ret = 0;
5619
57.7k
      break;
5620
169k
    }
5621
169k
    } /* not continuous */
5622
5623
    /* [ VC: Required Attribute ] */
5624
102k
    attr = elemDecl->attributes;
5625
247k
    while (attr != NULL) {
5626
144k
  if (attr->def == XML_ATTRIBUTE_REQUIRED) {
5627
12.8k
      int qualified = -1;
5628
5629
12.8k
      if ((attr->prefix == NULL) &&
5630
6.58k
    (xmlStrEqual(attr->name, BAD_CAST "xmlns"))) {
5631
1.42k
    xmlNsPtr ns;
5632
5633
1.42k
    ns = elem->nsDef;
5634
1.88k
    while (ns != NULL) {
5635
944
        if (ns->prefix == NULL)
5636
481
      goto found;
5637
463
        ns = ns->next;
5638
463
    }
5639
11.4k
      } else if (xmlStrEqual(attr->prefix, BAD_CAST "xmlns")) {
5640
1.54k
    xmlNsPtr ns;
5641
5642
1.54k
    ns = elem->nsDef;
5643
1.95k
    while (ns != NULL) {
5644
811
        if (xmlStrEqual(attr->name, ns->prefix))
5645
397
      goto found;
5646
414
        ns = ns->next;
5647
414
    }
5648
9.87k
      } else {
5649
9.87k
    xmlAttrPtr attrib;
5650
5651
9.87k
    attrib = elem->properties;
5652
15.9k
    while (attrib != NULL) {
5653
9.85k
        if (xmlStrEqual(attrib->name, attr->name)) {
5654
7.11k
      if (attr->prefix != NULL) {
5655
4.17k
          xmlNsPtr nameSpace = attrib->ns;
5656
5657
          /*
5658
           * qualified names handling is problematic, having a
5659
           * different prefix should be possible but DTDs don't
5660
           * allow to define the URI instead of the prefix :-(
5661
           */
5662
4.17k
          if (nameSpace == NULL) {
5663
2.03k
        if (qualified < 0)
5664
1.35k
            qualified = 0;
5665
2.14k
          } else if (!xmlStrEqual(nameSpace->prefix,
5666
2.14k
                attr->prefix)) {
5667
1.25k
        if (qualified < 1)
5668
915
            qualified = 1;
5669
1.25k
          } else
5670
881
        goto found;
5671
4.17k
      } else {
5672
          /*
5673
           * We should allow applications to define namespaces
5674
           * for their application even if the DTD doesn't
5675
           * carry one, otherwise, basically we would always
5676
           * break.
5677
           */
5678
2.93k
          goto found;
5679
2.93k
      }
5680
7.11k
        }
5681
6.03k
        attrib = attrib->next;
5682
6.03k
    }
5683
9.87k
      }
5684
8.14k
      if (qualified == -1) {
5685
6.02k
    if (attr->prefix == NULL) {
5686
3.16k
        xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
5687
3.16k
           "Element %s does not carry attribute %s\n",
5688
3.16k
         elem->name, attr->name, NULL);
5689
3.16k
        ret = 0;
5690
3.16k
          } else {
5691
2.86k
        xmlErrValidNode(ctxt, elem, XML_DTD_MISSING_ATTRIBUTE,
5692
2.86k
           "Element %s does not carry attribute %s:%s\n",
5693
2.86k
         elem->name, attr->prefix,attr->name);
5694
2.86k
        ret = 0;
5695
2.86k
    }
5696
6.02k
      } else if (qualified == 0) {
5697
1.20k
    if (xmlErrValidWarning(ctxt, elem, XML_DTD_NO_PREFIX,
5698
1.20k
                           "Element %s required attribute %s:%s "
5699
1.20k
                                       "has no prefix\n",
5700
1.20k
                           elem->name, attr->prefix,
5701
1.20k
                                       attr->name) < 0)
5702
299
                    ret = 0;
5703
1.20k
      } else if (qualified == 1) {
5704
915
    if (xmlErrValidWarning(ctxt, elem, XML_DTD_DIFFERENT_PREFIX,
5705
915
                           "Element %s required attribute %s:%s "
5706
915
                                       "has different prefix\n",
5707
915
                           elem->name, attr->prefix,
5708
915
                                       attr->name) < 0)
5709
205
                    ret = 0;
5710
915
      }
5711
8.14k
  }
5712
144k
found:
5713
144k
        attr = attr->nexth;
5714
144k
    }
5715
102k
    return(ret);
5716
102k
}
5717
5718
/**
5719
 * Try to validate the root element.
5720
 * Performs the following check as described by the
5721
 * XML-1.0 recommendation:
5722
 *
5723
 * @deprecated Internal function, don't use.
5724
 *
5725
 * - [ VC: Root Element Type ]
5726
 *
5727
 * It doesn't try to recurse or apply other checks to the element.
5728
 *
5729
 * @param ctxt  the validation context
5730
 * @param doc  a document instance
5731
 * @returns 1 if valid or 0 otherwise.
5732
 */
5733
5734
int
5735
53.8k
xmlValidateRoot(xmlValidCtxt *ctxt, xmlDoc *doc) {
5736
53.8k
    xmlNodePtr root;
5737
53.8k
    int ret;
5738
5739
53.8k
    if (doc == NULL) return(0);
5740
5741
53.8k
    root = xmlDocGetRootElement(doc);
5742
53.8k
    if ((root == NULL) || (root->name == NULL)) {
5743
5.72k
  xmlErrValid(ctxt, XML_DTD_NO_ROOT,
5744
5.72k
              "no root element\n", NULL);
5745
5.72k
        return(0);
5746
5.72k
    }
5747
5748
    /*
5749
     * When doing post validation against a separate DTD, those may
5750
     * no internal subset has been generated
5751
     */
5752
48.1k
    if ((doc->intSubset != NULL) &&
5753
40.7k
  (doc->intSubset->name != NULL)) {
5754
  /*
5755
   * Check first the document root against the NQName
5756
   */
5757
36.3k
  if (!xmlStrEqual(doc->intSubset->name, root->name)) {
5758
24.0k
      if ((root->ns != NULL) && (root->ns->prefix != NULL)) {
5759
1.25k
    xmlChar fn[50];
5760
1.25k
    xmlChar *fullname;
5761
5762
1.25k
    fullname = xmlBuildQName(root->name, root->ns->prefix, fn, 50);
5763
1.25k
    if (fullname == NULL) {
5764
5
        xmlVErrMemory(ctxt);
5765
5
        return(0);
5766
5
    }
5767
1.24k
    ret = xmlStrEqual(doc->intSubset->name, fullname);
5768
1.24k
    if ((fullname != fn) && (fullname != root->name))
5769
498
        xmlFree(fullname);
5770
1.24k
    if (ret == 1)
5771
492
        goto name_ok;
5772
1.24k
      }
5773
23.5k
      if ((xmlStrEqual(doc->intSubset->name, BAD_CAST "HTML")) &&
5774
210
    (xmlStrEqual(root->name, BAD_CAST "html")))
5775
6
    goto name_ok;
5776
23.4k
      xmlErrValidNode(ctxt, root, XML_DTD_ROOT_NAME,
5777
23.4k
       "root and DTD name do not match '%s' and '%s'\n",
5778
23.4k
       root->name, doc->intSubset->name, NULL);
5779
23.4k
      return(0);
5780
23.5k
  }
5781
36.3k
    }
5782
24.6k
name_ok:
5783
24.6k
    return(1);
5784
48.1k
}
5785
5786
5787
/**
5788
 * Try to validate the subtree under an element.
5789
 *
5790
 * @param ctxt  the validation context
5791
 * @param doc  a document instance
5792
 * @param root  an element instance
5793
 * @returns 1 if valid or 0 otherwise.
5794
 */
5795
5796
int
5797
16.6k
xmlValidateElement(xmlValidCtxt *ctxt, xmlDoc *doc, xmlNode *root) {
5798
16.6k
    xmlNodePtr elem;
5799
16.6k
    xmlAttrPtr attr;
5800
16.6k
    xmlNsPtr ns;
5801
16.6k
    xmlChar *value;
5802
16.6k
    int ret = 1;
5803
5804
16.6k
    if (root == NULL) return(0);
5805
5806
16.6k
    CHECK_DTD;
5807
5808
16.0k
    elem = root;
5809
399k
    while (1) {
5810
399k
        ret &= xmlValidateOneElement(ctxt, doc, elem);
5811
5812
399k
        if (elem->type == XML_ELEMENT_NODE) {
5813
281k
            attr = elem->properties;
5814
445k
            while (attr != NULL) {
5815
163k
                if (attr->children == NULL)
5816
5.72k
                    value = xmlStrdup(BAD_CAST "");
5817
157k
                else
5818
157k
                    value = xmlNodeListGetString(doc, attr->children, 0);
5819
163k
                if (value == NULL) {
5820
76
                    xmlVErrMemory(ctxt);
5821
76
                    ret = 0;
5822
163k
                } else {
5823
163k
                    ret &= xmlValidateOneAttribute(ctxt, doc, elem, attr, value);
5824
163k
                    xmlFree(value);
5825
163k
                }
5826
163k
                attr= attr->next;
5827
163k
            }
5828
5829
281k
            ns = elem->nsDef;
5830
508k
            while (ns != NULL) {
5831
226k
                if (elem->ns == NULL)
5832
170k
                    ret &= xmlValidateOneNamespace(ctxt, doc, elem, NULL,
5833
170k
                                                   ns, ns->href);
5834
56.7k
                else
5835
56.7k
                    ret &= xmlValidateOneNamespace(ctxt, doc, elem,
5836
56.7k
                                                   elem->ns->prefix, ns,
5837
56.7k
                                                   ns->href);
5838
226k
                ns = ns->next;
5839
226k
            }
5840
5841
281k
            if (elem->children != NULL) {
5842
172k
                elem = elem->children;
5843
172k
                continue;
5844
172k
            }
5845
281k
        }
5846
5847
399k
        while (1) {
5848
399k
            if (elem == root)
5849
16.0k
                goto done;
5850
383k
            if (elem->next != NULL)
5851
210k
                break;
5852
172k
            elem = elem->parent;
5853
172k
        }
5854
210k
        elem = elem->next;
5855
210k
    }
5856
5857
16.0k
done:
5858
16.0k
    return(ret);
5859
16.0k
}
5860
5861
/**
5862
 * @param ref  A reference to be validated
5863
 * @param ctxt  Validation context
5864
 * @param name  Name of ID we are searching for
5865
 */
5866
static void
5867
xmlValidateRef(xmlRefPtr ref, xmlValidCtxtPtr ctxt,
5868
13.7k
                     const xmlChar *name) {
5869
13.7k
    xmlAttrPtr id;
5870
13.7k
    xmlAttrPtr attr;
5871
5872
13.7k
    if (ref == NULL)
5873
0
  return;
5874
13.7k
    if ((ref->attr == NULL) && (ref->name == NULL))
5875
0
  return;
5876
13.7k
    attr = ref->attr;
5877
13.7k
    if (attr == NULL) {
5878
677
  xmlChar *dup, *str = NULL, *cur, save;
5879
5880
677
  dup = xmlStrdup(name);
5881
677
  if (dup == NULL) {
5882
1
            xmlVErrMemory(ctxt);
5883
1
      return;
5884
1
  }
5885
676
  cur = dup;
5886
1.48k
  while (*cur != 0) {
5887
1.37k
      str = cur;
5888
7.99k
      while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
5889
1.37k
      save = *cur;
5890
1.37k
      *cur = 0;
5891
1.37k
      id = xmlGetID(ctxt->doc, str);
5892
1.37k
      if (id == NULL) {
5893
1.15k
    xmlErrValidNodeNr(ctxt, NULL, XML_DTD_UNKNOWN_ID,
5894
1.15k
     "attribute %s line %d references an unknown ID \"%s\"\n",
5895
1.15k
           ref->name, ref->lineno, str);
5896
1.15k
    ctxt->valid = 0;
5897
1.15k
      }
5898
1.37k
      if (save == 0)
5899
573
    break;
5900
804
      *cur = save;
5901
1.44k
      while (IS_BLANK_CH(*cur)) cur++;
5902
804
  }
5903
676
  xmlFree(dup);
5904
13.0k
    } else if (attr->atype == XML_ATTRIBUTE_IDREF) {
5905
7.32k
  id = xmlGetID(ctxt->doc, name);
5906
7.32k
  if (id == NULL) {
5907
5.69k
      xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
5908
5.69k
     "IDREF attribute %s references an unknown ID \"%s\"\n",
5909
5.69k
       attr->name, name, NULL);
5910
5.69k
      ctxt->valid = 0;
5911
5.69k
  }
5912
7.32k
    } else if (attr->atype == XML_ATTRIBUTE_IDREFS) {
5913
5.71k
  xmlChar *dup, *str = NULL, *cur, save;
5914
5915
5.71k
  dup = xmlStrdup(name);
5916
5.71k
  if (dup == NULL) {
5917
9
      xmlVErrMemory(ctxt);
5918
9
      ctxt->valid = 0;
5919
9
      return;
5920
9
  }
5921
5.70k
  cur = dup;
5922
30.6k
  while (*cur != 0) {
5923
28.7k
      str = cur;
5924
18.9M
      while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
5925
28.7k
      save = *cur;
5926
28.7k
      *cur = 0;
5927
28.7k
      id = xmlGetID(ctxt->doc, str);
5928
28.7k
      if (id == NULL) {
5929
28.1k
    xmlErrValidNode(ctxt, attr->parent, XML_DTD_UNKNOWN_ID,
5930
28.1k
     "IDREFS attribute %s references an unknown ID \"%s\"\n",
5931
28.1k
           attr->name, str, NULL);
5932
28.1k
    ctxt->valid = 0;
5933
28.1k
      }
5934
28.7k
      if (save == 0)
5935
3.81k
    break;
5936
24.9k
      *cur = save;
5937
207k
      while (IS_BLANK_CH(*cur)) cur++;
5938
24.9k
  }
5939
5.70k
  xmlFree(dup);
5940
5.70k
    }
5941
13.7k
}
5942
5943
/**
5944
 * @param data  Contents of current link
5945
 * @param user  Value supplied by the user
5946
 * @returns 0 to abort the walk or 1 to continue.
5947
 */
5948
static int
5949
xmlWalkValidateList(const void *data, void *user)
5950
13.7k
{
5951
13.7k
  xmlValidateMemoPtr memo = (xmlValidateMemoPtr)user;
5952
13.7k
  xmlValidateRef((xmlRefPtr)data, memo->ctxt, memo->name);
5953
13.7k
  return 1;
5954
13.7k
}
5955
5956
/**
5957
 * @param payload  list of references
5958
 * @param data  validation context
5959
 * @param name  name of ID we are searching for
5960
 */
5961
static void
5962
2.81k
xmlValidateCheckRefCallback(void *payload, void *data, const xmlChar *name) {
5963
2.81k
    xmlListPtr ref_list = (xmlListPtr) payload;
5964
2.81k
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
5965
2.81k
    xmlValidateMemo memo;
5966
5967
2.81k
    if (ref_list == NULL)
5968
0
  return;
5969
2.81k
    memo.ctxt = ctxt;
5970
2.81k
    memo.name = name;
5971
5972
2.81k
    xmlListWalk(ref_list, xmlWalkValidateList, &memo);
5973
5974
2.81k
}
5975
5976
/**
5977
 * Performs the final step of document validation once all the
5978
 * incremental validation steps have been completed.
5979
 *
5980
 * @deprecated Internal function, don't use.
5981
 *
5982
 * Performs the following checks described by the XML Rec:
5983
 *
5984
 * - Check all the IDREF/IDREFS attributes definition for validity.
5985
 *
5986
 * @param ctxt  the validation context
5987
 * @param doc  a document instance
5988
 * @returns 1 if valid or 0 otherwise.
5989
 */
5990
5991
int
5992
23.0k
xmlValidateDocumentFinal(xmlValidCtxt *ctxt, xmlDoc *doc) {
5993
23.0k
    xmlRefTablePtr table;
5994
23.0k
    xmlParserCtxtPtr pctxt = NULL;
5995
23.0k
    xmlParserInputPtr oldInput = NULL;
5996
5997
23.0k
    if (ctxt == NULL)
5998
19
        return(0);
5999
23.0k
    if (doc == NULL) {
6000
0
        xmlErrValid(ctxt, XML_DTD_NO_DOC,
6001
0
    "xmlValidateDocumentFinal: doc == NULL\n", NULL);
6002
0
  return(0);
6003
0
    }
6004
6005
    /*
6006
     * Check all the NOTATION/NOTATIONS attributes
6007
     */
6008
    /*
6009
     * Check all the ENTITY/ENTITIES attributes definition for validity
6010
     */
6011
    /*
6012
     * Check all the IDREF/IDREFS attributes definition for validity
6013
     */
6014
6015
    /*
6016
     * Don't print line numbers.
6017
     */
6018
23.0k
    if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
6019
15.2k
        pctxt = ctxt->userData;
6020
15.2k
        oldInput = pctxt->input;
6021
15.2k
        pctxt->input = NULL;
6022
15.2k
    }
6023
6024
23.0k
    table = (xmlRefTablePtr) doc->refs;
6025
23.0k
    ctxt->doc = doc;
6026
23.0k
    ctxt->valid = 1;
6027
23.0k
    xmlHashScan(table, xmlValidateCheckRefCallback, ctxt);
6028
6029
23.0k
    if (ctxt->flags & XML_VCTXT_USE_PCTXT)
6030
15.2k
        pctxt->input = oldInput;
6031
6032
23.0k
    return(ctxt->valid);
6033
23.0k
}
6034
6035
/**
6036
 * Try to validate the document against the DTD instance.
6037
 *
6038
 * Note that the internal subset (if present) is de-coupled
6039
 * (i.e. not used), which could cause problems if ID or IDREF
6040
 * attributes are present.
6041
 *
6042
 * @param ctxt  the validation context
6043
 * @param doc  a document instance
6044
 * @param dtd  a DTD instance
6045
 * @returns 1 if valid or 0 otherwise.
6046
 */
6047
6048
int
6049
9.59k
xmlValidateDtd(xmlValidCtxt *ctxt, xmlDoc *doc, xmlDtd *dtd) {
6050
9.59k
    int ret;
6051
9.59k
    xmlDtdPtr oldExt, oldInt;
6052
9.59k
    xmlNodePtr root;
6053
6054
9.59k
    if (dtd == NULL)
6055
1.19k
        return(0);
6056
8.39k
    if (doc == NULL)
6057
358
        return(0);
6058
6059
8.03k
    oldExt = doc->extSubset;
6060
8.03k
    oldInt = doc->intSubset;
6061
8.03k
    doc->extSubset = dtd;
6062
8.03k
    doc->intSubset = NULL;
6063
8.03k
    if (doc->ids != NULL) {
6064
196
        xmlFreeIDTable(doc->ids);
6065
196
        doc->ids = NULL;
6066
196
    }
6067
8.03k
    if (doc->refs != NULL) {
6068
200
        xmlFreeRefTable(doc->refs);
6069
200
        doc->refs = NULL;
6070
200
    }
6071
6072
8.03k
    ret = xmlValidateRoot(ctxt, doc);
6073
8.03k
    if (ret != 0) {
6074
7.41k
        root = xmlDocGetRootElement(doc);
6075
7.41k
        ret = xmlValidateElement(ctxt, doc, root);
6076
7.41k
        ret &= xmlValidateDocumentFinal(ctxt, doc);
6077
7.41k
    }
6078
6079
8.03k
    doc->extSubset = oldExt;
6080
8.03k
    doc->intSubset = oldInt;
6081
8.03k
    if (doc->ids != NULL) {
6082
527
        xmlFreeIDTable(doc->ids);
6083
527
        doc->ids = NULL;
6084
527
    }
6085
8.03k
    if (doc->refs != NULL) {
6086
375
        xmlFreeRefTable(doc->refs);
6087
375
        doc->refs = NULL;
6088
375
    }
6089
6090
8.03k
    return(ret);
6091
8.39k
}
6092
6093
/**
6094
 * Validate a document against a DTD.
6095
 *
6096
 * Like #xmlValidateDtd but uses the parser context's error handler.
6097
 *
6098
 * @since 2.14.0
6099
 *
6100
 * @param ctxt  a parser context
6101
 * @param doc  a document instance
6102
 * @param dtd  a dtd instance
6103
 * @returns 1 if valid or 0 otherwise.
6104
 */
6105
int
6106
0
xmlCtxtValidateDtd(xmlParserCtxt *ctxt, xmlDoc *doc, xmlDtd *dtd) {
6107
0
    if ((ctxt == NULL) || (ctxt->html))
6108
0
        return(0);
6109
6110
0
    xmlCtxtReset(ctxt);
6111
6112
0
    return(xmlValidateDtd(&ctxt->vctxt, doc, dtd));
6113
0
}
6114
6115
static void
6116
xmlValidateNotationCallback(void *payload, void *data,
6117
27.5k
                      const xmlChar *name ATTRIBUTE_UNUSED) {
6118
27.5k
    xmlEntityPtr cur = (xmlEntityPtr) payload;
6119
27.5k
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
6120
27.5k
    if (cur == NULL)
6121
0
  return;
6122
27.5k
    if (cur->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
6123
1.87k
  xmlChar *notation = cur->content;
6124
6125
1.87k
  if (notation != NULL) {
6126
1.58k
      int ret;
6127
6128
1.58k
      ret = xmlValidateNotationUse(ctxt, cur->doc, notation);
6129
1.58k
      if (ret != 1) {
6130
1.32k
    ctxt->valid = 0;
6131
1.32k
      }
6132
1.58k
  }
6133
1.87k
    }
6134
27.5k
}
6135
6136
static void
6137
xmlValidateAttributeCallback(void *payload, void *data,
6138
53.7k
                       const xmlChar *name ATTRIBUTE_UNUSED) {
6139
53.7k
    xmlAttributePtr cur = (xmlAttributePtr) payload;
6140
53.7k
    xmlValidCtxtPtr ctxt = (xmlValidCtxtPtr) data;
6141
53.7k
    xmlDocPtr doc;
6142
53.7k
    xmlElementPtr elem = NULL;
6143
6144
53.7k
    if (cur == NULL)
6145
0
  return;
6146
53.7k
    if (cur->atype == XML_ATTRIBUTE_NOTATION) {
6147
1.55k
        const xmlChar *elemLocalName;
6148
1.55k
        xmlChar *elemPrefix;
6149
6150
1.55k
  doc = cur->doc;
6151
1.55k
  if (cur->elem == NULL) {
6152
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
6153
0
       "xmlValidateAttributeCallback(%s): internal error\n",
6154
0
       (const char *) cur->name);
6155
0
      return;
6156
0
  }
6157
6158
1.55k
        elemLocalName = xmlSplitQName4(cur->elem, &elemPrefix);
6159
1.55k
        if (elemLocalName == NULL) {
6160
5
            xmlVErrMemory(ctxt);
6161
5
            return;
6162
5
        }
6163
6164
1.54k
  if ((doc != NULL) && (doc->intSubset != NULL))
6165
1.34k
      elem = xmlHashLookup2(doc->intSubset->elements,
6166
1.34k
                                  elemLocalName, elemPrefix);
6167
1.54k
  if ((elem == NULL) && (doc != NULL) && (doc->extSubset != NULL))
6168
799
      elem = xmlHashLookup2(doc->extSubset->elements,
6169
799
                                  elemLocalName, elemPrefix);
6170
1.54k
  if ((elem == NULL) && (cur->parent != NULL) &&
6171
201
      (cur->parent->type == XML_DTD_NODE))
6172
201
      elem = xmlHashLookup2(((xmlDtdPtr) cur->parent)->elements,
6173
201
                                  elemLocalName, elemPrefix);
6174
6175
1.54k
        xmlFree(elemPrefix);
6176
6177
1.54k
  if (elem == NULL) {
6178
195
      xmlErrValidNode(ctxt, NULL, XML_DTD_UNKNOWN_ELEM,
6179
195
       "attribute %s: could not find decl for element %s\n",
6180
195
       cur->name, cur->elem, NULL);
6181
195
      return;
6182
195
  }
6183
1.35k
  if (elem->etype == XML_ELEMENT_TYPE_EMPTY) {
6184
237
      xmlErrValidNode(ctxt, NULL, XML_DTD_EMPTY_NOTATION,
6185
237
       "NOTATION attribute %s declared for EMPTY element %s\n",
6186
237
       cur->name, cur->elem, NULL);
6187
237
      ctxt->valid = 0;
6188
237
  }
6189
1.35k
    }
6190
53.7k
}
6191
6192
/**
6193
 * Performs the final validation steps of DTD content once all the
6194
 * subsets have been parsed.
6195
 *
6196
 * @deprecated Internal function, don't use.
6197
 *
6198
 * Performs the following checks described by the XML Rec:
6199
 *
6200
 * - check that ENTITY and ENTITIES type attributes default or
6201
 *   possible values matches one of the defined entities.
6202
 * - check that NOTATION type attributes default or
6203
 *   possible values matches one of the defined notations.
6204
 *
6205
 * @param ctxt  the validation context
6206
 * @param doc  a document instance
6207
 * @returns 1 if valid or 0 if invalid and -1 if not well-formed.
6208
 */
6209
6210
int
6211
45.8k
xmlValidateDtdFinal(xmlValidCtxt *ctxt, xmlDoc *doc) {
6212
45.8k
    xmlDtdPtr dtd;
6213
45.8k
    xmlAttributeTablePtr table;
6214
45.8k
    xmlEntitiesTablePtr entities;
6215
6216
45.8k
    if ((doc == NULL) || (ctxt == NULL)) return(0);
6217
45.8k
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL))
6218
0
  return(0);
6219
45.8k
    ctxt->doc = doc;
6220
45.8k
    ctxt->valid = 1;
6221
45.8k
    dtd = doc->intSubset;
6222
45.8k
    if ((dtd != NULL) && (dtd->attributes != NULL)) {
6223
17.4k
  table = (xmlAttributeTablePtr) dtd->attributes;
6224
17.4k
  xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
6225
17.4k
    }
6226
45.8k
    if ((dtd != NULL) && (dtd->entities != NULL)) {
6227
15.3k
  entities = (xmlEntitiesTablePtr) dtd->entities;
6228
15.3k
  xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
6229
15.3k
    }
6230
45.8k
    dtd = doc->extSubset;
6231
45.8k
    if ((dtd != NULL) && (dtd->attributes != NULL)) {
6232
1.44k
  table = (xmlAttributeTablePtr) dtd->attributes;
6233
1.44k
  xmlHashScan(table, xmlValidateAttributeCallback, ctxt);
6234
1.44k
    }
6235
45.8k
    if ((dtd != NULL) && (dtd->entities != NULL)) {
6236
1.20k
  entities = (xmlEntitiesTablePtr) dtd->entities;
6237
1.20k
  xmlHashScan(entities, xmlValidateNotationCallback, ctxt);
6238
1.20k
    }
6239
45.8k
    return(ctxt->valid);
6240
45.8k
}
6241
6242
/**
6243
 * Validate a document.
6244
 *
6245
 * @param ctxt  parser context (optional)
6246
 * @param vctxt  validation context (optional)
6247
 * @param doc  document
6248
 * @returns 1 if valid or 0 otherwise.
6249
 */
6250
static int
6251
xmlValidateDocumentInternal(xmlParserCtxtPtr ctxt, xmlValidCtxtPtr vctxt,
6252
33.7k
                            xmlDocPtr doc) {
6253
33.7k
    int ret;
6254
33.7k
    xmlNodePtr root;
6255
6256
33.7k
    if (doc == NULL)
6257
0
        return(0);
6258
33.7k
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
6259
5.96k
        xmlErrValid(vctxt, XML_DTD_NO_DTD,
6260
5.96k
              "no DTD found!\n", NULL);
6261
5.96k
  return(0);
6262
5.96k
    }
6263
6264
27.8k
    if ((doc->intSubset != NULL) && ((doc->intSubset->SystemID != NULL) ||
6265
18.3k
  (doc->intSubset->ExternalID != NULL)) && (doc->extSubset == NULL)) {
6266
16.3k
  xmlChar *sysID = NULL;
6267
6268
16.3k
  if (doc->intSubset->SystemID != NULL) {
6269
16.1k
            int res;
6270
6271
16.1k
            res = xmlBuildURISafe(doc->intSubset->SystemID, doc->URL, &sysID);
6272
16.1k
            if (res < 0) {
6273
113
                xmlVErrMemory(vctxt);
6274
113
                return 0;
6275
16.0k
            } else if (res != 0) {
6276
2.33k
                xmlErrValid(vctxt, XML_DTD_LOAD_ERROR,
6277
2.33k
      "Could not build URI for external subset \"%s\"\n",
6278
2.33k
      (const char *) doc->intSubset->SystemID);
6279
2.33k
    return 0;
6280
2.33k
      }
6281
16.1k
  }
6282
6283
13.9k
        if (ctxt != NULL) {
6284
1.64k
            xmlParserInputPtr input;
6285
6286
1.64k
            input = xmlLoadResource(ctxt, (const char *) sysID,
6287
1.64k
                    (const char *) doc->intSubset->ExternalID,
6288
1.64k
                    XML_RESOURCE_DTD);
6289
1.64k
            if (input == NULL) {
6290
962
                xmlFree(sysID);
6291
962
                return 0;
6292
962
            }
6293
6294
681
            doc->extSubset = xmlCtxtParseDtd(ctxt, input,
6295
681
                                             doc->intSubset->ExternalID,
6296
681
                                             sysID);
6297
12.2k
        } else {
6298
12.2k
            doc->extSubset = xmlParseDTD(doc->intSubset->ExternalID, sysID);
6299
12.2k
        }
6300
6301
12.9k
  if (sysID != NULL)
6302
12.7k
      xmlFree(sysID);
6303
12.9k
        if (doc->extSubset == NULL) {
6304
12.8k
      if (doc->intSubset->SystemID != NULL) {
6305
12.6k
    xmlErrValid(vctxt, XML_DTD_LOAD_ERROR,
6306
12.6k
           "Could not load the external subset \"%s\"\n",
6307
12.6k
           (const char *) doc->intSubset->SystemID);
6308
12.6k
      } else {
6309
231
    xmlErrValid(vctxt, XML_DTD_LOAD_ERROR,
6310
231
           "Could not load the external subset \"%s\"\n",
6311
231
           (const char *) doc->intSubset->ExternalID);
6312
231
      }
6313
12.8k
      return(0);
6314
12.8k
  }
6315
12.9k
    }
6316
6317
11.5k
    if (doc->ids != NULL) {
6318
512
          xmlFreeIDTable(doc->ids);
6319
512
          doc->ids = NULL;
6320
512
    }
6321
11.5k
    if (doc->refs != NULL) {
6322
179
          xmlFreeRefTable(doc->refs);
6323
179
          doc->refs = NULL;
6324
179
    }
6325
11.5k
    ret = xmlValidateDtdFinal(vctxt, doc);
6326
11.5k
    if (!xmlValidateRoot(vctxt, doc)) return(0);
6327
6328
4.34k
    root = xmlDocGetRootElement(doc);
6329
4.34k
    ret &= xmlValidateElement(vctxt, doc, root);
6330
4.34k
    ret &= xmlValidateDocumentFinal(vctxt, doc);
6331
4.34k
    return(ret);
6332
11.5k
}
6333
6334
/**
6335
 * Try to validate the document instance.
6336
 *
6337
 * @deprecated This function can't report malloc or other failures.
6338
 * Use #xmlCtxtValidateDocument.
6339
 *
6340
 * Performs the all the checks described by the XML Rec,
6341
 * i.e. validates the internal and external subset (if present)
6342
 * and validates the document tree.
6343
 *
6344
 * @param vctxt  the validation context
6345
 * @param doc  a document instance
6346
 * @returns 1 if valid or 0 otherwise.
6347
 */
6348
int
6349
18.7k
xmlValidateDocument(xmlValidCtxt *vctxt, xmlDoc *doc) {
6350
18.7k
    return(xmlValidateDocumentInternal(NULL, vctxt, doc));
6351
18.7k
}
6352
6353
/**
6354
 * Validate a document.
6355
 *
6356
 * Like #xmlValidateDocument but uses the parser context's error handler.
6357
 *
6358
 * Option XML_PARSE_DTDLOAD should be enabled in the parser context
6359
 * to make external entities work.
6360
 *
6361
 * @since 2.14.0
6362
 *
6363
 * @param ctxt  a parser context
6364
 * @param doc  a document instance
6365
 * @returns 1 if valid or 0 otherwise.
6366
 */
6367
int
6368
15.0k
xmlCtxtValidateDocument(xmlParserCtxt *ctxt, xmlDoc *doc) {
6369
15.0k
    if ((ctxt == NULL) || (ctxt->html))
6370
0
        return(0);
6371
6372
15.0k
    xmlCtxtReset(ctxt);
6373
6374
15.0k
    return(xmlValidateDocumentInternal(ctxt, &ctxt->vctxt, doc));
6375
15.0k
}
6376
6377
/************************************************************************
6378
 *                  *
6379
 *    Routines for dynamic validation editing     *
6380
 *                  *
6381
 ************************************************************************/
6382
6383
/**
6384
 * Build/extend a list of  potential children allowed by the content tree
6385
 *
6386
 * @param ctree  an element content tree
6387
 * @param names  an array to store the list of child names
6388
 * @param len  a pointer to the number of element in the list
6389
 * @param max  the size of the array
6390
 * @returns the number of element in the list, or -1 in case of error.
6391
 */
6392
6393
int
6394
xmlValidGetPotentialChildren(xmlElementContent *ctree,
6395
                             const xmlChar **names,
6396
0
                             int *len, int max) {
6397
0
    int i;
6398
6399
0
    if ((ctree == NULL) || (names == NULL) || (len == NULL))
6400
0
        return(-1);
6401
0
    if (*len >= max) return(*len);
6402
6403
0
    switch (ctree->type) {
6404
0
  case XML_ELEMENT_CONTENT_PCDATA:
6405
0
      for (i = 0; i < *len;i++)
6406
0
    if (xmlStrEqual(BAD_CAST "#PCDATA", names[i])) return(*len);
6407
0
      names[(*len)++] = BAD_CAST "#PCDATA";
6408
0
      break;
6409
0
  case XML_ELEMENT_CONTENT_ELEMENT:
6410
0
      for (i = 0; i < *len;i++)
6411
0
    if (xmlStrEqual(ctree->name, names[i])) return(*len);
6412
0
      names[(*len)++] = ctree->name;
6413
0
      break;
6414
0
  case XML_ELEMENT_CONTENT_SEQ:
6415
0
      xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6416
0
      xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6417
0
      break;
6418
0
  case XML_ELEMENT_CONTENT_OR:
6419
0
      xmlValidGetPotentialChildren(ctree->c1, names, len, max);
6420
0
      xmlValidGetPotentialChildren(ctree->c2, names, len, max);
6421
0
      break;
6422
0
   }
6423
6424
0
   return(*len);
6425
0
}
6426
6427
/*
6428
 * Dummy function to suppress messages while we try out valid elements
6429
 */
6430
static void xmlNoValidityErr(void *ctx ATTRIBUTE_UNUSED,
6431
0
                                const char *msg ATTRIBUTE_UNUSED, ...) {
6432
0
}
6433
6434
/**
6435
 * This function returns the list of authorized children to insert
6436
 * within an existing tree while respecting the validity constraints
6437
 * forced by the Dtd. The insertion point is defined using `prev` and
6438
 * `next` in the following ways:
6439
 *  to insert before 'node': xmlValidGetValidElements(node->prev, node, ...
6440
 *  to insert next 'node': xmlValidGetValidElements(node, node->next, ...
6441
 *  to replace 'node': xmlValidGetValidElements(node->prev, node->next, ...
6442
 *  to prepend a child to 'node': xmlValidGetValidElements(NULL, node->childs,
6443
 *  to append a child to 'node': xmlValidGetValidElements(node->last, NULL, ...
6444
 *
6445
 * pointers to the element names are inserted at the beginning of the array
6446
 * and do not need to be freed.
6447
 *
6448
 * @deprecated This feature will be removed.
6449
 *
6450
 * @param prev  an element to insert after
6451
 * @param next  an element to insert next
6452
 * @param names  an array to store the list of child names
6453
 * @param max  the size of the array
6454
 * @returns the number of element in the list, or -1 in case of error. If
6455
 *    the function returns the value `max` the caller is invited to grow the
6456
 *    receiving array and retry.
6457
 */
6458
6459
int
6460
xmlValidGetValidElements(xmlNode *prev, xmlNode *next, const xmlChar **names,
6461
3.71k
                         int max) {
6462
3.71k
    xmlValidCtxt vctxt;
6463
3.71k
    int nb_valid_elements = 0;
6464
3.71k
    const xmlChar *elements[256]={0};
6465
3.71k
    int nb_elements = 0, i;
6466
3.71k
    const xmlChar *name;
6467
6468
3.71k
    xmlNode *ref_node;
6469
3.71k
    xmlNode *parent;
6470
3.71k
    xmlNode *test_node;
6471
6472
3.71k
    xmlNode *prev_next;
6473
3.71k
    xmlNode *next_prev;
6474
3.71k
    xmlNode *parent_childs;
6475
3.71k
    xmlNode *parent_last;
6476
6477
3.71k
    xmlElement *element_desc;
6478
6479
3.71k
    if (prev == NULL && next == NULL)
6480
0
        return(-1);
6481
6482
3.71k
    if (names == NULL) return(-1);
6483
3.71k
    if (max <= 0) return(-1);
6484
6485
3.71k
    memset(&vctxt, 0, sizeof (xmlValidCtxt));
6486
3.71k
    vctxt.error = xmlNoValidityErr; /* this suppresses err/warn output */
6487
6488
3.71k
    nb_valid_elements = 0;
6489
3.71k
    ref_node = prev ? prev : next;
6490
3.71k
    parent = ref_node->parent;
6491
6492
    /*
6493
     * Retrieves the parent element declaration
6494
     */
6495
3.71k
    element_desc = xmlGetDtdElementDesc(parent->doc->intSubset,
6496
3.71k
                                         parent->name);
6497
3.71k
    if ((element_desc == NULL) && (parent->doc->extSubset != NULL))
6498
0
        element_desc = xmlGetDtdElementDesc(parent->doc->extSubset,
6499
0
                                             parent->name);
6500
3.71k
    if (element_desc == NULL) return(-1);
6501
6502
    /*
6503
     * Do a backup of the current tree structure
6504
     */
6505
0
    prev_next = prev ? prev->next : NULL;
6506
0
    next_prev = next ? next->prev : NULL;
6507
0
    parent_childs = parent->children;
6508
0
    parent_last = parent->last;
6509
6510
    /*
6511
     * Creates a dummy node and insert it into the tree
6512
     */
6513
0
    test_node = xmlNewDocNode (ref_node->doc, NULL, BAD_CAST "<!dummy?>", NULL);
6514
0
    if (test_node == NULL)
6515
0
        return(-1);
6516
6517
0
    test_node->parent = parent;
6518
0
    test_node->prev = prev;
6519
0
    test_node->next = next;
6520
0
    name = test_node->name;
6521
6522
0
    if (prev) prev->next = test_node;
6523
0
    else parent->children = test_node;
6524
6525
0
    if (next) next->prev = test_node;
6526
0
    else parent->last = test_node;
6527
6528
    /*
6529
     * Insert each potential child node and check if the parent is
6530
     * still valid
6531
     */
6532
0
    nb_elements = xmlValidGetPotentialChildren(element_desc->content,
6533
0
           elements, &nb_elements, 256);
6534
6535
0
    for (i = 0;i < nb_elements;i++) {
6536
0
  test_node->name = elements[i];
6537
0
  if (xmlValidateOneElement(&vctxt, parent->doc, parent)) {
6538
0
      int j;
6539
6540
0
      for (j = 0; j < nb_valid_elements;j++)
6541
0
    if (xmlStrEqual(elements[i], names[j])) break;
6542
0
      names[nb_valid_elements++] = elements[i];
6543
0
      if (nb_valid_elements >= max) break;
6544
0
  }
6545
0
    }
6546
6547
    /*
6548
     * Restore the tree structure
6549
     */
6550
0
    if (prev) prev->next = prev_next;
6551
0
    if (next) next->prev = next_prev;
6552
0
    parent->children = parent_childs;
6553
0
    parent->last = parent_last;
6554
6555
    /*
6556
     * Free up the dummy node
6557
     */
6558
0
    test_node->name = name;
6559
0
    xmlFreeNode(test_node);
6560
6561
0
    return(nb_valid_elements);
6562
0
}
6563
#endif /* LIBXML_VALID_ENABLED */