1 /* libxml2 - Library for parsing XML documents
2 * Copyright (C) 2006-2019 Free Software Foundation, Inc.
3 *
4 * This file is not part of the GNU gettext program, but is used with
5 * GNU gettext.
6 *
7 * The original copyright notice is as follows:
8 */
9
10 /*
11 * Copyright (C) 1998-2012 Daniel Veillard. All Rights Reserved.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is fur-
18 * nished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
25 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * THE SOFTWARE.
30 *
31 * daniel@veillard.com
32 */
33
34 /*
35 * parserInternals.c : Internal routines (and obsolete ones) needed for the
36 * XML and HTML parsers.
37 */
38
39 #define IN_LIBXML
40 #include "libxml.h"
41
42 #if defined(_WIN32) && !defined (__CYGWIN__)
43 #define XML_DIR_SEP '\\'
44 #else
45 #define XML_DIR_SEP '/'
46 #endif
47
48 #include <string.h>
49 #ifdef HAVE_CTYPE_H
50 #include <ctype.h>
51 #endif
52 #ifdef HAVE_STDLIB_H
53 #include <stdlib.h>
54 #endif
55 #ifdef HAVE_SYS_STAT_H
56 #include <sys/stat.h>
57 #endif
58 #ifdef HAVE_FCNTL_H
59 #include <fcntl.h>
60 #endif
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
64 #ifdef LIBXML_ZLIB_ENABLED
65 #include <zlib.h>
66 #endif
67
68 #include <libxml/xmlmemory.h>
69 #include <libxml/tree.h>
70 #include <libxml/parser.h>
71 #include <libxml/parserInternals.h>
72 #include <libxml/valid.h>
73 #include <libxml/entities.h>
74 #include <libxml/xmlerror.h>
75 #include <libxml/encoding.h>
76 #include <libxml/valid.h>
77 #include <libxml/xmlIO.h>
78 #include <libxml/uri.h>
79 #include <libxml/dict.h>
80 #include <libxml/SAX.h>
81 #ifdef LIBXML_CATALOG_ENABLED
82 #include <libxml/catalog.h>
83 #endif
84 #include <libxml/globals.h>
85 #include <libxml/chvalid.h>
86
87 #define CUR(ctxt) ctxt->input->cur
88 #define END(ctxt) ctxt->input->end
89 #define VALID_CTXT(ctxt) (CUR(ctxt) <= END(ctxt))
90
91 #include "buf.h"
92 #include "enc.h"
93
94 /*
95 * Various global defaults for parsing
96 */
97
98 /**
99 * xmlCheckVersion:
100 * @version: the include version number
101 *
102 * check the compiled lib version against the include one.
103 * This can warn or immediately kill the application
104 */
105 void
xmlCheckVersion(int version)106 xmlCheckVersion(int version) {
107 int myversion = (int) LIBXML_VERSION;
108
109 xmlInitParser();
110
111 if ((myversion / 10000) != (version / 10000)) {
112 xmlGenericError(xmlGenericErrorContext,
113 "Fatal: program compiled against libxml %d using libxml %d\n",
114 (version / 10000), (myversion / 10000));
115 fprintf(stderr,
116 "Fatal: program compiled against libxml %d using libxml %d\n",
117 (version / 10000), (myversion / 10000));
118 }
119 if ((myversion / 100) < (version / 100)) {
120 xmlGenericError(xmlGenericErrorContext,
121 "Warning: program compiled against libxml %d using older %d\n",
122 (version / 100), (myversion / 100));
123 }
124 }
125
126
127 /************************************************************************
128 * *
129 * Some factorized error routines *
130 * *
131 ************************************************************************/
132
133
134 /**
135 * xmlErrMemory:
136 * @ctxt: an XML parser context
137 * @extra: extra informations
138 *
139 * Handle a redefinition of attribute error
140 */
141 void
xmlErrMemory(xmlParserCtxtPtr ctxt,const char * extra)142 xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
143 {
144 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
145 (ctxt->instate == XML_PARSER_EOF))
146 return;
147 if (ctxt != NULL) {
148 ctxt->errNo = XML_ERR_NO_MEMORY;
149 ctxt->instate = XML_PARSER_EOF;
150 ctxt->disableSAX = 1;
151 }
152 if (extra)
153 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
154 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
155 NULL, NULL, 0, 0,
156 "Memory allocation failed : %s\n", extra);
157 else
158 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
159 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
160 NULL, NULL, 0, 0, "Memory allocation failed\n");
161 }
162
163 /**
164 * __xmlErrEncoding:
165 * @ctxt: an XML parser context
166 * @xmlerr: the error number
167 * @msg: the error message
168 * @str1: an string info
169 * @str2: an string info
170 *
171 * Handle an encoding error
172 */
173 void
__xmlErrEncoding(xmlParserCtxtPtr ctxt,xmlParserErrors xmlerr,const char * msg,const xmlChar * str1,const xmlChar * str2)174 __xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
175 const char *msg, const xmlChar * str1, const xmlChar * str2)
176 {
177 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
178 (ctxt->instate == XML_PARSER_EOF))
179 return;
180 if (ctxt != NULL)
181 ctxt->errNo = xmlerr;
182 __xmlRaiseError(NULL, NULL, NULL,
183 ctxt, NULL, XML_FROM_PARSER, xmlerr, XML_ERR_FATAL,
184 NULL, 0, (const char *) str1, (const char *) str2,
185 NULL, 0, 0, msg, str1, str2);
186 if (ctxt != NULL) {
187 ctxt->wellFormed = 0;
188 if (ctxt->recovery == 0)
189 ctxt->disableSAX = 1;
190 }
191 }
192
193 /**
194 * xmlErrInternal:
195 * @ctxt: an XML parser context
196 * @msg: the error message
197 * @str: error informations
198 *
199 * Handle an internal error
200 */
201 static void LIBXML_ATTR_FORMAT(2,0)
xmlErrInternal(xmlParserCtxtPtr ctxt,const char * msg,const xmlChar * str)202 xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str)
203 {
204 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
205 (ctxt->instate == XML_PARSER_EOF))
206 return;
207 if (ctxt != NULL)
208 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
209 __xmlRaiseError(NULL, NULL, NULL,
210 ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR,
211 XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL,
212 0, 0, msg, str);
213 if (ctxt != NULL) {
214 ctxt->wellFormed = 0;
215 if (ctxt->recovery == 0)
216 ctxt->disableSAX = 1;
217 }
218 }
219
220 /**
221 * xmlErrEncodingInt:
222 * @ctxt: an XML parser context
223 * @error: the error number
224 * @msg: the error message
225 * @val: an integer value
226 *
227 * n encoding error
228 */
229 static void LIBXML_ATTR_FORMAT(3,0)
xmlErrEncodingInt(xmlParserCtxtPtr ctxt,xmlParserErrors error,const char * msg,int val)230 xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
231 const char *msg, int val)
232 {
233 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
234 (ctxt->instate == XML_PARSER_EOF))
235 return;
236 if (ctxt != NULL)
237 ctxt->errNo = error;
238 __xmlRaiseError(NULL, NULL, NULL,
239 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
240 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
241 if (ctxt != NULL) {
242 ctxt->wellFormed = 0;
243 if (ctxt->recovery == 0)
244 ctxt->disableSAX = 1;
245 }
246 }
247
248 /**
249 * xmlIsLetter:
250 * @c: an unicode character (int)
251 *
252 * Check whether the character is allowed by the production
253 * [84] Letter ::= BaseChar | Ideographic
254 *
255 * Returns 0 if not, non-zero otherwise
256 */
257 int
xmlIsLetter(int c)258 xmlIsLetter(int c) {
259 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
260 }
261
262 /************************************************************************
263 * *
264 * Input handling functions for progressive parsing *
265 * *
266 ************************************************************************/
267
268 /* #define DEBUG_INPUT */
269 /* #define DEBUG_STACK */
270 /* #define DEBUG_PUSH */
271
272
273 /* we need to keep enough input to show errors in context */
274 #define LINE_LEN 80
275
276 #ifdef DEBUG_INPUT
277 #define CHECK_BUFFER(in) check_buffer(in)
278
279 static
check_buffer(xmlParserInputPtr in)280 void check_buffer(xmlParserInputPtr in) {
281 if (in->base != xmlBufContent(in->buf->buffer)) {
282 xmlGenericError(xmlGenericErrorContext,
283 "xmlParserInput: base mismatch problem\n");
284 }
285 if (in->cur < in->base) {
286 xmlGenericError(xmlGenericErrorContext,
287 "xmlParserInput: cur < base problem\n");
288 }
289 if (in->cur > in->base + xmlBufUse(in->buf->buffer)) {
290 xmlGenericError(xmlGenericErrorContext,
291 "xmlParserInput: cur > base + use problem\n");
292 }
293 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d\n",
294 (int) in, (int) xmlBufContent(in->buf->buffer), in->cur - in->base,
295 xmlBufUse(in->buf->buffer));
296 }
297
298 #else
299 #define CHECK_BUFFER(in)
300 #endif
301
302
303 /**
304 * xmlParserInputRead:
305 * @in: an XML parser input
306 * @len: an indicative size for the lookahead
307 *
308 * This function was internal and is deprecated.
309 *
310 * Returns -1 as this is an error to use it.
311 */
312 int
xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED,int len ATTRIBUTE_UNUSED)313 xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) {
314 return(-1);
315 }
316
317 /**
318 * xmlParserInputGrow:
319 * @in: an XML parser input
320 * @len: an indicative size for the lookahead
321 *
322 * This function increase the input for the parser. It tries to
323 * preserve pointers to the input buffer, and keep already read data
324 *
325 * Returns the amount of char read, or -1 in case of error, 0 indicate the
326 * end of this entity
327 */
328 int
xmlParserInputGrow(xmlParserInputPtr in,int len)329 xmlParserInputGrow(xmlParserInputPtr in, int len) {
330 int ret;
331 size_t indx;
332 const xmlChar *content;
333
334 if ((in == NULL) || (len < 0)) return(-1);
335 #ifdef DEBUG_INPUT
336 xmlGenericError(xmlGenericErrorContext, "Grow\n");
337 #endif
338 if (in->buf == NULL) return(-1);
339 if (in->base == NULL) return(-1);
340 if (in->cur == NULL) return(-1);
341 if (in->buf->buffer == NULL) return(-1);
342
343 CHECK_BUFFER(in);
344
345 indx = in->cur - in->base;
346 if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) {
347
348 CHECK_BUFFER(in);
349
350 return(0);
351 }
352 if (in->buf->readcallback != NULL) {
353 ret = xmlParserInputBufferGrow(in->buf, len);
354 } else
355 return(0);
356
357 /*
358 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
359 * block, but we use it really as an integer to do some
360 * pointer arithmetic. Insure will raise it as a bug but in
361 * that specific case, that's not !
362 */
363
364 content = xmlBufContent(in->buf->buffer);
365 if (in->base != content) {
366 /*
367 * the buffer has been reallocated
368 */
369 indx = in->cur - in->base;
370 in->base = content;
371 in->cur = &content[indx];
372 }
373 in->end = xmlBufEnd(in->buf->buffer);
374
375 CHECK_BUFFER(in);
376
377 return(ret);
378 }
379
380 /**
381 * xmlParserInputShrink:
382 * @in: an XML parser input
383 *
384 * This function removes used input for the parser.
385 */
386 void
xmlParserInputShrink(xmlParserInputPtr in)387 xmlParserInputShrink(xmlParserInputPtr in) {
388 size_t used;
389 size_t ret;
390 size_t indx;
391 const xmlChar *content;
392
393 #ifdef DEBUG_INPUT
394 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
395 #endif
396 if (in == NULL) return;
397 if (in->buf == NULL) return;
398 if (in->base == NULL) return;
399 if (in->cur == NULL) return;
400 if (in->buf->buffer == NULL) return;
401
402 CHECK_BUFFER(in);
403
404 used = in->cur - xmlBufContent(in->buf->buffer);
405 /*
406 * Do not shrink on large buffers whose only a tiny fraction
407 * was consumed
408 */
409 if (used > INPUT_CHUNK) {
410 ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN);
411 if (ret > 0) {
412 in->cur -= ret;
413 in->consumed += ret;
414 }
415 in->end = xmlBufEnd(in->buf->buffer);
416 }
417
418 CHECK_BUFFER(in);
419
420 if (xmlBufUse(in->buf->buffer) > INPUT_CHUNK) {
421 return;
422 }
423 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
424 content = xmlBufContent(in->buf->buffer);
425 if (in->base != content) {
426 /*
427 * the buffer has been reallocated
428 */
429 indx = in->cur - in->base;
430 in->base = content;
431 in->cur = &content[indx];
432 }
433 in->end = xmlBufEnd(in->buf->buffer);
434
435 CHECK_BUFFER(in);
436 }
437
438 /************************************************************************
439 * *
440 * UTF8 character input and related functions *
441 * *
442 ************************************************************************/
443
444 /**
445 * xmlNextChar:
446 * @ctxt: the XML parser context
447 *
448 * Skip to the next char input char.
449 */
450
451 void
xmlNextChar(xmlParserCtxtPtr ctxt)452 xmlNextChar(xmlParserCtxtPtr ctxt)
453 {
454 if ((ctxt == NULL) || (ctxt->instate == XML_PARSER_EOF) ||
455 (ctxt->input == NULL))
456 return;
457
458 if (!(VALID_CTXT(ctxt))) {
459 xmlErrInternal(ctxt, "Parser input data memory error\n", NULL);
460 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
461 xmlStopParser(ctxt);
462 return;
463 }
464
465 if ((*ctxt->input->cur == 0) &&
466 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
467 return;
468 }
469
470 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
471 const unsigned char *cur;
472 unsigned char c;
473
474 /*
475 * 2.11 End-of-Line Handling
476 * the literal two-character sequence "#xD#xA" or a standalone
477 * literal #xD, an XML processor must pass to the application
478 * the single character #xA.
479 */
480 if (*(ctxt->input->cur) == '\n') {
481 ctxt->input->line++; ctxt->input->col = 1;
482 } else
483 ctxt->input->col++;
484
485 /*
486 * We are supposed to handle UTF8, check it's valid
487 * From rfc2044: encoding of the Unicode values on UTF-8:
488 *
489 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
490 * 0000 0000-0000 007F 0xxxxxxx
491 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
492 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
493 *
494 * Check for the 0x110000 limit too
495 */
496 cur = ctxt->input->cur;
497
498 c = *cur;
499 if (c & 0x80) {
500 if (c == 0xC0)
501 goto encoding_error;
502 if (cur[1] == 0) {
503 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
504 cur = ctxt->input->cur;
505 }
506 if ((cur[1] & 0xc0) != 0x80)
507 goto encoding_error;
508 if ((c & 0xe0) == 0xe0) {
509 unsigned int val;
510
511 if (cur[2] == 0) {
512 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
513 cur = ctxt->input->cur;
514 }
515 if ((cur[2] & 0xc0) != 0x80)
516 goto encoding_error;
517 if ((c & 0xf0) == 0xf0) {
518 if (cur[3] == 0) {
519 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
520 cur = ctxt->input->cur;
521 }
522 if (((c & 0xf8) != 0xf0) ||
523 ((cur[3] & 0xc0) != 0x80))
524 goto encoding_error;
525 /* 4-byte code */
526 ctxt->input->cur += 4;
527 val = (cur[0] & 0x7) << 18;
528 val |= (cur[1] & 0x3f) << 12;
529 val |= (cur[2] & 0x3f) << 6;
530 val |= cur[3] & 0x3f;
531 } else {
532 /* 3-byte code */
533 ctxt->input->cur += 3;
534 val = (cur[0] & 0xf) << 12;
535 val |= (cur[1] & 0x3f) << 6;
536 val |= cur[2] & 0x3f;
537 }
538 if (((val > 0xd7ff) && (val < 0xe000)) ||
539 ((val > 0xfffd) && (val < 0x10000)) ||
540 (val >= 0x110000)) {
541 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
542 "Char 0x%X out of allowed range\n",
543 val);
544 }
545 } else
546 /* 2-byte code */
547 ctxt->input->cur += 2;
548 } else
549 /* 1-byte code */
550 ctxt->input->cur++;
551
552 ctxt->nbChars++;
553 } else {
554 /*
555 * Assume it's a fixed length encoding (1) with
556 * a compatible encoding for the ASCII set, since
557 * XML constructs only use < 128 chars
558 */
559
560 if (*(ctxt->input->cur) == '\n') {
561 ctxt->input->line++; ctxt->input->col = 1;
562 } else
563 ctxt->input->col++;
564 ctxt->input->cur++;
565 ctxt->nbChars++;
566 }
567 if (*ctxt->input->cur == 0)
568 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
569 return;
570 encoding_error:
571 /*
572 * If we detect an UTF8 error that probably mean that the
573 * input encoding didn't get properly advertised in the
574 * declaration header. Report the error and switch the encoding
575 * to ISO-Latin-1 (if you don't like this policy, just declare the
576 * encoding !)
577 */
578 if ((ctxt == NULL) || (ctxt->input == NULL) ||
579 (ctxt->input->end - ctxt->input->cur < 4)) {
580 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
581 "Input is not proper UTF-8, indicate encoding !\n",
582 NULL, NULL);
583 } else {
584 char buffer[150];
585
586 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
587 ctxt->input->cur[0], ctxt->input->cur[1],
588 ctxt->input->cur[2], ctxt->input->cur[3]);
589 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
590 "Input is not proper UTF-8, indicate encoding !\n%s",
591 BAD_CAST buffer, NULL);
592 }
593 ctxt->charset = XML_CHAR_ENCODING_8859_1;
594 ctxt->input->cur++;
595 return;
596 }
597
598 /**
599 * xmlCurrentChar:
600 * @ctxt: the XML parser context
601 * @len: pointer to the length of the char read
602 *
603 * The current char value, if using UTF-8 this may actually span multiple
604 * bytes in the input buffer. Implement the end of line normalization:
605 * 2.11 End-of-Line Handling
606 * Wherever an external parsed entity or the literal entity value
607 * of an internal parsed entity contains either the literal two-character
608 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
609 * must pass to the application the single character #xA.
610 * This behavior can conveniently be produced by normalizing all
611 * line breaks to #xA on input, before parsing.)
612 *
613 * Returns the current char value and its length
614 */
615
616 int
xmlCurrentChar(xmlParserCtxtPtr ctxt,int * len)617 xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
618 if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0);
619 if (ctxt->instate == XML_PARSER_EOF)
620 return(0);
621
622 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
623 *len = 1;
624 return((int) *ctxt->input->cur);
625 }
626 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
627 /*
628 * We are supposed to handle UTF8, check it's valid
629 * From rfc2044: encoding of the Unicode values on UTF-8:
630 *
631 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
632 * 0000 0000-0000 007F 0xxxxxxx
633 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
634 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
635 *
636 * Check for the 0x110000 limit too
637 */
638 const unsigned char *cur = ctxt->input->cur;
639 unsigned char c;
640 unsigned int val;
641
642 c = *cur;
643 if (c & 0x80) {
644 if (((c & 0x40) == 0) || (c == 0xC0))
645 goto encoding_error;
646 if (cur[1] == 0) {
647 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
648 cur = ctxt->input->cur;
649 }
650 if ((cur[1] & 0xc0) != 0x80)
651 goto encoding_error;
652 if ((c & 0xe0) == 0xe0) {
653 if (cur[2] == 0) {
654 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
655 cur = ctxt->input->cur;
656 }
657 if ((cur[2] & 0xc0) != 0x80)
658 goto encoding_error;
659 if ((c & 0xf0) == 0xf0) {
660 if (cur[3] == 0) {
661 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
662 cur = ctxt->input->cur;
663 }
664 if (((c & 0xf8) != 0xf0) ||
665 ((cur[3] & 0xc0) != 0x80))
666 goto encoding_error;
667 /* 4-byte code */
668 *len = 4;
669 val = (cur[0] & 0x7) << 18;
670 val |= (cur[1] & 0x3f) << 12;
671 val |= (cur[2] & 0x3f) << 6;
672 val |= cur[3] & 0x3f;
673 if (val < 0x10000)
674 goto encoding_error;
675 } else {
676 /* 3-byte code */
677 *len = 3;
678 val = (cur[0] & 0xf) << 12;
679 val |= (cur[1] & 0x3f) << 6;
680 val |= cur[2] & 0x3f;
681 if (val < 0x800)
682 goto encoding_error;
683 }
684 } else {
685 /* 2-byte code */
686 *len = 2;
687 val = (cur[0] & 0x1f) << 6;
688 val |= cur[1] & 0x3f;
689 if (val < 0x80)
690 goto encoding_error;
691 }
692 if (!IS_CHAR(val)) {
693 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
694 "Char 0x%X out of allowed range\n", val);
695 }
696 return(val);
697 } else {
698 /* 1-byte code */
699 *len = 1;
700 if (*ctxt->input->cur == 0)
701 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
702 if ((*ctxt->input->cur == 0) &&
703 (ctxt->input->end > ctxt->input->cur)) {
704 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
705 "Char 0x0 out of allowed range\n", 0);
706 }
707 if (*ctxt->input->cur == 0xD) {
708 if (ctxt->input->cur[1] == 0xA) {
709 ctxt->nbChars++;
710 ctxt->input->cur++;
711 }
712 return(0xA);
713 }
714 return((int) *ctxt->input->cur);
715 }
716 }
717 /*
718 * Assume it's a fixed length encoding (1) with
719 * a compatible encoding for the ASCII set, since
720 * XML constructs only use < 128 chars
721 */
722 *len = 1;
723 if (*ctxt->input->cur == 0xD) {
724 if (ctxt->input->cur[1] == 0xA) {
725 ctxt->nbChars++;
726 ctxt->input->cur++;
727 }
728 return(0xA);
729 }
730 return((int) *ctxt->input->cur);
731 encoding_error:
732 /*
733 * An encoding problem may arise from a truncated input buffer
734 * splitting a character in the middle. In that case do not raise
735 * an error but return 0 to endicate an end of stream problem
736 */
737 if (ctxt->input->end - ctxt->input->cur < 4) {
738 *len = 0;
739 return(0);
740 }
741
742 /*
743 * If we detect an UTF8 error that probably mean that the
744 * input encoding didn't get properly advertised in the
745 * declaration header. Report the error and switch the encoding
746 * to ISO-Latin-1 (if you don't like this policy, just declare the
747 * encoding !)
748 */
749 {
750 char buffer[150];
751
752 snprintf(&buffer[0], 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
753 ctxt->input->cur[0], ctxt->input->cur[1],
754 ctxt->input->cur[2], ctxt->input->cur[3]);
755 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
756 "Input is not proper UTF-8, indicate encoding !\n%s",
757 BAD_CAST buffer, NULL);
758 }
759 ctxt->charset = XML_CHAR_ENCODING_8859_1;
760 *len = 1;
761 return((int) *ctxt->input->cur);
762 }
763
764 /**
765 * xmlStringCurrentChar:
766 * @ctxt: the XML parser context
767 * @cur: pointer to the beginning of the char
768 * @len: pointer to the length of the char read
769 *
770 * The current char value, if using UTF-8 this may actually span multiple
771 * bytes in the input buffer.
772 *
773 * Returns the current char value and its length
774 */
775
776 int
xmlStringCurrentChar(xmlParserCtxtPtr ctxt,const xmlChar * cur,int * len)777 xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
778 {
779 if ((len == NULL) || (cur == NULL)) return(0);
780 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
781 /*
782 * We are supposed to handle UTF8, check it's valid
783 * From rfc2044: encoding of the Unicode values on UTF-8:
784 *
785 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
786 * 0000 0000-0000 007F 0xxxxxxx
787 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
788 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
789 *
790 * Check for the 0x110000 limit too
791 */
792 unsigned char c;
793 unsigned int val;
794
795 c = *cur;
796 if (c & 0x80) {
797 if ((cur[1] & 0xc0) != 0x80)
798 goto encoding_error;
799 if ((c & 0xe0) == 0xe0) {
800
801 if ((cur[2] & 0xc0) != 0x80)
802 goto encoding_error;
803 if ((c & 0xf0) == 0xf0) {
804 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
805 goto encoding_error;
806 /* 4-byte code */
807 *len = 4;
808 val = (cur[0] & 0x7) << 18;
809 val |= (cur[1] & 0x3f) << 12;
810 val |= (cur[2] & 0x3f) << 6;
811 val |= cur[3] & 0x3f;
812 } else {
813 /* 3-byte code */
814 *len = 3;
815 val = (cur[0] & 0xf) << 12;
816 val |= (cur[1] & 0x3f) << 6;
817 val |= cur[2] & 0x3f;
818 }
819 } else {
820 /* 2-byte code */
821 *len = 2;
822 val = (cur[0] & 0x1f) << 6;
823 val |= cur[1] & 0x3f;
824 }
825 if (!IS_CHAR(val)) {
826 xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,
827 "Char 0x%X out of allowed range\n", val);
828 }
829 return (val);
830 } else {
831 /* 1-byte code */
832 *len = 1;
833 return ((int) *cur);
834 }
835 }
836 /*
837 * Assume it's a fixed length encoding (1) with
838 * a compatible encoding for the ASCII set, since
839 * XML constructs only use < 128 chars
840 */
841 *len = 1;
842 return ((int) *cur);
843 encoding_error:
844
845 /*
846 * An encoding problem may arise from a truncated input buffer
847 * splitting a character in the middle. In that case do not raise
848 * an error but return 0 to endicate an end of stream problem
849 */
850 if ((ctxt == NULL) || (ctxt->input == NULL) ||
851 (ctxt->input->end - ctxt->input->cur < 4)) {
852 *len = 0;
853 return(0);
854 }
855 /*
856 * If we detect an UTF8 error that probably mean that the
857 * input encoding didn't get properly advertised in the
858 * declaration header. Report the error and switch the encoding
859 * to ISO-Latin-1 (if you don't like this policy, just declare the
860 * encoding !)
861 */
862 {
863 char buffer[150];
864
865 snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
866 ctxt->input->cur[0], ctxt->input->cur[1],
867 ctxt->input->cur[2], ctxt->input->cur[3]);
868 __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR,
869 "Input is not proper UTF-8, indicate encoding !\n%s",
870 BAD_CAST buffer, NULL);
871 }
872 *len = 1;
873 return ((int) *cur);
874 }
875
876 /**
877 * xmlCopyCharMultiByte:
878 * @out: pointer to an array of xmlChar
879 * @val: the char value
880 *
881 * append the char value in the array
882 *
883 * Returns the number of xmlChar written
884 */
885 int
xmlCopyCharMultiByte(xmlChar * out,int val)886 xmlCopyCharMultiByte(xmlChar *out, int val) {
887 if (out == NULL) return(0);
888 /*
889 * We are supposed to handle UTF8, check it's valid
890 * From rfc2044: encoding of the Unicode values on UTF-8:
891 *
892 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
893 * 0000 0000-0000 007F 0xxxxxxx
894 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
895 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
896 */
897 if (val >= 0x80) {
898 xmlChar *savedout = out;
899 int bits;
900 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
901 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
902 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
903 else {
904 xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR,
905 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
906 val);
907 return(0);
908 }
909 for ( ; bits >= 0; bits-= 6)
910 *out++= ((val >> bits) & 0x3F) | 0x80 ;
911 return (out - savedout);
912 }
913 *out = (xmlChar) val;
914 return 1;
915 }
916
917 /**
918 * xmlCopyChar:
919 * @len: Ignored, compatibility
920 * @out: pointer to an array of xmlChar
921 * @val: the char value
922 *
923 * append the char value in the array
924 *
925 * Returns the number of xmlChar written
926 */
927
928 int
xmlCopyChar(int len ATTRIBUTE_UNUSED,xmlChar * out,int val)929 xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
930 if (out == NULL) return(0);
931 /* the len parameter is ignored */
932 if (val >= 0x80) {
933 return(xmlCopyCharMultiByte (out, val));
934 }
935 *out = (xmlChar) val;
936 return 1;
937 }
938
939 /************************************************************************
940 * *
941 * Commodity functions to switch encodings *
942 * *
943 ************************************************************************/
944
945 static int
946 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
947 xmlCharEncodingHandlerPtr handler, int len);
948 static int
949 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
950 xmlCharEncodingHandlerPtr handler, int len);
951 /**
952 * xmlSwitchEncoding:
953 * @ctxt: the parser context
954 * @enc: the encoding value (number)
955 *
956 * change the input functions when discovering the character encoding
957 * of a given entity.
958 *
959 * Returns 0 in case of success, -1 otherwise
960 */
961 int
xmlSwitchEncoding(xmlParserCtxtPtr ctxt,xmlCharEncoding enc)962 xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
963 {
964 xmlCharEncodingHandlerPtr handler;
965 int len = -1;
966 int ret;
967
968 if (ctxt == NULL) return(-1);
969 switch (enc) {
970 case XML_CHAR_ENCODING_ERROR:
971 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
972 "encoding unknown\n", NULL, NULL);
973 return(-1);
974 case XML_CHAR_ENCODING_NONE:
975 /* let's assume it's UTF-8 without the XML decl */
976 ctxt->charset = XML_CHAR_ENCODING_UTF8;
977 return(0);
978 case XML_CHAR_ENCODING_UTF8:
979 /* default encoding, no conversion should be needed */
980 ctxt->charset = XML_CHAR_ENCODING_UTF8;
981
982 /*
983 * Errata on XML-1.0 June 20 2001
984 * Specific handling of the Byte Order Mark for
985 * UTF-8
986 */
987 if ((ctxt->input != NULL) &&
988 (ctxt->input->cur[0] == 0xEF) &&
989 (ctxt->input->cur[1] == 0xBB) &&
990 (ctxt->input->cur[2] == 0xBF)) {
991 ctxt->input->cur += 3;
992 }
993 return(0);
994 case XML_CHAR_ENCODING_UTF16LE:
995 case XML_CHAR_ENCODING_UTF16BE:
996 /*The raw input characters are encoded
997 *in UTF-16. As we expect this function
998 *to be called after xmlCharEncInFunc, we expect
999 *ctxt->input->cur to contain UTF-8 encoded characters.
1000 *So the raw UTF16 Byte Order Mark
1001 *has also been converted into
1002 *an UTF-8 BOM. Let's skip that BOM.
1003 */
1004 if ((ctxt->input != NULL) && (ctxt->input->cur != NULL) &&
1005 (ctxt->input->cur[0] == 0xEF) &&
1006 (ctxt->input->cur[1] == 0xBB) &&
1007 (ctxt->input->cur[2] == 0xBF)) {
1008 ctxt->input->cur += 3;
1009 }
1010 len = 90;
1011 break;
1012 case XML_CHAR_ENCODING_UCS2:
1013 len = 90;
1014 break;
1015 case XML_CHAR_ENCODING_UCS4BE:
1016 case XML_CHAR_ENCODING_UCS4LE:
1017 case XML_CHAR_ENCODING_UCS4_2143:
1018 case XML_CHAR_ENCODING_UCS4_3412:
1019 len = 180;
1020 break;
1021 case XML_CHAR_ENCODING_EBCDIC:
1022 case XML_CHAR_ENCODING_8859_1:
1023 case XML_CHAR_ENCODING_8859_2:
1024 case XML_CHAR_ENCODING_8859_3:
1025 case XML_CHAR_ENCODING_8859_4:
1026 case XML_CHAR_ENCODING_8859_5:
1027 case XML_CHAR_ENCODING_8859_6:
1028 case XML_CHAR_ENCODING_8859_7:
1029 case XML_CHAR_ENCODING_8859_8:
1030 case XML_CHAR_ENCODING_8859_9:
1031 case XML_CHAR_ENCODING_ASCII:
1032 case XML_CHAR_ENCODING_2022_JP:
1033 case XML_CHAR_ENCODING_SHIFT_JIS:
1034 case XML_CHAR_ENCODING_EUC_JP:
1035 len = 45;
1036 break;
1037 }
1038 handler = xmlGetCharEncodingHandler(enc);
1039 if (handler == NULL) {
1040 /*
1041 * Default handlers.
1042 */
1043 switch (enc) {
1044 case XML_CHAR_ENCODING_ASCII:
1045 /* default encoding, no conversion should be needed */
1046 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1047 return(0);
1048 case XML_CHAR_ENCODING_UTF16LE:
1049 break;
1050 case XML_CHAR_ENCODING_UTF16BE:
1051 break;
1052 case XML_CHAR_ENCODING_UCS4LE:
1053 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1054 "encoding not supported %s\n",
1055 BAD_CAST "USC4 little endian", NULL);
1056 break;
1057 case XML_CHAR_ENCODING_UCS4BE:
1058 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1059 "encoding not supported %s\n",
1060 BAD_CAST "USC4 big endian", NULL);
1061 break;
1062 case XML_CHAR_ENCODING_EBCDIC:
1063 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1064 "encoding not supported %s\n",
1065 BAD_CAST "EBCDIC", NULL);
1066 break;
1067 case XML_CHAR_ENCODING_UCS4_2143:
1068 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1069 "encoding not supported %s\n",
1070 BAD_CAST "UCS4 2143", NULL);
1071 break;
1072 case XML_CHAR_ENCODING_UCS4_3412:
1073 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1074 "encoding not supported %s\n",
1075 BAD_CAST "UCS4 3412", NULL);
1076 break;
1077 case XML_CHAR_ENCODING_UCS2:
1078 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1079 "encoding not supported %s\n",
1080 BAD_CAST "UCS2", NULL);
1081 break;
1082 case XML_CHAR_ENCODING_8859_1:
1083 case XML_CHAR_ENCODING_8859_2:
1084 case XML_CHAR_ENCODING_8859_3:
1085 case XML_CHAR_ENCODING_8859_4:
1086 case XML_CHAR_ENCODING_8859_5:
1087 case XML_CHAR_ENCODING_8859_6:
1088 case XML_CHAR_ENCODING_8859_7:
1089 case XML_CHAR_ENCODING_8859_8:
1090 case XML_CHAR_ENCODING_8859_9:
1091 /*
1092 * We used to keep the internal content in the
1093 * document encoding however this turns being unmaintainable
1094 * So xmlGetCharEncodingHandler() will return non-null
1095 * values for this now.
1096 */
1097 if ((ctxt->inputNr == 1) &&
1098 (ctxt->encoding == NULL) &&
1099 (ctxt->input != NULL) &&
1100 (ctxt->input->encoding != NULL)) {
1101 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1102 }
1103 ctxt->charset = enc;
1104 return(0);
1105 case XML_CHAR_ENCODING_2022_JP:
1106 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1107 "encoding not supported %s\n",
1108 BAD_CAST "ISO-2022-JP", NULL);
1109 break;
1110 case XML_CHAR_ENCODING_SHIFT_JIS:
1111 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1112 "encoding not supported %s\n",
1113 BAD_CAST "Shift_JIS", NULL);
1114 break;
1115 case XML_CHAR_ENCODING_EUC_JP:
1116 __xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1117 "encoding not supported %s\n",
1118 BAD_CAST "EUC-JP", NULL);
1119 break;
1120 default:
1121 break;
1122 }
1123 }
1124 /*
1125 * TODO: We could recover from errors in external entites if we
1126 * didn't stop the parser. But most callers of this function don't
1127 * check the return value.
1128 */
1129 if (handler == NULL) {
1130 xmlStopParser(ctxt);
1131 return(-1);
1132 }
1133 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1134 ret = xmlSwitchToEncodingInt(ctxt, handler, len);
1135 if ((ret < 0) || (ctxt->errNo == XML_I18N_CONV_FAILED)) {
1136 /*
1137 * on encoding conversion errors, stop the parser
1138 */
1139 xmlStopParser(ctxt);
1140 ctxt->errNo = XML_I18N_CONV_FAILED;
1141 }
1142 return(ret);
1143 }
1144
1145 /**
1146 * xmlSwitchInputEncoding:
1147 * @ctxt: the parser context
1148 * @input: the input stream
1149 * @handler: the encoding handler
1150 * @len: the number of bytes to convert for the first line or -1
1151 *
1152 * change the input functions when discovering the character encoding
1153 * of a given entity.
1154 *
1155 * Returns 0 in case of success, -1 otherwise
1156 */
1157 static int
xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt,xmlParserInputPtr input,xmlCharEncodingHandlerPtr handler,int len)1158 xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1159 xmlCharEncodingHandlerPtr handler, int len)
1160 {
1161 int nbchars;
1162
1163 if (handler == NULL)
1164 return (-1);
1165 if (input == NULL)
1166 return (-1);
1167 if (input->buf != NULL) {
1168 if (input->buf->encoder != NULL) {
1169 /*
1170 * Check in case the auto encoding detetection triggered
1171 * in already.
1172 */
1173 if (input->buf->encoder == handler)
1174 return (0);
1175
1176 /*
1177 * "UTF-16" can be used for both LE and BE
1178 if ((!xmlStrncmp(BAD_CAST input->buf->encoder->name,
1179 BAD_CAST "UTF-16", 6)) &&
1180 (!xmlStrncmp(BAD_CAST handler->name,
1181 BAD_CAST "UTF-16", 6))) {
1182 return(0);
1183 }
1184 */
1185
1186 /*
1187 * Note: this is a bit dangerous, but that's what it
1188 * takes to use nearly compatible signature for different
1189 * encodings.
1190 */
1191 xmlCharEncCloseFunc(input->buf->encoder);
1192 input->buf->encoder = handler;
1193 return (0);
1194 }
1195 input->buf->encoder = handler;
1196
1197 /*
1198 * Is there already some content down the pipe to convert ?
1199 */
1200 if (xmlBufIsEmpty(input->buf->buffer) == 0) {
1201 int processed;
1202 unsigned int use;
1203
1204 /*
1205 * Specific handling of the Byte Order Mark for
1206 * UTF-16
1207 */
1208 if ((handler->name != NULL) &&
1209 (!strcmp(handler->name, "UTF-16LE") ||
1210 !strcmp(handler->name, "UTF-16")) &&
1211 (input->cur[0] == 0xFF) && (input->cur[1] == 0xFE)) {
1212 input->cur += 2;
1213 }
1214 if ((handler->name != NULL) &&
1215 (!strcmp(handler->name, "UTF-16BE")) &&
1216 (input->cur[0] == 0xFE) && (input->cur[1] == 0xFF)) {
1217 input->cur += 2;
1218 }
1219 /*
1220 * Errata on XML-1.0 June 20 2001
1221 * Specific handling of the Byte Order Mark for
1222 * UTF-8
1223 */
1224 if ((handler->name != NULL) &&
1225 (!strcmp(handler->name, "UTF-8")) &&
1226 (input->cur[0] == 0xEF) &&
1227 (input->cur[1] == 0xBB) && (input->cur[2] == 0xBF)) {
1228 input->cur += 3;
1229 }
1230
1231 /*
1232 * Shrink the current input buffer.
1233 * Move it as the raw buffer and create a new input buffer
1234 */
1235 processed = input->cur - input->base;
1236 xmlBufShrink(input->buf->buffer, processed);
1237 input->buf->raw = input->buf->buffer;
1238 input->buf->buffer = xmlBufCreate();
1239 input->buf->rawconsumed = processed;
1240 use = xmlBufUse(input->buf->raw);
1241
1242 if (ctxt->html) {
1243 /*
1244 * convert as much as possible of the buffer
1245 */
1246 nbchars = xmlCharEncInput(input->buf, 1);
1247 } else {
1248 /*
1249 * convert just enough to get
1250 * '<?xml version="1.0" encoding="xxx"?>'
1251 * parsed with the autodetected encoding
1252 * into the parser reading buffer.
1253 */
1254 nbchars = xmlCharEncFirstLineInput(input->buf, len);
1255 }
1256 xmlBufResetInput(input->buf->buffer, input);
1257 if (nbchars < 0) {
1258 xmlErrInternal(ctxt,
1259 "switching encoding: encoder error\n",
1260 NULL);
1261 return (-1);
1262 }
1263 input->buf->rawconsumed += use - xmlBufUse(input->buf->raw);
1264 }
1265 return (0);
1266 } else if (input->length == 0) {
1267 /*
1268 * When parsing a static memory array one must know the
1269 * size to be able to convert the buffer.
1270 */
1271 xmlErrInternal(ctxt, "switching encoding : no input\n", NULL);
1272 /*
1273 * Callers assume that the input buffer takes ownership of the
1274 * encoding handler. xmlCharEncCloseFunc frees unregistered
1275 * handlers and avoids a memory leak.
1276 */
1277 xmlCharEncCloseFunc(handler);
1278 return (-1);
1279 }
1280 /*
1281 * We should actually raise an error here, see issue #34.
1282 */
1283 xmlCharEncCloseFunc(handler);
1284 return (0);
1285 }
1286
1287 /**
1288 * xmlSwitchInputEncoding:
1289 * @ctxt: the parser context
1290 * @input: the input stream
1291 * @handler: the encoding handler
1292 *
1293 * change the input functions when discovering the character encoding
1294 * of a given entity.
1295 *
1296 * Returns 0 in case of success, -1 otherwise
1297 */
1298 int
xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt,xmlParserInputPtr input,xmlCharEncodingHandlerPtr handler)1299 xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
1300 xmlCharEncodingHandlerPtr handler) {
1301 return(xmlSwitchInputEncodingInt(ctxt, input, handler, -1));
1302 }
1303
1304 /**
1305 * xmlSwitchToEncodingInt:
1306 * @ctxt: the parser context
1307 * @handler: the encoding handler
1308 * @len: the length to convert or -1
1309 *
1310 * change the input functions when discovering the character encoding
1311 * of a given entity, and convert only @len bytes of the output, this
1312 * is needed on auto detect to allows any declared encoding later to
1313 * convert the actual content after the xmlDecl
1314 *
1315 * Returns 0 in case of success, -1 otherwise
1316 */
1317 static int
xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,xmlCharEncodingHandlerPtr handler,int len)1318 xmlSwitchToEncodingInt(xmlParserCtxtPtr ctxt,
1319 xmlCharEncodingHandlerPtr handler, int len) {
1320 int ret = 0;
1321
1322 if (handler != NULL) {
1323 if (ctxt->input != NULL) {
1324 ret = xmlSwitchInputEncodingInt(ctxt, ctxt->input, handler, len);
1325 } else {
1326 xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n",
1327 NULL);
1328 return(-1);
1329 }
1330 /*
1331 * The parsing is now done in UTF8 natively
1332 */
1333 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1334 } else
1335 return(-1);
1336 return(ret);
1337 }
1338
1339 /**
1340 * xmlSwitchToEncoding:
1341 * @ctxt: the parser context
1342 * @handler: the encoding handler
1343 *
1344 * change the input functions when discovering the character encoding
1345 * of a given entity.
1346 *
1347 * Returns 0 in case of success, -1 otherwise
1348 */
1349 int
xmlSwitchToEncoding(xmlParserCtxtPtr ctxt,xmlCharEncodingHandlerPtr handler)1350 xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1351 {
1352 return (xmlSwitchToEncodingInt(ctxt, handler, -1));
1353 }
1354
1355 /************************************************************************
1356 * *
1357 * Commodity functions to handle entities processing *
1358 * *
1359 ************************************************************************/
1360
1361 /**
1362 * xmlFreeInputStream:
1363 * @input: an xmlParserInputPtr
1364 *
1365 * Free up an input stream.
1366 */
1367 void
xmlFreeInputStream(xmlParserInputPtr input)1368 xmlFreeInputStream(xmlParserInputPtr input) {
1369 if (input == NULL) return;
1370
1371 if (input->filename != NULL) xmlFree((char *) input->filename);
1372 if (input->directory != NULL) xmlFree((char *) input->directory);
1373 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1374 if (input->version != NULL) xmlFree((char *) input->version);
1375 if ((input->free != NULL) && (input->base != NULL))
1376 input->free((xmlChar *) input->base);
1377 if (input->buf != NULL)
1378 xmlFreeParserInputBuffer(input->buf);
1379 xmlFree(input);
1380 }
1381
1382 /**
1383 * xmlNewInputStream:
1384 * @ctxt: an XML parser context
1385 *
1386 * Create a new input stream structure.
1387 *
1388 * Returns the new input stream or NULL
1389 */
1390 xmlParserInputPtr
xmlNewInputStream(xmlParserCtxtPtr ctxt)1391 xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1392 xmlParserInputPtr input;
1393
1394 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1395 if (input == NULL) {
1396 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1397 return(NULL);
1398 }
1399 memset(input, 0, sizeof(xmlParserInput));
1400 input->line = 1;
1401 input->col = 1;
1402 input->standalone = -1;
1403
1404 /*
1405 * If the context is NULL the id cannot be initialized, but that
1406 * should not happen while parsing which is the situation where
1407 * the id is actually needed.
1408 */
1409 if (ctxt != NULL)
1410 input->id = ctxt->input_id++;
1411
1412 return(input);
1413 }
1414
1415 /**
1416 * xmlNewIOInputStream:
1417 * @ctxt: an XML parser context
1418 * @input: an I/O Input
1419 * @enc: the charset encoding if known
1420 *
1421 * Create a new input stream structure encapsulating the @input into
1422 * a stream suitable for the parser.
1423 *
1424 * Returns the new input stream or NULL
1425 */
1426 xmlParserInputPtr
xmlNewIOInputStream(xmlParserCtxtPtr ctxt,xmlParserInputBufferPtr input,xmlCharEncoding enc)1427 xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1428 xmlCharEncoding enc) {
1429 xmlParserInputPtr inputStream;
1430
1431 if (input == NULL) return(NULL);
1432 if (xmlParserDebugEntities)
1433 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1434 inputStream = xmlNewInputStream(ctxt);
1435 if (inputStream == NULL) {
1436 return(NULL);
1437 }
1438 inputStream->filename = NULL;
1439 inputStream->buf = input;
1440 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1441
1442 if (enc != XML_CHAR_ENCODING_NONE) {
1443 xmlSwitchEncoding(ctxt, enc);
1444 }
1445
1446 return(inputStream);
1447 }
1448
1449 /**
1450 * xmlNewEntityInputStream:
1451 * @ctxt: an XML parser context
1452 * @entity: an Entity pointer
1453 *
1454 * Create a new input stream based on an xmlEntityPtr
1455 *
1456 * Returns the new input stream or NULL
1457 */
1458 xmlParserInputPtr
xmlNewEntityInputStream(xmlParserCtxtPtr ctxt,xmlEntityPtr entity)1459 xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1460 xmlParserInputPtr input;
1461
1462 if (entity == NULL) {
1463 xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n",
1464 NULL);
1465 return(NULL);
1466 }
1467 if (xmlParserDebugEntities)
1468 xmlGenericError(xmlGenericErrorContext,
1469 "new input from entity: %s\n", entity->name);
1470 if (entity->content == NULL) {
1471 switch (entity->etype) {
1472 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1473 xmlErrInternal(ctxt, "Cannot parse entity %s\n",
1474 entity->name);
1475 break;
1476 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1477 case XML_EXTERNAL_PARAMETER_ENTITY:
1478 return(xmlLoadExternalEntity((char *) entity->URI,
1479 (char *) entity->ExternalID, ctxt));
1480 case XML_INTERNAL_GENERAL_ENTITY:
1481 xmlErrInternal(ctxt,
1482 "Internal entity %s without content !\n",
1483 entity->name);
1484 break;
1485 case XML_INTERNAL_PARAMETER_ENTITY:
1486 xmlErrInternal(ctxt,
1487 "Internal parameter entity %s without content !\n",
1488 entity->name);
1489 break;
1490 case XML_INTERNAL_PREDEFINED_ENTITY:
1491 xmlErrInternal(ctxt,
1492 "Predefined entity %s without content !\n",
1493 entity->name);
1494 break;
1495 }
1496 return(NULL);
1497 }
1498 input = xmlNewInputStream(ctxt);
1499 if (input == NULL) {
1500 return(NULL);
1501 }
1502 if (entity->URI != NULL)
1503 input->filename = (char *) xmlStrdup((xmlChar *) entity->URI);
1504 input->base = entity->content;
1505 if (entity->length == 0)
1506 entity->length = xmlStrlen(entity->content);
1507 input->cur = entity->content;
1508 input->length = entity->length;
1509 input->end = &entity->content[input->length];
1510 return(input);
1511 }
1512
1513 /**
1514 * xmlNewStringInputStream:
1515 * @ctxt: an XML parser context
1516 * @buffer: an memory buffer
1517 *
1518 * Create a new input stream based on a memory buffer.
1519 * Returns the new input stream
1520 */
1521 xmlParserInputPtr
xmlNewStringInputStream(xmlParserCtxtPtr ctxt,const xmlChar * buffer)1522 xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1523 xmlParserInputPtr input;
1524
1525 if (buffer == NULL) {
1526 xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n",
1527 NULL);
1528 return(NULL);
1529 }
1530 if (xmlParserDebugEntities)
1531 xmlGenericError(xmlGenericErrorContext,
1532 "new fixed input: %.30s\n", buffer);
1533 input = xmlNewInputStream(ctxt);
1534 if (input == NULL) {
1535 xmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
1536 return(NULL);
1537 }
1538 input->base = buffer;
1539 input->cur = buffer;
1540 input->length = xmlStrlen(buffer);
1541 input->end = &buffer[input->length];
1542 return(input);
1543 }
1544
1545 /**
1546 * xmlNewInputFromFile:
1547 * @ctxt: an XML parser context
1548 * @filename: the filename to use as entity
1549 *
1550 * Create a new input stream based on a file or an URL.
1551 *
1552 * Returns the new input stream or NULL in case of error
1553 */
1554 xmlParserInputPtr
xmlNewInputFromFile(xmlParserCtxtPtr ctxt,const char * filename)1555 xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1556 xmlParserInputBufferPtr buf;
1557 xmlParserInputPtr inputStream;
1558 char *directory = NULL;
1559 xmlChar *URI = NULL;
1560
1561 if (xmlParserDebugEntities)
1562 xmlGenericError(xmlGenericErrorContext,
1563 "new input from file: %s\n", filename);
1564 if (ctxt == NULL) return(NULL);
1565 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1566 if (buf == NULL) {
1567 if (filename == NULL)
1568 __xmlLoaderErr(ctxt,
1569 "failed to load external entity: NULL filename \n",
1570 NULL);
1571 else
1572 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n",
1573 (const char *) filename);
1574 return(NULL);
1575 }
1576
1577 inputStream = xmlNewInputStream(ctxt);
1578 if (inputStream == NULL)
1579 return(NULL);
1580
1581 inputStream->buf = buf;
1582 inputStream = xmlCheckHTTPInput(ctxt, inputStream);
1583 if (inputStream == NULL)
1584 return(NULL);
1585
1586 if (inputStream->filename == NULL)
1587 URI = xmlStrdup((xmlChar *) filename);
1588 else
1589 URI = xmlStrdup((xmlChar *) inputStream->filename);
1590 directory = xmlParserGetDirectory((const char *) URI);
1591 if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename);
1592 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
1593 if (URI != NULL) xmlFree((char *) URI);
1594 inputStream->directory = directory;
1595
1596 xmlBufResetInput(inputStream->buf->buffer, inputStream);
1597 if ((ctxt->directory == NULL) && (directory != NULL))
1598 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1599 return(inputStream);
1600 }
1601
1602 /************************************************************************
1603 * *
1604 * Commodity functions to handle parser contexts *
1605 * *
1606 ************************************************************************/
1607
1608 /**
1609 * xmlInitParserCtxt:
1610 * @ctxt: an XML parser context
1611 *
1612 * Initialize a parser context
1613 *
1614 * Returns 0 in case of success and -1 in case of error
1615 */
1616
1617 int
xmlInitParserCtxt(xmlParserCtxtPtr ctxt)1618 xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1619 {
1620 xmlParserInputPtr input;
1621
1622 if(ctxt==NULL) {
1623 xmlErrInternal(NULL, "Got NULL parser context\n", NULL);
1624 return(-1);
1625 }
1626
1627 xmlDefaultSAXHandlerInit();
1628
1629 if (ctxt->dict == NULL)
1630 ctxt->dict = xmlDictCreate();
1631 if (ctxt->dict == NULL) {
1632 xmlErrMemory(NULL, "cannot initialize parser context\n");
1633 return(-1);
1634 }
1635 xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT);
1636
1637 if (ctxt->sax == NULL)
1638 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1639 if (ctxt->sax == NULL) {
1640 xmlErrMemory(NULL, "cannot initialize parser context\n");
1641 return(-1);
1642 }
1643 else
1644 xmlSAXVersion(ctxt->sax, 2);
1645
1646 ctxt->maxatts = 0;
1647 ctxt->atts = NULL;
1648 /* Allocate the Input stack */
1649 if (ctxt->inputTab == NULL) {
1650 ctxt->inputTab = (xmlParserInputPtr *)
1651 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1652 ctxt->inputMax = 5;
1653 }
1654 if (ctxt->inputTab == NULL) {
1655 xmlErrMemory(NULL, "cannot initialize parser context\n");
1656 ctxt->inputNr = 0;
1657 ctxt->inputMax = 0;
1658 ctxt->input = NULL;
1659 return(-1);
1660 }
1661 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1662 xmlFreeInputStream(input);
1663 }
1664 ctxt->inputNr = 0;
1665 ctxt->input = NULL;
1666
1667 ctxt->version = NULL;
1668 ctxt->encoding = NULL;
1669 ctxt->standalone = -1;
1670 ctxt->hasExternalSubset = 0;
1671 ctxt->hasPErefs = 0;
1672 ctxt->html = 0;
1673 ctxt->external = 0;
1674 ctxt->instate = XML_PARSER_START;
1675 ctxt->token = 0;
1676 ctxt->directory = NULL;
1677
1678 /* Allocate the Node stack */
1679 if (ctxt->nodeTab == NULL) {
1680 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1681 ctxt->nodeMax = 10;
1682 }
1683 if (ctxt->nodeTab == NULL) {
1684 xmlErrMemory(NULL, "cannot initialize parser context\n");
1685 ctxt->nodeNr = 0;
1686 ctxt->nodeMax = 0;
1687 ctxt->node = NULL;
1688 ctxt->inputNr = 0;
1689 ctxt->inputMax = 0;
1690 ctxt->input = NULL;
1691 return(-1);
1692 }
1693 ctxt->nodeNr = 0;
1694 ctxt->node = NULL;
1695
1696 /* Allocate the Name stack */
1697 if (ctxt->nameTab == NULL) {
1698 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
1699 ctxt->nameMax = 10;
1700 }
1701 if (ctxt->nameTab == NULL) {
1702 xmlErrMemory(NULL, "cannot initialize parser context\n");
1703 ctxt->nodeNr = 0;
1704 ctxt->nodeMax = 0;
1705 ctxt->node = NULL;
1706 ctxt->inputNr = 0;
1707 ctxt->inputMax = 0;
1708 ctxt->input = NULL;
1709 ctxt->nameNr = 0;
1710 ctxt->nameMax = 0;
1711 ctxt->name = NULL;
1712 return(-1);
1713 }
1714 ctxt->nameNr = 0;
1715 ctxt->name = NULL;
1716
1717 /* Allocate the space stack */
1718 if (ctxt->spaceTab == NULL) {
1719 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
1720 ctxt->spaceMax = 10;
1721 }
1722 if (ctxt->spaceTab == NULL) {
1723 xmlErrMemory(NULL, "cannot initialize parser context\n");
1724 ctxt->nodeNr = 0;
1725 ctxt->nodeMax = 0;
1726 ctxt->node = NULL;
1727 ctxt->inputNr = 0;
1728 ctxt->inputMax = 0;
1729 ctxt->input = NULL;
1730 ctxt->nameNr = 0;
1731 ctxt->nameMax = 0;
1732 ctxt->name = NULL;
1733 ctxt->spaceNr = 0;
1734 ctxt->spaceMax = 0;
1735 ctxt->space = NULL;
1736 return(-1);
1737 }
1738 ctxt->spaceNr = 1;
1739 ctxt->spaceMax = 10;
1740 ctxt->spaceTab[0] = -1;
1741 ctxt->space = &ctxt->spaceTab[0];
1742 ctxt->userData = ctxt;
1743 ctxt->myDoc = NULL;
1744 ctxt->wellFormed = 1;
1745 ctxt->nsWellFormed = 1;
1746 ctxt->valid = 1;
1747 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
1748 if (ctxt->loadsubset) {
1749 ctxt->options |= XML_PARSE_DTDLOAD;
1750 }
1751 ctxt->validate = xmlDoValidityCheckingDefaultValue;
1752 ctxt->pedantic = xmlPedanticParserDefaultValue;
1753 if (ctxt->pedantic) {
1754 ctxt->options |= XML_PARSE_PEDANTIC;
1755 }
1756 ctxt->linenumbers = xmlLineNumbersDefaultValue;
1757 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
1758 if (ctxt->keepBlanks == 0) {
1759 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
1760 ctxt->options |= XML_PARSE_NOBLANKS;
1761 }
1762
1763 ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_0;
1764 ctxt->vctxt.userData = ctxt;
1765 ctxt->vctxt.error = xmlParserValidityError;
1766 ctxt->vctxt.warning = xmlParserValidityWarning;
1767 if (ctxt->validate) {
1768 if (xmlGetWarningsDefaultValue == 0)
1769 ctxt->vctxt.warning = NULL;
1770 else
1771 ctxt->vctxt.warning = xmlParserValidityWarning;
1772 ctxt->vctxt.nodeMax = 0;
1773 ctxt->options |= XML_PARSE_DTDVALID;
1774 }
1775 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
1776 if (ctxt->replaceEntities) {
1777 ctxt->options |= XML_PARSE_NOENT;
1778 }
1779 ctxt->record_info = 0;
1780 ctxt->nbChars = 0;
1781 ctxt->checkIndex = 0;
1782 ctxt->inSubset = 0;
1783 ctxt->errNo = XML_ERR_OK;
1784 ctxt->depth = 0;
1785 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1786 ctxt->catalogs = NULL;
1787 ctxt->nbentities = 0;
1788 ctxt->sizeentities = 0;
1789 ctxt->sizeentcopy = 0;
1790 ctxt->input_id = 1;
1791 xmlInitNodeInfoSeq(&ctxt->node_seq);
1792 return(0);
1793 }
1794
1795 /**
1796 * xmlFreeParserCtxt:
1797 * @ctxt: an XML parser context
1798 *
1799 * Free all the memory used by a parser context. However the parsed
1800 * document in ctxt->myDoc is not freed.
1801 */
1802
1803 void
xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)1804 xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
1805 {
1806 xmlParserInputPtr input;
1807
1808 if (ctxt == NULL) return;
1809
1810 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
1811 xmlFreeInputStream(input);
1812 }
1813 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
1814 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
1815 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
1816 if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab);
1817 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
1818 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
1819 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
1820 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
1821 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
1822 #ifdef LIBXML_SAX1_ENABLED
1823 if ((ctxt->sax != NULL) &&
1824 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
1825 #else
1826 if (ctxt->sax != NULL)
1827 #endif /* LIBXML_SAX1_ENABLED */
1828 xmlFree(ctxt->sax);
1829 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
1830 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
1831 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
1832 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
1833 if (ctxt->nsTab != NULL) xmlFree((char *) ctxt->nsTab);
1834 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
1835 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
1836 if (ctxt->attsDefault != NULL)
1837 xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
1838 if (ctxt->attsSpecial != NULL)
1839 xmlHashFree(ctxt->attsSpecial, NULL);
1840 if (ctxt->freeElems != NULL) {
1841 xmlNodePtr cur, next;
1842
1843 cur = ctxt->freeElems;
1844 while (cur != NULL) {
1845 next = cur->next;
1846 xmlFree(cur);
1847 cur = next;
1848 }
1849 }
1850 if (ctxt->freeAttrs != NULL) {
1851 xmlAttrPtr cur, next;
1852
1853 cur = ctxt->freeAttrs;
1854 while (cur != NULL) {
1855 next = cur->next;
1856 xmlFree(cur);
1857 cur = next;
1858 }
1859 }
1860 /*
1861 * cleanup the error strings
1862 */
1863 if (ctxt->lastError.message != NULL)
1864 xmlFree(ctxt->lastError.message);
1865 if (ctxt->lastError.file != NULL)
1866 xmlFree(ctxt->lastError.file);
1867 if (ctxt->lastError.str1 != NULL)
1868 xmlFree(ctxt->lastError.str1);
1869 if (ctxt->lastError.str2 != NULL)
1870 xmlFree(ctxt->lastError.str2);
1871 if (ctxt->lastError.str3 != NULL)
1872 xmlFree(ctxt->lastError.str3);
1873
1874 #ifdef LIBXML_CATALOG_ENABLED
1875 if (ctxt->catalogs != NULL)
1876 xmlCatalogFreeLocal(ctxt->catalogs);
1877 #endif
1878 xmlFree(ctxt);
1879 }
1880
1881 /**
1882 * xmlNewParserCtxt:
1883 *
1884 * Allocate and initialize a new parser context.
1885 *
1886 * Returns the xmlParserCtxtPtr or NULL
1887 */
1888
1889 xmlParserCtxtPtr
xmlNewParserCtxt(void)1890 xmlNewParserCtxt(void)
1891 {
1892 xmlParserCtxtPtr ctxt;
1893
1894 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
1895 if (ctxt == NULL) {
1896 xmlErrMemory(NULL, "cannot allocate parser context\n");
1897 return(NULL);
1898 }
1899 memset(ctxt, 0, sizeof(xmlParserCtxt));
1900 if (xmlInitParserCtxt(ctxt) < 0) {
1901 xmlFreeParserCtxt(ctxt);
1902 return(NULL);
1903 }
1904 return(ctxt);
1905 }
1906
1907 /************************************************************************
1908 * *
1909 * Handling of node informations *
1910 * *
1911 ************************************************************************/
1912
1913 /**
1914 * xmlClearParserCtxt:
1915 * @ctxt: an XML parser context
1916 *
1917 * Clear (release owned resources) and reinitialize a parser context
1918 */
1919
1920 void
xmlClearParserCtxt(xmlParserCtxtPtr ctxt)1921 xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
1922 {
1923 if (ctxt==NULL)
1924 return;
1925 xmlClearNodeInfoSeq(&ctxt->node_seq);
1926 xmlCtxtReset(ctxt);
1927 }
1928
1929
1930 /**
1931 * xmlParserFindNodeInfo:
1932 * @ctx: an XML parser context
1933 * @node: an XML node within the tree
1934 *
1935 * Find the parser node info struct for a given node
1936 *
1937 * Returns an xmlParserNodeInfo block pointer or NULL
1938 */
1939 const xmlParserNodeInfo *
xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,const xmlNodePtr node)1940 xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, const xmlNodePtr node)
1941 {
1942 unsigned long pos;
1943
1944 if ((ctx == NULL) || (node == NULL))
1945 return (NULL);
1946 /* Find position where node should be at */
1947 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
1948 if (pos < ctx->node_seq.length
1949 && ctx->node_seq.buffer[pos].node == node)
1950 return &ctx->node_seq.buffer[pos];
1951 else
1952 return NULL;
1953 }
1954
1955
1956 /**
1957 * xmlInitNodeInfoSeq:
1958 * @seq: a node info sequence pointer
1959 *
1960 * -- Initialize (set to initial state) node info sequence
1961 */
1962 void
xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)1963 xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1964 {
1965 if (seq == NULL)
1966 return;
1967 seq->length = 0;
1968 seq->maximum = 0;
1969 seq->buffer = NULL;
1970 }
1971
1972 /**
1973 * xmlClearNodeInfoSeq:
1974 * @seq: a node info sequence pointer
1975 *
1976 * -- Clear (release memory and reinitialize) node
1977 * info sequence
1978 */
1979 void
xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)1980 xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
1981 {
1982 if (seq == NULL)
1983 return;
1984 if (seq->buffer != NULL)
1985 xmlFree(seq->buffer);
1986 xmlInitNodeInfoSeq(seq);
1987 }
1988
1989 /**
1990 * xmlParserFindNodeInfoIndex:
1991 * @seq: a node info sequence pointer
1992 * @node: an XML node pointer
1993 *
1994 *
1995 * xmlParserFindNodeInfoIndex : Find the index that the info record for
1996 * the given node is or should be at in a sorted sequence
1997 *
1998 * Returns a long indicating the position of the record
1999 */
2000 unsigned long
xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,const xmlNodePtr node)2001 xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
2002 const xmlNodePtr node)
2003 {
2004 unsigned long upper, lower, middle;
2005 int found = 0;
2006
2007 if ((seq == NULL) || (node == NULL))
2008 return ((unsigned long) -1);
2009
2010 /* Do a binary search for the key */
2011 lower = 1;
2012 upper = seq->length;
2013 middle = 0;
2014 while (lower <= upper && !found) {
2015 middle = lower + (upper - lower) / 2;
2016 if (node == seq->buffer[middle - 1].node)
2017 found = 1;
2018 else if (node < seq->buffer[middle - 1].node)
2019 upper = middle - 1;
2020 else
2021 lower = middle + 1;
2022 }
2023
2024 /* Return position */
2025 if (middle == 0 || seq->buffer[middle - 1].node < node)
2026 return middle;
2027 else
2028 return middle - 1;
2029 }
2030
2031
2032 /**
2033 * xmlParserAddNodeInfo:
2034 * @ctxt: an XML parser context
2035 * @info: a node info sequence pointer
2036 *
2037 * Insert node info record into the sorted sequence
2038 */
2039 void
xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,const xmlParserNodeInfoPtr info)2040 xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
2041 const xmlParserNodeInfoPtr info)
2042 {
2043 unsigned long pos;
2044
2045 if ((ctxt == NULL) || (info == NULL)) return;
2046
2047 /* Find pos and check to see if node is already in the sequence */
2048 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
2049 info->node);
2050
2051 if ((pos < ctxt->node_seq.length) &&
2052 (ctxt->node_seq.buffer != NULL) &&
2053 (ctxt->node_seq.buffer[pos].node == info->node)) {
2054 ctxt->node_seq.buffer[pos] = *info;
2055 }
2056
2057 /* Otherwise, we need to add new node to buffer */
2058 else {
2059 if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) ||
2060 (ctxt->node_seq.buffer == NULL)) {
2061 xmlParserNodeInfo *tmp_buffer;
2062 unsigned int byte_size;
2063
2064 if (ctxt->node_seq.maximum == 0)
2065 ctxt->node_seq.maximum = 2;
2066 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2067 (2 * ctxt->node_seq.maximum));
2068
2069 if (ctxt->node_seq.buffer == NULL)
2070 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
2071 else
2072 tmp_buffer =
2073 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2074 byte_size);
2075
2076 if (tmp_buffer == NULL) {
2077 xmlErrMemory(ctxt, "failed to allocate buffer\n");
2078 return;
2079 }
2080 ctxt->node_seq.buffer = tmp_buffer;
2081 ctxt->node_seq.maximum *= 2;
2082 }
2083
2084 /* If position is not at end, move elements out of the way */
2085 if (pos != ctxt->node_seq.length) {
2086 unsigned long i;
2087
2088 for (i = ctxt->node_seq.length; i > pos; i--)
2089 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2090 }
2091
2092 /* Copy element and increase length */
2093 ctxt->node_seq.buffer[pos] = *info;
2094 ctxt->node_seq.length++;
2095 }
2096 }
2097
2098 /************************************************************************
2099 * *
2100 * Defaults settings *
2101 * *
2102 ************************************************************************/
2103 /**
2104 * xmlPedanticParserDefault:
2105 * @val: int 0 or 1
2106 *
2107 * Set and return the previous value for enabling pedantic warnings.
2108 *
2109 * Returns the last value for 0 for no substitution, 1 for substitution.
2110 */
2111
2112 int
xmlPedanticParserDefault(int val)2113 xmlPedanticParserDefault(int val) {
2114 int old = xmlPedanticParserDefaultValue;
2115
2116 xmlPedanticParserDefaultValue = val;
2117 return(old);
2118 }
2119
2120 /**
2121 * xmlLineNumbersDefault:
2122 * @val: int 0 or 1
2123 *
2124 * Set and return the previous value for enabling line numbers in elements
2125 * contents. This may break on old application and is turned off by default.
2126 *
2127 * Returns the last value for 0 for no substitution, 1 for substitution.
2128 */
2129
2130 int
xmlLineNumbersDefault(int val)2131 xmlLineNumbersDefault(int val) {
2132 int old = xmlLineNumbersDefaultValue;
2133
2134 xmlLineNumbersDefaultValue = val;
2135 return(old);
2136 }
2137
2138 /**
2139 * xmlSubstituteEntitiesDefault:
2140 * @val: int 0 or 1
2141 *
2142 * Set and return the previous value for default entity support.
2143 * Initially the parser always keep entity references instead of substituting
2144 * entity values in the output. This function has to be used to change the
2145 * default parser behavior
2146 * SAX::substituteEntities() has to be used for changing that on a file by
2147 * file basis.
2148 *
2149 * Returns the last value for 0 for no substitution, 1 for substitution.
2150 */
2151
2152 int
xmlSubstituteEntitiesDefault(int val)2153 xmlSubstituteEntitiesDefault(int val) {
2154 int old = xmlSubstituteEntitiesDefaultValue;
2155
2156 xmlSubstituteEntitiesDefaultValue = val;
2157 return(old);
2158 }
2159
2160 /**
2161 * xmlKeepBlanksDefault:
2162 * @val: int 0 or 1
2163 *
2164 * Set and return the previous value for default blanks text nodes support.
2165 * The 1.x version of the parser used an heuristic to try to detect
2166 * ignorable white spaces. As a result the SAX callback was generating
2167 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
2168 * using the DOM output text nodes containing those blanks were not generated.
2169 * The 2.x and later version will switch to the XML standard way and
2170 * ignorableWhitespace() are only generated when running the parser in
2171 * validating mode and when the current element doesn't allow CDATA or
2172 * mixed content.
2173 * This function is provided as a way to force the standard behavior
2174 * on 1.X libs and to switch back to the old mode for compatibility when
2175 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2176 * by using xmlIsBlankNode() commodity function to detect the "empty"
2177 * nodes generated.
2178 * This value also affect autogeneration of indentation when saving code
2179 * if blanks sections are kept, indentation is not generated.
2180 *
2181 * Returns the last value for 0 for no substitution, 1 for substitution.
2182 */
2183
2184 int
xmlKeepBlanksDefault(int val)2185 xmlKeepBlanksDefault(int val) {
2186 int old = xmlKeepBlanksDefaultValue;
2187
2188 xmlKeepBlanksDefaultValue = val;
2189 if (!val) xmlIndentTreeOutput = 1;
2190 return(old);
2191 }
2192
2193 #define bottom_parserInternals
2194 #include "elfgcchack.h"
2195