Coverage Report

Created: 2024-02-11 06:24

/src/libxml2/valid.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * valid.c : part of the code use to do the DTD handling and the validity
3
 *           checking
4
 *
5
 * See Copyright for the status of this software.
6
 *
7
 * daniel@veillard.com
8
 */
9
10
#define IN_LIBXML
11
#include "libxml.h"
12
13
#include <string.h>
14
#include <stdlib.h>
15
16
#include <libxml/xmlmemory.h>
17
#include <libxml/hash.h>
18
#include <libxml/uri.h>
19
#include <libxml/valid.h>
20
#include <libxml/parser.h>
21
#include <libxml/parserInternals.h>
22
#include <libxml/xmlerror.h>
23
#include <libxml/list.h>
24
#include <libxml/xmlsave.h>
25
26
#include "private/error.h"
27
#include "private/parser.h"
28
#include "private/regexp.h"
29
#include "private/save.h"
30
#include "private/tree.h"
31
32
static xmlElementPtr
33
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name);
34
35
#ifdef LIBXML_VALID_ENABLED
36
static int
37
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
38
                                  const xmlChar *value);
39
#endif
40
/************************************************************************
41
 *                  *
42
 *      Error handling routines       *
43
 *                  *
44
 ************************************************************************/
45
46
/**
47
 * xmlVErrMemory:
48
 * @ctxt:  an XML validation parser context
49
 * @extra:  extra information
50
 *
51
 * Handle an out of memory error
52
 */
53
static void
54
xmlVErrMemory(xmlValidCtxtPtr ctxt)
55
4.06k
{
56
4.06k
    if (ctxt != NULL) {
57
4.06k
        if (ctxt->flags & XML_VCTXT_USE_PCTXT) {
58
3.19k
            xmlCtxtErrMemory(ctxt->userData);
59
3.19k
        } else {
60
872
            xmlRaiseMemoryError(NULL, ctxt->error, ctxt->userData,
61
872
                                XML_FROM_VALID, NULL);
62
872
        }
63
4.06k
    } else {
64
0
        xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_VALID, NULL);
65
0
    }
66
4.06k
}
67
68
static void
69
xmlDoErrValid(xmlValidCtxtPtr ctxt, xmlNodePtr node,
70
              xmlParserErrors code, int level,
71
              const xmlChar *str1, const xmlChar *str2, const xmlChar *str3,
72
              int int1,
73
3.45M
              const char *msg, ...) {
74
3.45M
    xmlParserCtxtPtr pctxt = NULL;
75
3.45M
    va_list ap;
76
77
3.45M
    if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT))
78
2.57M
        pctxt = ctxt->userData;
79
80
3.45M
    va_start(ap, msg);
81
3.45M
    if (pctxt != NULL) {
82
2.57M
        xmlCtxtVErr(pctxt, node, XML_FROM_VALID, code, level,
83
2.57M
                    str1, str2, str3, int1, msg, ap);
84
2.57M
    } else {
85
875k
        xmlGenericErrorFunc channel = NULL;
86
875k
        void *data = NULL;
87
875k
        int res;
88
89
875k
        if (ctxt != NULL) {
90
874k
            channel = ctxt->error;
91
874k
            data = ctxt->userData;
92
874k
        }
93
875k
        res = xmlVRaiseError(NULL, channel, data, NULL, node,
94
875k
                             XML_FROM_VALID, code, level, NULL, 0,
95
875k
                             (const char *) str1, (const char *) str2,
96
875k
                             (const char *) str2, int1, 0,
97
875k
                             msg, ap);
98
875k
        if (res < 0)
99
509
            xmlVErrMemory(ctxt);
100
875k
    }
101
3.45M
    va_end(ap);
102
3.45M
}
103
104
/**
105
 * xmlErrValid:
106
 * @ctxt:  an XML validation parser context
107
 * @error:  the error number
108
 * @extra:  extra information
109
 *
110
 * Handle a validation error
111
 */
112
static void LIBXML_ATTR_FORMAT(3,0)
113
xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
114
            const char *msg, const char *extra)
115
12.6k
{
116
12.6k
    xmlDoErrValid(ctxt, NULL, error, XML_ERR_ERROR, (const xmlChar *) extra,
117
12.6k
                  NULL, NULL, 0, msg, extra);
118
12.6k
}
119
120
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
121
/**
122
 * xmlErrValidNode:
123
 * @ctxt:  an XML validation parser context
124
 * @node:  the node raising the error
125
 * @error:  the error number
126
 * @str1:  extra information
127
 * @str2:  extra information
128
 * @str3:  extra information
129
 *
130
 * Handle a validation error, provide contextual information
131
 */
132
static void LIBXML_ATTR_FORMAT(4,0)
133
xmlErrValidNode(xmlValidCtxtPtr ctxt,
134
                xmlNodePtr node, xmlParserErrors error,
135
                const char *msg, const xmlChar * str1,
136
                const xmlChar * str2, const xmlChar * str3)
137
3.06M
{
138
3.06M
    xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str2, str3, 0,
139
3.06M
                  msg, str1, str2, str3);
140
3.06M
}
141
#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
142
143
#ifdef LIBXML_VALID_ENABLED
144
/**
145
 * xmlErrValidNodeNr:
146
 * @ctxt:  an XML validation parser context
147
 * @node:  the node raising the error
148
 * @error:  the error number
149
 * @str1:  extra information
150
 * @int2:  extra information
151
 * @str3:  extra information
152
 *
153
 * Handle a validation error, provide contextual information
154
 */
155
static void LIBXML_ATTR_FORMAT(4,0)
156
xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
157
                xmlNodePtr node, xmlParserErrors error,
158
                const char *msg, const xmlChar * str1,
159
                int int2, const xmlChar * str3)
160
124k
{
161
124k
    xmlDoErrValid(ctxt, node, error, XML_ERR_ERROR, str1, str3, NULL, int2,
162
124k
                  msg, str1, int2, str3);
163
124k
}
164
165
/**
166
 * xmlErrValidWarning:
167
 * @ctxt:  an XML validation parser context
168
 * @node:  the node raising the error
169
 * @error:  the error number
170
 * @str1:  extra information
171
 * @str2:  extra information
172
 * @str3:  extra information
173
 *
174
 * Handle a validation error, provide contextual information
175
 */
176
static void LIBXML_ATTR_FORMAT(4,0)
177
xmlErrValidWarning(xmlValidCtxtPtr ctxt,
178
                xmlNodePtr node, xmlParserErrors error,
179
                const char *msg, const xmlChar * str1,
180
                const xmlChar * str2, const xmlChar * str3)
181
257k
{
182
257k
    xmlDoErrValid(ctxt, node, error, XML_ERR_WARNING, str1, str2, str3, 0,
183
257k
                  msg, str1, str2, str3);
184
257k
}
185
186
187
188
#ifdef LIBXML_REGEXP_ENABLED
189
/*
190
 * If regexp are enabled we can do continuous validation without the
191
 * need of a tree to validate the content model. this is done in each
192
 * callbacks.
193
 * Each xmlValidState represent the validation state associated to the
194
 * set of nodes currently open from the document root to the current element.
195
 */
196
197
198
typedef struct _xmlValidState {
199
    xmlElementPtr  elemDecl;  /* pointer to the content model */
200
    xmlNodePtr           node;    /* pointer to the current node */
201
    xmlRegExecCtxtPtr    exec;    /* regexp runtime */
202
} _xmlValidState;
203
204
205
static int
206
369k
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
207
369k
    if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
208
3.57k
  ctxt->vstateMax = 10;
209
3.57k
  ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
210
3.57k
                  sizeof(ctxt->vstateTab[0]));
211
3.57k
        if (ctxt->vstateTab == NULL) {
212
31
      xmlVErrMemory(ctxt);
213
31
      return(-1);
214
31
  }
215
3.57k
    }
216
217
369k
    if (ctxt->vstateNr >= ctxt->vstateMax) {
218
2.66k
        xmlValidState *tmp;
219
220
2.66k
  tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
221
2.66k
               2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
222
2.66k
        if (tmp == NULL) {
223
6
      xmlVErrMemory(ctxt);
224
6
      return(-1);
225
6
  }
226
2.66k
  ctxt->vstateMax *= 2;
227
2.66k
  ctxt->vstateTab = tmp;
228
2.66k
    }
229
369k
    ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
230
369k
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
231
369k
    ctxt->vstateTab[ctxt->vstateNr].node = node;
232
369k
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
233
71.5k
  if (elemDecl->contModel == NULL)
234
1.23k
      xmlValidBuildContentModel(ctxt, elemDecl);
235
71.5k
  if (elemDecl->contModel != NULL) {
236
71.4k
      ctxt->vstateTab[ctxt->vstateNr].exec =
237
71.4k
    xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
238
71.4k
            if (ctxt->vstateTab[ctxt->vstateNr].exec == NULL) {
239
52
                xmlVErrMemory(ctxt);
240
52
                return(-1);
241
52
            }
242
71.4k
  } else {
243
150
      ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
244
150
      xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
245
150
                      XML_ERR_INTERNAL_ERROR,
246
150
          "Failed to build content model regexp for %s\n",
247
150
          node->name, NULL, NULL);
248
150
  }
249
71.5k
    }
250
369k
    return(ctxt->vstateNr++);
251
369k
}
252
253
static int
254
369k
vstateVPop(xmlValidCtxtPtr ctxt) {
255
369k
    xmlElementPtr elemDecl;
256
257
369k
    if (ctxt->vstateNr < 1) return(-1);
258
369k
    ctxt->vstateNr--;
259
369k
    elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
260
369k
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
261
369k
    ctxt->vstateTab[ctxt->vstateNr].node = NULL;
262
369k
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
263
71.5k
  xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
264
71.5k
    }
265
369k
    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
266
369k
    if (ctxt->vstateNr >= 1)
267
365k
  ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
268
3.70k
    else
269
3.70k
  ctxt->vstate = NULL;
270
369k
    return(ctxt->vstateNr);
271
369k
}
272
273
#else /* not LIBXML_REGEXP_ENABLED */
274
/*
275
 * If regexp are not enabled, it uses a home made algorithm less
276
 * complex and easier to
277
 * debug/maintain than a generic NFA -> DFA state based algo. The
278
 * only restriction is on the deepness of the tree limited by the
279
 * size of the occurs bitfield
280
 *
281
 * this is the content of a saved state for rollbacks
282
 */
283
284
#define ROLLBACK_OR 0
285
#define ROLLBACK_PARENT 1
286
287
typedef struct _xmlValidState {
288
    xmlElementContentPtr cont;  /* pointer to the content model subtree */
289
    xmlNodePtr           node;  /* pointer to the current node in the list */
290
    long                 occurs;/* bitfield for multiple occurrences */
291
    unsigned char        depth; /* current depth in the overall tree */
292
    unsigned char        state; /* ROLLBACK_XXX */
293
} _xmlValidState;
294
295
#define MAX_RECURSE 25000
296
#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
297
#define CONT ctxt->vstate->cont
298
#define NODE ctxt->vstate->node
299
#define DEPTH ctxt->vstate->depth
300
#define OCCURS ctxt->vstate->occurs
301
#define STATE ctxt->vstate->state
302
303
#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
304
#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
305
306
#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
307
#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
308
309
static int
310
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
311
      xmlNodePtr node, unsigned char depth, long occurs,
312
      unsigned char state) {
313
    int i = ctxt->vstateNr - 1;
314
315
    if (ctxt->vstateNr > MAX_RECURSE) {
316
  return(-1);
317
    }
318
    if (ctxt->vstateTab == NULL) {
319
  ctxt->vstateMax = 8;
320
  ctxt->vstateTab = (xmlValidState *) xmlMalloc(
321
         ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
322
  if (ctxt->vstateTab == NULL) {
323
      xmlVErrMemory(ctxt);
324
      return(-1);
325
  }
326
    }
327
    if (ctxt->vstateNr >= ctxt->vstateMax) {
328
        xmlValidState *tmp;
329
330
        tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
331
               2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
332
        if (tmp == NULL) {
333
      xmlVErrMemory(ctxt);
334
      return(-1);
335
  }
336
  ctxt->vstateMax *= 2;
337
  ctxt->vstateTab = tmp;
338
  ctxt->vstate = &ctxt->vstateTab[0];
339
    }
340
    /*
341
     * Don't push on the stack a state already here
342
     */
343
    if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
344
  (ctxt->vstateTab[i].node == node) &&
345
  (ctxt->vstateTab[i].depth == depth) &&
346
  (ctxt->vstateTab[i].occurs == occurs) &&
347
  (ctxt->vstateTab[i].state == state))
348
  return(ctxt->vstateNr);
349
    ctxt->vstateTab[ctxt->vstateNr].cont = cont;
350
    ctxt->vstateTab[ctxt->vstateNr].node = node;
351
    ctxt->vstateTab[ctxt->vstateNr].depth = depth;
352
    ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
353
    ctxt->vstateTab[ctxt->vstateNr].state = state;
354
    return(ctxt->vstateNr++);
355
}
356
357
static int
358
vstateVPop(xmlValidCtxtPtr ctxt) {
359
    if (ctxt->vstateNr <= 1) return(-1);
360
    ctxt->vstateNr--;
361
    ctxt->vstate = &ctxt->vstateTab[0];
362
    ctxt->vstate->cont =  ctxt->vstateTab[ctxt->vstateNr].cont;
363
    ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
364
    ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
365
    ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
366
    ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
367
    return(ctxt->vstateNr);
368
}
369
370
#endif /* LIBXML_REGEXP_ENABLED */
371
372
static int
373
nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
374
36.2k
{
375
36.2k
    if (ctxt->nodeMax <= 0) {
376
1.68k
        ctxt->nodeMax = 4;
377
1.68k
        ctxt->nodeTab =
378
1.68k
            (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
379
1.68k
                                     sizeof(ctxt->nodeTab[0]));
380
1.68k
        if (ctxt->nodeTab == NULL) {
381
7
      xmlVErrMemory(ctxt);
382
7
            ctxt->nodeMax = 0;
383
7
            return (0);
384
7
        }
385
1.68k
    }
386
36.2k
    if (ctxt->nodeNr >= ctxt->nodeMax) {
387
6
        xmlNodePtr *tmp;
388
6
        tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
389
6
            ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
390
6
        if (tmp == NULL) {
391
1
      xmlVErrMemory(ctxt);
392
1
            return (0);
393
1
        }
394
5
        ctxt->nodeMax *= 2;
395
5
  ctxt->nodeTab = tmp;
396
5
    }
397
36.2k
    ctxt->nodeTab[ctxt->nodeNr] = value;
398
36.2k
    ctxt->node = value;
399
36.2k
    return (ctxt->nodeNr++);
400
36.2k
}
401
static xmlNodePtr
402
nodeVPop(xmlValidCtxtPtr ctxt)
403
110k
{
404
110k
    xmlNodePtr ret;
405
406
110k
    if (ctxt->nodeNr <= 0)
407
74.5k
        return (NULL);
408
36.1k
    ctxt->nodeNr--;
409
36.1k
    if (ctxt->nodeNr > 0)
410
34.2k
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
411
1.88k
    else
412
1.88k
        ctxt->node = NULL;
413
36.1k
    ret = ctxt->nodeTab[ctxt->nodeNr];
414
36.1k
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
415
36.1k
    return (ret);
416
110k
}
417
418
/* TODO: use hash table for accesses to elem and attribute definitions */
419
420
421
#define CHECK_DTD           \
422
2.31M
   if (doc == NULL) return(0);         \
423
2.31M
   else if ((doc->intSubset == NULL) &&       \
424
2.31M
      (doc->extSubset == NULL)) return(0)
425
426
#ifdef LIBXML_REGEXP_ENABLED
427
428
/************************************************************************
429
 *                  *
430
 *    Content model validation based on the regexps   *
431
 *                  *
432
 ************************************************************************/
433
434
/**
435
 * xmlValidBuildAContentModel:
436
 * @content:  the content model
437
 * @ctxt:  the schema parser context
438
 * @name:  the element name whose content is being built
439
 *
440
 * Generate the automata sequence needed for that type
441
 *
442
 * Returns 1 if successful or 0 in case of error.
443
 */
444
static int
445
xmlValidBuildAContentModel(xmlElementContentPtr content,
446
               xmlValidCtxtPtr ctxt,
447
159k
               const xmlChar *name) {
448
159k
    if (content == NULL) {
449
0
  xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
450
0
      "Found NULL content in content model of %s\n",
451
0
      name, NULL, NULL);
452
0
  return(0);
453
0
    }
454
159k
    switch (content->type) {
455
0
  case XML_ELEMENT_CONTENT_PCDATA:
456
0
      xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
457
0
          "Found PCDATA in content model of %s\n",
458
0
                name, NULL, NULL);
459
0
      return(0);
460
0
      break;
461
140k
  case XML_ELEMENT_CONTENT_ELEMENT: {
462
140k
      xmlAutomataStatePtr oldstate = ctxt->state;
463
140k
      xmlChar fn[50];
464
140k
      xmlChar *fullname;
465
466
140k
      fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
467
140k
      if (fullname == NULL) {
468
12
          xmlVErrMemory(ctxt);
469
12
    return(0);
470
12
      }
471
472
140k
      switch (content->ocur) {
473
119k
    case XML_ELEMENT_CONTENT_ONCE:
474
119k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
475
119k
          ctxt->state, NULL, fullname, NULL);
476
119k
        break;
477
10.7k
    case XML_ELEMENT_CONTENT_OPT:
478
10.7k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
479
10.7k
          ctxt->state, NULL, fullname, NULL);
480
10.7k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
481
10.7k
        break;
482
4.01k
    case XML_ELEMENT_CONTENT_PLUS:
483
4.01k
        ctxt->state = xmlAutomataNewTransition(ctxt->am,
484
4.01k
          ctxt->state, NULL, fullname, NULL);
485
4.01k
        xmlAutomataNewTransition(ctxt->am, ctxt->state,
486
4.01k
                           ctxt->state, fullname, NULL);
487
4.01k
        break;
488
6.04k
    case XML_ELEMENT_CONTENT_MULT:
489
6.04k
        ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
490
6.04k
              ctxt->state, NULL);
491
6.04k
        xmlAutomataNewTransition(ctxt->am,
492
6.04k
          ctxt->state, ctxt->state, fullname, NULL);
493
6.04k
        break;
494
140k
      }
495
140k
      if ((fullname != fn) && (fullname != content->name))
496
5.16k
    xmlFree(fullname);
497
140k
      break;
498
140k
  }
499
5.68k
  case XML_ELEMENT_CONTENT_SEQ: {
500
5.68k
      xmlAutomataStatePtr oldstate, oldend;
501
5.68k
      xmlElementContentOccur ocur;
502
503
      /*
504
       * Simply iterate over the content
505
       */
506
5.68k
      oldstate = ctxt->state;
507
5.68k
      ocur = content->ocur;
508
5.68k
      if (ocur != XML_ELEMENT_CONTENT_ONCE) {
509
4.39k
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
510
4.39k
    oldstate = ctxt->state;
511
4.39k
      }
512
71.1k
      do {
513
71.1k
    xmlValidBuildAContentModel(content->c1, ctxt, name);
514
71.1k
    content = content->c2;
515
71.1k
      } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
516
71.1k
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
517
5.68k
      xmlValidBuildAContentModel(content, ctxt, name);
518
5.68k
      oldend = ctxt->state;
519
5.68k
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
520
5.68k
      switch (ocur) {
521
1.29k
    case XML_ELEMENT_CONTENT_ONCE:
522
1.29k
        break;
523
995
    case XML_ELEMENT_CONTENT_OPT:
524
995
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
525
995
        break;
526
2.84k
    case XML_ELEMENT_CONTENT_MULT:
527
2.84k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
528
2.84k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
529
2.84k
        break;
530
558
    case XML_ELEMENT_CONTENT_PLUS:
531
558
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
532
558
        break;
533
5.68k
      }
534
5.68k
      break;
535
5.68k
  }
536
13.0k
  case XML_ELEMENT_CONTENT_OR: {
537
13.0k
      xmlAutomataStatePtr oldstate, oldend;
538
13.0k
      xmlElementContentOccur ocur;
539
540
13.0k
      ocur = content->ocur;
541
13.0k
      if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
542
13.0k
    (ocur == XML_ELEMENT_CONTENT_MULT)) {
543
3.72k
    ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
544
3.72k
      ctxt->state, NULL);
545
3.72k
      }
546
13.0k
      oldstate = ctxt->state;
547
13.0k
      oldend = xmlAutomataNewState(ctxt->am);
548
549
      /*
550
       * iterate over the subtypes and remerge the end with an
551
       * epsilon transition
552
       */
553
63.5k
      do {
554
63.5k
    ctxt->state = oldstate;
555
63.5k
    xmlValidBuildAContentModel(content->c1, ctxt, name);
556
63.5k
    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
557
63.5k
    content = content->c2;
558
63.5k
      } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
559
63.5k
         (content->ocur == XML_ELEMENT_CONTENT_ONCE));
560
13.0k
      ctxt->state = oldstate;
561
13.0k
      xmlValidBuildAContentModel(content, ctxt, name);
562
13.0k
      xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
563
13.0k
      ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
564
13.0k
      switch (ocur) {
565
1.55k
    case XML_ELEMENT_CONTENT_ONCE:
566
1.55k
        break;
567
7.81k
    case XML_ELEMENT_CONTENT_OPT:
568
7.81k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
569
7.81k
        break;
570
2.05k
    case XML_ELEMENT_CONTENT_MULT:
571
2.05k
        xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
572
2.05k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
573
2.05k
        break;
574
1.67k
    case XML_ELEMENT_CONTENT_PLUS:
575
1.67k
        xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
576
1.67k
        break;
577
13.0k
      }
578
13.0k
      break;
579
13.0k
  }
580
13.0k
  default:
581
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
582
0
                  "ContentModel broken for element %s\n",
583
0
      (const char *) name);
584
0
      return(0);
585
159k
    }
586
159k
    return(1);
587
159k
}
588
/**
589
 * xmlValidBuildContentModel:
590
 * @ctxt:  a validation context
591
 * @elem:  an element declaration node
592
 *
593
 * (Re)Build the automata associated to the content model of this
594
 * element
595
 *
596
 * Returns 1 in case of success, 0 in case of error
597
 */
598
int
599
5.82k
xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {
600
5.82k
    int ret = 0;
601
602
5.82k
    if ((ctxt == NULL) || (elem == NULL))
603
0
  return(0);
604
5.82k
    if (elem->type != XML_ELEMENT_DECL)
605
0
  return(0);
606
5.82k
    if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
607
0
  return(1);
608
    /* TODO: should we rebuild in this case ? */
609
5.82k
    if (elem->contModel != NULL) {
610
0
  if (!xmlRegexpIsDeterminist(elem->contModel)) {
611
0
      ctxt->valid = 0;
612
0
      return(0);
613
0
  }
614
0
  return(1);
615
0
    }
616
617
5.82k
    ctxt->am = xmlNewAutomata();
618
5.82k
    if (ctxt->am == NULL) {
619
55
        xmlVErrMemory(ctxt);
620
55
  return(0);
621
55
    }
622
5.77k
    ctxt->state = xmlAutomataGetInitState(ctxt->am);
623
5.77k
    xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
624
5.77k
    xmlAutomataSetFinalState(ctxt->am, ctxt->state);
625
5.77k
    elem->contModel = xmlAutomataCompile(ctxt->am);
626
5.77k
    if (elem->contModel == NULL) {
627
632
        xmlVErrMemory(ctxt);
628
632
        goto done;
629
632
    }
630
5.14k
    if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
631
600
  char expr[5000];
632
600
  expr[0] = 0;
633
600
  xmlSnprintfElementContent(expr, 5000, elem->content, 1);
634
600
  xmlErrValidNode(ctxt, (xmlNodePtr) elem,
635
600
                  XML_DTD_CONTENT_NOT_DETERMINIST,
636
600
         "Content model of %s is not deterministic: %s\n",
637
600
         elem->name, BAD_CAST expr, NULL);
638
600
        ctxt->valid = 0;
639
600
  goto done;
640
600
    }
641
642
4.54k
    ret = 1;
643
644
5.77k
done:
645
5.77k
    ctxt->state = NULL;
646
5.77k
    xmlFreeAutomata(ctxt->am);
647
5.77k
    ctxt->am = NULL;
648
5.77k
    return(ret);
649
4.54k
}
650
651
#endif /* LIBXML_REGEXP_ENABLED */
652
653
/****************************************************************
654
 *                *
655
 *  Util functions for data allocation/deallocation   *
656
 *                *
657
 ****************************************************************/
658
659
/**
660
 * xmlNewValidCtxt:
661
 *
662
 * Allocate a validation context structure.
663
 *
664
 * Returns NULL if not, otherwise the new validation context structure
665
 */
666
23.2k
xmlValidCtxtPtr xmlNewValidCtxt(void) {
667
23.2k
    xmlValidCtxtPtr ret;
668
669
23.2k
    if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL)
670
142
  return (NULL);
671
672
23.1k
    (void) memset(ret, 0, sizeof (xmlValidCtxt));
673
674
23.1k
    return (ret);
675
23.2k
}
676
677
/**
678
 * xmlFreeValidCtxt:
679
 * @cur:  the validation context to free
680
 *
681
 * Free a validation context structure.
682
 */
683
void
684
23.2k
xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
685
23.2k
    if (cur == NULL)
686
142
        return;
687
23.1k
    if (cur->vstateTab != NULL)
688
0
        xmlFree(cur->vstateTab);
689
23.1k
    if (cur->nodeTab != NULL)
690
0
        xmlFree(cur->nodeTab);
691
23.1k
    xmlFree(cur);
692
23.1k
}
693
694
#endif /* LIBXML_VALID_ENABLED */
695
696
/**
697
 * xmlNewDocElementContent:
698
 * @doc:  the document
699
 * @name:  the subelement name or NULL
700
 * @type:  the type of element content decl
701
 *
702
 * Allocate an element content structure for the document.
703
 *
704
 * Returns NULL if not, otherwise the new element content structure
705
 */
706
xmlElementContentPtr
707
xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
708
1.46M
                        xmlElementContentType type) {
709
1.46M
    xmlElementContentPtr ret;
710
1.46M
    xmlDictPtr dict = NULL;
711
712
1.46M
    if (doc != NULL)
713
1.46M
        dict = doc->dict;
714
715
1.46M
    switch(type) {
716
731k
  case XML_ELEMENT_CONTENT_ELEMENT:
717
731k
      if (name == NULL) {
718
0
          xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
719
0
      "xmlNewElementContent : name == NULL !\n",
720
0
      NULL);
721
0
      }
722
731k
      break;
723
46.9k
        case XML_ELEMENT_CONTENT_PCDATA:
724
280k
  case XML_ELEMENT_CONTENT_SEQ:
725
736k
  case XML_ELEMENT_CONTENT_OR:
726
736k
      if (name != NULL) {
727
0
          xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
728
0
      "xmlNewElementContent : name != NULL !\n",
729
0
      NULL);
730
0
      }
731
736k
      break;
732
0
  default:
733
0
      xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
734
0
        "Internal: ELEMENT content corrupted invalid type\n",
735
0
        NULL);
736
0
      return(NULL);
737
1.46M
    }
738
1.46M
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
739
1.46M
    if (ret == NULL)
740
318
  return(NULL);
741
1.46M
    memset(ret, 0, sizeof(xmlElementContent));
742
1.46M
    ret->type = type;
743
1.46M
    ret->ocur = XML_ELEMENT_CONTENT_ONCE;
744
1.46M
    if (name != NULL) {
745
730k
        int l;
746
730k
  const xmlChar *tmp;
747
748
730k
  tmp = xmlSplitQName3(name, &l);
749
730k
  if (tmp == NULL) {
750
573k
      if (dict == NULL)
751
123k
    ret->name = xmlStrdup(name);
752
449k
      else
753
449k
          ret->name = xmlDictLookup(dict, name, -1);
754
573k
  } else {
755
157k
      if (dict == NULL) {
756
107k
    ret->prefix = xmlStrndup(name, l);
757
107k
    ret->name = xmlStrdup(tmp);
758
107k
      } else {
759
50.3k
          ret->prefix = xmlDictLookup(dict, name, l);
760
50.3k
    ret->name = xmlDictLookup(dict, tmp, -1);
761
50.3k
      }
762
157k
            if (ret->prefix == NULL)
763
20
                goto error;
764
157k
  }
765
730k
        if (ret->name == NULL)
766
79
            goto error;
767
730k
    }
768
1.46M
    return(ret);
769
770
99
error:
771
99
    xmlFreeDocElementContent(doc, ret);
772
99
    return(NULL);
773
1.46M
}
774
775
/**
776
 * xmlNewElementContent:
777
 * @name:  the subelement name or NULL
778
 * @type:  the type of element content decl
779
 *
780
 * Allocate an element content structure.
781
 * Deprecated in favor of xmlNewDocElementContent
782
 *
783
 * Returns NULL if not, otherwise the new element content structure
784
 */
785
xmlElementContentPtr
786
0
xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
787
0
    return(xmlNewDocElementContent(NULL, name, type));
788
0
}
789
790
/**
791
 * xmlCopyDocElementContent:
792
 * @doc:  the document owning the element declaration
793
 * @cur:  An element content pointer.
794
 *
795
 * Build a copy of an element content description.
796
 *
797
 * Returns the new xmlElementContentPtr or NULL in case of error.
798
 */
799
xmlElementContentPtr
800
16.6k
xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
801
16.6k
    xmlElementContentPtr ret = NULL, prev = NULL, tmp;
802
16.6k
    xmlDictPtr dict = NULL;
803
804
16.6k
    if (cur == NULL) return(NULL);
805
806
16.6k
    if (doc != NULL)
807
0
        dict = doc->dict;
808
809
16.6k
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
810
16.6k
    if (ret == NULL)
811
7
  return(NULL);
812
16.6k
    memset(ret, 0, sizeof(xmlElementContent));
813
16.6k
    ret->type = cur->type;
814
16.6k
    ret->ocur = cur->ocur;
815
16.6k
    if (cur->name != NULL) {
816
12.6k
  if (dict)
817
0
      ret->name = xmlDictLookup(dict, cur->name, -1);
818
12.6k
  else
819
12.6k
      ret->name = xmlStrdup(cur->name);
820
12.6k
        if (ret->name == NULL)
821
10
            goto error;
822
12.6k
    }
823
824
16.6k
    if (cur->prefix != NULL) {
825
3.39k
  if (dict)
826
0
      ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
827
3.39k
  else
828
3.39k
      ret->prefix = xmlStrdup(cur->prefix);
829
3.39k
        if (ret->prefix == NULL)
830
4
            goto error;
831
3.39k
    }
832
16.6k
    if (cur->c1 != NULL) {
833
3.38k
        ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
834
3.38k
        if (ret->c1 == NULL)
835
13
            goto error;
836
3.37k
  ret->c1->parent = ret;
837
3.37k
    }
838
16.5k
    if (cur->c2 != NULL) {
839
3.37k
        prev = ret;
840
3.37k
  cur = cur->c2;
841
17.9k
  while (cur != NULL) {
842
14.6k
      tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
843
14.6k
      if (tmp == NULL)
844
11
                goto error;
845
14.6k
      memset(tmp, 0, sizeof(xmlElementContent));
846
14.6k
      tmp->type = cur->type;
847
14.6k
      tmp->ocur = cur->ocur;
848
14.6k
      prev->c2 = tmp;
849
14.6k
      tmp->parent = prev;
850
14.6k
      if (cur->name != NULL) {
851
3.33k
    if (dict)
852
0
        tmp->name = xmlDictLookup(dict, cur->name, -1);
853
3.33k
    else
854
3.33k
        tmp->name = xmlStrdup(cur->name);
855
3.33k
                if (tmp->name == NULL)
856
3
                    goto error;
857
3.33k
      }
858
859
14.6k
      if (cur->prefix != NULL) {
860
140
    if (dict)
861
0
        tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
862
140
    else
863
140
        tmp->prefix = xmlStrdup(cur->prefix);
864
140
                if (tmp->prefix == NULL)
865
0
                    goto error;
866
140
      }
867
14.6k
      if (cur->c1 != NULL) {
868
11.3k
          tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
869
11.3k
          if (tmp->c1 == NULL)
870
29
                    goto error;
871
11.2k
    tmp->c1->parent = tmp;
872
11.2k
            }
873
14.6k
      prev = tmp;
874
14.6k
      cur = cur->c2;
875
14.6k
  }
876
3.37k
    }
877
16.5k
    return(ret);
878
879
70
error:
880
70
    xmlFreeElementContent(ret);
881
70
    return(NULL);
882
16.5k
}
883
884
/**
885
 * xmlCopyElementContent:
886
 * @cur:  An element content pointer.
887
 *
888
 * Build a copy of an element content description.
889
 * Deprecated, use xmlCopyDocElementContent instead
890
 *
891
 * Returns the new xmlElementContentPtr or NULL in case of error.
892
 */
893
xmlElementContentPtr
894
1.93k
xmlCopyElementContent(xmlElementContentPtr cur) {
895
1.93k
    return(xmlCopyDocElementContent(NULL, cur));
896
1.93k
}
897
898
/**
899
 * xmlFreeDocElementContent:
900
 * @doc: the document owning the element declaration
901
 * @cur:  the element content tree to free
902
 *
903
 * Free an element content structure. The whole subtree is removed.
904
 */
905
void
906
174k
xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
907
174k
    xmlDictPtr dict = NULL;
908
174k
    size_t depth = 0;
909
910
174k
    if (cur == NULL)
911
53.9k
        return;
912
120k
    if (doc != NULL)
913
119k
        dict = doc->dict;
914
915
1.49M
    while (1) {
916
1.49M
        xmlElementContentPtr parent;
917
918
2.20M
        while ((cur->c1 != NULL) || (cur->c2 != NULL)) {
919
703k
            cur = (cur->c1 != NULL) ? cur->c1 : cur->c2;
920
703k
            depth += 1;
921
703k
        }
922
923
1.49M
  switch (cur->type) {
924
47.4k
      case XML_ELEMENT_CONTENT_PCDATA:
925
794k
      case XML_ELEMENT_CONTENT_ELEMENT:
926
1.03M
      case XML_ELEMENT_CONTENT_SEQ:
927
1.49M
      case XML_ELEMENT_CONTENT_OR:
928
1.49M
    break;
929
0
      default:
930
0
    xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
931
0
      "Internal: ELEMENT content corrupted invalid type\n",
932
0
      NULL);
933
0
    return;
934
1.49M
  }
935
1.49M
  if (dict) {
936
1.00M
      if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
937
0
          xmlFree((xmlChar *) cur->name);
938
1.00M
      if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
939
0
          xmlFree((xmlChar *) cur->prefix);
940
1.00M
  } else {
941
489k
      if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
942
489k
      if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
943
489k
  }
944
1.49M
        parent = cur->parent;
945
1.49M
        if ((depth == 0) || (parent == NULL)) {
946
120k
            xmlFree(cur);
947
120k
            break;
948
120k
        }
949
1.37M
        if (cur == parent->c1)
950
703k
            parent->c1 = NULL;
951
673k
        else
952
673k
            parent->c2 = NULL;
953
1.37M
  xmlFree(cur);
954
955
1.37M
        if (parent->c2 != NULL) {
956
673k
      cur = parent->c2;
957
703k
        } else {
958
703k
            depth -= 1;
959
703k
            cur = parent;
960
703k
        }
961
1.37M
    }
962
120k
}
963
964
/**
965
 * xmlFreeElementContent:
966
 * @cur:  the element content tree to free
967
 *
968
 * Free an element content structure. The whole subtree is removed.
969
 * Deprecated, use xmlFreeDocElementContent instead
970
 */
971
void
972
70
xmlFreeElementContent(xmlElementContentPtr cur) {
973
70
    xmlFreeDocElementContent(NULL, cur);
974
70
}
975
976
#ifdef LIBXML_OUTPUT_ENABLED
977
/**
978
 * xmlSprintfElementContent:
979
 * @buf:  an output buffer
980
 * @content:  An element table
981
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
982
 *
983
 * Deprecated, unsafe, use xmlSnprintfElementContent
984
 */
985
void
986
xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
987
                   xmlElementContentPtr content ATTRIBUTE_UNUSED,
988
0
       int englob ATTRIBUTE_UNUSED) {
989
0
}
990
#endif /* LIBXML_OUTPUT_ENABLED */
991
992
/**
993
 * xmlSnprintfElementContent:
994
 * @buf:  an output buffer
995
 * @size:  the buffer size
996
 * @content:  An element table
997
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
998
 *
999
 * This will dump the content of the element content definition
1000
 * Intended just for the debug routine
1001
 */
1002
void
1003
499k
xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
1004
499k
    int len;
1005
1006
499k
    if (content == NULL) return;
1007
499k
    len = strlen(buf);
1008
499k
    if (size - len < 50) {
1009
255
  if ((size - len > 4) && (buf[len - 1] != '.'))
1010
255
      strcat(buf, " ...");
1011
255
  return;
1012
255
    }
1013
499k
    if (englob) strcat(buf, "(");
1014
499k
    switch (content->type) {
1015
0
        case XML_ELEMENT_CONTENT_PCDATA:
1016
0
            strcat(buf, "#PCDATA");
1017
0
      break;
1018
289k
  case XML_ELEMENT_CONTENT_ELEMENT: {
1019
289k
            int qnameLen = xmlStrlen(content->name);
1020
1021
289k
      if (content->prefix != NULL)
1022
134k
                qnameLen += xmlStrlen(content->prefix) + 1;
1023
289k
      if (size - len < qnameLen + 10) {
1024
2.31k
    strcat(buf, " ...");
1025
2.31k
    return;
1026
2.31k
      }
1027
287k
      if (content->prefix != NULL) {
1028
132k
    strcat(buf, (char *) content->prefix);
1029
132k
    strcat(buf, ":");
1030
132k
      }
1031
287k
      if (content->name != NULL)
1032
287k
    strcat(buf, (char *) content->name);
1033
287k
      break;
1034
289k
        }
1035
83.8k
  case XML_ELEMENT_CONTENT_SEQ:
1036
83.8k
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1037
83.8k
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1038
7.35k
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1039
76.4k
      else
1040
76.4k
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1041
83.8k
      len = strlen(buf);
1042
83.8k
      if (size - len < 50) {
1043
800
    if ((size - len > 4) && (buf[len - 1] != '.'))
1044
299
        strcat(buf, " ...");
1045
800
    return;
1046
800
      }
1047
83.0k
            strcat(buf, " , ");
1048
83.0k
      if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
1049
83.0k
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1050
83.0k
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1051
6.22k
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1052
76.8k
      else
1053
76.8k
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1054
83.0k
      break;
1055
126k
  case XML_ELEMENT_CONTENT_OR:
1056
126k
      if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
1057
126k
          (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1058
46
    xmlSnprintfElementContent(buf, size, content->c1, 1);
1059
126k
      else
1060
126k
    xmlSnprintfElementContent(buf, size, content->c1, 0);
1061
126k
      len = strlen(buf);
1062
126k
      if (size - len < 50) {
1063
704
    if ((size - len > 4) && (buf[len - 1] != '.'))
1064
464
        strcat(buf, " ...");
1065
704
    return;
1066
704
      }
1067
125k
            strcat(buf, " | ");
1068
125k
      if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
1069
125k
     (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
1070
125k
    (content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1071
1.73k
    xmlSnprintfElementContent(buf, size, content->c2, 1);
1072
123k
      else
1073
123k
    xmlSnprintfElementContent(buf, size, content->c2, 0);
1074
125k
      break;
1075
499k
    }
1076
495k
    if (size - strlen(buf) <= 2) return;
1077
495k
    if (englob)
1078
96.4k
        strcat(buf, ")");
1079
495k
    switch (content->ocur) {
1080
400k
        case XML_ELEMENT_CONTENT_ONCE:
1081
400k
      break;
1082
15.2k
        case XML_ELEMENT_CONTENT_OPT:
1083
15.2k
      strcat(buf, "?");
1084
15.2k
      break;
1085
68.7k
        case XML_ELEMENT_CONTENT_MULT:
1086
68.7k
      strcat(buf, "*");
1087
68.7k
      break;
1088
11.0k
        case XML_ELEMENT_CONTENT_PLUS:
1089
11.0k
      strcat(buf, "+");
1090
11.0k
      break;
1091
495k
    }
1092
495k
}
1093
1094
/****************************************************************
1095
 *                *
1096
 *  Registration of DTD declarations      *
1097
 *                *
1098
 ****************************************************************/
1099
1100
/**
1101
 * xmlFreeElement:
1102
 * @elem:  An element
1103
 *
1104
 * Deallocate the memory used by an element definition
1105
 */
1106
static void
1107
112k
xmlFreeElement(xmlElementPtr elem) {
1108
112k
    if (elem == NULL) return;
1109
112k
    xmlUnlinkNode((xmlNodePtr) elem);
1110
112k
    xmlFreeDocElementContent(elem->doc, elem->content);
1111
112k
    if (elem->name != NULL)
1112
112k
  xmlFree((xmlChar *) elem->name);
1113
112k
    if (elem->prefix != NULL)
1114
7.70k
  xmlFree((xmlChar *) elem->prefix);
1115
112k
#ifdef LIBXML_REGEXP_ENABLED
1116
112k
    if (elem->contModel != NULL)
1117
5.14k
  xmlRegFreeRegexp(elem->contModel);
1118
112k
#endif
1119
112k
    xmlFree(elem);
1120
112k
}
1121
1122
1123
/**
1124
 * xmlAddElementDecl:
1125
 * @ctxt:  the validation context
1126
 * @dtd:  pointer to the DTD
1127
 * @name:  the entity name
1128
 * @type:  the element type
1129
 * @content:  the element content tree or NULL
1130
 *
1131
 * Register a new element declaration
1132
 *
1133
 * Returns NULL if not, otherwise the entity
1134
 */
1135
xmlElementPtr
1136
xmlAddElementDecl(xmlValidCtxtPtr ctxt,
1137
                  xmlDtdPtr dtd, const xmlChar *name,
1138
                  xmlElementTypeVal type,
1139
96.8k
      xmlElementContentPtr content) {
1140
96.8k
    xmlElementPtr ret;
1141
96.8k
    xmlElementTablePtr table;
1142
96.8k
    xmlAttributePtr oldAttributes = NULL;
1143
96.8k
    const xmlChar *localName;
1144
96.8k
    xmlChar *prefix = NULL;
1145
1146
96.8k
    if (dtd == NULL) {
1147
0
  return(NULL);
1148
0
    }
1149
96.8k
    if (name == NULL) {
1150
0
  return(NULL);
1151
0
    }
1152
1153
96.8k
    switch (type) {
1154
22.3k
        case XML_ELEMENT_TYPE_EMPTY:
1155
22.3k
      if (content != NULL) {
1156
0
    xmlErrValid(ctxt, XML_DTD_CONTENT_ERROR,
1157
0
            "xmlAddElementDecl: content != NULL for EMPTY\n",
1158
0
      NULL);
1159
0
    return(NULL);
1160
0
      }
1161
22.3k
      break;
1162
22.3k
  case XML_ELEMENT_TYPE_ANY:
1163
2.12k
      if (content != NULL) {
1164
0
    xmlErrValid(ctxt, XML_DTD_CONTENT_ERROR,
1165
0
            "xmlAddElementDecl: content != NULL for ANY\n",
1166
0
      NULL);
1167
0
    return(NULL);
1168
0
      }
1169
2.12k
      break;
1170
35.7k
  case XML_ELEMENT_TYPE_MIXED:
1171
35.7k
      if (content == NULL) {
1172
258
    xmlErrValid(ctxt, XML_DTD_CONTENT_ERROR,
1173
258
            "xmlAddElementDecl: content == NULL for MIXED\n",
1174
258
      NULL);
1175
258
    return(NULL);
1176
258
      }
1177
35.5k
      break;
1178
36.6k
  case XML_ELEMENT_TYPE_ELEMENT:
1179
36.6k
      if (content == NULL) {
1180
1.73k
    xmlErrValid(ctxt, XML_DTD_CONTENT_ERROR,
1181
1.73k
            "xmlAddElementDecl: content == NULL for ELEMENT\n",
1182
1.73k
      NULL);
1183
1.73k
    return(NULL);
1184
1.73k
      }
1185
34.9k
      break;
1186
34.9k
  default:
1187
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1188
0
        "Internal: ELEMENT decl corrupted invalid type\n",
1189
0
        NULL);
1190
0
      return(NULL);
1191
96.8k
    }
1192
1193
    /*
1194
     * check if name is a QName
1195
     */
1196
94.8k
    localName = xmlSplitQName4(name, &prefix);
1197
94.8k
    if (localName == NULL)
1198
17
        goto mem_error;
1199
1200
    /*
1201
     * Create the Element table if needed.
1202
     */
1203
94.8k
    table = (xmlElementTablePtr) dtd->elements;
1204
94.8k
    if (table == NULL) {
1205
22.6k
  xmlDictPtr dict = NULL;
1206
1207
22.6k
  if (dtd->doc != NULL)
1208
22.6k
      dict = dtd->doc->dict;
1209
22.6k
        table = xmlHashCreateDict(0, dict);
1210
22.6k
        if (table == NULL)
1211
42
            goto mem_error;
1212
22.6k
  dtd->elements = (void *) table;
1213
22.6k
    }
1214
1215
    /*
1216
     * lookup old attributes inserted on an undefined element in the
1217
     * internal subset.
1218
     */
1219
94.8k
    if ((dtd->doc != NULL) && (dtd->doc->intSubset != NULL)) {
1220
94.8k
  ret = xmlHashLookup2(dtd->doc->intSubset->elements, localName, prefix);
1221
94.8k
  if ((ret != NULL) && (ret->etype == XML_ELEMENT_TYPE_UNDEFINED)) {
1222
1.08k
      oldAttributes = ret->attributes;
1223
1.08k
      ret->attributes = NULL;
1224
1.08k
      xmlHashRemoveEntry2(dtd->doc->intSubset->elements, localName, prefix,
1225
1.08k
                                NULL);
1226
1.08k
      xmlFreeElement(ret);
1227
1.08k
  }
1228
94.8k
    }
1229
1230
    /*
1231
     * The element may already be present if one of its attribute
1232
     * was registered first
1233
     */
1234
94.8k
    ret = xmlHashLookup2(table, localName, prefix);
1235
94.8k
    if (ret != NULL) {
1236
19.4k
  if (ret->etype != XML_ELEMENT_TYPE_UNDEFINED) {
1237
19.3k
#ifdef LIBXML_VALID_ENABLED
1238
      /*
1239
       * The element is already defined in this DTD.
1240
       */
1241
19.3k
      xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ELEM_REDEFINED,
1242
19.3k
                      "Redefinition of element %s\n",
1243
19.3k
          name, NULL, NULL);
1244
19.3k
#endif /* LIBXML_VALID_ENABLED */
1245
19.3k
            if (prefix != NULL)
1246
1.92k
          xmlFree(prefix);
1247
19.3k
      return(NULL);
1248
19.3k
  }
1249
108
  if (prefix != NULL) {
1250
10
      xmlFree(prefix);
1251
10
      prefix = NULL;
1252
10
  }
1253
75.3k
    } else {
1254
75.3k
        int res;
1255
1256
75.3k
  ret = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1257
75.3k
  if (ret == NULL)
1258
48
            goto mem_error;
1259
75.3k
  memset(ret, 0, sizeof(xmlElement));
1260
75.3k
  ret->type = XML_ELEMENT_DECL;
1261
1262
  /*
1263
   * fill the structure.
1264
   */
1265
75.3k
  ret->name = xmlStrdup(localName);
1266
75.3k
  if (ret->name == NULL) {
1267
46
      xmlFree(ret);
1268
46
      goto mem_error;
1269
46
  }
1270
75.2k
  ret->prefix = prefix;
1271
75.2k
        prefix = NULL;
1272
1273
  /*
1274
   * Validity Check:
1275
   * Insertion must not fail
1276
   */
1277
75.2k
        res = xmlHashAdd2(table, localName, ret->prefix, ret);
1278
75.2k
        if (res <= 0) {
1279
77
      xmlFreeElement(ret);
1280
77
            goto mem_error;
1281
77
  }
1282
  /*
1283
   * For new element, may have attributes from earlier
1284
   * definition in internal subset
1285
   */
1286
75.1k
  ret->attributes = oldAttributes;
1287
75.1k
    }
1288
1289
    /*
1290
     * Finish to fill the structure.
1291
     */
1292
75.3k
    ret->etype = type;
1293
    /*
1294
     * Avoid a stupid copy when called by the parser
1295
     * and flag it by setting a special parent value
1296
     * so the parser doesn't unallocate it.
1297
     */
1298
75.3k
    if (content != NULL) {
1299
57.9k
        if ((ctxt != NULL) && (ctxt->flags & XML_VCTXT_USE_PCTXT)) {
1300
57.9k
            ret->content = content;
1301
57.9k
            content->parent = (xmlElementContentPtr) 1;
1302
57.9k
        } else if (content != NULL){
1303
0
            ret->content = xmlCopyDocElementContent(dtd->doc, content);
1304
0
            if (ret->content == NULL)
1305
0
                goto mem_error;
1306
0
        }
1307
57.9k
    }
1308
1309
    /*
1310
     * Link it to the DTD
1311
     */
1312
75.3k
    ret->parent = dtd;
1313
75.3k
    ret->doc = dtd->doc;
1314
75.3k
    if (dtd->last == NULL) {
1315
20.5k
  dtd->children = dtd->last = (xmlNodePtr) ret;
1316
54.7k
    } else {
1317
54.7k
        dtd->last->next = (xmlNodePtr) ret;
1318
54.7k
  ret->prev = dtd->last;
1319
54.7k
  dtd->last = (xmlNodePtr) ret;
1320
54.7k
    }
1321
75.3k
    if (prefix != NULL)
1322
0
  xmlFree(prefix);
1323
75.3k
    return(ret);
1324
1325
230
mem_error:
1326
230
    xmlVErrMemory(ctxt);
1327
230
    if (prefix != NULL)
1328
25
        xmlFree(prefix);
1329
230
    return(NULL);
1330
75.3k
}
1331
1332
static void
1333
110k
xmlFreeElementTableEntry(void *elem, const xmlChar *name ATTRIBUTE_UNUSED) {
1334
110k
    xmlFreeElement((xmlElementPtr) elem);
1335
110k
}
1336
1337
/**
1338
 * xmlFreeElementTable:
1339
 * @table:  An element table
1340
 *
1341
 * Deallocate the memory used by an element hash table.
1342
 */
1343
void
1344
53.2k
xmlFreeElementTable(xmlElementTablePtr table) {
1345
53.2k
    xmlHashFree(table, xmlFreeElementTableEntry);
1346
53.2k
}
1347
1348
#ifdef LIBXML_TREE_ENABLED
1349
/**
1350
 * xmlCopyElement:
1351
 * @elem:  An element
1352
 *
1353
 * Build a copy of an element.
1354
 *
1355
 * Returns the new xmlElementPtr or NULL in case of error.
1356
 */
1357
static void *
1358
4.43k
xmlCopyElement(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
1359
4.43k
    xmlElementPtr elem = (xmlElementPtr) payload;
1360
4.43k
    xmlElementPtr cur;
1361
1362
4.43k
    cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
1363
4.43k
    if (cur == NULL)
1364
14
  return(NULL);
1365
4.42k
    memset(cur, 0, sizeof(xmlElement));
1366
4.42k
    cur->type = XML_ELEMENT_DECL;
1367
4.42k
    cur->etype = elem->etype;
1368
4.42k
    if (elem->name != NULL) {
1369
4.42k
  cur->name = xmlStrdup(elem->name);
1370
4.42k
        if (cur->name == NULL)
1371
7
            goto error;
1372
4.42k
    }
1373
4.41k
    if (elem->prefix != NULL) {
1374
467
  cur->prefix = xmlStrdup(elem->prefix);
1375
467
        if (cur->prefix == NULL)
1376
2
            goto error;
1377
467
    }
1378
4.41k
    if (elem->content != NULL) {
1379
1.93k
        cur->content = xmlCopyElementContent(elem->content);
1380
1.93k
        if (cur->content == NULL)
1381
35
            goto error;
1382
1.93k
    }
1383
    /* TODO : rebuild the attribute list on the copy */
1384
4.37k
    cur->attributes = NULL;
1385
4.37k
    return(cur);
1386
1387
44
error:
1388
44
    xmlFreeElement(cur);
1389
44
    return(NULL);
1390
4.41k
}
1391
1392
/**
1393
 * xmlCopyElementTable:
1394
 * @table:  An element table
1395
 *
1396
 * Build a copy of an element table.
1397
 *
1398
 * Returns the new xmlElementTablePtr or NULL in case of error.
1399
 */
1400
xmlElementTablePtr
1401
2.49k
xmlCopyElementTable(xmlElementTablePtr table) {
1402
2.49k
    return(xmlHashCopySafe(table, xmlCopyElement, xmlFreeElementTableEntry));
1403
2.49k
}
1404
#endif /* LIBXML_TREE_ENABLED */
1405
1406
#ifdef LIBXML_OUTPUT_ENABLED
1407
/**
1408
 * xmlDumpElementDecl:
1409
 * @buf:  the XML buffer output
1410
 * @elem:  An element table
1411
 *
1412
 * DEPRECATED: Use xmlSaveTree.
1413
 *
1414
 * This will dump the content of the element declaration as an XML
1415
 * DTD definition
1416
 */
1417
void
1418
0
xmlDumpElementDecl(xmlBufferPtr buf, xmlElementPtr elem) {
1419
0
    xmlSaveCtxtPtr save;
1420
1421
0
    if ((buf == NULL) || (elem == NULL))
1422
0
        return;
1423
1424
0
    save = xmlSaveToBuffer(buf, NULL, 0);
1425
0
    xmlSaveTree(save, (xmlNodePtr) elem);
1426
0
    xmlSaveClose(save);
1427
0
}
1428
1429
/**
1430
 * xmlDumpElementDeclScan:
1431
 * @elem:  An element table
1432
 * @buf:  the XML buffer output
1433
 *
1434
 * This routine is used by the hash scan function.  It just reverses
1435
 * the arguments.
1436
 */
1437
static void
1438
xmlDumpElementDeclScan(void *elem, void *buf,
1439
0
                       const xmlChar *name ATTRIBUTE_UNUSED) {
1440
0
    xmlDumpElementDecl((xmlBufferPtr) buf, (xmlElementPtr) elem);
1441
0
}
1442
1443
/**
1444
 * xmlDumpElementTable:
1445
 * @buf:  the XML buffer output
1446
 * @table:  An element table
1447
 *
1448
 * DEPRECATED: Don't use.
1449
 *
1450
 * This will dump the content of the element table as an XML DTD definition
1451
 */
1452
void
1453
0
xmlDumpElementTable(xmlBufferPtr buf, xmlElementTablePtr table) {
1454
0
    if ((buf == NULL) || (table == NULL))
1455
0
        return;
1456
0
    xmlHashScan(table, xmlDumpElementDeclScan, buf);
1457
0
}
1458
#endif /* LIBXML_OUTPUT_ENABLED */
1459
1460
/**
1461
 * xmlCreateEnumeration:
1462
 * @name:  the enumeration name or NULL
1463
 *
1464
 * create and initialize an enumeration attribute node.
1465
 *
1466
 * Returns the xmlEnumerationPtr just created or NULL in case
1467
 *                of error.
1468
 */
1469
xmlEnumerationPtr
1470
138k
xmlCreateEnumeration(const xmlChar *name) {
1471
138k
    xmlEnumerationPtr ret;
1472
1473
138k
    ret = (xmlEnumerationPtr) xmlMalloc(sizeof(xmlEnumeration));
1474
138k
    if (ret == NULL)
1475
58
        return(NULL);
1476
138k
    memset(ret, 0, sizeof(xmlEnumeration));
1477
1478
138k
    if (name != NULL) {
1479
138k
        ret->name = xmlStrdup(name);
1480
138k
        if (ret->name == NULL) {
1481
65
            xmlFree(ret);
1482
65
            return(NULL);
1483
65
        }
1484
138k
    }
1485
1486
137k
    return(ret);
1487
138k
}
1488
1489
/**
1490
 * xmlFreeEnumeration:
1491
 * @cur:  the tree to free.
1492
 *
1493
 * free an enumeration attribute node (recursive).
1494
 */
1495
void
1496
96.0k
xmlFreeEnumeration(xmlEnumerationPtr cur) {
1497
234k
    while (cur != NULL) {
1498
137k
        xmlEnumerationPtr next = cur->next;
1499
1500
137k
        xmlFree((xmlChar *) cur->name);
1501
137k
        xmlFree(cur);
1502
1503
137k
        cur = next;
1504
137k
    }
1505
96.0k
}
1506
1507
#ifdef LIBXML_TREE_ENABLED
1508
/**
1509
 * xmlCopyEnumeration:
1510
 * @cur:  the tree to copy.
1511
 *
1512
 * Copy an enumeration attribute node (recursive).
1513
 *
1514
 * Returns the xmlEnumerationPtr just created or NULL in case
1515
 *                of error.
1516
 */
1517
xmlEnumerationPtr
1518
1.69k
xmlCopyEnumeration(xmlEnumerationPtr cur) {
1519
1.69k
    xmlEnumerationPtr ret = NULL;
1520
1.69k
    xmlEnumerationPtr last = NULL;
1521
1522
4.35k
    while (cur != NULL) {
1523
2.66k
        xmlEnumerationPtr copy = xmlCreateEnumeration(cur->name);
1524
1525
2.66k
        if (copy == NULL) {
1526
5
            xmlFreeEnumeration(ret);
1527
5
            return(NULL);
1528
5
        }
1529
1530
2.65k
        if (ret == NULL) {
1531
1.69k
            ret = last = copy;
1532
1.69k
        } else {
1533
963
            last->next = copy;
1534
963
            last = copy;
1535
963
        }
1536
1537
2.65k
        cur = cur->next;
1538
2.65k
    }
1539
1540
1.69k
    return(ret);
1541
1.69k
}
1542
#endif /* LIBXML_TREE_ENABLED */
1543
1544
#ifdef LIBXML_VALID_ENABLED
1545
/**
1546
 * xmlScanIDAttributeDecl:
1547
 * @ctxt:  the validation context
1548
 * @elem:  the element name
1549
 * @err: whether to raise errors here
1550
 *
1551
 * Verify that the element don't have too many ID attributes
1552
 * declared.
1553
 *
1554
 * Returns the number of ID attributes found.
1555
 */
1556
static int
1557
94.0k
xmlScanIDAttributeDecl(xmlValidCtxtPtr ctxt, xmlElementPtr elem, int err) {
1558
94.0k
    xmlAttributePtr cur;
1559
94.0k
    int ret = 0;
1560
1561
94.0k
    if (elem == NULL) return(0);
1562
94.0k
    cur = elem->attributes;
1563
337k
    while (cur != NULL) {
1564
243k
        if (cur->atype == XML_ATTRIBUTE_ID) {
1565
236k
      ret ++;
1566
236k
      if ((ret > 1) && (err))
1567
186k
    xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_MULTIPLE_ID,
1568
186k
         "Element %s has too many ID attributes defined : %s\n",
1569
186k
           elem->name, cur->name, NULL);
1570
236k
  }
1571
243k
  cur = cur->nexth;
1572
243k
    }
1573
94.0k
    return(ret);
1574
94.0k
}
1575
#endif /* LIBXML_VALID_ENABLED */
1576
1577
/**
1578
 * xmlFreeAttribute:
1579
 * @elem:  An attribute
1580
 *
1581
 * Deallocate the memory used by an attribute definition
1582
 */
1583
static void
1584
503k
xmlFreeAttribute(xmlAttributePtr attr) {
1585
503k
    xmlDictPtr dict;
1586
1587
503k
    if (attr == NULL) return;
1588
503k
    if (attr->doc != NULL)
1589
502k
  dict = attr->doc->dict;
1590
1.17k
    else
1591
1.17k
  dict = NULL;
1592
503k
    xmlUnlinkNode((xmlNodePtr) attr);
1593
503k
    if (attr->tree != NULL)
1594
70.5k
        xmlFreeEnumeration(attr->tree);
1595
503k
    if (dict) {
1596
418k
        if ((attr->elem != NULL) && (!xmlDictOwns(dict, attr->elem)))
1597
0
      xmlFree((xmlChar *) attr->elem);
1598
418k
        if ((attr->name != NULL) && (!xmlDictOwns(dict, attr->name)))
1599
0
      xmlFree((xmlChar *) attr->name);
1600
418k
        if ((attr->prefix != NULL) && (!xmlDictOwns(dict, attr->prefix)))
1601
0
      xmlFree((xmlChar *) attr->prefix);
1602
418k
        if ((attr->defaultValue != NULL) &&
1603
418k
      (!xmlDictOwns(dict, attr->defaultValue)))
1604
0
      xmlFree((xmlChar *) attr->defaultValue);
1605
418k
    } else {
1606
85.3k
  if (attr->elem != NULL)
1607
85.3k
      xmlFree((xmlChar *) attr->elem);
1608
85.3k
  if (attr->name != NULL)
1609
85.3k
      xmlFree((xmlChar *) attr->name);
1610
85.3k
  if (attr->defaultValue != NULL)
1611
17.7k
      xmlFree((xmlChar *) attr->defaultValue);
1612
85.3k
  if (attr->prefix != NULL)
1613
13.4k
      xmlFree((xmlChar *) attr->prefix);
1614
85.3k
    }
1615
503k
    xmlFree(attr);
1616
503k
}
1617
1618
1619
/**
1620
 * xmlAddAttributeDecl:
1621
 * @ctxt:  the validation context
1622
 * @dtd:  pointer to the DTD
1623
 * @elem:  the element name
1624
 * @name:  the attribute name
1625
 * @ns:  the attribute namespace prefix
1626
 * @type:  the attribute type
1627
 * @def:  the attribute default type
1628
 * @defaultValue:  the attribute default value
1629
 * @tree:  if it's an enumeration, the associated list
1630
 *
1631
 * Register a new attribute declaration
1632
 * Note that @tree becomes the ownership of the DTD
1633
 *
1634
 * Returns NULL if not new, otherwise the attribute decl
1635
 */
1636
xmlAttributePtr
1637
xmlAddAttributeDecl(xmlValidCtxtPtr ctxt,
1638
                    xmlDtdPtr dtd, const xmlChar *elem,
1639
                    const xmlChar *name, const xmlChar *ns,
1640
        xmlAttributeType type, xmlAttributeDefault def,
1641
506k
        const xmlChar *defaultValue, xmlEnumerationPtr tree) {
1642
506k
    xmlAttributePtr ret = NULL;
1643
506k
    xmlAttributeTablePtr table;
1644
506k
    xmlElementPtr elemDef;
1645
506k
    xmlDictPtr dict = NULL;
1646
506k
    int res;
1647
1648
506k
    if (dtd == NULL) {
1649
0
  xmlFreeEnumeration(tree);
1650
0
  return(NULL);
1651
0
    }
1652
506k
    if (name == NULL) {
1653
328
  xmlFreeEnumeration(tree);
1654
328
  return(NULL);
1655
328
    }
1656
505k
    if (elem == NULL) {
1657
0
  xmlFreeEnumeration(tree);
1658
0
  return(NULL);
1659
0
    }
1660
505k
    if (dtd->doc != NULL)
1661
505k
  dict = dtd->doc->dict;
1662
1663
505k
#ifdef LIBXML_VALID_ENABLED
1664
    /*
1665
     * Check the type and possibly the default value.
1666
     */
1667
505k
    switch (type) {
1668
252k
        case XML_ATTRIBUTE_CDATA:
1669
252k
      break;
1670
106k
        case XML_ATTRIBUTE_ID:
1671
106k
      break;
1672
6.41k
        case XML_ATTRIBUTE_IDREF:
1673
6.41k
      break;
1674
8.49k
        case XML_ATTRIBUTE_IDREFS:
1675
8.49k
      break;
1676
11.0k
        case XML_ATTRIBUTE_ENTITY:
1677
11.0k
      break;
1678
10.9k
        case XML_ATTRIBUTE_ENTITIES:
1679
10.9k
      break;
1680
31.7k
        case XML_ATTRIBUTE_NMTOKEN:
1681
31.7k
      break;
1682
8.68k
        case XML_ATTRIBUTE_NMTOKENS:
1683
8.68k
      break;
1684
68.3k
        case XML_ATTRIBUTE_ENUMERATION:
1685
68.3k
      break;
1686
1.54k
        case XML_ATTRIBUTE_NOTATION:
1687
1.54k
      break;
1688
0
  default:
1689
0
      xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1690
0
        "Internal: ATTRIBUTE struct corrupted invalid type\n",
1691
0
        NULL);
1692
0
      xmlFreeEnumeration(tree);
1693
0
      return(NULL);
1694
505k
    }
1695
505k
    if ((defaultValue != NULL) &&
1696
505k
        (!xmlValidateAttributeValueInternal(dtd->doc, type, defaultValue))) {
1697
77.3k
  xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_ATTRIBUTE_DEFAULT,
1698
77.3k
                  "Attribute %s of %s: invalid default value\n",
1699
77.3k
                  elem, name, defaultValue);
1700
77.3k
  defaultValue = NULL;
1701
77.3k
  if (ctxt != NULL)
1702
77.3k
      ctxt->valid = 0;
1703
77.3k
    }
1704
505k
#endif /* LIBXML_VALID_ENABLED */
1705
1706
    /*
1707
     * Check first that an attribute defined in the external subset wasn't
1708
     * already defined in the internal subset
1709
     */
1710
505k
    if ((dtd->doc != NULL) && (dtd->doc->extSubset == dtd) &&
1711
505k
  (dtd->doc->intSubset != NULL) &&
1712
505k
  (dtd->doc->intSubset->attributes != NULL)) {
1713
23.8k
        ret = xmlHashLookup3(dtd->doc->intSubset->attributes, name, ns, elem);
1714
23.8k
  if (ret != NULL) {
1715
9.03k
      xmlFreeEnumeration(tree);
1716
9.03k
      return(NULL);
1717
9.03k
  }
1718
23.8k
    }
1719
1720
    /*
1721
     * Create the Attribute table if needed.
1722
     */
1723
496k
    table = (xmlAttributeTablePtr) dtd->attributes;
1724
496k
    if (table == NULL) {
1725
36.5k
        table = xmlHashCreateDict(0, dict);
1726
36.5k
  dtd->attributes = (void *) table;
1727
36.5k
    }
1728
496k
    if (table == NULL)
1729
92
        goto mem_error;
1730
1731
496k
    ret = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1732
496k
    if (ret == NULL)
1733
123
        goto mem_error;
1734
496k
    memset(ret, 0, sizeof(xmlAttribute));
1735
496k
    ret->type = XML_ATTRIBUTE_DECL;
1736
1737
    /*
1738
     * fill the structure.
1739
     */
1740
496k
    ret->atype = type;
1741
    /*
1742
     * doc must be set before possible error causes call
1743
     * to xmlFreeAttribute (because it's used to check on
1744
     * dict use)
1745
     */
1746
496k
    ret->doc = dtd->doc;
1747
496k
    if (dict) {
1748
418k
  ret->name = xmlDictLookup(dict, name, -1);
1749
418k
  ret->elem = xmlDictLookup(dict, elem, -1);
1750
418k
    } else {
1751
78.6k
  ret->name = xmlStrdup(name);
1752
78.6k
  ret->elem = xmlStrdup(elem);
1753
78.6k
    }
1754
496k
    if ((ret->name == NULL) || (ret->elem == NULL))
1755
109
        goto mem_error;
1756
496k
    if (ns != NULL) {
1757
50.2k
        if (dict)
1758
37.9k
            ret->prefix = xmlDictLookup(dict, ns, -1);
1759
12.2k
        else
1760
12.2k
            ret->prefix = xmlStrdup(ns);
1761
50.2k
        if (ret->prefix == NULL)
1762
22
            goto mem_error;
1763
50.2k
    }
1764
496k
    ret->def = def;
1765
496k
    ret->tree = tree;
1766
496k
    tree = NULL;
1767
496k
    if (defaultValue != NULL) {
1768
241k
        if (dict)
1769
225k
      ret->defaultValue = xmlDictLookup(dict, defaultValue, -1);
1770
15.3k
  else
1771
15.3k
      ret->defaultValue = xmlStrdup(defaultValue);
1772
241k
        if (ret->defaultValue == NULL)
1773
40
            xmlVErrMemory(ctxt);
1774
241k
    }
1775
1776
    /*
1777
     * Validity Check:
1778
     * Search the DTD for previous declarations of the ATTLIST
1779
     */
1780
496k
    res = xmlHashAdd3(table, ret->name, ret->prefix, ret->elem, ret);
1781
496k
    if (res <= 0) {
1782
249k
        if (res < 0)
1783
148
            goto mem_error;
1784
249k
#ifdef LIBXML_VALID_ENABLED
1785
        /*
1786
         * The attribute is already defined in this DTD.
1787
         */
1788
249k
        xmlErrValidWarning(ctxt, (xmlNodePtr) dtd,
1789
249k
                XML_DTD_ATTRIBUTE_REDEFINED,
1790
249k
                "Attribute %s of element %s: already defined\n",
1791
249k
                name, elem, NULL);
1792
249k
#endif /* LIBXML_VALID_ENABLED */
1793
249k
  xmlFreeAttribute(ret);
1794
249k
  return(NULL);
1795
249k
    }
1796
1797
    /*
1798
     * Validity Check:
1799
     * Multiple ID per element
1800
     */
1801
247k
    elemDef = xmlGetDtdElementDesc2(ctxt, dtd, elem);
1802
247k
    if (elemDef != NULL) {
1803
1804
247k
#ifdef LIBXML_VALID_ENABLED
1805
247k
        if ((type == XML_ATTRIBUTE_ID) &&
1806
247k
      (xmlScanIDAttributeDecl(ctxt, elemDef, 1) != 0)) {
1807
32.5k
      xmlErrValidNode(ctxt, (xmlNodePtr) dtd, XML_DTD_MULTIPLE_ID,
1808
32.5k
     "Element %s has too may ID attributes defined : %s\n",
1809
32.5k
       elem, name, NULL);
1810
32.5k
      if (ctxt != NULL)
1811
32.5k
    ctxt->valid = 0;
1812
32.5k
  }
1813
247k
#endif /* LIBXML_VALID_ENABLED */
1814
1815
  /*
1816
   * Insert namespace default def first they need to be
1817
   * processed first.
1818
   */
1819
247k
  if ((xmlStrEqual(ret->name, BAD_CAST "xmlns")) ||
1820
247k
      ((ret->prefix != NULL &&
1821
242k
       (xmlStrEqual(ret->prefix, BAD_CAST "xmlns"))))) {
1822
10.6k
      ret->nexth = elemDef->attributes;
1823
10.6k
      elemDef->attributes = ret;
1824
236k
  } else {
1825
236k
      xmlAttributePtr tmp = elemDef->attributes;
1826
1827
238k
      while ((tmp != NULL) &&
1828
238k
       ((xmlStrEqual(tmp->name, BAD_CAST "xmlns")) ||
1829
170k
        ((ret->prefix != NULL &&
1830
167k
         (xmlStrEqual(ret->prefix, BAD_CAST "xmlns")))))) {
1831
2.93k
    if (tmp->nexth == NULL)
1832
606
        break;
1833
2.32k
    tmp = tmp->nexth;
1834
2.32k
      }
1835
236k
      if (tmp != NULL) {
1836
167k
    ret->nexth = tmp->nexth;
1837
167k
          tmp->nexth = ret;
1838
167k
      } else {
1839
68.5k
    ret->nexth = elemDef->attributes;
1840
68.5k
    elemDef->attributes = ret;
1841
68.5k
      }
1842
236k
  }
1843
247k
    }
1844
1845
    /*
1846
     * Link it to the DTD
1847
     */
1848
247k
    ret->parent = dtd;
1849
247k
    if (dtd->last == NULL) {
1850
25.5k
  dtd->children = dtd->last = (xmlNodePtr) ret;
1851
221k
    } else {
1852
221k
        dtd->last->next = (xmlNodePtr) ret;
1853
221k
  ret->prev = dtd->last;
1854
221k
  dtd->last = (xmlNodePtr) ret;
1855
221k
    }
1856
247k
    return(ret);
1857
1858
494
mem_error:
1859
494
    xmlVErrMemory(ctxt);
1860
494
    xmlFreeEnumeration(tree);
1861
494
    xmlFreeAttribute(ret);
1862
494
    return(NULL);
1863
496k
}
1864
1865
static void
1866
254k
xmlFreeAttributeTableEntry(void *attr, const xmlChar *name ATTRIBUTE_UNUSED) {
1867
254k
    xmlFreeAttribute((xmlAttributePtr) attr);
1868
254k
}
1869
1870
/**
1871
 * xmlFreeAttributeTable:
1872
 * @table:  An attribute table
1873
 *
1874
 * Deallocate the memory used by an entities hash table.
1875
 */
1876
void
1877
38.4k
xmlFreeAttributeTable(xmlAttributeTablePtr table) {
1878
38.4k
    xmlHashFree(table, xmlFreeAttributeTableEntry);
1879
38.4k
}
1880
1881
#ifdef LIBXML_TREE_ENABLED
1882
/**
1883
 * xmlCopyAttribute:
1884
 * @attr:  An attribute
1885
 *
1886
 * Build a copy of an attribute.
1887
 *
1888
 * Returns the new xmlAttributePtr or NULL in case of error.
1889
 */
1890
static void *
1891
6.75k
xmlCopyAttribute(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
1892
6.75k
    xmlAttributePtr attr = (xmlAttributePtr) payload;
1893
6.75k
    xmlAttributePtr cur;
1894
1895
6.75k
    cur = (xmlAttributePtr) xmlMalloc(sizeof(xmlAttribute));
1896
6.75k
    if (cur == NULL)
1897
10
  return(NULL);
1898
6.74k
    memset(cur, 0, sizeof(xmlAttribute));
1899
6.74k
    cur->type = XML_ATTRIBUTE_DECL;
1900
6.74k
    cur->atype = attr->atype;
1901
6.74k
    cur->def = attr->def;
1902
6.74k
    if (attr->tree != NULL) {
1903
1.69k
        cur->tree = xmlCopyEnumeration(attr->tree);
1904
1.69k
        if (cur->tree == NULL)
1905
5
            goto error;
1906
1.69k
    }
1907
6.73k
    if (attr->elem != NULL) {
1908
6.73k
  cur->elem = xmlStrdup(attr->elem);
1909
6.73k
        if (cur->elem == NULL)
1910
8
            goto error;
1911
6.73k
    }
1912
6.72k
    if (attr->name != NULL) {
1913
6.72k
  cur->name = xmlStrdup(attr->name);
1914
6.72k
        if (cur->name == NULL)
1915
9
            goto error;
1916
6.72k
    }
1917
6.71k
    if (attr->prefix != NULL) {
1918
1.17k
  cur->prefix = xmlStrdup(attr->prefix);
1919
1.17k
        if (cur->prefix == NULL)
1920
2
            goto error;
1921
1.17k
    }
1922
6.71k
    if (attr->defaultValue != NULL) {
1923
2.42k
  cur->defaultValue = xmlStrdup(attr->defaultValue);
1924
2.42k
        if (cur->defaultValue == NULL)
1925
5
            goto error;
1926
2.42k
    }
1927
6.71k
    return(cur);
1928
1929
29
error:
1930
29
    xmlFreeAttribute(cur);
1931
29
    return(NULL);
1932
6.71k
}
1933
1934
/**
1935
 * xmlCopyAttributeTable:
1936
 * @table:  An attribute table
1937
 *
1938
 * Build a copy of an attribute table.
1939
 *
1940
 * Returns the new xmlAttributeTablePtr or NULL in case of error.
1941
 */
1942
xmlAttributeTablePtr
1943
2.05k
xmlCopyAttributeTable(xmlAttributeTablePtr table) {
1944
2.05k
    return(xmlHashCopySafe(table, xmlCopyAttribute,
1945
2.05k
                           xmlFreeAttributeTableEntry));
1946
2.05k
}
1947
#endif /* LIBXML_TREE_ENABLED */
1948
1949
#ifdef LIBXML_OUTPUT_ENABLED
1950
/**
1951
 * xmlDumpAttributeDecl:
1952
 * @buf:  the XML buffer output
1953
 * @attr:  An attribute declaration
1954
 *
1955
 * DEPRECATED: Use xmlSaveTree.
1956
 *
1957
 * This will dump the content of the attribute declaration as an XML
1958
 * DTD definition
1959
 */
1960
void
1961
0
xmlDumpAttributeDecl(xmlBufferPtr buf, xmlAttributePtr attr) {
1962
0
    xmlSaveCtxtPtr save;
1963
1964
0
    if ((buf == NULL) || (attr == NULL))
1965
0
        return;
1966
1967
0
    save = xmlSaveToBuffer(buf, NULL, 0);
1968
0
    xmlSaveTree(save, (xmlNodePtr) attr);
1969
0
    xmlSaveClose(save);
1970
0
}
1971
1972
/**
1973
 * xmlDumpAttributeDeclScan:
1974
 * @attr:  An attribute declaration
1975
 * @buf:  the XML buffer output
1976
 *
1977
 * This is used with the hash scan function - just reverses arguments
1978
 */
1979
static void
1980
xmlDumpAttributeDeclScan(void *attr, void *buf,
1981
0
                         const xmlChar *name ATTRIBUTE_UNUSED) {
1982
0
    xmlDumpAttributeDecl((xmlBufferPtr) buf, (xmlAttributePtr) attr);
1983
0
}
1984
1985
/**
1986
 * xmlDumpAttributeTable:
1987
 * @buf:  the XML buffer output
1988
 * @table:  An attribute table
1989
 *
1990
 * DEPRECATED: Don't use.
1991
 *
1992
 * This will dump the content of the attribute table as an XML DTD definition
1993
 */
1994
void
1995
0
xmlDumpAttributeTable(xmlBufferPtr buf, xmlAttributeTablePtr table) {
1996
0
    if ((buf == NULL) || (table == NULL))
1997
0
        return;
1998
0
    xmlHashScan(table, xmlDumpAttributeDeclScan, buf);
1999
0
}
2000
#endif /* LIBXML_OUTPUT_ENABLED */
2001
2002
/************************************************************************
2003
 *                  *
2004
 *        NOTATIONs       *
2005
 *                  *
2006
 ************************************************************************/
2007
/**
2008
 * xmlFreeNotation:
2009
 * @not:  A notation
2010
 *
2011
 * Deallocate the memory used by an notation definition
2012
 */
2013
static void
2014
4.33k
xmlFreeNotation(xmlNotationPtr nota) {
2015
4.33k
    if (nota == NULL) return;
2016
4.29k
    if (nota->name != NULL)
2017
4.26k
  xmlFree((xmlChar *) nota->name);
2018
4.29k
    if (nota->PublicID != NULL)
2019
2.14k
  xmlFree((xmlChar *) nota->PublicID);
2020
4.29k
    if (nota->SystemID != NULL)
2021
2.18k
  xmlFree((xmlChar *) nota->SystemID);
2022
4.29k
    xmlFree(nota);
2023
4.29k
}
2024
2025
2026
/**
2027
 * xmlAddNotationDecl:
2028
 * @dtd:  pointer to the DTD
2029
 * @ctxt:  the validation context
2030
 * @name:  the entity name
2031
 * @PublicID:  the public identifier or NULL
2032
 * @SystemID:  the system identifier or NULL
2033
 *
2034
 * Register a new notation declaration
2035
 *
2036
 * Returns NULL if not, otherwise the entity
2037
 */
2038
xmlNotationPtr
2039
xmlAddNotationDecl(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
2040
             const xmlChar *name,
2041
4.24k
                   const xmlChar *PublicID, const xmlChar *SystemID) {
2042
4.24k
    xmlNotationPtr ret = NULL;
2043
4.24k
    xmlNotationTablePtr table;
2044
4.24k
    int res;
2045
2046
4.24k
    if (dtd == NULL) {
2047
0
  return(NULL);
2048
0
    }
2049
4.24k
    if (name == NULL) {
2050
0
  return(NULL);
2051
0
    }
2052
4.24k
    if ((PublicID == NULL) && (SystemID == NULL)) {
2053
0
  return(NULL);
2054
0
    }
2055
2056
    /*
2057
     * Create the Notation table if needed.
2058
     */
2059
4.24k
    table = (xmlNotationTablePtr) dtd->notations;
2060
4.24k
    if (table == NULL) {
2061
1.02k
  xmlDictPtr dict = NULL;
2062
1.02k
  if (dtd->doc != NULL)
2063
1.02k
      dict = dtd->doc->dict;
2064
2065
1.02k
        dtd->notations = table = xmlHashCreateDict(0, dict);
2066
1.02k
        if (table == NULL)
2067
17
            goto mem_error;
2068
1.02k
    }
2069
2070
4.23k
    ret = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2071
4.23k
    if (ret == NULL)
2072
26
        goto mem_error;
2073
4.20k
    memset(ret, 0, sizeof(xmlNotation));
2074
2075
    /*
2076
     * fill the structure.
2077
     */
2078
4.20k
    ret->name = xmlStrdup(name);
2079
4.20k
    if (ret->name == NULL)
2080
24
        goto mem_error;
2081
4.18k
    if (SystemID != NULL) {
2082
2.15k
        ret->SystemID = xmlStrdup(SystemID);
2083
2.15k
        if (ret->SystemID == NULL)
2084
14
            goto mem_error;
2085
2.15k
    }
2086
4.16k
    if (PublicID != NULL) {
2087
2.11k
        ret->PublicID = xmlStrdup(PublicID);
2088
2.11k
        if (ret->PublicID == NULL)
2089
16
            goto mem_error;
2090
2.11k
    }
2091
2092
    /*
2093
     * Validity Check:
2094
     * Check the DTD for previous declarations of the ATTLIST
2095
     */
2096
4.15k
    res = xmlHashAdd(table, name, ret);
2097
4.15k
    if (res <= 0) {
2098
2.96k
        if (res < 0)
2099
46
            goto mem_error;
2100
2.92k
#ifdef LIBXML_VALID_ENABLED
2101
2.92k
        xmlErrValid(ctxt, XML_DTD_NOTATION_REDEFINED,
2102
2.92k
                    "xmlAddNotationDecl: %s already defined\n",
2103
2.92k
                    (const char *) name);
2104
2.92k
#endif /* LIBXML_VALID_ENABLED */
2105
2.92k
  xmlFreeNotation(ret);
2106
2.92k
  return(NULL);
2107
2.96k
    }
2108
1.18k
    return(ret);
2109
2110
143
mem_error:
2111
143
    xmlVErrMemory(ctxt);
2112
143
    xmlFreeNotation(ret);
2113
143
    return(NULL);
2114
4.15k
}
2115
2116
static void
2117
1.26k
xmlFreeNotationTableEntry(void *nota, const xmlChar *name ATTRIBUTE_UNUSED) {
2118
1.26k
    xmlFreeNotation((xmlNotationPtr) nota);
2119
1.26k
}
2120
2121
/**
2122
 * xmlFreeNotationTable:
2123
 * @table:  An notation table
2124
 *
2125
 * Deallocate the memory used by an entities hash table.
2126
 */
2127
void
2128
1.06k
xmlFreeNotationTable(xmlNotationTablePtr table) {
2129
1.06k
    xmlHashFree(table, xmlFreeNotationTableEntry);
2130
1.06k
}
2131
2132
#ifdef LIBXML_TREE_ENABLED
2133
/**
2134
 * xmlCopyNotation:
2135
 * @nota:  A notation
2136
 *
2137
 * Build a copy of a notation.
2138
 *
2139
 * Returns the new xmlNotationPtr or NULL in case of error.
2140
 */
2141
static void *
2142
88
xmlCopyNotation(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2143
88
    xmlNotationPtr nota = (xmlNotationPtr) payload;
2144
88
    xmlNotationPtr cur;
2145
2146
88
    cur = (xmlNotationPtr) xmlMalloc(sizeof(xmlNotation));
2147
88
    if (cur == NULL)
2148
1
  return(NULL);
2149
87
    memset(cur, 0, sizeof(*cur));
2150
87
    if (nota->name != NULL) {
2151
87
  cur->name = xmlStrdup(nota->name);
2152
87
        if (cur->name == NULL)
2153
1
            goto error;
2154
87
    }
2155
86
    if (nota->PublicID != NULL) {
2156
42
  cur->PublicID = xmlStrdup(nota->PublicID);
2157
42
        if (cur->PublicID == NULL)
2158
1
            goto error;
2159
42
    }
2160
85
    if (nota->SystemID != NULL) {
2161
45
  cur->SystemID = xmlStrdup(nota->SystemID);
2162
45
        if (cur->SystemID == NULL)
2163
1
            goto error;
2164
45
    }
2165
84
    return(cur);
2166
2167
3
error:
2168
3
    xmlFreeNotation(cur);
2169
3
    return(NULL);
2170
85
}
2171
2172
/**
2173
 * xmlCopyNotationTable:
2174
 * @table:  A notation table
2175
 *
2176
 * Build a copy of a notation table.
2177
 *
2178
 * Returns the new xmlNotationTablePtr or NULL in case of error.
2179
 */
2180
xmlNotationTablePtr
2181
66
xmlCopyNotationTable(xmlNotationTablePtr table) {
2182
66
    return(xmlHashCopySafe(table, xmlCopyNotation, xmlFreeNotationTableEntry));
2183
66
}
2184
#endif /* LIBXML_TREE_ENABLED */
2185
2186
#ifdef LIBXML_OUTPUT_ENABLED
2187
/**
2188
 * xmlDumpNotationDecl:
2189
 * @buf:  the XML buffer output
2190
 * @nota:  A notation declaration
2191
 *
2192
 * DEPRECATED: Don't use.
2193
 *
2194
 * This will dump the content the notation declaration as an XML DTD definition
2195
 */
2196
void
2197
0
xmlDumpNotationDecl(xmlBufferPtr buf, xmlNotationPtr nota) {
2198
0
    xmlSaveCtxtPtr save;
2199
2200
0
    if ((buf == NULL) || (nota == NULL))
2201
0
        return;
2202
2203
0
    save = xmlSaveToBuffer(buf, NULL, 0);
2204
0
    xmlSaveNotationDecl(save, nota);
2205
0
    xmlSaveClose(save);
2206
0
}
2207
2208
/**
2209
 * xmlDumpNotationTable:
2210
 * @buf:  the XML buffer output
2211
 * @table:  A notation table
2212
 *
2213
 * DEPRECATED: Don't use.
2214
 *
2215
 * This will dump the content of the notation table as an XML DTD definition
2216
 */
2217
void
2218
0
xmlDumpNotationTable(xmlBufferPtr buf, xmlNotationTablePtr table) {
2219
0
    xmlSaveCtxtPtr save;
2220
2221
0
    if ((buf == NULL) || (table == NULL))
2222
0
        return;
2223
2224
0
    save = xmlSaveToBuffer(buf, NULL, 0);
2225
0
    xmlSaveNotationTable(save, table);
2226
0
    xmlSaveClose(save);
2227
0
}
2228
#endif /* LIBXML_OUTPUT_ENABLED */
2229
2230
/************************************************************************
2231
 *                  *
2232
 *        IDs         *
2233
 *                  *
2234
 ************************************************************************/
2235
/**
2236
 * DICT_FREE:
2237
 * @str:  a string
2238
 *
2239
 * Free a string if it is not owned by the "dict" dictionary in the
2240
 * current scope
2241
 */
2242
#define DICT_FREE(str)            \
2243
54.2k
  if ((str) && ((!dict) ||       \
2244
54.2k
      (xmlDictOwns(dict, (const xmlChar *)(str)) == 0)))  \
2245
54.2k
      xmlFree((char *)(str));
2246
2247
static int
2248
742k
xmlIsStreaming(xmlValidCtxtPtr ctxt) {
2249
742k
    xmlParserCtxtPtr pctxt;
2250
2251
742k
    if (ctxt == NULL)
2252
3.34k
        return(0);
2253
739k
    if ((ctxt->flags & XML_VCTXT_USE_PCTXT) == 0)
2254
93.1k
        return(0);
2255
646k
    pctxt = ctxt->userData;
2256
646k
    return(pctxt->parseMode == XML_PARSE_READER);
2257
739k
}
2258
2259
/**
2260
 * xmlFreeID:
2261
 * @not:  A id
2262
 *
2263
 * Deallocate the memory used by an id definition
2264
 */
2265
static void
2266
49.3k
xmlFreeID(xmlIDPtr id) {
2267
49.3k
    xmlDictPtr dict = NULL;
2268
2269
49.3k
    if (id == NULL) return;
2270
2271
49.3k
    if (id->doc != NULL)
2272
49.3k
        dict = id->doc->dict;
2273
2274
49.3k
    if (id->value != NULL)
2275
49.3k
  DICT_FREE(id->value)
2276
49.3k
    if (id->name != NULL)
2277
4.89k
  DICT_FREE(id->name)
2278
49.3k
    xmlFree(id);
2279
49.3k
}
2280
2281
2282
/**
2283
 * xmlAddIDSafe:
2284
 * @doc:  pointer to the document
2285
 * @value:  the value name
2286
 * @attr:  the attribute holding the ID
2287
 * @id:  pointer to new xmlIdPtr (optional)
2288
 *
2289
 * Register a new id declaration
2290
 *
2291
 * Returns 1 on success, 0 if the ID already exists, -1 if a memory
2292
 * allocation fails.
2293
 */
2294
int
2295
xmlAddIDSafe(xmlDocPtr doc, const xmlChar *value, xmlAttrPtr attr,
2296
346k
             int streaming, xmlIDPtr *id) {
2297
346k
    xmlIDPtr ret;
2298
346k
    xmlIDTablePtr table;
2299
2300
346k
    if (id != NULL)
2301
260k
        *id = NULL;
2302
2303
346k
    if (doc == NULL) {
2304
0
  return(-1);
2305
0
    }
2306
346k
    if ((value == NULL) || (value[0] == 0)) {
2307
49.6k
  return(0);
2308
49.6k
    }
2309
296k
    if (attr == NULL) {
2310
0
  return(-1);
2311
0
    }
2312
2313
    /*
2314
     * Create the ID table if needed.
2315
     */
2316
296k
    table = (xmlIDTablePtr) doc->ids;
2317
296k
    if (table == NULL)  {
2318
11.2k
        doc->ids = table = xmlHashCreateDict(0, doc->dict);
2319
11.2k
        if (table == NULL)
2320
48
            return(-1);
2321
285k
    } else {
2322
285k
        ret = xmlHashLookup(table, value);
2323
285k
        if (ret != NULL) {
2324
            /*
2325
             * Update the attribute to make entities work.
2326
             */
2327
247k
            if (!streaming) {
2328
202k
                if (ret->attr != NULL) {
2329
202k
                    ret->attr->id = NULL;
2330
202k
                    ret->attr = attr;
2331
202k
                }
2332
202k
                attr->id = ret;
2333
202k
            }
2334
247k
      attr->atype = XML_ATTRIBUTE_ID;
2335
247k
            return(0);
2336
247k
        }
2337
285k
    }
2338
2339
49.4k
    ret = (xmlIDPtr) xmlMalloc(sizeof(xmlID));
2340
49.4k
    if (ret == NULL)
2341
56
  return(-1);
2342
49.3k
    memset(ret, 0, sizeof(*ret));
2343
2344
    /*
2345
     * fill the structure.
2346
     */
2347
49.3k
    ret->doc = doc;
2348
49.3k
    ret->value = xmlStrdup(value);
2349
49.3k
    if (ret->value == NULL) {
2350
50
        xmlFreeID(ret);
2351
50
        return(-1);
2352
50
    }
2353
49.3k
    if (streaming) {
2354
  /*
2355
   * Operating in streaming mode, attr is gonna disappear
2356
   */
2357
4.90k
  if (doc->dict != NULL)
2358
3.46k
      ret->name = xmlDictLookup(doc->dict, attr->name, -1);
2359
1.43k
  else
2360
1.43k
      ret->name = xmlStrdup(attr->name);
2361
4.90k
        if (ret->name == NULL) {
2362
10
            xmlFreeID(ret);
2363
10
            return(-1);
2364
10
        }
2365
4.89k
  ret->attr = NULL;
2366
44.4k
    } else {
2367
44.4k
  ret->attr = attr;
2368
44.4k
  ret->name = NULL;
2369
44.4k
    }
2370
49.3k
    ret->lineno = xmlGetLineNo(attr->parent);
2371
2372
49.3k
    if (xmlHashAddEntry(table, value, ret) < 0) {
2373
90
  xmlFreeID(ret);
2374
90
  return(-1);
2375
90
    }
2376
2377
49.2k
    attr->atype = XML_ATTRIBUTE_ID;
2378
49.2k
    if (!streaming)
2379
44.3k
        attr->id = ret;
2380
2381
49.2k
    if (id != NULL)
2382
42.6k
        *id = ret;
2383
49.2k
    return(1);
2384
49.3k
}
2385
2386
/**
2387
 * xmlAddID:
2388
 * @ctxt:  the validation context
2389
 * @doc:  pointer to the document
2390
 * @value:  the value name
2391
 * @attr:  the attribute holding the ID
2392
 *
2393
 * Register a new id declaration
2394
 *
2395
 * Returns NULL if not, otherwise the new xmlIDPtr
2396
 */
2397
xmlIDPtr
2398
xmlAddID(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2399
260k
         xmlAttrPtr attr) {
2400
260k
    xmlIDPtr id;
2401
260k
    int res;
2402
2403
260k
    res = xmlAddIDSafe(doc, value, attr, xmlIsStreaming(ctxt), &id);
2404
260k
    if (res < 0) {
2405
233
        xmlVErrMemory(ctxt);
2406
233
    }
2407
259k
#ifdef LIBXML_VALID_ENABLED
2408
259k
    else if (res == 0) {
2409
217k
        if (ctxt != NULL) {
2410
            /*
2411
             * The id is already defined in this DTD.
2412
             */
2413
214k
            xmlErrValidNode(ctxt, attr->parent, XML_DTD_ID_REDEFINED,
2414
214k
                            "ID %s already defined\n", value, NULL, NULL);
2415
214k
        }
2416
217k
    }
2417
260k
#endif /* LIBXML_VALID_ENABLED */
2418
2419
260k
    return(id);
2420
260k
}
2421
2422
static void
2423
49.2k
xmlFreeIDTableEntry(void *id, const xmlChar *name ATTRIBUTE_UNUSED) {
2424
49.2k
    xmlFreeID((xmlIDPtr) id);
2425
49.2k
}
2426
2427
/**
2428
 * xmlFreeIDTable:
2429
 * @table:  An id table
2430
 *
2431
 * Deallocate the memory used by an ID hash table.
2432
 */
2433
void
2434
11.1k
xmlFreeIDTable(xmlIDTablePtr table) {
2435
11.1k
    xmlHashFree(table, xmlFreeIDTableEntry);
2436
11.1k
}
2437
2438
/**
2439
 * xmlIsID:
2440
 * @doc:  the document
2441
 * @elem:  the element carrying the attribute
2442
 * @attr:  the attribute
2443
 *
2444
 * Determine whether an attribute is of type ID. In case we have DTD(s)
2445
 * then this is done if DTD loading has been requested. In the case
2446
 * of HTML documents parsed with the HTML parser, then ID detection is
2447
 * done systematically.
2448
 *
2449
 * Returns 0 or 1 depending on the lookup result or -1 if a memory allocation
2450
 * failed.
2451
 */
2452
int
2453
2.11M
xmlIsID(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2454
2.11M
    if ((attr == NULL) || (attr->name == NULL)) return(0);
2455
2.11M
    if ((attr->ns != NULL) && (attr->ns->prefix != NULL) &&
2456
2.11M
        (!strcmp((char *) attr->name, "id")) &&
2457
2.11M
        (!strcmp((char *) attr->ns->prefix, "xml")))
2458
17.8k
  return(1);
2459
2.09M
    if (doc == NULL) return(0);
2460
2.09M
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL) &&
2461
2.09M
        (doc->type != XML_HTML_DOCUMENT_NODE)) {
2462
867k
  return(0);
2463
1.22M
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2464
250k
        if ((xmlStrEqual(BAD_CAST "id", attr->name)) ||
2465
250k
      ((xmlStrEqual(BAD_CAST "name", attr->name)) &&
2466
211k
      ((elem == NULL) || (xmlStrEqual(elem->name, BAD_CAST "a")))))
2467
93.8k
      return(1);
2468
156k
  return(0);
2469
977k
    } else if (elem == NULL) {
2470
0
  return(0);
2471
977k
    } else {
2472
977k
  xmlAttributePtr attrDecl = NULL;
2473
2474
977k
  xmlChar felem[50];
2475
977k
  xmlChar *fullelemname;
2476
977k
        const xmlChar *aprefix;
2477
2478
977k
  fullelemname = (elem->ns != NULL && elem->ns->prefix != NULL) ?
2479
132k
      xmlBuildQName(elem->name, elem->ns->prefix, felem, 50) :
2480
977k
      (xmlChar *)elem->name;
2481
977k
        if (fullelemname == NULL)
2482
19
            return(-1);
2483
2484
977k
        aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
2485
2486
977k
  if (fullelemname != NULL) {
2487
977k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullelemname,
2488
977k
                              attr->name, aprefix);
2489
977k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
2490
33.4k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullelemname,
2491
33.4k
                attr->name, aprefix);
2492
977k
  }
2493
2494
977k
  if ((fullelemname != felem) && (fullelemname != elem->name))
2495
3.01k
      xmlFree(fullelemname);
2496
2497
977k
        if ((attrDecl != NULL) && (attrDecl->atype == XML_ATTRIBUTE_ID))
2498
76.6k
      return(1);
2499
977k
    }
2500
900k
    return(0);
2501
2.09M
}
2502
2503
/**
2504
 * xmlRemoveID:
2505
 * @doc:  the document
2506
 * @attr:  the attribute
2507
 *
2508
 * Remove the given attribute from the ID table maintained internally.
2509
 *
2510
 * Returns -1 if the lookup failed and 0 otherwise
2511
 */
2512
int
2513
242k
xmlRemoveID(xmlDocPtr doc, xmlAttrPtr attr) {
2514
242k
    xmlIDTablePtr table;
2515
2516
242k
    if (doc == NULL) return(-1);
2517
242k
    if ((attr == NULL) || (attr->id == NULL)) return(-1);
2518
2519
43.5k
    table = (xmlIDTablePtr) doc->ids;
2520
43.5k
    if (table == NULL)
2521
41.4k
        return(-1);
2522
2523
2.12k
    if (xmlHashRemoveEntry(table, attr->id->value, xmlFreeIDTableEntry) < 0)
2524
0
        return(-1);
2525
2526
2.12k
    attr->atype = 0;
2527
2.12k
    attr->id = NULL;
2528
2529
2.12k
    return(0);
2530
2.12k
}
2531
2532
/**
2533
 * xmlGetID:
2534
 * @doc:  pointer to the document
2535
 * @ID:  the ID value
2536
 *
2537
 * Search the attribute declaring the given ID
2538
 *
2539
 * Returns NULL if not found, otherwise the xmlAttrPtr defining the ID
2540
 */
2541
xmlAttrPtr
2542
561k
xmlGetID(xmlDocPtr doc, const xmlChar *ID) {
2543
561k
    xmlIDTablePtr table;
2544
561k
    xmlIDPtr id;
2545
2546
561k
    if (doc == NULL) {
2547
0
  return(NULL);
2548
0
    }
2549
2550
561k
    if (ID == NULL) {
2551
0
  return(NULL);
2552
0
    }
2553
2554
561k
    table = (xmlIDTablePtr) doc->ids;
2555
561k
    if (table == NULL)
2556
521k
        return(NULL);
2557
2558
39.9k
    id = xmlHashLookup(table, ID);
2559
39.9k
    if (id == NULL)
2560
29.8k
  return(NULL);
2561
10.0k
    if (id->attr == NULL) {
2562
  /*
2563
   * We are operating on a stream, return a well known reference
2564
   * since the attribute node doesn't exist anymore
2565
   */
2566
2.55k
  return((xmlAttrPtr) doc);
2567
2.55k
    }
2568
7.51k
    return(id->attr);
2569
10.0k
}
2570
2571
/************************************************************************
2572
 *                  *
2573
 *        Refs          *
2574
 *                  *
2575
 ************************************************************************/
2576
typedef struct xmlRemoveMemo_t
2577
{
2578
  xmlListPtr l;
2579
  xmlAttrPtr ap;
2580
} xmlRemoveMemo;
2581
2582
typedef xmlRemoveMemo *xmlRemoveMemoPtr;
2583
2584
typedef struct xmlValidateMemo_t
2585
{
2586
    xmlValidCtxtPtr ctxt;
2587
    const xmlChar *name;
2588
} xmlValidateMemo;
2589
2590
typedef xmlValidateMemo *xmlValidateMemoPtr;
2591
2592
/**
2593
 * xmlFreeRef:
2594
 * @lk:  A list link
2595
 *
2596
 * Deallocate the memory used by a ref definition
2597
 */
2598
static void
2599
482k
xmlFreeRef(xmlLinkPtr lk) {
2600
482k
    xmlRefPtr ref = (xmlRefPtr)xmlLinkGetData(lk);
2601
482k
    if (ref == NULL) return;
2602
482k
    if (ref->value != NULL)
2603
482k
        xmlFree((xmlChar *)ref->value);
2604
482k
    if (ref->name != NULL)
2605
113k
        xmlFree((xmlChar *)ref->name);
2606
482k
    xmlFree(ref);
2607
482k
}
2608
2609
/**
2610
 * xmlFreeRefTableEntry:
2611
 * @list_ref:  A list of references.
2612
 *
2613
 * Deallocate the memory used by a list of references
2614
 */
2615
static void
2616
16.3k
xmlFreeRefTableEntry(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
2617
16.3k
    xmlListPtr list_ref = (xmlListPtr) payload;
2618
16.3k
    if (list_ref == NULL) return;
2619
16.3k
    xmlListDelete(list_ref);
2620
16.3k
}
2621
2622
/**
2623
 * xmlWalkRemoveRef:
2624
 * @data:  Contents of current link
2625
 * @user:  Value supplied by the user
2626
 *
2627
 * Returns 0 to abort the walk or 1 to continue
2628
 */
2629
static int
2630
xmlWalkRemoveRef(const void *data, void *user)
2631
0
{
2632
0
    xmlAttrPtr attr0 = ((xmlRefPtr)data)->attr;
2633
0
    xmlAttrPtr attr1 = ((xmlRemoveMemoPtr)user)->ap;
2634
0
    xmlListPtr ref_list = ((xmlRemoveMemoPtr)user)->l;
2635
2636
0
    if (attr0 == attr1) { /* Matched: remove and terminate walk */
2637
0
        xmlListRemoveFirst(ref_list, (void *)data);
2638
0
        return 0;
2639
0
    }
2640
0
    return 1;
2641
0
}
2642
2643
/**
2644
 * xmlDummyCompare
2645
 * @data0:  Value supplied by the user
2646
 * @data1:  Value supplied by the user
2647
 *
2648
 * Do nothing, return 0. Used to create unordered lists.
2649
 */
2650
static int
2651
xmlDummyCompare(const void *data0 ATTRIBUTE_UNUSED,
2652
                const void *data1 ATTRIBUTE_UNUSED)
2653
466k
{
2654
466k
    return (0);
2655
466k
}
2656
2657
/**
2658
 * xmlAddRef:
2659
 * @ctxt:  the validation context
2660
 * @doc:  pointer to the document
2661
 * @value:  the value name
2662
 * @attr:  the attribute holding the Ref
2663
 *
2664
 * DEPRECATED, do not use. This function will be removed from the public API.
2665
 *
2666
 * Register a new ref declaration
2667
 *
2668
 * Returns NULL if not, otherwise the new xmlRefPtr
2669
 */
2670
xmlRefPtr
2671
xmlAddRef(xmlValidCtxtPtr ctxt, xmlDocPtr doc, const xmlChar *value,
2672
482k
    xmlAttrPtr attr) {
2673
482k
    xmlRefPtr ret = NULL;
2674
482k
    xmlRefTablePtr table;
2675
482k
    xmlListPtr ref_list;
2676
2677
482k
    if (doc == NULL) {
2678
0
        return(NULL);
2679
0
    }
2680
482k
    if (value == NULL) {
2681
24
        return(NULL);
2682
24
    }
2683
482k
    if (attr == NULL) {
2684
0
        return(NULL);
2685
0
    }
2686
2687
    /*
2688
     * Create the Ref table if needed.
2689
     */
2690
482k
    table = (xmlRefTablePtr) doc->refs;
2691
482k
    if (table == NULL) {
2692
4.87k
        doc->refs = table = xmlHashCreateDict(0, doc->dict);
2693
4.87k
        if (table == NULL)
2694
23
            goto failed;
2695
4.87k
    }
2696
2697
482k
    ret = (xmlRefPtr) xmlMalloc(sizeof(xmlRef));
2698
482k
    if (ret == NULL)
2699
39
        goto failed;
2700
482k
    memset(ret, 0, sizeof(*ret));
2701
2702
    /*
2703
     * fill the structure.
2704
     */
2705
482k
    ret->value = xmlStrdup(value);
2706
482k
    if (ret->value == NULL)
2707
60
        goto failed;
2708
482k
    if (xmlIsStreaming(ctxt)) {
2709
  /*
2710
   * Operating in streaming mode, attr is gonna disappear
2711
   */
2712
113k
  ret->name = xmlStrdup(attr->name);
2713
113k
        if (ret->name == NULL)
2714
9
            goto failed;
2715
113k
  ret->attr = NULL;
2716
368k
    } else {
2717
368k
  ret->name = NULL;
2718
368k
  ret->attr = attr;
2719
368k
    }
2720
482k
    ret->lineno = xmlGetLineNo(attr->parent);
2721
2722
    /* To add a reference :-
2723
     * References are maintained as a list of references,
2724
     * Lookup the entry, if no entry create new nodelist
2725
     * Add the owning node to the NodeList
2726
     * Return the ref
2727
     */
2728
2729
482k
    if (NULL == (ref_list = xmlHashLookup(table, value))) {
2730
16.4k
        int res;
2731
2732
16.4k
        if (NULL == (ref_list = xmlListCreate(xmlFreeRef, xmlDummyCompare)))
2733
53
      goto failed;
2734
16.3k
        res = xmlHashAdd(table, value, ref_list);
2735
16.3k
        if (res <= 0) {
2736
52
            xmlListDelete(ref_list);
2737
52
      goto failed;
2738
52
        }
2739
16.3k
    }
2740
482k
    if (xmlListAppend(ref_list, ret) != 0)
2741
58
        goto failed;
2742
482k
    return(ret);
2743
2744
294
failed:
2745
294
    xmlVErrMemory(ctxt);
2746
294
    if (ret != NULL) {
2747
232
        if (ret->value != NULL)
2748
172
      xmlFree((char *)ret->value);
2749
232
        if (ret->name != NULL)
2750
40
      xmlFree((char *)ret->name);
2751
232
        xmlFree(ret);
2752
232
    }
2753
294
    return(NULL);
2754
482k
}
2755
2756
/**
2757
 * xmlFreeRefTable:
2758
 * @table:  An ref table
2759
 *
2760
 * DEPRECATED, do not use. This function will be removed from the public API.
2761
 *
2762
 * Deallocate the memory used by an Ref hash table.
2763
 */
2764
void
2765
4.84k
xmlFreeRefTable(xmlRefTablePtr table) {
2766
4.84k
    xmlHashFree(table, xmlFreeRefTableEntry);
2767
4.84k
}
2768
2769
/**
2770
 * xmlIsRef:
2771
 * @doc:  the document
2772
 * @elem:  the element carrying the attribute
2773
 * @attr:  the attribute
2774
 *
2775
 * DEPRECATED, do not use. This function will be removed from the public API.
2776
 *
2777
 * Determine whether an attribute is of type Ref. In case we have DTD(s)
2778
 * then this is simple, otherwise we use an heuristic: name Ref (upper
2779
 * or lowercase).
2780
 *
2781
 * Returns 0 or 1 depending on the lookup result
2782
 */
2783
int
2784
1.84M
xmlIsRef(xmlDocPtr doc, xmlNodePtr elem, xmlAttrPtr attr) {
2785
1.84M
    if (attr == NULL)
2786
0
        return(0);
2787
1.84M
    if (doc == NULL) {
2788
0
        doc = attr->doc;
2789
0
  if (doc == NULL) return(0);
2790
0
    }
2791
2792
1.84M
    if ((doc->intSubset == NULL) && (doc->extSubset == NULL)) {
2793
934k
        return(0);
2794
934k
    } else if (doc->type == XML_HTML_DOCUMENT_NODE) {
2795
        /* TODO @@@ */
2796
49.9k
        return(0);
2797
863k
    } else {
2798
863k
        xmlAttributePtr attrDecl;
2799
863k
        const xmlChar *aprefix;
2800
2801
863k
        if (elem == NULL) return(0);
2802
863k
        aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
2803
863k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name, attr->name,
2804
863k
                                      aprefix);
2805
863k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
2806
26.8k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name, attr->name,
2807
26.8k
                                          aprefix);
2808
2809
863k
  if ((attrDecl != NULL) &&
2810
863k
      (attrDecl->atype == XML_ATTRIBUTE_IDREF ||
2811
174k
       attrDecl->atype == XML_ATTRIBUTE_IDREFS))
2812
92.6k
  return(1);
2813
863k
    }
2814
770k
    return(0);
2815
1.84M
}
2816
2817
/**
2818
 * xmlRemoveRef:
2819
 * @doc:  the document
2820
 * @attr:  the attribute
2821
 *
2822
 * DEPRECATED, do not use. This function will be removed from the public API.
2823
 *
2824
 * Remove the given attribute from the Ref table maintained internally.
2825
 *
2826
 * Returns -1 if the lookup failed and 0 otherwise
2827
 */
2828
int
2829
0
xmlRemoveRef(xmlDocPtr doc, xmlAttrPtr attr) {
2830
0
    xmlListPtr ref_list;
2831
0
    xmlRefTablePtr table;
2832
0
    xmlChar *ID;
2833
0
    xmlRemoveMemo target;
2834
2835
0
    if (doc == NULL) return(-1);
2836
0
    if (attr == NULL) return(-1);
2837
2838
0
    table = (xmlRefTablePtr) doc->refs;
2839
0
    if (table == NULL)
2840
0
        return(-1);
2841
2842
0
    ID = xmlNodeListGetString(doc, attr->children, 1);
2843
0
    if (ID == NULL)
2844
0
        return(-1);
2845
2846
0
    ref_list = xmlHashLookup(table, ID);
2847
0
    if(ref_list == NULL) {
2848
0
        xmlFree(ID);
2849
0
        return (-1);
2850
0
    }
2851
2852
    /* At this point, ref_list refers to a list of references which
2853
     * have the same key as the supplied attr. Our list of references
2854
     * is ordered by reference address and we don't have that information
2855
     * here to use when removing. We'll have to walk the list and
2856
     * check for a matching attribute, when we find one stop the walk
2857
     * and remove the entry.
2858
     * The list is ordered by reference, so that means we don't have the
2859
     * key. Passing the list and the reference to the walker means we
2860
     * will have enough data to be able to remove the entry.
2861
     */
2862
0
    target.l = ref_list;
2863
0
    target.ap = attr;
2864
2865
    /* Remove the supplied attr from our list */
2866
0
    xmlListWalk(ref_list, xmlWalkRemoveRef, &target);
2867
2868
    /*If the list is empty then remove the list entry in the hash */
2869
0
    if (xmlListEmpty(ref_list))
2870
0
        xmlHashUpdateEntry(table, ID, NULL, xmlFreeRefTableEntry);
2871
0
    xmlFree(ID);
2872
0
    return(0);
2873
0
}
2874
2875
/**
2876
 * xmlGetRefs:
2877
 * @doc:  pointer to the document
2878
 * @ID:  the ID value
2879
 *
2880
 * DEPRECATED, do not use. This function will be removed from the public API.
2881
 *
2882
 * Find the set of references for the supplied ID.
2883
 *
2884
 * Returns NULL if not found, otherwise node set for the ID.
2885
 */
2886
xmlListPtr
2887
0
xmlGetRefs(xmlDocPtr doc, const xmlChar *ID) {
2888
0
    xmlRefTablePtr table;
2889
2890
0
    if (doc == NULL) {
2891
0
        return(NULL);
2892
0
    }
2893
2894
0
    if (ID == NULL) {
2895
0
        return(NULL);
2896
0
    }
2897
2898
0
    table = (xmlRefTablePtr) doc->refs;
2899
0
    if (table == NULL)
2900
0
        return(NULL);
2901
2902
0
    return (xmlHashLookup(table, ID));
2903
0
}
2904
2905
/************************************************************************
2906
 *                  *
2907
 *    Routines for validity checking        *
2908
 *                  *
2909
 ************************************************************************/
2910
2911
/**
2912
 * xmlGetDtdElementDesc:
2913
 * @dtd:  a pointer to the DtD to search
2914
 * @name:  the element name
2915
 *
2916
 * Search the DTD for the description of this element
2917
 *
2918
 * NOTE: A NULL return value can also mean that a memory allocation failed.
2919
 *
2920
 * returns the xmlElementPtr if found or NULL
2921
 */
2922
2923
xmlElementPtr
2924
0
xmlGetDtdElementDesc(xmlDtdPtr dtd, const xmlChar *name) {
2925
0
    xmlElementTablePtr table;
2926
0
    xmlElementPtr cur;
2927
0
    xmlChar *uqname = NULL, *prefix = NULL;
2928
2929
0
    if ((dtd == NULL) || (name == NULL)) return(NULL);
2930
0
    if (dtd->elements == NULL)
2931
0
  return(NULL);
2932
0
    table = (xmlElementTablePtr) dtd->elements;
2933
2934
0
    uqname = xmlSplitQName2(name, &prefix);
2935
0
    if (uqname != NULL)
2936
0
        name = uqname;
2937
0
    cur = xmlHashLookup2(table, name, prefix);
2938
0
    if (prefix != NULL) xmlFree(prefix);
2939
0
    if (uqname != NULL) xmlFree(uqname);
2940
0
    return(cur);
2941
0
}
2942
2943
/**
2944
 * xmlGetDtdElementDesc2:
2945
 * @dtd:  a pointer to the DtD to search
2946
 * @name:  the element name
2947
 * @create:  create an empty description if not found
2948
 *
2949
 * Search the DTD for the description of this element
2950
 *
2951
 * returns the xmlElementPtr if found or NULL
2952
 */
2953
2954
static xmlElementPtr
2955
247k
xmlGetDtdElementDesc2(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd, const xmlChar *name) {
2956
247k
    xmlElementTablePtr table;
2957
247k
    xmlElementPtr cur = NULL;
2958
247k
    const xmlChar *localName;
2959
247k
    xmlChar *prefix = NULL;
2960
2961
247k
    if (dtd == NULL) return(NULL);
2962
2963
    /*
2964
     * Create the Element table if needed.
2965
     */
2966
247k
    if (dtd->elements == NULL) {
2967
28.2k
  xmlDictPtr dict = NULL;
2968
2969
28.2k
  if (dtd->doc != NULL)
2970
28.2k
      dict = dtd->doc->dict;
2971
2972
28.2k
  dtd->elements = xmlHashCreateDict(0, dict);
2973
28.2k
  if (dtd->elements == NULL)
2974
63
            goto mem_error;
2975
28.2k
    }
2976
247k
    table = (xmlElementTablePtr) dtd->elements;
2977
2978
247k
    localName = xmlSplitQName4(name, &prefix);
2979
247k
    if (localName == NULL)
2980
17
        goto mem_error;
2981
247k
    cur = xmlHashLookup2(table, localName, prefix);
2982
247k
    if (cur == NULL) {
2983
32.4k
  cur = (xmlElementPtr) xmlMalloc(sizeof(xmlElement));
2984
32.4k
  if (cur == NULL)
2985
57
            goto mem_error;
2986
32.4k
  memset(cur, 0, sizeof(xmlElement));
2987
32.4k
  cur->type = XML_ELEMENT_DECL;
2988
2989
  /*
2990
   * fill the structure.
2991
   */
2992
32.4k
  cur->name = xmlStrdup(localName);
2993
32.4k
        if (cur->name == NULL)
2994
60
            goto mem_error;
2995
32.3k
  cur->prefix = prefix;
2996
32.3k
        prefix = NULL;
2997
32.3k
  cur->etype = XML_ELEMENT_TYPE_UNDEFINED;
2998
2999
32.3k
  if (xmlHashAdd2(table, localName, cur->prefix, cur) <= 0)
3000
123
            goto mem_error;
3001
32.3k
    }
3002
3003
247k
    if (prefix != NULL)
3004
31.5k
        xmlFree(prefix);
3005
247k
    return(cur);
3006
3007
320
mem_error:
3008
320
    xmlVErrMemory(ctxt);
3009
320
    xmlFree(prefix);
3010
320
    xmlFreeElement(cur);
3011
320
    return(NULL);
3012
247k
}
3013
3014
/**
3015
 * xmlGetDtdQElementDesc:
3016
 * @dtd:  a pointer to the DtD to search
3017
 * @name:  the element name
3018
 * @prefix:  the element namespace prefix
3019
 *
3020
 * Search the DTD for the description of this element
3021
 *
3022
 * returns the xmlElementPtr if found or NULL
3023
 */
3024
3025
xmlElementPtr
3026
xmlGetDtdQElementDesc(xmlDtdPtr dtd, const xmlChar *name,
3027
2.41M
                const xmlChar *prefix) {
3028
2.41M
    xmlElementTablePtr table;
3029
3030
2.41M
    if (dtd == NULL) return(NULL);
3031
1.94M
    if (dtd->elements == NULL) return(NULL);
3032
1.59M
    table = (xmlElementTablePtr) dtd->elements;
3033
3034
1.59M
    return(xmlHashLookup2(table, name, prefix));
3035
1.94M
}
3036
3037
/**
3038
 * xmlGetDtdAttrDesc:
3039
 * @dtd:  a pointer to the DtD to search
3040
 * @elem:  the element name
3041
 * @name:  the attribute name
3042
 *
3043
 * Search the DTD for the description of this attribute on
3044
 * this element.
3045
 *
3046
 * returns the xmlAttributePtr if found or NULL
3047
 */
3048
3049
xmlAttributePtr
3050
3.78k
xmlGetDtdAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name) {
3051
3.78k
    xmlAttributeTablePtr table;
3052
3.78k
    xmlAttributePtr cur;
3053
3.78k
    xmlChar *uqname = NULL, *prefix = NULL;
3054
3055
3.78k
    if (dtd == NULL) return(NULL);
3056
3.78k
    if (dtd->attributes == NULL) return(NULL);
3057
3058
0
    table = (xmlAttributeTablePtr) dtd->attributes;
3059
0
    if (table == NULL)
3060
0
  return(NULL);
3061
3062
0
    uqname = xmlSplitQName2(name, &prefix);
3063
3064
0
    if (uqname != NULL) {
3065
0
  cur = xmlHashLookup3(table, uqname, prefix, elem);
3066
0
  if (prefix != NULL) xmlFree(prefix);
3067
0
  if (uqname != NULL) xmlFree(uqname);
3068
0
    } else
3069
0
  cur = xmlHashLookup3(table, name, NULL, elem);
3070
0
    return(cur);
3071
0
}
3072
3073
/**
3074
 * xmlGetDtdQAttrDesc:
3075
 * @dtd:  a pointer to the DtD to search
3076
 * @elem:  the element name
3077
 * @name:  the attribute name
3078
 * @prefix:  the attribute namespace prefix
3079
 *
3080
 * Search the DTD for the description of this qualified attribute on
3081
 * this element.
3082
 *
3083
 * returns the xmlAttributePtr if found or NULL
3084
 */
3085
3086
xmlAttributePtr
3087
xmlGetDtdQAttrDesc(xmlDtdPtr dtd, const xmlChar *elem, const xmlChar *name,
3088
3.28M
            const xmlChar *prefix) {
3089
3.28M
    xmlAttributeTablePtr table;
3090
3091
3.28M
    if (dtd == NULL) return(NULL);
3092
3.28M
    if (dtd->attributes == NULL) return(NULL);
3093
2.63M
    table = (xmlAttributeTablePtr) dtd->attributes;
3094
3095
2.63M
    return(xmlHashLookup3(table, name, prefix, elem));
3096
3.28M
}
3097
3098
/**
3099
 * xmlGetDtdNotationDesc:
3100
 * @dtd:  a pointer to the DtD to search
3101
 * @name:  the notation name
3102
 *
3103
 * Search the DTD for the description of this notation
3104
 *
3105
 * returns the xmlNotationPtr if found or NULL
3106
 */
3107
3108
xmlNotationPtr
3109
8.73k
xmlGetDtdNotationDesc(xmlDtdPtr dtd, const xmlChar *name) {
3110
8.73k
    xmlNotationTablePtr table;
3111
3112
8.73k
    if (dtd == NULL) return(NULL);
3113
8.19k
    if (dtd->notations == NULL) return(NULL);
3114
1.79k
    table = (xmlNotationTablePtr) dtd->notations;
3115
3116
1.79k
    return(xmlHashLookup(table, name));
3117
8.19k
}
3118
3119
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
3120
/**
3121
 * xmlValidateNotationUse:
3122
 * @ctxt:  the validation context
3123
 * @doc:  the document
3124
 * @notationName:  the notation name to check
3125
 *
3126
 * Validate that the given name match a notation declaration.
3127
 * - [ VC: Notation Declared ]
3128
 *
3129
 * returns 1 if valid or 0 otherwise
3130
 */
3131
3132
int
3133
xmlValidateNotationUse(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3134
425
                       const xmlChar *notationName) {
3135
425
    xmlNotationPtr notaDecl;
3136
425
    if ((doc == NULL) || (doc->intSubset == NULL) ||
3137
425
        (notationName == NULL)) return(-1);
3138
3139
424
    notaDecl = xmlGetDtdNotationDesc(doc->intSubset, notationName);
3140
424
    if ((notaDecl == NULL) && (doc->extSubset != NULL))
3141
31
  notaDecl = xmlGetDtdNotationDesc(doc->extSubset, notationName);
3142
3143
424
    if ((notaDecl == NULL) && (ctxt != NULL)) {
3144
386
  xmlErrValidNode(ctxt, (xmlNodePtr) doc, XML_DTD_UNKNOWN_NOTATION,
3145
386
                  "NOTATION %s is not declared\n",
3146
386
            notationName, NULL, NULL);
3147
386
  return(0);
3148
386
    }
3149
38
    return(1);
3150
424
}
3151
#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */
3152
3153
/**
3154
 * xmlIsMixedElement:
3155
 * @doc:  the document
3156
 * @name:  the element name
3157
 *
3158
 * Search in the DtDs whether an element accept Mixed content (or ANY)
3159
 * basically if it is supposed to accept text childs
3160
 *
3161
 * returns 0 if no, 1 if yes, and -1 if no element description is available
3162
 */
3163
3164
int
3165
0
xmlIsMixedElement(xmlDocPtr doc, const xmlChar *name) {
3166
0
    xmlElementPtr elemDecl;
3167
3168
0
    if ((doc == NULL) || (doc->intSubset == NULL)) return(-1);
3169
3170
0
    elemDecl = xmlGetDtdElementDesc(doc->intSubset, name);
3171
0
    if ((elemDecl == NULL) && (doc->extSubset != NULL))
3172
0
  elemDecl = xmlGetDtdElementDesc(doc->extSubset, name);
3173
0
    if (elemDecl == NULL) return(-1);
3174
0
    switch (elemDecl->etype) {
3175
0
  case XML_ELEMENT_TYPE_UNDEFINED:
3176
0
      return(-1);
3177
0
  case XML_ELEMENT_TYPE_ELEMENT:
3178
0
      return(0);
3179
0
        case XML_ELEMENT_TYPE_EMPTY:
3180
      /*
3181
       * return 1 for EMPTY since we want VC error to pop up
3182
       * on <empty>     </empty> for example
3183
       */
3184
0
  case XML_ELEMENT_TYPE_ANY:
3185
0
  case XML_ELEMENT_TYPE_MIXED:
3186
0
      return(1);
3187
0
    }
3188
0
    return(1);
3189
0
}
3190
3191
#ifdef LIBXML_VALID_ENABLED
3192
3193
/**
3194
 * xmlValidNormalizeString:
3195
 * @str: a string
3196
 *
3197
 * Normalize a string in-place.
3198
 */
3199
static void
3200
59.1k
xmlValidNormalizeString(xmlChar *str) {
3201
59.1k
    xmlChar *dst;
3202
59.1k
    const xmlChar *src;
3203
3204
59.1k
    if (str == NULL)
3205
0
        return;
3206
59.1k
    src = str;
3207
59.1k
    dst = str;
3208
3209
92.3k
    while (*src == 0x20) src++;
3210
827k
    while (*src != 0) {
3211
768k
  if (*src == 0x20) {
3212
153k
      while (*src == 0x20) src++;
3213
53.0k
      if (*src != 0)
3214
48.9k
    *dst++ = 0x20;
3215
715k
  } else {
3216
715k
      *dst++ = *src++;
3217
715k
  }
3218
768k
    }
3219
59.1k
    *dst = 0;
3220
59.1k
}
3221
3222
/**
3223
 * xmlCtxtGetDtdElementDesc:
3224
 * @ctxt:  validation context
3225
 * @dtd:  a pointer to the DtD to search
3226
 * @name:  the element name
3227
 *
3228
 * Search the DTD for the description of this element
3229
 *
3230
 * returns the xmlElementPtr if found or NULL
3231
 */
3232
3233
static xmlElementPtr
3234
xmlCtxtGetDtdElementDesc(xmlValidCtxtPtr ctxt, xmlDtdPtr dtd,
3235
92.0k
                         const xmlChar *name) {
3236
92.0k
    xmlElementTablePtr table;
3237
92.0k
    xmlElementPtr cur;
3238
92.0k
    const xmlChar *localName;
3239
92.0k
    xmlChar *prefix;
3240
3241
92.0k
    if ((dtd == NULL) || (name == NULL)) return(NULL);
3242
70.1k
    if (dtd->elements == NULL)
3243
15.6k
  return(NULL);
3244
54.5k
    table = (xmlElementTablePtr) dtd->elements;
3245
3246
54.5k
    localName = xmlSplitQName4(name, &prefix);
3247
54.5k
    if (localName == NULL) {
3248
30
        xmlVErrMemory(ctxt);
3249
30
        return(NULL);
3250
30
    }
3251
54.4k
    cur = xmlHashLookup2(table, localName, prefix);
3252
54.4k
    if (prefix != NULL)
3253
514
        xmlFree(prefix);
3254
54.4k
    return(cur);
3255
54.5k
}
3256
3257
static int
3258
603k
xmlIsDocNameStartChar(xmlDocPtr doc, int c) {
3259
603k
    if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3260
        /*
3261
   * Use the new checks of production [4] [4a] amd [5] of the
3262
   * Update 5 of XML-1.0
3263
   */
3264
249k
  if (((c >= 'a') && (c <= 'z')) ||
3265
249k
      ((c >= 'A') && (c <= 'Z')) ||
3266
249k
      (c == '_') || (c == ':') ||
3267
249k
      ((c >= 0xC0) && (c <= 0xD6)) ||
3268
249k
      ((c >= 0xD8) && (c <= 0xF6)) ||
3269
249k
      ((c >= 0xF8) && (c <= 0x2FF)) ||
3270
249k
      ((c >= 0x370) && (c <= 0x37D)) ||
3271
249k
      ((c >= 0x37F) && (c <= 0x1FFF)) ||
3272
249k
      ((c >= 0x200C) && (c <= 0x200D)) ||
3273
249k
      ((c >= 0x2070) && (c <= 0x218F)) ||
3274
249k
      ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3275
249k
      ((c >= 0x3001) && (c <= 0xD7FF)) ||
3276
249k
      ((c >= 0xF900) && (c <= 0xFDCF)) ||
3277
249k
      ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3278
249k
      ((c >= 0x10000) && (c <= 0xEFFFF)))
3279
147k
      return(1);
3280
354k
    } else {
3281
354k
        if (IS_LETTER(c) || (c == '_') || (c == ':'))
3282
109k
      return(1);
3283
354k
    }
3284
346k
    return(0);
3285
603k
}
3286
3287
static int
3288
1.97M
xmlIsDocNameChar(xmlDocPtr doc, int c) {
3289
1.97M
    if ((doc == NULL) || (doc->properties & XML_DOC_OLD10) == 0) {
3290
        /*
3291
   * Use the new checks of production [4] [4a] amd [5] of the
3292
   * Update 5 of XML-1.0
3293
   */
3294
1.51M
  if (((c >= 'a') && (c <= 'z')) ||
3295
1.51M
      ((c >= 'A') && (c <= 'Z')) ||
3296
1.51M
      ((c >= '0') && (c <= '9')) || /* !start */
3297
1.51M
      (c == '_') || (c == ':') ||
3298
1.51M
      (c == '-') || (c == '.') || (c == 0xB7) || /* !start */
3299
1.51M
      ((c >= 0xC0) && (c <= 0xD6)) ||
3300
1.51M
      ((c >= 0xD8) && (c <= 0xF6)) ||
3301
1.51M
      ((c >= 0xF8) && (c <= 0x2FF)) ||
3302
1.51M
      ((c >= 0x300) && (c <= 0x36F)) || /* !start */
3303
1.51M
      ((c >= 0x370) && (c <= 0x37D)) ||
3304
1.51M
      ((c >= 0x37F) && (c <= 0x1FFF)) ||
3305
1.51M
      ((c >= 0x200C) && (c <= 0x200D)) ||
3306
1.51M
      ((c >= 0x203F) && (c <= 0x2040)) || /* !start */
3307
1.51M
      ((c >= 0x2070) && (c <= 0x218F)) ||
3308
1.51M
      ((c >= 0x2C00) && (c <= 0x2FEF)) ||
3309
1.51M
      ((c >= 0x3001) && (c <= 0xD7FF)) ||
3310
1.51M
      ((c >= 0xF900) && (c <= 0xFDCF)) ||
3311
1.51M
      ((c >= 0xFDF0) && (c <= 0xFFFD)) ||
3312
1.51M
      ((c >= 0x10000) && (c <= 0xEFFFF)))
3313
1.28M
       return(1);
3314
1.51M
    } else {
3315
451k
        if ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
3316
451k
            (c == '.') || (c == '-') ||
3317
451k
      (c == '_') || (c == ':') ||
3318
451k
      (IS_COMBINING(c)) ||
3319
451k
      (IS_EXTENDER(c)))
3320
325k
      return(1);
3321
451k
    }
3322
358k
    return(0);
3323
1.97M
}
3324
3325
/**
3326
 * xmlValidateNameValue:
3327
 * @doc:  pointer to the document or NULL
3328
 * @value:  an Name value
3329
 *
3330
 * Validate that the given value match Name production
3331
 *
3332
 * returns 1 if valid or 0 otherwise
3333
 */
3334
3335
static int
3336
186k
xmlValidateNameValueInternal(xmlDocPtr doc, const xmlChar *value) {
3337
186k
    const xmlChar *cur;
3338
186k
    int val, len;
3339
3340
186k
    if (value == NULL) return(0);
3341
186k
    cur = value;
3342
186k
    val = xmlStringCurrentChar(NULL, cur, &len);
3343
186k
    cur += len;
3344
186k
    if (!xmlIsDocNameStartChar(doc, val))
3345
64.4k
  return(0);
3346
3347
122k
    val = xmlStringCurrentChar(NULL, cur, &len);
3348
122k
    cur += len;
3349
597k
    while (xmlIsDocNameChar(doc, val)) {
3350
475k
  val = xmlStringCurrentChar(NULL, cur, &len);
3351
475k
  cur += len;
3352
475k
    }
3353
3354
122k
    if (val != 0) return(0);
3355
3356
91.5k
    return(1);
3357
122k
}
3358
3359
/**
3360
 * xmlValidateNameValue:
3361
 * @value:  an Name value
3362
 *
3363
 * Validate that the given value match Name production
3364
 *
3365
 * returns 1 if valid or 0 otherwise
3366
 */
3367
3368
int
3369
0
xmlValidateNameValue(const xmlChar *value) {
3370
0
    return(xmlValidateNameValueInternal(NULL, value));
3371
0
}
3372
3373
/**
3374
 * xmlValidateNamesValueInternal:
3375
 * @doc:  pointer to the document or NULL
3376
 * @value:  an Names value
3377
 *
3378
 * Validate that the given value match Names production
3379
 *
3380
 * returns 1 if valid or 0 otherwise
3381
 */
3382
3383
static int
3384
380k
xmlValidateNamesValueInternal(xmlDocPtr doc, const xmlChar *value) {
3385
380k
    const xmlChar *cur;
3386
380k
    int val, len;
3387
3388
380k
    if (value == NULL) return(0);
3389
380k
    cur = value;
3390
380k
    val = xmlStringCurrentChar(NULL, cur, &len);
3391
380k
    cur += len;
3392
3393
380k
    if (!xmlIsDocNameStartChar(doc, val))
3394
277k
  return(0);
3395
3396
102k
    val = xmlStringCurrentChar(NULL, cur, &len);
3397
102k
    cur += len;
3398
516k
    while (xmlIsDocNameChar(doc, val)) {
3399
413k
  val = xmlStringCurrentChar(NULL, cur, &len);
3400
413k
  cur += len;
3401
413k
    }
3402
3403
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3404
134k
    while (val == 0x20) {
3405
72.3k
  while (val == 0x20) {
3406
36.4k
      val = xmlStringCurrentChar(NULL, cur, &len);
3407
36.4k
      cur += len;
3408
36.4k
  }
3409
3410
35.9k
  if (!xmlIsDocNameStartChar(doc, val))
3411
4.49k
      return(0);
3412
3413
31.4k
  val = xmlStringCurrentChar(NULL, cur, &len);
3414
31.4k
  cur += len;
3415
3416
456k
  while (xmlIsDocNameChar(doc, val)) {
3417
425k
      val = xmlStringCurrentChar(NULL, cur, &len);
3418
425k
      cur += len;
3419
425k
  }
3420
31.4k
    }
3421
3422
98.3k
    if (val != 0) return(0);
3423
3424
8.90k
    return(1);
3425
98.3k
}
3426
3427
/**
3428
 * xmlValidateNamesValue:
3429
 * @value:  an Names value
3430
 *
3431
 * Validate that the given value match Names production
3432
 *
3433
 * returns 1 if valid or 0 otherwise
3434
 */
3435
3436
int
3437
0
xmlValidateNamesValue(const xmlChar *value) {
3438
0
    return(xmlValidateNamesValueInternal(NULL, value));
3439
0
}
3440
3441
/**
3442
 * xmlValidateNmtokenValueInternal:
3443
 * @doc:  pointer to the document or NULL
3444
 * @value:  an Nmtoken value
3445
 *
3446
 * Validate that the given value match Nmtoken production
3447
 *
3448
 * [ VC: Name Token ]
3449
 *
3450
 * returns 1 if valid or 0 otherwise
3451
 */
3452
3453
static int
3454
7.76k
xmlValidateNmtokenValueInternal(xmlDocPtr doc, const xmlChar *value) {
3455
7.76k
    const xmlChar *cur;
3456
7.76k
    int val, len;
3457
3458
7.76k
    if (value == NULL) return(0);
3459
7.76k
    cur = value;
3460
7.76k
    val = xmlStringCurrentChar(NULL, cur, &len);
3461
7.76k
    cur += len;
3462
3463
7.76k
    if (!xmlIsDocNameChar(doc, val))
3464
1.07k
  return(0);
3465
3466
6.68k
    val = xmlStringCurrentChar(NULL, cur, &len);
3467
6.68k
    cur += len;
3468
22.4k
    while (xmlIsDocNameChar(doc, val)) {
3469
15.8k
  val = xmlStringCurrentChar(NULL, cur, &len);
3470
15.8k
  cur += len;
3471
15.8k
    }
3472
3473
6.68k
    if (val != 0) return(0);
3474
3475
4.26k
    return(1);
3476
6.68k
}
3477
3478
/**
3479
 * xmlValidateNmtokenValue:
3480
 * @value:  an Nmtoken value
3481
 *
3482
 * Validate that the given value match Nmtoken production
3483
 *
3484
 * [ VC: Name Token ]
3485
 *
3486
 * returns 1 if valid or 0 otherwise
3487
 */
3488
3489
int
3490
0
xmlValidateNmtokenValue(const xmlChar *value) {
3491
0
    return(xmlValidateNmtokenValueInternal(NULL, value));
3492
0
}
3493
3494
/**
3495
 * xmlValidateNmtokensValueInternal:
3496
 * @doc:  pointer to the document or NULL
3497
 * @value:  an Nmtokens value
3498
 *
3499
 * Validate that the given value match Nmtokens production
3500
 *
3501
 * [ VC: Name Token ]
3502
 *
3503
 * returns 1 if valid or 0 otherwise
3504
 */
3505
3506
static int
3507
83.0k
xmlValidateNmtokensValueInternal(xmlDocPtr doc, const xmlChar *value) {
3508
83.0k
    const xmlChar *cur;
3509
83.0k
    int val, len;
3510
3511
83.0k
    if (value == NULL) return(0);
3512
83.0k
    cur = value;
3513
83.0k
    val = xmlStringCurrentChar(NULL, cur, &len);
3514
83.0k
    cur += len;
3515
3516
83.0k
    while (IS_BLANK(val)) {
3517
9.82k
  val = xmlStringCurrentChar(NULL, cur, &len);
3518
9.82k
  cur += len;
3519
9.82k
    }
3520
3521
83.0k
    if (!xmlIsDocNameChar(doc, val))
3522
10.9k
  return(0);
3523
3524
218k
    while (xmlIsDocNameChar(doc, val)) {
3525
146k
  val = xmlStringCurrentChar(NULL, cur, &len);
3526
146k
  cur += len;
3527
146k
    }
3528
3529
    /* Should not test IS_BLANK(val) here -- see erratum E20*/
3530
82.2k
    while (val == 0x20) {
3531
24.2k
  while (val == 0x20) {
3532
12.5k
      val = xmlStringCurrentChar(NULL, cur, &len);
3533
12.5k
      cur += len;
3534
12.5k
  }
3535
11.7k
  if (val == 0) return(1);
3536
3537
11.6k
  if (!xmlIsDocNameChar(doc, val))
3538
1.52k
      return(0);
3539
3540
10.1k
  val = xmlStringCurrentChar(NULL, cur, &len);
3541
10.1k
  cur += len;
3542
3543
56.4k
  while (xmlIsDocNameChar(doc, val)) {
3544
46.3k
      val = xmlStringCurrentChar(NULL, cur, &len);
3545
46.3k
      cur += len;
3546
46.3k
  }
3547
10.1k
    }
3548
3549
70.4k
    if (val != 0) return(0);
3550
3551
66.7k
    return(1);
3552
70.4k
}
3553
3554
/**
3555
 * xmlValidateNmtokensValue:
3556
 * @value:  an Nmtokens value
3557
 *
3558
 * Validate that the given value match Nmtokens production
3559
 *
3560
 * [ VC: Name Token ]
3561
 *
3562
 * returns 1 if valid or 0 otherwise
3563
 */
3564
3565
int
3566
0
xmlValidateNmtokensValue(const xmlChar *value) {
3567
0
    return(xmlValidateNmtokensValueInternal(NULL, value));
3568
0
}
3569
3570
/**
3571
 * xmlValidateNotationDecl:
3572
 * @ctxt:  the validation context
3573
 * @doc:  a document instance
3574
 * @nota:  a notation definition
3575
 *
3576
 * Try to validate a single notation definition
3577
 * basically it does the following checks as described by the
3578
 * XML-1.0 recommendation:
3579
 *  - it seems that no validity constraint exists on notation declarations
3580
 * But this function get called anyway ...
3581
 *
3582
 * returns 1 if valid or 0 otherwise
3583
 */
3584
3585
int
3586
xmlValidateNotationDecl(xmlValidCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlDocPtr doc ATTRIBUTE_UNUSED,
3587
472
                         xmlNotationPtr nota ATTRIBUTE_UNUSED) {
3588
472
    int ret = 1;
3589
3590
472
    return(ret);
3591
472
}
3592
3593
/**
3594
 * xmlValidateAttributeValueInternal:
3595
 * @doc: the document
3596
 * @type:  an attribute type
3597
 * @value:  an attribute value
3598
 *
3599
 * Validate that the given attribute value match  the proper production
3600
 *
3601
 * returns 1 if valid or 0 otherwise
3602
 */
3603
3604
static int
3605
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
3606
981k
                                  const xmlChar *value) {
3607
981k
    switch (type) {
3608
9.35k
  case XML_ATTRIBUTE_ENTITIES:
3609
380k
  case XML_ATTRIBUTE_IDREFS:
3610
380k
      return(xmlValidateNamesValueInternal(doc, value));
3611
20.7k
  case XML_ATTRIBUTE_ENTITY:
3612
59.7k
  case XML_ATTRIBUTE_IDREF:
3613
183k
  case XML_ATTRIBUTE_ID:
3614
186k
  case XML_ATTRIBUTE_NOTATION:
3615
186k
      return(xmlValidateNameValueInternal(doc, value));
3616
6.11k
  case XML_ATTRIBUTE_NMTOKENS:
3617
83.0k
  case XML_ATTRIBUTE_ENUMERATION:
3618
83.0k
      return(xmlValidateNmtokensValueInternal(doc, value));
3619
7.76k
  case XML_ATTRIBUTE_NMTOKEN:
3620
7.76k
      return(xmlValidateNmtokenValueInternal(doc, value));
3621
323k
        case XML_ATTRIBUTE_CDATA:
3622
323k
      break;
3623
981k
    }
3624
323k
    return(1);
3625
981k
}
3626
3627
/**
3628
 * xmlValidateAttributeValue:
3629
 * @type:  an attribute type
3630
 * @value:  an attribute value
3631
 *
3632
 * Validate that the given attribute value match  the proper production
3633
 *
3634
 * [ VC: ID ]
3635
 * Values of type ID must match the Name production....
3636
 *
3637
 * [ VC: IDREF ]
3638
 * Values of type IDREF must match the Name production, and values
3639
 * of type IDREFS must match Names ...
3640
 *
3641
 * [ VC: Entity Name ]
3642
 * Values of type ENTITY must match the Name production, values
3643
 * of type ENTITIES must match Names ...
3644
 *
3645
 * [ VC: Name Token ]
3646
 * Values of type NMTOKEN must match the Nmtoken production; values
3647
 * of type NMTOKENS must match Nmtokens.
3648
 *
3649
 * returns 1 if valid or 0 otherwise
3650
 */
3651
int
3652
0
xmlValidateAttributeValue(xmlAttributeType type, const xmlChar *value) {
3653
0
    return(xmlValidateAttributeValueInternal(NULL, type, value));
3654
0
}
3655
3656
/**
3657
 * xmlValidateAttributeValue2:
3658
 * @ctxt:  the validation context
3659
 * @doc:  the document
3660
 * @name:  the attribute name (used for error reporting only)
3661
 * @type:  the attribute type
3662
 * @value:  the attribute value
3663
 *
3664
 * Validate that the given attribute value match a given type.
3665
 * This typically cannot be done before having finished parsing
3666
 * the subsets.
3667
 *
3668
 * [ VC: IDREF ]
3669
 * Values of type IDREF must match one of the declared IDs
3670
 * Values of type IDREFS must match a sequence of the declared IDs
3671
 * each Name must match the value of an ID attribute on some element
3672
 * in the XML document; i.e. IDREF values must match the value of
3673
 * some ID attribute
3674
 *
3675
 * [ VC: Entity Name ]
3676
 * Values of type ENTITY must match one declared entity
3677
 * Values of type ENTITIES must match a sequence of declared entities
3678
 *
3679
 * [ VC: Notation Attributes ]
3680
 * all notation names in the declaration must be declared.
3681
 *
3682
 * returns 1 if valid or 0 otherwise
3683
 */
3684
3685
static int
3686
xmlValidateAttributeValue2(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3687
644k
      const xmlChar *name, xmlAttributeType type, const xmlChar *value) {
3688
644k
    int ret = 1;
3689
644k
    switch (type) {
3690
363k
  case XML_ATTRIBUTE_IDREFS:
3691
399k
  case XML_ATTRIBUTE_IDREF:
3692
468k
  case XML_ATTRIBUTE_ID:
3693
473k
  case XML_ATTRIBUTE_NMTOKENS:
3694
489k
  case XML_ATTRIBUTE_ENUMERATION:
3695
492k
  case XML_ATTRIBUTE_NMTOKEN:
3696
624k
        case XML_ATTRIBUTE_CDATA:
3697
624k
      break;
3698
12.1k
  case XML_ATTRIBUTE_ENTITY: {
3699
12.1k
      xmlEntityPtr ent;
3700
3701
12.1k
      ent = xmlGetDocEntity(doc, value);
3702
      /* yeah it's a bit messy... */
3703
12.1k
      if ((ent == NULL) && (doc->standalone == 1)) {
3704
0
    doc->standalone = 0;
3705
0
    ent = xmlGetDocEntity(doc, value);
3706
0
      }
3707
12.1k
      if (ent == NULL) {
3708
10.6k
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3709
10.6k
        XML_DTD_UNKNOWN_ENTITY,
3710
10.6k
   "ENTITY attribute %s reference an unknown entity \"%s\"\n",
3711
10.6k
           name, value, NULL);
3712
10.6k
    ret = 0;
3713
10.6k
      } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3714
1.56k
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3715
1.56k
        XML_DTD_ENTITY_TYPE,
3716
1.56k
   "ENTITY attribute %s reference an entity \"%s\" of wrong type\n",
3717
1.56k
           name, value, NULL);
3718
1.56k
    ret = 0;
3719
1.56k
      }
3720
12.1k
      break;
3721
492k
        }
3722
3.91k
  case XML_ATTRIBUTE_ENTITIES: {
3723
3.91k
      xmlChar *dup, *nam = NULL, *cur, save;
3724
3.91k
      xmlEntityPtr ent;
3725
3726
3.91k
      dup = xmlStrdup(value);
3727
3.91k
      if (dup == NULL) {
3728
13
                xmlVErrMemory(ctxt);
3729
13
    return(0);
3730
13
            }
3731
3.90k
      cur = dup;
3732
13.7k
      while (*cur != 0) {
3733
11.4k
    nam = cur;
3734
286k
    while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++;
3735
11.4k
    save = *cur;
3736
11.4k
    *cur = 0;
3737
11.4k
    ent = xmlGetDocEntity(doc, nam);
3738
11.4k
    if (ent == NULL) {
3739
10.8k
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3740
10.8k
            XML_DTD_UNKNOWN_ENTITY,
3741
10.8k
       "ENTITIES attribute %s reference an unknown entity \"%s\"\n",
3742
10.8k
         name, nam, NULL);
3743
10.8k
        ret = 0;
3744
10.8k
    } else if (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
3745
109
        xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3746
109
            XML_DTD_ENTITY_TYPE,
3747
109
       "ENTITIES attribute %s reference an entity \"%s\" of wrong type\n",
3748
109
         name, nam, NULL);
3749
109
        ret = 0;
3750
109
    }
3751
11.4k
    if (save == 0)
3752
1.65k
        break;
3753
9.83k
    *cur = save;
3754
16.4k
    while (IS_BLANK_CH(*cur)) cur++;
3755
9.83k
      }
3756
3.90k
      xmlFree(dup);
3757
3.90k
      break;
3758
3.91k
  }
3759
3.17k
  case XML_ATTRIBUTE_NOTATION: {
3760
3.17k
      xmlNotationPtr nota;
3761
3762
3.17k
      nota = xmlGetDtdNotationDesc(doc->intSubset, value);
3763
3.17k
      if ((nota == NULL) && (doc->extSubset != NULL))
3764
1.53k
    nota = xmlGetDtdNotationDesc(doc->extSubset, value);
3765
3766
3.17k
      if (nota == NULL) {
3767
2.42k
    xmlErrValidNode(ctxt, (xmlNodePtr) doc,
3768
2.42k
                    XML_DTD_UNKNOWN_NOTATION,
3769
2.42k
       "NOTATION attribute %s reference an unknown notation \"%s\"\n",
3770
2.42k
           name, value, NULL);
3771
2.42k
    ret = 0;
3772
2.42k
      }
3773
3.17k
      break;
3774
3.91k
        }
3775
644k
    }
3776
644k
    return(ret);
3777
644k
}
3778
3779
/**
3780
 * xmlValidCtxtNormalizeAttributeValue:
3781
 * @ctxt: the validation context
3782
 * @doc:  the document
3783
 * @elem:  the parent
3784
 * @name:  the attribute name
3785
 * @value:  the attribute value
3786
 * @ctxt:  the validation context or NULL
3787
 *
3788
 * Does the validation related extra step of the normalization of attribute
3789
 * values:
3790
 *
3791
 * If the declared value is not CDATA, then the XML processor must further
3792
 * process the normalized attribute value by discarding any leading and
3793
 * trailing space (#x20) characters, and by replacing sequences of space
3794
 * (#x20) characters by single space (#x20) character.
3795
 *
3796
 * Also  check VC: Standalone Document Declaration in P32, and update
3797
 *  ctxt->valid accordingly
3798
 *
3799
 * returns a new normalized string if normalization is needed, NULL otherwise
3800
 *      the caller must free the returned value.
3801
 */
3802
3803
xmlChar *
3804
xmlValidCtxtNormalizeAttributeValue(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3805
768k
       xmlNodePtr elem, const xmlChar *name, const xmlChar *value) {
3806
768k
    xmlChar *ret;
3807
768k
    xmlAttributePtr attrDecl = NULL;
3808
768k
    const xmlChar *localName;
3809
768k
    xmlChar *prefix = NULL;
3810
768k
    int extsubset = 0;
3811
3812
768k
    if (doc == NULL) return(NULL);
3813
768k
    if (elem == NULL) return(NULL);
3814
768k
    if (name == NULL) return(NULL);
3815
768k
    if (value == NULL) return(NULL);
3816
3817
648k
    localName = xmlSplitQName4(name, &prefix);
3818
648k
    if (localName == NULL)
3819
110
        goto mem_error;
3820
3821
648k
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3822
12.8k
  xmlChar buf[50];
3823
12.8k
  xmlChar *elemname;
3824
3825
12.8k
  elemname = xmlBuildQName(elem->name, elem->ns->prefix, buf, 50);
3826
12.8k
  if (elemname == NULL)
3827
11
      goto mem_error;
3828
12.8k
        if (doc->intSubset != NULL)
3829
7.91k
            attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName,
3830
7.91k
                                      prefix, elemname);
3831
12.8k
  if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3832
1.42k
      attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName,
3833
1.42k
                                      prefix, elemname);
3834
1.42k
      if (attrDecl != NULL)
3835
0
    extsubset = 1;
3836
1.42k
  }
3837
12.8k
  if ((elemname != buf) && (elemname != elem->name))
3838
2.04k
      xmlFree(elemname);
3839
12.8k
    }
3840
648k
    if ((attrDecl == NULL) && (doc->intSubset != NULL))
3841
432k
  attrDecl = xmlHashLookup3(doc->intSubset->attributes, localName,
3842
432k
                                  prefix, elem->name);
3843
648k
    if ((attrDecl == NULL) && (doc->extSubset != NULL)) {
3844
17.3k
  attrDecl = xmlHashLookup3(doc->extSubset->attributes, localName,
3845
17.3k
                                  prefix, elem->name);
3846
17.3k
  if (attrDecl != NULL)
3847
5.26k
      extsubset = 1;
3848
17.3k
    }
3849
3850
648k
    if (attrDecl == NULL)
3851
415k
  goto done;
3852
233k
    if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3853
174k
  goto done;
3854
3855
59.1k
    ret = xmlStrdup(value);
3856
59.1k
    if (ret == NULL)
3857
71
  goto mem_error;
3858
59.1k
    xmlValidNormalizeString(ret);
3859
59.1k
    if ((doc->standalone) && (extsubset == 1) && (!xmlStrEqual(value, ret))) {
3860
280
  xmlErrValidNode(ctxt, elem, XML_DTD_NOT_STANDALONE,
3861
280
"standalone: %s on %s value had to be normalized based on external subset declaration\n",
3862
280
         name, elem->name, NULL);
3863
280
  ctxt->valid = 0;
3864
280
    }
3865
3866
59.1k
    xmlFree(prefix);
3867
59.1k
    return(ret);
3868
3869
192
mem_error:
3870
192
    xmlVErrMemory(ctxt);
3871
3872
589k
done:
3873
589k
    xmlFree(prefix);
3874
589k
    return(NULL);
3875
192
}
3876
3877
/**
3878
 * xmlValidNormalizeAttributeValue:
3879
 * @doc:  the document
3880
 * @elem:  the parent
3881
 * @name:  the attribute name
3882
 * @value:  the attribute value
3883
 *
3884
 * Does the validation related extra step of the normalization of attribute
3885
 * values:
3886
 *
3887
 * If the declared value is not CDATA, then the XML processor must further
3888
 * process the normalized attribute value by discarding any leading and
3889
 * trailing space (#x20) characters, and by replacing sequences of space
3890
 * (#x20) characters by single space (#x20) character.
3891
 *
3892
 * Returns a new normalized string if normalization is needed, NULL otherwise
3893
 *      the caller must free the returned value.
3894
 */
3895
3896
xmlChar *
3897
xmlValidNormalizeAttributeValue(xmlDocPtr doc, xmlNodePtr elem,
3898
0
              const xmlChar *name, const xmlChar *value) {
3899
0
    xmlChar *ret;
3900
0
    xmlAttributePtr attrDecl = NULL;
3901
3902
0
    if (doc == NULL) return(NULL);
3903
0
    if (elem == NULL) return(NULL);
3904
0
    if (name == NULL) return(NULL);
3905
0
    if (value == NULL) return(NULL);
3906
3907
0
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
3908
0
  xmlChar fn[50];
3909
0
  xmlChar *fullname;
3910
3911
0
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
3912
0
  if (fullname == NULL)
3913
0
      return(NULL);
3914
0
  if ((fullname != fn) && (fullname != elem->name))
3915
0
      xmlFree(fullname);
3916
0
    }
3917
0
    attrDecl = xmlGetDtdAttrDesc(doc->intSubset, elem->name, name);
3918
0
    if ((attrDecl == NULL) && (doc->extSubset != NULL))
3919
0
  attrDecl = xmlGetDtdAttrDesc(doc->extSubset, elem->name, name);
3920
3921
0
    if (attrDecl == NULL)
3922
0
  return(NULL);
3923
0
    if (attrDecl->atype == XML_ATTRIBUTE_CDATA)
3924
0
  return(NULL);
3925
3926
0
    ret = xmlStrdup(value);
3927
0
    if (ret == NULL)
3928
0
  return(NULL);
3929
0
    xmlValidNormalizeString(ret);
3930
0
    return(ret);
3931
0
}
3932
3933
static void
3934
xmlValidateAttributeIdCallback(void *payload, void *data,
3935
93
                         const xmlChar *name ATTRIBUTE_UNUSED) {
3936
93
    xmlAttributePtr attr = (xmlAttributePtr) payload;
3937
93
    int *count = (int *) data;
3938
93
    if (attr->atype == XML_ATTRIBUTE_ID) (*count)++;
3939
93
}
3940
3941
/**
3942
 * xmlValidateAttributeDecl:
3943
 * @ctxt:  the validation context
3944
 * @doc:  a document instance
3945
 * @attr:  an attribute definition
3946
 *
3947
 * Try to validate a single attribute definition
3948
 * basically it does the following checks as described by the
3949
 * XML-1.0 recommendation:
3950
 *  - [ VC: Attribute Default Legal ]
3951
 *  - [ VC: Enumeration ]
3952
 *  - [ VC: ID Attribute Default ]
3953
 *
3954
 * The ID/IDREF uniqueness and matching are done separately
3955
 *
3956
 * returns 1 if valid or 0 otherwise
3957
 */
3958
3959
int
3960
xmlValidateAttributeDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
3961
75.3k
                         xmlAttributePtr attr) {
3962
75.3k
    int ret = 1;
3963
75.3k
    int val;
3964
75.3k
    CHECK_DTD;
3965
75.3k
    if(attr == NULL) return(1);
3966
3967
    /* Attribute Default Legal */
3968
    /* Enumeration */
3969
75.3k
    if (attr->defaultValue != NULL) {
3970
19.9k
  val = xmlValidateAttributeValueInternal(doc, attr->atype,
3971
19.9k
                                          attr->defaultValue);
3972
19.9k
  if (val == 0) {
3973
0
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_DEFAULT,
3974
0
         "Syntax of default value for attribute %s of %s is not valid\n",
3975
0
             attr->name, attr->elem, NULL);
3976
0
  }
3977
19.9k
        ret &= val;
3978
19.9k
    }
3979
3980
    /* ID Attribute Default */
3981
75.3k
    if ((attr->atype == XML_ATTRIBUTE_ID)&&
3982
75.3k
        (attr->def != XML_ATTRIBUTE_IMPLIED) &&
3983
75.3k
  (attr->def != XML_ATTRIBUTE_REQUIRED)) {
3984
1.27k
  xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_FIXED,
3985
1.27k
          "ID attribute %s of %s is not valid must be #IMPLIED or #REQUIRED\n",
3986
1.27k
         attr->name, attr->elem, NULL);
3987
1.27k
  ret = 0;
3988
1.27k
    }
3989
3990
    /* One ID per Element Type */
3991
75.3k
    if (attr->atype == XML_ATTRIBUTE_ID) {
3992
15.9k
        int nbId;
3993
3994
  /* the trick is that we parse DtD as their own internal subset */
3995
15.9k
        xmlElementPtr elem = xmlCtxtGetDtdElementDesc(ctxt, doc->intSubset,
3996
15.9k
                                                attr->elem);
3997
15.9k
  if (elem != NULL) {
3998
8.42k
      nbId = xmlScanIDAttributeDecl(ctxt, elem, 0);
3999
8.42k
  } else {
4000
7.52k
      xmlAttributeTablePtr table;
4001
4002
      /*
4003
       * The attribute may be declared in the internal subset and the
4004
       * element in the external subset.
4005
       */
4006
7.52k
      nbId = 0;
4007
7.52k
      if (doc->intSubset != NULL) {
4008
7.52k
    table = (xmlAttributeTablePtr) doc->intSubset->attributes;
4009
7.52k
    xmlHashScan3(table, NULL, NULL, attr->elem,
4010
7.52k
           xmlValidateAttributeIdCallback, &nbId);
4011
7.52k
      }
4012
7.52k
  }
4013
15.9k
  if (nbId > 1) {
4014
4015
461
      xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4016
461
       "Element %s has %d ID attribute defined in the internal subset : %s\n",
4017
461
       attr->elem, nbId, attr->name);
4018
15.4k
  } else if (doc->extSubset != NULL) {
4019
7.77k
      int extId = 0;
4020
7.77k
      elem = xmlCtxtGetDtdElementDesc(ctxt, doc->extSubset, attr->elem);
4021
7.77k
      if (elem != NULL) {
4022
7.77k
    extId = xmlScanIDAttributeDecl(ctxt, elem, 0);
4023
7.77k
      }
4024
7.77k
      if (extId > 1) {
4025
58
    xmlErrValidNodeNr(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4026
58
       "Element %s has %d ID attribute defined in the external subset : %s\n",
4027
58
           attr->elem, extId, attr->name);
4028
7.71k
      } else if (extId + nbId > 1) {
4029
6
    xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ID_SUBSET,
4030
6
"Element %s has ID attributes defined in the internal and external subset : %s\n",
4031
6
           attr->elem, attr->name, NULL);
4032
6
      }
4033
7.77k
  }
4034
15.9k
    }
4035
4036
    /* Validity Constraint: Enumeration */
4037
75.3k
    if ((attr->defaultValue != NULL) && (attr->tree != NULL)) {
4038
12.9k
        xmlEnumerationPtr tree = attr->tree;
4039
14.9k
  while (tree != NULL) {
4040
13.6k
      if (xmlStrEqual(tree->name, attr->defaultValue)) break;
4041
2.07k
      tree = tree->next;
4042
2.07k
  }
4043
12.9k
  if (tree == NULL) {
4044
1.37k
      xmlErrValidNode(ctxt, (xmlNodePtr) attr, XML_DTD_ATTRIBUTE_VALUE,
4045
1.37k
"Default value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4046
1.37k
       attr->defaultValue, attr->name, attr->elem);
4047
1.37k
      ret = 0;
4048
1.37k
  }
4049
12.9k
    }
4050
4051
75.3k
    return(ret);
4052
75.3k
}
4053
4054
/**
4055
 * xmlValidateElementDecl:
4056
 * @ctxt:  the validation context
4057
 * @doc:  a document instance
4058
 * @elem:  an element definition
4059
 *
4060
 * Try to validate a single element definition
4061
 * basically it does the following checks as described by the
4062
 * XML-1.0 recommendation:
4063
 *  - [ VC: One ID per Element Type ]
4064
 *  - [ VC: No Duplicate Types ]
4065
 *  - [ VC: Unique Element Type Declaration ]
4066
 *
4067
 * returns 1 if valid or 0 otherwise
4068
 */
4069
4070
int
4071
xmlValidateElementDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4072
39.3k
                       xmlElementPtr elem) {
4073
39.3k
    int ret = 1;
4074
39.3k
    xmlElementPtr tst;
4075
4076
39.3k
    CHECK_DTD;
4077
4078
39.3k
    if (elem == NULL) return(1);
4079
4080
#if 0
4081
#ifdef LIBXML_REGEXP_ENABLED
4082
    /* Build the regexp associated to the content model */
4083
    ret = xmlValidBuildContentModel(ctxt, elem);
4084
#endif
4085
#endif
4086
4087
    /* No Duplicate Types */
4088
33.6k
    if (elem->etype == XML_ELEMENT_TYPE_MIXED) {
4089
11.8k
  xmlElementContentPtr cur, next;
4090
11.8k
        const xmlChar *name;
4091
4092
11.8k
  cur = elem->content;
4093
57.0k
  while (cur != NULL) {
4094
57.0k
      if (cur->type != XML_ELEMENT_CONTENT_OR) break;
4095
45.2k
      if (cur->c1 == NULL) break;
4096
45.2k
      if (cur->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4097
40.5k
    name = cur->c1->name;
4098
40.5k
    next = cur->c2;
4099
601k
    while (next != NULL) {
4100
601k
        if (next->type == XML_ELEMENT_CONTENT_ELEMENT) {
4101
40.5k
            if ((xmlStrEqual(next->name, name)) &&
4102
40.5k
          (xmlStrEqual(next->prefix, cur->c1->prefix))) {
4103
1.04k
          if (cur->c1->prefix == NULL) {
4104
351
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4105
351
       "Definition of %s has duplicate references of %s\n",
4106
351
               elem->name, name, NULL);
4107
696
          } else {
4108
696
        xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4109
696
       "Definition of %s has duplicate references of %s:%s\n",
4110
696
               elem->name, cur->c1->prefix, name);
4111
696
          }
4112
1.04k
          ret = 0;
4113
1.04k
      }
4114
40.5k
      break;
4115
40.5k
        }
4116
560k
        if (next->c1 == NULL) break;
4117
560k
        if (next->c1->type != XML_ELEMENT_CONTENT_ELEMENT) break;
4118
560k
        if ((xmlStrEqual(next->c1->name, name)) &&
4119
560k
            (xmlStrEqual(next->c1->prefix, cur->c1->prefix))) {
4120
73.7k
      if (cur->c1->prefix == NULL) {
4121
2.04k
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4122
2.04k
         "Definition of %s has duplicate references to %s\n",
4123
2.04k
           elem->name, name, NULL);
4124
71.7k
      } else {
4125
71.7k
          xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_CONTENT_ERROR,
4126
71.7k
         "Definition of %s has duplicate references to %s:%s\n",
4127
71.7k
           elem->name, cur->c1->prefix, name);
4128
71.7k
      }
4129
73.7k
      ret = 0;
4130
73.7k
        }
4131
560k
        next = next->c2;
4132
560k
    }
4133
40.5k
      }
4134
45.2k
      cur = cur->c2;
4135
45.2k
  }
4136
11.8k
    }
4137
4138
    /* VC: Unique Element Type Declaration */
4139
33.6k
    tst = xmlCtxtGetDtdElementDesc(ctxt, doc->intSubset, elem->name);
4140
33.6k
    if ((tst != NULL ) && (tst != elem) &&
4141
33.6k
  ((tst->prefix == elem->prefix) ||
4142
1.27k
   (xmlStrEqual(tst->prefix, elem->prefix))) &&
4143
33.6k
  (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4144
1.25k
  xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4145
1.25k
                  "Redefinition of element %s\n",
4146
1.25k
           elem->name, NULL, NULL);
4147
1.25k
  ret = 0;
4148
1.25k
    }
4149
33.6k
    tst = xmlCtxtGetDtdElementDesc(ctxt, doc->extSubset, elem->name);
4150
33.6k
    if ((tst != NULL ) && (tst != elem) &&
4151
33.6k
  ((tst->prefix == elem->prefix) ||
4152
15
   (xmlStrEqual(tst->prefix, elem->prefix))) &&
4153
33.6k
  (tst->etype != XML_ELEMENT_TYPE_UNDEFINED)) {
4154
0
  xmlErrValidNode(ctxt, (xmlNodePtr) elem, XML_DTD_ELEM_REDEFINED,
4155
0
                  "Redefinition of element %s\n",
4156
0
           elem->name, NULL, NULL);
4157
0
  ret = 0;
4158
0
    }
4159
    /* One ID per Element Type
4160
     * already done when registering the attribute
4161
    if (xmlScanIDAttributeDecl(ctxt, elem) > 1) {
4162
  ret = 0;
4163
    } */
4164
33.6k
    return(ret);
4165
39.3k
}
4166
4167
/**
4168
 * xmlValidateOneAttribute:
4169
 * @ctxt:  the validation context
4170
 * @doc:  a document instance
4171
 * @elem:  an element instance
4172
 * @attr:  an attribute instance
4173
 * @value:  the attribute value (without entities processing)
4174
 *
4175
 * Try to validate a single attribute for an element
4176
 * basically it does the following checks as described by the
4177
 * XML-1.0 recommendation:
4178
 *  - [ VC: Attribute Value Type ]
4179
 *  - [ VC: Fixed Attribute Default ]
4180
 *  - [ VC: Entity Name ]
4181
 *  - [ VC: Name Token ]
4182
 *  - [ VC: ID ]
4183
 *  - [ VC: IDREF ]
4184
 *  - [ VC: Entity Name ]
4185
 *  - [ VC: Notation Attributes ]
4186
 *
4187
 * The ID/IDREF uniqueness and matching are done separately
4188
 *
4189
 * returns 1 if valid or 0 otherwise
4190
 */
4191
4192
int
4193
xmlValidateOneAttribute(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4194
                        xmlNodePtr elem, xmlAttrPtr attr, const xmlChar *value)
4195
985k
{
4196
985k
    xmlAttributePtr attrDecl =  NULL;
4197
985k
    const xmlChar *aprefix;
4198
985k
    int val;
4199
985k
    int ret = 1;
4200
4201
985k
    CHECK_DTD;
4202
985k
    if ((elem == NULL) || (elem->name == NULL)) return(0);
4203
985k
    if ((attr == NULL) || (attr->name == NULL)) return(0);
4204
4205
985k
    aprefix = (attr->ns != NULL) ? attr->ns->prefix : NULL;
4206
4207
985k
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL)) {
4208
30.1k
  xmlChar fn[50];
4209
30.1k
  xmlChar *fullname;
4210
4211
30.1k
  fullname = xmlBuildQName(elem->name, elem->ns->prefix, fn, 50);
4212
30.1k
  if (fullname == NULL) {
4213
4
            xmlVErrMemory(ctxt);
4214
4
      return(0);
4215
4
        }
4216
30.1k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4217
30.1k
                                      attr->name, aprefix);
4218
30.1k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
4219
1.13k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4220
1.13k
                                          attr->name, aprefix);
4221
30.1k
  if ((fullname != fn) && (fullname != elem->name))
4222
5.86k
      xmlFree(fullname);
4223
30.1k
    }
4224
985k
    if (attrDecl == NULL) {
4225
984k
        attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4226
984k
                                      attr->name, aprefix);
4227
984k
        if ((attrDecl == NULL) && (doc->extSubset != NULL))
4228
14.1k
            attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4229
14.1k
                                          attr->name, aprefix);
4230
984k
    }
4231
4232
4233
    /* Validity Constraint: Attribute Value Type */
4234
985k
    if (attrDecl == NULL) {
4235
390k
  xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4236
390k
         "No declaration for attribute %s of element %s\n",
4237
390k
         attr->name, elem->name, NULL);
4238
390k
  return(0);
4239
390k
    }
4240
595k
    attr->atype = attrDecl->atype;
4241
4242
595k
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4243
595k
    if (val == 0) {
4244
391k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4245
391k
     "Syntax of value for attribute %s of %s is not valid\n",
4246
391k
         attr->name, elem->name, NULL);
4247
391k
        ret = 0;
4248
391k
    }
4249
4250
    /* Validity constraint: Fixed Attribute Default */
4251
595k
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4252
38.8k
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4253
33.9k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4254
33.9k
     "Value for attribute %s of %s is different from default \"%s\"\n",
4255
33.9k
       attr->name, elem->name, attrDecl->defaultValue);
4256
33.9k
      ret = 0;
4257
33.9k
  }
4258
38.8k
    }
4259
4260
    /* Validity Constraint: ID uniqueness */
4261
595k
    if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4262
67.9k
        if (xmlAddID(ctxt, doc, value, attr) == NULL)
4263
64.8k
      ret = 0;
4264
67.9k
    }
4265
4266
595k
    if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4267
595k
  (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4268
390k
        if (xmlAddRef(ctxt, doc, value, attr) == NULL)
4269
121
      ret = 0;
4270
390k
    }
4271
4272
    /* Validity Constraint: Notation Attributes */
4273
595k
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4274
2.11k
        xmlEnumerationPtr tree = attrDecl->tree;
4275
2.11k
        xmlNotationPtr nota;
4276
4277
        /* First check that the given NOTATION was declared */
4278
2.11k
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4279
2.11k
  if (nota == NULL)
4280
1.45k
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4281
4282
2.11k
  if (nota == NULL) {
4283
1.45k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4284
1.45k
       "Value \"%s\" for attribute %s of %s is not a declared Notation\n",
4285
1.45k
       value, attr->name, elem->name);
4286
1.45k
      ret = 0;
4287
1.45k
        }
4288
4289
  /* Second, verify that it's among the list */
4290
3.74k
  while (tree != NULL) {
4291
2.96k
      if (xmlStrEqual(tree->name, value)) break;
4292
1.63k
      tree = tree->next;
4293
1.63k
  }
4294
2.11k
  if (tree == NULL) {
4295
777
      xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4296
777
"Value \"%s\" for attribute %s of %s is not among the enumerated notations\n",
4297
777
       value, attr->name, elem->name);
4298
777
      ret = 0;
4299
777
  }
4300
2.11k
    }
4301
4302
    /* Validity Constraint: Enumeration */
4303
595k
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4304
12.2k
        xmlEnumerationPtr tree = attrDecl->tree;
4305
20.9k
  while (tree != NULL) {
4306
13.8k
      if (xmlStrEqual(tree->name, value)) break;
4307
8.66k
      tree = tree->next;
4308
8.66k
  }
4309
12.2k
  if (tree == NULL) {
4310
7.03k
      xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4311
7.03k
       "Value \"%s\" for attribute %s of %s is not among the enumerated set\n",
4312
7.03k
       value, attr->name, elem->name);
4313
7.03k
      ret = 0;
4314
7.03k
  }
4315
12.2k
    }
4316
4317
    /* Fixed Attribute Default */
4318
595k
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4319
595k
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4320
33.9k
  xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4321
33.9k
     "Value for attribute %s of %s must be \"%s\"\n",
4322
33.9k
         attr->name, elem->name, attrDecl->defaultValue);
4323
33.9k
        ret = 0;
4324
33.9k
    }
4325
4326
    /* Extra check for the attribute value */
4327
595k
    ret &= xmlValidateAttributeValue2(ctxt, doc, attr->name,
4328
595k
              attrDecl->atype, value);
4329
4330
595k
    return(ret);
4331
985k
}
4332
4333
/**
4334
 * xmlValidateOneNamespace:
4335
 * @ctxt:  the validation context
4336
 * @doc:  a document instance
4337
 * @elem:  an element instance
4338
 * @prefix:  the namespace prefix
4339
 * @ns:  an namespace declaration instance
4340
 * @value:  the attribute value (without entities processing)
4341
 *
4342
 * Try to validate a single namespace declaration for an element
4343
 * basically it does the following checks as described by the
4344
 * XML-1.0 recommendation:
4345
 *  - [ VC: Attribute Value Type ]
4346
 *  - [ VC: Fixed Attribute Default ]
4347
 *  - [ VC: Entity Name ]
4348
 *  - [ VC: Name Token ]
4349
 *  - [ VC: ID ]
4350
 *  - [ VC: IDREF ]
4351
 *  - [ VC: Entity Name ]
4352
 *  - [ VC: Notation Attributes ]
4353
 *
4354
 * The ID/IDREF uniqueness and matching are done separately
4355
 *
4356
 * returns 1 if valid or 0 otherwise
4357
 */
4358
4359
int
4360
xmlValidateOneNamespace(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
4361
96.7k
xmlNodePtr elem, const xmlChar *prefix, xmlNsPtr ns, const xmlChar *value) {
4362
    /* xmlElementPtr elemDecl; */
4363
96.7k
    xmlAttributePtr attrDecl =  NULL;
4364
96.7k
    int val;
4365
96.7k
    int ret = 1;
4366
4367
96.7k
    CHECK_DTD;
4368
96.7k
    if ((elem == NULL) || (elem->name == NULL)) return(0);
4369
96.7k
    if ((ns == NULL) || (ns->href == NULL)) return(0);
4370
4371
89.1k
    if (prefix != NULL) {
4372
30.0k
  xmlChar fn[50];
4373
30.0k
  xmlChar *fullname;
4374
4375
30.0k
  fullname = xmlBuildQName(elem->name, prefix, fn, 50);
4376
30.0k
  if (fullname == NULL) {
4377
4
      xmlVErrMemory(ctxt);
4378
4
      return(0);
4379
4
  }
4380
30.0k
  if (ns->prefix != NULL) {
4381
28.7k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4382
28.7k
                              ns->prefix, BAD_CAST "xmlns");
4383
28.7k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4384
1.41k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4385
1.41k
            ns->prefix, BAD_CAST "xmlns");
4386
28.7k
  } else {
4387
1.26k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, fullname,
4388
1.26k
                                          BAD_CAST "xmlns", NULL);
4389
1.26k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4390
647
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, fullname,
4391
647
                                              BAD_CAST "xmlns", NULL);
4392
1.26k
  }
4393
30.0k
  if ((fullname != fn) && (fullname != elem->name))
4394
6.76k
      xmlFree(fullname);
4395
30.0k
    }
4396
89.1k
    if (attrDecl == NULL) {
4397
88.4k
  if (ns->prefix != NULL) {
4398
60.9k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4399
60.9k
                              ns->prefix, BAD_CAST "xmlns");
4400
60.9k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4401
2.79k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4402
2.79k
                ns->prefix, BAD_CAST "xmlns");
4403
60.9k
  } else {
4404
27.5k
      attrDecl = xmlGetDtdQAttrDesc(doc->intSubset, elem->name,
4405
27.5k
                                          BAD_CAST "xmlns", NULL);
4406
27.5k
      if ((attrDecl == NULL) && (doc->extSubset != NULL))
4407
2.84k
    attrDecl = xmlGetDtdQAttrDesc(doc->extSubset, elem->name,
4408
2.84k
                                              BAD_CAST "xmlns", NULL);
4409
27.5k
  }
4410
88.4k
    }
4411
4412
4413
    /* Validity Constraint: Attribute Value Type */
4414
89.1k
    if (attrDecl == NULL) {
4415
42.6k
  if (ns->prefix != NULL) {
4416
35.9k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4417
35.9k
       "No declaration for attribute xmlns:%s of element %s\n",
4418
35.9k
       ns->prefix, elem->name, NULL);
4419
35.9k
  } else {
4420
6.76k
      xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_ATTRIBUTE,
4421
6.76k
       "No declaration for attribute xmlns of element %s\n",
4422
6.76k
       elem->name, NULL, NULL);
4423
6.76k
  }
4424
42.6k
  return(0);
4425
42.6k
    }
4426
4427
46.5k
    val = xmlValidateAttributeValueInternal(doc, attrDecl->atype, value);
4428
46.5k
    if (val == 0) {
4429
17.2k
  if (ns->prefix != NULL) {
4430
9.22k
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4431
9.22k
         "Syntax of value for attribute xmlns:%s of %s is not valid\n",
4432
9.22k
       ns->prefix, elem->name, NULL);
4433
9.22k
  } else {
4434
8.00k
      xmlErrValidNode(ctxt, elem, XML_DTD_INVALID_DEFAULT,
4435
8.00k
         "Syntax of value for attribute xmlns of %s is not valid\n",
4436
8.00k
       elem->name, NULL, NULL);
4437
8.00k
  }
4438
17.2k
        ret = 0;
4439
17.2k
    }
4440
4441
    /* Validity constraint: Fixed Attribute Default */
4442
46.5k
    if (attrDecl->def == XML_ATTRIBUTE_FIXED) {
4443
20.3k
  if (!xmlStrEqual(value, attrDecl->defaultValue)) {
4444
2.48k
      if (ns->prefix != NULL) {
4445
185
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4446
185
       "Value for attribute xmlns:%s of %s is different from default \"%s\"\n",
4447
185
           ns->prefix, elem->name, attrDecl->defaultValue);
4448
2.29k
      } else {
4449
2.29k
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_DEFAULT,
4450
2.29k
       "Value for attribute xmlns of %s is different from default \"%s\"\n",
4451
2.29k
           elem->name, attrDecl->defaultValue, NULL);
4452
2.29k
      }
4453
2.48k
      ret = 0;
4454
2.48k
  }
4455
20.3k
    }
4456
4457
    /*
4458
     * Casting ns to xmlAttrPtr is wrong. We'd need separate functions
4459
     * xmlAddID and xmlAddRef for namespace declarations, but it makes
4460
     * no practical sense to use ID types anyway.
4461
     */
4462
#if 0
4463
    /* Validity Constraint: ID uniqueness */
4464
    if (attrDecl->atype == XML_ATTRIBUTE_ID) {
4465
        if (xmlAddID(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4466
      ret = 0;
4467
    }
4468
4469
    if ((attrDecl->atype == XML_ATTRIBUTE_IDREF) ||
4470
  (attrDecl->atype == XML_ATTRIBUTE_IDREFS)) {
4471
        if (xmlAddRef(ctxt, doc, value, (xmlAttrPtr) ns) == NULL)
4472
      ret = 0;
4473
    }
4474
#endif
4475
4476
    /* Validity Constraint: Notation Attributes */
4477
46.5k
    if (attrDecl->atype == XML_ATTRIBUTE_NOTATION) {
4478
0
        xmlEnumerationPtr tree = attrDecl->tree;
4479
0
        xmlNotationPtr nota;
4480
4481
        /* First check that the given NOTATION was declared */
4482
0
  nota = xmlGetDtdNotationDesc(doc->intSubset, value);
4483
0
  if (nota == NULL)
4484
0
      nota = xmlGetDtdNotationDesc(doc->extSubset, value);
4485
4486
0
  if (nota == NULL) {
4487
0
      if (ns->prefix != NULL) {
4488
0
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4489
0
       "Value \"%s\" for attribute xmlns:%s of %s is not a declared Notation\n",
4490
0
           value, ns->prefix, elem->name);
4491
0
      } else {
4492
0
    xmlErrValidNode(ctxt, elem, XML_DTD_UNKNOWN_NOTATION,
4493
0
       "Value \"%s\" for attribute xmlns of %s is not a declared Notation\n",
4494
0
           value, elem->name, NULL);
4495
0
      }
4496
0
      ret = 0;
4497
0
        }
4498
4499
  /* Second, verify that it's among the list */
4500
0
  while (tree != NULL) {
4501
0
      if (xmlStrEqual(tree->name, value)) break;
4502
0
      tree = tree->next;
4503
0
  }
4504
0
  if (tree == NULL) {
4505
0
      if (ns->prefix != NULL) {
4506
0
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4507
0
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated notations\n",
4508
0
           value, ns->prefix, elem->name);
4509
0
      } else {
4510
0
    xmlErrValidNode(ctxt, elem, XML_DTD_NOTATION_VALUE,
4511
0
"Value \"%s\" for attribute xmlns of %s is not among the enumerated notations\n",
4512
0
           value, elem->name, NULL);
4513
0
      }
4514
0
      ret = 0;
4515
0
  }
4516
0
    }
4517
4518
    /* Validity Constraint: Enumeration */
4519
46.5k
    if (attrDecl->atype == XML_ATTRIBUTE_ENUMERATION) {
4520
3.69k
        xmlEnumerationPtr tree = attrDecl->tree;
4521
7.43k
  while (tree != NULL) {
4522
4.61k
      if (xmlStrEqual(tree->name, value)) break;
4523
3.74k
      tree = tree->next;
4524
3.74k
  }
4525
3.69k
  if (tree == NULL) {
4526
2.82k
      if (ns->prefix != NULL) {
4527
279
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4528
279
"Value \"%s\" for attribute xmlns:%s of %s is not among the enumerated set\n",
4529
279
           value, ns->prefix, elem->name);
4530
2.55k
      } else {
4531
2.55k
    xmlErrValidNode(ctxt, elem, XML_DTD_ATTRIBUTE_VALUE,
4532
2.55k
"Value \"%s\" for attribute xmlns of %s is not among the enumerated set\n",
4533
2.55k
           value, elem->name, NULL);
4534
2.55k
      }
4535
2.82k
      ret = 0;
4536
2.82k
  }
4537
3.69k
    }
4538
4539
    /* Fixed Attribute Default */
4540
46.5k
    if ((attrDecl->def == XML_ATTRIBUTE_FIXED) &&
4541
46.5k
        (!xmlStrEqual(attrDecl->defaultValue, value))) {
4542
2.48k
  if (ns->prefix != NULL) {
4543
185
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4544
185
       "Value for attribute xmlns:%s of %s must be \"%s\"\n",
4545
185
       ns->prefix, elem->name, attrDecl->defaultValue);
4546
2.29k
  } else {
4547
2.29k
      xmlErrValidNode(ctxt, elem, XML_DTD_ELEM_NAMESPACE,
4548
2.29k
       "Value for attribute xmlns of %s must be \"%s\"\n",
4549
2.29k
       elem->name, attrDecl->defaultValue, NULL);
4550
2.29k
  }
4551
2.48k
        ret = 0;
4552
2.48k
    }
4553
4554
    /* Extra check for the attribute value */
4555
46.5k
    if (ns->prefix != NULL) {
4556
25.7k
  ret &= xmlValidateAttributeValue2(ctxt, doc, ns->prefix,
4557
25.7k
            attrDecl->atype, value);
4558
25.7k
    } else {
4559
20.7k
  ret &= xmlValidateAttributeValue2(ctxt, doc, BAD_CAST "xmlns",
4560
20.7k
            attrDecl->atype, value);
4561
20.7k
    }
4562
4563
46.5k
    return(ret);
4564
89.1k
}
4565
4566
#ifndef  LIBXML_REGEXP_ENABLED
4567
/**
4568
 * xmlValidateSkipIgnorable:
4569
 * @ctxt:  the validation context
4570
 * @child:  the child list
4571
 *
4572
 * Skip ignorable elements w.r.t. the validation process
4573
 *
4574
 * returns the first element to consider for validation of the content model
4575
 */
4576
4577
static xmlNodePtr
4578
xmlValidateSkipIgnorable(xmlNodePtr child) {
4579
    while (child != NULL) {
4580
  switch (child->type) {
4581
      /* These things are ignored (skipped) during validation.  */
4582
      case XML_PI_NODE:
4583
      case XML_COMMENT_NODE:
4584
      case XML_XINCLUDE_START:
4585
      case XML_XINCLUDE_END:
4586
    child = child->next;
4587
    break;
4588
      case XML_TEXT_NODE:
4589
    if (xmlIsBlankNode(child))
4590
        child = child->next;
4591
    else
4592
        return(child);
4593
    break;
4594
      /* keep current node */
4595
      default:
4596
    return(child);
4597
  }
4598
    }
4599
    return(child);
4600
}
4601
4602
/**
4603
 * xmlValidateElementType:
4604
 * @ctxt:  the validation context
4605
 *
4606
 * Try to validate the content model of an element internal function
4607
 *
4608
 * returns 1 if valid or 0 ,-1 in case of error, -2 if an entity
4609
 *           reference is found and -3 if the validation succeeded but
4610
 *           the content model is not determinist.
4611
 */
4612
4613
static int
4614
xmlValidateElementType(xmlValidCtxtPtr ctxt) {
4615
    int ret = -1;
4616
    int determinist = 1;
4617
4618
    NODE = xmlValidateSkipIgnorable(NODE);
4619
    if ((NODE == NULL) && (CONT == NULL))
4620
  return(1);
4621
    if ((NODE == NULL) &&
4622
  ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4623
   (CONT->ocur == XML_ELEMENT_CONTENT_OPT))) {
4624
  return(1);
4625
    }
4626
    if (CONT == NULL) return(-1);
4627
    if ((NODE != NULL) && (NODE->type == XML_ENTITY_REF_NODE))
4628
  return(-2);
4629
4630
    /*
4631
     * We arrive here when more states need to be examined
4632
     */
4633
cont:
4634
4635
    /*
4636
     * We just recovered from a rollback generated by a possible
4637
     * epsilon transition, go directly to the analysis phase
4638
     */
4639
    if (STATE == ROLLBACK_PARENT) {
4640
  ret = 1;
4641
  goto analyze;
4642
    }
4643
4644
    /*
4645
     * we may have to save a backup state here. This is the equivalent
4646
     * of handling epsilon transition in NFAs.
4647
     */
4648
    if ((CONT != NULL) &&
4649
  ((CONT->parent == NULL) ||
4650
   (CONT->parent == (xmlElementContentPtr) 1) ||
4651
   (CONT->parent->type != XML_ELEMENT_CONTENT_OR)) &&
4652
  ((CONT->ocur == XML_ELEMENT_CONTENT_MULT) ||
4653
   (CONT->ocur == XML_ELEMENT_CONTENT_OPT) ||
4654
   ((CONT->ocur == XML_ELEMENT_CONTENT_PLUS) && (OCCURRENCE)))) {
4655
  if (vstateVPush(ctxt, CONT, NODE, DEPTH, OCCURS, ROLLBACK_PARENT) < 0)
4656
      return(0);
4657
    }
4658
4659
4660
    /*
4661
     * Check first if the content matches
4662
     */
4663
    switch (CONT->type) {
4664
  case XML_ELEMENT_CONTENT_PCDATA:
4665
      if (NODE == NULL) {
4666
    ret = 0;
4667
    break;
4668
      }
4669
      if (NODE->type == XML_TEXT_NODE) {
4670
    /*
4671
     * go to next element in the content model
4672
     * skipping ignorable elems
4673
     */
4674
    do {
4675
        NODE = NODE->next;
4676
        NODE = xmlValidateSkipIgnorable(NODE);
4677
        if ((NODE != NULL) &&
4678
      (NODE->type == XML_ENTITY_REF_NODE))
4679
      return(-2);
4680
    } while ((NODE != NULL) &&
4681
       ((NODE->type != XML_ELEMENT_NODE) &&
4682
        (NODE->type != XML_TEXT_NODE) &&
4683
        (NODE->type != XML_CDATA_SECTION_NODE)));
4684
                ret = 1;
4685
    break;
4686
      } else {
4687
    ret = 0;
4688
    break;
4689
      }
4690
      break;
4691
  case XML_ELEMENT_CONTENT_ELEMENT:
4692
      if (NODE == NULL) {
4693
    ret = 0;
4694
    break;
4695
      }
4696
      ret = ((NODE->type == XML_ELEMENT_NODE) &&
4697
       (xmlStrEqual(NODE->name, CONT->name)));
4698
      if (ret == 1) {
4699
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4700
        ret = (CONT->prefix == NULL);
4701
    } else if (CONT->prefix == NULL) {
4702
        ret = 0;
4703
    } else {
4704
        ret = xmlStrEqual(NODE->ns->prefix, CONT->prefix);
4705
    }
4706
      }
4707
      if (ret == 1) {
4708
    /*
4709
     * go to next element in the content model
4710
     * skipping ignorable elems
4711
     */
4712
    do {
4713
        NODE = NODE->next;
4714
        NODE = xmlValidateSkipIgnorable(NODE);
4715
        if ((NODE != NULL) &&
4716
      (NODE->type == XML_ENTITY_REF_NODE))
4717
      return(-2);
4718
    } while ((NODE != NULL) &&
4719
       ((NODE->type != XML_ELEMENT_NODE) &&
4720
        (NODE->type != XML_TEXT_NODE) &&
4721
        (NODE->type != XML_CDATA_SECTION_NODE)));
4722
      } else {
4723
    ret = 0;
4724
    break;
4725
      }
4726
      break;
4727
  case XML_ELEMENT_CONTENT_OR:
4728
      /*
4729
       * Small optimization.
4730
       */
4731
      if (CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) {
4732
    if ((NODE == NULL) ||
4733
        (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4734
        DEPTH++;
4735
        CONT = CONT->c2;
4736
        goto cont;
4737
    }
4738
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4739
        ret = (CONT->c1->prefix == NULL);
4740
    } else if (CONT->c1->prefix == NULL) {
4741
        ret = 0;
4742
    } else {
4743
        ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4744
    }
4745
    if (ret == 0) {
4746
        DEPTH++;
4747
        CONT = CONT->c2;
4748
        goto cont;
4749
    }
4750
      }
4751
4752
      /*
4753
       * save the second branch 'or' branch
4754
       */
4755
      if (vstateVPush(ctxt, CONT->c2, NODE, DEPTH + 1,
4756
          OCCURS, ROLLBACK_OR) < 0)
4757
    return(-1);
4758
      DEPTH++;
4759
      CONT = CONT->c1;
4760
      goto cont;
4761
  case XML_ELEMENT_CONTENT_SEQ:
4762
      /*
4763
       * Small optimization.
4764
       */
4765
      if ((CONT->c1->type == XML_ELEMENT_CONTENT_ELEMENT) &&
4766
    ((CONT->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4767
     (CONT->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4768
    if ((NODE == NULL) ||
4769
        (!xmlStrEqual(NODE->name, CONT->c1->name))) {
4770
        DEPTH++;
4771
        CONT = CONT->c2;
4772
        goto cont;
4773
    }
4774
    if ((NODE->ns == NULL) || (NODE->ns->prefix == NULL)) {
4775
        ret = (CONT->c1->prefix == NULL);
4776
    } else if (CONT->c1->prefix == NULL) {
4777
        ret = 0;
4778
    } else {
4779
        ret = xmlStrEqual(NODE->ns->prefix, CONT->c1->prefix);
4780
    }
4781
    if (ret == 0) {
4782
        DEPTH++;
4783
        CONT = CONT->c2;
4784
        goto cont;
4785
    }
4786
      }
4787
      DEPTH++;
4788
      CONT = CONT->c1;
4789
      goto cont;
4790
    }
4791
4792
    /*
4793
     * At this point handle going up in the tree
4794
     */
4795
    if (ret == -1) {
4796
  return(ret);
4797
    }
4798
analyze:
4799
    while (CONT != NULL) {
4800
  /*
4801
   * First do the analysis depending on the occurrence model at
4802
   * this level.
4803
   */
4804
  if (ret == 0) {
4805
      switch (CONT->ocur) {
4806
    xmlNodePtr cur;
4807
4808
    case XML_ELEMENT_CONTENT_ONCE:
4809
        cur = ctxt->vstate->node;
4810
        if (vstateVPop(ctxt) < 0 ) {
4811
      return(0);
4812
        }
4813
        if (cur != ctxt->vstate->node)
4814
      determinist = -3;
4815
        goto cont;
4816
    case XML_ELEMENT_CONTENT_PLUS:
4817
        if (OCCURRENCE == 0) {
4818
      cur = ctxt->vstate->node;
4819
      if (vstateVPop(ctxt) < 0 ) {
4820
          return(0);
4821
      }
4822
      if (cur != ctxt->vstate->node)
4823
          determinist = -3;
4824
      goto cont;
4825
        }
4826
        ret = 1;
4827
        break;
4828
    case XML_ELEMENT_CONTENT_MULT:
4829
        ret = 1;
4830
        break;
4831
    case XML_ELEMENT_CONTENT_OPT:
4832
        ret = 1;
4833
        break;
4834
      }
4835
  } else {
4836
      switch (CONT->ocur) {
4837
    case XML_ELEMENT_CONTENT_OPT:
4838
        ret = 1;
4839
        break;
4840
    case XML_ELEMENT_CONTENT_ONCE:
4841
        ret = 1;
4842
        break;
4843
    case XML_ELEMENT_CONTENT_PLUS:
4844
        if (STATE == ROLLBACK_PARENT) {
4845
      ret = 1;
4846
      break;
4847
        }
4848
        if (NODE == NULL) {
4849
      ret = 1;
4850
      break;
4851
        }
4852
        SET_OCCURRENCE;
4853
        goto cont;
4854
    case XML_ELEMENT_CONTENT_MULT:
4855
        if (STATE == ROLLBACK_PARENT) {
4856
      ret = 1;
4857
      break;
4858
        }
4859
        if (NODE == NULL) {
4860
      ret = 1;
4861
      break;
4862
        }
4863
        /* SET_OCCURRENCE; */
4864
        goto cont;
4865
      }
4866
  }
4867
  STATE = 0;
4868
4869
  /*
4870
   * Then act accordingly at the parent level
4871
   */
4872
  RESET_OCCURRENCE;
4873
  if ((CONT->parent == NULL) ||
4874
            (CONT->parent == (xmlElementContentPtr) 1))
4875
      break;
4876
4877
  switch (CONT->parent->type) {
4878
      case XML_ELEMENT_CONTENT_PCDATA:
4879
    return(-1);
4880
      case XML_ELEMENT_CONTENT_ELEMENT:
4881
    return(-1);
4882
      case XML_ELEMENT_CONTENT_OR:
4883
    if (ret == 1) {
4884
        CONT = CONT->parent;
4885
        DEPTH--;
4886
    } else {
4887
        CONT = CONT->parent;
4888
        DEPTH--;
4889
    }
4890
    break;
4891
      case XML_ELEMENT_CONTENT_SEQ:
4892
    if (ret == 0) {
4893
        CONT = CONT->parent;
4894
        DEPTH--;
4895
    } else if (CONT == CONT->parent->c1) {
4896
        CONT = CONT->parent->c2;
4897
        goto cont;
4898
    } else {
4899
        CONT = CONT->parent;
4900
        DEPTH--;
4901
    }
4902
  }
4903
    }
4904
    if (NODE != NULL) {
4905
  xmlNodePtr cur;
4906
4907
  cur = ctxt->vstate->node;
4908
  if (vstateVPop(ctxt) < 0 ) {
4909
      return(0);
4910
  }
4911
  if (cur != ctxt->vstate->node)
4912
      determinist = -3;
4913
  goto cont;
4914
    }
4915
    if (ret == 0) {
4916
  xmlNodePtr cur;
4917
4918
  cur = ctxt->vstate->node;
4919
  if (vstateVPop(ctxt) < 0 ) {
4920
      return(0);
4921
  }
4922
  if (cur != ctxt->vstate->node)
4923
      determinist = -3;
4924
  goto cont;
4925
    }
4926
    return(determinist);
4927
}
4928
#endif
4929
4930
/**
4931
 * xmlSnprintfElements:
4932
 * @buf:  an output buffer
4933
 * @size:  the size of the buffer
4934
 * @content:  An element
4935
 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
4936
 *
4937
 * This will dump the list of elements to the buffer
4938
 * Intended just for the debug routine
4939
 */
4940
static void
4941
80.5k
xmlSnprintfElements(char *buf, int size, xmlNodePtr node, int glob) {
4942
80.5k
    xmlNodePtr cur;
4943
80.5k
    int len;
4944
4945
80.5k
    if (node == NULL) return;
4946
77.1k
    if (glob) strcat(buf, "(");
4947
77.1k
    cur = node;
4948
225k
    while (cur != NULL) {
4949
148k
  len = strlen(buf);
4950
148k
  if (size - len < 50) {
4951
11
      if ((size - len > 4) && (buf[len - 1] != '.'))
4952
11
    strcat(buf, " ...");
4953
11
      return;
4954
11
  }
4955
148k
        switch (cur->type) {
4956
109k
            case XML_ELEMENT_NODE:
4957
109k
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
4958
2.26k
        if (size - len < xmlStrlen(cur->ns->prefix) + 10) {
4959
0
      if ((size - len > 4) && (buf[len - 1] != '.'))
4960
0
          strcat(buf, " ...");
4961
0
      return;
4962
0
        }
4963
2.26k
        strcat(buf, (char *) cur->ns->prefix);
4964
2.26k
        strcat(buf, ":");
4965
2.26k
    }
4966
109k
                if (size - len < xmlStrlen(cur->name) + 10) {
4967
39
        if ((size - len > 4) && (buf[len - 1] != '.'))
4968
39
      strcat(buf, " ...");
4969
39
        return;
4970
39
    }
4971
109k
          strcat(buf, (char *) cur->name);
4972
109k
    if (cur->next != NULL)
4973
50.3k
        strcat(buf, " ");
4974
109k
    break;
4975
32.6k
            case XML_TEXT_NODE:
4976
32.6k
    if (xmlIsBlankNode(cur))
4977
6.99k
        break;
4978
                /* Falls through. */
4979
29.4k
            case XML_CDATA_SECTION_NODE:
4980
30.7k
            case XML_ENTITY_REF_NODE:
4981
30.7k
          strcat(buf, "CDATA");
4982
30.7k
    if (cur->next != NULL)
4983
15.7k
        strcat(buf, " ");
4984
30.7k
    break;
4985
0
            case XML_ATTRIBUTE_NODE:
4986
0
            case XML_DOCUMENT_NODE:
4987
0
      case XML_HTML_DOCUMENT_NODE:
4988
0
            case XML_DOCUMENT_TYPE_NODE:
4989
0
            case XML_DOCUMENT_FRAG_NODE:
4990
0
            case XML_NOTATION_NODE:
4991
0
      case XML_NAMESPACE_DECL:
4992
0
          strcat(buf, "???");
4993
0
    if (cur->next != NULL)
4994
0
        strcat(buf, " ");
4995
0
    break;
4996
0
            case XML_ENTITY_NODE:
4997
120
            case XML_PI_NODE:
4998
120
            case XML_DTD_NODE:
4999
1.31k
            case XML_COMMENT_NODE:
5000
1.31k
      case XML_ELEMENT_DECL:
5001
1.31k
      case XML_ATTRIBUTE_DECL:
5002
1.31k
      case XML_ENTITY_DECL:
5003
1.31k
      case XML_XINCLUDE_START:
5004
1.31k
      case XML_XINCLUDE_END:
5005
1.31k
    break;
5006
148k
  }
5007
148k
  cur = cur->next;
5008
148k
    }
5009
77.1k
    if (glob) strcat(buf, ")");
5010
77.1k
}
5011
5012
/**
5013
 * xmlValidateElementContent:
5014
 * @ctxt:  the validation context
5015
 * @child:  the child list
5016
 * @elemDecl:  pointer to the element declaration
5017
 * @warn:  emit the error message
5018
 * @parent: the parent element (for error reporting)
5019
 *
5020
 * Try to validate the content model of an element
5021
 *
5022
 * returns 1 if valid or 0 if not and -1 in case of error
5023
 */
5024
5025
static int
5026
xmlValidateElementContent(xmlValidCtxtPtr ctxt, xmlNodePtr child,
5027
107k
       xmlElementPtr elemDecl, int warn, xmlNodePtr parent) {
5028
107k
    int ret = 1;
5029
#ifndef  LIBXML_REGEXP_ENABLED
5030
    xmlNodePtr repl = NULL, last = NULL, tmp;
5031
#endif
5032
107k
    xmlNodePtr cur;
5033
107k
    xmlElementContentPtr cont;
5034
107k
    const xmlChar *name;
5035
5036
107k
    if ((elemDecl == NULL) || (parent == NULL) || (ctxt == NULL))
5037
0
  return(-1);
5038
107k
    cont = elemDecl->content;
5039
107k
    name = elemDecl->name;
5040
5041
107k
#ifdef LIBXML_REGEXP_ENABLED
5042
    /* Build the regexp associated to the content model */
5043
107k
    if (elemDecl->contModel == NULL)
5044
4.59k
  ret = xmlValidBuildContentModel(ctxt, elemDecl);
5045
107k
    if (elemDecl->contModel == NULL) {
5046
537
  return(-1);
5047
106k
    } else {
5048
106k
  xmlRegExecCtxtPtr exec;
5049
5050
106k
  if (!xmlRegexpIsDeterminist(elemDecl->contModel)) {
5051
5.69k
      return(-1);
5052
5.69k
  }
5053
100k
  ctxt->nodeMax = 0;
5054
100k
  ctxt->nodeNr = 0;
5055
100k
  ctxt->nodeTab = NULL;
5056
100k
  exec = xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
5057
100k
  if (exec == NULL) {
5058
45
            xmlVErrMemory(ctxt);
5059
45
            return(-1);
5060
45
        }
5061
100k
        cur = child;
5062
219k
        while (cur != NULL) {
5063
138k
            switch (cur->type) {
5064
794
                case XML_ENTITY_REF_NODE:
5065
                    /*
5066
                     * Push the current node to be able to roll back
5067
                     * and process within the entity
5068
                     */
5069
794
                    if ((cur->children != NULL) &&
5070
794
                        (cur->children->children != NULL)) {
5071
566
                        nodeVPush(ctxt, cur);
5072
566
                        cur = cur->children->children;
5073
566
                        continue;
5074
566
                    }
5075
228
                    break;
5076
34.9k
                case XML_TEXT_NODE:
5077
34.9k
                    if (xmlIsBlankNode(cur))
5078
16.0k
                        break;
5079
18.8k
                    ret = 0;
5080
18.8k
                    goto fail;
5081
1.03k
                case XML_CDATA_SECTION_NODE:
5082
                    /* TODO */
5083
1.03k
                    ret = 0;
5084
1.03k
                    goto fail;
5085
100k
                case XML_ELEMENT_NODE:
5086
100k
                    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
5087
1.24k
                        xmlChar fn[50];
5088
1.24k
                        xmlChar *fullname;
5089
5090
1.24k
                        fullname = xmlBuildQName(cur->name,
5091
1.24k
                                                 cur->ns->prefix, fn, 50);
5092
1.24k
                        if (fullname == NULL) {
5093
5
                            xmlVErrMemory(ctxt);
5094
5
                            ret = -1;
5095
5
                            goto fail;
5096
5
                        }
5097
1.24k
                        ret = xmlRegExecPushString(exec, fullname, NULL);
5098
1.24k
                        if ((fullname != fn) && (fullname != cur->name))
5099
132
                            xmlFree(fullname);
5100
99.3k
                    } else {
5101
99.3k
                        ret = xmlRegExecPushString(exec, cur->name, NULL);
5102
99.3k
                    }
5103
100k
                    break;
5104
100k
                default:
5105
763
                    break;
5106
138k
            }
5107
117k
            if (ret == XML_REGEXP_OUT_OF_MEMORY)
5108
183
                xmlVErrMemory(ctxt);
5109
            /*
5110
             * Switch to next element
5111
             */
5112
117k
            cur = cur->next;
5113
118k
            while (cur == NULL) {
5114
67.1k
                cur = nodeVPop(ctxt);
5115
67.1k
                if (cur == NULL)
5116
66.7k
                    break;
5117
465
                cur = cur->next;
5118
465
            }
5119
117k
        }
5120
80.9k
        ret = xmlRegExecPushString(exec, NULL, NULL);
5121
100k
fail:
5122
100k
        xmlRegFreeExecCtxt(exec);
5123
100k
    }
5124
#else  /* LIBXML_REGEXP_ENABLED */
5125
    /*
5126
     * Allocate the stack
5127
     */
5128
    ctxt->vstateMax = 8;
5129
    ctxt->vstateTab = (xmlValidState *) xmlMalloc(
5130
     ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
5131
    if (ctxt->vstateTab == NULL) {
5132
  xmlVErrMemory(ctxt);
5133
  return(-1);
5134
    }
5135
    /*
5136
     * The first entry in the stack is reserved to the current state
5137
     */
5138
    ctxt->nodeMax = 0;
5139
    ctxt->nodeNr = 0;
5140
    ctxt->nodeTab = NULL;
5141
    ctxt->vstate = &ctxt->vstateTab[0];
5142
    ctxt->vstateNr = 1;
5143
    CONT = cont;
5144
    NODE = child;
5145
    DEPTH = 0;
5146
    OCCURS = 0;
5147
    STATE = 0;
5148
    ret = xmlValidateElementType(ctxt);
5149
    if ((ret == -3) && (warn)) {
5150
  char expr[5000];
5151
  expr[0] = 0;
5152
  xmlSnprintfElementContent(expr, 5000, elemDecl->content, 1);
5153
  xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
5154
                XML_DTD_CONTENT_NOT_DETERMINIST,
5155
          "Content model of %s is not deterministic: %s\n",
5156
          name, BAD_CAST expr, NULL);
5157
    } else if (ret == -2) {
5158
  /*
5159
   * An entities reference appeared at this level.
5160
   * Build a minimal representation of this node content
5161
   * sufficient to run the validation process on it
5162
   */
5163
  cur = child;
5164
  while (cur != NULL) {
5165
      switch (cur->type) {
5166
    case XML_ENTITY_REF_NODE:
5167
        /*
5168
         * Push the current node to be able to roll back
5169
         * and process within the entity
5170
         */
5171
        if ((cur->children != NULL) &&
5172
      (cur->children->children != NULL)) {
5173
      nodeVPush(ctxt, cur);
5174
      cur = cur->children->children;
5175
      continue;
5176
        }
5177
        break;
5178
    case XML_TEXT_NODE:
5179
        if (xmlIsBlankNode(cur))
5180
      break;
5181
        /* no break on purpose */
5182
    case XML_CDATA_SECTION_NODE:
5183
        /* no break on purpose */
5184
    case XML_ELEMENT_NODE:
5185
        /*
5186
         * Allocate a new node and minimally fills in
5187
         * what's required
5188
         */
5189
        tmp = (xmlNodePtr) xmlMalloc(sizeof(xmlNode));
5190
        if (tmp == NULL) {
5191
      xmlVErrMemory(ctxt);
5192
      xmlFreeNodeList(repl);
5193
      ret = -1;
5194
      goto done;
5195
        }
5196
        tmp->type = cur->type;
5197
        tmp->name = cur->name;
5198
        tmp->ns = cur->ns;
5199
        tmp->next = NULL;
5200
        tmp->content = NULL;
5201
        if (repl == NULL)
5202
      repl = last = tmp;
5203
        else {
5204
      last->next = tmp;
5205
      last = tmp;
5206
        }
5207
        if (cur->type == XML_CDATA_SECTION_NODE) {
5208
      /*
5209
       * E59 spaces in CDATA does not match the
5210
       * nonterminal S
5211
       */
5212
      tmp->content = xmlStrdup(BAD_CAST "CDATA");
5213
        }
5214
        break;
5215
    default:
5216
        break;
5217
      }
5218
      /*
5219
       * Switch to next element
5220
       */
5221
      cur = cur->next;
5222
      while (cur == NULL) {
5223
    cur = nodeVPop(ctxt);
5224
    if (cur == NULL)
5225
        break;
5226
    cur = cur->next;
5227
      }
5228
  }
5229
5230
  /*
5231
   * Relaunch the validation
5232
   */
5233
  ctxt->vstate = &ctxt->vstateTab[0];
5234
  ctxt->vstateNr = 1;
5235
  CONT = cont;
5236
  NODE = repl;
5237
  DEPTH = 0;
5238
  OCCURS = 0;
5239
  STATE = 0;
5240
  ret = xmlValidateElementType(ctxt);
5241
    }
5242
#endif /* LIBXML_REGEXP_ENABLED */
5243
100k
    if ((warn) && ((ret != 1) && (ret != -3))) {
5244
80.5k
  if (ctxt != NULL) {
5245
80.5k
      char expr[5000];
5246
80.5k
      char list[5000];
5247
5248
80.5k
      expr[0] = 0;
5249
80.5k
      xmlSnprintfElementContent(&expr[0], 5000, cont, 1);
5250
80.5k
      list[0] = 0;
5251
#ifndef LIBXML_REGEXP_ENABLED
5252
      if (repl != NULL)
5253
    xmlSnprintfElements(&list[0], 5000, repl, 1);
5254
      else
5255
#endif /* LIBXML_REGEXP_ENABLED */
5256
80.5k
    xmlSnprintfElements(&list[0], 5000, child, 1);
5257
5258
80.5k
      if (name != NULL) {
5259
80.5k
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5260
80.5k
     "Element %s content does not follow the DTD, expecting %s, got %s\n",
5261
80.5k
           name, BAD_CAST expr, BAD_CAST list);
5262
80.5k
      } else {
5263
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5264
0
     "Element content does not follow the DTD, expecting %s, got %s\n",
5265
0
           BAD_CAST expr, BAD_CAST list, NULL);
5266
0
      }
5267
80.5k
  } else {
5268
0
      if (name != NULL) {
5269
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5270
0
           "Element %s content does not follow the DTD\n",
5271
0
           name, NULL, NULL);
5272
0
      } else {
5273
0
    xmlErrValidNode(ctxt, parent, XML_DTD_CONTENT_MODEL,
5274
0
           "Element content does not follow the DTD\n",
5275
0
                    NULL, NULL, NULL);
5276
0
      }
5277
0
  }
5278
80.5k
  ret = 0;
5279
80.5k
    }
5280
100k
    if (ret == -3)
5281
0
  ret = 1;
5282
5283
#ifndef  LIBXML_REGEXP_ENABLED
5284
done:
5285
    /*
5286
     * Deallocate the copy if done, and free up the validation stack
5287
     */
5288
    while (repl != NULL) {
5289
  tmp = repl->next;
5290
  xmlFree(repl);
5291
  repl = tmp;
5292
    }
5293
    ctxt->vstateMax = 0;
5294
    if (ctxt->vstateTab != NULL) {
5295
  xmlFree(ctxt->vstateTab);
5296
  ctxt->vstateTab = NULL;
5297
    }
5298
#endif
5299
100k
    ctxt->nodeMax = 0;
5300
100k
    ctxt->nodeNr = 0;
5301
100k
    if (ctxt->nodeTab != NULL) {
5302
258
  xmlFree(ctxt->nodeTab);
5303
258
  ctxt->nodeTab = NULL;
5304
258
    }
5305
100k
    return(ret);
5306
5307
107k
}
5308
5309
/**
5310
 * xmlValidateCdataElement:
5311
 * @ctxt:  the validation context
5312
 * @doc:  a document instance
5313
 * @elem:  an element instance
5314
 *
5315
 * Check that an element follows #CDATA
5316
 *
5317
 * returns 1 if valid or 0 otherwise
5318
 */
5319
static int
5320
xmlValidateOneCdataElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5321
18.0k
                           xmlNodePtr elem) {
5322
18.0k
    int ret = 1;
5323
18.0k
    xmlNodePtr cur, child;
5324
5325
18.0k
    if ((ctxt == NULL) || (doc == NULL) || (elem == NULL) ||
5326
18.0k
        (elem->type != XML_ELEMENT_NODE))
5327
0
  return(0);
5328
5329
18.0k
    child = elem->children;
5330
5331
18.0k
    cur = child;
5332
109k
    while (cur != NULL) {
5333
97.6k
  switch (cur->type) {
5334
61.2k
      case XML_ENTITY_REF_NODE:
5335
    /*
5336
     * Push the current node to be able to roll back
5337
     * and process within the entity
5338
     */
5339
61.2k
    if ((cur->children != NULL) &&
5340
61.2k
        (cur->children->children != NULL)) {
5341
35.7k
        nodeVPush(ctxt, cur);
5342
35.7k
        cur = cur->children->children;
5343
35.7k
        continue;
5344
35.7k
    }
5345
25.5k
    break;
5346
25.5k
      case XML_COMMENT_NODE:
5347
1.07k
      case XML_PI_NODE:
5348
28.9k
      case XML_TEXT_NODE:
5349
30.1k
      case XML_CDATA_SECTION_NODE:
5350
30.1k
    break;
5351
6.32k
      default:
5352
6.32k
    ret = 0;
5353
6.32k
    goto done;
5354
97.6k
  }
5355
  /*
5356
   * Switch to next element
5357
   */
5358
55.6k
  cur = cur->next;
5359
91.3k
  while (cur == NULL) {
5360
43.5k
      cur = nodeVPop(ctxt);
5361
43.5k
      if (cur == NULL)
5362
7.83k
    break;
5363
35.6k
      cur = cur->next;
5364
35.6k
  }
5365
55.6k
    }
5366
18.0k
done:
5367
18.0k
    ctxt->nodeMax = 0;
5368
18.0k
    ctxt->nodeNr = 0;
5369
18.0k
    if (ctxt->nodeTab != NULL) {
5370
1.41k
  xmlFree(ctxt->nodeTab);
5371
1.41k
  ctxt->nodeTab = NULL;
5372
1.41k
    }
5373
18.0k
    return(ret);
5374
18.0k
}
5375
5376
#ifdef LIBXML_REGEXP_ENABLED
5377
/**
5378
 * xmlValidateCheckMixed:
5379
 * @ctxt:  the validation context
5380
 * @cont:  the mixed content model
5381
 * @qname:  the qualified name as appearing in the serialization
5382
 *
5383
 * Check if the given node is part of the content model.
5384
 *
5385
 * Returns 1 if yes, 0 if no, -1 in case of error
5386
 */
5387
static int
5388
xmlValidateCheckMixed(xmlValidCtxtPtr ctxt,
5389
9.07k
                xmlElementContentPtr cont, const xmlChar *qname) {
5390
9.07k
    const xmlChar *name;
5391
9.07k
    int plen;
5392
9.07k
    name = xmlSplitQName3(qname, &plen);
5393
5394
9.07k
    if (name == NULL) {
5395
31.0k
  while (cont != NULL) {
5396
29.3k
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5397
4.47k
    if ((cont->prefix == NULL) && (xmlStrEqual(cont->name, qname)))
5398
2.78k
        return(1);
5399
24.9k
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5400
24.9k
         (cont->c1 != NULL) &&
5401
24.9k
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5402
17.3k
    if ((cont->c1->prefix == NULL) &&
5403
17.3k
        (xmlStrEqual(cont->c1->name, qname)))
5404
3.09k
        return(1);
5405
17.3k
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5406
7.57k
    (cont->c1 == NULL) ||
5407
7.57k
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5408
0
    xmlErrValid(NULL, XML_DTD_MIXED_CORRUPT,
5409
0
      "Internal: MIXED struct corrupted\n",
5410
0
      NULL);
5411
0
    break;
5412
0
      }
5413
23.4k
      cont = cont->c2;
5414
23.4k
  }
5415
7.57k
    } else {
5416
5.32k
  while (cont != NULL) {
5417
3.87k
      if (cont->type == XML_ELEMENT_CONTENT_ELEMENT) {
5418
1.49k
    if ((cont->prefix != NULL) &&
5419
1.49k
        (xmlStrncmp(cont->prefix, qname, plen) == 0) &&
5420
1.49k
        (xmlStrEqual(cont->name, name)))
5421
34
        return(1);
5422
2.37k
      } else if ((cont->type == XML_ELEMENT_CONTENT_OR) &&
5423
2.37k
         (cont->c1 != NULL) &&
5424
2.37k
         (cont->c1->type == XML_ELEMENT_CONTENT_ELEMENT)){
5425
876
    if ((cont->c1->prefix != NULL) &&
5426
876
        (xmlStrncmp(cont->c1->prefix, qname, plen) == 0) &&
5427
876
        (xmlStrEqual(cont->c1->name, name)))
5428
10
        return(1);
5429
1.50k
      } else if ((cont->type != XML_ELEMENT_CONTENT_OR) ||
5430
1.50k
    (cont->c1 == NULL) ||
5431
1.50k
    (cont->c1->type != XML_ELEMENT_CONTENT_PCDATA)){
5432
0
    xmlErrValid(ctxt, XML_DTD_MIXED_CORRUPT,
5433
0
      "Internal: MIXED struct corrupted\n",
5434
0
      NULL);
5435
0
    break;
5436
0
      }
5437
3.82k
      cont = cont->c2;
5438
3.82k
  }
5439
1.50k
    }
5440
3.15k
    return(0);
5441
9.07k
}
5442
#endif /* LIBXML_REGEXP_ENABLED */
5443
5444
/**
5445
 * xmlValidGetElemDecl:
5446
 * @ctxt:  the validation context
5447
 * @doc:  a document instance
5448
 * @elem:  an element instance
5449
 * @extsubset:  pointer, (out) indicate if the declaration was found
5450
 *              in the external subset.
5451
 *
5452
 * Finds a declaration associated to an element in the document.
5453
 *
5454
 * returns the pointer to the declaration or NULL if not found.
5455
 */
5456
static xmlElementPtr
5457
xmlValidGetElemDecl(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5458
1.29M
              xmlNodePtr elem, int *extsubset) {
5459
1.29M
    xmlElementPtr elemDecl = NULL;
5460
1.29M
    const xmlChar *prefix = NULL;
5461
5462
1.29M
    if ((ctxt == NULL) || (doc == NULL) ||
5463
1.29M
        (elem == NULL) || (elem->name == NULL))
5464
842
        return(NULL);
5465
1.29M
    if (extsubset != NULL)
5466
1.29M
  *extsubset = 0;
5467
5468
    /*
5469
     * Fetch the declaration for the qualified name
5470
     */
5471
1.29M
    if ((elem->ns != NULL) && (elem->ns->prefix != NULL))
5472
58.9k
  prefix = elem->ns->prefix;
5473
5474
1.29M
    if (prefix != NULL) {
5475
58.9k
  elemDecl = xmlGetDtdQElementDesc(doc->intSubset,
5476
58.9k
                             elem->name, prefix);
5477
58.9k
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5478
3.03k
      elemDecl = xmlGetDtdQElementDesc(doc->extSubset,
5479
3.03k
                                 elem->name, prefix);
5480
3.03k
      if ((elemDecl != NULL) && (extsubset != NULL))
5481
72
    *extsubset = 1;
5482
3.03k
  }
5483
58.9k
    }
5484
5485
    /*
5486
     * Fetch the declaration for the non qualified name
5487
     * This is "non-strict" validation should be done on the
5488
     * full QName but in that case being flexible makes sense.
5489
     */
5490
1.29M
    if (elemDecl == NULL) {
5491
1.28M
  elemDecl = xmlGetDtdQElementDesc(doc->intSubset, elem->name, NULL);
5492
1.28M
  if ((elemDecl == NULL) && (doc->extSubset != NULL)) {
5493
51.4k
      elemDecl = xmlGetDtdQElementDesc(doc->extSubset, elem->name, NULL);
5494
51.4k
      if ((elemDecl != NULL) && (extsubset != NULL))
5495
38.3k
    *extsubset = 1;
5496
51.4k
  }
5497
1.28M
    }
5498
1.29M
    if (elemDecl == NULL) {
5499
598k
  xmlErrValidNode(ctxt, elem,
5500
598k
      XML_DTD_UNKNOWN_ELEM,
5501
598k
         "No declaration for element %s\n",
5502
598k
         elem->name, NULL, NULL);
5503
598k
    }
5504
1.29M
    return(elemDecl);
5505
1.29M
}
5506
5507
#ifdef LIBXML_REGEXP_ENABLED
5508
/**
5509
 * xmlValidatePushElement:
5510
 * @ctxt:  the validation context
5511
 * @doc:  a document instance
5512
 * @elem:  an element instance
5513
 * @qname:  the qualified name as appearing in the serialization
5514
 *
5515
 * Push a new element start on the validation stack.
5516
 *
5517
 * returns 1 if no validation problem was found or 0 otherwise
5518
 */
5519
int
5520
xmlValidatePushElement(xmlValidCtxtPtr ctxt, xmlDocPtr doc,
5521
369k
                       xmlNodePtr elem, const xmlChar *qname) {
5522
369k
    int ret = 1;
5523
369k
    xmlElementPtr eDecl;
5524
369k
    int extsubset = 0;
5525
5526
369k
    if (ctxt == NULL)
5527
0
        return(0);
5528
/* printf("PushElem %s\n", qname); */
5529
369k
    if ((ctxt->vstateNr > 0) && (ctxt->vstate != NULL)) {
5530
366k
  xmlValidStatePtr state = ctxt->vstate;
5531
366k