blob: 67fbd639aa8ef721b24ed6a09344efbc471d75c5 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002 * parserInternals.c : Internal routines (and obsolete ones) needed for the
3 * XML and HTML parsers.
Owen Taylor3473f882001-02-23 17:55:21 +00004 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
12
Daniel Veillard3c5ed912002-01-08 10:36:16 +000013#if defined(WIN32) && !defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +000014#define XML_DIR_SEP '\\'
15#else
Owen Taylor3473f882001-02-23 17:55:21 +000016#define XML_DIR_SEP '/'
17#endif
18
Owen Taylor3473f882001-02-23 17:55:21 +000019#include <string.h>
20#ifdef HAVE_CTYPE_H
21#include <ctype.h>
22#endif
23#ifdef HAVE_STDLIB_H
24#include <stdlib.h>
25#endif
26#ifdef HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
29#ifdef HAVE_FCNTL_H
30#include <fcntl.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35#ifdef HAVE_ZLIB_H
36#include <zlib.h>
37#endif
38
39#include <libxml/xmlmemory.h>
40#include <libxml/tree.h>
41#include <libxml/parser.h>
42#include <libxml/parserInternals.h>
43#include <libxml/valid.h>
44#include <libxml/entities.h>
45#include <libxml/xmlerror.h>
46#include <libxml/encoding.h>
47#include <libxml/valid.h>
48#include <libxml/xmlIO.h>
49#include <libxml/uri.h>
Daniel Veillard2fdbd322003-08-18 12:15:38 +000050#include <libxml/dict.h>
Daniel Veillard16698282001-09-14 10:29:27 +000051#include <libxml/SAX.h>
Daniel Veillard5d90b6c2001-08-22 14:29:45 +000052#ifdef LIBXML_CATALOG_ENABLED
53#include <libxml/catalog.h>
54#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000055#include <libxml/globals.h>
William M. Brack68aca052003-10-11 15:22:13 +000056#include <libxml/chvalid.h>
Owen Taylor3473f882001-02-23 17:55:21 +000057
Daniel Veillarda53c6882001-07-25 17:18:57 +000058/*
59 * Various global defaults for parsing
60 */
Owen Taylor3473f882001-02-23 17:55:21 +000061
Daniel Veillard5e2dace2001-07-18 19:30:27 +000062/**
Owen Taylor3473f882001-02-23 17:55:21 +000063 * xmlCheckVersion:
64 * @version: the include version number
65 *
66 * check the compiled lib version against the include one.
67 * This can warn or immediately kill the application
68 */
69void
70xmlCheckVersion(int version) {
71 int myversion = (int) LIBXML_VERSION;
72
Daniel Veillard6f350292001-10-14 09:56:15 +000073 xmlInitParser();
Daniel Veillard4de4d3b2001-05-07 20:50:47 +000074
Owen Taylor3473f882001-02-23 17:55:21 +000075 if ((myversion / 10000) != (version / 10000)) {
76 xmlGenericError(xmlGenericErrorContext,
77 "Fatal: program compiled against libxml %d using libxml %d\n",
78 (version / 10000), (myversion / 10000));
Daniel Veillardc69e0b12001-11-20 08:35:07 +000079 fprintf(stderr,
80 "Fatal: program compiled against libxml %d using libxml %d\n",
81 (version / 10000), (myversion / 10000));
Owen Taylor3473f882001-02-23 17:55:21 +000082 }
83 if ((myversion / 100) < (version / 100)) {
84 xmlGenericError(xmlGenericErrorContext,
85 "Warning: program compiled against libxml %d using older %d\n",
86 (version / 100), (myversion / 100));
87 }
88}
89
Daniel Veillardce9457f2003-10-05 21:33:18 +000090
91/************************************************************************
92 * *
93 * Some factorized error routines *
94 * *
95 ************************************************************************/
96
97
98/**
99 * xmlErrMemory:
100 * @ctxt: an XML parser context
101 * @extra: extra informations
102 *
103 * Handle a redefinition of attribute error
104 */
105void
106xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
107{
Daniel Veillard157fee02003-10-31 10:36:03 +0000108 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
109 (ctxt->instate == XML_PARSER_EOF))
110 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000111 if (ctxt != NULL) {
112 ctxt->errNo = XML_ERR_NO_MEMORY;
113 ctxt->instate = XML_PARSER_EOF;
114 ctxt->disableSAX = 1;
115 }
116 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000117 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000118 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
119 NULL, NULL, 0, 0,
120 "Memory allocation failed : %s\n", extra);
121 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000122 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000123 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
124 NULL, NULL, 0, 0, "Memory allocation failed\n");
125}
126
127/**
Daniel Veillarda840b692003-10-19 13:35:37 +0000128 * __xmlErrEncoding:
Daniel Veillardce9457f2003-10-05 21:33:18 +0000129 * @ctxt: an XML parser context
130 * @error: the error number
131 * @msg: the error message
132 * @str1: an string info
133 * @str2: an string info
134 *
135 * Handle an encoding error
136 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000137void
138__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors error,
139 const char *msg, const xmlChar * str1, const xmlChar * str2)
Daniel Veillardce9457f2003-10-05 21:33:18 +0000140{
Daniel Veillard157fee02003-10-31 10:36:03 +0000141 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
142 (ctxt->instate == XML_PARSER_EOF))
143 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000144 if (ctxt != NULL)
145 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000146 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000147 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
148 NULL, 0, (const char *) str1, (const char *) str2,
149 NULL, 0, 0, msg, str1, str2);
150 if (ctxt != NULL) {
151 ctxt->wellFormed = 0;
152 if (ctxt->recovery == 0)
153 ctxt->disableSAX = 1;
154 }
155}
156
157/**
158 * xmlErrInternal:
159 * @ctxt: an XML parser context
160 * @msg: the error message
161 * @str: error informations
162 *
163 * Handle an internal error
164 */
165static void
166xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
167{
Daniel Veillard157fee02003-10-31 10:36:03 +0000168 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
169 (ctxt->instate == XML_PARSER_EOF))
170 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000171 if (ctxt != NULL)
172 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000173 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000174 ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
175 XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
176 0, 0, msg, str);
177 if (ctxt != NULL) {
178 ctxt->wellFormed = 0;
179 if (ctxt->recovery == 0)
180 ctxt->disableSAX = 1;
181 }
182}
183
184/**
185 * xmlErrEncodingInt:
186 * @ctxt: an XML parser context
187 * @error: the error number
188 * @msg: the error message
189 * @val: an integer value
190 *
191 * n encoding error
192 */
193static void
194xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
195 const char *msg, int val)
196{
Daniel Veillard157fee02003-10-31 10:36:03 +0000197 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
198 (ctxt->instate == XML_PARSER_EOF))
199 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000200 if (ctxt != NULL)
201 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000202 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000203 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
204 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
205 if (ctxt != NULL) {
206 ctxt->wellFormed = 0;
207 if (ctxt->recovery == 0)
208 ctxt->disableSAX = 1;
209 }
210}
211
Owen Taylor3473f882001-02-23 17:55:21 +0000212/**
213 * xmlIsLetter:
214 * @c: an unicode character (int)
215 *
216 * Check whether the character is allowed by the production
217 * [84] Letter ::= BaseChar | Ideographic
218 *
219 * Returns 0 if not, non-zero otherwise
220 */
221int
222xmlIsLetter(int c) {
223 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
224}
225
Owen Taylor3473f882001-02-23 17:55:21 +0000226/************************************************************************
227 * *
228 * Input handling functions for progressive parsing *
229 * *
230 ************************************************************************/
231
232/* #define DEBUG_INPUT */
233/* #define DEBUG_STACK */
234/* #define DEBUG_PUSH */
235
236
237/* we need to keep enough input to show errors in context */
238#define LINE_LEN 80
239
240#ifdef DEBUG_INPUT
241#define CHECK_BUFFER(in) check_buffer(in)
242
Daniel Veillard01c13b52002-12-10 15:19:08 +0000243static
Owen Taylor3473f882001-02-23 17:55:21 +0000244void check_buffer(xmlParserInputPtr in) {
245 if (in->base != in->buf->buffer->content) {
246 xmlGenericError(xmlGenericErrorContext,
247 "xmlParserInput: base mismatch problem\n");
248 }
249 if (in->cur < in->base) {
250 xmlGenericError(xmlGenericErrorContext,
251 "xmlParserInput: cur < base problem\n");
252 }
253 if (in->cur > in->base + in->buf->buffer->use) {
254 xmlGenericError(xmlGenericErrorContext,
255 "xmlParserInput: cur > base + use problem\n");
256 }
257 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
258 (int) in, (int) in->buf->buffer->content, in->cur - in->base,
259 in->buf->buffer->use, in->buf->buffer->size);
260}
261
262#else
263#define CHECK_BUFFER(in)
264#endif
265
266
267/**
268 * xmlParserInputRead:
269 * @in: an XML parser input
270 * @len: an indicative size for the lookahead
271 *
272 * This function refresh the input for the parser. It doesn't try to
273 * preserve pointers to the input buffer, and discard already read data
274 *
275 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
276 * end of this entity
277 */
278int
279xmlParserInputRead(xmlParserInputPtr in, int len) {
280 int ret;
281 int used;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000282 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000283
284#ifdef DEBUG_INPUT
285 xmlGenericError(xmlGenericErrorContext, "Read\n");
286#endif
287 if (in->buf == NULL) return(-1);
288 if (in->base == NULL) return(-1);
289 if (in->cur == NULL) return(-1);
290 if (in->buf->buffer == NULL) return(-1);
291 if (in->buf->readcallback == NULL) return(-1);
292
293 CHECK_BUFFER(in);
294
295 used = in->cur - in->buf->buffer->content;
296 ret = xmlBufferShrink(in->buf->buffer, used);
297 if (ret > 0) {
298 in->cur -= ret;
299 in->consumed += ret;
300 }
301 ret = xmlParserInputBufferRead(in->buf, len);
302 if (in->base != in->buf->buffer->content) {
303 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000304 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000305 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000306 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000307 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000308 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000309 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000310 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000311
312 CHECK_BUFFER(in);
313
314 return(ret);
315}
316
317/**
318 * xmlParserInputGrow:
319 * @in: an XML parser input
320 * @len: an indicative size for the lookahead
321 *
322 * This function increase the input for the parser. It tries to
323 * preserve pointers to the input buffer, and keep already read data
324 *
325 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
326 * end of this entity
327 */
328int
329xmlParserInputGrow(xmlParserInputPtr in, int len) {
330 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000331 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000332
333#ifdef DEBUG_INPUT
334 xmlGenericError(xmlGenericErrorContext, "Grow\n");
335#endif
336 if (in->buf == NULL) return(-1);
337 if (in->base == NULL) return(-1);
338 if (in->cur == NULL) return(-1);
339 if (in->buf->buffer == NULL) return(-1);
340
341 CHECK_BUFFER(in);
342
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000343 indx = in->cur - in->base;
344 if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000345
346 CHECK_BUFFER(in);
347
348 return(0);
349 }
350 if (in->buf->readcallback != NULL)
351 ret = xmlParserInputBufferGrow(in->buf, len);
352 else
353 return(0);
354
355 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +0000356 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +0000357 * block, but we use it really as an integer to do some
358 * pointer arithmetic. Insure will raise it as a bug but in
359 * that specific case, that's not !
360 */
361 if (in->base != in->buf->buffer->content) {
362 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000363 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000364 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000365 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000366 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000367 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000368 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000369 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000370
371 CHECK_BUFFER(in);
372
373 return(ret);
374}
375
376/**
377 * xmlParserInputShrink:
378 * @in: an XML parser input
379 *
380 * This function removes used input for the parser.
381 */
382void
383xmlParserInputShrink(xmlParserInputPtr in) {
384 int used;
385 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000386 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000387
388#ifdef DEBUG_INPUT
389 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
390#endif
391 if (in->buf == NULL) return;
392 if (in->base == NULL) return;
393 if (in->cur == NULL) return;
394 if (in->buf->buffer == NULL) return;
395
396 CHECK_BUFFER(in);
397
398 used = in->cur - in->buf->buffer->content;
399 /*
400 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000401 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +0000402 */
Owen Taylor3473f882001-02-23 17:55:21 +0000403 if (used > INPUT_CHUNK) {
404 ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
405 if (ret > 0) {
406 in->cur -= ret;
407 in->consumed += ret;
408 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000409 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000410 }
411
412 CHECK_BUFFER(in);
413
414 if (in->buf->buffer->use > INPUT_CHUNK) {
415 return;
416 }
417 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
418 if (in->base != in->buf->buffer->content) {
419 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000420 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000421 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000422 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000423 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000424 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000425 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000426 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000427
428 CHECK_BUFFER(in);
429}
430
431/************************************************************************
432 * *
433 * UTF8 character input and related functions *
434 * *
435 ************************************************************************/
436
437/**
438 * xmlNextChar:
439 * @ctxt: the XML parser context
440 *
441 * Skip to the next char input char.
442 */
443
444void
Daniel Veillard77a90a72003-03-22 00:04:05 +0000445xmlNextChar(xmlParserCtxtPtr ctxt)
446{
Owen Taylor3473f882001-02-23 17:55:21 +0000447 if (ctxt->instate == XML_PARSER_EOF)
Daniel Veillard77a90a72003-03-22 00:04:05 +0000448 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000449
Daniel Veillardfdc91562002-07-01 21:52:03 +0000450 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000451 if ((*ctxt->input->cur == 0) &&
452 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
453 (ctxt->instate != XML_PARSER_COMMENT)) {
454 /*
455 * If we are at the end of the current entity and
456 * the context allows it, we pop consumed entities
457 * automatically.
458 * the auto closing should be blocked in other cases
459 */
460 xmlPopInput(ctxt);
461 } else {
462 const unsigned char *cur;
463 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +0000464
Daniel Veillard77a90a72003-03-22 00:04:05 +0000465 /*
466 * 2.11 End-of-Line Handling
467 * the literal two-character sequence "#xD#xA" or a standalone
468 * literal #xD, an XML processor must pass to the application
469 * the single character #xA.
470 */
471 if (*(ctxt->input->cur) == '\n') {
472 ctxt->input->line++;
473 ctxt->input->col = 1;
474 } else
475 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +0000476
Daniel Veillard77a90a72003-03-22 00:04:05 +0000477 /*
478 * We are supposed to handle UTF8, check it's valid
479 * From rfc2044: encoding of the Unicode values on UTF-8:
480 *
481 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
482 * 0000 0000-0000 007F 0xxxxxxx
483 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
484 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
485 *
486 * Check for the 0x110000 limit too
487 */
488 cur = ctxt->input->cur;
489
490 c = *cur;
491 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000492 if (c == 0xC0)
493 goto encoding_error;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000494 if (cur[1] == 0)
495 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
496 if ((cur[1] & 0xc0) != 0x80)
497 goto encoding_error;
498 if ((c & 0xe0) == 0xe0) {
499 unsigned int val;
500
501 if (cur[2] == 0)
502 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
503 if ((cur[2] & 0xc0) != 0x80)
504 goto encoding_error;
505 if ((c & 0xf0) == 0xf0) {
506 if (cur[3] == 0)
507 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
508 if (((c & 0xf8) != 0xf0) ||
509 ((cur[3] & 0xc0) != 0x80))
510 goto encoding_error;
511 /* 4-byte code */
512 ctxt->input->cur += 4;
513 val = (cur[0] & 0x7) << 18;
514 val |= (cur[1] & 0x3f) << 12;
515 val |= (cur[2] & 0x3f) << 6;
516 val |= cur[3] & 0x3f;
517 } else {
518 /* 3-byte code */
519 ctxt->input->cur += 3;
520 val = (cur[0] & 0xf) << 12;
521 val |= (cur[1] & 0x3f) << 6;
522 val |= cur[2] & 0x3f;
523 }
524 if (((val > 0xd7ff) && (val < 0xe000)) ||
525 ((val > 0xfffd) && (val < 0x10000)) ||
526 (val >= 0x110000)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000527 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
528 "Char 0x%X out of allowed range\n",
529 val);
Daniel Veillard77a90a72003-03-22 00:04:05 +0000530 }
531 } else
532 /* 2-byte code */
533 ctxt->input->cur += 2;
534 } else
535 /* 1-byte code */
536 ctxt->input->cur++;
537
538 ctxt->nbChars++;
539 if (*ctxt->input->cur == 0)
540 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
541 }
Owen Taylor3473f882001-02-23 17:55:21 +0000542 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000543 /*
544 * Assume it's a fixed length encoding (1) with
545 * a compatible encoding for the ASCII set, since
546 * XML constructs only use < 128 chars
547 */
548
549 if (*(ctxt->input->cur) == '\n') {
550 ctxt->input->line++;
551 ctxt->input->col = 1;
552 } else
553 ctxt->input->col++;
554 ctxt->input->cur++;
555 ctxt->nbChars++;
556 if (*ctxt->input->cur == 0)
557 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +0000558 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000559 if ((*ctxt->input->cur == '%') && (!ctxt->html))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000560 xmlParserHandlePEReference(ctxt);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000561 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000562 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000563 xmlPopInput(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +0000564 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000565encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +0000566 /*
567 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000568 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000569 * declaration header. Report the error and switch the encoding
570 * to ISO-Latin-1 (if you don't like this policy, just declare the
571 * encoding !)
572 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000573 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000574 "Input is not proper UTF-8, indicate encoding !\n",
575 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000576 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000577 ctxt->sax->error(ctxt->userData,
Daniel Veillard77a90a72003-03-22 00:04:05 +0000578 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
579 ctxt->input->cur[0], ctxt->input->cur[1],
580 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000581 }
Daniel Veillard77a90a72003-03-22 00:04:05 +0000582 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000583 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000584 return;
585}
586
587/**
588 * xmlCurrentChar:
589 * @ctxt: the XML parser context
590 * @len: pointer to the length of the char read
591 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000592 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000593 * bytes in the input buffer. Implement the end of line normalization:
594 * 2.11 End-of-Line Handling
595 * Wherever an external parsed entity or the literal entity value
596 * of an internal parsed entity contains either the literal two-character
597 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
598 * must pass to the application the single character #xA.
599 * This behavior can conveniently be produced by normalizing all
600 * line breaks to #xA on input, before parsing.)
601 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000602 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000603 */
604
605int
606xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
607 if (ctxt->instate == XML_PARSER_EOF)
608 return(0);
609
Daniel Veillard561b7f82002-03-20 21:55:57 +0000610 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
611 *len = 1;
612 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000613 }
614 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
615 /*
616 * We are supposed to handle UTF8, check it's valid
617 * From rfc2044: encoding of the Unicode values on UTF-8:
618 *
619 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
620 * 0000 0000-0000 007F 0xxxxxxx
621 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
622 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
623 *
624 * Check for the 0x110000 limit too
625 */
626 const unsigned char *cur = ctxt->input->cur;
627 unsigned char c;
628 unsigned int val;
629
630 c = *cur;
631 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000632 if (c == 0xC0)
633 goto encoding_error;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000634 if (cur[1] == 0)
635 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
636 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000637 goto encoding_error;
638 if ((c & 0xe0) == 0xe0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000639
640 if (cur[2] == 0)
641 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
642 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000643 goto encoding_error;
644 if ((c & 0xf0) == 0xf0) {
645 if (cur[3] == 0)
646 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000647 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +0000648 ((cur[3] & 0xc0) != 0x80))
649 goto encoding_error;
650 /* 4-byte code */
651 *len = 4;
652 val = (cur[0] & 0x7) << 18;
653 val |= (cur[1] & 0x3f) << 12;
654 val |= (cur[2] & 0x3f) << 6;
655 val |= cur[3] & 0x3f;
656 } else {
657 /* 3-byte code */
658 *len = 3;
659 val = (cur[0] & 0xf) << 12;
660 val |= (cur[1] & 0x3f) << 6;
661 val |= cur[2] & 0x3f;
662 }
663 } else {
664 /* 2-byte code */
665 *len = 2;
666 val = (cur[0] & 0x1f) << 6;
667 val |= cur[1] & 0x3f;
668 }
669 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000670 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
671 "Char 0x%X out of allowed range\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000672 }
673 return(val);
674 } else {
675 /* 1-byte code */
676 *len = 1;
677 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000678 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000679 ctxt->nbChars++;
680 ctxt->input->cur++;
681 }
682 return(0xA);
683 }
684 return((int) *ctxt->input->cur);
685 }
686 }
687 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000688 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000689 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000690 * XML constructs only use < 128 chars
691 */
692 *len = 1;
693 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000694 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000695 ctxt->nbChars++;
696 ctxt->input->cur++;
697 }
698 return(0xA);
699 }
700 return((int) *ctxt->input->cur);
701encoding_error:
702 /*
Daniel Veillardd2ff0392002-11-22 12:28:38 +0000703 * An encoding problem may arise from a truncated input buffer
704 * splitting a character in the middle. In that case do not raise
705 * an error but return 0 to endicate an end of stream problem
706 */
707 if (ctxt->input->end - ctxt->input->cur < 4) {
708 *len = 0;
709 return(0);
710 }
711
712 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000713 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000714 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000715 * declaration header. Report the error and switch the encoding
716 * to ISO-Latin-1 (if you don't like this policy, just declare the
717 * encoding !)
718 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000719 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000720 "Input is not proper UTF-8, indicate encoding !\n",
721 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000722 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000723 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Daniel Veillard561b7f82002-03-20 21:55:57 +0000724 ctxt->input->cur[0], ctxt->input->cur[1],
725 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000726 }
Owen Taylor3473f882001-02-23 17:55:21 +0000727 ctxt->charset = XML_CHAR_ENCODING_8859_1;
728 *len = 1;
729 return((int) *ctxt->input->cur);
730}
731
732/**
733 * xmlStringCurrentChar:
734 * @ctxt: the XML parser context
735 * @cur: pointer to the beginning of the char
736 * @len: pointer to the length of the char read
737 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000738 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000739 * bytes in the input buffer.
740 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000741 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000742 */
743
744int
Daniel Veillardd8224e02002-01-13 15:43:22 +0000745xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
746{
Daniel Veillard61d80a22001-04-27 17:13:01 +0000747 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +0000748 /*
749 * We are supposed to handle UTF8, check it's valid
750 * From rfc2044: encoding of the Unicode values on UTF-8:
751 *
752 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
753 * 0000 0000-0000 007F 0xxxxxxx
754 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
755 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
756 *
757 * Check for the 0x110000 limit too
758 */
759 unsigned char c;
760 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +0000761
Daniel Veillardd8224e02002-01-13 15:43:22 +0000762 c = *cur;
763 if (c & 0x80) {
764 if ((cur[1] & 0xc0) != 0x80)
765 goto encoding_error;
766 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000767
Daniel Veillardd8224e02002-01-13 15:43:22 +0000768 if ((cur[2] & 0xc0) != 0x80)
769 goto encoding_error;
770 if ((c & 0xf0) == 0xf0) {
771 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
772 goto encoding_error;
773 /* 4-byte code */
774 *len = 4;
775 val = (cur[0] & 0x7) << 18;
776 val |= (cur[1] & 0x3f) << 12;
777 val |= (cur[2] & 0x3f) << 6;
778 val |= cur[3] & 0x3f;
779 } else {
780 /* 3-byte code */
781 *len = 3;
782 val = (cur[0] & 0xf) << 12;
783 val |= (cur[1] & 0x3f) << 6;
784 val |= cur[2] & 0x3f;
785 }
786 } else {
787 /* 2-byte code */
788 *len = 2;
789 val = (cur[0] & 0x1f) << 6;
790 val |= cur[1] & 0x3f;
791 }
792 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000793 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
794 "Char 0x%X out of allowed range\n", val);
Daniel Veillardd8224e02002-01-13 15:43:22 +0000795 }
796 return (val);
797 } else {
798 /* 1-byte code */
799 *len = 1;
800 return ((int) *cur);
801 }
Owen Taylor3473f882001-02-23 17:55:21 +0000802 }
803 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000804 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000805 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000806 * XML constructs only use < 128 chars
807 */
808 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000809 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000810encoding_error:
Daniel Veillardd8224e02002-01-13 15:43:22 +0000811
Owen Taylor3473f882001-02-23 17:55:21 +0000812 /*
813 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000814 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000815 * declaration header. Report the error and switch the encoding
816 * to ISO-Latin-1 (if you don't like this policy, just declare the
817 * encoding !)
818 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000819 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000820 "Input is not proper UTF-8, indicate encoding !\n",
821 NULL, NULL);
822 if ((ctxt != NULL) && (ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
823 ctxt->sax->error(ctxt->userData,
824 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
825 ctxt->input->cur[0], ctxt->input->cur[1],
826 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000827 }
Owen Taylor3473f882001-02-23 17:55:21 +0000828 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000829 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000830}
831
832/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000833 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000834 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +0000835 * @val: the char value
836 *
837 * append the char value in the array
838 *
839 * Returns the number of xmlChar written
840 */
Owen Taylor3473f882001-02-23 17:55:21 +0000841int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000842xmlCopyCharMultiByte(xmlChar *out, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +0000843 /*
844 * We are supposed to handle UTF8, check it's valid
845 * From rfc2044: encoding of the Unicode values on UTF-8:
846 *
847 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
848 * 0000 0000-0000 007F 0xxxxxxx
849 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
850 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
851 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000852 if (val >= 0x80) {
853 xmlChar *savedout = out;
854 int bits;
855 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
856 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
857 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
858 else {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000859 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000860 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Daniel Veillardce9457f2003-10-05 21:33:18 +0000861 val);
Owen Taylor3473f882001-02-23 17:55:21 +0000862 return(0);
863 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000864 for ( ; bits >= 0; bits-= 6)
865 *out++= ((val >> bits) & 0x3F) | 0x80 ;
866 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +0000867 }
868 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000869 return 1;
870}
871
872/**
873 * xmlCopyChar:
874 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000875 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000876 * @val: the char value
877 *
878 * append the char value in the array
879 *
880 * Returns the number of xmlChar written
881 */
882
883int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000884xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000885 /* the len parameter is ignored */
886 if (val >= 0x80) {
887 return(xmlCopyCharMultiByte (out, val));
888 }
889 *out = (xmlChar) val;
890 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000891}
892
893/************************************************************************
894 * *
895 * Commodity functions to switch encodings *
896 * *
897 ************************************************************************/
898
899/**
900 * xmlSwitchEncoding:
901 * @ctxt: the parser context
902 * @enc: the encoding value (number)
903 *
904 * change the input functions when discovering the character encoding
905 * of a given entity.
906 *
907 * Returns 0 in case of success, -1 otherwise
908 */
909int
910xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
911{
912 xmlCharEncodingHandlerPtr handler;
913
914 switch (enc) {
915 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000916 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000917 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000918 break;
919 case XML_CHAR_ENCODING_NONE:
920 /* let's assume it's UTF-8 without the XML decl */
921 ctxt->charset = XML_CHAR_ENCODING_UTF8;
922 return(0);
923 case XML_CHAR_ENCODING_UTF8:
924 /* default encoding, no conversion should be needed */
925 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +0000926
927 /*
928 * Errata on XML-1.0 June 20 2001
929 * Specific handling of the Byte Order Mark for
930 * UTF-8
931 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +0000932 if ((ctxt->input != NULL) &&
933 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +0000934 (ctxt->input->cur[1] == 0xBB) &&
935 (ctxt->input->cur[2] == 0xBF)) {
936 ctxt->input->cur += 3;
937 }
Owen Taylor3473f882001-02-23 17:55:21 +0000938 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +0000939 case XML_CHAR_ENCODING_UTF16LE:
940 case XML_CHAR_ENCODING_UTF16BE:
941 /*The raw input characters are encoded
942 *in UTF-16. As we expect this function
943 *to be called after xmlCharEncInFunc, we expect
944 *ctxt->input->cur to contain UTF-8 encoded characters.
945 *So the raw UTF16 Byte Order Mark
946 *has also been converted into
947 *an UTF-8 BOM. Let's skip that BOM.
948 */
949 if ((ctxt->input != NULL) &&
950 (ctxt->input->cur[0] == 0xEF) &&
951 (ctxt->input->cur[1] == 0xBB) &&
952 (ctxt->input->cur[2] == 0xBF)) {
953 ctxt->input->cur += 3;
954 }
955 break ;
Owen Taylor3473f882001-02-23 17:55:21 +0000956 default:
957 break;
958 }
959 handler = xmlGetCharEncodingHandler(enc);
960 if (handler == NULL) {
961 /*
962 * Default handlers.
963 */
964 switch (enc) {
965 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000966 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000967 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000968 break;
969 case XML_CHAR_ENCODING_NONE:
970 /* let's assume it's UTF-8 without the XML decl */
971 ctxt->charset = XML_CHAR_ENCODING_UTF8;
972 return(0);
973 case XML_CHAR_ENCODING_UTF8:
974 case XML_CHAR_ENCODING_ASCII:
975 /* default encoding, no conversion should be needed */
976 ctxt->charset = XML_CHAR_ENCODING_UTF8;
977 return(0);
978 case XML_CHAR_ENCODING_UTF16LE:
979 break;
980 case XML_CHAR_ENCODING_UTF16BE:
981 break;
982 case XML_CHAR_ENCODING_UCS4LE:
Daniel Veillarda840b692003-10-19 13:35:37 +0000983 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000984 "encoding not supported %s\n",
985 BAD_CAST "USC4 little endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000986 break;
987 case XML_CHAR_ENCODING_UCS4BE:
Daniel Veillarda840b692003-10-19 13:35:37 +0000988 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000989 "encoding not supported %s\n",
990 BAD_CAST "USC4 big endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000991 break;
992 case XML_CHAR_ENCODING_EBCDIC:
Daniel Veillarda840b692003-10-19 13:35:37 +0000993 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000994 "encoding not supported %s\n",
995 BAD_CAST "EBCDIC", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000996 break;
997 case XML_CHAR_ENCODING_UCS4_2143:
Daniel Veillarda840b692003-10-19 13:35:37 +0000998 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000999 "encoding not supported %s\n",
1000 BAD_CAST "UCS4 2143", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001001 break;
1002 case XML_CHAR_ENCODING_UCS4_3412:
Daniel Veillarda840b692003-10-19 13:35:37 +00001003 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001004 "encoding not supported %s\n",
1005 BAD_CAST "UCS4 3412", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001006 break;
1007 case XML_CHAR_ENCODING_UCS2:
Daniel Veillarda840b692003-10-19 13:35:37 +00001008 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001009 "encoding not supported %s\n",
1010 BAD_CAST "UCS2", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001011 break;
1012 case XML_CHAR_ENCODING_8859_1:
1013 case XML_CHAR_ENCODING_8859_2:
1014 case XML_CHAR_ENCODING_8859_3:
1015 case XML_CHAR_ENCODING_8859_4:
1016 case XML_CHAR_ENCODING_8859_5:
1017 case XML_CHAR_ENCODING_8859_6:
1018 case XML_CHAR_ENCODING_8859_7:
1019 case XML_CHAR_ENCODING_8859_8:
1020 case XML_CHAR_ENCODING_8859_9:
1021 /*
1022 * We used to keep the internal content in the
1023 * document encoding however this turns being unmaintainable
1024 * So xmlGetCharEncodingHandler() will return non-null
1025 * values for this now.
1026 */
1027 if ((ctxt->inputNr == 1) &&
1028 (ctxt->encoding == NULL) &&
1029 (ctxt->input->encoding != NULL)) {
1030 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1031 }
1032 ctxt->charset = enc;
1033 return(0);
1034 case XML_CHAR_ENCODING_2022_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001035 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001036 "encoding not supported %s\n",
1037 BAD_CAST "ISO-2022-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001038 break;
1039 case XML_CHAR_ENCODING_SHIFT_JIS:
Daniel Veillarda840b692003-10-19 13:35:37 +00001040 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001041 "encoding not supported %s\n",
1042 BAD_CAST "Shift_JIS", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001043 break;
1044 case XML_CHAR_ENCODING_EUC_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001045 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001046 "encoding not supported %s\n",
1047 BAD_CAST "EUC-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001048 break;
1049 }
1050 }
1051 if (handler == NULL)
1052 return(-1);
1053 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1054 return(xmlSwitchToEncoding(ctxt, handler));
1055}
1056
1057/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001058 * xmlSwitchInputEncoding:
1059 * @ctxt: the parser context
1060 * @input: the input stream
1061 * @handler: the encoding handler
1062 *
1063 * change the input functions when discovering the character encoding
1064 * of a given entity.
1065 *
1066 * Returns 0 in case of success, -1 otherwise
1067 */
1068int
1069xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1070 xmlCharEncodingHandlerPtr handler)
1071{
1072 int nbchars;
1073
1074 if (handler == NULL)
1075 return (-1);
1076 if (input == NULL)
1077 return (-1);
1078 if (input->buf != NULL) {
1079 if (input->buf->encoder != NULL) {
1080 /*
1081 * Check in case the auto encoding detetection triggered
1082 * in already.
1083 */
1084 if (input->buf->encoder == handler)
1085 return (0);
1086
1087 /*
1088 * "UTF-16" can be used for both LE and BE
1089 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1090 BAD_CAST "UTF-16", 6)) &&
1091 (!xmlStrncmp(BAD_CAST handler->name,
1092 BAD_CAST "UTF-16", 6))) {
1093 return(0);
1094 }
1095 */
1096
1097 /*
1098 * Note: this is a bit dangerous, but that's what it
1099 * takes to use nearly compatible signature for different
1100 * encodings.
1101 */
1102 xmlCharEncCloseFunc(input->buf->encoder);
1103 input->buf->encoder = handler;
1104 return (0);
1105 }
1106 input->buf->encoder = handler;
1107
1108 /*
1109 * Is there already some content down the pipe to convert ?
1110 */
1111 if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) {
1112 int processed;
1113
1114 /*
1115 * Specific handling of the Byte Order Mark for
1116 * UTF-16
1117 */
1118 if ((handler->name != NULL) &&
1119 (!strcmp(handler->name, "UTF-16LE")) &&
1120 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1121 input->cur += 2;
1122 }
1123 if ((handler->name != NULL) &&
1124 (!strcmp(handler->name, "UTF-16BE")) &&
1125 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1126 input->cur += 2;
1127 }
1128 /*
1129 * Errata on XML-1.0 June 20 2001
1130 * Specific handling of the Byte Order Mark for
1131 * UTF-8
1132 */
1133 if ((handler->name != NULL) &&
1134 (!strcmp(handler->name, "UTF-8")) &&
1135 (input->cur[0] == 0xEF) &&
1136 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1137 input->cur += 3;
1138 }
1139
1140 /*
1141 * Shrink the current input buffer.
1142 * Move it as the raw buffer and create a new input buffer
1143 */
1144 processed = input->cur - input->base;
1145 xmlBufferShrink(input->buf->buffer, processed);
1146 input->buf->raw = input->buf->buffer;
1147 input->buf->buffer = xmlBufferCreate();
1148
1149 if (ctxt->html) {
1150 /*
1151 * convert as much as possible of the buffer
1152 */
1153 nbchars = xmlCharEncInFunc(input->buf->encoder,
1154 input->buf->buffer,
1155 input->buf->raw);
1156 } else {
1157 /*
1158 * convert just enough to get
1159 * '<?xml version="1.0" encoding="xxx"?>'
1160 * parsed with the autodetected encoding
1161 * into the parser reading buffer.
1162 */
1163 nbchars = xmlCharEncFirstLine(input->buf->encoder,
1164 input->buf->buffer,
1165 input->buf->raw);
1166 }
1167 if (nbchars < 0) {
1168 xmlErrInternal(ctxt,
1169 "switching encoding: encoder error\n",
1170 NULL);
1171 return (-1);
1172 }
1173 input->base = input->cur = input->buf->buffer->content;
1174 input->end = &input->base[input->buf->buffer->use];
1175
1176 }
1177 return (0);
1178 } else {
1179 if ((input->length == 0) || (input->buf == NULL)) {
1180 /*
1181 * When parsing a static memory array one must know the
1182 * size to be able to convert the buffer.
1183 */
1184 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1185 return (-1);
1186 } else {
1187 int processed;
1188
1189 /*
1190 * Shrink the current input buffer.
1191 * Move it as the raw buffer and create a new input buffer
1192 */
1193 processed = input->cur - input->base;
1194
1195 input->buf->raw = xmlBufferCreate();
1196 xmlBufferAdd(input->buf->raw, input->cur,
1197 input->length - processed);
1198 input->buf->buffer = xmlBufferCreate();
1199
1200 /*
1201 * convert as much as possible of the raw input
1202 * to the parser reading buffer.
1203 */
1204 nbchars = xmlCharEncInFunc(input->buf->encoder,
1205 input->buf->buffer,
1206 input->buf->raw);
1207 if (nbchars < 0) {
1208 xmlErrInternal(ctxt,
1209 "switching encoding: encoder error\n",
1210 NULL);
1211 return (-1);
1212 }
1213
1214 /*
1215 * Conversion succeeded, get rid of the old buffer
1216 */
1217 if ((input->free != NULL) && (input->base != NULL))
1218 input->free((xmlChar *) input->base);
1219 input->base = input->cur = input->buf->buffer->content;
1220 input->end = &input->base[input->buf->buffer->use];
1221 }
1222 }
1223 return (0);
1224}
1225
1226/**
Owen Taylor3473f882001-02-23 17:55:21 +00001227 * xmlSwitchToEncoding:
1228 * @ctxt: the parser context
1229 * @handler: the encoding handler
1230 *
1231 * change the input functions when discovering the character encoding
1232 * of a given entity.
1233 *
1234 * Returns 0 in case of success, -1 otherwise
1235 */
1236int
1237xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1238{
Owen Taylor3473f882001-02-23 17:55:21 +00001239 if (handler != NULL) {
1240 if (ctxt->input != NULL) {
Daniel Veillarda840b692003-10-19 13:35:37 +00001241 xmlSwitchInputEncoding(ctxt, ctxt->input, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00001242 } else {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001243 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1244 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001245 return(-1);
1246 }
1247 /*
1248 * The parsing is now done in UTF8 natively
1249 */
1250 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1251 } else
1252 return(-1);
1253 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001254}
1255
1256/************************************************************************
1257 * *
1258 * Commodity functions to handle entities processing *
1259 * *
1260 ************************************************************************/
1261
1262/**
1263 * xmlFreeInputStream:
1264 * @input: an xmlParserInputPtr
1265 *
1266 * Free up an input stream.
1267 */
1268void
1269xmlFreeInputStream(xmlParserInputPtr input) {
1270 if (input == NULL) return;
1271
1272 if (input->filename != NULL) xmlFree((char *) input->filename);
1273 if (input->directory != NULL) xmlFree((char *) input->directory);
1274 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1275 if (input->version != NULL) xmlFree((char *) input->version);
1276 if ((input->free != NULL) && (input->base != NULL))
1277 input->free((xmlChar *) input->base);
1278 if (input->buf != NULL)
1279 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001280 xmlFree(input);
1281}
1282
1283/**
1284 * xmlNewInputStream:
1285 * @ctxt: an XML parser context
1286 *
1287 * Create a new input stream structure
1288 * Returns the new input stream or NULL
1289 */
1290xmlParserInputPtr
1291xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1292 xmlParserInputPtr input;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001293 static int id = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001294
1295 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1296 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001297 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001298 return(NULL);
1299 }
1300 memset(input, 0, sizeof(xmlParserInput));
1301 input->line = 1;
1302 input->col = 1;
1303 input->standalone = -1;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001304 /*
1305 * we don't care about thread reentrancy unicity for a single
1306 * parser context (and hence thread) is sufficient.
1307 */
1308 input->id = id++;
Owen Taylor3473f882001-02-23 17:55:21 +00001309 return(input);
1310}
1311
1312/**
1313 * xmlNewIOInputStream:
1314 * @ctxt: an XML parser context
1315 * @input: an I/O Input
1316 * @enc: the charset encoding if known
1317 *
1318 * Create a new input stream structure encapsulating the @input into
1319 * a stream suitable for the parser.
1320 *
1321 * Returns the new input stream or NULL
1322 */
1323xmlParserInputPtr
1324xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1325 xmlCharEncoding enc) {
1326 xmlParserInputPtr inputStream;
1327
1328 if (xmlParserDebugEntities)
1329 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1330 inputStream = xmlNewInputStream(ctxt);
1331 if (inputStream == NULL) {
1332 return(NULL);
1333 }
1334 inputStream->filename = NULL;
1335 inputStream->buf = input;
1336 inputStream->base = inputStream->buf->buffer->content;
1337 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001338 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001339 if (enc != XML_CHAR_ENCODING_NONE) {
1340 xmlSwitchEncoding(ctxt, enc);
1341 }
1342
1343 return(inputStream);
1344}
1345
1346/**
1347 * xmlNewEntityInputStream:
1348 * @ctxt: an XML parser context
1349 * @entity: an Entity pointer
1350 *
1351 * Create a new input stream based on an xmlEntityPtr
1352 *
1353 * Returns the new input stream or NULL
1354 */
1355xmlParserInputPtr
1356xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1357 xmlParserInputPtr input;
1358
1359 if (entity == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001360 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1361 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001362 return(NULL);
1363 }
1364 if (xmlParserDebugEntities)
1365 xmlGenericError(xmlGenericErrorContext,
1366 "new input from entity: %s\n", entity->name);
1367 if (entity->content == NULL) {
1368 switch (entity->etype) {
1369 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001370 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1371 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001372 break;
1373 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1374 case XML_EXTERNAL_PARAMETER_ENTITY:
1375 return(xmlLoadExternalEntity((char *) entity->URI,
1376 (char *) entity->ExternalID, ctxt));
1377 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001378 xmlErrInternal(ctxt,
1379 "Internal entity %s without content !\n",
1380 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001381 break;
1382 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001383 xmlErrInternal(ctxt,
1384 "Internal parameter entity %s without content !\n",
1385 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001386 break;
1387 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001388 xmlErrInternal(ctxt,
1389 "Predefined entity %s without content !\n",
1390 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001391 break;
1392 }
1393 return(NULL);
1394 }
1395 input = xmlNewInputStream(ctxt);
1396 if (input == NULL) {
1397 return(NULL);
1398 }
1399 input->filename = (char *) entity->URI;
1400 input->base = entity->content;
1401 input->cur = entity->content;
1402 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001403 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001404 return(input);
1405}
1406
1407/**
1408 * xmlNewStringInputStream:
1409 * @ctxt: an XML parser context
1410 * @buffer: an memory buffer
1411 *
1412 * Create a new input stream based on a memory buffer.
1413 * Returns the new input stream
1414 */
1415xmlParserInputPtr
1416xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1417 xmlParserInputPtr input;
1418
1419 if (buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001420 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1421 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001422 return(NULL);
1423 }
1424 if (xmlParserDebugEntities)
1425 xmlGenericError(xmlGenericErrorContext,
1426 "new fixed input: %.30s\n", buffer);
1427 input = xmlNewInputStream(ctxt);
1428 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001429 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001430 return(NULL);
1431 }
1432 input->base = buffer;
1433 input->cur = buffer;
1434 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001435 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001436 return(input);
1437}
1438
1439/**
1440 * xmlNewInputFromFile:
1441 * @ctxt: an XML parser context
1442 * @filename: the filename to use as entity
1443 *
Daniel Veillarda840b692003-10-19 13:35:37 +00001444 * Create a new input stream based on a file or an URL.
Owen Taylor3473f882001-02-23 17:55:21 +00001445 *
1446 * Returns the new input stream or NULL in case of error
1447 */
1448xmlParserInputPtr
1449xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1450 xmlParserInputBufferPtr buf;
1451 xmlParserInputPtr inputStream;
1452 char *directory = NULL;
1453 xmlChar *URI = NULL;
1454
1455 if (xmlParserDebugEntities)
1456 xmlGenericError(xmlGenericErrorContext,
1457 "new input from file: %s\n", filename);
1458 if (ctxt == NULL) return(NULL);
1459 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001460 if (buf == NULL) {
1461 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1462 (const char *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001463 return(NULL);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001464 }
Owen Taylor3473f882001-02-23 17:55:21 +00001465
Owen Taylor3473f882001-02-23 17:55:21 +00001466 inputStream = xmlNewInputStream(ctxt);
1467 if (inputStream == NULL) {
1468 if (directory != NULL) xmlFree((char *) directory);
1469 if (URI != NULL) xmlFree((char *) URI);
1470 return(NULL);
1471 }
Daniel Veillarda840b692003-10-19 13:35:37 +00001472 inputStream->buf = buf;
1473 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1474 if (inputStream == NULL)
1475 return(NULL);
1476
1477 if (inputStream->filename == NULL)
1478 URI = xmlStrdup((xmlChar *) filename);
1479 else
1480 URI = xmlStrdup((xmlChar *) inputStream->filename);
1481 directory = xmlParserGetDirectory((const char *) URI);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001482 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001483 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001484 inputStream->directory = directory;
Owen Taylor3473f882001-02-23 17:55:21 +00001485
1486 inputStream->base = inputStream->buf->buffer->content;
1487 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001488 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001489 if ((ctxt->directory == NULL) && (directory != NULL))
1490 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1491 return(inputStream);
1492}
1493
1494/************************************************************************
1495 * *
1496 * Commodity functions to handle parser contexts *
1497 * *
1498 ************************************************************************/
1499
1500/**
1501 * xmlInitParserCtxt:
1502 * @ctxt: an XML parser context
1503 *
1504 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001505 *
1506 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001507 */
1508
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001509int
Owen Taylor3473f882001-02-23 17:55:21 +00001510xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1511{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001512 if(ctxt==NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001513 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001514 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001515 }
1516
Owen Taylor3473f882001-02-23 17:55:21 +00001517 xmlDefaultSAXHandlerInit();
1518
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001519 ctxt->dict = xmlDictCreate();
1520 if (ctxt->dict == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001521 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001522 return(-1);
1523 }
William M. Brack8b2c7f12002-11-22 05:07:29 +00001524 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1525 if (ctxt->sax == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001526 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001527 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001528 }
1529 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001530 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001531
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001532 ctxt->maxatts = 0;
1533 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001534 /* Allocate the Input stack */
1535 ctxt->inputTab = (xmlParserInputPtr *)
1536 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1537 if (ctxt->inputTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001538 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001539 ctxt->inputNr = 0;
1540 ctxt->inputMax = 0;
1541 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001542 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001543 }
1544 ctxt->inputNr = 0;
1545 ctxt->inputMax = 5;
1546 ctxt->input = NULL;
1547
1548 ctxt->version = NULL;
1549 ctxt->encoding = NULL;
1550 ctxt->standalone = -1;
1551 ctxt->hasExternalSubset = 0;
1552 ctxt->hasPErefs = 0;
1553 ctxt->html = 0;
1554 ctxt->external = 0;
1555 ctxt->instate = XML_PARSER_START;
1556 ctxt->token = 0;
1557 ctxt->directory = NULL;
1558
1559 /* Allocate the Node stack */
1560 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1561 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001562 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001563 ctxt->nodeNr = 0;
1564 ctxt->nodeMax = 0;
1565 ctxt->node = NULL;
1566 ctxt->inputNr = 0;
1567 ctxt->inputMax = 0;
1568 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001569 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001570 }
1571 ctxt->nodeNr = 0;
1572 ctxt->nodeMax = 10;
1573 ctxt->node = NULL;
1574
1575 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001576 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00001577 if (ctxt->nameTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001578 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001579 ctxt->nodeNr = 0;
1580 ctxt->nodeMax = 0;
1581 ctxt->node = NULL;
1582 ctxt->inputNr = 0;
1583 ctxt->inputMax = 0;
1584 ctxt->input = NULL;
1585 ctxt->nameNr = 0;
1586 ctxt->nameMax = 0;
1587 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001588 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001589 }
1590 ctxt->nameNr = 0;
1591 ctxt->nameMax = 10;
1592 ctxt->name = NULL;
1593
1594 /* Allocate the space stack */
1595 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1596 if (ctxt->spaceTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001597 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001598 ctxt->nodeNr = 0;
1599 ctxt->nodeMax = 0;
1600 ctxt->node = NULL;
1601 ctxt->inputNr = 0;
1602 ctxt->inputMax = 0;
1603 ctxt->input = NULL;
1604 ctxt->nameNr = 0;
1605 ctxt->nameMax = 0;
1606 ctxt->name = NULL;
1607 ctxt->spaceNr = 0;
1608 ctxt->spaceMax = 0;
1609 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001610 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001611 }
1612 ctxt->spaceNr = 1;
1613 ctxt->spaceMax = 10;
1614 ctxt->spaceTab[0] = -1;
1615 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00001616 ctxt->userData = ctxt;
1617 ctxt->myDoc = NULL;
1618 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00001619 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001620 ctxt->valid = 1;
1621 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1622 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1623 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00001624 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00001625 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00001626 if (ctxt->keepBlanks == 0)
Daniel Veillard11476b42003-09-26 14:51:39 +00001627 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00001628
Owen Taylor3473f882001-02-23 17:55:21 +00001629 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001630 ctxt->vctxt.error = xmlParserValidityError;
1631 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00001632 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00001633 if (xmlGetWarningsDefaultValue == 0)
1634 ctxt->vctxt.warning = NULL;
1635 else
1636 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00001637 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001638 }
1639 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1640 ctxt->record_info = 0;
1641 ctxt->nbChars = 0;
1642 ctxt->checkIndex = 0;
1643 ctxt->inSubset = 0;
1644 ctxt->errNo = XML_ERR_OK;
1645 ctxt->depth = 0;
1646 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001647 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001648 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001649 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001650}
1651
1652/**
1653 * xmlFreeParserCtxt:
1654 * @ctxt: an XML parser context
1655 *
1656 * Free all the memory used by a parser context. However the parsed
1657 * document in ctxt->myDoc is not freed.
1658 */
1659
1660void
1661xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1662{
1663 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00001664
1665 if (ctxt == NULL) return;
1666
1667 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1668 xmlFreeInputStream(input);
1669 }
Owen Taylor3473f882001-02-23 17:55:21 +00001670 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001671 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001672 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1673 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1674 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1675 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001676 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1677 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00001678#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00001679 if ((ctxt->sax != NULL) &&
1680 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00001681#else
1682 if (ctxt->sax != NULL)
1683#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001684 xmlFree(ctxt->sax);
1685 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00001686 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001687 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001688 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001689 if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001690 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1691 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1692 if (ctxt->attsDefault != NULL)
1693 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001694 if (ctxt->attsSpecial != NULL)
1695 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001696 if (ctxt->freeElems != NULL) {
1697 xmlNodePtr cur, next;
1698
1699 cur = ctxt->freeElems;
1700 while (cur != NULL) {
1701 next = cur->next;
1702 xmlFree(cur);
1703 cur = next;
1704 }
1705 }
1706 if (ctxt->freeAttrs != NULL) {
1707 xmlAttrPtr cur, next;
1708
1709 cur = ctxt->freeAttrs;
1710 while (cur != NULL) {
1711 next = cur->next;
1712 xmlFree(cur);
1713 cur = next;
1714 }
1715 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001716 /*
1717 * cleanup the error strings
1718 */
1719 if (ctxt->lastError.message != NULL)
1720 xmlFree(ctxt->lastError.message);
1721 if (ctxt->lastError.file != NULL)
1722 xmlFree(ctxt->lastError.file);
1723 if (ctxt->lastError.str1 != NULL)
1724 xmlFree(ctxt->lastError.str1);
1725 if (ctxt->lastError.str2 != NULL)
1726 xmlFree(ctxt->lastError.str2);
1727 if (ctxt->lastError.str3 != NULL)
1728 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001729
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001730#ifdef LIBXML_CATALOG_ENABLED
1731 if (ctxt->catalogs != NULL)
1732 xmlCatalogFreeLocal(ctxt->catalogs);
1733#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001734 xmlFree(ctxt);
1735}
1736
1737/**
1738 * xmlNewParserCtxt:
1739 *
1740 * Allocate and initialize a new parser context.
1741 *
1742 * Returns the xmlParserCtxtPtr or NULL
1743 */
1744
1745xmlParserCtxtPtr
1746xmlNewParserCtxt()
1747{
1748 xmlParserCtxtPtr ctxt;
1749
1750 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1751 if (ctxt == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001752 xmlErrMemory(NULL, "cannot allocate parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001753 return(NULL);
1754 }
1755 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001756 if (xmlInitParserCtxt(ctxt) < 0) {
1757 xmlFreeParserCtxt(ctxt);
1758 return(NULL);
1759 }
Owen Taylor3473f882001-02-23 17:55:21 +00001760 return(ctxt);
1761}
1762
1763/************************************************************************
1764 * *
1765 * Handling of node informations *
1766 * *
1767 ************************************************************************/
1768
1769/**
1770 * xmlClearParserCtxt:
1771 * @ctxt: an XML parser context
1772 *
1773 * Clear (release owned resources) and reinitialize a parser context
1774 */
1775
1776void
1777xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1778{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001779 if (ctxt==NULL)
1780 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001781 xmlClearNodeInfoSeq(&ctxt->node_seq);
1782 xmlInitParserCtxt(ctxt);
1783}
1784
1785/**
1786 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001787 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00001788 * @node: an XML node within the tree
1789 *
1790 * Find the parser node info struct for a given node
1791 *
1792 * Returns an xmlParserNodeInfo block pointer or NULL
1793 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001794const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,
1795 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001796{
1797 unsigned long pos;
1798
1799 /* Find position where node should be at */
1800 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Daniel Veillardb1d62872001-09-21 09:47:08 +00001801 if (pos < ctx->node_seq.length && ctx->node_seq.buffer[pos].node == node)
Owen Taylor3473f882001-02-23 17:55:21 +00001802 return &ctx->node_seq.buffer[pos];
1803 else
1804 return NULL;
1805}
1806
1807
1808/**
1809 * xmlInitNodeInfoSeq:
1810 * @seq: a node info sequence pointer
1811 *
1812 * -- Initialize (set to initial state) node info sequence
1813 */
1814void
1815xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1816{
1817 seq->length = 0;
1818 seq->maximum = 0;
1819 seq->buffer = NULL;
1820}
1821
1822/**
1823 * xmlClearNodeInfoSeq:
1824 * @seq: a node info sequence pointer
1825 *
1826 * -- Clear (release memory and reinitialize) node
1827 * info sequence
1828 */
1829void
1830xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1831{
1832 if ( seq->buffer != NULL )
1833 xmlFree(seq->buffer);
1834 xmlInitNodeInfoSeq(seq);
1835}
1836
1837
1838/**
1839 * xmlParserFindNodeInfoIndex:
1840 * @seq: a node info sequence pointer
1841 * @node: an XML node pointer
1842 *
1843 *
1844 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1845 * the given node is or should be at in a sorted sequence
1846 *
1847 * Returns a long indicating the position of the record
1848 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001849unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1850 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001851{
1852 unsigned long upper, lower, middle;
1853 int found = 0;
1854
1855 /* Do a binary search for the key */
1856 lower = 1;
1857 upper = seq->length;
1858 middle = 0;
1859 while ( lower <= upper && !found) {
1860 middle = lower + (upper - lower) / 2;
1861 if ( node == seq->buffer[middle - 1].node )
1862 found = 1;
1863 else if ( node < seq->buffer[middle - 1].node )
1864 upper = middle - 1;
1865 else
1866 lower = middle + 1;
1867 }
1868
1869 /* Return position */
1870 if ( middle == 0 || seq->buffer[middle - 1].node < node )
1871 return middle;
1872 else
1873 return middle - 1;
1874}
1875
1876
1877/**
1878 * xmlParserAddNodeInfo:
1879 * @ctxt: an XML parser context
1880 * @info: a node info sequence pointer
1881 *
1882 * Insert node info record into the sorted sequence
1883 */
1884void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001885xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001886 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00001887{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001888 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001889
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001890 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00001891 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001892 info->node);
1893 if (pos < ctxt->node_seq.length
1894 && ctxt->node_seq.buffer[pos].node == info->node) {
1895 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00001896 }
1897
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001898 /* Otherwise, we need to add new node to buffer */
1899 else {
1900 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
1901 xmlParserNodeInfo *tmp_buffer;
1902 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00001903
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001904 if (ctxt->node_seq.maximum == 0)
1905 ctxt->node_seq.maximum = 2;
1906 byte_size = (sizeof(*ctxt->node_seq.buffer) *
1907 (2 * ctxt->node_seq.maximum));
1908
1909 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00001910 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001911 else
1912 tmp_buffer =
1913 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
1914 byte_size);
1915
1916 if (tmp_buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001917 xmlErrMemory(ctxt, "failed to allocate buffer\n");
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001918 return;
1919 }
1920 ctxt->node_seq.buffer = tmp_buffer;
1921 ctxt->node_seq.maximum *= 2;
1922 }
1923
1924 /* If position is not at end, move elements out of the way */
1925 if (pos != ctxt->node_seq.length) {
1926 unsigned long i;
1927
1928 for (i = ctxt->node_seq.length; i > pos; i--)
1929 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
1930 }
1931
1932 /* Copy element and increase length */
1933 ctxt->node_seq.buffer[pos] = *info;
1934 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00001935 }
Owen Taylor3473f882001-02-23 17:55:21 +00001936}
1937
1938/************************************************************************
1939 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00001940 * Defaults settings *
1941 * *
1942 ************************************************************************/
1943/**
1944 * xmlPedanticParserDefault:
1945 * @val: int 0 or 1
1946 *
1947 * Set and return the previous value for enabling pedantic warnings.
1948 *
1949 * Returns the last value for 0 for no substitution, 1 for substitution.
1950 */
1951
1952int
1953xmlPedanticParserDefault(int val) {
1954 int old = xmlPedanticParserDefaultValue;
1955
1956 xmlPedanticParserDefaultValue = val;
1957 return(old);
1958}
1959
1960/**
1961 * xmlLineNumbersDefault:
1962 * @val: int 0 or 1
1963 *
1964 * Set and return the previous value for enabling line numbers in elements
1965 * contents. This may break on old application and is turned off by default.
1966 *
1967 * Returns the last value for 0 for no substitution, 1 for substitution.
1968 */
1969
1970int
1971xmlLineNumbersDefault(int val) {
1972 int old = xmlLineNumbersDefaultValue;
1973
1974 xmlLineNumbersDefaultValue = val;
1975 return(old);
1976}
1977
1978/**
1979 * xmlSubstituteEntitiesDefault:
1980 * @val: int 0 or 1
1981 *
1982 * Set and return the previous value for default entity support.
1983 * Initially the parser always keep entity references instead of substituting
1984 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001985 * default parser behavior
1986 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00001987 * file basis.
1988 *
1989 * Returns the last value for 0 for no substitution, 1 for substitution.
1990 */
1991
1992int
1993xmlSubstituteEntitiesDefault(int val) {
1994 int old = xmlSubstituteEntitiesDefaultValue;
1995
1996 xmlSubstituteEntitiesDefaultValue = val;
1997 return(old);
1998}
1999
2000/**
2001 * xmlKeepBlanksDefault:
2002 * @val: int 0 or 1
2003 *
2004 * Set and return the previous value for default blanks text nodes support.
2005 * The 1.x version of the parser used an heuristic to try to detect
2006 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00002007 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00002008 * using the DOM output text nodes containing those blanks were not generated.
2009 * The 2.x and later version will switch to the XML standard way and
2010 * ignorableWhitespace() are only generated when running the parser in
2011 * validating mode and when the current element doesn't allow CDATA or
2012 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002013 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002014 * on 1.X libs and to switch back to the old mode for compatibility when
2015 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2016 * by using xmlIsBlankNode() commodity function to detect the "empty"
2017 * nodes generated.
2018 * This value also affect autogeneration of indentation when saving code
2019 * if blanks sections are kept, indentation is not generated.
2020 *
2021 * Returns the last value for 0 for no substitution, 1 for substitution.
2022 */
2023
2024int
2025xmlKeepBlanksDefault(int val) {
2026 int old = xmlKeepBlanksDefaultValue;
2027
2028 xmlKeepBlanksDefaultValue = val;
2029 xmlIndentTreeOutput = !val;
2030 return(old);
2031}
2032