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