Coverage Report

Created: 2025-07-18 06:56

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