blob: 807169bd6d32f763f2e81e7a7d04756d60809cb6 [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
Daniel Veillard29b17482004-08-16 00:39:03 +0000130 * @xmlerr: the error number
Daniel Veillardce9457f2003-10-05 21:33:18 +0000131 * @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
Daniel Veillard29b17482004-08-16 00:39:03 +0000138__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
Daniel Veillarda840b692003-10-19 13:35:37 +0000139 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)
Daniel Veillard29b17482004-08-16 00:39:03 +0000145 ctxt->errNo = xmlerr;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000146 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillard29b17482004-08-16 00:39:03 +0000147 ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000148 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
Daniel Veillardce682bc2004-11-05 17:22:25 +0000284 if (in == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000285#ifdef DEBUG_INPUT
286 xmlGenericError(xmlGenericErrorContext, "Read\n");
287#endif
288 if (in->buf == NULL) return(-1);
289 if (in->base == NULL) return(-1);
290 if (in->cur == NULL) return(-1);
291 if (in->buf->buffer == NULL) return(-1);
292 if (in->buf->readcallback == NULL) return(-1);
293
294 CHECK_BUFFER(in);
295
296 used = in->cur - in->buf->buffer->content;
297 ret = xmlBufferShrink(in->buf->buffer, used);
298 if (ret > 0) {
299 in->cur -= ret;
300 in->consumed += ret;
301 }
302 ret = xmlParserInputBufferRead(in->buf, len);
303 if (in->base != in->buf->buffer->content) {
304 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000305 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000306 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000307 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000308 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000309 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000310 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000311 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000312
313 CHECK_BUFFER(in);
314
315 return(ret);
316}
317
318/**
319 * xmlParserInputGrow:
320 * @in: an XML parser input
321 * @len: an indicative size for the lookahead
322 *
323 * This function increase the input for the parser. It tries to
324 * preserve pointers to the input buffer, and keep already read data
325 *
326 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
327 * end of this entity
328 */
329int
330xmlParserInputGrow(xmlParserInputPtr in, int len) {
331 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000332 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000333
Daniel Veillardce682bc2004-11-05 17:22:25 +0000334 if (in == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000335#ifdef DEBUG_INPUT
336 xmlGenericError(xmlGenericErrorContext, "Grow\n");
337#endif
338 if (in->buf == NULL) return(-1);
339 if (in->base == NULL) return(-1);
340 if (in->cur == NULL) return(-1);
341 if (in->buf->buffer == NULL) return(-1);
342
343 CHECK_BUFFER(in);
344
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000345 indx = in->cur - in->base;
346 if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000347
348 CHECK_BUFFER(in);
349
350 return(0);
351 }
352 if (in->buf->readcallback != NULL)
353 ret = xmlParserInputBufferGrow(in->buf, len);
354 else
355 return(0);
356
357 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +0000358 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +0000359 * block, but we use it really as an integer to do some
360 * pointer arithmetic. Insure will raise it as a bug but in
361 * that specific case, that's not !
362 */
363 if (in->base != in->buf->buffer->content) {
364 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000365 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000366 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000367 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000368 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000369 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000370 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000371 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000372
373 CHECK_BUFFER(in);
374
375 return(ret);
376}
377
378/**
379 * xmlParserInputShrink:
380 * @in: an XML parser input
381 *
382 * This function removes used input for the parser.
383 */
384void
385xmlParserInputShrink(xmlParserInputPtr in) {
386 int used;
387 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000388 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000389
390#ifdef DEBUG_INPUT
391 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
392#endif
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000393 if (in == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000394 if (in->buf == NULL) return;
395 if (in->base == NULL) return;
396 if (in->cur == NULL) return;
397 if (in->buf->buffer == NULL) return;
398
399 CHECK_BUFFER(in);
400
401 used = in->cur - in->buf->buffer->content;
402 /*
403 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000404 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +0000405 */
Owen Taylor3473f882001-02-23 17:55:21 +0000406 if (used > INPUT_CHUNK) {
407 ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
408 if (ret > 0) {
409 in->cur -= ret;
410 in->consumed += ret;
411 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000412 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000413 }
414
415 CHECK_BUFFER(in);
416
417 if (in->buf->buffer->use > INPUT_CHUNK) {
418 return;
419 }
420 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
421 if (in->base != in->buf->buffer->content) {
422 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000423 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000424 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000425 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000426 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000427 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000428 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000429 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000430
431 CHECK_BUFFER(in);
432}
433
434/************************************************************************
435 * *
436 * UTF8 character input and related functions *
437 * *
438 ************************************************************************/
439
440/**
441 * xmlNextChar:
442 * @ctxt: the XML parser context
443 *
444 * Skip to the next char input char.
445 */
446
447void
Daniel Veillard77a90a72003-03-22 00:04:05 +0000448xmlNextChar(xmlParserCtxtPtr ctxt)
449{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000450 if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
451 (ctxt->input == NULL))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000452 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000453
Daniel Veillardfdc91562002-07-01 21:52:03 +0000454 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000455 if ((*ctxt->input->cur == 0) &&
456 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
457 (ctxt->instate != XML_PARSER_COMMENT)) {
458 /*
459 * If we are at the end of the current entity and
460 * the context allows it, we pop consumed entities
461 * automatically.
462 * the auto closing should be blocked in other cases
463 */
464 xmlPopInput(ctxt);
465 } else {
466 const unsigned char *cur;
467 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +0000468
Daniel Veillard77a90a72003-03-22 00:04:05 +0000469 /*
470 * 2.11 End-of-Line Handling
471 * the literal two-character sequence "#xD#xA" or a standalone
472 * literal #xD, an XML processor must pass to the application
473 * the single character #xA.
474 */
475 if (*(ctxt->input->cur) == '\n') {
476 ctxt->input->line++;
477 ctxt->input->col = 1;
478 } else
479 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +0000480
Daniel Veillard77a90a72003-03-22 00:04:05 +0000481 /*
482 * We are supposed to handle UTF8, check it's valid
483 * From rfc2044: encoding of the Unicode values on UTF-8:
484 *
485 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
486 * 0000 0000-0000 007F 0xxxxxxx
487 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
488 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
489 *
490 * Check for the 0x110000 limit too
491 */
492 cur = ctxt->input->cur;
493
494 c = *cur;
495 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000496 if (c == 0xC0)
497 goto encoding_error;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000498 if (cur[1] == 0)
499 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
500 if ((cur[1] & 0xc0) != 0x80)
501 goto encoding_error;
502 if ((c & 0xe0) == 0xe0) {
503 unsigned int val;
504
505 if (cur[2] == 0)
506 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
507 if ((cur[2] & 0xc0) != 0x80)
508 goto encoding_error;
509 if ((c & 0xf0) == 0xf0) {
510 if (cur[3] == 0)
511 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
512 if (((c & 0xf8) != 0xf0) ||
513 ((cur[3] & 0xc0) != 0x80))
514 goto encoding_error;
515 /* 4-byte code */
516 ctxt->input->cur += 4;
517 val = (cur[0] & 0x7) << 18;
518 val |= (cur[1] & 0x3f) << 12;
519 val |= (cur[2] & 0x3f) << 6;
520 val |= cur[3] & 0x3f;
521 } else {
522 /* 3-byte code */
523 ctxt->input->cur += 3;
524 val = (cur[0] & 0xf) << 12;
525 val |= (cur[1] & 0x3f) << 6;
526 val |= cur[2] & 0x3f;
527 }
528 if (((val > 0xd7ff) && (val < 0xe000)) ||
529 ((val > 0xfffd) && (val < 0x10000)) ||
530 (val >= 0x110000)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000531 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
532 "Char 0x%X out of allowed range\n",
533 val);
Daniel Veillard77a90a72003-03-22 00:04:05 +0000534 }
535 } else
536 /* 2-byte code */
537 ctxt->input->cur += 2;
538 } else
539 /* 1-byte code */
540 ctxt->input->cur++;
541
542 ctxt->nbChars++;
543 if (*ctxt->input->cur == 0)
544 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
545 }
Owen Taylor3473f882001-02-23 17:55:21 +0000546 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000547 /*
548 * Assume it's a fixed length encoding (1) with
549 * a compatible encoding for the ASCII set, since
550 * XML constructs only use < 128 chars
551 */
552
553 if (*(ctxt->input->cur) == '\n') {
554 ctxt->input->line++;
555 ctxt->input->col = 1;
556 } else
557 ctxt->input->col++;
558 ctxt->input->cur++;
559 ctxt->nbChars++;
560 if (*ctxt->input->cur == 0)
561 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +0000562 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000563 if ((*ctxt->input->cur == '%') && (!ctxt->html))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000564 xmlParserHandlePEReference(ctxt);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000565 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000566 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000567 xmlPopInput(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +0000568 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000569encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +0000570 /*
571 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000572 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000573 * declaration header. Report the error and switch the encoding
574 * to ISO-Latin-1 (if you don't like this policy, just declare the
575 * encoding !)
576 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000577 if ((ctxt == NULL) || (ctxt->input == NULL) ||
578 (ctxt->input->end - ctxt->input->cur < 4)) {
579 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
580 "Input is not proper UTF-8, indicate encoding !\n",
581 NULL, NULL);
582 } else {
583 char buffer[150];
584
585 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
586 ctxt->input->cur[0], ctxt->input->cur[1],
587 ctxt->input->cur[2], ctxt->input->cur[3]);
588 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
589 "Input is not proper UTF-8, indicate encoding !\n%s",
590 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000591 }
Daniel Veillard77a90a72003-03-22 00:04:05 +0000592 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000593 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000594 return;
595}
596
597/**
598 * xmlCurrentChar:
599 * @ctxt: the XML parser context
600 * @len: pointer to the length of the char read
601 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000602 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000603 * bytes in the input buffer. Implement the end of line normalization:
604 * 2.11 End-of-Line Handling
605 * Wherever an external parsed entity or the literal entity value
606 * of an internal parsed entity contains either the literal two-character
607 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
608 * must pass to the application the single character #xA.
609 * This behavior can conveniently be produced by normalizing all
610 * line breaks to #xA on input, before parsing.)
611 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000612 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000613 */
614
615int
616xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000617 if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000618 if (ctxt->instate == XML_PARSER_EOF)
619 return(0);
620
Daniel Veillard561b7f82002-03-20 21:55:57 +0000621 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
622 *len = 1;
623 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000624 }
625 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
626 /*
627 * We are supposed to handle UTF8, check it's valid
628 * From rfc2044: encoding of the Unicode values on UTF-8:
629 *
630 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
631 * 0000 0000-0000 007F 0xxxxxxx
632 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
633 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
634 *
635 * Check for the 0x110000 limit too
636 */
637 const unsigned char *cur = ctxt->input->cur;
638 unsigned char c;
639 unsigned int val;
640
641 c = *cur;
642 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000643 if (c == 0xC0)
644 goto encoding_error;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000645 if (cur[1] == 0)
646 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
647 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000648 goto encoding_error;
649 if ((c & 0xe0) == 0xe0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000650
651 if (cur[2] == 0)
652 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
653 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000654 goto encoding_error;
655 if ((c & 0xf0) == 0xf0) {
656 if (cur[3] == 0)
657 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000658 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +0000659 ((cur[3] & 0xc0) != 0x80))
660 goto encoding_error;
661 /* 4-byte code */
662 *len = 4;
663 val = (cur[0] & 0x7) << 18;
664 val |= (cur[1] & 0x3f) << 12;
665 val |= (cur[2] & 0x3f) << 6;
666 val |= cur[3] & 0x3f;
667 } else {
668 /* 3-byte code */
669 *len = 3;
670 val = (cur[0] & 0xf) << 12;
671 val |= (cur[1] & 0x3f) << 6;
672 val |= cur[2] & 0x3f;
673 }
674 } else {
675 /* 2-byte code */
676 *len = 2;
677 val = (cur[0] & 0x1f) << 6;
678 val |= cur[1] & 0x3f;
679 }
680 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000681 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
682 "Char 0x%X out of allowed range\n", val);
Owen Taylor3473f882001-02-23 17:55:21 +0000683 }
684 return(val);
685 } else {
686 /* 1-byte code */
687 *len = 1;
688 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000689 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000690 ctxt->nbChars++;
691 ctxt->input->cur++;
692 }
693 return(0xA);
694 }
695 return((int) *ctxt->input->cur);
696 }
697 }
698 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000699 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000700 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000701 * XML constructs only use < 128 chars
702 */
703 *len = 1;
704 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000705 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000706 ctxt->nbChars++;
707 ctxt->input->cur++;
708 }
709 return(0xA);
710 }
711 return((int) *ctxt->input->cur);
712encoding_error:
713 /*
Daniel Veillardd2ff0392002-11-22 12:28:38 +0000714 * An encoding problem may arise from a truncated input buffer
715 * splitting a character in the middle. In that case do not raise
716 * an error but return 0 to endicate an end of stream problem
717 */
718 if (ctxt->input->end - ctxt->input->cur < 4) {
719 *len = 0;
720 return(0);
721 }
722
723 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000724 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000725 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000726 * declaration header. Report the error and switch the encoding
727 * to ISO-Latin-1 (if you don't like this policy, just declare the
728 * encoding !)
729 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000730 {
731 char buffer[150];
732
733 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Daniel Veillard561b7f82002-03-20 21:55:57 +0000734 ctxt->input->cur[0], ctxt->input->cur[1],
735 ctxt->input->cur[2], ctxt->input->cur[3]);
Daniel Veillarda521d282004-11-09 14:59:59 +0000736 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
737 "Input is not proper UTF-8, indicate encoding !\n%s",
738 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000739 }
Owen Taylor3473f882001-02-23 17:55:21 +0000740 ctxt->charset = XML_CHAR_ENCODING_8859_1;
741 *len = 1;
742 return((int) *ctxt->input->cur);
743}
744
745/**
746 * xmlStringCurrentChar:
747 * @ctxt: the XML parser context
748 * @cur: pointer to the beginning of the char
749 * @len: pointer to the length of the char read
750 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000751 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000752 * bytes in the input buffer.
753 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000754 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000755 */
756
757int
Daniel Veillardd8224e02002-01-13 15:43:22 +0000758xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
759{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000760 if ((len == NULL) || (cur == NULL)) return(0);
Daniel Veillard61d80a22001-04-27 17:13:01 +0000761 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +0000762 /*
763 * We are supposed to handle UTF8, check it's valid
764 * From rfc2044: encoding of the Unicode values on UTF-8:
765 *
766 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
767 * 0000 0000-0000 007F 0xxxxxxx
768 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
769 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
770 *
771 * Check for the 0x110000 limit too
772 */
773 unsigned char c;
774 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +0000775
Daniel Veillardd8224e02002-01-13 15:43:22 +0000776 c = *cur;
777 if (c & 0x80) {
778 if ((cur[1] & 0xc0) != 0x80)
779 goto encoding_error;
780 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000781
Daniel Veillardd8224e02002-01-13 15:43:22 +0000782 if ((cur[2] & 0xc0) != 0x80)
783 goto encoding_error;
784 if ((c & 0xf0) == 0xf0) {
785 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
786 goto encoding_error;
787 /* 4-byte code */
788 *len = 4;
789 val = (cur[0] & 0x7) << 18;
790 val |= (cur[1] & 0x3f) << 12;
791 val |= (cur[2] & 0x3f) << 6;
792 val |= cur[3] & 0x3f;
793 } else {
794 /* 3-byte code */
795 *len = 3;
796 val = (cur[0] & 0xf) << 12;
797 val |= (cur[1] & 0x3f) << 6;
798 val |= cur[2] & 0x3f;
799 }
800 } else {
801 /* 2-byte code */
802 *len = 2;
803 val = (cur[0] & 0x1f) << 6;
804 val |= cur[1] & 0x3f;
805 }
806 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000807 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
808 "Char 0x%X out of allowed range\n", val);
Daniel Veillardd8224e02002-01-13 15:43:22 +0000809 }
810 return (val);
811 } else {
812 /* 1-byte code */
813 *len = 1;
814 return ((int) *cur);
815 }
Owen Taylor3473f882001-02-23 17:55:21 +0000816 }
817 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000818 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000819 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000820 * XML constructs only use < 128 chars
821 */
822 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000823 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000824encoding_error:
Daniel Veillardd8224e02002-01-13 15:43:22 +0000825
Owen Taylor3473f882001-02-23 17:55:21 +0000826 /*
Daniel Veillarda521d282004-11-09 14:59:59 +0000827 * An encoding problem may arise from a truncated input buffer
828 * splitting a character in the middle. In that case do not raise
829 * an error but return 0 to endicate an end of stream problem
830 */
831 if ((ctxt == NULL) || (ctxt->input == NULL) ||
832 (ctxt->input->end - ctxt->input->cur < 4)) {
833 *len = 0;
834 return(0);
835 }
836 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000837 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000838 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000839 * declaration header. Report the error and switch the encoding
840 * to ISO-Latin-1 (if you don't like this policy, just declare the
841 * encoding !)
842 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000843 {
844 char buffer[150];
845
846 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
847 ctxt->input->cur[0], ctxt->input->cur[1],
848 ctxt->input->cur[2], ctxt->input->cur[3]);
849 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
850 "Input is not proper UTF-8, indicate encoding !\n%s",
851 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000852 }
Owen Taylor3473f882001-02-23 17:55:21 +0000853 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000854 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000855}
856
857/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000858 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000859 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +0000860 * @val: the char value
861 *
862 * append the char value in the array
863 *
864 * Returns the number of xmlChar written
865 */
Owen Taylor3473f882001-02-23 17:55:21 +0000866int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000867xmlCopyCharMultiByte(xmlChar *out, int val) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000868 if (out == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000869 /*
870 * We are supposed to handle UTF8, check it's valid
871 * From rfc2044: encoding of the Unicode values on UTF-8:
872 *
873 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
874 * 0000 0000-0000 007F 0xxxxxxx
875 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
876 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
877 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000878 if (val >= 0x80) {
879 xmlChar *savedout = out;
880 int bits;
881 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
882 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
883 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
884 else {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000885 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000886 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Daniel Veillardce9457f2003-10-05 21:33:18 +0000887 val);
Owen Taylor3473f882001-02-23 17:55:21 +0000888 return(0);
889 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000890 for ( ; bits >= 0; bits-= 6)
891 *out++= ((val >> bits) & 0x3F) | 0x80 ;
892 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +0000893 }
894 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000895 return 1;
896}
897
898/**
899 * xmlCopyChar:
900 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000901 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000902 * @val: the char value
903 *
904 * append the char value in the array
905 *
906 * Returns the number of xmlChar written
907 */
908
909int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000910xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000911 if (out == NULL) return(0);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000912 /* the len parameter is ignored */
913 if (val >= 0x80) {
914 return(xmlCopyCharMultiByte (out, val));
915 }
916 *out = (xmlChar) val;
917 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000918}
919
920/************************************************************************
921 * *
922 * Commodity functions to switch encodings *
923 * *
924 ************************************************************************/
925
926/**
927 * xmlSwitchEncoding:
928 * @ctxt: the parser context
929 * @enc: the encoding value (number)
930 *
931 * change the input functions when discovering the character encoding
932 * of a given entity.
933 *
934 * Returns 0 in case of success, -1 otherwise
935 */
936int
937xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
938{
939 xmlCharEncodingHandlerPtr handler;
940
Daniel Veillard42595322004-11-08 10:52:06 +0000941 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000942 switch (enc) {
943 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000944 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000945 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000946 break;
947 case XML_CHAR_ENCODING_NONE:
948 /* let's assume it's UTF-8 without the XML decl */
949 ctxt->charset = XML_CHAR_ENCODING_UTF8;
950 return(0);
951 case XML_CHAR_ENCODING_UTF8:
952 /* default encoding, no conversion should be needed */
953 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +0000954
955 /*
956 * Errata on XML-1.0 June 20 2001
957 * Specific handling of the Byte Order Mark for
958 * UTF-8
959 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +0000960 if ((ctxt->input != NULL) &&
961 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +0000962 (ctxt->input->cur[1] == 0xBB) &&
963 (ctxt->input->cur[2] == 0xBF)) {
964 ctxt->input->cur += 3;
965 }
Owen Taylor3473f882001-02-23 17:55:21 +0000966 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +0000967 case XML_CHAR_ENCODING_UTF16LE:
968 case XML_CHAR_ENCODING_UTF16BE:
969 /*The raw input characters are encoded
970 *in UTF-16. As we expect this function
971 *to be called after xmlCharEncInFunc, we expect
972 *ctxt->input->cur to contain UTF-8 encoded characters.
973 *So the raw UTF16 Byte Order Mark
974 *has also been converted into
975 *an UTF-8 BOM. Let's skip that BOM.
976 */
977 if ((ctxt->input != NULL) &&
978 (ctxt->input->cur[0] == 0xEF) &&
979 (ctxt->input->cur[1] == 0xBB) &&
980 (ctxt->input->cur[2] == 0xBF)) {
981 ctxt->input->cur += 3;
982 }
983 break ;
Owen Taylor3473f882001-02-23 17:55:21 +0000984 default:
985 break;
986 }
987 handler = xmlGetCharEncodingHandler(enc);
988 if (handler == NULL) {
989 /*
990 * Default handlers.
991 */
992 switch (enc) {
993 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000994 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000995 "encoding unknown\n", NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000996 break;
997 case XML_CHAR_ENCODING_NONE:
998 /* let's assume it's UTF-8 without the XML decl */
999 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1000 return(0);
1001 case XML_CHAR_ENCODING_UTF8:
1002 case XML_CHAR_ENCODING_ASCII:
1003 /* default encoding, no conversion should be needed */
1004 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1005 return(0);
1006 case XML_CHAR_ENCODING_UTF16LE:
1007 break;
1008 case XML_CHAR_ENCODING_UTF16BE:
1009 break;
1010 case XML_CHAR_ENCODING_UCS4LE:
Daniel Veillarda840b692003-10-19 13:35:37 +00001011 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001012 "encoding not supported %s\n",
1013 BAD_CAST "USC4 little endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001014 break;
1015 case XML_CHAR_ENCODING_UCS4BE:
Daniel Veillarda840b692003-10-19 13:35:37 +00001016 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001017 "encoding not supported %s\n",
1018 BAD_CAST "USC4 big endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001019 break;
1020 case XML_CHAR_ENCODING_EBCDIC:
Daniel Veillarda840b692003-10-19 13:35:37 +00001021 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001022 "encoding not supported %s\n",
1023 BAD_CAST "EBCDIC", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001024 break;
1025 case XML_CHAR_ENCODING_UCS4_2143:
Daniel Veillarda840b692003-10-19 13:35:37 +00001026 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001027 "encoding not supported %s\n",
1028 BAD_CAST "UCS4 2143", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001029 break;
1030 case XML_CHAR_ENCODING_UCS4_3412:
Daniel Veillarda840b692003-10-19 13:35:37 +00001031 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001032 "encoding not supported %s\n",
1033 BAD_CAST "UCS4 3412", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001034 break;
1035 case XML_CHAR_ENCODING_UCS2:
Daniel Veillarda840b692003-10-19 13:35:37 +00001036 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001037 "encoding not supported %s\n",
1038 BAD_CAST "UCS2", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001039 break;
1040 case XML_CHAR_ENCODING_8859_1:
1041 case XML_CHAR_ENCODING_8859_2:
1042 case XML_CHAR_ENCODING_8859_3:
1043 case XML_CHAR_ENCODING_8859_4:
1044 case XML_CHAR_ENCODING_8859_5:
1045 case XML_CHAR_ENCODING_8859_6:
1046 case XML_CHAR_ENCODING_8859_7:
1047 case XML_CHAR_ENCODING_8859_8:
1048 case XML_CHAR_ENCODING_8859_9:
1049 /*
1050 * We used to keep the internal content in the
1051 * document encoding however this turns being unmaintainable
1052 * So xmlGetCharEncodingHandler() will return non-null
1053 * values for this now.
1054 */
1055 if ((ctxt->inputNr == 1) &&
1056 (ctxt->encoding == NULL) &&
1057 (ctxt->input->encoding != NULL)) {
1058 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1059 }
1060 ctxt->charset = enc;
1061 return(0);
1062 case XML_CHAR_ENCODING_2022_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001063 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001064 "encoding not supported %s\n",
1065 BAD_CAST "ISO-2022-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001066 break;
1067 case XML_CHAR_ENCODING_SHIFT_JIS:
Daniel Veillarda840b692003-10-19 13:35:37 +00001068 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001069 "encoding not supported %s\n",
1070 BAD_CAST "Shift_JIS", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001071 break;
1072 case XML_CHAR_ENCODING_EUC_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001073 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001074 "encoding not supported %s\n",
1075 BAD_CAST "EUC-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001076 break;
1077 }
1078 }
1079 if (handler == NULL)
1080 return(-1);
1081 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1082 return(xmlSwitchToEncoding(ctxt, handler));
1083}
1084
1085/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001086 * xmlSwitchInputEncoding:
1087 * @ctxt: the parser context
1088 * @input: the input stream
1089 * @handler: the encoding handler
1090 *
1091 * change the input functions when discovering the character encoding
1092 * of a given entity.
1093 *
1094 * Returns 0 in case of success, -1 otherwise
1095 */
1096int
1097xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1098 xmlCharEncodingHandlerPtr handler)
1099{
1100 int nbchars;
1101
1102 if (handler == NULL)
1103 return (-1);
1104 if (input == NULL)
1105 return (-1);
1106 if (input->buf != NULL) {
1107 if (input->buf->encoder != NULL) {
1108 /*
1109 * Check in case the auto encoding detetection triggered
1110 * in already.
1111 */
1112 if (input->buf->encoder == handler)
1113 return (0);
1114
1115 /*
1116 * "UTF-16" can be used for both LE and BE
1117 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1118 BAD_CAST "UTF-16", 6)) &&
1119 (!xmlStrncmp(BAD_CAST handler->name,
1120 BAD_CAST "UTF-16", 6))) {
1121 return(0);
1122 }
1123 */
1124
1125 /*
1126 * Note: this is a bit dangerous, but that's what it
1127 * takes to use nearly compatible signature for different
1128 * encodings.
1129 */
1130 xmlCharEncCloseFunc(input->buf->encoder);
1131 input->buf->encoder = handler;
1132 return (0);
1133 }
1134 input->buf->encoder = handler;
1135
1136 /*
1137 * Is there already some content down the pipe to convert ?
1138 */
1139 if ((input->buf->buffer != NULL) && (input->buf->buffer->use > 0)) {
1140 int processed;
Daniel Veillard36711902004-02-11 13:25:26 +00001141 unsigned int use;
Daniel Veillarda840b692003-10-19 13:35:37 +00001142
1143 /*
1144 * Specific handling of the Byte Order Mark for
1145 * UTF-16
1146 */
1147 if ((handler->name != NULL) &&
William M. Brack1d8c9b22004-12-25 10:14:57 +00001148 (!strcmp(handler->name, "UTF-16LE") ||
1149 !strcmp(handler->name, "UTF-16")) &&
Daniel Veillarda840b692003-10-19 13:35:37 +00001150 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1151 input->cur += 2;
1152 }
1153 if ((handler->name != NULL) &&
1154 (!strcmp(handler->name, "UTF-16BE")) &&
1155 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1156 input->cur += 2;
1157 }
1158 /*
1159 * Errata on XML-1.0 June 20 2001
1160 * Specific handling of the Byte Order Mark for
1161 * UTF-8
1162 */
1163 if ((handler->name != NULL) &&
1164 (!strcmp(handler->name, "UTF-8")) &&
1165 (input->cur[0] == 0xEF) &&
1166 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1167 input->cur += 3;
1168 }
1169
1170 /*
1171 * Shrink the current input buffer.
1172 * Move it as the raw buffer and create a new input buffer
1173 */
1174 processed = input->cur - input->base;
1175 xmlBufferShrink(input->buf->buffer, processed);
1176 input->buf->raw = input->buf->buffer;
1177 input->buf->buffer = xmlBufferCreate();
Daniel Veillard36711902004-02-11 13:25:26 +00001178 input->buf->rawconsumed = processed;
1179 use = input->buf->raw->use;
Daniel Veillarda840b692003-10-19 13:35:37 +00001180
1181 if (ctxt->html) {
1182 /*
1183 * convert as much as possible of the buffer
1184 */
1185 nbchars = xmlCharEncInFunc(input->buf->encoder,
1186 input->buf->buffer,
1187 input->buf->raw);
1188 } else {
1189 /*
1190 * convert just enough to get
1191 * '<?xml version="1.0" encoding="xxx"?>'
1192 * parsed with the autodetected encoding
1193 * into the parser reading buffer.
1194 */
1195 nbchars = xmlCharEncFirstLine(input->buf->encoder,
1196 input->buf->buffer,
1197 input->buf->raw);
1198 }
1199 if (nbchars < 0) {
1200 xmlErrInternal(ctxt,
1201 "switching encoding: encoder error\n",
1202 NULL);
1203 return (-1);
1204 }
Daniel Veillard36711902004-02-11 13:25:26 +00001205 input->buf->rawconsumed += use - input->buf->raw->use;
Daniel Veillarda840b692003-10-19 13:35:37 +00001206 input->base = input->cur = input->buf->buffer->content;
1207 input->end = &input->base[input->buf->buffer->use];
1208
1209 }
1210 return (0);
1211 } else {
1212 if ((input->length == 0) || (input->buf == NULL)) {
1213 /*
1214 * When parsing a static memory array one must know the
1215 * size to be able to convert the buffer.
1216 */
1217 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1218 return (-1);
1219 } else {
1220 int processed;
1221
1222 /*
1223 * Shrink the current input buffer.
1224 * Move it as the raw buffer and create a new input buffer
1225 */
1226 processed = input->cur - input->base;
1227
1228 input->buf->raw = xmlBufferCreate();
1229 xmlBufferAdd(input->buf->raw, input->cur,
1230 input->length - processed);
1231 input->buf->buffer = xmlBufferCreate();
1232
1233 /*
1234 * convert as much as possible of the raw input
1235 * to the parser reading buffer.
1236 */
1237 nbchars = xmlCharEncInFunc(input->buf->encoder,
1238 input->buf->buffer,
1239 input->buf->raw);
1240 if (nbchars < 0) {
1241 xmlErrInternal(ctxt,
1242 "switching encoding: encoder error\n",
1243 NULL);
1244 return (-1);
1245 }
1246
1247 /*
1248 * Conversion succeeded, get rid of the old buffer
1249 */
1250 if ((input->free != NULL) && (input->base != NULL))
1251 input->free((xmlChar *) input->base);
1252 input->base = input->cur = input->buf->buffer->content;
1253 input->end = &input->base[input->buf->buffer->use];
1254 }
1255 }
1256 return (0);
1257}
1258
1259/**
Owen Taylor3473f882001-02-23 17:55:21 +00001260 * xmlSwitchToEncoding:
1261 * @ctxt: the parser context
1262 * @handler: the encoding handler
1263 *
1264 * change the input functions when discovering the character encoding
1265 * of a given entity.
1266 *
1267 * Returns 0 in case of success, -1 otherwise
1268 */
1269int
1270xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1271{
Owen Taylor3473f882001-02-23 17:55:21 +00001272 if (handler != NULL) {
1273 if (ctxt->input != NULL) {
Daniel Veillarda840b692003-10-19 13:35:37 +00001274 xmlSwitchInputEncoding(ctxt, ctxt->input, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00001275 } else {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001276 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1277 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001278 return(-1);
1279 }
1280 /*
1281 * The parsing is now done in UTF8 natively
1282 */
1283 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1284 } else
1285 return(-1);
1286 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001287}
1288
1289/************************************************************************
1290 * *
1291 * Commodity functions to handle entities processing *
1292 * *
1293 ************************************************************************/
1294
1295/**
1296 * xmlFreeInputStream:
1297 * @input: an xmlParserInputPtr
1298 *
1299 * Free up an input stream.
1300 */
1301void
1302xmlFreeInputStream(xmlParserInputPtr input) {
1303 if (input == NULL) return;
1304
1305 if (input->filename != NULL) xmlFree((char *) input->filename);
1306 if (input->directory != NULL) xmlFree((char *) input->directory);
1307 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1308 if (input->version != NULL) xmlFree((char *) input->version);
1309 if ((input->free != NULL) && (input->base != NULL))
1310 input->free((xmlChar *) input->base);
1311 if (input->buf != NULL)
1312 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001313 xmlFree(input);
1314}
1315
1316/**
1317 * xmlNewInputStream:
1318 * @ctxt: an XML parser context
1319 *
1320 * Create a new input stream structure
1321 * Returns the new input stream or NULL
1322 */
1323xmlParserInputPtr
1324xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1325 xmlParserInputPtr input;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001326 static int id = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001327
1328 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1329 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001330 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001331 return(NULL);
1332 }
1333 memset(input, 0, sizeof(xmlParserInput));
1334 input->line = 1;
1335 input->col = 1;
1336 input->standalone = -1;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001337 /*
1338 * we don't care about thread reentrancy unicity for a single
1339 * parser context (and hence thread) is sufficient.
1340 */
1341 input->id = id++;
Owen Taylor3473f882001-02-23 17:55:21 +00001342 return(input);
1343}
1344
1345/**
1346 * xmlNewIOInputStream:
1347 * @ctxt: an XML parser context
1348 * @input: an I/O Input
1349 * @enc: the charset encoding if known
1350 *
1351 * Create a new input stream structure encapsulating the @input into
1352 * a stream suitable for the parser.
1353 *
1354 * Returns the new input stream or NULL
1355 */
1356xmlParserInputPtr
1357xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1358 xmlCharEncoding enc) {
1359 xmlParserInputPtr inputStream;
1360
Daniel Veillard42595322004-11-08 10:52:06 +00001361 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001362 if (xmlParserDebugEntities)
1363 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1364 inputStream = xmlNewInputStream(ctxt);
1365 if (inputStream == NULL) {
1366 return(NULL);
1367 }
1368 inputStream->filename = NULL;
1369 inputStream->buf = input;
1370 inputStream->base = inputStream->buf->buffer->content;
1371 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001372 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001373 if (enc != XML_CHAR_ENCODING_NONE) {
1374 xmlSwitchEncoding(ctxt, enc);
1375 }
1376
1377 return(inputStream);
1378}
1379
1380/**
1381 * xmlNewEntityInputStream:
1382 * @ctxt: an XML parser context
1383 * @entity: an Entity pointer
1384 *
1385 * Create a new input stream based on an xmlEntityPtr
1386 *
1387 * Returns the new input stream or NULL
1388 */
1389xmlParserInputPtr
1390xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1391 xmlParserInputPtr input;
1392
1393 if (entity == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001394 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1395 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001396 return(NULL);
1397 }
1398 if (xmlParserDebugEntities)
1399 xmlGenericError(xmlGenericErrorContext,
1400 "new input from entity: %s\n", entity->name);
1401 if (entity->content == NULL) {
1402 switch (entity->etype) {
1403 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001404 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1405 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001406 break;
1407 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1408 case XML_EXTERNAL_PARAMETER_ENTITY:
1409 return(xmlLoadExternalEntity((char *) entity->URI,
1410 (char *) entity->ExternalID, ctxt));
1411 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001412 xmlErrInternal(ctxt,
1413 "Internal entity %s without content !\n",
1414 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001415 break;
1416 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001417 xmlErrInternal(ctxt,
1418 "Internal parameter entity %s without content !\n",
1419 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001420 break;
1421 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001422 xmlErrInternal(ctxt,
1423 "Predefined entity %s without content !\n",
1424 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001425 break;
1426 }
1427 return(NULL);
1428 }
1429 input = xmlNewInputStream(ctxt);
1430 if (input == NULL) {
1431 return(NULL);
1432 }
1433 input->filename = (char *) entity->URI;
1434 input->base = entity->content;
1435 input->cur = entity->content;
1436 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001437 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001438 return(input);
1439}
1440
1441/**
1442 * xmlNewStringInputStream:
1443 * @ctxt: an XML parser context
1444 * @buffer: an memory buffer
1445 *
1446 * Create a new input stream based on a memory buffer.
1447 * Returns the new input stream
1448 */
1449xmlParserInputPtr
1450xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1451 xmlParserInputPtr input;
1452
1453 if (buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001454 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1455 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001456 return(NULL);
1457 }
1458 if (xmlParserDebugEntities)
1459 xmlGenericError(xmlGenericErrorContext,
1460 "new fixed input: %.30s\n", buffer);
1461 input = xmlNewInputStream(ctxt);
1462 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001463 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001464 return(NULL);
1465 }
1466 input->base = buffer;
1467 input->cur = buffer;
1468 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001469 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001470 return(input);
1471}
1472
1473/**
1474 * xmlNewInputFromFile:
1475 * @ctxt: an XML parser context
1476 * @filename: the filename to use as entity
1477 *
Daniel Veillarda840b692003-10-19 13:35:37 +00001478 * Create a new input stream based on a file or an URL.
Owen Taylor3473f882001-02-23 17:55:21 +00001479 *
1480 * Returns the new input stream or NULL in case of error
1481 */
1482xmlParserInputPtr
1483xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1484 xmlParserInputBufferPtr buf;
1485 xmlParserInputPtr inputStream;
1486 char *directory = NULL;
1487 xmlChar *URI = NULL;
1488
1489 if (xmlParserDebugEntities)
1490 xmlGenericError(xmlGenericErrorContext,
1491 "new input from file: %s\n", filename);
1492 if (ctxt == NULL) return(NULL);
1493 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001494 if (buf == NULL) {
1495 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1496 (const char *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001497 return(NULL);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001498 }
Owen Taylor3473f882001-02-23 17:55:21 +00001499
Owen Taylor3473f882001-02-23 17:55:21 +00001500 inputStream = xmlNewInputStream(ctxt);
1501 if (inputStream == NULL) {
1502 if (directory != NULL) xmlFree((char *) directory);
1503 if (URI != NULL) xmlFree((char *) URI);
1504 return(NULL);
1505 }
Daniel Veillarda840b692003-10-19 13:35:37 +00001506 inputStream->buf = buf;
1507 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1508 if (inputStream == NULL)
1509 return(NULL);
1510
1511 if (inputStream->filename == NULL)
1512 URI = xmlStrdup((xmlChar *) filename);
1513 else
1514 URI = xmlStrdup((xmlChar *) inputStream->filename);
1515 directory = xmlParserGetDirectory((const char *) URI);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001516 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001517 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001518 inputStream->directory = directory;
Owen Taylor3473f882001-02-23 17:55:21 +00001519
1520 inputStream->base = inputStream->buf->buffer->content;
1521 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001522 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001523 if ((ctxt->directory == NULL) && (directory != NULL))
1524 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1525 return(inputStream);
1526}
1527
1528/************************************************************************
1529 * *
1530 * Commodity functions to handle parser contexts *
1531 * *
1532 ************************************************************************/
1533
1534/**
1535 * xmlInitParserCtxt:
1536 * @ctxt: an XML parser context
1537 *
1538 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539 *
1540 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001541 */
1542
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001543int
Owen Taylor3473f882001-02-23 17:55:21 +00001544xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1545{
Daniel Veillarda521d282004-11-09 14:59:59 +00001546 xmlParserInputPtr input;
1547
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001548 if(ctxt==NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001549 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001550 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001551 }
1552
Owen Taylor3473f882001-02-23 17:55:21 +00001553 xmlDefaultSAXHandlerInit();
1554
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001555 if (ctxt->dict == NULL)
1556 ctxt->dict = xmlDictCreate();
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001557 if (ctxt->dict == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001558 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001559 return(-1);
1560 }
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001561 if (ctxt->sax == NULL)
1562 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
William M. Brack8b2c7f12002-11-22 05:07:29 +00001563 if (ctxt->sax == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001564 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001565 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001566 }
1567 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001568 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001569
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001570 ctxt->maxatts = 0;
1571 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001572 /* Allocate the Input stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001573 if (ctxt->inputTab == NULL) {
1574 ctxt->inputTab = (xmlParserInputPtr *)
1575 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1576 ctxt->inputMax = 5;
1577 }
Owen Taylor3473f882001-02-23 17:55:21 +00001578 if (ctxt->inputTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001579 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001580 ctxt->inputNr = 0;
1581 ctxt->inputMax = 0;
1582 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001583 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001584 }
Daniel Veillarda521d282004-11-09 14:59:59 +00001585 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1586 xmlFreeInputStream(input);
1587 }
Owen Taylor3473f882001-02-23 17:55:21 +00001588 ctxt->inputNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001589 ctxt->input = NULL;
1590
1591 ctxt->version = NULL;
1592 ctxt->encoding = NULL;
1593 ctxt->standalone = -1;
1594 ctxt->hasExternalSubset = 0;
1595 ctxt->hasPErefs = 0;
1596 ctxt->html = 0;
1597 ctxt->external = 0;
1598 ctxt->instate = XML_PARSER_START;
1599 ctxt->token = 0;
1600 ctxt->directory = NULL;
1601
1602 /* Allocate the Node stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001603 if (ctxt->nodeTab == NULL) {
1604 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1605 ctxt->nodeMax = 10;
1606 }
Owen Taylor3473f882001-02-23 17:55:21 +00001607 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001608 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001609 ctxt->nodeNr = 0;
1610 ctxt->nodeMax = 0;
1611 ctxt->node = NULL;
1612 ctxt->inputNr = 0;
1613 ctxt->inputMax = 0;
1614 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001615 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001616 }
1617 ctxt->nodeNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001618 ctxt->node = NULL;
1619
1620 /* Allocate the Name stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001621 if (ctxt->nameTab == NULL) {
1622 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1623 ctxt->nameMax = 10;
1624 }
Owen Taylor3473f882001-02-23 17:55:21 +00001625 if (ctxt->nameTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001626 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001627 ctxt->nodeNr = 0;
1628 ctxt->nodeMax = 0;
1629 ctxt->node = NULL;
1630 ctxt->inputNr = 0;
1631 ctxt->inputMax = 0;
1632 ctxt->input = NULL;
1633 ctxt->nameNr = 0;
1634 ctxt->nameMax = 0;
1635 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001636 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001637 }
1638 ctxt->nameNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001639 ctxt->name = NULL;
1640
1641 /* Allocate the space stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001642 if (ctxt->spaceTab == NULL) {
1643 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1644 ctxt->spaceMax = 10;
1645 }
Owen Taylor3473f882001-02-23 17:55:21 +00001646 if (ctxt->spaceTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001647 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001648 ctxt->nodeNr = 0;
1649 ctxt->nodeMax = 0;
1650 ctxt->node = NULL;
1651 ctxt->inputNr = 0;
1652 ctxt->inputMax = 0;
1653 ctxt->input = NULL;
1654 ctxt->nameNr = 0;
1655 ctxt->nameMax = 0;
1656 ctxt->name = NULL;
1657 ctxt->spaceNr = 0;
1658 ctxt->spaceMax = 0;
1659 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001660 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001661 }
1662 ctxt->spaceNr = 1;
1663 ctxt->spaceMax = 10;
1664 ctxt->spaceTab[0] = -1;
1665 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00001666 ctxt->userData = ctxt;
1667 ctxt->myDoc = NULL;
1668 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00001669 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001670 ctxt->valid = 1;
1671 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1672 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1673 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00001674 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00001675 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00001676 if (ctxt->keepBlanks == 0)
Daniel Veillard11476b42003-09-26 14:51:39 +00001677 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00001678
Daniel Veillardeff45a92004-10-29 12:10:55 +00001679 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
Owen Taylor3473f882001-02-23 17:55:21 +00001680 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001681 ctxt->vctxt.error = xmlParserValidityError;
1682 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00001683 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00001684 if (xmlGetWarningsDefaultValue == 0)
1685 ctxt->vctxt.warning = NULL;
1686 else
1687 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00001688 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001689 }
1690 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1691 ctxt->record_info = 0;
1692 ctxt->nbChars = 0;
1693 ctxt->checkIndex = 0;
1694 ctxt->inSubset = 0;
1695 ctxt->errNo = XML_ERR_OK;
1696 ctxt->depth = 0;
1697 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001698 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001699 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001700 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001701}
1702
1703/**
1704 * xmlFreeParserCtxt:
1705 * @ctxt: an XML parser context
1706 *
1707 * Free all the memory used by a parser context. However the parsed
1708 * document in ctxt->myDoc is not freed.
1709 */
1710
1711void
1712xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1713{
1714 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00001715
1716 if (ctxt == NULL) return;
1717
1718 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1719 xmlFreeInputStream(input);
1720 }
Owen Taylor3473f882001-02-23 17:55:21 +00001721 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001722 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001723 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1724 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1725 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1726 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001727 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1728 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00001729#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00001730 if ((ctxt->sax != NULL) &&
1731 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00001732#else
1733 if (ctxt->sax != NULL)
1734#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001735 xmlFree(ctxt->sax);
1736 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00001737 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001738 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001739 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00001740 if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001741 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1742 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1743 if (ctxt->attsDefault != NULL)
1744 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001745 if (ctxt->attsSpecial != NULL)
1746 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001747 if (ctxt->freeElems != NULL) {
1748 xmlNodePtr cur, next;
1749
1750 cur = ctxt->freeElems;
1751 while (cur != NULL) {
1752 next = cur->next;
1753 xmlFree(cur);
1754 cur = next;
1755 }
1756 }
1757 if (ctxt->freeAttrs != NULL) {
1758 xmlAttrPtr cur, next;
1759
1760 cur = ctxt->freeAttrs;
1761 while (cur != NULL) {
1762 next = cur->next;
1763 xmlFree(cur);
1764 cur = next;
1765 }
1766 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001767 /*
1768 * cleanup the error strings
1769 */
1770 if (ctxt->lastError.message != NULL)
1771 xmlFree(ctxt->lastError.message);
1772 if (ctxt->lastError.file != NULL)
1773 xmlFree(ctxt->lastError.file);
1774 if (ctxt->lastError.str1 != NULL)
1775 xmlFree(ctxt->lastError.str1);
1776 if (ctxt->lastError.str2 != NULL)
1777 xmlFree(ctxt->lastError.str2);
1778 if (ctxt->lastError.str3 != NULL)
1779 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001780
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001781#ifdef LIBXML_CATALOG_ENABLED
1782 if (ctxt->catalogs != NULL)
1783 xmlCatalogFreeLocal(ctxt->catalogs);
1784#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001785 xmlFree(ctxt);
1786}
1787
1788/**
1789 * xmlNewParserCtxt:
1790 *
1791 * Allocate and initialize a new parser context.
1792 *
1793 * Returns the xmlParserCtxtPtr or NULL
1794 */
1795
1796xmlParserCtxtPtr
1797xmlNewParserCtxt()
1798{
1799 xmlParserCtxtPtr ctxt;
1800
1801 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1802 if (ctxt == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001803 xmlErrMemory(NULL, "cannot allocate parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001804 return(NULL);
1805 }
1806 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001807 if (xmlInitParserCtxt(ctxt) < 0) {
1808 xmlFreeParserCtxt(ctxt);
1809 return(NULL);
1810 }
Owen Taylor3473f882001-02-23 17:55:21 +00001811 return(ctxt);
1812}
1813
1814/************************************************************************
1815 * *
1816 * Handling of node informations *
1817 * *
1818 ************************************************************************/
1819
1820/**
1821 * xmlClearParserCtxt:
1822 * @ctxt: an XML parser context
1823 *
1824 * Clear (release owned resources) and reinitialize a parser context
1825 */
1826
1827void
1828xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1829{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001830 if (ctxt==NULL)
1831 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001832 xmlClearNodeInfoSeq(&ctxt->node_seq);
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001833 xmlCtxtReset(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001834}
1835
Daniel Veillardce682bc2004-11-05 17:22:25 +00001836
Owen Taylor3473f882001-02-23 17:55:21 +00001837/**
1838 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001839 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00001840 * @node: an XML node within the tree
1841 *
1842 * Find the parser node info struct for a given node
1843 *
1844 * Returns an xmlParserNodeInfo block pointer or NULL
1845 */
Daniel Veillardce682bc2004-11-05 17:22:25 +00001846const xmlParserNodeInfo *
1847xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001848{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001849 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001850
Daniel Veillardce682bc2004-11-05 17:22:25 +00001851 if ((ctx == NULL) || (node == NULL))
1852 return (NULL);
1853 /* Find position where node should be at */
1854 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
1855 if (pos < ctx->node_seq.length
1856 && ctx->node_seq.buffer[pos].node == node)
1857 return &ctx->node_seq.buffer[pos];
1858 else
1859 return NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001860}
1861
1862
1863/**
1864 * xmlInitNodeInfoSeq:
1865 * @seq: a node info sequence pointer
1866 *
1867 * -- Initialize (set to initial state) node info sequence
1868 */
1869void
1870xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1871{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001872 if (seq == NULL)
1873 return;
1874 seq->length = 0;
1875 seq->maximum = 0;
1876 seq->buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001877}
1878
1879/**
1880 * xmlClearNodeInfoSeq:
1881 * @seq: a node info sequence pointer
1882 *
1883 * -- Clear (release memory and reinitialize) node
1884 * info sequence
1885 */
1886void
1887xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1888{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001889 if (seq == NULL)
1890 return;
1891 if (seq->buffer != NULL)
1892 xmlFree(seq->buffer);
1893 xmlInitNodeInfoSeq(seq);
Owen Taylor3473f882001-02-23 17:55:21 +00001894}
1895
Owen Taylor3473f882001-02-23 17:55:21 +00001896/**
1897 * xmlParserFindNodeInfoIndex:
1898 * @seq: a node info sequence pointer
1899 * @node: an XML node pointer
1900 *
1901 *
1902 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1903 * the given node is or should be at in a sorted sequence
1904 *
1905 * Returns a long indicating the position of the record
1906 */
Daniel Veillardce682bc2004-11-05 17:22:25 +00001907unsigned long
1908xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1909 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001910{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001911 unsigned long upper, lower, middle;
1912 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001913
Daniel Veillardce682bc2004-11-05 17:22:25 +00001914 if ((seq == NULL) || (node == NULL))
1915 return (-1);
1916
1917 /* Do a binary search for the key */
1918 lower = 1;
1919 upper = seq->length;
1920 middle = 0;
1921 while (lower <= upper && !found) {
1922 middle = lower + (upper - lower) / 2;
1923 if (node == seq->buffer[middle - 1].node)
1924 found = 1;
1925 else if (node < seq->buffer[middle - 1].node)
1926 upper = middle - 1;
1927 else
1928 lower = middle + 1;
1929 }
1930
1931 /* Return position */
1932 if (middle == 0 || seq->buffer[middle - 1].node < node)
1933 return middle;
Owen Taylor3473f882001-02-23 17:55:21 +00001934 else
Daniel Veillardce682bc2004-11-05 17:22:25 +00001935 return middle - 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001936}
1937
1938
1939/**
1940 * xmlParserAddNodeInfo:
1941 * @ctxt: an XML parser context
1942 * @info: a node info sequence pointer
1943 *
1944 * Insert node info record into the sorted sequence
1945 */
1946void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001947xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00001948 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00001949{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001950 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001951
Daniel Veillardce682bc2004-11-05 17:22:25 +00001952 if ((ctxt == NULL) || (info == NULL)) return;
1953
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001954 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00001955 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001956 info->node);
1957 if (pos < ctxt->node_seq.length
1958 && ctxt->node_seq.buffer[pos].node == info->node) {
1959 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00001960 }
1961
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001962 /* Otherwise, we need to add new node to buffer */
1963 else {
1964 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
1965 xmlParserNodeInfo *tmp_buffer;
1966 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00001967
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001968 if (ctxt->node_seq.maximum == 0)
1969 ctxt->node_seq.maximum = 2;
1970 byte_size = (sizeof(*ctxt->node_seq.buffer) *
1971 (2 * ctxt->node_seq.maximum));
1972
1973 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00001974 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001975 else
1976 tmp_buffer =
1977 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
1978 byte_size);
1979
1980 if (tmp_buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001981 xmlErrMemory(ctxt, "failed to allocate buffer\n");
Daniel Veillardc8c7be42002-01-23 17:53:44 +00001982 return;
1983 }
1984 ctxt->node_seq.buffer = tmp_buffer;
1985 ctxt->node_seq.maximum *= 2;
1986 }
1987
1988 /* If position is not at end, move elements out of the way */
1989 if (pos != ctxt->node_seq.length) {
1990 unsigned long i;
1991
1992 for (i = ctxt->node_seq.length; i > pos; i--)
1993 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
1994 }
1995
1996 /* Copy element and increase length */
1997 ctxt->node_seq.buffer[pos] = *info;
1998 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00001999 }
Owen Taylor3473f882001-02-23 17:55:21 +00002000}
2001
2002/************************************************************************
2003 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00002004 * Defaults settings *
2005 * *
2006 ************************************************************************/
2007/**
2008 * xmlPedanticParserDefault:
2009 * @val: int 0 or 1
2010 *
2011 * Set and return the previous value for enabling pedantic warnings.
2012 *
2013 * Returns the last value for 0 for no substitution, 1 for substitution.
2014 */
2015
2016int
2017xmlPedanticParserDefault(int val) {
2018 int old = xmlPedanticParserDefaultValue;
2019
2020 xmlPedanticParserDefaultValue = val;
2021 return(old);
2022}
2023
2024/**
2025 * xmlLineNumbersDefault:
2026 * @val: int 0 or 1
2027 *
2028 * Set and return the previous value for enabling line numbers in elements
2029 * contents. This may break on old application and is turned off by default.
2030 *
2031 * Returns the last value for 0 for no substitution, 1 for substitution.
2032 */
2033
2034int
2035xmlLineNumbersDefault(int val) {
2036 int old = xmlLineNumbersDefaultValue;
2037
2038 xmlLineNumbersDefaultValue = val;
2039 return(old);
2040}
2041
2042/**
2043 * xmlSubstituteEntitiesDefault:
2044 * @val: int 0 or 1
2045 *
2046 * Set and return the previous value for default entity support.
2047 * Initially the parser always keep entity references instead of substituting
2048 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002049 * default parser behavior
2050 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00002051 * file basis.
2052 *
2053 * Returns the last value for 0 for no substitution, 1 for substitution.
2054 */
2055
2056int
2057xmlSubstituteEntitiesDefault(int val) {
2058 int old = xmlSubstituteEntitiesDefaultValue;
2059
2060 xmlSubstituteEntitiesDefaultValue = val;
2061 return(old);
2062}
2063
2064/**
2065 * xmlKeepBlanksDefault:
2066 * @val: int 0 or 1
2067 *
2068 * Set and return the previous value for default blanks text nodes support.
2069 * The 1.x version of the parser used an heuristic to try to detect
2070 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00002071 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00002072 * using the DOM output text nodes containing those blanks were not generated.
2073 * The 2.x and later version will switch to the XML standard way and
2074 * ignorableWhitespace() are only generated when running the parser in
2075 * validating mode and when the current element doesn't allow CDATA or
2076 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002077 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002078 * on 1.X libs and to switch back to the old mode for compatibility when
2079 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2080 * by using xmlIsBlankNode() commodity function to detect the "empty"
2081 * nodes generated.
2082 * This value also affect autogeneration of indentation when saving code
2083 * if blanks sections are kept, indentation is not generated.
2084 *
2085 * Returns the last value for 0 for no substitution, 1 for substitution.
2086 */
2087
2088int
2089xmlKeepBlanksDefault(int val) {
2090 int old = xmlKeepBlanksDefaultValue;
2091
2092 xmlKeepBlanksDefaultValue = val;
2093 xmlIndentTreeOutput = !val;
2094 return(old);
2095}
2096