blob: cbcfde0e6e713d5104ed4f4f9c8991a57a1794da [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
Nick Wellnhofere3890542017-10-09 00:20:01 +020013#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
Nick Wellnhofercb5541c2017-11-13 17:08:38 +010035#ifdef LIBXML_ZLIB_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000036#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
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -080058#define CUR(ctxt) ctxt->input->cur
59#define END(ctxt) ctxt->input->end
60#define VALID_CTXT(ctxt) (CUR(ctxt) <= END(ctxt))
61
Daniel Veillard768eb3b2012-07-16 14:19:49 +080062#include "buf.h"
63#include "enc.h"
64
Daniel Veillarda53c6882001-07-25 17:18:57 +000065/*
66 * Various global defaults for parsing
67 */
Owen Taylor3473f882001-02-23 17:55:21 +000068
Daniel Veillard5e2dace2001-07-18 19:30:27 +000069/**
Owen Taylor3473f882001-02-23 17:55:21 +000070 * xmlCheckVersion:
71 * @version: the include version number
72 *
73 * check the compiled lib version against the include one.
74 * This can warn or immediately kill the application
75 */
76void
77xmlCheckVersion(int version) {
78 int myversion = (int) LIBXML_VERSION;
79
Daniel Veillard6f350292001-10-14 09:56:15 +000080 xmlInitParser();
Daniel Veillard4de4d3b2001-05-07 20:50:47 +000081
Owen Taylor3473f882001-02-23 17:55:21 +000082 if ((myversion / 10000) != (version / 10000)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +080083 xmlGenericError(xmlGenericErrorContext,
Owen Taylor3473f882001-02-23 17:55:21 +000084 "Fatal: program compiled against libxml %d using libxml %d\n",
85 (version / 10000), (myversion / 10000));
Daniel Veillardf8e3db02012-09-11 13:26:36 +080086 fprintf(stderr,
Daniel Veillardc69e0b12001-11-20 08:35:07 +000087 "Fatal: program compiled against libxml %d using libxml %d\n",
88 (version / 10000), (myversion / 10000));
Owen Taylor3473f882001-02-23 17:55:21 +000089 }
90 if ((myversion / 100) < (version / 100)) {
Daniel Veillardf8e3db02012-09-11 13:26:36 +080091 xmlGenericError(xmlGenericErrorContext,
Owen Taylor3473f882001-02-23 17:55:21 +000092 "Warning: program compiled against libxml %d using older %d\n",
93 (version / 100), (myversion / 100));
94 }
95}
96
Daniel Veillardce9457f2003-10-05 21:33:18 +000097
98/************************************************************************
99 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800100 * Some factorized error routines *
Daniel Veillardce9457f2003-10-05 21:33:18 +0000101 * *
102 ************************************************************************/
103
104
105/**
106 * xmlErrMemory:
107 * @ctxt: an XML parser context
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700108 * @extra: extra information
Daniel Veillardce9457f2003-10-05 21:33:18 +0000109 *
110 * Handle a redefinition of attribute error
111 */
112void
113xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
114{
Daniel Veillard157fee02003-10-31 10:36:03 +0000115 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
116 (ctxt->instate == XML_PARSER_EOF))
117 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000118 if (ctxt != NULL) {
119 ctxt->errNo = XML_ERR_NO_MEMORY;
120 ctxt->instate = XML_PARSER_EOF;
121 ctxt->disableSAX = 1;
122 }
123 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000124 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000125 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
126 NULL, NULL, 0, 0,
127 "Memory allocation failed : %s\n", extra);
128 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000129 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000130 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
131 NULL, NULL, 0, 0, "Memory allocation failed\n");
132}
133
134/**
Daniel Veillarda840b692003-10-19 13:35:37 +0000135 * __xmlErrEncoding:
Daniel Veillardce9457f2003-10-05 21:33:18 +0000136 * @ctxt: an XML parser context
Daniel Veillard29b17482004-08-16 00:39:03 +0000137 * @xmlerr: the error number
Daniel Veillardce9457f2003-10-05 21:33:18 +0000138 * @msg: the error message
139 * @str1: an string info
140 * @str2: an string info
141 *
142 * Handle an encoding error
143 */
Daniel Veillarda840b692003-10-19 13:35:37 +0000144void
Daniel Veillard29b17482004-08-16 00:39:03 +0000145__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
Daniel Veillarda840b692003-10-19 13:35:37 +0000146 const char *msg, const xmlChar * str1, const xmlChar * str2)
Daniel Veillardce9457f2003-10-05 21:33:18 +0000147{
Daniel Veillard157fee02003-10-31 10:36:03 +0000148 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
149 (ctxt->instate == XML_PARSER_EOF))
150 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000151 if (ctxt != NULL)
Daniel Veillard29b17482004-08-16 00:39:03 +0000152 ctxt->errNo = xmlerr;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000153 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillard29b17482004-08-16 00:39:03 +0000154 ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000155 NULL, 0, (const char *) str1, (const char *) str2,
156 NULL, 0, 0, msg, str1, str2);
157 if (ctxt != NULL) {
158 ctxt->wellFormed = 0;
159 if (ctxt->recovery == 0)
160 ctxt->disableSAX = 1;
161 }
162}
163
164/**
165 * xmlErrInternal:
166 * @ctxt: an XML parser context
167 * @msg: the error message
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700168 * @str: error information
Daniel Veillardce9457f2003-10-05 21:33:18 +0000169 *
170 * Handle an internal error
171 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800172static void LIBXML_ATTR_FORMAT(2,0)
Daniel Veillardce9457f2003-10-05 21:33:18 +0000173xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
174{
Daniel Veillard157fee02003-10-31 10:36:03 +0000175 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
176 (ctxt->instate == XML_PARSER_EOF))
177 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000178 if (ctxt != NULL)
179 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000180 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000181 ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
182 XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
183 0, 0, msg, str);
184 if (ctxt != NULL) {
185 ctxt->wellFormed = 0;
186 if (ctxt->recovery == 0)
187 ctxt->disableSAX = 1;
188 }
189}
190
191/**
192 * xmlErrEncodingInt:
193 * @ctxt: an XML parser context
194 * @error: the error number
195 * @msg: the error message
196 * @val: an integer value
197 *
198 * n encoding error
199 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800200static void LIBXML_ATTR_FORMAT(3,0)
Daniel Veillardce9457f2003-10-05 21:33:18 +0000201xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
202 const char *msg, int val)
203{
Daniel Veillard157fee02003-10-31 10:36:03 +0000204 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
205 (ctxt->instate == XML_PARSER_EOF))
206 return;
Daniel Veillardce9457f2003-10-05 21:33:18 +0000207 if (ctxt != NULL)
208 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000209 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000210 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
211 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
212 if (ctxt != NULL) {
213 ctxt->wellFormed = 0;
214 if (ctxt->recovery == 0)
215 ctxt->disableSAX = 1;
216 }
217}
218
Owen Taylor3473f882001-02-23 17:55:21 +0000219/**
220 * xmlIsLetter:
221 * @c: an unicode character (int)
222 *
223 * Check whether the character is allowed by the production
224 * [84] Letter ::= BaseChar | Ideographic
225 *
226 * Returns 0 if not, non-zero otherwise
227 */
228int
229xmlIsLetter(int c) {
230 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
231}
232
Owen Taylor3473f882001-02-23 17:55:21 +0000233/************************************************************************
234 * *
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800235 * Input handling functions for progressive parsing *
Owen Taylor3473f882001-02-23 17:55:21 +0000236 * *
237 ************************************************************************/
238
239/* #define DEBUG_INPUT */
240/* #define DEBUG_STACK */
241/* #define DEBUG_PUSH */
242
243
244/* we need to keep enough input to show errors in context */
245#define LINE_LEN 80
246
247#ifdef DEBUG_INPUT
248#define CHECK_BUFFER(in) check_buffer(in)
249
Daniel Veillard01c13b52002-12-10 15:19:08 +0000250static
Owen Taylor3473f882001-02-23 17:55:21 +0000251void check_buffer(xmlParserInputPtr in) {
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800252 if (in->base != xmlBufContent(in->buf->buffer)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000253 xmlGenericError(xmlGenericErrorContext,
254 "xmlParserInput: base mismatch problem\n");
255 }
256 if (in->cur < in->base) {
257 xmlGenericError(xmlGenericErrorContext,
258 "xmlParserInput: cur < base problem\n");
259 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800260 if (in->cur > in->base + xmlBufUse(in->buf->buffer)) {
Owen Taylor3473f882001-02-23 17:55:21 +0000261 xmlGenericError(xmlGenericErrorContext,
262 "xmlParserInput: cur > base + use problem\n");
263 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800264 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d\n",
265 (int) in, (int) xmlBufContent(in->buf->buffer), in->cur - in->base,
266 xmlBufUse(in->buf->buffer));
Owen Taylor3473f882001-02-23 17:55:21 +0000267}
268
269#else
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800270#define CHECK_BUFFER(in)
Owen Taylor3473f882001-02-23 17:55:21 +0000271#endif
272
273
274/**
275 * xmlParserInputRead:
276 * @in: an XML parser input
277 * @len: an indicative size for the lookahead
278 *
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800279 * This function was internal and is deprecated.
Owen Taylor3473f882001-02-23 17:55:21 +0000280 *
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800281 * Returns -1 as this is an error to use it.
Owen Taylor3473f882001-02-23 17:55:21 +0000282 */
283int
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800284xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
285 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000286}
287
288/**
289 * xmlParserInputGrow:
290 * @in: an XML parser input
291 * @len: an indicative size for the lookahead
292 *
293 * This function increase the input for the parser. It tries to
294 * preserve pointers to the input buffer, and keep already read data
295 *
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800296 * Returns the amount of char read, or -1 in case of error, 0 indicate the
Owen Taylor3473f882001-02-23 17:55:21 +0000297 * end of this entity
298 */
299int
300xmlParserInputGrow(xmlParserInputPtr in, int len) {
David Kilzerd433ea62016-05-18 14:52:59 -0700301 int ret;
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800302 size_t indx;
303 const xmlChar *content;
Owen Taylor3473f882001-02-23 17:55:21 +0000304
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800305 if ((in == NULL) || (len < 0)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000306#ifdef DEBUG_INPUT
307 xmlGenericError(xmlGenericErrorContext, "Grow\n");
308#endif
309 if (in->buf == NULL) return(-1);
310 if (in->base == NULL) return(-1);
311 if (in->cur == NULL) return(-1);
312 if (in->buf->buffer == NULL) return(-1);
313
314 CHECK_BUFFER(in);
315
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000316 indx = in->cur - in->base;
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800317 if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000318
319 CHECK_BUFFER(in);
320
321 return(0);
322 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800323 if (in->buf->readcallback != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +0000324 ret = xmlParserInputBufferGrow(in->buf, len);
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800325 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000326 return(0);
327
328 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +0000329 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +0000330 * block, but we use it really as an integer to do some
331 * pointer arithmetic. Insure will raise it as a bug but in
332 * that specific case, that's not !
333 */
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800334
335 content = xmlBufContent(in->buf->buffer);
336 if (in->base != content) {
Owen Taylor3473f882001-02-23 17:55:21 +0000337 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000338 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000339 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000340 indx = in->cur - in->base;
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800341 in->base = content;
342 in->cur = &content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000343 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800344 in->end = xmlBufEnd(in->buf->buffer);
Owen Taylor3473f882001-02-23 17:55:21 +0000345
346 CHECK_BUFFER(in);
347
348 return(ret);
349}
350
351/**
352 * xmlParserInputShrink:
353 * @in: an XML parser input
354 *
355 * This function removes used input for the parser.
356 */
357void
358xmlParserInputShrink(xmlParserInputPtr in) {
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800359 size_t used;
360 size_t ret;
361 size_t indx;
362 const xmlChar *content;
Owen Taylor3473f882001-02-23 17:55:21 +0000363
364#ifdef DEBUG_INPUT
365 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
366#endif
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000367 if (in == NULL) return;
Owen Taylor3473f882001-02-23 17:55:21 +0000368 if (in->buf == NULL) return;
369 if (in->base == NULL) return;
370 if (in->cur == NULL) return;
371 if (in->buf->buffer == NULL) return;
372
373 CHECK_BUFFER(in);
374
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800375 used = in->cur - xmlBufContent(in->buf->buffer);
Owen Taylor3473f882001-02-23 17:55:21 +0000376 /*
377 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000378 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +0000379 */
Owen Taylor3473f882001-02-23 17:55:21 +0000380 if (used > INPUT_CHUNK) {
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800381 ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
Owen Taylor3473f882001-02-23 17:55:21 +0000382 if (ret > 0) {
383 in->cur -= ret;
384 in->consumed += ret;
385 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800386 in->end = xmlBufEnd(in->buf->buffer);
Owen Taylor3473f882001-02-23 17:55:21 +0000387 }
388
389 CHECK_BUFFER(in);
390
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800391 if (xmlBufUse(in->buf->buffer) > INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000392 return;
393 }
394 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800395 content = xmlBufContent(in->buf->buffer);
396 if (in->base != content) {
Owen Taylor3473f882001-02-23 17:55:21 +0000397 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000398 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000399 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000400 indx = in->cur - in->base;
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800401 in->base = content;
402 in->cur = &content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000403 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +0800404 in->end = xmlBufEnd(in->buf->buffer);
Owen Taylor3473f882001-02-23 17:55:21 +0000405
406 CHECK_BUFFER(in);
407}
408
409/************************************************************************
410 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800411 * UTF8 character input and related functions *
Owen Taylor3473f882001-02-23 17:55:21 +0000412 * *
413 ************************************************************************/
414
415/**
416 * xmlNextChar:
417 * @ctxt: the XML parser context
418 *
419 * Skip to the next char input char.
420 */
421
422void
Daniel Veillard77a90a72003-03-22 00:04:05 +0000423xmlNextChar(xmlParserCtxtPtr ctxt)
424{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000425 if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
426 (ctxt->input == NULL))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000427 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000428
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800429 if (!(VALID_CTXT(ctxt))) {
430 xmlErrInternal(ctxt, "Parser input data memory error\n", NULL);
431 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
432 xmlStopParser(ctxt);
433 return;
434 }
435
436 if ((*ctxt->input->cur == 0) &&
437 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800438 return;
439 }
Owen Taylor3473f882001-02-23 17:55:21 +0000440
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800441 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
442 const unsigned char *cur;
443 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +0000444
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800445 /*
446 * 2.11 End-of-Line Handling
447 * the literal two-character sequence "#xD#xA" or a standalone
448 * literal #xD, an XML processor must pass to the application
449 * the single character #xA.
450 */
451 if (*(ctxt->input->cur) == '\n') {
452 ctxt->input->line++; ctxt->input->col = 1;
453 } else
454 ctxt->input->col++;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000455
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800456 /*
457 * We are supposed to handle UTF8, check it's valid
458 * From rfc2044: encoding of the Unicode values on UTF-8:
459 *
460 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
461 * 0000 0000-0000 007F 0xxxxxxx
462 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
463 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
464 *
465 * Check for the 0x110000 limit too
466 */
467 cur = ctxt->input->cur;
468
469 c = *cur;
470 if (c & 0x80) {
Daniel Veillardf19385a2017-08-28 20:40:19 +0200471 if (c == 0xC0)
472 goto encoding_error;
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800473 if (cur[1] == 0) {
474 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
475 cur = ctxt->input->cur;
476 }
477 if ((cur[1] & 0xc0) != 0x80)
478 goto encoding_error;
479 if ((c & 0xe0) == 0xe0) {
480 unsigned int val;
481
482 if (cur[2] == 0) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000483 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard33c76c82009-08-25 11:30:34 +0200484 cur = ctxt->input->cur;
485 }
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800486 if ((cur[2] & 0xc0) != 0x80)
Daniel Veillard77a90a72003-03-22 00:04:05 +0000487 goto encoding_error;
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800488 if ((c & 0xf0) == 0xf0) {
489 if (cur[3] == 0) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000490 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard33c76c82009-08-25 11:30:34 +0200491 cur = ctxt->input->cur;
492 }
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800493 if (((c & 0xf8) != 0xf0) ||
494 ((cur[3] & 0xc0) != 0x80))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000495 goto encoding_error;
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800496 /* 4-byte code */
497 ctxt->input->cur += 4;
498 val = (cur[0] & 0x7) << 18;
499 val |= (cur[1] & 0x3f) << 12;
500 val |= (cur[2] & 0x3f) << 6;
501 val |= cur[3] & 0x3f;
502 } else {
503 /* 3-byte code */
504 ctxt->input->cur += 3;
505 val = (cur[0] & 0xf) << 12;
506 val |= (cur[1] & 0x3f) << 6;
507 val |= cur[2] & 0x3f;
508 }
509 if (((val > 0xd7ff) && (val < 0xe000)) ||
510 ((val > 0xfffd) && (val < 0x10000)) ||
511 (val >= 0x110000)) {
512 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
513 "Char 0x%X out of allowed range\n",
514 val);
515 }
Daniel Veillard77a90a72003-03-22 00:04:05 +0000516 } else
Pranjal Jumde0bcd05c2016-03-01 15:18:04 -0800517 /* 2-byte code */
518 ctxt->input->cur += 2;
519 } else
520 /* 1-byte code */
521 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000522 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000523 /*
524 * Assume it's a fixed length encoding (1) with
525 * a compatible encoding for the ASCII set, since
526 * XML constructs only use < 128 chars
527 */
528
529 if (*(ctxt->input->cur) == '\n') {
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +0000530 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000531 } else
532 ctxt->input->col++;
533 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000534 }
Nick Wellnhofer453dff12017-06-19 17:55:20 +0200535 if (*ctxt->input->cur == 0)
536 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +0000537 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000538encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +0000539 /*
540 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000541 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000542 * declaration header. Report the error and switch the encoding
543 * to ISO-Latin-1 (if you don't like this policy, just declare the
544 * encoding !)
545 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000546 if ((ctxt == NULL) || (ctxt->input == NULL) ||
547 (ctxt->input->end - ctxt->input->cur < 4)) {
548 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
549 "Input is not proper UTF-8, indicate encoding !\n",
550 NULL, NULL);
551 } else {
552 char buffer[150];
553
554 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
555 ctxt->input->cur[0], ctxt->input->cur[1],
556 ctxt->input->cur[2], ctxt->input->cur[3]);
557 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
558 "Input is not proper UTF-8, indicate encoding !\n%s",
559 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000560 }
Daniel Veillard77a90a72003-03-22 00:04:05 +0000561 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000562 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000563 return;
564}
565
566/**
567 * xmlCurrentChar:
568 * @ctxt: the XML parser context
569 * @len: pointer to the length of the char read
570 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000571 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000572 * bytes in the input buffer. Implement the end of line normalization:
573 * 2.11 End-of-Line Handling
574 * Wherever an external parsed entity or the literal entity value
575 * of an internal parsed entity contains either the literal two-character
576 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
577 * must pass to the application the single character #xA.
578 * This behavior can conveniently be produced by normalizing all
579 * line breaks to #xA on input, before parsing.)
580 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000581 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000582 */
583
584int
585xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000586 if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000587 if (ctxt->instate == XML_PARSER_EOF)
588 return(0);
589
Daniel Veillard561b7f82002-03-20 21:55:57 +0000590 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
591 *len = 1;
592 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000593 }
594 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
595 /*
596 * We are supposed to handle UTF8, check it's valid
597 * From rfc2044: encoding of the Unicode values on UTF-8:
598 *
599 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
600 * 0000 0000-0000 007F 0xxxxxxx
601 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800602 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
Owen Taylor3473f882001-02-23 17:55:21 +0000603 *
604 * Check for the 0x110000 limit too
605 */
606 const unsigned char *cur = ctxt->input->cur;
607 unsigned char c;
608 unsigned int val;
609
610 c = *cur;
611 if (c & 0x80) {
Daniel Veillardb3edafd2008-01-11 08:00:57 +0000612 if (((c & 0x40) == 0) || (c == 0xC0))
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000613 goto encoding_error;
Daniel Veillard33c76c82009-08-25 11:30:34 +0200614 if (cur[1] == 0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000615 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard33c76c82009-08-25 11:30:34 +0200616 cur = ctxt->input->cur;
617 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000618 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000619 goto encoding_error;
620 if ((c & 0xe0) == 0xe0) {
Daniel Veillard33c76c82009-08-25 11:30:34 +0200621 if (cur[2] == 0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000622 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard33c76c82009-08-25 11:30:34 +0200623 cur = ctxt->input->cur;
624 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000625 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +0000626 goto encoding_error;
627 if ((c & 0xf0) == 0xf0) {
Daniel Veillard33c76c82009-08-25 11:30:34 +0200628 if (cur[3] == 0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000629 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard33c76c82009-08-25 11:30:34 +0200630 cur = ctxt->input->cur;
631 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000632 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +0000633 ((cur[3] & 0xc0) != 0x80))
634 goto encoding_error;
635 /* 4-byte code */
636 *len = 4;
637 val = (cur[0] & 0x7) << 18;
638 val |= (cur[1] & 0x3f) << 12;
639 val |= (cur[2] & 0x3f) << 6;
640 val |= cur[3] & 0x3f;
Daniel Veillardb3edafd2008-01-11 08:00:57 +0000641 if (val < 0x10000)
642 goto encoding_error;
Owen Taylor3473f882001-02-23 17:55:21 +0000643 } else {
644 /* 3-byte code */
645 *len = 3;
646 val = (cur[0] & 0xf) << 12;
647 val |= (cur[1] & 0x3f) << 6;
648 val |= cur[2] & 0x3f;
Daniel Veillardb3edafd2008-01-11 08:00:57 +0000649 if (val < 0x800)
650 goto encoding_error;
Owen Taylor3473f882001-02-23 17:55:21 +0000651 }
652 } else {
653 /* 2-byte code */
654 *len = 2;
655 val = (cur[0] & 0x1f) << 6;
656 val |= cur[1] & 0x3f;
Daniel Veillardb3edafd2008-01-11 08:00:57 +0000657 if (val < 0x80)
658 goto encoding_error;
Owen Taylor3473f882001-02-23 17:55:21 +0000659 }
660 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000661 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
662 "Char 0x%X out of allowed range\n", val);
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800663 }
Owen Taylor3473f882001-02-23 17:55:21 +0000664 return(val);
665 } else {
666 /* 1-byte code */
667 *len = 1;
Daniel Veillardb3edafd2008-01-11 08:00:57 +0000668 if (*ctxt->input->cur == 0)
669 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
670 if ((*ctxt->input->cur == 0) &&
671 (ctxt->input->end > ctxt->input->cur)) {
672 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
673 "Char 0x0 out of allowed range\n", 0);
674 }
Owen Taylor3473f882001-02-23 17:55:21 +0000675 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000676 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000677 ctxt->input->cur++;
678 }
679 return(0xA);
680 }
681 return((int) *ctxt->input->cur);
682 }
683 }
684 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000685 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000686 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000687 * XML constructs only use < 128 chars
688 */
689 *len = 1;
690 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +0000691 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +0000692 ctxt->input->cur++;
693 }
694 return(0xA);
695 }
696 return((int) *ctxt->input->cur);
697encoding_error:
698 /*
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200699 * An encoding problem may arise from a truncated input buffer
700 * splitting a character in the middle. In that case do not raise
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700701 * an error but return 0 to indicate an end of stream problem
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200702 */
703 if (ctxt->input->end - ctxt->input->cur < 4) {
704 *len = 0;
705 return(0);
706 }
707
708 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000709 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000710 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000711 * declaration header. Report the error and switch the encoding
712 * to ISO-Latin-1 (if you don't like this policy, just declare the
713 * encoding !)
714 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000715 {
716 char buffer[150];
717
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200718 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
719 ctxt->input->cur[0], ctxt->input->cur[1],
720 ctxt->input->cur[2], ctxt->input->cur[3]);
Daniel Veillarda521d282004-11-09 14:59:59 +0000721 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
722 "Input is not proper UTF-8, indicate encoding !\n%s",
723 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000724 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800725 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Owen Taylor3473f882001-02-23 17:55:21 +0000726 *len = 1;
727 return((int) *ctxt->input->cur);
728}
729
730/**
731 * xmlStringCurrentChar:
732 * @ctxt: the XML parser context
733 * @cur: pointer to the beginning of the char
734 * @len: pointer to the length of the char read
735 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000736 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000737 * bytes in the input buffer.
738 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000739 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000740 */
741
742int
Daniel Veillardd8224e02002-01-13 15:43:22 +0000743xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
744{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000745 if ((len == NULL) || (cur == NULL)) return(0);
Daniel Veillard61d80a22001-04-27 17:13:01 +0000746 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +0000747 /*
748 * We are supposed to handle UTF8, check it's valid
749 * From rfc2044: encoding of the Unicode values on UTF-8:
750 *
751 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
752 * 0000 0000-0000 007F 0xxxxxxx
753 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800754 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
Daniel Veillardd8224e02002-01-13 15:43:22 +0000755 *
756 * Check for the 0x110000 limit too
757 */
758 unsigned char c;
759 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +0000760
Daniel Veillardd8224e02002-01-13 15:43:22 +0000761 c = *cur;
762 if (c & 0x80) {
763 if ((cur[1] & 0xc0) != 0x80)
764 goto encoding_error;
765 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +0000766
Daniel Veillardd8224e02002-01-13 15:43:22 +0000767 if ((cur[2] & 0xc0) != 0x80)
768 goto encoding_error;
769 if ((c & 0xf0) == 0xf0) {
770 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
771 goto encoding_error;
772 /* 4-byte code */
773 *len = 4;
774 val = (cur[0] & 0x7) << 18;
775 val |= (cur[1] & 0x3f) << 12;
776 val |= (cur[2] & 0x3f) << 6;
777 val |= cur[3] & 0x3f;
778 } else {
779 /* 3-byte code */
780 *len = 3;
781 val = (cur[0] & 0xf) << 12;
782 val |= (cur[1] & 0x3f) << 6;
783 val |= cur[2] & 0x3f;
784 }
785 } else {
786 /* 2-byte code */
787 *len = 2;
788 val = (cur[0] & 0x1f) << 6;
789 val |= cur[1] & 0x3f;
790 }
791 if (!IS_CHAR(val)) {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000792 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
793 "Char 0x%X out of allowed range\n", val);
Daniel Veillardd8224e02002-01-13 15:43:22 +0000794 }
795 return (val);
796 } else {
797 /* 1-byte code */
798 *len = 1;
799 return ((int) *cur);
800 }
Owen Taylor3473f882001-02-23 17:55:21 +0000801 }
802 /*
Daniel Veillard60087f32001-10-10 09:45:09 +0000803 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000804 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +0000805 * XML constructs only use < 128 chars
806 */
807 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000808 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000809encoding_error:
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200810
811 /*
812 * An encoding problem may arise from a truncated input buffer
813 * splitting a character in the middle. In that case do not raise
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700814 * an error but return 0 to indicate an end of stream problem
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200815 */
816 if ((ctxt == NULL) || (ctxt->input == NULL) ||
817 (ctxt->input->end - ctxt->input->cur < 4)) {
818 *len = 0;
819 return(0);
820 }
Daniel Veillarda521d282004-11-09 14:59:59 +0000821 /*
Owen Taylor3473f882001-02-23 17:55:21 +0000822 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000823 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000824 * declaration header. Report the error and switch the encoding
825 * to ISO-Latin-1 (if you don't like this policy, just declare the
826 * encoding !)
827 */
Daniel Veillarda521d282004-11-09 14:59:59 +0000828 {
829 char buffer[150];
830
Nick Wellnhofer69936b12017-08-30 14:16:01 +0200831 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
832 ctxt->input->cur[0], ctxt->input->cur[1],
833 ctxt->input->cur[2], ctxt->input->cur[3]);
Daniel Veillarda521d282004-11-09 14:59:59 +0000834 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
835 "Input is not proper UTF-8, indicate encoding !\n%s",
836 BAD_CAST buffer, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +0000837 }
Owen Taylor3473f882001-02-23 17:55:21 +0000838 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +0000839 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000840}
841
842/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000843 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000844 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +0000845 * @val: the char value
846 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800847 * append the char value in the array
Owen Taylor3473f882001-02-23 17:55:21 +0000848 *
849 * Returns the number of xmlChar written
850 */
Owen Taylor3473f882001-02-23 17:55:21 +0000851int
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000852xmlCopyCharMultiByte(xmlChar *out, int val) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000853 if (out == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +0000854 /*
855 * We are supposed to handle UTF8, check it's valid
856 * From rfc2044: encoding of the Unicode values on UTF-8:
857 *
858 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
859 * 0000 0000-0000 007F 0xxxxxxx
860 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800861 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
Owen Taylor3473f882001-02-23 17:55:21 +0000862 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000863 if (val >= 0x80) {
864 xmlChar *savedout = out;
865 int bits;
866 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
867 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
868 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
869 else {
Daniel Veillardce9457f2003-10-05 21:33:18 +0000870 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000871 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Daniel Veillardce9457f2003-10-05 21:33:18 +0000872 val);
Owen Taylor3473f882001-02-23 17:55:21 +0000873 return(0);
874 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000875 for ( ; bits >= 0; bits-= 6)
876 *out++= ((val >> bits) & 0x3F) | 0x80 ;
877 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +0000878 }
879 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000880 return 1;
881}
882
883/**
884 * xmlCopyChar:
885 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000886 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000887 * @val: the char value
888 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800889 * append the char value in the array
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000890 *
891 * Returns the number of xmlChar written
892 */
893
894int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000895xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +0000896 if (out == NULL) return(0);
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000897 /* the len parameter is ignored */
898 if (val >= 0x80) {
899 return(xmlCopyCharMultiByte (out, val));
900 }
901 *out = (xmlChar) val;
902 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +0000903}
904
905/************************************************************************
906 * *
907 * Commodity functions to switch encodings *
908 * *
909 ************************************************************************/
910
Daniel Veillard7e385bd2009-08-26 11:38:49 +0200911static int
912xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
913 xmlCharEncodingHandlerPtr handler, int len);
914static int
915xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
916 xmlCharEncodingHandlerPtr handler, int len);
Owen Taylor3473f882001-02-23 17:55:21 +0000917/**
918 * xmlSwitchEncoding:
919 * @ctxt: the parser context
920 * @enc: the encoding value (number)
921 *
922 * change the input functions when discovering the character encoding
923 * of a given entity.
924 *
925 * Returns 0 in case of success, -1 otherwise
926 */
927int
928xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
929{
930 xmlCharEncodingHandlerPtr handler;
Daniel Veillard7e385bd2009-08-26 11:38:49 +0200931 int len = -1;
Daniel Veillardafd27c22015-11-09 18:07:18 +0800932 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +0000933
Daniel Veillard42595322004-11-08 10:52:06 +0000934 if (ctxt == NULL) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000935 switch (enc) {
936 case XML_CHAR_ENCODING_ERROR:
Daniel Veillarda840b692003-10-19 13:35:37 +0000937 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +0000938 "encoding unknown\n", NULL, NULL);
Daniel Veillard30e76072006-03-09 14:13:55 +0000939 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +0000940 case XML_CHAR_ENCODING_NONE:
941 /* let's assume it's UTF-8 without the XML decl */
942 ctxt->charset = XML_CHAR_ENCODING_UTF8;
943 return(0);
944 case XML_CHAR_ENCODING_UTF8:
945 /* default encoding, no conversion should be needed */
946 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +0000947
948 /*
949 * Errata on XML-1.0 June 20 2001
950 * Specific handling of the Byte Order Mark for
951 * UTF-8
952 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +0000953 if ((ctxt->input != NULL) &&
954 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +0000955 (ctxt->input->cur[1] == 0xBB) &&
956 (ctxt->input->cur[2] == 0xBF)) {
957 ctxt->input->cur += 3;
958 }
Owen Taylor3473f882001-02-23 17:55:21 +0000959 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +0000960 case XML_CHAR_ENCODING_UTF16LE:
961 case XML_CHAR_ENCODING_UTF16BE:
962 /*The raw input characters are encoded
963 *in UTF-16. As we expect this function
964 *to be called after xmlCharEncInFunc, we expect
965 *ctxt->input->cur to contain UTF-8 encoded characters.
966 *So the raw UTF16 Byte Order Mark
967 *has also been converted into
968 *an UTF-8 BOM. Let's skip that BOM.
969 */
Daniel Veillard30e76072006-03-09 14:13:55 +0000970 if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
Daniel Veillard2dcb9372003-07-16 21:18:19 +0000971 (ctxt->input->cur[0] == 0xEF) &&
972 (ctxt->input->cur[1] == 0xBB) &&
973 (ctxt->input->cur[2] == 0xBF)) {
974 ctxt->input->cur += 3;
975 }
Daniel Veillard7e385bd2009-08-26 11:38:49 +0200976 len = 90;
977 break;
978 case XML_CHAR_ENCODING_UCS2:
979 len = 90;
980 break;
981 case XML_CHAR_ENCODING_UCS4BE:
982 case XML_CHAR_ENCODING_UCS4LE:
983 case XML_CHAR_ENCODING_UCS4_2143:
984 case XML_CHAR_ENCODING_UCS4_3412:
985 len = 180;
986 break;
987 case XML_CHAR_ENCODING_EBCDIC:
988 case XML_CHAR_ENCODING_8859_1:
989 case XML_CHAR_ENCODING_8859_2:
990 case XML_CHAR_ENCODING_8859_3:
991 case XML_CHAR_ENCODING_8859_4:
992 case XML_CHAR_ENCODING_8859_5:
993 case XML_CHAR_ENCODING_8859_6:
994 case XML_CHAR_ENCODING_8859_7:
995 case XML_CHAR_ENCODING_8859_8:
996 case XML_CHAR_ENCODING_8859_9:
997 case XML_CHAR_ENCODING_ASCII:
998 case XML_CHAR_ENCODING_2022_JP:
999 case XML_CHAR_ENCODING_SHIFT_JIS:
1000 case XML_CHAR_ENCODING_EUC_JP:
1001 len = 45;
1002 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001003 }
1004 handler = xmlGetCharEncodingHandler(enc);
1005 if (handler == NULL) {
1006 /*
1007 * Default handlers.
1008 */
1009 switch (enc) {
Owen Taylor3473f882001-02-23 17:55:21 +00001010 case XML_CHAR_ENCODING_ASCII:
1011 /* default encoding, no conversion should be needed */
1012 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1013 return(0);
1014 case XML_CHAR_ENCODING_UTF16LE:
1015 break;
1016 case XML_CHAR_ENCODING_UTF16BE:
1017 break;
1018 case XML_CHAR_ENCODING_UCS4LE:
Daniel Veillarda840b692003-10-19 13:35:37 +00001019 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001020 "encoding not supported %s\n",
1021 BAD_CAST "USC4 little endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001022 break;
1023 case XML_CHAR_ENCODING_UCS4BE:
Daniel Veillarda840b692003-10-19 13:35:37 +00001024 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001025 "encoding not supported %s\n",
1026 BAD_CAST "USC4 big endian", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001027 break;
1028 case XML_CHAR_ENCODING_EBCDIC:
Daniel Veillarda840b692003-10-19 13:35:37 +00001029 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001030 "encoding not supported %s\n",
1031 BAD_CAST "EBCDIC", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001032 break;
1033 case XML_CHAR_ENCODING_UCS4_2143:
Daniel Veillarda840b692003-10-19 13:35:37 +00001034 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001035 "encoding not supported %s\n",
1036 BAD_CAST "UCS4 2143", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001037 break;
1038 case XML_CHAR_ENCODING_UCS4_3412:
Daniel Veillarda840b692003-10-19 13:35:37 +00001039 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001040 "encoding not supported %s\n",
1041 BAD_CAST "UCS4 3412", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001042 break;
1043 case XML_CHAR_ENCODING_UCS2:
Daniel Veillarda840b692003-10-19 13:35:37 +00001044 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001045 "encoding not supported %s\n",
1046 BAD_CAST "UCS2", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001047 break;
1048 case XML_CHAR_ENCODING_8859_1:
1049 case XML_CHAR_ENCODING_8859_2:
1050 case XML_CHAR_ENCODING_8859_3:
1051 case XML_CHAR_ENCODING_8859_4:
1052 case XML_CHAR_ENCODING_8859_5:
1053 case XML_CHAR_ENCODING_8859_6:
1054 case XML_CHAR_ENCODING_8859_7:
1055 case XML_CHAR_ENCODING_8859_8:
1056 case XML_CHAR_ENCODING_8859_9:
1057 /*
1058 * We used to keep the internal content in the
1059 * document encoding however this turns being unmaintainable
1060 * So xmlGetCharEncodingHandler() will return non-null
1061 * values for this now.
1062 */
1063 if ((ctxt->inputNr == 1) &&
1064 (ctxt->encoding == NULL) &&
Daniel Veillard30e76072006-03-09 14:13:55 +00001065 (ctxt->input != NULL) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001066 (ctxt->input->encoding != NULL)) {
1067 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1068 }
1069 ctxt->charset = enc;
1070 return(0);
1071 case XML_CHAR_ENCODING_2022_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001072 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001073 "encoding not supported %s\n",
1074 BAD_CAST "ISO-2022-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001075 break;
1076 case XML_CHAR_ENCODING_SHIFT_JIS:
Daniel Veillarda840b692003-10-19 13:35:37 +00001077 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001078 "encoding not supported %s\n",
1079 BAD_CAST "Shift_JIS", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001080 break;
1081 case XML_CHAR_ENCODING_EUC_JP:
Daniel Veillarda840b692003-10-19 13:35:37 +00001082 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillardce9457f2003-10-05 21:33:18 +00001083 "encoding not supported %s\n",
1084 BAD_CAST "EUC-JP", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001085 break;
Daniel Veillard30e76072006-03-09 14:13:55 +00001086 default:
1087 break;
Owen Taylor3473f882001-02-23 17:55:21 +00001088 }
1089 }
Nick Wellnhofer45ce1ee2017-06-09 15:27:25 +02001090 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001091 * TODO: We could recover from errors in external entities if we
Nick Wellnhofer45ce1ee2017-06-09 15:27:25 +02001092 * didn't stop the parser. But most callers of this function don't
1093 * check the return value.
1094 */
Nick Wellnhofer0db8dc92017-06-07 16:07:33 +02001095 if (handler == NULL) {
1096 xmlStopParser(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001097 return(-1);
Nick Wellnhofer0db8dc92017-06-07 16:07:33 +02001098 }
Owen Taylor3473f882001-02-23 17:55:21 +00001099 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillardafd27c22015-11-09 18:07:18 +08001100 ret = xmlSwitchToEncodingInt(ctxt, handler, len);
1101 if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
1102 /*
1103 * on encoding conversion errors, stop the parser
1104 */
1105 xmlStopParser(ctxt);
1106 ctxt->errNo = XML_I18N_CONV_FAILED;
1107 }
1108 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001109}
1110
1111/**
Daniel Veillarda840b692003-10-19 13:35:37 +00001112 * xmlSwitchInputEncoding:
1113 * @ctxt: the parser context
1114 * @input: the input stream
1115 * @handler: the encoding handler
Daniel Veillard7e385bd2009-08-26 11:38:49 +02001116 * @len: the number of bytes to convert for the first line or -1
Daniel Veillarda840b692003-10-19 13:35:37 +00001117 *
1118 * change the input functions when discovering the character encoding
1119 * of a given entity.
1120 *
1121 * Returns 0 in case of success, -1 otherwise
1122 */
Daniel Veillard7e385bd2009-08-26 11:38:49 +02001123static int
1124xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1125 xmlCharEncodingHandlerPtr handler, int len)
Daniel Veillarda840b692003-10-19 13:35:37 +00001126{
1127 int nbchars;
1128
1129 if (handler == NULL)
1130 return (-1);
1131 if (input == NULL)
1132 return (-1);
1133 if (input->buf != NULL) {
1134 if (input->buf->encoder != NULL) {
1135 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001136 * Check in case the auto encoding detection triggered
Daniel Veillarda840b692003-10-19 13:35:37 +00001137 * in already.
1138 */
1139 if (input->buf->encoder == handler)
1140 return (0);
1141
1142 /*
1143 * "UTF-16" can be used for both LE and BE
1144 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1145 BAD_CAST "UTF-16", 6)) &&
1146 (!xmlStrncmp(BAD_CAST handler->name,
1147 BAD_CAST "UTF-16", 6))) {
1148 return(0);
1149 }
1150 */
1151
1152 /*
1153 * Note: this is a bit dangerous, but that's what it
1154 * takes to use nearly compatible signature for different
1155 * encodings.
Haibo Huang735158e2021-02-23 17:48:08 -08001156 *
1157 * FIXME: Encoders might buffer partial byte sequences, so
1158 * this probably can't work. We should return an error and
1159 * make sure that callers never try to switch the encoding
1160 * twice.
Daniel Veillarda840b692003-10-19 13:35:37 +00001161 */
1162 xmlCharEncCloseFunc(input->buf->encoder);
1163 input->buf->encoder = handler;
1164 return (0);
1165 }
1166 input->buf->encoder = handler;
1167
1168 /*
1169 * Is there already some content down the pipe to convert ?
1170 */
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001171 if (xmlBufIsEmpty(input->buf->buffer) == 0) {
Daniel Veillarda840b692003-10-19 13:35:37 +00001172 int processed;
Daniel Veillard36711902004-02-11 13:25:26 +00001173 unsigned int use;
Daniel Veillarda840b692003-10-19 13:35:37 +00001174
1175 /*
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001176 * Specific handling of the Byte Order Mark for
Daniel Veillarda840b692003-10-19 13:35:37 +00001177 * UTF-16
1178 */
1179 if ((handler->name != NULL) &&
William M. Brack1d8c9b22004-12-25 10:14:57 +00001180 (!strcmp(handler->name, "UTF-16LE") ||
1181 !strcmp(handler->name, "UTF-16")) &&
Daniel Veillarda840b692003-10-19 13:35:37 +00001182 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1183 input->cur += 2;
1184 }
1185 if ((handler->name != NULL) &&
1186 (!strcmp(handler->name, "UTF-16BE")) &&
1187 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1188 input->cur += 2;
1189 }
1190 /*
1191 * Errata on XML-1.0 June 20 2001
1192 * Specific handling of the Byte Order Mark for
1193 * UTF-8
1194 */
1195 if ((handler->name != NULL) &&
1196 (!strcmp(handler->name, "UTF-8")) &&
1197 (input->cur[0] == 0xEF) &&
1198 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1199 input->cur += 3;
1200 }
1201
1202 /*
1203 * Shrink the current input buffer.
1204 * Move it as the raw buffer and create a new input buffer
1205 */
1206 processed = input->cur - input->base;
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001207 xmlBufShrink(input->buf->buffer, processed);
Daniel Veillarda840b692003-10-19 13:35:37 +00001208 input->buf->raw = input->buf->buffer;
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001209 input->buf->buffer = xmlBufCreate();
Daniel Veillard36711902004-02-11 13:25:26 +00001210 input->buf->rawconsumed = processed;
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001211 use = xmlBufUse(input->buf->raw);
Daniel Veillarda840b692003-10-19 13:35:37 +00001212
1213 if (ctxt->html) {
1214 /*
1215 * convert as much as possible of the buffer
1216 */
Elliott Hughes7fbecab2019-01-10 16:42:03 -08001217 nbchars = xmlCharEncInput(input->buf, 1);
Daniel Veillarda840b692003-10-19 13:35:37 +00001218 } else {
1219 /*
1220 * convert just enough to get
1221 * '<?xml version="1.0" encoding="xxx"?>'
1222 * parsed with the autodetected encoding
1223 * into the parser reading buffer.
1224 */
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001225 nbchars = xmlCharEncFirstLineInput(input->buf, len);
Daniel Veillarda840b692003-10-19 13:35:37 +00001226 }
Nick Wellnhoferf9e79972017-06-10 00:33:07 +02001227 xmlBufResetInput(input->buf->buffer, input);
Daniel Veillarda840b692003-10-19 13:35:37 +00001228 if (nbchars < 0) {
1229 xmlErrInternal(ctxt,
1230 "switching encoding: encoder error\n",
1231 NULL);
1232 return (-1);
1233 }
Daniel Veillard768eb3b2012-07-16 14:19:49 +08001234 input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
Daniel Veillarda840b692003-10-19 13:35:37 +00001235 }
1236 return (0);
Nick Wellnhofer3aca7f32017-08-21 13:06:29 +02001237 } else if (input->length == 0) {
1238 /*
1239 * When parsing a static memory array one must know the
1240 * size to be able to convert the buffer.
1241 */
Daniel Veillardc19d5352005-11-17 13:12:16 +00001242 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
Elliott Hughes7fbecab2019-01-10 16:42:03 -08001243 /*
1244 * Callers assume that the input buffer takes ownership of the
1245 * encoding handler. xmlCharEncCloseFunc frees unregistered
1246 * handlers and avoids a memory leak.
1247 */
1248 xmlCharEncCloseFunc(handler);
Daniel Veillardc19d5352005-11-17 13:12:16 +00001249 return (-1);
Daniel Veillarda840b692003-10-19 13:35:37 +00001250 }
Elliott Hughes7fbecab2019-01-10 16:42:03 -08001251 /*
1252 * We should actually raise an error here, see issue #34.
1253 */
1254 xmlCharEncCloseFunc(handler);
Daniel Veillarda840b692003-10-19 13:35:37 +00001255 return (0);
1256}
1257
1258/**
Daniel Veillard7e385bd2009-08-26 11:38:49 +02001259 * xmlSwitchInputEncoding:
1260 * @ctxt: the parser context
1261 * @input: the input stream
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
1270xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1271 xmlCharEncodingHandlerPtr handler) {
1272 return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
1273}
1274
1275/**
1276 * xmlSwitchToEncodingInt:
1277 * @ctxt: the parser context
1278 * @handler: the encoding handler
Michael Woodfb27e2c2012-09-28 08:59:33 +02001279 * @len: the length to convert or -1
Daniel Veillard7e385bd2009-08-26 11:38:49 +02001280 *
1281 * change the input functions when discovering the character encoding
1282 * of a given entity, and convert only @len bytes of the output, this
1283 * is needed on auto detect to allows any declared encoding later to
1284 * convert the actual content after the xmlDecl
1285 *
1286 * Returns 0 in case of success, -1 otherwise
1287 */
1288static int
1289xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
1290 xmlCharEncodingHandlerPtr handler, int len) {
1291 int ret = 0;
1292
1293 if (handler != NULL) {
1294 if (ctxt->input != NULL) {
1295 ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
1296 } else {
1297 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1298 NULL);
1299 return(-1);
1300 }
1301 /*
1302 * The parsing is now done in UTF8 natively
1303 */
1304 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1305 } else
1306 return(-1);
1307 return(ret);
1308}
1309
1310/**
Owen Taylor3473f882001-02-23 17:55:21 +00001311 * xmlSwitchToEncoding:
1312 * @ctxt: the parser context
1313 * @handler: the encoding handler
1314 *
1315 * change the input functions when discovering the character encoding
1316 * of a given entity.
1317 *
1318 * Returns 0 in case of success, -1 otherwise
1319 */
1320int
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001321xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
Owen Taylor3473f882001-02-23 17:55:21 +00001322{
Daniel Veillard7e385bd2009-08-26 11:38:49 +02001323 return (xmlSwitchToEncodingInt(ctxt, handler, -1));
Owen Taylor3473f882001-02-23 17:55:21 +00001324}
1325
1326/************************************************************************
1327 * *
1328 * Commodity functions to handle entities processing *
1329 * *
1330 ************************************************************************/
1331
1332/**
1333 * xmlFreeInputStream:
1334 * @input: an xmlParserInputPtr
1335 *
1336 * Free up an input stream.
1337 */
1338void
1339xmlFreeInputStream(xmlParserInputPtr input) {
1340 if (input == NULL) return;
1341
1342 if (input->filename != NULL) xmlFree((char *) input->filename);
1343 if (input->directory != NULL) xmlFree((char *) input->directory);
1344 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1345 if (input->version != NULL) xmlFree((char *) input->version);
1346 if ((input->free != NULL) && (input->base != NULL))
1347 input->free((xmlChar *) input->base);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001348 if (input->buf != NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001349 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001350 xmlFree(input);
1351}
1352
1353/**
1354 * xmlNewInputStream:
1355 * @ctxt: an XML parser context
1356 *
Daniel Veillard0d51cfe2012-05-15 11:18:40 +08001357 * Create a new input stream structure.
1358 *
Owen Taylor3473f882001-02-23 17:55:21 +00001359 * Returns the new input stream or NULL
1360 */
1361xmlParserInputPtr
1362xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1363 xmlParserInputPtr input;
1364
1365 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1366 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001367 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001368 return(NULL);
1369 }
1370 memset(input, 0, sizeof(xmlParserInput));
1371 input->line = 1;
1372 input->col = 1;
1373 input->standalone = -1;
Daniel Veillard0d51cfe2012-05-15 11:18:40 +08001374
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001375 /*
Daniel Veillard0d51cfe2012-05-15 11:18:40 +08001376 * If the context is NULL the id cannot be initialized, but that
1377 * should not happen while parsing which is the situation where
1378 * the id is actually needed.
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001379 */
Daniel Veillard0d51cfe2012-05-15 11:18:40 +08001380 if (ctxt != NULL)
1381 input->id = ctxt->input_id++;
1382
Owen Taylor3473f882001-02-23 17:55:21 +00001383 return(input);
1384}
1385
1386/**
1387 * xmlNewIOInputStream:
1388 * @ctxt: an XML parser context
1389 * @input: an I/O Input
1390 * @enc: the charset encoding if known
1391 *
1392 * Create a new input stream structure encapsulating the @input into
1393 * a stream suitable for the parser.
1394 *
1395 * Returns the new input stream or NULL
1396 */
1397xmlParserInputPtr
1398xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1399 xmlCharEncoding enc) {
1400 xmlParserInputPtr inputStream;
1401
Daniel Veillard42595322004-11-08 10:52:06 +00001402 if (input == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001403 if (xmlParserDebugEntities)
1404 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1405 inputStream = xmlNewInputStream(ctxt);
1406 if (inputStream == NULL) {
1407 return(NULL);
1408 }
1409 inputStream->filename = NULL;
1410 inputStream->buf = input;
Daniel Veillard61551a12012-07-16 16:28:47 +08001411 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1412
Owen Taylor3473f882001-02-23 17:55:21 +00001413 if (enc != XML_CHAR_ENCODING_NONE) {
1414 xmlSwitchEncoding(ctxt, enc);
1415 }
1416
1417 return(inputStream);
1418}
1419
1420/**
1421 * xmlNewEntityInputStream:
1422 * @ctxt: an XML parser context
1423 * @entity: an Entity pointer
1424 *
1425 * Create a new input stream based on an xmlEntityPtr
1426 *
1427 * Returns the new input stream or NULL
1428 */
1429xmlParserInputPtr
1430xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1431 xmlParserInputPtr input;
1432
1433 if (entity == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001434 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1435 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001436 return(NULL);
1437 }
1438 if (xmlParserDebugEntities)
1439 xmlGenericError(xmlGenericErrorContext,
1440 "new input from entity: %s\n", entity->name);
1441 if (entity->content == NULL) {
1442 switch (entity->etype) {
1443 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001444 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1445 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001446 break;
1447 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1448 case XML_EXTERNAL_PARAMETER_ENTITY:
1449 return(xmlLoadExternalEntity((char *) entity->URI,
1450 (char *) entity->ExternalID, ctxt));
1451 case XML_INTERNAL_GENERAL_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001452 xmlErrInternal(ctxt,
1453 "Internal entity %s without content !\n",
1454 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001455 break;
1456 case XML_INTERNAL_PARAMETER_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001457 xmlErrInternal(ctxt,
1458 "Internal parameter entity %s without content !\n",
1459 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001460 break;
1461 case XML_INTERNAL_PREDEFINED_ENTITY:
Daniel Veillardce9457f2003-10-05 21:33:18 +00001462 xmlErrInternal(ctxt,
1463 "Predefined entity %s without content !\n",
1464 entity->name);
Owen Taylor3473f882001-02-23 17:55:21 +00001465 break;
1466 }
1467 return(NULL);
1468 }
1469 input = xmlNewInputStream(ctxt);
1470 if (input == NULL) {
1471 return(NULL);
1472 }
Daniel Veillard87303e32008-04-28 18:07:29 +00001473 if (entity->URI != NULL)
1474 input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001475 input->base = entity->content;
Daniel Veillardfdfeecc2015-11-20 15:07:38 +08001476 if (entity->length == 0)
1477 entity->length = xmlStrlen(entity->content);
Owen Taylor3473f882001-02-23 17:55:21 +00001478 input->cur = entity->content;
1479 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001480 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001481 return(input);
1482}
1483
1484/**
1485 * xmlNewStringInputStream:
1486 * @ctxt: an XML parser context
1487 * @buffer: an memory buffer
1488 *
1489 * Create a new input stream based on a memory buffer.
1490 * Returns the new input stream
1491 */
1492xmlParserInputPtr
1493xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1494 xmlParserInputPtr input;
1495
1496 if (buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001497 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1498 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001499 return(NULL);
1500 }
1501 if (xmlParserDebugEntities)
1502 xmlGenericError(xmlGenericErrorContext,
1503 "new fixed input: %.30s\n", buffer);
1504 input = xmlNewInputStream(ctxt);
1505 if (input == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001506 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001507 return(NULL);
1508 }
1509 input->base = buffer;
1510 input->cur = buffer;
1511 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001512 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001513 return(input);
1514}
1515
1516/**
1517 * xmlNewInputFromFile:
1518 * @ctxt: an XML parser context
1519 * @filename: the filename to use as entity
1520 *
Daniel Veillarda840b692003-10-19 13:35:37 +00001521 * Create a new input stream based on a file or an URL.
Owen Taylor3473f882001-02-23 17:55:21 +00001522 *
1523 * Returns the new input stream or NULL in case of error
1524 */
1525xmlParserInputPtr
1526xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1527 xmlParserInputBufferPtr buf;
1528 xmlParserInputPtr inputStream;
1529 char *directory = NULL;
1530 xmlChar *URI = NULL;
1531
1532 if (xmlParserDebugEntities)
1533 xmlGenericError(xmlGenericErrorContext,
1534 "new input from file: %s\n", filename);
1535 if (ctxt == NULL) return(NULL);
1536 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001537 if (buf == NULL) {
Daniel Veillard6e84bb22005-10-26 09:00:29 +00001538 if (filename == NULL)
1539 __xmlLoaderErr(ctxt,
1540 "failed to load external entity: NULL filename \n",
1541 NULL);
1542 else
1543 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1544 (const char *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00001545 return(NULL);
Daniel Veillarde8039df2003-10-27 11:25:13 +00001546 }
Owen Taylor3473f882001-02-23 17:55:21 +00001547
Owen Taylor3473f882001-02-23 17:55:21 +00001548 inputStream = xmlNewInputStream(ctxt);
Daniel Veillard30e76072006-03-09 14:13:55 +00001549 if (inputStream == NULL)
Owen Taylor3473f882001-02-23 17:55:21 +00001550 return(NULL);
Daniel Veillard30e76072006-03-09 14:13:55 +00001551
Daniel Veillarda840b692003-10-19 13:35:37 +00001552 inputStream->buf = buf;
1553 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1554 if (inputStream == NULL)
1555 return(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001556
Daniel Veillarda840b692003-10-19 13:35:37 +00001557 if (inputStream->filename == NULL)
1558 URI = xmlStrdup((xmlChar *) filename);
1559 else
1560 URI = xmlStrdup((xmlChar *) inputStream->filename);
1561 directory = xmlParserGetDirectory((const char *) URI);
Daniel Veillard5addfeb2006-10-17 20:32:22 +00001562 if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001563 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001564 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001565 inputStream->directory = directory;
Owen Taylor3473f882001-02-23 17:55:21 +00001566
Daniel Veillard61551a12012-07-16 16:28:47 +08001567 xmlBufResetInput(inputStream->buf->buffer, inputStream);
Owen Taylor3473f882001-02-23 17:55:21 +00001568 if ((ctxt->directory == NULL) && (directory != NULL))
1569 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1570 return(inputStream);
1571}
1572
1573/************************************************************************
1574 * *
1575 * Commodity functions to handle parser contexts *
1576 * *
1577 ************************************************************************/
1578
1579/**
1580 * xmlInitParserCtxt:
1581 * @ctxt: an XML parser context
1582 *
1583 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001584 *
1585 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001586 */
1587
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001588int
Owen Taylor3473f882001-02-23 17:55:21 +00001589xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1590{
Daniel Veillarda521d282004-11-09 14:59:59 +00001591 xmlParserInputPtr input;
1592
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001593 if(ctxt==NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001594 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001595 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001596 }
1597
Owen Taylor3473f882001-02-23 17:55:21 +00001598 xmlDefaultSAXHandlerInit();
1599
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001600 if (ctxt->dict == NULL)
1601 ctxt->dict = xmlDictCreate();
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001602 if (ctxt->dict == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001603 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001604 return(-1);
1605 }
Daniel Veillard52d8ade2012-07-30 10:08:45 +08001606 xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
1607
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001608 if (ctxt->sax == NULL)
1609 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
William M. Brack8b2c7f12002-11-22 05:07:29 +00001610 if (ctxt->sax == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001611 xmlErrMemory(NULL, "cannot initialize parser context\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001612 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001613 }
1614 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001615 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001616
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001617 ctxt->maxatts = 0;
1618 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001619 /* Allocate the Input stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001620 if (ctxt->inputTab == NULL) {
1621 ctxt->inputTab = (xmlParserInputPtr *)
1622 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1623 ctxt->inputMax = 5;
1624 }
Owen Taylor3473f882001-02-23 17:55:21 +00001625 if (ctxt->inputTab == 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->inputNr = 0;
1628 ctxt->inputMax = 0;
1629 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001630 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001631 }
Daniel Veillarda521d282004-11-09 14:59:59 +00001632 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1633 xmlFreeInputStream(input);
1634 }
Owen Taylor3473f882001-02-23 17:55:21 +00001635 ctxt->inputNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001636 ctxt->input = NULL;
1637
1638 ctxt->version = NULL;
1639 ctxt->encoding = NULL;
1640 ctxt->standalone = -1;
1641 ctxt->hasExternalSubset = 0;
1642 ctxt->hasPErefs = 0;
1643 ctxt->html = 0;
1644 ctxt->external = 0;
1645 ctxt->instate = XML_PARSER_START;
1646 ctxt->token = 0;
1647 ctxt->directory = NULL;
1648
1649 /* Allocate the Node stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001650 if (ctxt->nodeTab == NULL) {
1651 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1652 ctxt->nodeMax = 10;
1653 }
Owen Taylor3473f882001-02-23 17:55:21 +00001654 if (ctxt->nodeTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001655 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001656 ctxt->nodeNr = 0;
1657 ctxt->nodeMax = 0;
1658 ctxt->node = NULL;
1659 ctxt->inputNr = 0;
1660 ctxt->inputMax = 0;
1661 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001662 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001663 }
1664 ctxt->nodeNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001665 ctxt->node = NULL;
1666
1667 /* Allocate the Name stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001668 if (ctxt->nameTab == NULL) {
1669 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1670 ctxt->nameMax = 10;
1671 }
Owen Taylor3473f882001-02-23 17:55:21 +00001672 if (ctxt->nameTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001673 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001674 ctxt->nodeNr = 0;
1675 ctxt->nodeMax = 0;
1676 ctxt->node = NULL;
1677 ctxt->inputNr = 0;
1678 ctxt->inputMax = 0;
1679 ctxt->input = NULL;
1680 ctxt->nameNr = 0;
1681 ctxt->nameMax = 0;
1682 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001683 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001684 }
1685 ctxt->nameNr = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001686 ctxt->name = NULL;
1687
1688 /* Allocate the space stack */
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001689 if (ctxt->spaceTab == NULL) {
1690 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1691 ctxt->spaceMax = 10;
1692 }
Owen Taylor3473f882001-02-23 17:55:21 +00001693 if (ctxt->spaceTab == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001694 xmlErrMemory(NULL, "cannot initialize parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001695 ctxt->nodeNr = 0;
1696 ctxt->nodeMax = 0;
1697 ctxt->node = NULL;
1698 ctxt->inputNr = 0;
1699 ctxt->inputMax = 0;
1700 ctxt->input = NULL;
1701 ctxt->nameNr = 0;
1702 ctxt->nameMax = 0;
1703 ctxt->name = NULL;
1704 ctxt->spaceNr = 0;
1705 ctxt->spaceMax = 0;
1706 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001707 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001708 }
1709 ctxt->spaceNr = 1;
1710 ctxt->spaceMax = 10;
1711 ctxt->spaceTab[0] = -1;
1712 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00001713 ctxt->userData = ctxt;
1714 ctxt->myDoc = NULL;
1715 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00001716 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001717 ctxt->valid = 1;
1718 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001719 if (ctxt->loadsubset) {
1720 ctxt->options |= XML_PARSE_DTDLOAD;
1721 }
Owen Taylor3473f882001-02-23 17:55:21 +00001722 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1723 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001724 if (ctxt->pedantic) {
1725 ctxt->options |= XML_PARSE_PEDANTIC;
1726 }
Daniel Veillarda53c6882001-07-25 17:18:57 +00001727 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00001728 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001729 if (ctxt->keepBlanks == 0) {
Daniel Veillard11476b42003-09-26 14:51:39 +00001730 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001731 ctxt->options |= XML_PARSE_NOBLANKS;
1732 }
Daniel Veillard16698282001-09-14 10:29:27 +00001733
Daniel Veillardeff45a92004-10-29 12:10:55 +00001734 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
Owen Taylor3473f882001-02-23 17:55:21 +00001735 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00001736 ctxt->vctxt.error = xmlParserValidityError;
1737 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00001738 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00001739 if (xmlGetWarningsDefaultValue == 0)
1740 ctxt->vctxt.warning = NULL;
1741 else
1742 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00001743 ctxt->vctxt.nodeMax = 0;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001744 ctxt->options |= XML_PARSE_DTDVALID;
Owen Taylor3473f882001-02-23 17:55:21 +00001745 }
1746 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
Daniel Veillardc35af8b2014-06-11 16:59:16 +08001747 if (ctxt->replaceEntities) {
1748 ctxt->options |= XML_PARSE_NOENT;
1749 }
Owen Taylor3473f882001-02-23 17:55:21 +00001750 ctxt->record_info = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001751 ctxt->checkIndex = 0;
1752 ctxt->inSubset = 0;
1753 ctxt->errNo = XML_ERR_OK;
1754 ctxt->depth = 0;
1755 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001756 ctxt->catalogs = NULL;
Daniel Veillard4bf899b2008-08-20 17:04:30 +00001757 ctxt->nbentities = 0;
Daniel Veillard23f05e02013-02-19 10:21:49 +08001758 ctxt->sizeentities = 0;
1759 ctxt->sizeentcopy = 0;
Daniel Veillard0d51cfe2012-05-15 11:18:40 +08001760 ctxt->input_id = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001761 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001762 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001763}
1764
1765/**
1766 * xmlFreeParserCtxt:
1767 * @ctxt: an XML parser context
1768 *
1769 * Free all the memory used by a parser context. However the parsed
1770 * document in ctxt->myDoc is not freed.
1771 */
1772
1773void
1774xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1775{
1776 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00001777
1778 if (ctxt == NULL) return;
1779
1780 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1781 xmlFreeInputStream(input);
1782 }
Owen Taylor3473f882001-02-23 17:55:21 +00001783 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001784 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001785 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
Eugene Pimenov615904f2010-03-15 15:16:02 +01001786 if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
Owen Taylor3473f882001-02-23 17:55:21 +00001787 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1788 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1789 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00001790 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1791 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00001792#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00001793 if ((ctxt->sax != NULL) &&
1794 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00001795#else
1796 if (ctxt->sax != NULL)
1797#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001798 xmlFree(ctxt->sax);
1799 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00001800 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00001801 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001802 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +00001803 if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001804 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1805 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001806 if (ctxt->attsDefault != NULL)
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01001807 xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001808 if (ctxt->attsSpecial != NULL)
1809 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001810 if (ctxt->freeElems != NULL) {
1811 xmlNodePtr cur, next;
1812
1813 cur = ctxt->freeElems;
1814 while (cur != NULL) {
1815 next = cur->next;
1816 xmlFree(cur);
1817 cur = next;
1818 }
1819 }
1820 if (ctxt->freeAttrs != NULL) {
1821 xmlAttrPtr cur, next;
1822
1823 cur = ctxt->freeAttrs;
1824 while (cur != NULL) {
1825 next = cur->next;
1826 xmlFree(cur);
1827 cur = next;
1828 }
1829 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00001830 /*
1831 * cleanup the error strings
1832 */
1833 if (ctxt->lastError.message != NULL)
1834 xmlFree(ctxt->lastError.message);
1835 if (ctxt->lastError.file != NULL)
1836 xmlFree(ctxt->lastError.file);
1837 if (ctxt->lastError.str1 != NULL)
1838 xmlFree(ctxt->lastError.str1);
1839 if (ctxt->lastError.str2 != NULL)
1840 xmlFree(ctxt->lastError.str2);
1841 if (ctxt->lastError.str3 != NULL)
1842 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00001843
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00001844#ifdef LIBXML_CATALOG_ENABLED
1845 if (ctxt->catalogs != NULL)
1846 xmlCatalogFreeLocal(ctxt->catalogs);
1847#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001848 xmlFree(ctxt);
1849}
1850
1851/**
1852 * xmlNewParserCtxt:
1853 *
1854 * Allocate and initialize a new parser context.
1855 *
1856 * Returns the xmlParserCtxtPtr or NULL
1857 */
1858
1859xmlParserCtxtPtr
Daniel Veillard75e389d2005-07-29 22:02:24 +00001860xmlNewParserCtxt(void)
Owen Taylor3473f882001-02-23 17:55:21 +00001861{
1862 xmlParserCtxtPtr ctxt;
1863
1864 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1865 if (ctxt == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00001866 xmlErrMemory(NULL, "cannot allocate parser context\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001867 return(NULL);
1868 }
1869 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001870 if (xmlInitParserCtxt(ctxt) < 0) {
1871 xmlFreeParserCtxt(ctxt);
1872 return(NULL);
1873 }
Owen Taylor3473f882001-02-23 17:55:21 +00001874 return(ctxt);
1875}
1876
1877/************************************************************************
1878 * *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001879 * Handling of node information *
Owen Taylor3473f882001-02-23 17:55:21 +00001880 * *
1881 ************************************************************************/
1882
1883/**
1884 * xmlClearParserCtxt:
1885 * @ctxt: an XML parser context
1886 *
1887 * Clear (release owned resources) and reinitialize a parser context
1888 */
1889
1890void
1891xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1892{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001893 if (ctxt==NULL)
1894 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001895 xmlClearNodeInfoSeq(&ctxt->node_seq);
Daniel Veillard36e5cd52004-11-02 14:52:23 +00001896 xmlCtxtReset(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001897}
1898
Daniel Veillardce682bc2004-11-05 17:22:25 +00001899
Owen Taylor3473f882001-02-23 17:55:21 +00001900/**
1901 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00001902 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00001903 * @node: an XML node within the tree
1904 *
1905 * Find the parser node info struct for a given node
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001906 *
Owen Taylor3473f882001-02-23 17:55:21 +00001907 * Returns an xmlParserNodeInfo block pointer or NULL
1908 */
Daniel Veillardce682bc2004-11-05 17:22:25 +00001909const xmlParserNodeInfo *
1910xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001911{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001912 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00001913
Daniel Veillardce682bc2004-11-05 17:22:25 +00001914 if ((ctx == NULL) || (node == NULL))
1915 return (NULL);
1916 /* Find position where node should be at */
1917 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
1918 if (pos < ctx->node_seq.length
1919 && ctx->node_seq.buffer[pos].node == node)
1920 return &ctx->node_seq.buffer[pos];
1921 else
1922 return NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001923}
1924
1925
1926/**
1927 * xmlInitNodeInfoSeq:
1928 * @seq: a node info sequence pointer
1929 *
1930 * -- Initialize (set to initial state) node info sequence
1931 */
1932void
1933xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1934{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001935 if (seq == NULL)
1936 return;
1937 seq->length = 0;
1938 seq->maximum = 0;
1939 seq->buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001940}
1941
1942/**
1943 * xmlClearNodeInfoSeq:
1944 * @seq: a node info sequence pointer
1945 *
1946 * -- Clear (release memory and reinitialize) node
1947 * info sequence
1948 */
1949void
1950xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1951{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001952 if (seq == NULL)
1953 return;
1954 if (seq->buffer != NULL)
1955 xmlFree(seq->buffer);
1956 xmlInitNodeInfoSeq(seq);
Owen Taylor3473f882001-02-23 17:55:21 +00001957}
1958
Owen Taylor3473f882001-02-23 17:55:21 +00001959/**
1960 * xmlParserFindNodeInfoIndex:
1961 * @seq: a node info sequence pointer
1962 * @node: an XML node pointer
1963 *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001964 *
Owen Taylor3473f882001-02-23 17:55:21 +00001965 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1966 * the given node is or should be at in a sorted sequence
1967 *
1968 * Returns a long indicating the position of the record
1969 */
Daniel Veillardce682bc2004-11-05 17:22:25 +00001970unsigned long
1971xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
1972 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00001973{
Daniel Veillardce682bc2004-11-05 17:22:25 +00001974 unsigned long upper, lower, middle;
1975 int found = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001976
Daniel Veillardce682bc2004-11-05 17:22:25 +00001977 if ((seq == NULL) || (node == NULL))
Daniel Veillard6a0baa02005-12-10 11:11:12 +00001978 return ((unsigned long) -1);
Daniel Veillardce682bc2004-11-05 17:22:25 +00001979
1980 /* Do a binary search for the key */
1981 lower = 1;
1982 upper = seq->length;
1983 middle = 0;
1984 while (lower <= upper && !found) {
1985 middle = lower + (upper - lower) / 2;
1986 if (node == seq->buffer[middle - 1].node)
1987 found = 1;
1988 else if (node < seq->buffer[middle - 1].node)
1989 upper = middle - 1;
1990 else
1991 lower = middle + 1;
1992 }
1993
1994 /* Return position */
1995 if (middle == 0 || seq->buffer[middle - 1].node < node)
1996 return middle;
Owen Taylor3473f882001-02-23 17:55:21 +00001997 else
Daniel Veillardce682bc2004-11-05 17:22:25 +00001998 return middle - 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001999}
2000
2001
2002/**
2003 * xmlParserAddNodeInfo:
2004 * @ctxt: an XML parser context
2005 * @info: a node info sequence pointer
2006 *
2007 * Insert node info record into the sorted sequence
2008 */
2009void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002010xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002011 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00002012{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002013 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002014
Daniel Veillardce682bc2004-11-05 17:22:25 +00002015 if ((ctxt == NULL) || (info == NULL)) return;
2016
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002017 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00002018 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002019 info->node);
Daniel Veillard30e76072006-03-09 14:13:55 +00002020
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002021 if ((pos < ctxt->node_seq.length) &&
Daniel Veillard30e76072006-03-09 14:13:55 +00002022 (ctxt->node_seq.buffer != NULL) &&
2023 (ctxt->node_seq.buffer[pos].node == info->node)) {
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002024 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00002025 }
2026
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002027 /* Otherwise, we need to add new node to buffer */
2028 else {
Daniel Veillardff76eb22013-08-03 22:25:13 +08002029 if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
2030 (ctxt->node_seq.buffer == NULL)) {
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002031 xmlParserNodeInfo *tmp_buffer;
2032 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00002033
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002034 if (ctxt->node_seq.maximum == 0)
2035 ctxt->node_seq.maximum = 2;
2036 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2037 (2 * ctxt->node_seq.maximum));
2038
2039 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00002040 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002041 else
2042 tmp_buffer =
2043 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2044 byte_size);
2045
2046 if (tmp_buffer == NULL) {
Daniel Veillardce9457f2003-10-05 21:33:18 +00002047 xmlErrMemory(ctxt, "failed to allocate buffer\n");
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002048 return;
2049 }
2050 ctxt->node_seq.buffer = tmp_buffer;
2051 ctxt->node_seq.maximum *= 2;
2052 }
2053
2054 /* If position is not at end, move elements out of the way */
2055 if (pos != ctxt->node_seq.length) {
2056 unsigned long i;
2057
2058 for (i = ctxt->node_seq.length; i > pos; i--)
2059 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2060 }
2061
2062 /* Copy element and increase length */
2063 ctxt->node_seq.buffer[pos] = *info;
2064 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00002065 }
Owen Taylor3473f882001-02-23 17:55:21 +00002066}
2067
2068/************************************************************************
2069 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00002070 * Defaults settings *
2071 * *
2072 ************************************************************************/
2073/**
2074 * xmlPedanticParserDefault:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002075 * @val: int 0 or 1
Daniel Veillarda53c6882001-07-25 17:18:57 +00002076 *
2077 * Set and return the previous value for enabling pedantic warnings.
2078 *
2079 * Returns the last value for 0 for no substitution, 1 for substitution.
2080 */
2081
2082int
2083xmlPedanticParserDefault(int val) {
2084 int old = xmlPedanticParserDefaultValue;
2085
2086 xmlPedanticParserDefaultValue = val;
2087 return(old);
2088}
2089
2090/**
2091 * xmlLineNumbersDefault:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002092 * @val: int 0 or 1
Daniel Veillarda53c6882001-07-25 17:18:57 +00002093 *
2094 * Set and return the previous value for enabling line numbers in elements
2095 * contents. This may break on old application and is turned off by default.
2096 *
2097 * Returns the last value for 0 for no substitution, 1 for substitution.
2098 */
2099
2100int
2101xmlLineNumbersDefault(int val) {
2102 int old = xmlLineNumbersDefaultValue;
2103
2104 xmlLineNumbersDefaultValue = val;
2105 return(old);
2106}
2107
2108/**
2109 * xmlSubstituteEntitiesDefault:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002110 * @val: int 0 or 1
Daniel Veillarda53c6882001-07-25 17:18:57 +00002111 *
2112 * Set and return the previous value for default entity support.
2113 * Initially the parser always keep entity references instead of substituting
2114 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002115 * default parser behavior
2116 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00002117 * file basis.
2118 *
2119 * Returns the last value for 0 for no substitution, 1 for substitution.
2120 */
2121
2122int
2123xmlSubstituteEntitiesDefault(int val) {
2124 int old = xmlSubstituteEntitiesDefaultValue;
2125
2126 xmlSubstituteEntitiesDefaultValue = val;
2127 return(old);
2128}
2129
2130/**
2131 * xmlKeepBlanksDefault:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002132 * @val: int 0 or 1
Daniel Veillarda53c6882001-07-25 17:18:57 +00002133 *
2134 * Set and return the previous value for default blanks text nodes support.
2135 * The 1.x version of the parser used an heuristic to try to detect
2136 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00002137 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00002138 * using the DOM output text nodes containing those blanks were not generated.
2139 * The 2.x and later version will switch to the XML standard way and
2140 * ignorableWhitespace() are only generated when running the parser in
2141 * validating mode and when the current element doesn't allow CDATA or
2142 * mixed content.
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002143 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002144 * on 1.X libs and to switch back to the old mode for compatibility when
2145 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2146 * by using xmlIsBlankNode() commodity function to detect the "empty"
2147 * nodes generated.
2148 * This value also affect autogeneration of indentation when saving code
2149 * if blanks sections are kept, indentation is not generated.
2150 *
2151 * Returns the last value for 0 for no substitution, 1 for substitution.
2152 */
2153
2154int
2155xmlKeepBlanksDefault(int val) {
2156 int old = xmlKeepBlanksDefaultValue;
2157
2158 xmlKeepBlanksDefaultValue = val;
Nick Wellnhofer2f522dc2009-08-20 12:11:17 +02002159 if (!val) xmlIndentTreeOutput = 1;
Daniel Veillarda53c6882001-07-25 17:18:57 +00002160 return(old);
2161}
2162
Daniel Veillard5d4644e2005-04-01 13:11:58 +00002163#define bottom_parserInternals
2164#include "elfgcchack.h"