parser.c 428 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
 *            implemented on top of the SAX interfaces
 *
 * References:
 *   The XML specification:
 *     http://www.w3.org/TR/REC-xml
 *   Original 1.0 version:
 *     http://www.w3.org/TR/1998/REC-xml-19980210
 *   XML second edition working draft
 *     http://www.w3.org/TR/2000/WD-xml-2e-20000814
 *
 * Okay this is a big file, the parser core is around 7000 lines, then it
 * is followed by the progressive parser top routines, then the various
15
 * high level APIs to call the parser and a few miscellaneous functions.
16
17
18
19
 * A number of helper functions and deprecated ones have been moved to
 * parserInternals.c to reduce this file size.
 * As much as possible the functions are associated with their relative
 * production in the XML specification. A few productions defining the
Daniel Veillard's avatar
Daniel Veillard committed
20
 * different ranges of character are actually implanted either in
21
22
23
24
 * parserInternals.h or parserInternals.c
 * The DOM tree build is realized from the default SAX callbacks in
 * the module SAX.c.
 * The routines doing the validation checks are in valid.c and called either
25
 * from the SAX callbacks or as standalone functions using a preparsed
26
27
28
29
 * document.
 *
 * See Copyright for the status of this software.
 *
30
 * daniel@veillard.com
31
32
 */

33
34
35
36
37
/* To avoid EBCDIC trouble when parsing on zOS */
#if defined(__MVS__)
#pragma convert("ISO8859-1")
#endif

38
#define IN_LIBXML
Bjorn Reese's avatar
Bjorn Reese committed
39
40
#include "libxml.h"

Nick Wellnhofer's avatar
Nick Wellnhofer committed
41
#if defined(_WIN32) && !defined (__CYGWIN__)
42
43
44
45
46
47
#define XML_DIR_SEP '\\'
#else
#define XML_DIR_SEP '/'
#endif

#include <stdlib.h>
48
#include <limits.h>
49
#include <string.h>
50
#include <stdarg.h>
51
#include <stddef.h>
52
#include <libxml/xmlmemory.h>
53
54
#include <libxml/threads.h>
#include <libxml/globals.h>
55
56
57
58
59
60
61
62
63
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/valid.h>
#include <libxml/entities.h>
#include <libxml/xmlerror.h>
#include <libxml/encoding.h>
#include <libxml/xmlIO.h>
#include <libxml/uri.h>
64
65
66
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
67
68
69
70
#ifdef LIBXML_SCHEMAS_ENABLED
#include <libxml/xmlschemastypes.h>
#include <libxml/relaxng.h>
#endif
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

87
88
89
#include "buf.h"
#include "enc.h"

90
91
92
static void
xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info);

93
94
95
96
static xmlParserCtxtPtr
xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID,
	                  const xmlChar *base, xmlParserCtxtPtr pctx);

97
98
static void xmlHaltParser(xmlParserCtxtPtr ctxt);

99
100
101
102
103
104
static int
xmlParseElementStart(xmlParserCtxtPtr ctxt);

static void
xmlParseElementEnd(xmlParserCtxtPtr ctxt);

105
106
107
108
109
110
111
112
113
114
115
116
/************************************************************************
 *									*
 *	Arbitrary limits set in the parser. See XML_PARSE_HUGE		*
 *									*
 ************************************************************************/

#define XML_PARSER_BIG_ENTITY 1000
#define XML_PARSER_LOT_ENTITY 5000

/*
 * XML_PARSER_NON_LINEAR is the threshold where the ratio of parsed entity
 *    replacement over the size in byte of the input indicates that you have
Jared Yanovich's avatar
Jared Yanovich committed
117
 *    and exponential behaviour. A value of 10 correspond to at least 3 entity
118
119
120
121
122
123
124
125
126
127
128
129
130
131
 *    replacement per byte of input.
 */
#define XML_PARSER_NON_LINEAR 10

/*
 * xmlParserEntityCheck
 *
 * Function to check non-linear entity expansion behaviour
 * This is here to detect and stop exponential linear entity expansion
 * This is not a limitation of the parser but a safety
 * boundary feature. It can be disabled with the XML_PARSE_HUGE
 * parser option.
 */
static int
132
xmlParserEntityCheck(xmlParserCtxtPtr ctxt, size_t size,
133
                     xmlEntityPtr ent, size_t replacement)
134
{
135
    size_t consumed = 0;
136
137
138
139
140

    if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE))
        return (0);
    if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP)
        return (1);
Daniel Veillard's avatar
Daniel Veillard committed
141
142
143
144
145
146

    /*
     * This may look absurd but is needed to detect
     * entities problems
     */
    if ((ent != NULL) && (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
147
148
	(ent->content != NULL) && (ent->checked == 0) &&
	(ctxt->errNo != XML_ERR_ENTITY_LOOP)) {
149
	unsigned long oldnbent = ctxt->nbentities, diff;
Daniel Veillard's avatar
Daniel Veillard committed
150
151
152
153
	xmlChar *rep;

	ent->checked = 1;

154
        ++ctxt->depth;
Daniel Veillard's avatar
Daniel Veillard committed
155
156
	rep = xmlStringDecodeEntities(ctxt, ent->content,
				  XML_SUBSTITUTE_REF, 0, 0, 0);
157
        --ctxt->depth;
Nick Wellnhofer's avatar
Nick Wellnhofer committed
158
	if ((rep == NULL) || (ctxt->errNo == XML_ERR_ENTITY_LOOP)) {
159
160
	    ent->content[0] = 0;
	}
Daniel Veillard's avatar
Daniel Veillard committed
161

162
163
164
165
        diff = ctxt->nbentities - oldnbent + 1;
        if (diff > INT_MAX / 2)
            diff = INT_MAX / 2;
	ent->checked = diff * 2;
Daniel Veillard's avatar
Daniel Veillard committed
166
167
168
169
170
171
172
	if (rep != NULL) {
	    if (xmlStrchr(rep, '<'))
		ent->checked |= 1;
	    xmlFree(rep);
	    rep = NULL;
	}
    }
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    if (replacement != 0) {
	if (replacement < XML_MAX_TEXT_LENGTH)
	    return(0);

        /*
	 * If the volume of entity copy reaches 10 times the
	 * amount of parsed data and over the large text threshold
	 * then that's very likely to be an abuse.
	 */
        if (ctxt->input != NULL) {
	    consumed = ctxt->input->consumed +
	               (ctxt->input->cur - ctxt->input->base);
	}
        consumed += ctxt->sizeentities;

        if (replacement < XML_PARSER_NON_LINEAR * consumed)
	    return(0);
    } else if (size != 0) {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        /*
         * Do the check based on the replacement size of the entity
         */
        if (size < XML_PARSER_BIG_ENTITY)
	    return(0);

        /*
         * A limit on the amount of text data reasonably used
         */
        if (ctxt->input != NULL) {
            consumed = ctxt->input->consumed +
                (ctxt->input->cur - ctxt->input->base);
        }
        consumed += ctxt->sizeentities;

        if ((size < XML_PARSER_NON_LINEAR * consumed) &&
	    (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed))
            return (0);
    } else if (ent != NULL) {
        /*
         * use the number of parsed entities in the replacement
         */
213
        size = ent->checked / 2;
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

        /*
         * The amount of data parsed counting entities size only once
         */
        if (ctxt->input != NULL) {
            consumed = ctxt->input->consumed +
                (ctxt->input->cur - ctxt->input->base);
        }
        consumed += ctxt->sizeentities;

        /*
         * Check the density of entities for the amount of data
	 * knowing an entity reference will take at least 3 bytes
         */
        if (size * 3 < consumed * XML_PARSER_NON_LINEAR)
            return (0);
    } else {
        /*
Daniel Veillard's avatar
Daniel Veillard committed
232
         * strange we got no data for checking
233
         */
Daniel Veillard's avatar
Daniel Veillard committed
234
235
236
237
	if (((ctxt->lastError.code != XML_ERR_UNDECLARED_ENTITY) &&
	     (ctxt->lastError.code != XML_WAR_UNDECLARED_ENTITY)) ||
	    (ctxt->nbentities <= 10000))
	    return (0);
238
239
240
241
242
    }
    xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
    return (1);
}

243
/**
244
 * xmlParserMaxDepth:
245
 *
246
247
248
249
 * arbitrary depth limit for the XML documents that we allow to
 * process. This is not a limitation of the parser but a safety
 * boundary feature. It can be disabled with the XML_PARSE_HUGE
 * parser option.
250
 */
251
unsigned int xmlParserMaxDepth = 256;
252

253

254
255

#define SAX2 1
256
#define XML_PARSER_BIG_BUFFER_SIZE 300
257
#define XML_PARSER_BUFFER_SIZE 100
258
259
#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"

260
261
262
263
264
265
266
267
268
269
270
/**
 * XML_PARSER_CHUNK_SIZE
 *
 * When calling GROW that's the minimal amount of data
 * the parser expected to have received. It is not a hard
 * limit but an optimization when reading strings like Names
 * It is not strictly needed as long as inputs available characters
 * are followed by 0, which should be provided by the I/O level
 */
#define XML_PARSER_CHUNK_SIZE 100

271
272
273
274
/*
 * List of XML prefixed PI allowed by W3C specs
 */

275
static const char *xmlW3CPIs[] = {
276
    "xml-stylesheet",
277
    "xml-model",
278
279
280
    NULL
};

281

282
/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
283
284
static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
                                              const xmlChar **str);
285

286
static xmlParserErrors
287
288
xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
	              xmlSAXHandlerPtr sax,
289
		      void *user_data, int depth, const xmlChar *URL,
290
		      const xmlChar *ID, xmlNodePtr *list);
291

292
293
294
static int
xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options,
                          const char *encoding);
295
#ifdef LIBXML_LEGACY_ENABLED
296
297
298
static void
xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
                      xmlNodePtr lastNode);
299
#endif /* LIBXML_LEGACY_ENABLED */
300

301
static xmlParserErrors
302
303
xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
		      const xmlChar *string, void *user_data, xmlNodePtr *lst);
304

305
306
307
static int
xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity);

308
309
/************************************************************************
 *									*
Daniel Veillard's avatar
Daniel Veillard committed
310
 *		Some factorized error routines				*
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 *									*
 ************************************************************************/

/**
 * xmlErrAttributeDup:
 * @ctxt:  an XML parser context
 * @prefix:  the attribute prefix
 * @localname:  the attribute localname
 *
 * Handle a redefinition of attribute error
 */
static void
xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
                   const xmlChar * localname)
{
326
327
328
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
329
330
    if (ctxt != NULL)
	ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
331

332
    if (prefix == NULL)
333
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
334
                        XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0,
335
336
                        (const char *) localname, NULL, NULL, 0, 0,
                        "Attribute %s redefined\n", localname);
337
    else
338
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
339
                        XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0,
340
341
342
                        (const char *) prefix, (const char *) localname,
                        NULL, 0, 0, "Attribute %s:%s redefined\n", prefix,
                        localname);
343
344
345
346
347
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
348
349
350
351
352
353
354
355
356
357
358
}

/**
 * xmlFatalErr:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @extra:  extra information string
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
static void
359
xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
360
361
362
{
    const char *errmsg;

363
364
365
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
366
367
    switch (error) {
        case XML_ERR_INVALID_HEX_CHARREF:
368
            errmsg = "CharRef: invalid hexadecimal value";
369
            break;
370
        case XML_ERR_INVALID_DEC_CHARREF:
371
            errmsg = "CharRef: invalid decimal value";
372
            break;
373
        case XML_ERR_INVALID_CHARREF:
374
            errmsg = "CharRef: invalid value";
375
            break;
376
        case XML_ERR_INTERNAL_ERROR:
377
378
            errmsg = "internal error";
            break;
379
        case XML_ERR_PEREF_AT_EOF:
380
            errmsg = "PEReference at end of document";
381
            break;
382
        case XML_ERR_PEREF_IN_PROLOG:
383
            errmsg = "PEReference in prolog";
384
            break;
385
        case XML_ERR_PEREF_IN_EPILOG:
386
            errmsg = "PEReference in epilog";
387
            break;
388
        case XML_ERR_PEREF_NO_NAME:
389
            errmsg = "PEReference: no name";
390
            break;
391
        case XML_ERR_PEREF_SEMICOL_MISSING:
392
            errmsg = "PEReference: expecting ';'";
393
            break;
394
        case XML_ERR_ENTITY_LOOP:
395
            errmsg = "Detected an entity reference loop";
396
            break;
397
        case XML_ERR_ENTITY_NOT_STARTED:
398
            errmsg = "EntityValue: \" or ' expected";
399
            break;
400
        case XML_ERR_ENTITY_PE_INTERNAL:
401
            errmsg = "PEReferences forbidden in internal subset";
402
            break;
403
        case XML_ERR_ENTITY_NOT_FINISHED:
404
            errmsg = "EntityValue: \" or ' expected";
405
            break;
406
        case XML_ERR_ATTRIBUTE_NOT_STARTED:
407
            errmsg = "AttValue: \" or ' expected";
408
            break;
409
        case XML_ERR_LT_IN_ATTRIBUTE:
410
            errmsg = "Unescaped '<' not allowed in attributes values";
411
            break;
412
        case XML_ERR_LITERAL_NOT_STARTED:
413
            errmsg = "SystemLiteral \" or ' expected";
414
            break;
415
        case XML_ERR_LITERAL_NOT_FINISHED:
416
            errmsg = "Unfinished System or Public ID \" or ' expected";
417
            break;
418
        case XML_ERR_MISPLACED_CDATA_END:
419
            errmsg = "Sequence ']]>' not allowed in content";
420
            break;
421
        case XML_ERR_URI_REQUIRED:
422
            errmsg = "SYSTEM or PUBLIC, the URI is missing";
423
            break;
424
        case XML_ERR_PUBID_REQUIRED:
425
            errmsg = "PUBLIC, the Public Identifier is missing";
426
            break;
427
        case XML_ERR_HYPHEN_IN_COMMENT:
428
            errmsg = "Comment must not contain '--' (double-hyphen)";
429
            break;
430
        case XML_ERR_PI_NOT_STARTED:
431
            errmsg = "xmlParsePI : no target name";
432
            break;
433
        case XML_ERR_RESERVED_XML_NAME:
434
            errmsg = "Invalid PI name";
435
            break;
436
        case XML_ERR_NOTATION_NOT_STARTED:
437
            errmsg = "NOTATION: Name expected here";
438
            break;
439
        case XML_ERR_NOTATION_NOT_FINISHED:
440
            errmsg = "'>' required to close NOTATION declaration";
441
            break;
442
        case XML_ERR_VALUE_REQUIRED:
443
            errmsg = "Entity value required";
444
            break;
445
        case XML_ERR_URI_FRAGMENT:
446
447
            errmsg = "Fragment not allowed";
            break;
448
        case XML_ERR_ATTLIST_NOT_STARTED:
449
            errmsg = "'(' required to start ATTLIST enumeration";
450
            break;
451
        case XML_ERR_NMTOKEN_REQUIRED:
452
            errmsg = "NmToken expected in ATTLIST enumeration";
453
            break;
454
        case XML_ERR_ATTLIST_NOT_FINISHED:
455
            errmsg = "')' required to finish ATTLIST enumeration";
456
            break;
457
        case XML_ERR_MIXED_NOT_STARTED:
458
            errmsg = "MixedContentDecl : '|' or ')*' expected";
459
            break;
460
        case XML_ERR_PCDATA_REQUIRED:
461
            errmsg = "MixedContentDecl : '#PCDATA' expected";
462
            break;
463
        case XML_ERR_ELEMCONTENT_NOT_STARTED:
464
            errmsg = "ContentDecl : Name or '(' expected";
465
            break;
466
        case XML_ERR_ELEMCONTENT_NOT_FINISHED:
467
            errmsg = "ContentDecl : ',' '|' or ')' expected";
468
            break;
469
        case XML_ERR_PEREF_IN_INT_SUBSET:
470
            errmsg =
471
                "PEReference: forbidden within markup decl in internal subset";
472
            break;
473
        case XML_ERR_GT_REQUIRED:
474
            errmsg = "expected '>'";
475
            break;
476
        case XML_ERR_CONDSEC_INVALID:
477
            errmsg = "XML conditional section '[' expected";
478
            break;
479
        case XML_ERR_EXT_SUBSET_NOT_FINISHED:
480
            errmsg = "Content error in the external subset";
481
482
483
            break;
        case XML_ERR_CONDSEC_INVALID_KEYWORD:
            errmsg =
484
                "conditional section INCLUDE or IGNORE keyword expected";
485
            break;
486
        case XML_ERR_CONDSEC_NOT_FINISHED:
487
            errmsg = "XML conditional section not closed";
488
            break;
489
        case XML_ERR_XMLDECL_NOT_STARTED:
490
            errmsg = "Text declaration '<?xml' required";
491
            break;
492
        case XML_ERR_XMLDECL_NOT_FINISHED:
493
            errmsg = "parsing XML declaration: '?>' expected";
494
            break;
495
        case XML_ERR_EXT_ENTITY_STANDALONE:
496
            errmsg = "external parsed entities cannot be standalone";
497
            break;
498
        case XML_ERR_ENTITYREF_SEMICOL_MISSING:
499
            errmsg = "EntityRef: expecting ';'";
500
            break;
501
        case XML_ERR_DOCTYPE_NOT_FINISHED:
502
            errmsg = "DOCTYPE improperly terminated";
503
            break;
504
        case XML_ERR_LTSLASH_REQUIRED:
505
            errmsg = "EndTag: '</' not found";
506
            break;
507
        case XML_ERR_EQUAL_REQUIRED:
508
            errmsg = "expected '='";
509
            break;
510
        case XML_ERR_STRING_NOT_CLOSED:
511
            errmsg = "String not closed expecting \" or '";
512
            break;
513
        case XML_ERR_STRING_NOT_STARTED:
514
            errmsg = "String not started expecting ' or \"";
515
            break;
516
        case XML_ERR_ENCODING_NAME:
517
            errmsg = "Invalid XML encoding name";
518
            break;
519
        case XML_ERR_STANDALONE_VALUE:
520
            errmsg = "standalone accepts only 'yes' or 'no'";
521
            break;
522
        case XML_ERR_DOCUMENT_EMPTY:
523
            errmsg = "Document is empty";
524
            break;
525
        case XML_ERR_DOCUMENT_END:
526
            errmsg = "Extra content at the end of the document";
527
            break;
528
        case XML_ERR_NOT_WELL_BALANCED:
529
            errmsg = "chunk is not well balanced";
530
            break;
531
        case XML_ERR_EXTRA_CONTENT:
532
            errmsg = "extra content at the end of well balanced chunk";
533
            break;
534
        case XML_ERR_VERSION_MISSING:
535
            errmsg = "Malformed declaration expecting version";
536
            break;
537
        case XML_ERR_NAME_TOO_LONG:
538
            errmsg = "Name too long use XML_PARSE_HUGE option";
539
            break;
540
#if 0
541
        case:
542
            errmsg = "";
543
            break;
544
#endif
545
        default:
546
            errmsg = "Unregistered error message";
547
    }
548
549
    if (ctxt != NULL)
	ctxt->errNo = error;
550
551
552
553
554
555
556
557
558
    if (info == NULL) {
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
                        XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, "%s\n",
                        errmsg);
    } else {
        __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
                        XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, "%s: %s\n",
                        errmsg, info);
    }
559
560
561
562
563
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
564
565
}

566
567
568
569
570
571
572
573
/**
 * xmlFatalErrMsg:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
574
static void LIBXML_ATTR_FORMAT(3,0)
575
576
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
               const char *msg)
577
{
578
579
580
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
581
582
    if (ctxt != NULL)
	ctxt->errNo = error;
583
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
584
                    XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg);
585
586
587
588
589
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
590
591
}

592
593
594
595
596
597
598
599
600
601
/**
 * xmlWarningMsg:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @str1:  extra data
 * @str2:  extra data
 *
 * Handle a warning.
 */
602
static void LIBXML_ATTR_FORMAT(3,0)
603
604
605
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
              const char *msg, const xmlChar *str1, const xmlChar *str2)
{
Daniel Veillard's avatar
Daniel Veillard committed
606
    xmlStructuredErrorFunc schannel = NULL;
607

608
609
610
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
611
612
    if ((ctxt != NULL) && (ctxt->sax != NULL) &&
        (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard's avatar
Daniel Veillard committed
613
        schannel = ctxt->sax->serror;
614
615
    if (ctxt != NULL) {
        __xmlRaiseError(schannel,
616
617
                    (ctxt->sax) ? ctxt->sax->warning : NULL,
                    ctxt->userData,
618
619
620
621
                    ctxt, NULL, XML_FROM_PARSER, error,
                    XML_ERR_WARNING, NULL, 0,
		    (const char *) str1, (const char *) str2, NULL, 0, 0,
		    msg, (const char *) str1, (const char *) str2);
622
623
624
625
626
627
628
    } else {
        __xmlRaiseError(schannel, NULL, NULL,
                    ctxt, NULL, XML_FROM_PARSER, error,
                    XML_ERR_WARNING, NULL, 0,
		    (const char *) str1, (const char *) str2, NULL, 0, 0,
		    msg, (const char *) str1, (const char *) str2);
    }
629
630
631
632
633
634
635
636
637
}

/**
 * xmlValidityError:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @str1:  extra data
 *
638
 * Handle a validity error.
639
 */
640
static void LIBXML_ATTR_FORMAT(3,0)
641
xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
642
              const char *msg, const xmlChar *str1, const xmlChar *str2)
643
{
Daniel Veillard's avatar
Daniel Veillard committed
644
    xmlStructuredErrorFunc schannel = NULL;
645
646
647
648

    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
649
650
651
652
653
    if (ctxt != NULL) {
	ctxt->errNo = error;
	if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
	    schannel = ctxt->sax->serror;
    }
654
655
    if (ctxt != NULL) {
        __xmlRaiseError(schannel,
656
                    ctxt->vctxt.error, ctxt->vctxt.userData,
657
658
                    ctxt, NULL, XML_FROM_DTD, error,
                    XML_ERR_ERROR, NULL, 0, (const char *) str1,
659
660
		    (const char *) str2, NULL, 0, 0,
		    msg, (const char *) str1, (const char *) str2);
661
	ctxt->valid = 0;
662
663
664
665
666
667
    } else {
        __xmlRaiseError(schannel, NULL, NULL,
                    ctxt, NULL, XML_FROM_DTD, error,
                    XML_ERR_ERROR, NULL, 0, (const char *) str1,
		    (const char *) str2, NULL, 0, 0,
		    msg, (const char *) str1, (const char *) str2);
668
    }
669
670
}

671
672
673
674
675
676
677
678
679
/**
 * xmlFatalErrMsgInt:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @val:  an integer value
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
680
static void LIBXML_ATTR_FORMAT(3,0)
681
xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
682
                  const char *msg, int val)
683
{
684
685
686
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
687
688
    if (ctxt != NULL)
	ctxt->errNo = error;
689
    __xmlRaiseError(NULL, NULL, NULL,
690
691
                    ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
                    NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
692
693
694
695
696
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
697
698
}

699
700
701
702
703
704
705
706
707
708
709
/**
 * xmlFatalErrMsgStrIntStr:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @str1:  an string info
 * @val:  an integer value
 * @str2:  an string info
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
710
static void LIBXML_ATTR_FORMAT(3,0)
711
xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillard's avatar
Daniel Veillard committed
712
                  const char *msg, const xmlChar *str1, int val,
713
714
		  const xmlChar *str2)
{
715
716
717
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
718
719
    if (ctxt != NULL)
	ctxt->errNo = error;
720
    __xmlRaiseError(NULL, NULL, NULL,
721
722
723
                    ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
                    NULL, 0, (const char *) str1, (const char *) str2,
		    NULL, val, 0, msg, str1, val, str2);
724
725
726
727
728
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
729
730
}

731
732
733
734
735
736
737
738
739
/**
 * xmlFatalErrMsgStr:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @val:  a string value
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
740
static void LIBXML_ATTR_FORMAT(3,0)
741
xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
742
                  const char *msg, const xmlChar * val)
743
{
744
745
746
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
747
748
    if (ctxt != NULL)
	ctxt->errNo = error;
749
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
750
751
752
                    XML_FROM_PARSER, error, XML_ERR_FATAL,
                    NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
                    val);
753
754
755
756
757
    if (ctxt != NULL) {
	ctxt->wellFormed = 0;
	if (ctxt->recovery == 0)
	    ctxt->disableSAX = 1;
    }
758
759
}

760
761
762
763
764
765
766
767
768
/**
 * xmlErrMsgStr:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the error message
 * @val:  a string value
 *
 * Handle a non fatal parser error
 */
769
static void LIBXML_ATTR_FORMAT(3,0)
770
771
772
xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
                  const char *msg, const xmlChar * val)
{
773
774
775
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
776
777
    if (ctxt != NULL)
	ctxt->errNo = error;
778
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
779
780
781
782
783
                    XML_FROM_PARSER, error, XML_ERR_ERROR,
                    NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
                    val);
}

784
785
786
787
788
789
790
791
792
793
/**
 * xmlNsErr:
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the message
 * @info1:  extra information string
 * @info2:  extra information string
 *
 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
 */
794
static void LIBXML_ATTR_FORMAT(3,0)
795
796
xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
         const char *msg,
797
798
         const xmlChar * info1, const xmlChar * info2,
         const xmlChar * info3)
799
{
800
801
802
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
803
804
    if (ctxt != NULL)
	ctxt->errNo = error;
805
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
806
807
808
                    XML_ERR_ERROR, NULL, 0, (const char *) info1,
                    (const char *) info2, (const char *) info3, 0, 0, msg,
                    info1, info2, info3);
809
810
    if (ctxt != NULL)
	ctxt->nsWellFormed = 0;
811
812
}

813
814
815
816
817
818
819
820
/**
 * xmlNsWarn
 * @ctxt:  an XML parser context
 * @error:  the error number
 * @msg:  the message
 * @info1:  extra information string
 * @info2:  extra information string
 *
Daniel Veillard's avatar
Daniel Veillard committed
821
 * Handle a namespace warning error
822
 */
823
static void LIBXML_ATTR_FORMAT(3,0)
824
825
826
827
828
829
830
831
832
833
834
835
836
837
xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error,
         const char *msg,
         const xmlChar * info1, const xmlChar * info2,
         const xmlChar * info3)
{
    if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
        (ctxt->instate == XML_PARSER_EOF))
	return;
    __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
                    XML_ERR_WARNING, NULL, 0, (const char *) info1,
                    (const char *) info2, (const char *) info3, 0, 0, msg,
                    info1, info2, info3);
}

838
839
/************************************************************************
 *									*
Daniel Veillard's avatar
Daniel Veillard committed
840
 *		Library wide options					*
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
 *									*
 ************************************************************************/

/**
  * xmlHasFeature:
  * @feature: the feature to be examined
  *
  * Examines if the library has been compiled with a given feature.
  *
  * Returns a non-zero value if the feature exist, otherwise zero.
  * Returns zero (0) if the feature does not exist or an unknown
  * unknown feature is requested, non-zero otherwise.
  */
int
xmlHasFeature(xmlFeature feature)
{
    switch (feature) {
858
	case XML_WITH_THREAD:
859
860
861
862
863
#ifdef LIBXML_THREAD_ENABLED
	    return(1);
#else
	    return(0);
#endif
864
        case XML_WITH_TREE:
865
866
867
868
869
#ifdef LIBXML_TREE_ENABLED
            return(1);
#else
            return(0);
#endif
870
        case XML_WITH_OUTPUT:
871
872
873
874
875
#ifdef LIBXML_OUTPUT_ENABLED
            return(1);
#else
            return(0);
#endif
876
        case XML_WITH_PUSH:
877
878
879
880
881
#ifdef LIBXML_PUSH_ENABLED
            return(1);
#else
            return(0);
#endif
882
        case XML_WITH_READER:
883
884
885
886
887
#ifdef LIBXML_READER_ENABLED
            return(1);
#else
            return(0);
#endif
888
        case XML_WITH_PATTERN:
889
890
891
892
893
#ifdef LIBXML_PATTERN_ENABLED
            return(1);
#else
            return(0);
#endif
894
        case XML_WITH_WRITER:
895
896
897
898
899
#ifdef LIBXML_WRITER_ENABLED
            return(1);
#else
            return(0);
#endif
900
        case XML_WITH_SAX1:
901
902
903
904
905
#ifdef LIBXML_SAX1_ENABLED
            return(1);
#else
            return(0);
#endif
906
        case XML_WITH_FTP:
907
908
909
910
911
#ifdef LIBXML_FTP_ENABLED
            return(1);
#else
            return(0);
#endif
912
        case XML_WITH_HTTP:
913
914
915
916
917
#ifdef LIBXML_HTTP_ENABLED
            return(1);
#else
            return(0);
#endif
918
        case XML_WITH_VALID:
919
920
921
922
923
#ifdef LIBXML_VALID_ENABLED
            return(1);
#else
            return(0);
#endif
924
        case XML_WITH_HTML:
925
926
927
928
929
#ifdef LIBXML_HTML_ENABLED
            return(1);
#else
            return(0);
#endif
930
        case XML_WITH_LEGACY:
931
932
933
934
935
#ifdef LIBXML_LEGACY_ENABLED
            return(1);
#else
            return(0);
#endif
936
        case XML_WITH_C14N:
937
938
939
940
941
#ifdef LIBXML_C14N_ENABLED
            return(1);
#else
            return(0);
#endif
942
        case XML_WITH_CATALOG:
943
944
945
946
947
#ifdef LIBXML_CATALOG_ENABLED
            return(1);
#else
            return(0);
#endif
948
        case XML_WITH_XPATH:
949
950
951
952
953
#ifdef LIBXML_XPATH_ENABLED
            return(1);
#else
            return(0);
#endif
954
        case XML_WITH_XPTR:
955
956
957
958
959
#ifdef LIBXML_XPTR_ENABLED
            return(1);
#else
            return(0);
#endif
960
        case XML_WITH_XINCLUDE:
961
962
963
964
965
#ifdef LIBXML_XINCLUDE_ENABLED
            return(1);
#else
            return(0);
#endif
966
        case XML_WITH_ICONV:
967
968
969
970
971
#ifdef LIBXML_ICONV_ENABLED
            return(1);
#else
            return(0);
#endif
972
        case XML_WITH_ISO8859X:
973
974
975
976
977
#ifdef LIBXML_ISO8859X_ENABLED
            return(1);
#else
            return(0);
#endif
978
        case XML_WITH_UNICODE:
979
980
981
982
983
#ifdef LIBXML_UNICODE_ENABLED
            return(1);
#else
            return(0);
#endif
984
        case XML_WITH_REGEXP:
985
986
987
988
989
#ifdef LIBXML_REGEXP_ENABLED
            return(1);
#else
            return(0);
#endif
990
        case XML_WITH_AUTOMATA:
991
992
993
994
995
#ifdef LIBXML_AUTOMATA_ENABLED
            return(1);
#else
            return(0);
#endif
996
        case XML_WITH_EXPR: