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