Coverage Report

Created: 2025-07-23 08:18

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