blob: 2d1bd697f0db34de6bbf2abc2d116836ad1bad34 [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/**
125 * xmlErrEncoding:
126 * @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 */
134static void
135xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors error,
136 const char *msg, const xmlChar * str1, const xmlChar * str2)
137{
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 Veillardce9457f2003-10-05 21:33:18 +0000561 xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
562 "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 Veillardce9457f2003-10-05 21:33:18 +0000707 xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
708 "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 Veillardce9457f2003-10-05 21:33:18 +0000807 xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
808 "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 Veillardce9457f2003-10-05 21:33:18 +0000904 xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
905 "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 Veillardce9457f2003-10-05 21:33:18 +0000954 xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
955 "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 Veillardce9457f2003-10-05 21:33:18 +0000971 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
972 "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 Veillardce9457f2003-10-05 21:33:18 +0000976 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
977 "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 Veillardce9457f2003-10-05 21:33:18 +0000981 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
982 "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 Veillardce9457f2003-10-05 21:33:18 +0000986 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
987 "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 Veillardce9457f2003-10-05 21:33:18 +0000991 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
992 "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 Veillardce9457f2003-10-05 21:33:18 +0000996 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
997 "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 Veillardce9457f2003-10-05 21:33:18 +00001023 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1024 "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 Veillardce9457f2003-10-05 21:33:18 +00001028 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1029 "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 Veillardce9457f2003-10-05 21:33:18 +00001033 xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1034 "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/**
1046 * xmlSwitchToEncoding:
1047 * @ctxt: the parser context
1048 * @handler: the encoding handler
1049 *
1050 * change the input functions when discovering the character encoding
1051 * of a given entity.
1052 *
1053 * Returns 0 in case of success, -1 otherwise
1054 */
1055int
1056xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1057{
1058 int nbchars;
1059
1060 if (handler != NULL) {
1061 if (ctxt->input != NULL) {
1062 if (ctxt->input->buf != NULL) {
1063 if (ctxt->input->buf->encoder != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001064 /*
1065 * Check in case the auto encoding detetection triggered
1066 * in already.
1067 */
Owen Taylor3473f882001-02-23 17:55:21 +00001068 if (ctxt->input->buf->encoder == handler)
1069 return(0);
Daniel Veillard878eab02002-02-19 13:46:09 +00001070
1071 /*
1072 * "UTF-16" can be used for both LE and BE
Daniel Veillard878eab02002-02-19 13:46:09 +00001073 if ((!xmlStrncmp(BAD_CAST ctxt->input->buf->encoder->name,
1074 BAD_CAST "UTF-16", 6)) &&
1075 (!xmlStrncmp(BAD_CAST handler->name,
1076 BAD_CAST "UTF-16", 6))) {
1077 return(0);
1078 }
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001079 */
Daniel Veillard878eab02002-02-19 13:46:09 +00001080
Owen Taylor3473f882001-02-23 17:55:21 +00001081 /*
1082 * Note: this is a bit dangerous, but that's what it
1083 * takes to use nearly compatible signature for different
1084 * encodings.
1085 */
1086 xmlCharEncCloseFunc(ctxt->input->buf->encoder);
1087 ctxt->input->buf->encoder = handler;
1088 return(0);
1089 }
1090 ctxt->input->buf->encoder = handler;
1091
1092 /*
1093 * Is there already some content down the pipe to convert ?
1094 */
1095 if ((ctxt->input->buf->buffer != NULL) &&
1096 (ctxt->input->buf->buffer->use > 0)) {
1097 int processed;
1098
1099 /*
1100 * Specific handling of the Byte Order Mark for
1101 * UTF-16
1102 */
1103 if ((handler->name != NULL) &&
1104 (!strcmp(handler->name, "UTF-16LE")) &&
1105 (ctxt->input->cur[0] == 0xFF) &&
1106 (ctxt->input->cur[1] == 0xFE)) {
1107 ctxt->input->cur += 2;
1108 }
1109 if ((handler->name != NULL) &&
1110 (!strcmp(handler->name, "UTF-16BE")) &&
1111 (ctxt->input->cur[0] == 0xFE) &&
1112 (ctxt->input->cur[1] == 0xFF)) {
1113 ctxt->input->cur += 2;
1114 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001115 /*
1116 * Errata on XML-1.0 June 20 2001
1117 * Specific handling of the Byte Order Mark for
1118 * UTF-8
1119 */
1120 if ((handler->name != NULL) &&
1121 (!strcmp(handler->name, "UTF-8")) &&
1122 (ctxt->input->cur[0] == 0xEF) &&
1123 (ctxt->input->cur[1] == 0xBB) &&
Daniel Veillard7dd05702001-10-04 14:25:12 +00001124 (ctxt->input->cur[2] == 0xBF)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001125 ctxt->input->cur += 3;
1126 }
Owen Taylor3473f882001-02-23 17:55:21 +00001127
1128 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001129 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001130 * Move it as the raw buffer and create a new input buffer
1131 */
1132 processed = ctxt->input->cur - ctxt->input->base;
1133 xmlBufferShrink(ctxt->input->buf->buffer, processed);
1134 ctxt->input->buf->raw = ctxt->input->buf->buffer;
1135 ctxt->input->buf->buffer = xmlBufferCreate();
1136
1137 if (ctxt->html) {
1138 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001139 * convert as much as possible of the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00001140 */
1141 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1142 ctxt->input->buf->buffer,
1143 ctxt->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(ctxt->input->buf->encoder,
1152 ctxt->input->buf->buffer,
1153 ctxt->input->buf->raw);
1154 }
1155 if (nbchars < 0) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001156 xmlErrInternal(ctxt,
1157 "xmlSwitchToEncoding: encoder error\n",
1158 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001159 return(-1);
1160 }
1161 ctxt->input->base =
1162 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001163 ctxt->input->end =
1164 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001165
1166 }
1167 return(0);
1168 } else {
1169 if ((ctxt->input->length == 0) || (ctxt->input->buf == NULL)) {
1170 /*
1171 * When parsing a static memory array one must know the
1172 * size to be able to convert the buffer.
1173 */
Daniel Veillardce9457f2003-10-05 21:33:18 +00001174 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1175 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001176 return(-1);
1177 } else {
1178 int processed;
1179
1180 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001181 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001182 * Move it as the raw buffer and create a new input buffer
1183 */
1184 processed = ctxt->input->cur - ctxt->input->base;
1185
1186 ctxt->input->buf->raw = xmlBufferCreate();
1187 xmlBufferAdd(ctxt->input->buf->raw, ctxt->input->cur,
1188 ctxt->input->length - processed);
1189 ctxt->input->buf->buffer = xmlBufferCreate();
1190
1191 /*
1192 * convert as much as possible of the raw input
1193 * to the parser reading buffer.
1194 */
1195 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1196 ctxt->input->buf->buffer,
1197 ctxt->input->buf->raw);
1198 if (nbchars < 0) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001199 xmlErrInternal(ctxt,
1200 "xmlSwitchToEncoding: encoder error\n",
1201 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001202 return(-1);
1203 }
1204
1205 /*
1206 * Conversion succeeded, get rid of the old buffer
1207 */
1208 if ((ctxt->input->free != NULL) &&
1209 (ctxt->input->base != NULL))
1210 ctxt->input->free((xmlChar *) ctxt->input->base);
1211 ctxt->input->base =
1212 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001213 ctxt->input->end =
1214 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001215 }
1216 }
1217 } else {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001218 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1219 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001220 return(-1);
1221 }
1222 /*
1223 * The parsing is now done in UTF8 natively
1224 */
1225 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1226 } else
1227 return(-1);
1228 return(0);
1229
1230}
1231
1232/************************************************************************
1233 * *
1234 * Commodity functions to handle entities processing *
1235 * *
1236 ************************************************************************/
1237
1238/**
1239 * xmlFreeInputStream:
1240 * @input: an xmlParserInputPtr
1241 *
1242 * Free up an input stream.
1243 */
1244void
1245xmlFreeInputStream(xmlParserInputPtr input) {
1246 if (input == NULL) return;
1247
1248 if (input->filename != NULL) xmlFree((char *) input->filename);
1249 if (input->directory != NULL) xmlFree((char *) input->directory);
1250 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1251 if (input->version != NULL) xmlFree((char *) input->version);
1252 if ((input->free != NULL) && (input->base != NULL))
1253 input->free((xmlChar *) input->base);
1254 if (input->buf != NULL)
1255 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001256 xmlFree(input);
1257}
1258
1259/**
1260 * xmlNewInputStream:
1261 * @ctxt: an XML parser context
1262 *
1263 * Create a new input stream structure
1264 * Returns the new input stream or NULL
1265 */
1266xmlParserInputPtr
1267xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1268 xmlParserInputPtr input;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001269 static int id = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001270
1271 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1272 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001273 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001274 return(NULL);
1275 }
1276 memset(input, 0, sizeof(xmlParserInput));
1277 input->line = 1;
1278 input->col = 1;
1279 input->standalone = -1;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001280 /*
1281 * we don't care about thread reentrancy unicity for a single
1282 * parser context (and hence thread) is sufficient.
1283 */
1284 input->id = id++;
Owen Taylor3473f882001-02-23 17:55:21 +00001285 return(input);
1286}
1287
1288/**
1289 * xmlNewIOInputStream:
1290 * @ctxt: an XML parser context
1291 * @input: an I/O Input
1292 * @enc: the charset encoding if known
1293 *
1294 * Create a new input stream structure encapsulating the @input into
1295 * a stream suitable for the parser.
1296 *
1297 * Returns the new input stream or NULL
1298 */
1299xmlParserInputPtr
1300xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1301 xmlCharEncoding enc) {
1302 xmlParserInputPtr inputStream;
1303
1304 if (xmlParserDebugEntities)
1305 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1306 inputStream = xmlNewInputStream(ctxt);
1307 if (inputStream == NULL) {
1308 return(NULL);
1309 }
1310 inputStream->filename = NULL;
1311 inputStream->buf = input;
1312 inputStream->base = inputStream->buf->buffer->content;
1313 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001314 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001315 if (enc != XML_CHAR_ENCODING_NONE) {
1316 xmlSwitchEncoding(ctxt, enc);
1317 }
1318
1319 return(inputStream);
1320}
1321
1322/**
1323 * xmlNewEntityInputStream:
1324 * @ctxt: an XML parser context
1325 * @entity: an Entity pointer
1326 *
1327 * Create a new input stream based on an xmlEntityPtr
1328 *
1329 * Returns the new input stream or NULL
1330 */
1331xmlParserInputPtr
1332xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1333 xmlParserInputPtr input;
1334
1335 if (entity == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001336 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1337 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001338 return(NULL);
1339 }
1340 if (xmlParserDebugEntities)
1341 xmlGenericError(xmlGenericErrorContext,
1342 "new input from entity: %s\n", entity->name);
1343 if (entity->content == NULL) {
1344 switch (entity->etype) {
1345 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001346 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1347 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001348 break;
1349 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1350 case XML_EXTERNAL_PARAMETER_ENTITY:
1351 return(xmlLoadExternalEntity((char *) entity->URI,
1352 (char *) entity->ExternalID, ctxt));
1353 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001354 xmlErrInternal(ctxt,
1355 "Internal entity %s without content !\n",
1356 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001357 break;
1358 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001359 xmlErrInternal(ctxt,
1360 "Internal parameter entity %s without content !\n",
1361 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001362 break;
1363 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001364 xmlErrInternal(ctxt,
1365 "Predefined entity %s without content !\n",
1366 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001367 break;
1368 }
1369 return(NULL);
1370 }
1371 input = xmlNewInputStream(ctxt);
1372 if (input == NULL) {
1373 return(NULL);
1374 }
1375 input->filename = (char *) entity->URI;
1376 input->base = entity->content;
1377 input->cur = entity->content;
1378 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001379 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001380 return(input);
1381}
1382
1383/**
1384 * xmlNewStringInputStream:
1385 * @ctxt: an XML parser context
1386 * @buffer: an memory buffer
1387 *
1388 * Create a new input stream based on a memory buffer.
1389 * Returns the new input stream
1390 */
1391xmlParserInputPtr
1392xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1393 xmlParserInputPtr input;
1394
1395 if (buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001396 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1397 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001398 return(NULL);
1399 }
1400 if (xmlParserDebugEntities)
1401 xmlGenericError(xmlGenericErrorContext,
1402 "new fixed input: %.30s\n", buffer);
1403 input = xmlNewInputStream(ctxt);
1404 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001405 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001406 return(NULL);
1407 }
1408 input->base = buffer;
1409 input->cur = buffer;
1410 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001411 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001412 return(input);
1413}
1414
1415/**
1416 * xmlNewInputFromFile:
1417 * @ctxt: an XML parser context
1418 * @filename: the filename to use as entity
1419 *
1420 * Create a new input stream based on a file.
1421 *
1422 * Returns the new input stream or NULL in case of error
1423 */
1424xmlParserInputPtr
1425xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1426 xmlParserInputBufferPtr buf;
1427 xmlParserInputPtr inputStream;
1428 char *directory = NULL;
1429 xmlChar *URI = NULL;
1430
1431 if (xmlParserDebugEntities)
1432 xmlGenericError(xmlGenericErrorContext,
1433 "new input from file: %s\n", filename);
1434 if (ctxt == NULL) return(NULL);
1435 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1436 if (buf == NULL)
1437 return(NULL);
1438
1439 URI = xmlStrdup((xmlChar *) filename);
1440 directory = xmlParserGetDirectory((const char *) URI);
1441
1442 inputStream = xmlNewInputStream(ctxt);
1443 if (inputStream == NULL) {
1444 if (directory != NULL) xmlFree((char *) directory);
1445 if (URI != NULL) xmlFree((char *) URI);
1446 return(NULL);
1447 }
1448
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001449 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001450 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001451 inputStream->directory = directory;
1452 inputStream->buf = buf;
1453
1454 inputStream->base = inputStream->buf->buffer->content;
1455 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001456 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001457 if ((ctxt->directory == NULL) && (directory != NULL))
1458 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1459 return(inputStream);
1460}
1461
1462/************************************************************************
1463 * *
1464 * Commodity functions to handle parser contexts *
1465 * *
1466 ************************************************************************/
1467
1468/**
1469 * xmlInitParserCtxt:
1470 * @ctxt: an XML parser context
1471 *
1472 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001473 *
1474 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001475 */
1476
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001477int
Owen Taylor3473f882001-02-23 17:55:21 +00001478xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1479{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001480 if(ctxt==NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001481 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001482 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001483 }
1484
Owen Taylor3473f882001-02-23 17:55:21 +00001485 xmlDefaultSAXHandlerInit();
1486
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001487 ctxt->dict = xmlDictCreate();
1488 if (ctxt->dict == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001489 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001490 return(-1);
1491 }
William M. Brack8b2c7f12002-11-22 05:07:29 +00001492 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1493 if (ctxt->sax == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001494 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001495 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001496 }
1497 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001498 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001499
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001500 ctxt->maxatts = 0;
1501 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001502 /* Allocate the Input stack */
1503 ctxt->inputTab = (xmlParserInputPtr *)
1504 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1505 if (ctxt->inputTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001506 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001507 ctxt->inputNr = 0;
1508 ctxt->inputMax = 0;
1509 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001510 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001511 }
1512 ctxt->inputNr = 0;
1513 ctxt->inputMax = 5;
1514 ctxt->input = NULL;
1515
1516 ctxt->version = NULL;
1517 ctxt->encoding = NULL;
1518 ctxt->standalone = -1;
1519 ctxt->hasExternalSubset = 0;
1520 ctxt->hasPErefs = 0;
1521 ctxt->html = 0;
1522 ctxt->external = 0;
1523 ctxt->instate = XML_PARSER_START;
1524 ctxt->token = 0;
1525 ctxt->directory = NULL;
1526
1527 /* Allocate the Node stack */
1528 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1529 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001530 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001531 ctxt->nodeNr = 0;
1532 ctxt->nodeMax = 0;
1533 ctxt->node = NULL;
1534 ctxt->inputNr = 0;
1535 ctxt->inputMax = 0;
1536 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001537 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001538 }
1539 ctxt->nodeNr = 0;
1540 ctxt->nodeMax = 10;
1541 ctxt->node = NULL;
1542
1543 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001544 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00001545 if (ctxt->nameTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001546 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001547 ctxt->nodeNr = 0;
1548 ctxt->nodeMax = 0;
1549 ctxt->node = NULL;
1550 ctxt->inputNr = 0;
1551 ctxt->inputMax = 0;
1552 ctxt->input = NULL;
1553 ctxt->nameNr = 0;
1554 ctxt->nameMax = 0;
1555 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001556 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001557 }
1558 ctxt->nameNr = 0;
1559 ctxt->nameMax = 10;
1560 ctxt->name = NULL;
1561
1562 /* Allocate the space stack */
1563 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1564 if (ctxt->spaceTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001565 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001566 ctxt->nodeNr = 0;
1567 ctxt->nodeMax = 0;
1568 ctxt->node = NULL;
1569 ctxt->inputNr = 0;
1570 ctxt->inputMax = 0;
1571 ctxt->input = NULL;
1572 ctxt->nameNr = 0;
1573 ctxt->nameMax = 0;
1574 ctxt->name = NULL;
1575 ctxt->spaceNr = 0;
1576 ctxt->spaceMax = 0;
1577 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001578 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001579 }
1580 ctxt->spaceNr = 1;
1581 ctxt->spaceMax = 10;
1582 ctxt->spaceTab[0] = -1;
1583 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00001584 ctxt->userData = ctxt;
1585 ctxt->myDoc = NULL;
1586 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00001587 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001588 ctxt->valid = 1;
1589 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1590 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1591 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00001592 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00001593 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00001594 if (ctxt->keepBlanks == 0)
Daniel Veillard11476b42003-09-26 14:51:39 +00001595 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00001596
Owen Taylor3473f882001-02-23 17:55:21 +00001597 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001598 ctxt->vctxt.error = xmlParserValidityError;
1599 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00001600 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00001601 if (xmlGetWarningsDefaultValue == 0)
1602 ctxt->vctxt.warning = NULL;
1603 else
1604 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00001605 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001606 }
1607 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1608 ctxt->record_info = 0;
1609 ctxt->nbChars = 0;
1610 ctxt->checkIndex = 0;
1611 ctxt->inSubset = 0;
1612 ctxt->errNo = XML_ERR_OK;
1613 ctxt->depth = 0;
1614 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001615 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001616 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001617 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001618}
1619
1620/**
1621 * xmlFreeParserCtxt:
1622 * @ctxt: an XML parser context
1623 *
1624 * Free all the memory used by a parser context. However the parsed
1625 * document in ctxt->myDoc is not freed.
1626 */
1627
1628void
1629xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1630{
1631 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00001632
1633 if (ctxt == NULL) return;
1634
1635 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1636 xmlFreeInputStream(input);
1637 }
Owen Taylor3473f882001-02-23 17:55:21 +00001638 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001639 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001640 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1641 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1642 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1643 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001644 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1645 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00001646#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00001647 if ((ctxt->sax != NULL) &&
1648 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00001649#else
1650 if (ctxt->sax != NULL)
1651#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001652 xmlFree(ctxt->sax);
1653 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00001654 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001655 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001656 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001657 if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001658 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1659 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1660 if (ctxt->attsDefault != NULL)
1661 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001662 if (ctxt->attsSpecial != NULL)
1663 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001664 if (ctxt->freeElems != NULL) {
1665 xmlNodePtr cur, next;
1666
1667 cur = ctxt->freeElems;
1668 while (cur != NULL) {
1669 next = cur->next;
1670 xmlFree(cur);
1671 cur = next;
1672 }
1673 }
1674 if (ctxt->freeAttrs != NULL) {
1675 xmlAttrPtr cur, next;
1676
1677 cur = ctxt->freeAttrs;
1678 while (cur != NULL) {
1679 next = cur->next;
1680 xmlFree(cur);
1681 cur = next;
1682 }
1683 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001684 /*
1685 * cleanup the error strings
1686 */
1687 if (ctxt->lastError.message != NULL)
1688 xmlFree(ctxt->lastError.message);
1689 if (ctxt->lastError.file != NULL)
1690 xmlFree(ctxt->lastError.file);
1691 if (ctxt->lastError.str1 != NULL)
1692 xmlFree(ctxt->lastError.str1);
1693 if (ctxt->lastError.str2 != NULL)
1694 xmlFree(ctxt->lastError.str2);
1695 if (ctxt->lastError.str3 != NULL)
1696 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001697
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001698#ifdef LIBXML_CATALOG_ENABLED
1699 if (ctxt->catalogs != NULL)
1700 xmlCatalogFreeLocal(ctxt->catalogs);
1701#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001702 xmlFree(ctxt);
1703}
1704
1705/**
1706 * xmlNewParserCtxt:
1707 *
1708 * Allocate and initialize a new parser context.
1709 *
1710 * Returns the xmlParserCtxtPtr or NULL
1711 */
1712
1713xmlParserCtxtPtr
1714xmlNewParserCtxt()
1715{
1716 xmlParserCtxtPtr ctxt;
1717
1718 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1719 if (ctxt == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001720 xmlErrMemory(NULL, "cannot allocate parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001721 return(NULL);
1722 }
1723 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001724 if (xmlInitParserCtxt(ctxt) < 0) {
1725 xmlFreeParserCtxt(ctxt);
1726 return(NULL);
1727 }
Owen Taylor3473f882001-02-23 17:55:21 +00001728 return(ctxt);
1729}
1730
1731/************************************************************************
1732 * *
1733 * Handling of node informations *
1734 * *
1735 ************************************************************************/
1736
1737/**
1738 * xmlClearParserCtxt:
1739 * @ctxt: an XML parser context
1740 *
1741 * Clear (release owned resources) and reinitialize a parser context
1742 */
1743
1744void
1745xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1746{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001747 if (ctxt==NULL)
1748 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001749 xmlClearNodeInfoSeq(&ctxt->node_seq);
1750 xmlInitParserCtxt(ctxt);
1751}
1752
1753/**
1754 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001755 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00001756 * @node: an XML node within the tree
1757 *
1758 * Find the parser node info struct for a given node
1759 *
1760 * Returns an xmlParserNodeInfo block pointer or NULL
1761 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001762const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,
1763 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001764{
1765 unsigned long pos;
1766
1767 /* Find position where node should be at */
1768 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Daniel Veillardb1d62872001-09-21 09:47:08 +00001769 if (pos < ctx->node_seq.length && ctx->node_seq.buffer[pos].node == node)
Owen Taylor3473f882001-02-23 17:55:21 +00001770 return &ctx->node_seq.buffer[pos];
1771 else
1772 return NULL;
1773}
1774
1775
1776/**
1777 * xmlInitNodeInfoSeq:
1778 * @seq: a node info sequence pointer
1779 *
1780 * -- Initialize (set to initial state) node info sequence
1781 */
1782void
1783xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1784{
1785 seq->length = 0;
1786 seq->maximum = 0;
1787 seq->buffer = NULL;
1788}
1789
1790/**
1791 * xmlClearNodeInfoSeq:
1792 * @seq: a node info sequence pointer
1793 *
1794 * -- Clear (release memory and reinitialize) node
1795 * info sequence
1796 */
1797void
1798xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1799{
1800 if ( seq->buffer != NULL )
1801 xmlFree(seq->buffer);
1802 xmlInitNodeInfoSeq(seq);
1803}
1804
1805
1806/**
1807 * xmlParserFindNodeInfoIndex:
1808 * @seq: a node info sequence pointer
1809 * @node: an XML node pointer
1810 *
1811 *
1812 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1813 * the given node is or should be at in a sorted sequence
1814 *
1815 * Returns a long indicating the position of the record
1816 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001817unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1818 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001819{
1820 unsigned long upper, lower, middle;
1821 int found = 0;
1822
1823 /* Do a binary search for the key */
1824 lower = 1;
1825 upper = seq->length;
1826 middle = 0;
1827 while ( lower <= upper && !found) {
1828 middle = lower + (upper - lower) / 2;
1829 if ( node == seq->buffer[middle - 1].node )
1830 found = 1;
1831 else if ( node < seq->buffer[middle - 1].node )
1832 upper = middle - 1;
1833 else
1834 lower = middle + 1;
1835 }
1836
1837 /* Return position */
1838 if ( middle == 0 || seq->buffer[middle - 1].node < node )
1839 return middle;
1840 else
1841 return middle - 1;
1842}
1843
1844
1845/**
1846 * xmlParserAddNodeInfo:
1847 * @ctxt: an XML parser context
1848 * @info: a node info sequence pointer
1849 *
1850 * Insert node info record into the sorted sequence
1851 */
1852void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001853xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001854 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00001855{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001856 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001857
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001858 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00001859 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001860 info->node);
1861 if (pos < ctxt->node_seq.length
1862 && ctxt->node_seq.buffer[pos].node == info->node) {
1863 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00001864 }
1865
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001866 /* Otherwise, we need to add new node to buffer */
1867 else {
1868 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
1869 xmlParserNodeInfo *tmp_buffer;
1870 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00001871
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001872 if (ctxt->node_seq.maximum == 0)
1873 ctxt->node_seq.maximum = 2;
1874 byte_size = (sizeof(*ctxt->node_seq.buffer) *
1875 (2 * ctxt->node_seq.maximum));
1876
1877 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00001878 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001879 else
1880 tmp_buffer =
1881 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
1882 byte_size);
1883
1884 if (tmp_buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001885 xmlErrMemory(ctxt, "failed to allocate buffer\n");
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001886 return;
1887 }
1888 ctxt->node_seq.buffer = tmp_buffer;
1889 ctxt->node_seq.maximum *= 2;
1890 }
1891
1892 /* If position is not at end, move elements out of the way */
1893 if (pos != ctxt->node_seq.length) {
1894 unsigned long i;
1895
1896 for (i = ctxt->node_seq.length; i > pos; i--)
1897 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
1898 }
1899
1900 /* Copy element and increase length */
1901 ctxt->node_seq.buffer[pos] = *info;
1902 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00001903 }
Owen Taylor3473f882001-02-23 17:55:21 +00001904}
1905
1906/************************************************************************
1907 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00001908 * Defaults settings *
1909 * *
1910 ************************************************************************/
1911/**
1912 * xmlPedanticParserDefault:
1913 * @val: int 0 or 1
1914 *
1915 * Set and return the previous value for enabling pedantic warnings.
1916 *
1917 * Returns the last value for 0 for no substitution, 1 for substitution.
1918 */
1919
1920int
1921xmlPedanticParserDefault(int val) {
1922 int old = xmlPedanticParserDefaultValue;
1923
1924 xmlPedanticParserDefaultValue = val;
1925 return(old);
1926}
1927
1928/**
1929 * xmlLineNumbersDefault:
1930 * @val: int 0 or 1
1931 *
1932 * Set and return the previous value for enabling line numbers in elements
1933 * contents. This may break on old application and is turned off by default.
1934 *
1935 * Returns the last value for 0 for no substitution, 1 for substitution.
1936 */
1937
1938int
1939xmlLineNumbersDefault(int val) {
1940 int old = xmlLineNumbersDefaultValue;
1941
1942 xmlLineNumbersDefaultValue = val;
1943 return(old);
1944}
1945
1946/**
1947 * xmlSubstituteEntitiesDefault:
1948 * @val: int 0 or 1
1949 *
1950 * Set and return the previous value for default entity support.
1951 * Initially the parser always keep entity references instead of substituting
1952 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001953 * default parser behavior
1954 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00001955 * file basis.
1956 *
1957 * Returns the last value for 0 for no substitution, 1 for substitution.
1958 */
1959
1960int
1961xmlSubstituteEntitiesDefault(int val) {
1962 int old = xmlSubstituteEntitiesDefaultValue;
1963
1964 xmlSubstituteEntitiesDefaultValue = val;
1965 return(old);
1966}
1967
1968/**
1969 * xmlKeepBlanksDefault:
1970 * @val: int 0 or 1
1971 *
1972 * Set and return the previous value for default blanks text nodes support.
1973 * The 1.x version of the parser used an heuristic to try to detect
1974 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00001975 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00001976 * using the DOM output text nodes containing those blanks were not generated.
1977 * The 2.x and later version will switch to the XML standard way and
1978 * ignorableWhitespace() are only generated when running the parser in
1979 * validating mode and when the current element doesn't allow CDATA or
1980 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001981 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00001982 * on 1.X libs and to switch back to the old mode for compatibility when
1983 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
1984 * by using xmlIsBlankNode() commodity function to detect the "empty"
1985 * nodes generated.
1986 * This value also affect autogeneration of indentation when saving code
1987 * if blanks sections are kept, indentation is not generated.
1988 *
1989 * Returns the last value for 0 for no substitution, 1 for substitution.
1990 */
1991
1992int
1993xmlKeepBlanksDefault(int val) {
1994 int old = xmlKeepBlanksDefaultValue;
1995
1996 xmlKeepBlanksDefaultValue = val;
1997 xmlIndentTreeOutput = !val;
1998 return(old);
1999}
2000