blob: 7d18788ac19c68ceba331a51d7e1665d459b4081 [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{
108 if (ctxt != NULL) {
109 ctxt->errNo = XML_ERR_NO_MEMORY;
110 ctxt->instate = XML_PARSER_EOF;
111 ctxt->disableSAX = 1;
112 }
113 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000114 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000115 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
116 NULL, NULL, 0, 0,
117 "Memory allocation failed : %s\n", extra);
118 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000119 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000120 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
121 NULL, NULL, 0, 0, "Memory allocation failed\n");
122}
123
124/**
Daniel Veillarda840b692003-10-19 13:35:37 +0000125 * __xmlErrEncoding:
Daniel Veillardce9457f2003-10-05 21:33:18 +0000126 * @ctxt: an XML parser context
127 * @error: the error number
128 * @msg: the error message
129 * @str1: an string info
130 * @str2: an string info
131 *
132 * Handle an encoding error
133 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000134void
135__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors error,
136 const char *msg, const xmlChar * str1, const xmlChar * str2)
Daniel Veillardce9457f2003-10-05 21:33:18 +0000137{
138 if (ctxt != NULL)
139 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000140 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000141 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
142 NULL, 0, (const char *) str1, (const char *) str2,
143 NULL, 0, 0, msg, str1, str2);
144 if (ctxt != NULL) {
145 ctxt->wellFormed = 0;
146 if (ctxt->recovery == 0)
147 ctxt->disableSAX = 1;
148 }
149}
150
151/**
152 * xmlErrInternal:
153 * @ctxt: an XML parser context
154 * @msg: the error message
155 * @str: error informations
156 *
157 * Handle an internal error
158 */
159static void
160xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
161{
162 if (ctxt != NULL)
163 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000164 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000165 ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
166 XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
167 0, 0, msg, str);
168 if (ctxt != NULL) {
169 ctxt->wellFormed = 0;
170 if (ctxt->recovery == 0)
171 ctxt->disableSAX = 1;
172 }
173}
174
175/**
176 * xmlErrEncodingInt:
177 * @ctxt: an XML parser context
178 * @error: the error number
179 * @msg: the error message
180 * @val: an integer value
181 *
182 * n encoding error
183 */
184static void
185xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
186 const char *msg, int val)
187{
188 if (ctxt != NULL)
189 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000190 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000191 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
192 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
193 if (ctxt != NULL) {
194 ctxt->wellFormed = 0;
195 if (ctxt->recovery == 0)
196 ctxt->disableSAX = 1;
197 }
198}
199
Owen Taylor3473f882001-02-23 17:55:21 +0000200/**
201 * xmlIsLetter:
202 * @c: an unicode character (int)
203 *
204 * Check whether the character is allowed by the production
205 * [84] Letter ::= BaseChar | Ideographic
206 *
207 * Returns 0 if not, non-zero otherwise
208 */
209int
210xmlIsLetter(int c) {
211 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
212}
213
Owen Taylor3473f882001-02-23 17:55:21 +0000214/************************************************************************
215 * *
216 * Input handling functions for progressive parsing *
217 * *
218 ************************************************************************/
219
220/* #define DEBUG_INPUT */
221/* #define DEBUG_STACK */
222/* #define DEBUG_PUSH */
223
224
225/* we need to keep enough input to show errors in context */
226#define LINE_LEN 80
227
228#ifdef DEBUG_INPUT
229#define CHECK_BUFFER(in) check_buffer(in)
230
Daniel Veillard01c13b52002-12-10 15:19:08 +0000231static
Owen Taylor3473f882001-02-23 17:55:21 +0000232void check_buffer(xmlParserInputPtr in) {
233 if (in->base != in->buf->buffer->content) {
234 xmlGenericError(xmlGenericErrorContext,
235 "xmlParserInput: base mismatch problem\n");
236 }
237 if (in->cur < in->base) {
238 xmlGenericError(xmlGenericErrorContext,
239 "xmlParserInput: cur < base problem\n");
240 }
241 if (in->cur > in->base + in->buf->buffer->use) {
242 xmlGenericError(xmlGenericErrorContext,
243 "xmlParserInput: cur > base + use problem\n");
244 }
245 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
246 (int) in, (int) in->buf->buffer->content, in->cur - in->base,
247 in->buf->buffer->use, in->buf->buffer->size);
248}
249
250#else
251#define CHECK_BUFFER(in)
252#endif
253
254
255/**
256 * xmlParserInputRead:
257 * @in: an XML parser input
258 * @len: an indicative size for the lookahead
259 *
260 * This function refresh the input for the parser. It doesn't try to
261 * preserve pointers to the input buffer, and discard already read data
262 *
263 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
264 * end of this entity
265 */
266int
267xmlParserInputRead(xmlParserInputPtr in, int len) {
268 int ret;
269 int used;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000270 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000271
272#ifdef DEBUG_INPUT
273 xmlGenericError(xmlGenericErrorContext, "Read\n");
274#endif
275 if (in->buf == NULL) return(-1);
276 if (in->base == NULL) return(-1);
277 if (in->cur == NULL) return(-1);
278 if (in->buf->buffer == NULL) return(-1);
279 if (in->buf->readcallback == NULL) return(-1);
280
281 CHECK_BUFFER(in);
282
283 used = in->cur - in->buf->buffer->content;
284 ret = xmlBufferShrink(in->buf->buffer, used);
285 if (ret > 0) {
286 in->cur -= ret;
287 in->consumed += ret;
288 }
289 ret = xmlParserInputBufferRead(in->buf, len);
290 if (in->base != in->buf->buffer->content) {
291 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000292 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000293 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000294 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000295 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000296 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000297 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000298 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000299
300 CHECK_BUFFER(in);
301
302 return(ret);
303}
304
305/**
306 * xmlParserInputGrow:
307 * @in: an XML parser input
308 * @len: an indicative size for the lookahead
309 *
310 * This function increase the input for the parser. It tries to
311 * preserve pointers to the input buffer, and keep already read data
312 *
313 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
314 * end of this entity
315 */
316int
317xmlParserInputGrow(xmlParserInputPtr in, int len) {
318 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000319 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000320
321#ifdef DEBUG_INPUT
322 xmlGenericError(xmlGenericErrorContext, "Grow\n");
323#endif
324 if (in->buf == NULL) return(-1);
325 if (in->base == NULL) return(-1);
326 if (in->cur == NULL) return(-1);
327 if (in->buf->buffer == NULL) return(-1);
328
329 CHECK_BUFFER(in);
330
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000331 indx = in->cur - in->base;
332 if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000333
334 CHECK_BUFFER(in);
335
336 return(0);
337 }
338 if (in->buf->readcallback != NULL)
339 ret = xmlParserInputBufferGrow(in->buf, len);
340 else
341 return(0);
342
343 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +0000344 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +0000345 * block, but we use it really as an integer to do some
346 * pointer arithmetic. Insure will raise it as a bug but in
347 * that specific case, that's not !
348 */
349 if (in->base != in->buf->buffer->content) {
350 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000351 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000352 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000353 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000354 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000355 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000356 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000357 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000358
359 CHECK_BUFFER(in);
360
361 return(ret);
362}
363
364/**
365 * xmlParserInputShrink:
366 * @in: an XML parser input
367 *
368 * This function removes used input for the parser.
369 */
370void
371xmlParserInputShrink(xmlParserInputPtr in) {
372 int used;
373 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000374 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000375
376#ifdef DEBUG_INPUT
377 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
378#endif
379 if (in->buf == NULL) return;
380 if (in->base == NULL) return;
381 if (in->cur == NULL) return;
382 if (in->buf->buffer == NULL) return;
383
384 CHECK_BUFFER(in);
385
386 used = in->cur - in->buf->buffer->content;
387 /*
388 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000389 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +0000390 */
Owen Taylor3473f882001-02-23 17:55:21 +0000391 if (used > INPUT_CHUNK) {
392 ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
393 if (ret > 0) {
394 in->cur -= ret;
395 in->consumed += ret;
396 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000397 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000398 }
399
400 CHECK_BUFFER(in);
401
402 if (in->buf->buffer->use > INPUT_CHUNK) {
403 return;
404 }
405 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
406 if (in->base != in->buf->buffer->content) {
407 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000408 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000409 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000410 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000411 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000412 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000413 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000414 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000415
416 CHECK_BUFFER(in);
417}
418
419/************************************************************************
420 * *
421 * UTF8 character input and related functions *
422 * *
423 ************************************************************************/
424
425/**
426 * xmlNextChar:
427 * @ctxt: the XML parser context
428 *
429 * Skip to the next char input char.
430 */
431
432void
Daniel Veillard77a90a72003-03-22 00:04:05 +0000433xmlNextChar(xmlParserCtxtPtr ctxt)
434{
Owen Taylor3473f882001-02-23 17:55:21 +0000435 if (ctxt->instate == XML_PARSER_EOF)
Daniel Veillard77a90a72003-03-22 00:04:05 +0000436 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000437
Daniel Veillardfdc91562002-07-01 21:52:03 +0000438 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000439 if ((*ctxt->input->cur == 0) &&
440 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
441 (ctxt->instate != XML_PARSER_COMMENT)) {
442 /*
443 * If we are at the end of the current entity and
444 * the context allows it, we pop consumed entities
445 * automatically.
446 * the auto closing should be blocked in other cases
447 */
448 xmlPopInput(ctxt);
449 } else {
450 const unsigned char *cur;
451 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +0000452
Daniel Veillard77a90a72003-03-22 00:04:05 +0000453 /*
454 * 2.11 End-of-Line Handling
455 * the literal two-character sequence "#xD#xA" or a standalone
456 * literal #xD, an XML processor must pass to the application
457 * the single character #xA.
458 */
459 if (*(ctxt->input->cur) == '\n') {
460 ctxt->input->line++;
461 ctxt->input->col = 1;
462 } else
463 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +0000464
Daniel Veillard77a90a72003-03-22 00:04:05 +0000465 /*
466 * We are supposed to handle UTF8, check it's valid
467 * From rfc2044: encoding of the Unicode values on UTF-8:
468 *
469 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
470 * 0000 0000-0000 007F 0xxxxxxx
471 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
472 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
473 *
474 * Check for the 0x110000 limit too
475 */
476 cur = ctxt->input->cur;
477
478 c = *cur;
479 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000480 if (c == 0xC0)
481 goto encoding_error;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000482 if (cur[1] == 0)
483 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
484 if ((cur[1] & 0xc0) != 0x80)
485 goto encoding_error;
486 if ((c & 0xe0) == 0xe0) {
487 unsigned int val;
488
489 if (cur[2] == 0)
490 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
491 if ((cur[2] & 0xc0) != 0x80)
492 goto encoding_error;
493 if ((c & 0xf0) == 0xf0) {
494 if (cur[3] == 0)
495 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
496 if (((c & 0xf8) != 0xf0) ||
497 ((cur[3] & 0xc0) != 0x80))
498 goto encoding_error;
499 /* 4-byte code */
500 ctxt->input->cur += 4;
501 val = (cur[0] & 0x7) << 18;
502 val |= (cur[1] & 0x3f) << 12;
503 val |= (cur[2] & 0x3f) << 6;
504 val |= cur[3] & 0x3f;
505 } else {
506 /* 3-byte code */
507 ctxt->input->cur += 3;
508 val = (cur[0] & 0xf) << 12;
509 val |= (cur[1] & 0x3f) << 6;
510 val |= cur[2] & 0x3f;
511 }
512 if (((val > 0xd7ff) && (val < 0xe000)) ||
513 ((val > 0xfffd) && (val < 0x10000)) ||
514 (val >= 0x110000)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000515 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
516 "Char 0x%X out of allowed range\n",
517 val);
Daniel Veillard77a90a72003-03-22 00:04:05 +0000518 }
519 } else
520 /* 2-byte code */
521 ctxt->input->cur += 2;
522 } else
523 /* 1-byte code */
524 ctxt->input->cur++;
525
526 ctxt->nbChars++;
527 if (*ctxt->input->cur == 0)
528 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
529 }
Owen Taylor3473f882001-02-23 17:55:21 +0000530 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000531 /*
532 * Assume it's a fixed length encoding (1) with
533 * a compatible encoding for the ASCII set, since
534 * XML constructs only use < 128 chars
535 */
536
537 if (*(ctxt->input->cur) == '\n') {
538 ctxt->input->line++;
539 ctxt->input->col = 1;
540 } else
541 ctxt->input->col++;
542 ctxt->input->cur++;
543 ctxt->nbChars++;
544 if (*ctxt->input->cur == 0)
545 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +0000546 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000547 if ((*ctxt->input->cur == '%') && (!ctxt->html))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000548 xmlParserHandlePEReference(ctxt);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000549 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000550 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000551 xmlPopInput(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +0000552 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000553encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +0000554 /*
555 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000556 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000557 * declaration header. Report the error and switch the encoding
558 * to ISO-Latin-1 (if you don't like this policy, just declare the
559 * encoding !)
560 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000561 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000562 "Input is not proper UTF-8, indicate encoding !\n",
563 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000564 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000565 ctxt->sax->error(ctxt->userData,
Daniel Veillard77a90a72003-03-22 00:04:05 +0000566 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
567 ctxt->input->cur[0], ctxt->input->cur[1],
568 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000569 }
Daniel Veillard77a90a72003-03-22 00:04:05 +0000570 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000571 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000572 return;
573}
574
575/**
576 * xmlCurrentChar:
577 * @ctxt: the XML parser context
578 * @len: pointer to the length of the char read
579 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000580 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000581 * bytes in the input buffer. Implement the end of line normalization:
582 * 2.11 End-of-Line Handling
583 * Wherever an external parsed entity or the literal entity value
584 * of an internal parsed entity contains either the literal two-character
585 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
586 * must pass to the application the single character #xA.
587 * This behavior can conveniently be produced by normalizing all
588 * line breaks to #xA on input, before parsing.)
589 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000590 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000591 */
592
593int
594xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
595 if (ctxt->instate == XML_PARSER_EOF)
596 return(0);
597
Daniel Veillard561b7f82002-03-20 21:55:57 +0000598 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
599 *len = 1;
600 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000601 }
602 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
603 /*
604 * We are supposed to handle UTF8, check it's valid
605 * From rfc2044: encoding of the Unicode values on UTF-8:
606 *
607 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
608 * 0000 0000-0000 007F 0xxxxxxx
609 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
610 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
611 *
612 * Check for the 0x110000 limit too
613 */
614 const unsigned char *cur = ctxt->input->cur;
615 unsigned char c;
616 unsigned int val;
617
618 c = *cur;
619 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000620 if (c == 0xC0)
621 goto encoding_error;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000622 if (cur[1] == 0)
623 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
624 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000625 goto encoding_error;
626 if ((c & 0xe0) == 0xe0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000627
628 if (cur[2] == 0)
629 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
630 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000631 goto encoding_error;
632 if ((c & 0xf0) == 0xf0) {
633 if (cur[3] == 0)
634 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000635 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +0000636 ((cur[3] & 0xc0) != 0x80))
637 goto encoding_error;
638 /* 4-byte code */
639 *len = 4;
640 val = (cur[0] & 0x7) << 18;
641 val |= (cur[1] & 0x3f) << 12;
642 val |= (cur[2] & 0x3f) << 6;
643 val |= cur[3] & 0x3f;
644 } else {
645 /* 3-byte code */
646 *len = 3;
647 val = (cur[0] & 0xf) << 12;
648 val |= (cur[1] & 0x3f) << 6;
649 val |= cur[2] & 0x3f;
650 }
651 } else {
652 /* 2-byte code */
653 *len = 2;
654 val = (cur[0] & 0x1f) << 6;
655 val |= cur[1] & 0x3f;
656 }
657 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000658 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
659 "Char 0x%X out of allowed range\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000660 }
661 return(val);
662 } else {
663 /* 1-byte code */
664 *len = 1;
665 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000666 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000667 ctxt->nbChars++;
668 ctxt->input->cur++;
669 }
670 return(0xA);
671 }
672 return((int) *ctxt->input->cur);
673 }
674 }
675 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000676 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000677 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000678 * XML constructs only use < 128 chars
679 */
680 *len = 1;
681 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000682 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000683 ctxt->nbChars++;
684 ctxt->input->cur++;
685 }
686 return(0xA);
687 }
688 return((int) *ctxt->input->cur);
689encoding_error:
690 /*
Daniel Veillardd2ff0392002-11-22 12:28:38 +0000691 * An encoding problem may arise from a truncated input buffer
692 * splitting a character in the middle. In that case do not raise
693 * an error but return 0 to endicate an end of stream problem
694 */
695 if (ctxt->input->end - ctxt->input->cur < 4) {
696 *len = 0;
697 return(0);
698 }
699
700 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000701 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000702 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000703 * declaration header. Report the error and switch the encoding
704 * to ISO-Latin-1 (if you don't like this policy, just declare the
705 * encoding !)
706 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000707 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000708 "Input is not proper UTF-8, indicate encoding !\n",
709 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000710 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000711 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Daniel Veillard561b7f82002-03-20 21:55:57 +0000712 ctxt->input->cur[0], ctxt->input->cur[1],
713 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000714 }
Owen Taylor3473f882001-02-23 17:55:21 +0000715 ctxt->charset = XML_CHAR_ENCODING_8859_1;
716 *len = 1;
717 return((int) *ctxt->input->cur);
718}
719
720/**
721 * xmlStringCurrentChar:
722 * @ctxt: the XML parser context
723 * @cur: pointer to the beginning of the char
724 * @len: pointer to the length of the char read
725 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000726 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000727 * bytes in the input buffer.
728 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000729 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000730 */
731
732int
Daniel Veillardd8224e02002-01-13 15:43:22 +0000733xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
734{
Daniel Veillard61d80a22001-04-27 17:13:01 +0000735 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +0000736 /*
737 * We are supposed to handle UTF8, check it's valid
738 * From rfc2044: encoding of the Unicode values on UTF-8:
739 *
740 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
741 * 0000 0000-0000 007F 0xxxxxxx
742 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
743 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
744 *
745 * Check for the 0x110000 limit too
746 */
747 unsigned char c;
748 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +0000749
Daniel Veillardd8224e02002-01-13 15:43:22 +0000750 c = *cur;
751 if (c & 0x80) {
752 if ((cur[1] & 0xc0) != 0x80)
753 goto encoding_error;
754 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000755
Daniel Veillardd8224e02002-01-13 15:43:22 +0000756 if ((cur[2] & 0xc0) != 0x80)
757 goto encoding_error;
758 if ((c & 0xf0) == 0xf0) {
759 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
760 goto encoding_error;
761 /* 4-byte code */
762 *len = 4;
763 val = (cur[0] & 0x7) << 18;
764 val |= (cur[1] & 0x3f) << 12;
765 val |= (cur[2] & 0x3f) << 6;
766 val |= cur[3] & 0x3f;
767 } else {
768 /* 3-byte code */
769 *len = 3;
770 val = (cur[0] & 0xf) << 12;
771 val |= (cur[1] & 0x3f) << 6;
772 val |= cur[2] & 0x3f;
773 }
774 } else {
775 /* 2-byte code */
776 *len = 2;
777 val = (cur[0] & 0x1f) << 6;
778 val |= cur[1] & 0x3f;
779 }
780 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000781 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
782 "Char 0x%X out of allowed range\n", val);
Daniel Veillardd8224e02002-01-13 15:43:22 +0000783 }
784 return (val);
785 } else {
786 /* 1-byte code */
787 *len = 1;
788 return ((int) *cur);
789 }
Owen Taylor3473f882001-02-23 17:55:21 +0000790 }
791 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000792 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000793 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000794 * XML constructs only use < 128 chars
795 */
796 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000797 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000798encoding_error:
Daniel Veillardd8224e02002-01-13 15:43:22 +0000799
Owen Taylor3473f882001-02-23 17:55:21 +0000800 /*
801 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000802 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000803 * declaration header. Report the error and switch the encoding
804 * to ISO-Latin-1 (if you don't like this policy, just declare the
805 * encoding !)
806 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000807 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000808 "Input is not proper UTF-8, indicate encoding !\n",
809 NULL, NULL);
810 if ((ctxt != NULL) && (ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
811 ctxt->sax->error(ctxt->userData,
812 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
813 ctxt->input->cur[0], ctxt->input->cur[1],
814 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000815 }
Owen Taylor3473f882001-02-23 17:55:21 +0000816 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000817 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000818}
819
820/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000821 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000822 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +0000823 * @val: the char value
824 *
825 * append the char value in the array
826 *
827 * Returns the number of xmlChar written
828 */
Owen Taylor3473f882001-02-23 17:55:21 +0000829int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000830xmlCopyCharMultiByte(xmlChar *out, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +0000831 /*
832 * We are supposed to handle UTF8, check it's valid
833 * From rfc2044: encoding of the Unicode values on UTF-8:
834 *
835 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
836 * 0000 0000-0000 007F 0xxxxxxx
837 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
838 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
839 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000840 if (val >= 0x80) {
841 xmlChar *savedout = out;
842 int bits;
843 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
844 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
845 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
846 else {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000847 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000848 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Daniel Veillardce9457f2003-10-05 21:33:18 +0000849 val);
Owen Taylor3473f882001-02-23 17:55:21 +0000850 return(0);
851 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000852 for ( ; bits >= 0; bits-= 6)
853 *out++= ((val >> bits) & 0x3F) | 0x80 ;
854 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +0000855 }
856 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000857 return 1;
858}
859
860/**
861 * xmlCopyChar:
862 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000863 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000864 * @val: the char value
865 *
866 * append the char value in the array
867 *
868 * Returns the number of xmlChar written
869 */
870
871int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000872xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000873 /* the len parameter is ignored */
874 if (val >= 0x80) {
875 return(xmlCopyCharMultiByte (out, val));
876 }
877 *out = (xmlChar) val;
878 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000879}
880
881/************************************************************************
882 * *
883 * Commodity functions to switch encodings *
884 * *
885 ************************************************************************/
886
887/**
888 * xmlSwitchEncoding:
889 * @ctxt: the parser context
890 * @enc: the encoding value (number)
891 *
892 * change the input functions when discovering the character encoding
893 * of a given entity.
894 *
895 * Returns 0 in case of success, -1 otherwise
896 */
897int
898xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
899{
900 xmlCharEncodingHandlerPtr handler;
901
902 switch (enc) {
903 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000904 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000905 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000906 break;
907 case XML_CHAR_ENCODING_NONE:
908 /* let's assume it's UTF-8 without the XML decl */
909 ctxt->charset = XML_CHAR_ENCODING_UTF8;
910 return(0);
911 case XML_CHAR_ENCODING_UTF8:
912 /* default encoding, no conversion should be needed */
913 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +0000914
915 /*
916 * Errata on XML-1.0 June 20 2001
917 * Specific handling of the Byte Order Mark for
918 * UTF-8
919 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +0000920 if ((ctxt->input != NULL) &&
921 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +0000922 (ctxt->input->cur[1] == 0xBB) &&
923 (ctxt->input->cur[2] == 0xBF)) {
924 ctxt->input->cur += 3;
925 }
Owen Taylor3473f882001-02-23 17:55:21 +0000926 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +0000927 case XML_CHAR_ENCODING_UTF16LE:
928 case XML_CHAR_ENCODING_UTF16BE:
929 /*The raw input characters are encoded
930 *in UTF-16. As we expect this function
931 *to be called after xmlCharEncInFunc, we expect
932 *ctxt->input->cur to contain UTF-8 encoded characters.
933 *So the raw UTF16 Byte Order Mark
934 *has also been converted into
935 *an UTF-8 BOM. Let's skip that BOM.
936 */
937 if ((ctxt->input != NULL) &&
938 (ctxt->input->cur[0] == 0xEF) &&
939 (ctxt->input->cur[1] == 0xBB) &&
940 (ctxt->input->cur[2] == 0xBF)) {
941 ctxt->input->cur += 3;
942 }
943 break ;
Owen Taylor3473f882001-02-23 17:55:21 +0000944 default:
945 break;
946 }
947 handler = xmlGetCharEncodingHandler(enc);
948 if (handler == NULL) {
949 /*
950 * Default handlers.
951 */
952 switch (enc) {
953 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000954 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000955 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000956 break;
957 case XML_CHAR_ENCODING_NONE:
958 /* let's assume it's UTF-8 without the XML decl */
959 ctxt->charset = XML_CHAR_ENCODING_UTF8;
960 return(0);
961 case XML_CHAR_ENCODING_UTF8:
962 case XML_CHAR_ENCODING_ASCII:
963 /* default encoding, no conversion should be needed */
964 ctxt->charset = XML_CHAR_ENCODING_UTF8;
965 return(0);
966 case XML_CHAR_ENCODING_UTF16LE:
967 break;
968 case XML_CHAR_ENCODING_UTF16BE:
969 break;
970 case XML_CHAR_ENCODING_UCS4LE:
Daniel Veillarda840b692003-10-19 13:35:37 +0000971 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000972 "encoding not supported %s\n",
973 BAD_CAST "USC4 little endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000974 break;
975 case XML_CHAR_ENCODING_UCS4BE:
Daniel Veillarda840b692003-10-19 13:35:37 +0000976 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000977 "encoding not supported %s\n",
978 BAD_CAST "USC4 big endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000979 break;
980 case XML_CHAR_ENCODING_EBCDIC:
Daniel Veillarda840b692003-10-19 13:35:37 +0000981 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000982 "encoding not supported %s\n",
983 BAD_CAST "EBCDIC", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000984 break;
985 case XML_CHAR_ENCODING_UCS4_2143:
Daniel Veillarda840b692003-10-19 13:35:37 +0000986 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000987 "encoding not supported %s\n",
988 BAD_CAST "UCS4 2143", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000989 break;
990 case XML_CHAR_ENCODING_UCS4_3412:
Daniel Veillarda840b692003-10-19 13:35:37 +0000991 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000992 "encoding not supported %s\n",
993 BAD_CAST "UCS4 3412", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000994 break;
995 case XML_CHAR_ENCODING_UCS2:
Daniel Veillarda840b692003-10-19 13:35:37 +0000996 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000997 "encoding not supported %s\n",
998 BAD_CAST "UCS2", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000999 break;
1000 case XML_CHAR_ENCODING_8859_1:
1001 case XML_CHAR_ENCODING_8859_2:
1002 case XML_CHAR_ENCODING_8859_3:
1003 case XML_CHAR_ENCODING_8859_4:
1004 case XML_CHAR_ENCODING_8859_5:
1005 case XML_CHAR_ENCODING_8859_6:
1006 case XML_CHAR_ENCODING_8859_7:
1007 case XML_CHAR_ENCODING_8859_8:
1008 case XML_CHAR_ENCODING_8859_9:
1009 /*
1010 * We used to keep the internal content in the
1011 * document encoding however this turns being unmaintainable
1012 * So xmlGetCharEncodingHandler() will return non-null
1013 * values for this now.
1014 */
1015 if ((ctxt->inputNr == 1) &&
1016 (ctxt->encoding == NULL) &&
1017 (ctxt->input->encoding != NULL)) {
1018 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1019 }
1020 ctxt->charset = enc;
1021 return(0);
1022 case XML_CHAR_ENCODING_2022_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001023 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001024 "encoding not supported %s\n",
1025 BAD_CAST "ISO-2022-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001026 break;
1027 case XML_CHAR_ENCODING_SHIFT_JIS:
Daniel Veillarda840b692003-10-19 13:35:37 +00001028 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001029 "encoding not supported %s\n",
1030 BAD_CAST "Shift_JIS", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001031 break;
1032 case XML_CHAR_ENCODING_EUC_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001033 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001034 "encoding not supported %s\n",
1035 BAD_CAST "EUC-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001036 break;
1037 }
1038 }
1039 if (handler == NULL)
1040 return(-1);
1041 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1042 return(xmlSwitchToEncoding(ctxt, handler));
1043}
1044
1045/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001046 * xmlSwitchInputEncoding:
1047 * @ctxt: the parser context
1048 * @input: the input stream
1049 * @handler: the encoding handler
1050 *
1051 * change the input functions when discovering the character encoding
1052 * of a given entity.
1053 *
1054 * Returns 0 in case of success, -1 otherwise
1055 */
1056int
1057xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1058 xmlCharEncodingHandlerPtr handler)
1059{
1060 int nbchars;
1061
1062 if (handler == NULL)
1063 return (-1);
1064 if (input == NULL)
1065 return (-1);
1066 if (input->buf != NULL) {
1067 if (input->buf->encoder != NULL) {
1068 /*
1069 * Check in case the auto encoding detetection triggered
1070 * in already.
1071 */
1072 if (input->buf->encoder == handler)
1073 return (0);
1074
1075 /*
1076 * "UTF-16" can be used for both LE and BE
1077 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1078 BAD_CAST "UTF-16", 6)) &&
1079 (!xmlStrncmp(BAD_CAST handler->name,
1080 BAD_CAST "UTF-16", 6))) {
1081 return(0);
1082 }
1083 */
1084
1085 /*
1086 * Note: this is a bit dangerous, but that's what it
1087 * takes to use nearly compatible signature for different
1088 * encodings.
1089 */
1090 xmlCharEncCloseFunc(input->buf->encoder);
1091 input->buf->encoder = handler;
1092 return (0);
1093 }
1094 input->buf->encoder = handler;
1095
1096 /*
1097 * Is there already some content down the pipe to convert ?
1098 */
1099 if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) {
1100 int processed;
1101
1102 /*
1103 * Specific handling of the Byte Order Mark for
1104 * UTF-16
1105 */
1106 if ((handler->name != NULL) &&
1107 (!strcmp(handler->name, "UTF-16LE")) &&
1108 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1109 input->cur += 2;
1110 }
1111 if ((handler->name != NULL) &&
1112 (!strcmp(handler->name, "UTF-16BE")) &&
1113 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1114 input->cur += 2;
1115 }
1116 /*
1117 * Errata on XML-1.0 June 20 2001
1118 * Specific handling of the Byte Order Mark for
1119 * UTF-8
1120 */
1121 if ((handler->name != NULL) &&
1122 (!strcmp(handler->name, "UTF-8")) &&
1123 (input->cur[0] == 0xEF) &&
1124 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1125 input->cur += 3;
1126 }
1127
1128 /*
1129 * Shrink the current input buffer.
1130 * Move it as the raw buffer and create a new input buffer
1131 */
1132 processed = input->cur - input->base;
1133 xmlBufferShrink(input->buf->buffer, processed);
1134 input->buf->raw = input->buf->buffer;
1135 input->buf->buffer = xmlBufferCreate();
1136
1137 if (ctxt->html) {
1138 /*
1139 * convert as much as possible of the buffer
1140 */
1141 nbchars = xmlCharEncInFunc(input->buf->encoder,
1142 input->buf->buffer,
1143 input->buf->raw);
1144 } else {
1145 /*
1146 * convert just enough to get
1147 * '<?xml version="1.0" encoding="xxx"?>'
1148 * parsed with the autodetected encoding
1149 * into the parser reading buffer.
1150 */
1151 nbchars = xmlCharEncFirstLine(input->buf->encoder,
1152 input->buf->buffer,
1153 input->buf->raw);
1154 }
1155 if (nbchars < 0) {
1156 xmlErrInternal(ctxt,
1157 "switching encoding: encoder error\n",
1158 NULL);
1159 return (-1);
1160 }
1161 input->base = input->cur = input->buf->buffer->content;
1162 input->end = &input->base[input->buf->buffer->use];
1163
1164 }
1165 return (0);
1166 } else {
1167 if ((input->length == 0) || (input->buf == NULL)) {
1168 /*
1169 * When parsing a static memory array one must know the
1170 * size to be able to convert the buffer.
1171 */
1172 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1173 return (-1);
1174 } else {
1175 int processed;
1176
1177 /*
1178 * Shrink the current input buffer.
1179 * Move it as the raw buffer and create a new input buffer
1180 */
1181 processed = input->cur - input->base;
1182
1183 input->buf->raw = xmlBufferCreate();
1184 xmlBufferAdd(input->buf->raw, input->cur,
1185 input->length - processed);
1186 input->buf->buffer = xmlBufferCreate();
1187
1188 /*
1189 * convert as much as possible of the raw input
1190 * to the parser reading buffer.
1191 */
1192 nbchars = xmlCharEncInFunc(input->buf->encoder,
1193 input->buf->buffer,
1194 input->buf->raw);
1195 if (nbchars < 0) {
1196 xmlErrInternal(ctxt,
1197 "switching encoding: encoder error\n",
1198 NULL);
1199 return (-1);
1200 }
1201
1202 /*
1203 * Conversion succeeded, get rid of the old buffer
1204 */
1205 if ((input->free != NULL) && (input->base != NULL))
1206 input->free((xmlChar *) input->base);
1207 input->base = input->cur = input->buf->buffer->content;
1208 input->end = &input->base[input->buf->buffer->use];
1209 }
1210 }
1211 return (0);
1212}
1213
1214/**
Owen Taylor3473f882001-02-23 17:55:21 +00001215 * xmlSwitchToEncoding:
1216 * @ctxt: the parser context
1217 * @handler: the encoding handler
1218 *
1219 * change the input functions when discovering the character encoding
1220 * of a given entity.
1221 *
1222 * Returns 0 in case of success, -1 otherwise
1223 */
1224int
1225xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1226{
Owen Taylor3473f882001-02-23 17:55:21 +00001227 if (handler != NULL) {
1228 if (ctxt->input != NULL) {
Daniel Veillarda840b692003-10-19 13:35:37 +00001229 xmlSwitchInputEncoding(ctxt, ctxt->input, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00001230 } else {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001231 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1232 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001233 return(-1);
1234 }
1235 /*
1236 * The parsing is now done in UTF8 natively
1237 */
1238 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1239 } else
1240 return(-1);
1241 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001242}
1243
1244/************************************************************************
1245 * *
1246 * Commodity functions to handle entities processing *
1247 * *
1248 ************************************************************************/
1249
1250/**
1251 * xmlFreeInputStream:
1252 * @input: an xmlParserInputPtr
1253 *
1254 * Free up an input stream.
1255 */
1256void
1257xmlFreeInputStream(xmlParserInputPtr input) {
1258 if (input == NULL) return;
1259
1260 if (input->filename != NULL) xmlFree((char *) input->filename);
1261 if (input->directory != NULL) xmlFree((char *) input->directory);
1262 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1263 if (input->version != NULL) xmlFree((char *) input->version);
1264 if ((input->free != NULL) && (input->base != NULL))
1265 input->free((xmlChar *) input->base);
1266 if (input->buf != NULL)
1267 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001268 xmlFree(input);
1269}
1270
1271/**
1272 * xmlNewInputStream:
1273 * @ctxt: an XML parser context
1274 *
1275 * Create a new input stream structure
1276 * Returns the new input stream or NULL
1277 */
1278xmlParserInputPtr
1279xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1280 xmlParserInputPtr input;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001281 static int id = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001282
1283 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1284 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001285 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001286 return(NULL);
1287 }
1288 memset(input, 0, sizeof(xmlParserInput));
1289 input->line = 1;
1290 input->col = 1;
1291 input->standalone = -1;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001292 /*
1293 * we don't care about thread reentrancy unicity for a single
1294 * parser context (and hence thread) is sufficient.
1295 */
1296 input->id = id++;
Owen Taylor3473f882001-02-23 17:55:21 +00001297 return(input);
1298}
1299
1300/**
1301 * xmlNewIOInputStream:
1302 * @ctxt: an XML parser context
1303 * @input: an I/O Input
1304 * @enc: the charset encoding if known
1305 *
1306 * Create a new input stream structure encapsulating the @input into
1307 * a stream suitable for the parser.
1308 *
1309 * Returns the new input stream or NULL
1310 */
1311xmlParserInputPtr
1312xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1313 xmlCharEncoding enc) {
1314 xmlParserInputPtr inputStream;
1315
1316 if (xmlParserDebugEntities)
1317 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1318 inputStream = xmlNewInputStream(ctxt);
1319 if (inputStream == NULL) {
1320 return(NULL);
1321 }
1322 inputStream->filename = NULL;
1323 inputStream->buf = input;
1324 inputStream->base = inputStream->buf->buffer->content;
1325 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001326 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001327 if (enc != XML_CHAR_ENCODING_NONE) {
1328 xmlSwitchEncoding(ctxt, enc);
1329 }
1330
1331 return(inputStream);
1332}
1333
1334/**
1335 * xmlNewEntityInputStream:
1336 * @ctxt: an XML parser context
1337 * @entity: an Entity pointer
1338 *
1339 * Create a new input stream based on an xmlEntityPtr
1340 *
1341 * Returns the new input stream or NULL
1342 */
1343xmlParserInputPtr
1344xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1345 xmlParserInputPtr input;
1346
1347 if (entity == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001348 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1349 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001350 return(NULL);
1351 }
1352 if (xmlParserDebugEntities)
1353 xmlGenericError(xmlGenericErrorContext,
1354 "new input from entity: %s\n", entity->name);
1355 if (entity->content == NULL) {
1356 switch (entity->etype) {
1357 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001358 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1359 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001360 break;
1361 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1362 case XML_EXTERNAL_PARAMETER_ENTITY:
1363 return(xmlLoadExternalEntity((char *) entity->URI,
1364 (char *) entity->ExternalID, ctxt));
1365 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001366 xmlErrInternal(ctxt,
1367 "Internal entity %s without content !\n",
1368 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001369 break;
1370 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001371 xmlErrInternal(ctxt,
1372 "Internal parameter entity %s without content !\n",
1373 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001374 break;
1375 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001376 xmlErrInternal(ctxt,
1377 "Predefined entity %s without content !\n",
1378 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001379 break;
1380 }
1381 return(NULL);
1382 }
1383 input = xmlNewInputStream(ctxt);
1384 if (input == NULL) {
1385 return(NULL);
1386 }
1387 input->filename = (char *) entity->URI;
1388 input->base = entity->content;
1389 input->cur = entity->content;
1390 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001391 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001392 return(input);
1393}
1394
1395/**
1396 * xmlNewStringInputStream:
1397 * @ctxt: an XML parser context
1398 * @buffer: an memory buffer
1399 *
1400 * Create a new input stream based on a memory buffer.
1401 * Returns the new input stream
1402 */
1403xmlParserInputPtr
1404xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1405 xmlParserInputPtr input;
1406
1407 if (buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001408 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1409 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001410 return(NULL);
1411 }
1412 if (xmlParserDebugEntities)
1413 xmlGenericError(xmlGenericErrorContext,
1414 "new fixed input: %.30s\n", buffer);
1415 input = xmlNewInputStream(ctxt);
1416 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001417 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001418 return(NULL);
1419 }
1420 input->base = buffer;
1421 input->cur = buffer;
1422 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001423 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001424 return(input);
1425}
1426
1427/**
1428 * xmlNewInputFromFile:
1429 * @ctxt: an XML parser context
1430 * @filename: the filename to use as entity
1431 *
Daniel Veillarda840b692003-10-19 13:35:37 +00001432 * Create a new input stream based on a file or an URL.
Owen Taylor3473f882001-02-23 17:55:21 +00001433 *
1434 * Returns the new input stream or NULL in case of error
1435 */
1436xmlParserInputPtr
1437xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1438 xmlParserInputBufferPtr buf;
1439 xmlParserInputPtr inputStream;
1440 char *directory = NULL;
1441 xmlChar *URI = NULL;
1442
1443 if (xmlParserDebugEntities)
1444 xmlGenericError(xmlGenericErrorContext,
1445 "new input from file: %s\n", filename);
1446 if (ctxt == NULL) return(NULL);
1447 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001448 if (buf == NULL) {
1449 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1450 (const char *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001451 return(NULL);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001452 }
Owen Taylor3473f882001-02-23 17:55:21 +00001453
Owen Taylor3473f882001-02-23 17:55:21 +00001454 inputStream = xmlNewInputStream(ctxt);
1455 if (inputStream == NULL) {
1456 if (directory != NULL) xmlFree((char *) directory);
1457 if (URI != NULL) xmlFree((char *) URI);
1458 return(NULL);
1459 }
Daniel Veillarda840b692003-10-19 13:35:37 +00001460 inputStream->buf = buf;
1461 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1462 if (inputStream == NULL)
1463 return(NULL);
1464
1465 if (inputStream->filename == NULL)
1466 URI = xmlStrdup((xmlChar *) filename);
1467 else
1468 URI = xmlStrdup((xmlChar *) inputStream->filename);
1469 directory = xmlParserGetDirectory((const char *) URI);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001470 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001471 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001472 inputStream->directory = directory;
Owen Taylor3473f882001-02-23 17:55:21 +00001473
1474 inputStream->base = inputStream->buf->buffer->content;
1475 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001476 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001477 if ((ctxt->directory == NULL) && (directory != NULL))
1478 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1479 return(inputStream);
1480}
1481
1482/************************************************************************
1483 * *
1484 * Commodity functions to handle parser contexts *
1485 * *
1486 ************************************************************************/
1487
1488/**
1489 * xmlInitParserCtxt:
1490 * @ctxt: an XML parser context
1491 *
1492 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001493 *
1494 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001495 */
1496
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001497int
Owen Taylor3473f882001-02-23 17:55:21 +00001498xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1499{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001500 if(ctxt==NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001501 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001502 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001503 }
1504
Owen Taylor3473f882001-02-23 17:55:21 +00001505 xmlDefaultSAXHandlerInit();
1506
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001507 ctxt->dict = xmlDictCreate();
1508 if (ctxt->dict == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001509 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001510 return(-1);
1511 }
William M. Brack8b2c7f12002-11-22 05:07:29 +00001512 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1513 if (ctxt->sax == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001514 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001515 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001516 }
1517 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001518 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001519
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001520 ctxt->maxatts = 0;
1521 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001522 /* Allocate the Input stack */
1523 ctxt->inputTab = (xmlParserInputPtr *)
1524 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1525 if (ctxt->inputTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001526 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001527 ctxt->inputNr = 0;
1528 ctxt->inputMax = 0;
1529 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001530 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001531 }
1532 ctxt->inputNr = 0;
1533 ctxt->inputMax = 5;
1534 ctxt->input = NULL;
1535
1536 ctxt->version = NULL;
1537 ctxt->encoding = NULL;
1538 ctxt->standalone = -1;
1539 ctxt->hasExternalSubset = 0;
1540 ctxt->hasPErefs = 0;
1541 ctxt->html = 0;
1542 ctxt->external = 0;
1543 ctxt->instate = XML_PARSER_START;
1544 ctxt->token = 0;
1545 ctxt->directory = NULL;
1546
1547 /* Allocate the Node stack */
1548 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1549 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001550 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001551 ctxt->nodeNr = 0;
1552 ctxt->nodeMax = 0;
1553 ctxt->node = NULL;
1554 ctxt->inputNr = 0;
1555 ctxt->inputMax = 0;
1556 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001557 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001558 }
1559 ctxt->nodeNr = 0;
1560 ctxt->nodeMax = 10;
1561 ctxt->node = NULL;
1562
1563 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001564 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00001565 if (ctxt->nameTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001566 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001567 ctxt->nodeNr = 0;
1568 ctxt->nodeMax = 0;
1569 ctxt->node = NULL;
1570 ctxt->inputNr = 0;
1571 ctxt->inputMax = 0;
1572 ctxt->input = NULL;
1573 ctxt->nameNr = 0;
1574 ctxt->nameMax = 0;
1575 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001576 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001577 }
1578 ctxt->nameNr = 0;
1579 ctxt->nameMax = 10;
1580 ctxt->name = NULL;
1581
1582 /* Allocate the space stack */
1583 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1584 if (ctxt->spaceTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001585 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001586 ctxt->nodeNr = 0;
1587 ctxt->nodeMax = 0;
1588 ctxt->node = NULL;
1589 ctxt->inputNr = 0;
1590 ctxt->inputMax = 0;
1591 ctxt->input = NULL;
1592 ctxt->nameNr = 0;
1593 ctxt->nameMax = 0;
1594 ctxt->name = NULL;
1595 ctxt->spaceNr = 0;
1596 ctxt->spaceMax = 0;
1597 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001598 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001599 }
1600 ctxt->spaceNr = 1;
1601 ctxt->spaceMax = 10;
1602 ctxt->spaceTab[0] = -1;
1603 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00001604 ctxt->userData = ctxt;
1605 ctxt->myDoc = NULL;
1606 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00001607 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001608 ctxt->valid = 1;
1609 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1610 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1611 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00001612 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00001613 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00001614 if (ctxt->keepBlanks == 0)
Daniel Veillard11476b42003-09-26 14:51:39 +00001615 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00001616
Owen Taylor3473f882001-02-23 17:55:21 +00001617 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001618 ctxt->vctxt.error = xmlParserValidityError;
1619 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00001620 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00001621 if (xmlGetWarningsDefaultValue == 0)
1622 ctxt->vctxt.warning = NULL;
1623 else
1624 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00001625 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001626 }
1627 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1628 ctxt->record_info = 0;
1629 ctxt->nbChars = 0;
1630 ctxt->checkIndex = 0;
1631 ctxt->inSubset = 0;
1632 ctxt->errNo = XML_ERR_OK;
1633 ctxt->depth = 0;
1634 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001635 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001636 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001637 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001638}
1639
1640/**
1641 * xmlFreeParserCtxt:
1642 * @ctxt: an XML parser context
1643 *
1644 * Free all the memory used by a parser context. However the parsed
1645 * document in ctxt->myDoc is not freed.
1646 */
1647
1648void
1649xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1650{
1651 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00001652
1653 if (ctxt == NULL) return;
1654
1655 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1656 xmlFreeInputStream(input);
1657 }
Owen Taylor3473f882001-02-23 17:55:21 +00001658 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001659 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001660 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1661 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1662 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1663 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001664 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1665 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00001666#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00001667 if ((ctxt->sax != NULL) &&
1668 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00001669#else
1670 if (ctxt->sax != NULL)
1671#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001672 xmlFree(ctxt->sax);
1673 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00001674 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001675 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001676 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001677 if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001678 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1679 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1680 if (ctxt->attsDefault != NULL)
1681 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001682 if (ctxt->attsSpecial != NULL)
1683 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001684 if (ctxt->freeElems != NULL) {
1685 xmlNodePtr cur, next;
1686
1687 cur = ctxt->freeElems;
1688 while (cur != NULL) {
1689 next = cur->next;
1690 xmlFree(cur);
1691 cur = next;
1692 }
1693 }
1694 if (ctxt->freeAttrs != NULL) {
1695 xmlAttrPtr cur, next;
1696
1697 cur = ctxt->freeAttrs;
1698 while (cur != NULL) {
1699 next = cur->next;
1700 xmlFree(cur);
1701 cur = next;
1702 }
1703 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001704 /*
1705 * cleanup the error strings
1706 */
1707 if (ctxt->lastError.message != NULL)
1708 xmlFree(ctxt->lastError.message);
1709 if (ctxt->lastError.file != NULL)
1710 xmlFree(ctxt->lastError.file);
1711 if (ctxt->lastError.str1 != NULL)
1712 xmlFree(ctxt->lastError.str1);
1713 if (ctxt->lastError.str2 != NULL)
1714 xmlFree(ctxt->lastError.str2);
1715 if (ctxt->lastError.str3 != NULL)
1716 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001717
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001718#ifdef LIBXML_CATALOG_ENABLED
1719 if (ctxt->catalogs != NULL)
1720 xmlCatalogFreeLocal(ctxt->catalogs);
1721#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001722 xmlFree(ctxt);
1723}
1724
1725/**
1726 * xmlNewParserCtxt:
1727 *
1728 * Allocate and initialize a new parser context.
1729 *
1730 * Returns the xmlParserCtxtPtr or NULL
1731 */
1732
1733xmlParserCtxtPtr
1734xmlNewParserCtxt()
1735{
1736 xmlParserCtxtPtr ctxt;
1737
1738 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1739 if (ctxt == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001740 xmlErrMemory(NULL, "cannot allocate parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001741 return(NULL);
1742 }
1743 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001744 if (xmlInitParserCtxt(ctxt) < 0) {
1745 xmlFreeParserCtxt(ctxt);
1746 return(NULL);
1747 }
Owen Taylor3473f882001-02-23 17:55:21 +00001748 return(ctxt);
1749}
1750
1751/************************************************************************
1752 * *
1753 * Handling of node informations *
1754 * *
1755 ************************************************************************/
1756
1757/**
1758 * xmlClearParserCtxt:
1759 * @ctxt: an XML parser context
1760 *
1761 * Clear (release owned resources) and reinitialize a parser context
1762 */
1763
1764void
1765xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1766{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001767 if (ctxt==NULL)
1768 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001769 xmlClearNodeInfoSeq(&ctxt->node_seq);
1770 xmlInitParserCtxt(ctxt);
1771}
1772
1773/**
1774 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001775 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00001776 * @node: an XML node within the tree
1777 *
1778 * Find the parser node info struct for a given node
1779 *
1780 * Returns an xmlParserNodeInfo block pointer or NULL
1781 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001782const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,
1783 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001784{
1785 unsigned long pos;
1786
1787 /* Find position where node should be at */
1788 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Daniel Veillardb1d62872001-09-21 09:47:08 +00001789 if (pos < ctx->node_seq.length && ctx->node_seq.buffer[pos].node == node)
Owen Taylor3473f882001-02-23 17:55:21 +00001790 return &ctx->node_seq.buffer[pos];
1791 else
1792 return NULL;
1793}
1794
1795
1796/**
1797 * xmlInitNodeInfoSeq:
1798 * @seq: a node info sequence pointer
1799 *
1800 * -- Initialize (set to initial state) node info sequence
1801 */
1802void
1803xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1804{
1805 seq->length = 0;
1806 seq->maximum = 0;
1807 seq->buffer = NULL;
1808}
1809
1810/**
1811 * xmlClearNodeInfoSeq:
1812 * @seq: a node info sequence pointer
1813 *
1814 * -- Clear (release memory and reinitialize) node
1815 * info sequence
1816 */
1817void
1818xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1819{
1820 if ( seq->buffer != NULL )
1821 xmlFree(seq->buffer);
1822 xmlInitNodeInfoSeq(seq);
1823}
1824
1825
1826/**
1827 * xmlParserFindNodeInfoIndex:
1828 * @seq: a node info sequence pointer
1829 * @node: an XML node pointer
1830 *
1831 *
1832 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1833 * the given node is or should be at in a sorted sequence
1834 *
1835 * Returns a long indicating the position of the record
1836 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001837unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1838 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001839{
1840 unsigned long upper, lower, middle;
1841 int found = 0;
1842
1843 /* Do a binary search for the key */
1844 lower = 1;
1845 upper = seq->length;
1846 middle = 0;
1847 while ( lower <= upper && !found) {
1848 middle = lower + (upper - lower) / 2;
1849 if ( node == seq->buffer[middle - 1].node )
1850 found = 1;
1851 else if ( node < seq->buffer[middle - 1].node )
1852 upper = middle - 1;
1853 else
1854 lower = middle + 1;
1855 }
1856
1857 /* Return position */
1858 if ( middle == 0 || seq->buffer[middle - 1].node < node )
1859 return middle;
1860 else
1861 return middle - 1;
1862}
1863
1864
1865/**
1866 * xmlParserAddNodeInfo:
1867 * @ctxt: an XML parser context
1868 * @info: a node info sequence pointer
1869 *
1870 * Insert node info record into the sorted sequence
1871 */
1872void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001873xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001874 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00001875{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001876 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001877
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001878 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00001879 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001880 info->node);
1881 if (pos < ctxt->node_seq.length
1882 && ctxt->node_seq.buffer[pos].node == info->node) {
1883 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00001884 }
1885
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001886 /* Otherwise, we need to add new node to buffer */
1887 else {
1888 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
1889 xmlParserNodeInfo *tmp_buffer;
1890 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00001891
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001892 if (ctxt->node_seq.maximum == 0)
1893 ctxt->node_seq.maximum = 2;
1894 byte_size = (sizeof(*ctxt->node_seq.buffer) *
1895 (2 * ctxt->node_seq.maximum));
1896
1897 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00001898 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001899 else
1900 tmp_buffer =
1901 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
1902 byte_size);
1903
1904 if (tmp_buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001905 xmlErrMemory(ctxt, "failed to allocate buffer\n");
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001906 return;
1907 }
1908 ctxt->node_seq.buffer = tmp_buffer;
1909 ctxt->node_seq.maximum *= 2;
1910 }
1911
1912 /* If position is not at end, move elements out of the way */
1913 if (pos != ctxt->node_seq.length) {
1914 unsigned long i;
1915
1916 for (i = ctxt->node_seq.length; i > pos; i--)
1917 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
1918 }
1919
1920 /* Copy element and increase length */
1921 ctxt->node_seq.buffer[pos] = *info;
1922 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00001923 }
Owen Taylor3473f882001-02-23 17:55:21 +00001924}
1925
1926/************************************************************************
1927 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00001928 * Defaults settings *
1929 * *
1930 ************************************************************************/
1931/**
1932 * xmlPedanticParserDefault:
1933 * @val: int 0 or 1
1934 *
1935 * Set and return the previous value for enabling pedantic warnings.
1936 *
1937 * Returns the last value for 0 for no substitution, 1 for substitution.
1938 */
1939
1940int
1941xmlPedanticParserDefault(int val) {
1942 int old = xmlPedanticParserDefaultValue;
1943
1944 xmlPedanticParserDefaultValue = val;
1945 return(old);
1946}
1947
1948/**
1949 * xmlLineNumbersDefault:
1950 * @val: int 0 or 1
1951 *
1952 * Set and return the previous value for enabling line numbers in elements
1953 * contents. This may break on old application and is turned off by default.
1954 *
1955 * Returns the last value for 0 for no substitution, 1 for substitution.
1956 */
1957
1958int
1959xmlLineNumbersDefault(int val) {
1960 int old = xmlLineNumbersDefaultValue;
1961
1962 xmlLineNumbersDefaultValue = val;
1963 return(old);
1964}
1965
1966/**
1967 * xmlSubstituteEntitiesDefault:
1968 * @val: int 0 or 1
1969 *
1970 * Set and return the previous value for default entity support.
1971 * Initially the parser always keep entity references instead of substituting
1972 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001973 * default parser behavior
1974 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00001975 * file basis.
1976 *
1977 * Returns the last value for 0 for no substitution, 1 for substitution.
1978 */
1979
1980int
1981xmlSubstituteEntitiesDefault(int val) {
1982 int old = xmlSubstituteEntitiesDefaultValue;
1983
1984 xmlSubstituteEntitiesDefaultValue = val;
1985 return(old);
1986}
1987
1988/**
1989 * xmlKeepBlanksDefault:
1990 * @val: int 0 or 1
1991 *
1992 * Set and return the previous value for default blanks text nodes support.
1993 * The 1.x version of the parser used an heuristic to try to detect
1994 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00001995 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00001996 * using the DOM output text nodes containing those blanks were not generated.
1997 * The 2.x and later version will switch to the XML standard way and
1998 * ignorableWhitespace() are only generated when running the parser in
1999 * validating mode and when the current element doesn't allow CDATA or
2000 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002001 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002002 * on 1.X libs and to switch back to the old mode for compatibility when
2003 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2004 * by using xmlIsBlankNode() commodity function to detect the "empty"
2005 * nodes generated.
2006 * This value also affect autogeneration of indentation when saving code
2007 * if blanks sections are kept, indentation is not generated.
2008 *
2009 * Returns the last value for 0 for no substitution, 1 for substitution.
2010 */
2011
2012int
2013xmlKeepBlanksDefault(int val) {
2014 int old = xmlKeepBlanksDefaultValue;
2015
2016 xmlKeepBlanksDefaultValue = val;
2017 xmlIndentTreeOutput = !val;
2018 return(old);
2019}
2020