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