valid.c 188 KB
Newer Older
1 2 3 4 5 6
/*
 * valid.c : part of the code use to do the DTD handling and the validity
 *           checking
 *
 * See Copyright for the status of this software.
 *
7
 * daniel@veillard.com
8 9
 */

10
#define IN_LIBXML
11
#include "libxml.h"
12 13 14 15 16 17 18 19 20

#include <string.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <libxml/xmlmemory.h>
#include <libxml/hash.h>
21
#include <libxml/uri.h>
22 23 24 25 26
#include <libxml/valid.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xmlerror.h>
#include <libxml/list.h>
27
#include <libxml/globals.h>
28

29 30
static xmlElementPtr xmlGetDtdElementDesc2(xmlDtdPtr dtd, const xmlChar *name,
	                           int create);
31
/* #define DEBUG_VALID_ALGO */
32
/* #define DEBUG_REGEXP_ALGO */
33

34
#define TODO								\
35 36 37
    xmlGenericError(xmlGenericErrorContext,				\
	    "Unimplemented block at %s:%d\n",				\
            __FILE__, __LINE__);
38

39 40 41 42 43
#ifdef LIBXML_VALID_ENABLED
static int
xmlValidateAttributeValueInternal(xmlDocPtr doc, xmlAttributeType type,
                                  const xmlChar *value);
#endif
44 45 46 47 48 49 50
/************************************************************************
 *									*
 *			Error handling routines				*
 *									*
 ************************************************************************/

/**
51
 * xmlVErrMemory:
52 53 54 55 56 57
 * @ctxt:  an XML validation parser context
 * @extra:  extra informations
 *
 * Handle an out of memory error
 */
static void
58
xmlVErrMemory(xmlValidCtxtPtr ctxt, const char *extra)
59
{
60 61 62 63 64 65 66
    xmlGenericErrorFunc channel = NULL;
    xmlParserCtxtPtr pctxt = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        channel = ctxt->error;
        data = ctxt->userData;
67 68 69 70
	/* Use the special values to detect if it is part of a parsing
	   context */
	if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
	    (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
71 72 73
	    long delta = (char *) ctxt - (char *) ctxt->userData;
	    if ((delta > 0) && (delta < 250))
		pctxt = ctxt->userData;
74
	}
75
    }
76
    if (extra)
77
        __xmlRaiseError(NULL, channel, data,
78
                        pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
79 80
                        XML_ERR_FATAL, NULL, 0, extra, NULL, NULL, 0, 0,
                        "Memory allocation failed : %s\n", extra);
81
    else
82
        __xmlRaiseError(NULL, channel, data,
83
                        pctxt, NULL, XML_FROM_VALID, XML_ERR_NO_MEMORY,
84 85
                        XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0,
                        "Memory allocation failed\n");
86 87 88 89 90
}

/**
 * xmlErrValid:
 * @ctxt:  an XML validation parser context
91
 * @error:  the error number
92 93 94 95 96
 * @extra:  extra informations
 *
 * Handle a validation error
 */
static void
97
xmlErrValid(xmlValidCtxtPtr ctxt, xmlParserErrors error,
98
            const char *msg, const char *extra)
99
{
100 101 102 103 104 105 106
    xmlGenericErrorFunc channel = NULL;
    xmlParserCtxtPtr pctxt = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        channel = ctxt->error;
        data = ctxt->userData;
107 108 109 110
	/* Use the special values to detect if it is part of a parsing
	   context */
	if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
	    (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
111 112 113
	    long delta = (char *) ctxt - (char *) ctxt->userData;
	    if ((delta > 0) && (delta < 250))
		pctxt = ctxt->userData;
114
	}
115
    }
116
    if (extra)
117
        __xmlRaiseError(NULL, channel, data,
118
                        pctxt, NULL, XML_FROM_VALID, error,
119 120
                        XML_ERR_ERROR, NULL, 0, extra, NULL, NULL, 0, 0,
                        msg, extra);
121
    else
122
        __xmlRaiseError(NULL, channel, data,
123
                        pctxt, NULL, XML_FROM_VALID, error,
124
                        XML_ERR_ERROR, NULL, 0, NULL, NULL, NULL, 0, 0,
125
                        "%s", msg);
126 127
}

128
#if defined(LIBXML_VALID_ENABLED) || defined(LIBXML_SCHEMAS_ENABLED)
129
/**
130
 * xmlErrValidNode:
131 132 133 134
 * @ctxt:  an XML validation parser context
 * @node:  the node raising the error
 * @error:  the error number
 * @str1:  extra informations
135
 * @str2:  extra informations
136 137 138 139 140
 * @str3:  extra informations
 *
 * Handle a validation error, provide contextual informations
 */
static void
141
xmlErrValidNode(xmlValidCtxtPtr ctxt,
142 143
                xmlNodePtr node, xmlParserErrors error,
                const char *msg, const xmlChar * str1,
144
                const xmlChar * str2, const xmlChar * str3)
145
{
146
    xmlStructuredErrorFunc schannel = NULL;
147 148 149 150 151 152 153
    xmlGenericErrorFunc channel = NULL;
    xmlParserCtxtPtr pctxt = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        channel = ctxt->error;
        data = ctxt->userData;
154 155 156 157
	/* Use the special values to detect if it is part of a parsing
	   context */
	if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
	    (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
158 159 160
	    long delta = (char *) ctxt - (char *) ctxt->userData;
	    if ((delta > 0) && (delta < 250))
		pctxt = ctxt->userData;
161
	}
162
    }
163
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
164 165
                    XML_ERR_ERROR, NULL, 0,
                    (const char *) str1,
166 167
                    (const char *) str1,
                    (const char *) str3, 0, 0, msg, str1, str2, str3);
168
}
169 170 171
#endif /* LIBXML_VALID_ENABLED or LIBXML_SCHEMAS_ENABLED */

#ifdef LIBXML_VALID_ENABLED
172
/**
173
 * xmlErrValidNodeNr:
174 175 176 177
 * @ctxt:  an XML validation parser context
 * @node:  the node raising the error
 * @error:  the error number
 * @str1:  extra informations
178
 * @int2:  extra informations
179 180 181 182 183
 * @str3:  extra informations
 *
 * Handle a validation error, provide contextual informations
 */
static void
184
xmlErrValidNodeNr(xmlValidCtxtPtr ctxt,
185 186
                xmlNodePtr node, xmlParserErrors error,
                const char *msg, const xmlChar * str1,
187
                int int2, const xmlChar * str3)
188
{
189
    xmlStructuredErrorFunc schannel = NULL;
190 191 192 193 194 195 196
    xmlGenericErrorFunc channel = NULL;
    xmlParserCtxtPtr pctxt = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
        channel = ctxt->error;
        data = ctxt->userData;
197 198 199 200
	/* Use the special values to detect if it is part of a parsing
	   context */
	if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
	    (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
201 202 203
	    long delta = (char *) ctxt - (char *) ctxt->userData;
	    if ((delta > 0) && (delta < 250))
		pctxt = ctxt->userData;
204
	}
205
    }
206
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
207 208
                    XML_ERR_ERROR, NULL, 0,
                    (const char *) str1,
209 210
                    (const char *) str3,
                    NULL, int2, 0, msg, str1, int2, str3);
211
}
212

213 214 215 216 217
/**
 * xmlErrValidWarning:
 * @ctxt:  an XML validation parser context
 * @node:  the node raising the error
 * @error:  the error number
218 219 220
 * @str1:  extra information
 * @str2:  extra information
 * @str3:  extra information
221
 *
222
 * Handle a validation error, provide contextual information
223 224
 */
static void
225
xmlErrValidWarning(xmlValidCtxtPtr ctxt,
226 227 228 229
                xmlNodePtr node, xmlParserErrors error,
                const char *msg, const xmlChar * str1,
                const xmlChar * str2, const xmlChar * str3)
{
230
    xmlStructuredErrorFunc schannel = NULL;
231 232 233 234 235
    xmlGenericErrorFunc channel = NULL;
    xmlParserCtxtPtr pctxt = NULL;
    void *data = NULL;

    if (ctxt != NULL) {
236
        channel = ctxt->warning;
237
        data = ctxt->userData;
238 239 240 241
	/* Use the special values to detect if it is part of a parsing
	   context */
	if ((ctxt->finishDtd == XML_CTXT_FINISH_DTD_0) ||
	    (ctxt->finishDtd == XML_CTXT_FINISH_DTD_1)) {
242 243 244
	    long delta = (char *) ctxt - (char *) ctxt->userData;
	    if ((delta > 0) && (delta < 250))
		pctxt = ctxt->userData;
245
	}
246
    }
247
    __xmlRaiseError(schannel, channel, data, pctxt, node, XML_FROM_VALID, error,
248 249 250 251
                    XML_ERR_WARNING, NULL, 0,
                    (const char *) str1,
                    (const char *) str1,
                    (const char *) str3, 0, 0, msg, str1, str2, str3);
252 253
}

254

255

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
#ifdef LIBXML_REGEXP_ENABLED
/*
 * If regexp are enabled we can do continuous validation without the
 * need of a tree to validate the content model. this is done in each
 * callbacks.
 * Each xmlValidState represent the validation state associated to the
 * set of nodes currently open from the document root to the current element.
 */


typedef struct _xmlValidState {
    xmlElementPtr	 elemDecl;	/* pointer to the content model */
    xmlNodePtr           node;		/* pointer to the current node */
    xmlRegExecCtxtPtr    exec;		/* regexp runtime */
} _xmlValidState;


static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementPtr elemDecl, xmlNodePtr node) {
275
    if ((ctxt->vstateMax == 0) || (ctxt->vstateTab == NULL)) {
276 277 278 279
	ctxt->vstateMax = 10;
	ctxt->vstateTab = (xmlValidState *) xmlMalloc(ctxt->vstateMax *
		              sizeof(ctxt->vstateTab[0]));
        if (ctxt->vstateTab == NULL) {
280
	    xmlVErrMemory(ctxt, "malloc failed");
281 282 283 284 285
	    return(-1);
	}
    }

    if (ctxt->vstateNr >= ctxt->vstateMax) {
286 287 288 289 290
        xmlValidState *tmp;

	tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
	             2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
        if (tmp == NULL) {
291
	    xmlVErrMemory(ctxt, "realloc failed");
292 293
	    return(-1);
	}
294 295
	ctxt->vstateMax *= 2;
	ctxt->vstateTab = tmp;
296 297 298 299 300 301 302 303
    }
    ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr];
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = elemDecl;
    ctxt->vstateTab[ctxt->vstateNr].node = node;
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
	if (elemDecl->contModel == NULL)
	    xmlValidBuildContentModel(ctxt, elemDecl);
	if (elemDecl->contModel != NULL) {
304
	    ctxt->vstateTab[ctxt->vstateNr].exec =
305 306 307
		xmlRegNewExecCtxt(elemDecl->contModel, NULL, NULL);
	} else {
	    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
308 309 310 311
	    xmlErrValidNode(ctxt, (xmlNodePtr) elemDecl,
	                    XML_ERR_INTERNAL_ERROR,
			    "Failed to build content model regexp for %s\n",
			    node->name, NULL, NULL);
312 313 314 315 316 317 318 319 320
	}
    }
    return(ctxt->vstateNr++);
}

static int
vstateVPop(xmlValidCtxtPtr ctxt) {
    xmlElementPtr elemDecl;

321
    if (ctxt->vstateNr < 1) return(-1);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    ctxt->vstateNr--;
    elemDecl = ctxt->vstateTab[ctxt->vstateNr].elemDecl;
    ctxt->vstateTab[ctxt->vstateNr].elemDecl = NULL;
    ctxt->vstateTab[ctxt->vstateNr].node = NULL;
    if ((elemDecl != NULL) && (elemDecl->etype == XML_ELEMENT_TYPE_ELEMENT)) {
	xmlRegFreeExecCtxt(ctxt->vstateTab[ctxt->vstateNr].exec);
    }
    ctxt->vstateTab[ctxt->vstateNr].exec = NULL;
    if (ctxt->vstateNr >= 1)
	ctxt->vstate = &ctxt->vstateTab[ctxt->vstateNr - 1];
    else
	ctxt->vstate = NULL;
    return(ctxt->vstateNr);
}

#else /* not LIBXML_REGEXP_ENABLED */
338
/*
Daniel Veillard's avatar
Daniel Veillard committed
339 340
 * If regexp are not enabled, it uses a home made algorithm less
 * complex and easier to
341
 * debug/maintain than a generic NFA -> DFA state based algo. The
342 343 344 345 346 347 348 349 350
 * only restriction is on the deepness of the tree limited by the
 * size of the occurs bitfield
 *
 * this is the content of a saved state for rollbacks
 */

#define ROLLBACK_OR	0
#define ROLLBACK_PARENT	1

351
typedef struct _xmlValidState {
352 353
    xmlElementContentPtr cont;	/* pointer to the content model subtree */
    xmlNodePtr           node;	/* pointer to the current node in the list */
354
    long                 occurs;/* bitfield for multiple occurrences */
355 356 357 358
    unsigned char        depth; /* current depth in the overall tree */
    unsigned char        state; /* ROLLBACK_XXX */
} _xmlValidState;

359
#define MAX_RECURSE 25000
360 361 362 363 364 365 366
#define MAX_DEPTH ((sizeof(_xmlValidState.occurs)) * 8)
#define CONT ctxt->vstate->cont
#define NODE ctxt->vstate->node
#define DEPTH ctxt->vstate->depth
#define OCCURS ctxt->vstate->occurs
#define STATE ctxt->vstate->state

367 368
#define OCCURRENCE (ctxt->vstate->occurs & (1 << DEPTH))
#define PARENT_OCCURRENCE (ctxt->vstate->occurs & ((1 << DEPTH) - 1))
369

370 371
#define SET_OCCURRENCE ctxt->vstate->occurs |= (1 << DEPTH)
#define RESET_OCCURRENCE ctxt->vstate->occurs &= ((1 << DEPTH) - 1)
372 373 374 375 376

static int
vstateVPush(xmlValidCtxtPtr ctxt, xmlElementContentPtr cont,
	    xmlNodePtr node, unsigned char depth, long occurs,
	    unsigned char state) {
377 378
    int i = ctxt->vstateNr - 1;

379 380 381
    if (ctxt->vstateNr > MAX_RECURSE) {
	return(-1);
    }
382 383 384 385 386
    if (ctxt->vstateTab == NULL) {
	ctxt->vstateMax = 8;
	ctxt->vstateTab = (xmlValidState *) xmlMalloc(
		     ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
	if (ctxt->vstateTab == NULL) {
387
	    xmlVErrMemory(ctxt, "malloc failed");
388 389 390
	    return(-1);
	}
    }
391
    if (ctxt->vstateNr >= ctxt->vstateMax) {
392 393 394 395 396
        xmlValidState *tmp;

        tmp = (xmlValidState *) xmlRealloc(ctxt->vstateTab,
	             2 * ctxt->vstateMax * sizeof(ctxt->vstateTab[0]));
        if (tmp == NULL) {
397
	    xmlVErrMemory(ctxt, "malloc failed");
398
	    return(-1);
399
	}
400 401
	ctxt->vstateMax *= 2;
	ctxt->vstateTab = tmp;
402
	ctxt->vstate = &ctxt->vstateTab[0];
403
    }
404 405 406 407 408 409 410 411 412
    /*
     * Don't push on the stack a state already here
     */
    if ((i >= 0) && (ctxt->vstateTab[i].cont == cont) &&
	(ctxt->vstateTab[i].node == node) &&
	(ctxt->vstateTab[i].depth == depth) &&
	(ctxt->vstateTab[i].occurs == occurs) &&
	(ctxt->vstateTab[i].state == state))
	return(ctxt->vstateNr);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    ctxt->vstateTab[ctxt->vstateNr].cont = cont;
    ctxt->vstateTab[ctxt->vstateNr].node = node;
    ctxt->vstateTab[ctxt->vstateNr].depth = depth;
    ctxt->vstateTab[ctxt->vstateNr].occurs = occurs;
    ctxt->vstateTab[ctxt->vstateNr].state = state;
    return(ctxt->vstateNr++);
}

static int
vstateVPop(xmlValidCtxtPtr ctxt) {
    if (ctxt->vstateNr <= 1) return(-1);
    ctxt->vstateNr--;
    ctxt->vstate = &ctxt->vstateTab[0];
    ctxt->vstate->cont =  ctxt->vstateTab[ctxt->vstateNr].cont;
    ctxt->vstate->node = ctxt->vstateTab[ctxt->vstateNr].node;
    ctxt->vstate->depth = ctxt->vstateTab[ctxt->vstateNr].depth;
    ctxt->vstate->occurs = ctxt->vstateTab[ctxt->vstateNr].occurs;
    ctxt->vstate->state = ctxt->vstateTab[ctxt->vstateNr].state;
    return(ctxt->vstateNr);
}

434 435
#endif /* LIBXML_REGEXP_ENABLED */

Daniel Veillard's avatar
Daniel Veillard committed
436 437 438 439 440 441 442 443 444
static int
nodeVPush(xmlValidCtxtPtr ctxt, xmlNodePtr value)
{
    if (ctxt->nodeMax <= 0) {
        ctxt->nodeMax = 4;
        ctxt->nodeTab =
            (xmlNodePtr *) xmlMalloc(ctxt->nodeMax *
                                     sizeof(ctxt->nodeTab[0]));
        if (ctxt->nodeTab == NULL) {
445
	    xmlVErrMemory(ctxt, "malloc failed");
Daniel Veillard's avatar
Daniel Veillard committed
446 447 448 449 450
            ctxt->nodeMax = 0;
            return (0);
        }
    }
    if (ctxt->nodeNr >= ctxt->nodeMax) {
451 452 453 454
        xmlNodePtr *tmp;
        tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
			      ctxt->nodeMax * 2 * sizeof(ctxt->nodeTab[0]));
        if (tmp == NULL) {
455
	    xmlVErrMemory(ctxt, "realloc failed");
Daniel Veillard's avatar
Daniel Veillard committed
456 457
            return (0);
        }
458 459
        ctxt->nodeMax *= 2;
	ctxt->nodeTab = tmp;
Daniel Veillard's avatar
Daniel Veillard committed
460 461 462 463 464 465 466 467 468 469 470
    }
    ctxt->nodeTab[ctxt->nodeNr] = value;
    ctxt->node = value;
    return (ctxt->nodeNr++);
}
static xmlNodePtr
nodeVPop(xmlValidCtxtPtr ctxt)
{
    xmlNodePtr ret;

    if (ctxt->nodeNr <= 0)
471
        return (NULL);
Daniel Veillard's avatar
Daniel Veillard committed
472 473 474 475 476 477
    ctxt->nodeNr--;
    if (ctxt->nodeNr > 0)
        ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
    else
        ctxt->node = NULL;
    ret = ctxt->nodeTab[ctxt->nodeNr];
478
    ctxt->nodeTab[ctxt->nodeNr] = NULL;
Daniel Veillard's avatar
Daniel Veillard committed
479 480
    return (ret);
}
481 482

#ifdef DEBUG_VALID_ALGO
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
static void
xmlValidPrintNode(xmlNodePtr cur) {
    if (cur == NULL) {
	xmlGenericError(xmlGenericErrorContext, "null");
	return;
    }
    switch (cur->type) {
	case XML_ELEMENT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "%s ", cur->name);
	    break;
	case XML_TEXT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "text ");
	    break;
	case XML_CDATA_SECTION_NODE:
	    xmlGenericError(xmlGenericErrorContext, "cdata ");
	    break;
	case XML_ENTITY_REF_NODE:
	    xmlGenericError(xmlGenericErrorContext, "&%s; ", cur->name);
	    break;
	case XML_PI_NODE:
	    xmlGenericError(xmlGenericErrorContext, "pi(%s) ", cur->name);
	    break;
	case XML_COMMENT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "comment ");
	    break;
	case XML_ATTRIBUTE_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?attr? ");
	    break;
	case XML_ENTITY_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?ent? ");
	    break;
	case XML_DOCUMENT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?doc? ");
	    break;
	case XML_DOCUMENT_TYPE_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?doctype? ");
	    break;
	case XML_DOCUMENT_FRAG_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?frag? ");
	    break;
	case XML_NOTATION_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?nota? ");
	    break;
	case XML_HTML_DOCUMENT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?html? ");
	    break;
529 530 531 532 533
#ifdef LIBXML_DOCB_ENABLED
	case XML_DOCB_DOCUMENT_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?docb? ");
	    break;
#endif
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
	case XML_DTD_NODE:
	    xmlGenericError(xmlGenericErrorContext, "?dtd? ");
	    break;
	case XML_ELEMENT_DECL:
	    xmlGenericError(xmlGenericErrorContext, "?edecl? ");
	    break;
	case XML_ATTRIBUTE_DECL:
	    xmlGenericError(xmlGenericErrorContext, "?adecl? ");
	    break;
	case XML_ENTITY_DECL:
	    xmlGenericError(xmlGenericErrorContext, "?entdecl? ");
	    break;
	case XML_NAMESPACE_DECL:
	    xmlGenericError(xmlGenericErrorContext, "?nsdecl? ");
	    break;
	case XML_XINCLUDE_START:
	    xmlGenericError(xmlGenericErrorContext, "incstart ");
	    break;
	case XML_XINCLUDE_END:
	    xmlGenericError(xmlGenericErrorContext, "incend ");
	    break;
    }
}

static void
xmlValidPrintNodeList(xmlNodePtr cur) {
560 561 562
    if (cur == NULL)
	xmlGenericError(xmlGenericErrorContext, "null ");
    while (cur != NULL) {
563
	xmlValidPrintNode(cur);
564 565 566 567
	cur = cur->next;
    }
}

568 569
static void
xmlValidDebug(xmlNodePtr cur, xmlElementContentPtr cont) {
570
    char expr[5000];
571 572 573 574 575

    expr[0] = 0;
    xmlGenericError(xmlGenericErrorContext, "valid: ");
    xmlValidPrintNodeList(cur);
    xmlGenericError(xmlGenericErrorContext, "against ");
576
    xmlSnprintfElementContent(expr, 5000, cont, 1);
577 578 579
    xmlGenericError(xmlGenericErrorContext, "%s\n", expr);
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
static void
xmlValidDebugState(xmlValidStatePtr state) {
    xmlGenericError(xmlGenericErrorContext, "(");
    if (state->cont == NULL)
	xmlGenericError(xmlGenericErrorContext, "null,");
    else
	switch (state->cont->type) {
            case XML_ELEMENT_CONTENT_PCDATA:
		xmlGenericError(xmlGenericErrorContext, "pcdata,");
		break;
            case XML_ELEMENT_CONTENT_ELEMENT:
		xmlGenericError(xmlGenericErrorContext, "%s,",
			        state->cont->name);
		break;
            case XML_ELEMENT_CONTENT_SEQ:
		xmlGenericError(xmlGenericErrorContext, "seq,");
		break;
            case XML_ELEMENT_CONTENT_OR:
		xmlGenericError(xmlGenericErrorContext, "or,");
		break;
	}
    xmlValidPrintNode(state->node);
    xmlGenericError(xmlGenericErrorContext, ",%d,%X,%d)",
	    state->depth, state->occurs, state->state);
}

static void
xmlValidStateDebug(xmlValidCtxtPtr ctxt) {
    int i, j;

    xmlGenericError(xmlGenericErrorContext, "state: ");
    xmlValidDebugState(ctxt->vstate);
    xmlGenericError(xmlGenericErrorContext, " stack: %d ",
	    ctxt->vstateNr - 1);
    for (i = 0, j = ctxt->vstateNr - 1;(i < 3) && (j > 0);i++,j--)
	xmlValidDebugState(&ctxt->vstateTab[j]);
    xmlGenericError(xmlGenericErrorContext, "\n");
}

/*****
620
#define DEBUG_VALID_STATE(n,c) xmlValidDebug(n,c);
621 622 623
 *****/

#define DEBUG_VALID_STATE(n,c) xmlValidStateDebug(ctxt);
624 625
#define DEBUG_VALID_MSG(m)					\
    xmlGenericError(xmlGenericErrorContext, "%s\n", m);
626

627 628
#else
#define DEBUG_VALID_STATE(n,c)
629
#define DEBUG_VALID_MSG(m)
630 631
#endif

632
/* TODO: use hash table for accesses to elem and attribute definitions */
633

634

635 636 637 638 639
#define CHECK_DTD						\
   if (doc == NULL) return(0);					\
   else if ((doc->intSubset == NULL) &&				\
	    (doc->extSubset == NULL)) return(0)

640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
#ifdef LIBXML_REGEXP_ENABLED

/************************************************************************
 *									*
 *		Content model validation based on the regexps		*
 *									*
 ************************************************************************/

/**
 * xmlValidBuildAContentModel:
 * @content:  the content model
 * @ctxt:  the schema parser context
 * @name:  the element name whose content is being built
 *
 * Generate the automata sequence needed for that type
 *
656
 * Returns 1 if successful or 0 in case of error.
657 658 659 660 661 662
 */
static int
xmlValidBuildAContentModel(xmlElementContentPtr content,
		           xmlValidCtxtPtr ctxt,
		           const xmlChar *name) {
    if (content == NULL) {
663 664 665
	xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
			"Found NULL content in content model of %s\n",
			name, NULL, NULL);
666
	return(0);
667 668 669
    }
    switch (content->type) {
	case XML_ELEMENT_CONTENT_PCDATA:
670 671 672
	    xmlErrValidNode(ctxt, NULL, XML_ERR_INTERNAL_ERROR,
			    "Found PCDATA in content model of %s\n",
		            name, NULL, NULL);
673
	    return(0);
674 675 676
	    break;
	case XML_ELEMENT_CONTENT_ELEMENT: {
	    xmlAutomataStatePtr oldstate = ctxt->state;
677 678
	    xmlChar fn[50];
	    xmlChar *fullname;
679

680 681
	    fullname = xmlBuildQName(content->name, content->prefix, fn, 50);
	    if (fullname == NULL) {
682
	        xmlVErrMemory(ctxt, "Building content model");
683
		return(0);
684 685
	    }

686 687 688
	    switch (content->ocur) {
		case XML_ELEMENT_CONTENT_ONCE:
		    ctxt->state = xmlAutomataNewTransition(ctxt->am,
689
			    ctxt->state, NULL, fullname, NULL);
690 691 692
		    break;
		case XML_ELEMENT_CONTENT_OPT:
		    ctxt->state = xmlAutomataNewTransition(ctxt->am,
693
			    ctxt->state, NULL, fullname, NULL);
694 695 696 697
		    xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
		    break;
		case XML_ELEMENT_CONTENT_PLUS:
		    ctxt->state = xmlAutomataNewTransition(ctxt->am,
698
			    ctxt->state, NULL, fullname, NULL);
699
		    xmlAutomataNewTransition(ctxt->am, ctxt->state,
700
			                     ctxt->state, fullname, NULL);
701 702
		    break;
		case XML_ELEMENT_CONTENT_MULT:
703
		    ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
704
					    ctxt->state, NULL);
705
		    xmlAutomataNewTransition(ctxt->am,
706
			    ctxt->state, ctxt->state, fullname, NULL);
707 708
		    break;
	    }
709 710
	    if ((fullname != fn) && (fullname != content->name))
		xmlFree(fullname);
711 712 713
	    break;
	}
	case XML_ELEMENT_CONTENT_SEQ: {
714
	    xmlAutomataStatePtr oldstate, oldend;
715 716 717 718 719 720 721
	    xmlElementContentOccur ocur;

	    /*
	     * Simply iterate over the content
	     */
	    oldstate = ctxt->state;
	    ocur = content->ocur;
722 723 724 725
	    if (ocur != XML_ELEMENT_CONTENT_ONCE) {
		ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
		oldstate = ctxt->state;
	    }
726
	    do {
727 728
		xmlValidBuildAContentModel(content->c1, ctxt, name);
		content = content->c2;
729 730 731
	    } while ((content->type == XML_ELEMENT_CONTENT_SEQ) &&
		     (content->ocur == XML_ELEMENT_CONTENT_ONCE));
	    xmlValidBuildAContentModel(content, ctxt, name);
732 733
	    oldend = ctxt->state;
	    ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
734 735 736 737 738 739 740 741
	    switch (ocur) {
		case XML_ELEMENT_CONTENT_ONCE:
		    break;
		case XML_ELEMENT_CONTENT_OPT:
		    xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
		    break;
		case XML_ELEMENT_CONTENT_MULT:
		    xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
742
		    xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
743 744
		    break;
		case XML_ELEMENT_CONTENT_PLUS:
745
		    xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
746 747 748 749 750
		    break;
	    }
	    break;
	}
	case XML_ELEMENT_CONTENT_OR: {
751
	    xmlAutomataStatePtr oldstate, oldend;
752 753 754
	    xmlElementContentOccur ocur;

	    ocur = content->ocur;
755
	    if ((ocur == XML_ELEMENT_CONTENT_PLUS) ||
756 757 758 759 760 761
		(ocur == XML_ELEMENT_CONTENT_MULT)) {
		ctxt->state = xmlAutomataNewEpsilon(ctxt->am,
			ctxt->state, NULL);
	    }
	    oldstate = ctxt->state;
	    oldend = xmlAutomataNewState(ctxt->am);
762 763 764 765 766

	    /*
	     * iterate over the subtypes and remerge the end with an
	     * epsilon transition
	     */
767
	    do {
768
		ctxt->state = oldstate;
769
		xmlValidBuildAContentModel(content->c1, ctxt, name);
770
		xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
771
		content = content->c2;
772 773
	    } while ((content->type == XML_ELEMENT_CONTENT_OR) &&
		     (content->ocur == XML_ELEMENT_CONTENT_ONCE));
774
	    ctxt->state = oldstate;
775
	    xmlValidBuildAContentModel(content, ctxt, name);
776 777
	    xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldend);
	    ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldend, NULL);
778 779 780 781
	    switch (ocur) {
		case XML_ELEMENT_CONTENT_ONCE:
		    break;
		case XML_ELEMENT_CONTENT_OPT:
782
		    xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
783 784
		    break;
		case XML_ELEMENT_CONTENT_MULT:
785 786
		    xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
		    xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
787 788
		    break;
		case XML_ELEMENT_CONTENT_PLUS:
789
		    xmlAutomataNewEpsilon(ctxt->am, oldend, oldstate);
790 791 792 793 794
		    break;
	    }
	    break;
	}
	default:
795 796 797
	    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
	                "ContentModel broken for element %s\n",
			(const char *) name);
798
	    return(0);
799
    }
800
    return(1);
801 802 803 804 805 806 807 808 809
}
/**
 * xmlValidBuildContentModel:
 * @ctxt:  a validation context
 * @elem:  an element declaration node
 *
 * (Re)Build the automata associated to the content model of this
 * element
 *
810
 * Returns 1 in case of success, 0 in case of error
811 812 813 814 815
 */
int
xmlValidBuildContentModel(xmlValidCtxtPtr ctxt, xmlElementPtr elem) {

    if ((ctxt == NULL) || (elem == NULL))
816
	return(0);
817 818
    if (elem->type != XML_ELEMENT_DECL)
	return(0);
819 820
    if (elem->etype != XML_ELEMENT_TYPE_ELEMENT)
	return(1);
821
    /* TODO: should we rebuild in this case ? */
822 823 824 825 826
    if (elem->contModel != NULL) {
	if (!xmlRegexpIsDeterminist(elem->contModel)) {
	    ctxt->valid = 0;
	    return(0);
	}
827
	return(1);
828
    }
829 830 831

    ctxt->am = xmlNewAutomata();
    if (ctxt->am == NULL) {
832 833 834 835
	xmlErrValidNode(ctxt, (xmlNodePtr) elem,
	                XML_ERR_INTERNAL_ERROR,
	                "Cannot create automata for element %s\n",
		        elem->name, NULL, NULL);
836
	return(0);
837
    }
William M. Brack's avatar
William M. Brack committed
838
    ctxt->state = xmlAutomataGetInitState(ctxt->am);
839 840
    xmlValidBuildAContentModel(elem->content, ctxt, elem->name);
    xmlAutomataSetFinalState(ctxt->am, ctxt->state);
841
    elem->contModel = xmlAutomataCompile(ctxt->am);
842
    if (xmlRegexpIsDeterminist(elem->contModel) != 1) {
843 844 845
	char expr[5000];
	expr[0] = 0;
	xmlSnprintfElementContent(expr, 5000, elem->content, 1);
846 847 848 849
	xmlErrValidNode(ctxt, (xmlNodePtr) elem,
	                XML_DTD_CONTENT_NOT_DETERMINIST,
	       "Content model of %s is not determinist: %s\n",
	       elem->name, BAD_CAST expr, NULL);
850 851 852
#ifdef DEBUG_REGEXP_ALGO
        xmlRegexpPrint(stderr, elem->contModel);
#endif
853
        ctxt->valid = 0;
854 855 856 857
	ctxt->state = NULL;
	xmlFreeAutomata(ctxt->am);
	ctxt->am = NULL;
	return(0);
858 859 860 861
    }
    ctxt->state = NULL;
    xmlFreeAutomata(ctxt->am);
    ctxt->am = NULL;
862
    return(1);
863 864 865 866
}

#endif /* LIBXML_REGEXP_ENABLED */

867 868 869 870 871 872
/****************************************************************
 *								*
 *	Util functions for data allocation/deallocation		*
 *								*
 ****************************************************************/

873 874 875 876 877 878 879
/**
 * xmlNewValidCtxt:
 *
 * Allocate a validation context structure.
 *
 * Returns NULL if not, otherwise the new validation context structure
 */
880
xmlValidCtxtPtr xmlNewValidCtxt(void) {
881 882
    xmlValidCtxtPtr ret;

883
    if ((ret = xmlMalloc(sizeof (xmlValidCtxt))) == NULL) {
884
	xmlVErrMemory(NULL, "malloc failed");
885
	return (NULL);
886
    }
887 888 889 890 891 892 893 894 895 896 897 898 899 900

    (void) memset(ret, 0, sizeof (xmlValidCtxt));

    return (ret);
}

/**
 * xmlFreeValidCtxt:
 * @cur:  the validation context to free
 *
 * Free a validation context structure.
 */
void
xmlFreeValidCtxt(xmlValidCtxtPtr cur) {
901 902 903 904
    if (cur->vstateTab != NULL)
        xmlFree(cur->vstateTab);
    if (cur->nodeTab != NULL)
        xmlFree(cur->nodeTab);
905 906 907
    xmlFree(cur);
}

908 909
#endif /* LIBXML_VALID_ENABLED */

910
/**
911 912
 * xmlNewDocElementContent:
 * @doc:  the document
913 914 915
 * @name:  the subelement name or NULL
 * @type:  the type of element content decl
 *
916
 * Allocate an element content structure for the document.
917
 *
918
 * Returns NULL if not, otherwise the new element content structure
919 920
 */
xmlElementContentPtr
921 922
xmlNewDocElementContent(xmlDocPtr doc, const xmlChar *name,
                        xmlElementContentType type) {
923
    xmlElementContentPtr ret;
924 925 926 927
    xmlDictPtr dict = NULL;

    if (doc != NULL)
        dict = doc->dict;
928 929 930 931

    switch(type) {
	case XML_ELEMENT_CONTENT_ELEMENT:
	    if (name == NULL) {
932 933 934
	        xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
			"xmlNewElementContent : name == NULL !\n",
			NULL);
935 936 937 938 939 940
	    }
	    break;
        case XML_ELEMENT_CONTENT_PCDATA:
	case XML_ELEMENT_CONTENT_SEQ:
	case XML_ELEMENT_CONTENT_OR:
	    if (name != NULL) {
941 942 943
	        xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
			"xmlNewElementContent : name != NULL !\n",
			NULL);
944 945 946
	    }
	    break;
	default:
947
	    xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
948 949
		    "Internal: ELEMENT content corrupted invalid type\n",
		    NULL);
950 951 952 953
	    return(NULL);
    }
    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
    if (ret == NULL) {
954
	xmlVErrMemory(NULL, "malloc failed");
955 956
	return(NULL);
    }
957
    memset(ret, 0, sizeof(xmlElementContent));
958 959
    ret->type = type;
    ret->ocur = XML_ELEMENT_CONTENT_ONCE;
960
    if (name != NULL) {
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
        int l;
	const xmlChar *tmp;

	tmp = xmlSplitQName3(name, &l);
	if (tmp == NULL) {
	    if (dict == NULL)
		ret->name = xmlStrdup(name);
	    else
	        ret->name = xmlDictLookup(dict, name, -1);
	} else {
	    if (dict == NULL) {
		ret->prefix = xmlStrndup(name, l);
		ret->name = xmlStrdup(tmp);
	    } else {
	        ret->prefix = xmlDictLookup(dict, name, l);
		ret->name = xmlDictLookup(dict, tmp, -1);
	    }
	}
979
    }
980 981 982 983
    return(ret);
}

/**
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
 * xmlNewElementContent:
 * @name:  the subelement name or NULL
 * @type:  the type of element content decl
 *
 * Allocate an element content structure.
 * Deprecated in favor of xmlNewDocElementContent
 *
 * Returns NULL if not, otherwise the new element content structure
 */
xmlElementContentPtr
xmlNewElementContent(const xmlChar *name, xmlElementContentType type) {
    return(xmlNewDocElementContent(NULL, name, type));
}

/**
 * xmlCopyDocElementContent:
 * @doc:  the document owning the element declaration
1001
 * @cur:  An element content pointer.
1002 1003
 *
 * Build a copy of an element content description.
1004
 *
1005 1006 1007
 * Returns the new xmlElementContentPtr or NULL in case of error.
 */
xmlElementContentPtr
1008 1009 1010
xmlCopyDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
    xmlElementContentPtr ret = NULL, prev = NULL, tmp;
    xmlDictPtr dict = NULL;
1011 1012

    if (cur == NULL) return(NULL);
1013 1014 1015 1016 1017

    if (doc != NULL)
        dict = doc->dict;

    ret = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
1018
    if (ret == NULL) {
1019
	xmlVErrMemory(NULL, "malloc failed");
1020 1021
	return(NULL);
    }
1022 1023
    memset(ret, 0, sizeof(xmlElementContent));
    ret->type = cur->type;
1024
    ret->ocur = cur->ocur;
1025 1026 1027 1028 1029 1030
    if (cur->name != NULL) {
	if (dict)
	    ret->name = xmlDictLookup(dict, cur->name, -1);
	else
	    ret->name = xmlStrdup(cur->name);
    }
1031

1032 1033 1034 1035 1036 1037 1038 1039
    if (cur->prefix != NULL) {
	if (dict)
	    ret->prefix = xmlDictLookup(dict, cur->prefix, -1);
	else
	    ret->prefix = xmlStrdup(cur->prefix);
    }
    if (cur->c1 != NULL)
        ret->c1 = xmlCopyDocElementContent(doc, cur->c1);
1040 1041
    if (ret->c1 != NULL)
	ret->c1->parent = ret;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    if (cur->c2 != NULL) {
        prev = ret;
	cur = cur->c2;
	while (cur != NULL) {
	    tmp = (xmlElementContentPtr) xmlMalloc(sizeof(xmlElementContent));
	    if (tmp == NULL) {
		xmlVErrMemory(NULL, "malloc failed");
		return(ret);
	    }
	    memset(tmp, 0, sizeof(xmlElementContent));
	    tmp->type = cur->type;
	    tmp->ocur = cur->ocur;
	    prev->c2 = tmp;
	    if (cur->name != NULL) {
		if (dict)
		    tmp->name = xmlDictLookup(dict, cur->name, -1);
		else
		    tmp->name = xmlStrdup(cur->name);
	    }
1061

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	    if (cur->prefix != NULL) {
		if (dict)
		    tmp->prefix = xmlDictLookup(dict, cur->prefix, -1);
		else
		    tmp->prefix = xmlStrdup(cur->prefix);
	    }
	    if (cur->c1 != NULL)
	        tmp->c1 = xmlCopyDocElementContent(doc,cur->c1);
	    if (tmp->c1 != NULL)
		tmp->c1->parent = ret;
	    prev = tmp;
	    cur = cur->c2;
	}
    }
1076 1077 1078
    return(ret);
}

1079 1080 1081 1082 1083 1084
/**
 * xmlCopyElementContent:
 * @cur:  An element content pointer.
 *
 * Build a copy of an element content description.
 * Deprecated, use xmlCopyDocElementContent instead
1085
 *
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
 * Returns the new xmlElementContentPtr or NULL in case of error.
 */
xmlElementContentPtr
xmlCopyElementContent(xmlElementContentPtr cur) {
    return(xmlCopyDocElementContent(NULL, cur));
}

/**
 * xmlFreeDocElementContent:
 * @doc: the document owning the element declaration
 * @cur:  the element content tree to free
 *
 * Free an element content structure. The whole subtree is removed.
 */
void
xmlFreeDocElementContent(xmlDocPtr doc, xmlElementContentPtr cur) {
    xmlElementContentPtr next;
    xmlDictPtr dict = NULL;

    if (doc != NULL)
        dict = doc->dict;

    while (cur != NULL) {
        next = cur->c2;
	switch (cur->type) {
	    case XML_ELEMENT_CONTENT_PCDATA:
	    case XML_ELEMENT_CONTENT_ELEMENT:
	    case XML_ELEMENT_CONTENT_SEQ:
	    case XML_ELEMENT_CONTENT_OR:
		break;
	    default:
1117
		xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
			"Internal: ELEMENT content corrupted invalid type\n",
			NULL);
		return;
	}
	if (cur->c1 != NULL) xmlFreeDocElementContent(doc, cur->c1);
	if (dict) {
	    if ((cur->name != NULL) && (!xmlDictOwns(dict, cur->name)))
	        xmlFree((xmlChar *) cur->name);
	    if ((cur->prefix != NULL) && (!xmlDictOwns(dict, cur->prefix)))
	        xmlFree((xmlChar *) cur->prefix);
	} else {
	    if (cur->name != NULL) xmlFree((xmlChar *) cur->name);
	    if (cur->prefix != NULL) xmlFree((xmlChar *) cur->prefix);
	}
	xmlFree(cur);
	cur = next;
    }
}

1137 1138 1139 1140
/**
 * xmlFreeElementContent:
 * @cur:  the element content tree to free
 *
1141 1142
 * Free an element content structure. The whole subtree is removed.
 * Deprecated, use xmlFreeDocElementContent instead
1143 1144 1145
 */
void
xmlFreeElementContent(xmlElementContentPtr cur) {
1146
    xmlFreeDocElementContent(NULL, cur);
1147 1148
}

1149
#ifdef LIBXML_OUTPUT_ENABLED
1150 1151 1152 1153 1154 1155 1156 1157
/**
 * xmlDumpElementContent:
 * @buf:  An XML buffer
 * @content:  An element table
 * @glob: 1 if one must print the englobing parenthesis, 0 otherwise
 *
 * This will dump the content of the element table as an XML DTD definition
 */
1158
static void
1159 1160 1161 1162 1163 1164 1165 1166 1167
xmlDumpElementContent(xmlBufferPtr buf, xmlElementContentPtr content, int glob) {
    if (content == NULL) return;

    if (glob) xmlBufferWriteChar(buf, "(");
    switch (content->type) {
        case XML_ELEMENT_CONTENT_PCDATA:
            xmlBufferWriteChar(buf, "#PCDATA");
	    break;
	case XML_ELEMENT_CONTENT_ELEMENT:
1168 1169 1170 1171
	    if (content->prefix != NULL) {
		xmlBufferWriteCHAR(buf, content->prefix);
		xmlBufferWriteChar(buf, ":");
	    }
1172 1173 1174 1175 1176 1177 1178 1179 1180
	    xmlBufferWriteCHAR(buf, content->name);
	    break;
	case XML_ELEMENT_CONTENT_SEQ:
	    if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
	        (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
		xmlDumpElementContent(buf, content->c1, 1);
	    else
		xmlDumpElementContent(buf, content->c1, 0);
            xmlBufferWriteChar(buf, " , ");
1181 1182 1183
	    if ((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
	        ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) &&
		 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
		xmlDumpElementContent(buf, content->c2, 1);
	    else
		xmlDumpElementContent(buf, content->c2, 0);
	    break;
	case XML_ELEMENT_CONTENT_OR:
	    if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
	        (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
		xmlDumpElementContent(buf, content->c1, 1);
	    else
		xmlDumpElementContent(buf, content->c1, 0);
            xmlBufferWriteChar(buf, " | ");
1195 1196 1197
	    if ((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
	        ((content->c2->type == XML_ELEMENT_CONTENT_OR) &&
		 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)))
1198 1199 1200 1201 1202
		xmlDumpElementContent(buf, content->c2, 1);
	    else
		xmlDumpElementContent(buf, content->c2, 0);
	    break;
	default:
1203
	    xmlErrValid(NULL, XML_ERR_INTERNAL_ERROR,
1204 1205
		    "Internal: ELEMENT content corrupted invalid type\n",
		    NULL);
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
    }
    if (glob)
        xmlBufferWriteChar(buf, ")");
    switch (content->ocur) {
        case XML_ELEMENT_CONTENT_ONCE:
	    break;
        case XML_ELEMENT_CONTENT_OPT:
	    xmlBufferWriteChar(buf, "?");
	    break;
        case XML_ELEMENT_CONTENT_MULT:
	    xmlBufferWriteChar(buf, "*");
	    break;
        case XML_ELEMENT_CONTENT_PLUS:
	    xmlBufferWriteChar(buf, "+");
	    break;
    }
}

/**
 * xmlSprintfElementContent:
 * @buf:  an output buffer
 * @content:  An element table
1228
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1229
 *
1230 1231 1232 1233 1234
 * Deprecated, unsafe, use xmlSnprintfElementContent
 */
void
xmlSprintfElementContent(char *buf ATTRIBUTE_UNUSED,
	                 xmlElementContentPtr content ATTRIBUTE_UNUSED,
1235
			 int englob ATTRIBUTE_UNUSED) {
1236
}
1237
#endif /* LIBXML_OUTPUT_ENABLED */
1238 1239 1240 1241 1242 1243

/**
 * xmlSnprintfElementContent:
 * @buf:  an output buffer
 * @size:  the buffer size
 * @content:  An element table
1244
 * @englob: 1 if one must print the englobing parenthesis, 0 otherwise
1245
 *
1246 1247 1248 1249
 * This will dump the content of the element content definition
 * Intended just for the debug routine
 */
void
1250
xmlSnprintfElementContent(char *buf, int size, xmlElementContentPtr content, int englob) {
1251 1252
    int len;

1253
    if (content == NULL) return;
1254 1255 1256 1257 1258 1259
    len = strlen(buf);
    if (size - len < 50) {
	if ((size - len > 4) && (buf[len - 1] != '.'))
	    strcat(buf, " ...");
	return;
    }
1260
    if (englob) strcat(buf, "(");
1261 1262 1263 1264 1265
    switch (content->type) {
        case XML_ELEMENT_CONTENT_PCDATA:
            strcat(buf, "#PCDATA");
	    break;
	case XML_ELEMENT_CONTENT_ELEMENT:
1266
	    if (content->prefix != NULL) {
1267
		if (size - len < xmlStrlen(content->prefix) + 10) {
1268 1269 1270 1271 1272 1273
		    strcat(buf, " ...");
		    return;
		}
		strcat(buf, (char *) content->prefix);
		strcat(buf, ":");
	    }
1274
	    if (size - len < xmlStrlen(content->name) + 10) {
1275 1276 1277
		strcat(buf, " ...");
		return;
	    }
1278 1279
	    if (content->name != NULL)
		strcat(buf, (char *) content->name);
1280 1281 1282 1283
	    break;
	case XML_ELEMENT_CONTENT_SEQ:
	    if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
	        (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1284
		xmlSnprintfElementContent(buf, size, content->c1, 1);
1285
	    else
1286 1287 1288 1289 1290 1291 1292
		xmlSnprintfElementContent(buf, size, content->c1, 0);
	    len = strlen(buf);
	    if (size - len < 50) {
		if ((size - len > 4) && (buf[len - 1] != '.'))
		    strcat(buf, " ...");
		return;
	    }
1293
            strcat(buf, " , ");
1294 1295 1296
	    if (((content->c2->type == XML_ELEMENT_CONTENT_OR) ||
		 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
		(content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1297
		xmlSnprintfElementContent(buf, size, content->c2, 1);
1298
	    else
1299
		xmlSnprintfElementContent(buf, size, content->c2, 0);
1300 1301 1302 1303
	    break;
	case XML_ELEMENT_CONTENT_OR:
	    if ((content->c1->type == XML_ELEMENT_CONTENT_OR) ||
	        (content->c1->type == XML_ELEMENT_CONTENT_SEQ))
1304
		xmlSnprintfElementContent(buf, size, content->c1, 1);
1305
	    else
1306 1307 1308 1309 1310 1311 1312
		xmlSnprintfElementContent(buf, size, content->c1, 0);
	    len = strlen(buf);
	    if (size - len < 50) {
		if ((size - len > 4) && (buf[len - 1] != '.'))
		    strcat(buf, " ...");
		return;
	    }
1313
            strcat(buf, " | ");
1314 1315 1316
	    if (((content->c2->type == XML_ELEMENT_CONTENT_SEQ) ||
		 (content->c2->ocur != XML_ELEMENT_CONTENT_ONCE)) &&
		(content->c2->type != XML_ELEMENT_CONTENT_ELEMENT))
1317
		xmlSnprintfElementContent(buf, size, content->c2, 1);
1318
	    else
1319
		xmlSnprintfElementContent(buf, size, content->c2, 0);
1320 1321
	    break;
    }
1322
    if (englob)
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
        strcat(buf, ")");
    switch (content->ocur) {
        case XML_ELEMENT_CONTENT_ONCE:
	    break;
        case XML_ELEMENT_CONTENT_OPT:
	    strcat(buf, "?");
	    break;
        case XML_ELEMENT_CONTENT_MULT:
	    strcat(buf, "*");
	    break;
        case XML_ELEMENT_CONTENT_PLUS:
	    strcat(buf, "+");
	    break;
    }
}

/****************************************************************
 *								*
 *	Registration of DTD declarations			*
 *								*
 ****************************************************************/

/**
 * xmlFreeElement:
 * @elem:  An element
 *
 * Deallocate the memory used by an element definition
 */
1351
static void
1352 1353 1354
xmlFreeElement(xmlElementPtr elem) {
    if (elem == NULL) return;
    xmlUnlinkNode((xmlNodePtr) elem);
1355
    xmlFreeDocElementContent(elem->doc, elem->content);
1356 1357 1358 1359
    if (elem->name != NULL)
	xmlFree((xmlChar *) elem->name);
    if (elem->prefix != NULL)
	xmlFree((xmlChar *) elem->prefix);
1360 1361 1362 1363
#ifdef LIBXML_REGEXP_ENABLED
    if (elem->contModel != NULL)
	xmlRegFreeRegexp(elem->contModel);
#endif
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    xmlFree(elem);
}


/**
 * xmlAddElementDecl:
 * @ctxt:  the validation context
 * @dtd:  pointer to the DTD
 * @name:  the entity name
 * @type:  the element type
 * @content:  the element content tree or NULL
 *
 * Register a new element declaration
 *
1378
 * Returns NULL if not, otherwise the entity
1379 1380
 */
xmlElementPtr
1381
xmlAddElementDecl(xmlValidCtxtPtr ctxt,
1382
                  xmlDtdPtr dtd, const xmlChar *name,
1383 1384 1385 1386
                  xmlElementTypeVal type,
		  xmlElementContentPtr content) {
    xmlElementPtr ret;
    xmlElementTablePtr table;
1387
    xmlAttributePtr oldAttributes = NULL;
1388 1389 1390 1391 1392 1393 1394 1395
    xmlChar *ns, *uqname;

    if (dtd == NULL) {
	return(NULL);
    }
    if (name == NULL) {
	return(NULL);
    }
1396

1397 1398 1399
    switch (type) {
        case XML_ELEMENT_TYPE_EMPTY:
	    if (content != NULL) {
1400
		xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1401 1402
		        "xmlAddElementDecl: content != NULL for EMPTY\n",
			NULL);
1403 1404 1405 1406 1407
		return(NULL);
	    }
	    break;
	case XML_ELEMENT_TYPE_ANY:
	    if (content != NULL) {
1408
		xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1409 1410
		        "xmlAddElementDecl: content != NULL for ANY\n",
			NULL);
1411 1412 1413 1414 1415
		return(NULL);
	    }
	    break;
	case XML_ELEMENT_TYPE_MIXED:
	    if (content == NULL) {
1416
		xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1417 1418
		        "xmlAddElementDecl: content == NULL for MIXED\n",
			NULL);
1419 1420 1421 1422 1423
		return(NULL);
	    }
	    break;
	case XML_ELEMENT_TYPE_ELEMENT:
	    if (content == NULL) {
1424
		xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1425 1426
		        "xmlAddElementDecl: content == NULL for ELEMENT\n",
			NULL);
1427 1428 1429 1430
		return(NULL);
	    }
	    break;
	default:
1431
	    xmlErrValid(ctxt, XML_ERR_INTERNAL_ERROR,
1432 1433
		    "Internal: ELEMENT decl corrupted invalid type\n",
		    NULL);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
	    return(NULL);
    }

    /*
     * check if name is a QName
     */
    uqname = xmlSplitQName2(name, &ns);
    if (uqname != NULL)
	name = uqname;

    /*
     * Create the Element table if needed.
     */
    table = (xmlElementTablePtr) dtd->elements;
    if (table == NULL) {