Coverage Report

Created: 2025-07-11 06:13

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