Coverage Report

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