Coverage Report

Created: 2025-07-01 06:27

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