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 | /** |
| 125 | * xmlErrEncoding: |
| 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 | */ |
| 134 | static void |
| 135 | xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 136 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 561 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 707 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 807 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 904 | xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 954 | xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 971 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 976 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 981 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 986 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 991 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 996 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1023 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1028 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1033 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 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 | /** |
| 1046 | * xmlSwitchToEncoding: |
| 1047 | * @ctxt: the parser context |
| 1048 | * @handler: the encoding handler |
| 1049 | * |
| 1050 | * change the input functions when discovering the character encoding |
| 1051 | * of a given entity. |
| 1052 | * |
| 1053 | * Returns 0 in case of success, -1 otherwise |
| 1054 | */ |
| 1055 | int |
| 1056 | xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler) |
| 1057 | { |
| 1058 | int nbchars; |
| 1059 | |
| 1060 | if (handler != NULL) { |
| 1061 | if (ctxt->input != NULL) { |
| 1062 | if (ctxt->input->buf != NULL) { |
| 1063 | if (ctxt->input->buf->encoder != NULL) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1064 | /* |
| 1065 | * Check in case the auto encoding detetection triggered |
| 1066 | * in already. |
| 1067 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1068 | if (ctxt->input->buf->encoder == handler) |
| 1069 | return(0); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1070 | |
| 1071 | /* |
| 1072 | * "UTF-16" can be used for both LE and BE |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1073 | if ((!xmlStrncmp(BAD_CAST ctxt->input->buf->encoder->name, |
| 1074 | BAD_CAST "UTF-16", 6)) && |
| 1075 | (!xmlStrncmp(BAD_CAST handler->name, |
| 1076 | BAD_CAST "UTF-16", 6))) { |
| 1077 | return(0); |
| 1078 | } |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1079 | */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1080 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1081 | /* |
| 1082 | * Note: this is a bit dangerous, but that's what it |
| 1083 | * takes to use nearly compatible signature for different |
| 1084 | * encodings. |
| 1085 | */ |
| 1086 | xmlCharEncCloseFunc(ctxt->input->buf->encoder); |
| 1087 | ctxt->input->buf->encoder = handler; |
| 1088 | return(0); |
| 1089 | } |
| 1090 | ctxt->input->buf->encoder = handler; |
| 1091 | |
| 1092 | /* |
| 1093 | * Is there already some content down the pipe to convert ? |
| 1094 | */ |
| 1095 | if ((ctxt->input->buf->buffer != NULL) && |
| 1096 | (ctxt->input->buf->buffer->use > 0)) { |
| 1097 | int processed; |
| 1098 | |
| 1099 | /* |
| 1100 | * Specific handling of the Byte Order Mark for |
| 1101 | * UTF-16 |
| 1102 | */ |
| 1103 | if ((handler->name != NULL) && |
| 1104 | (!strcmp(handler->name, "UTF-16LE")) && |
| 1105 | (ctxt->input->cur[0] == 0xFF) && |
| 1106 | (ctxt->input->cur[1] == 0xFE)) { |
| 1107 | ctxt->input->cur += 2; |
| 1108 | } |
| 1109 | if ((handler->name != NULL) && |
| 1110 | (!strcmp(handler->name, "UTF-16BE")) && |
| 1111 | (ctxt->input->cur[0] == 0xFE) && |
| 1112 | (ctxt->input->cur[1] == 0xFF)) { |
| 1113 | ctxt->input->cur += 2; |
| 1114 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1115 | /* |
| 1116 | * Errata on XML-1.0 June 20 2001 |
| 1117 | * Specific handling of the Byte Order Mark for |
| 1118 | * UTF-8 |
| 1119 | */ |
| 1120 | if ((handler->name != NULL) && |
| 1121 | (!strcmp(handler->name, "UTF-8")) && |
| 1122 | (ctxt->input->cur[0] == 0xEF) && |
| 1123 | (ctxt->input->cur[1] == 0xBB) && |
Daniel Veillard | 7dd0570 | 2001-10-04 14:25:12 +0000 | [diff] [blame] | 1124 | (ctxt->input->cur[2] == 0xBF)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1125 | ctxt->input->cur += 3; |
| 1126 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1129 | * Shrink the current input buffer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1130 | * Move it as the raw buffer and create a new input buffer |
| 1131 | */ |
| 1132 | processed = ctxt->input->cur - ctxt->input->base; |
| 1133 | xmlBufferShrink(ctxt->input->buf->buffer, processed); |
| 1134 | ctxt->input->buf->raw = ctxt->input->buf->buffer; |
| 1135 | ctxt->input->buf->buffer = xmlBufferCreate(); |
| 1136 | |
| 1137 | if (ctxt->html) { |
| 1138 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1139 | * convert as much as possible of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1140 | */ |
| 1141 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 1142 | ctxt->input->buf->buffer, |
| 1143 | ctxt->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(ctxt->input->buf->encoder, |
| 1152 | ctxt->input->buf->buffer, |
| 1153 | ctxt->input->buf->raw); |
| 1154 | } |
| 1155 | if (nbchars < 0) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1156 | xmlErrInternal(ctxt, |
| 1157 | "xmlSwitchToEncoding: encoder error\n", |
| 1158 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1159 | return(-1); |
| 1160 | } |
| 1161 | ctxt->input->base = |
| 1162 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1163 | ctxt->input->end = |
| 1164 | &ctxt->input->base[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1165 | |
| 1166 | } |
| 1167 | return(0); |
| 1168 | } else { |
| 1169 | if ((ctxt->input->length == 0) || (ctxt->input->buf == NULL)) { |
| 1170 | /* |
| 1171 | * When parsing a static memory array one must know the |
| 1172 | * size to be able to convert the buffer. |
| 1173 | */ |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1174 | xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n", |
| 1175 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1176 | return(-1); |
| 1177 | } else { |
| 1178 | int processed; |
| 1179 | |
| 1180 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1181 | * Shrink the current input buffer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1182 | * Move it as the raw buffer and create a new input buffer |
| 1183 | */ |
| 1184 | processed = ctxt->input->cur - ctxt->input->base; |
| 1185 | |
| 1186 | ctxt->input->buf->raw = xmlBufferCreate(); |
| 1187 | xmlBufferAdd(ctxt->input->buf->raw, ctxt->input->cur, |
| 1188 | ctxt->input->length - processed); |
| 1189 | ctxt->input->buf->buffer = xmlBufferCreate(); |
| 1190 | |
| 1191 | /* |
| 1192 | * convert as much as possible of the raw input |
| 1193 | * to the parser reading buffer. |
| 1194 | */ |
| 1195 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 1196 | ctxt->input->buf->buffer, |
| 1197 | ctxt->input->buf->raw); |
| 1198 | if (nbchars < 0) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1199 | xmlErrInternal(ctxt, |
| 1200 | "xmlSwitchToEncoding: encoder error\n", |
| 1201 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1202 | return(-1); |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * Conversion succeeded, get rid of the old buffer |
| 1207 | */ |
| 1208 | if ((ctxt->input->free != NULL) && |
| 1209 | (ctxt->input->base != NULL)) |
| 1210 | ctxt->input->free((xmlChar *) ctxt->input->base); |
| 1211 | ctxt->input->base = |
| 1212 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1213 | ctxt->input->end = |
| 1214 | &ctxt->input->base[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | } else { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1218 | xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n", |
| 1219 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1220 | return(-1); |
| 1221 | } |
| 1222 | /* |
| 1223 | * The parsing is now done in UTF8 natively |
| 1224 | */ |
| 1225 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1226 | } else |
| 1227 | return(-1); |
| 1228 | return(0); |
| 1229 | |
| 1230 | } |
| 1231 | |
| 1232 | /************************************************************************ |
| 1233 | * * |
| 1234 | * Commodity functions to handle entities processing * |
| 1235 | * * |
| 1236 | ************************************************************************/ |
| 1237 | |
| 1238 | /** |
| 1239 | * xmlFreeInputStream: |
| 1240 | * @input: an xmlParserInputPtr |
| 1241 | * |
| 1242 | * Free up an input stream. |
| 1243 | */ |
| 1244 | void |
| 1245 | xmlFreeInputStream(xmlParserInputPtr input) { |
| 1246 | if (input == NULL) return; |
| 1247 | |
| 1248 | if (input->filename != NULL) xmlFree((char *) input->filename); |
| 1249 | if (input->directory != NULL) xmlFree((char *) input->directory); |
| 1250 | if (input->encoding != NULL) xmlFree((char *) input->encoding); |
| 1251 | if (input->version != NULL) xmlFree((char *) input->version); |
| 1252 | if ((input->free != NULL) && (input->base != NULL)) |
| 1253 | input->free((xmlChar *) input->base); |
| 1254 | if (input->buf != NULL) |
| 1255 | xmlFreeParserInputBuffer(input->buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1256 | xmlFree(input); |
| 1257 | } |
| 1258 | |
| 1259 | /** |
| 1260 | * xmlNewInputStream: |
| 1261 | * @ctxt: an XML parser context |
| 1262 | * |
| 1263 | * Create a new input stream structure |
| 1264 | * Returns the new input stream or NULL |
| 1265 | */ |
| 1266 | xmlParserInputPtr |
| 1267 | xmlNewInputStream(xmlParserCtxtPtr ctxt) { |
| 1268 | xmlParserInputPtr input; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1269 | static int id = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1270 | |
| 1271 | input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput)); |
| 1272 | if (input == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1273 | xmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1274 | return(NULL); |
| 1275 | } |
| 1276 | memset(input, 0, sizeof(xmlParserInput)); |
| 1277 | input->line = 1; |
| 1278 | input->col = 1; |
| 1279 | input->standalone = -1; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1280 | /* |
| 1281 | * we don't care about thread reentrancy unicity for a single |
| 1282 | * parser context (and hence thread) is sufficient. |
| 1283 | */ |
| 1284 | input->id = id++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1285 | return(input); |
| 1286 | } |
| 1287 | |
| 1288 | /** |
| 1289 | * xmlNewIOInputStream: |
| 1290 | * @ctxt: an XML parser context |
| 1291 | * @input: an I/O Input |
| 1292 | * @enc: the charset encoding if known |
| 1293 | * |
| 1294 | * Create a new input stream structure encapsulating the @input into |
| 1295 | * a stream suitable for the parser. |
| 1296 | * |
| 1297 | * Returns the new input stream or NULL |
| 1298 | */ |
| 1299 | xmlParserInputPtr |
| 1300 | xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input, |
| 1301 | xmlCharEncoding enc) { |
| 1302 | xmlParserInputPtr inputStream; |
| 1303 | |
| 1304 | if (xmlParserDebugEntities) |
| 1305 | xmlGenericError(xmlGenericErrorContext, "new input from I/O\n"); |
| 1306 | inputStream = xmlNewInputStream(ctxt); |
| 1307 | if (inputStream == NULL) { |
| 1308 | return(NULL); |
| 1309 | } |
| 1310 | inputStream->filename = NULL; |
| 1311 | inputStream->buf = input; |
| 1312 | inputStream->base = inputStream->buf->buffer->content; |
| 1313 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1314 | inputStream->end = &inputStream->base[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1315 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 1316 | xmlSwitchEncoding(ctxt, enc); |
| 1317 | } |
| 1318 | |
| 1319 | return(inputStream); |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * xmlNewEntityInputStream: |
| 1324 | * @ctxt: an XML parser context |
| 1325 | * @entity: an Entity pointer |
| 1326 | * |
| 1327 | * Create a new input stream based on an xmlEntityPtr |
| 1328 | * |
| 1329 | * Returns the new input stream or NULL |
| 1330 | */ |
| 1331 | xmlParserInputPtr |
| 1332 | xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 1333 | xmlParserInputPtr input; |
| 1334 | |
| 1335 | if (entity == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1336 | xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n", |
| 1337 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1338 | return(NULL); |
| 1339 | } |
| 1340 | if (xmlParserDebugEntities) |
| 1341 | xmlGenericError(xmlGenericErrorContext, |
| 1342 | "new input from entity: %s\n", entity->name); |
| 1343 | if (entity->content == NULL) { |
| 1344 | switch (entity->etype) { |
| 1345 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1346 | xmlErrInternal(ctxt, "Cannot parse entity %s\n", |
| 1347 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1348 | break; |
| 1349 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 1350 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 1351 | return(xmlLoadExternalEntity((char *) entity->URI, |
| 1352 | (char *) entity->ExternalID, ctxt)); |
| 1353 | case XML_INTERNAL_GENERAL_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1354 | xmlErrInternal(ctxt, |
| 1355 | "Internal entity %s without content !\n", |
| 1356 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | case XML_INTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1359 | xmlErrInternal(ctxt, |
| 1360 | "Internal parameter entity %s without content !\n", |
| 1361 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1362 | break; |
| 1363 | case XML_INTERNAL_PREDEFINED_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1364 | xmlErrInternal(ctxt, |
| 1365 | "Predefined entity %s without content !\n", |
| 1366 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | } |
| 1369 | return(NULL); |
| 1370 | } |
| 1371 | input = xmlNewInputStream(ctxt); |
| 1372 | if (input == NULL) { |
| 1373 | return(NULL); |
| 1374 | } |
| 1375 | input->filename = (char *) entity->URI; |
| 1376 | input->base = entity->content; |
| 1377 | input->cur = entity->content; |
| 1378 | input->length = entity->length; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1379 | input->end = &entity->content[input->length]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1380 | return(input); |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * xmlNewStringInputStream: |
| 1385 | * @ctxt: an XML parser context |
| 1386 | * @buffer: an memory buffer |
| 1387 | * |
| 1388 | * Create a new input stream based on a memory buffer. |
| 1389 | * Returns the new input stream |
| 1390 | */ |
| 1391 | xmlParserInputPtr |
| 1392 | xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) { |
| 1393 | xmlParserInputPtr input; |
| 1394 | |
| 1395 | if (buffer == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1396 | xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n", |
| 1397 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1398 | return(NULL); |
| 1399 | } |
| 1400 | if (xmlParserDebugEntities) |
| 1401 | xmlGenericError(xmlGenericErrorContext, |
| 1402 | "new fixed input: %.30s\n", buffer); |
| 1403 | input = xmlNewInputStream(ctxt); |
| 1404 | if (input == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1405 | xmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1406 | return(NULL); |
| 1407 | } |
| 1408 | input->base = buffer; |
| 1409 | input->cur = buffer; |
| 1410 | input->length = xmlStrlen(buffer); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1411 | input->end = &buffer[input->length]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1412 | return(input); |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * xmlNewInputFromFile: |
| 1417 | * @ctxt: an XML parser context |
| 1418 | * @filename: the filename to use as entity |
| 1419 | * |
| 1420 | * Create a new input stream based on a file. |
| 1421 | * |
| 1422 | * Returns the new input stream or NULL in case of error |
| 1423 | */ |
| 1424 | xmlParserInputPtr |
| 1425 | xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) { |
| 1426 | xmlParserInputBufferPtr buf; |
| 1427 | xmlParserInputPtr inputStream; |
| 1428 | char *directory = NULL; |
| 1429 | xmlChar *URI = NULL; |
| 1430 | |
| 1431 | if (xmlParserDebugEntities) |
| 1432 | xmlGenericError(xmlGenericErrorContext, |
| 1433 | "new input from file: %s\n", filename); |
| 1434 | if (ctxt == NULL) return(NULL); |
| 1435 | buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE); |
| 1436 | if (buf == NULL) |
| 1437 | return(NULL); |
| 1438 | |
| 1439 | URI = xmlStrdup((xmlChar *) filename); |
| 1440 | directory = xmlParserGetDirectory((const char *) URI); |
| 1441 | |
| 1442 | inputStream = xmlNewInputStream(ctxt); |
| 1443 | if (inputStream == NULL) { |
| 1444 | if (directory != NULL) xmlFree((char *) directory); |
| 1445 | if (URI != NULL) xmlFree((char *) URI); |
| 1446 | return(NULL); |
| 1447 | } |
| 1448 | |
Daniel Veillard | 8d8bf2c | 2003-09-17 19:36:25 +0000 | [diff] [blame] | 1449 | inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI); |
Daniel Veillard | a66b1d1 | 2003-09-17 20:54:38 +0000 | [diff] [blame] | 1450 | if (URI != NULL) xmlFree((char *) URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1451 | inputStream->directory = directory; |
| 1452 | inputStream->buf = buf; |
| 1453 | |
| 1454 | inputStream->base = inputStream->buf->buffer->content; |
| 1455 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1456 | inputStream->end = &inputStream->base[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1457 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 1458 | ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory); |
| 1459 | return(inputStream); |
| 1460 | } |
| 1461 | |
| 1462 | /************************************************************************ |
| 1463 | * * |
| 1464 | * Commodity functions to handle parser contexts * |
| 1465 | * * |
| 1466 | ************************************************************************/ |
| 1467 | |
| 1468 | /** |
| 1469 | * xmlInitParserCtxt: |
| 1470 | * @ctxt: an XML parser context |
| 1471 | * |
| 1472 | * Initialize a parser context |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1473 | * |
| 1474 | * Returns 0 in case of success and -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1475 | */ |
| 1476 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1477 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1478 | xmlInitParserCtxt(xmlParserCtxtPtr ctxt) |
| 1479 | { |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 1480 | if(ctxt==NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1481 | xmlErrInternal(NULL, "Got NULL parser context\n", NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1482 | return(-1); |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1485 | xmlDefaultSAXHandlerInit(); |
| 1486 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1487 | ctxt->dict = xmlDictCreate(); |
| 1488 | if (ctxt->dict == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1489 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1490 | return(-1); |
| 1491 | } |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 1492 | ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
| 1493 | if (ctxt->sax == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1494 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1495 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1496 | } |
| 1497 | else |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 1498 | xmlSAXVersion(ctxt->sax, 2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1499 | |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1500 | ctxt->maxatts = 0; |
| 1501 | ctxt->atts = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1502 | /* Allocate the Input stack */ |
| 1503 | ctxt->inputTab = (xmlParserInputPtr *) |
| 1504 | xmlMalloc(5 * sizeof(xmlParserInputPtr)); |
| 1505 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1506 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1507 | ctxt->inputNr = 0; |
| 1508 | ctxt->inputMax = 0; |
| 1509 | ctxt->input = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1510 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1511 | } |
| 1512 | ctxt->inputNr = 0; |
| 1513 | ctxt->inputMax = 5; |
| 1514 | ctxt->input = NULL; |
| 1515 | |
| 1516 | ctxt->version = NULL; |
| 1517 | ctxt->encoding = NULL; |
| 1518 | ctxt->standalone = -1; |
| 1519 | ctxt->hasExternalSubset = 0; |
| 1520 | ctxt->hasPErefs = 0; |
| 1521 | ctxt->html = 0; |
| 1522 | ctxt->external = 0; |
| 1523 | ctxt->instate = XML_PARSER_START; |
| 1524 | ctxt->token = 0; |
| 1525 | ctxt->directory = NULL; |
| 1526 | |
| 1527 | /* Allocate the Node stack */ |
| 1528 | ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr)); |
| 1529 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1530 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1531 | ctxt->nodeNr = 0; |
| 1532 | ctxt->nodeMax = 0; |
| 1533 | ctxt->node = NULL; |
| 1534 | ctxt->inputNr = 0; |
| 1535 | ctxt->inputMax = 0; |
| 1536 | ctxt->input = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1537 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1538 | } |
| 1539 | ctxt->nodeNr = 0; |
| 1540 | ctxt->nodeMax = 10; |
| 1541 | ctxt->node = NULL; |
| 1542 | |
| 1543 | /* Allocate the Name stack */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1544 | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1546 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1547 | ctxt->nodeNr = 0; |
| 1548 | ctxt->nodeMax = 0; |
| 1549 | ctxt->node = NULL; |
| 1550 | ctxt->inputNr = 0; |
| 1551 | ctxt->inputMax = 0; |
| 1552 | ctxt->input = NULL; |
| 1553 | ctxt->nameNr = 0; |
| 1554 | ctxt->nameMax = 0; |
| 1555 | ctxt->name = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1556 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1557 | } |
| 1558 | ctxt->nameNr = 0; |
| 1559 | ctxt->nameMax = 10; |
| 1560 | ctxt->name = NULL; |
| 1561 | |
| 1562 | /* Allocate the space stack */ |
| 1563 | ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int)); |
| 1564 | if (ctxt->spaceTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1565 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1566 | ctxt->nodeNr = 0; |
| 1567 | ctxt->nodeMax = 0; |
| 1568 | ctxt->node = NULL; |
| 1569 | ctxt->inputNr = 0; |
| 1570 | ctxt->inputMax = 0; |
| 1571 | ctxt->input = NULL; |
| 1572 | ctxt->nameNr = 0; |
| 1573 | ctxt->nameMax = 0; |
| 1574 | ctxt->name = NULL; |
| 1575 | ctxt->spaceNr = 0; |
| 1576 | ctxt->spaceMax = 0; |
| 1577 | ctxt->space = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1578 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1579 | } |
| 1580 | ctxt->spaceNr = 1; |
| 1581 | ctxt->spaceMax = 10; |
| 1582 | ctxt->spaceTab[0] = -1; |
| 1583 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1584 | ctxt->userData = ctxt; |
| 1585 | ctxt->myDoc = NULL; |
| 1586 | ctxt->wellFormed = 1; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 1587 | ctxt->nsWellFormed = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | ctxt->valid = 1; |
| 1589 | ctxt->loadsubset = xmlLoadExtDtdDefaultValue; |
| 1590 | ctxt->validate = xmlDoValidityCheckingDefaultValue; |
| 1591 | ctxt->pedantic = xmlPedanticParserDefaultValue; |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 1592 | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1593 | ctxt->keepBlanks = xmlKeepBlanksDefaultValue; |
Daniel Veillard | 1669828 | 2001-09-14 10:29:27 +0000 | [diff] [blame] | 1594 | if (ctxt->keepBlanks == 0) |
Daniel Veillard | 11476b4 | 2003-09-26 14:51:39 +0000 | [diff] [blame] | 1595 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
Daniel Veillard | 1669828 | 2001-09-14 10:29:27 +0000 | [diff] [blame] | 1596 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1597 | ctxt->vctxt.userData = ctxt; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 1598 | ctxt->vctxt.error = xmlParserValidityError; |
| 1599 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1600 | if (ctxt->validate) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1601 | if (xmlGetWarningsDefaultValue == 0) |
| 1602 | ctxt->vctxt.warning = NULL; |
| 1603 | else |
| 1604 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 1605 | ctxt->vctxt.nodeMax = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1606 | } |
| 1607 | ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue; |
| 1608 | ctxt->record_info = 0; |
| 1609 | ctxt->nbChars = 0; |
| 1610 | ctxt->checkIndex = 0; |
| 1611 | ctxt->inSubset = 0; |
| 1612 | ctxt->errNo = XML_ERR_OK; |
| 1613 | ctxt->depth = 0; |
| 1614 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 1615 | ctxt->catalogs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1616 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1617 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /** |
| 1621 | * xmlFreeParserCtxt: |
| 1622 | * @ctxt: an XML parser context |
| 1623 | * |
| 1624 | * Free all the memory used by a parser context. However the parsed |
| 1625 | * document in ctxt->myDoc is not freed. |
| 1626 | */ |
| 1627 | |
| 1628 | void |
| 1629 | xmlFreeParserCtxt(xmlParserCtxtPtr ctxt) |
| 1630 | { |
| 1631 | xmlParserInputPtr input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (ctxt == NULL) return; |
| 1634 | |
| 1635 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 1636 | xmlFreeInputStream(input); |
| 1637 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1638 | if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab); |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 1639 | if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1640 | if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab); |
| 1641 | if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab); |
| 1642 | if (ctxt->version != NULL) xmlFree((char *) ctxt->version); |
| 1643 | if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1644 | if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI); |
| 1645 | if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1646 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 1647 | if ((ctxt->sax != NULL) && |
| 1648 | (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1649 | #else |
| 1650 | if (ctxt->sax != NULL) |
| 1651 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1652 | xmlFree(ctxt->sax); |
| 1653 | if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory); |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 1654 | if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab); |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 1655 | if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1656 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1657 | if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1658 | if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab); |
| 1659 | if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs); |
| 1660 | if (ctxt->attsDefault != NULL) |
| 1661 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1662 | if (ctxt->attsSpecial != NULL) |
| 1663 | xmlHashFree(ctxt->attsSpecial, NULL); |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1664 | if (ctxt->freeElems != NULL) { |
| 1665 | xmlNodePtr cur, next; |
| 1666 | |
| 1667 | cur = ctxt->freeElems; |
| 1668 | while (cur != NULL) { |
| 1669 | next = cur->next; |
| 1670 | xmlFree(cur); |
| 1671 | cur = next; |
| 1672 | } |
| 1673 | } |
| 1674 | if (ctxt->freeAttrs != NULL) { |
| 1675 | xmlAttrPtr cur, next; |
| 1676 | |
| 1677 | cur = ctxt->freeAttrs; |
| 1678 | while (cur != NULL) { |
| 1679 | next = cur->next; |
| 1680 | xmlFree(cur); |
| 1681 | cur = next; |
| 1682 | } |
| 1683 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 1684 | /* |
| 1685 | * cleanup the error strings |
| 1686 | */ |
| 1687 | if (ctxt->lastError.message != NULL) |
| 1688 | xmlFree(ctxt->lastError.message); |
| 1689 | if (ctxt->lastError.file != NULL) |
| 1690 | xmlFree(ctxt->lastError.file); |
| 1691 | if (ctxt->lastError.str1 != NULL) |
| 1692 | xmlFree(ctxt->lastError.str1); |
| 1693 | if (ctxt->lastError.str2 != NULL) |
| 1694 | xmlFree(ctxt->lastError.str2); |
| 1695 | if (ctxt->lastError.str3 != NULL) |
| 1696 | xmlFree(ctxt->lastError.str3); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1697 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 1698 | #ifdef LIBXML_CATALOG_ENABLED |
| 1699 | if (ctxt->catalogs != NULL) |
| 1700 | xmlCatalogFreeLocal(ctxt->catalogs); |
| 1701 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1702 | xmlFree(ctxt); |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * xmlNewParserCtxt: |
| 1707 | * |
| 1708 | * Allocate and initialize a new parser context. |
| 1709 | * |
| 1710 | * Returns the xmlParserCtxtPtr or NULL |
| 1711 | */ |
| 1712 | |
| 1713 | xmlParserCtxtPtr |
| 1714 | xmlNewParserCtxt() |
| 1715 | { |
| 1716 | xmlParserCtxtPtr ctxt; |
| 1717 | |
| 1718 | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
| 1719 | if (ctxt == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1720 | xmlErrMemory(NULL, "cannot allocate parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1721 | return(NULL); |
| 1722 | } |
| 1723 | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1724 | if (xmlInitParserCtxt(ctxt) < 0) { |
| 1725 | xmlFreeParserCtxt(ctxt); |
| 1726 | return(NULL); |
| 1727 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1728 | return(ctxt); |
| 1729 | } |
| 1730 | |
| 1731 | /************************************************************************ |
| 1732 | * * |
| 1733 | * Handling of node informations * |
| 1734 | * * |
| 1735 | ************************************************************************/ |
| 1736 | |
| 1737 | /** |
| 1738 | * xmlClearParserCtxt: |
| 1739 | * @ctxt: an XML parser context |
| 1740 | * |
| 1741 | * Clear (release owned resources) and reinitialize a parser context |
| 1742 | */ |
| 1743 | |
| 1744 | void |
| 1745 | xmlClearParserCtxt(xmlParserCtxtPtr ctxt) |
| 1746 | { |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 1747 | if (ctxt==NULL) |
| 1748 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1749 | xmlClearNodeInfoSeq(&ctxt->node_seq); |
| 1750 | xmlInitParserCtxt(ctxt); |
| 1751 | } |
| 1752 | |
| 1753 | /** |
| 1754 | * xmlParserFindNodeInfo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 1755 | * @ctx: an XML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1756 | * @node: an XML node within the tree |
| 1757 | * |
| 1758 | * Find the parser node info struct for a given node |
| 1759 | * |
| 1760 | * Returns an xmlParserNodeInfo block pointer or NULL |
| 1761 | */ |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 1762 | const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, |
| 1763 | const xmlNodePtr node) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1764 | { |
| 1765 | unsigned long pos; |
| 1766 | |
| 1767 | /* Find position where node should be at */ |
| 1768 | pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node); |
Daniel Veillard | b1d6287 | 2001-09-21 09:47:08 +0000 | [diff] [blame] | 1769 | 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] | 1770 | return &ctx->node_seq.buffer[pos]; |
| 1771 | else |
| 1772 | return NULL; |
| 1773 | } |
| 1774 | |
| 1775 | |
| 1776 | /** |
| 1777 | * xmlInitNodeInfoSeq: |
| 1778 | * @seq: a node info sequence pointer |
| 1779 | * |
| 1780 | * -- Initialize (set to initial state) node info sequence |
| 1781 | */ |
| 1782 | void |
| 1783 | xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
| 1784 | { |
| 1785 | seq->length = 0; |
| 1786 | seq->maximum = 0; |
| 1787 | seq->buffer = NULL; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * xmlClearNodeInfoSeq: |
| 1792 | * @seq: a node info sequence pointer |
| 1793 | * |
| 1794 | * -- Clear (release memory and reinitialize) node |
| 1795 | * info sequence |
| 1796 | */ |
| 1797 | void |
| 1798 | xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
| 1799 | { |
| 1800 | if ( seq->buffer != NULL ) |
| 1801 | xmlFree(seq->buffer); |
| 1802 | xmlInitNodeInfoSeq(seq); |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | /** |
| 1807 | * xmlParserFindNodeInfoIndex: |
| 1808 | * @seq: a node info sequence pointer |
| 1809 | * @node: an XML node pointer |
| 1810 | * |
| 1811 | * |
| 1812 | * xmlParserFindNodeInfoIndex : Find the index that the info record for |
| 1813 | * the given node is or should be at in a sorted sequence |
| 1814 | * |
| 1815 | * Returns a long indicating the position of the record |
| 1816 | */ |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 1817 | unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq, |
| 1818 | const xmlNodePtr node) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1819 | { |
| 1820 | unsigned long upper, lower, middle; |
| 1821 | int found = 0; |
| 1822 | |
| 1823 | /* Do a binary search for the key */ |
| 1824 | lower = 1; |
| 1825 | upper = seq->length; |
| 1826 | middle = 0; |
| 1827 | while ( lower <= upper && !found) { |
| 1828 | middle = lower + (upper - lower) / 2; |
| 1829 | if ( node == seq->buffer[middle - 1].node ) |
| 1830 | found = 1; |
| 1831 | else if ( node < seq->buffer[middle - 1].node ) |
| 1832 | upper = middle - 1; |
| 1833 | else |
| 1834 | lower = middle + 1; |
| 1835 | } |
| 1836 | |
| 1837 | /* Return position */ |
| 1838 | if ( middle == 0 || seq->buffer[middle - 1].node < node ) |
| 1839 | return middle; |
| 1840 | else |
| 1841 | return middle - 1; |
| 1842 | } |
| 1843 | |
| 1844 | |
| 1845 | /** |
| 1846 | * xmlParserAddNodeInfo: |
| 1847 | * @ctxt: an XML parser context |
| 1848 | * @info: a node info sequence pointer |
| 1849 | * |
| 1850 | * Insert node info record into the sorted sequence |
| 1851 | */ |
| 1852 | void |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1853 | xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 1854 | const xmlParserNodeInfoPtr info) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1855 | { |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1856 | unsigned long pos; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1857 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1858 | /* 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] | 1859 | pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr) |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1860 | info->node); |
| 1861 | if (pos < ctxt->node_seq.length |
| 1862 | && ctxt->node_seq.buffer[pos].node == info->node) { |
| 1863 | ctxt->node_seq.buffer[pos] = *info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1866 | /* Otherwise, we need to add new node to buffer */ |
| 1867 | else { |
| 1868 | if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) { |
| 1869 | xmlParserNodeInfo *tmp_buffer; |
| 1870 | unsigned int byte_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1872 | if (ctxt->node_seq.maximum == 0) |
| 1873 | ctxt->node_seq.maximum = 2; |
| 1874 | byte_size = (sizeof(*ctxt->node_seq.buffer) * |
| 1875 | (2 * ctxt->node_seq.maximum)); |
| 1876 | |
| 1877 | if (ctxt->node_seq.buffer == NULL) |
Daniel Veillard | c4f65ab | 2003-04-21 23:07:45 +0000 | [diff] [blame] | 1878 | tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size); |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1879 | else |
| 1880 | tmp_buffer = |
| 1881 | (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer, |
| 1882 | byte_size); |
| 1883 | |
| 1884 | if (tmp_buffer == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1885 | xmlErrMemory(ctxt, "failed to allocate buffer\n"); |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | ctxt->node_seq.buffer = tmp_buffer; |
| 1889 | ctxt->node_seq.maximum *= 2; |
| 1890 | } |
| 1891 | |
| 1892 | /* If position is not at end, move elements out of the way */ |
| 1893 | if (pos != ctxt->node_seq.length) { |
| 1894 | unsigned long i; |
| 1895 | |
| 1896 | for (i = ctxt->node_seq.length; i > pos; i--) |
| 1897 | ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1]; |
| 1898 | } |
| 1899 | |
| 1900 | /* Copy element and increase length */ |
| 1901 | ctxt->node_seq.buffer[pos] = *info; |
| 1902 | ctxt->node_seq.length++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /************************************************************************ |
| 1907 | * * |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 1908 | * Defaults settings * |
| 1909 | * * |
| 1910 | ************************************************************************/ |
| 1911 | /** |
| 1912 | * xmlPedanticParserDefault: |
| 1913 | * @val: int 0 or 1 |
| 1914 | * |
| 1915 | * Set and return the previous value for enabling pedantic warnings. |
| 1916 | * |
| 1917 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 1918 | */ |
| 1919 | |
| 1920 | int |
| 1921 | xmlPedanticParserDefault(int val) { |
| 1922 | int old = xmlPedanticParserDefaultValue; |
| 1923 | |
| 1924 | xmlPedanticParserDefaultValue = val; |
| 1925 | return(old); |
| 1926 | } |
| 1927 | |
| 1928 | /** |
| 1929 | * xmlLineNumbersDefault: |
| 1930 | * @val: int 0 or 1 |
| 1931 | * |
| 1932 | * Set and return the previous value for enabling line numbers in elements |
| 1933 | * contents. This may break on old application and is turned off by default. |
| 1934 | * |
| 1935 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 1936 | */ |
| 1937 | |
| 1938 | int |
| 1939 | xmlLineNumbersDefault(int val) { |
| 1940 | int old = xmlLineNumbersDefaultValue; |
| 1941 | |
| 1942 | xmlLineNumbersDefaultValue = val; |
| 1943 | return(old); |
| 1944 | } |
| 1945 | |
| 1946 | /** |
| 1947 | * xmlSubstituteEntitiesDefault: |
| 1948 | * @val: int 0 or 1 |
| 1949 | * |
| 1950 | * Set and return the previous value for default entity support. |
| 1951 | * Initially the parser always keep entity references instead of substituting |
| 1952 | * 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] | 1953 | * default parser behavior |
| 1954 | * 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] | 1955 | * file basis. |
| 1956 | * |
| 1957 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 1958 | */ |
| 1959 | |
| 1960 | int |
| 1961 | xmlSubstituteEntitiesDefault(int val) { |
| 1962 | int old = xmlSubstituteEntitiesDefaultValue; |
| 1963 | |
| 1964 | xmlSubstituteEntitiesDefaultValue = val; |
| 1965 | return(old); |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * xmlKeepBlanksDefault: |
| 1970 | * @val: int 0 or 1 |
| 1971 | * |
| 1972 | * Set and return the previous value for default blanks text nodes support. |
| 1973 | * The 1.x version of the parser used an heuristic to try to detect |
| 1974 | * ignorable white spaces. As a result the SAX callback was generating |
Daniel Veillard | 11476b4 | 2003-09-26 14:51:39 +0000 | [diff] [blame] | 1975 | * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 1976 | * using the DOM output text nodes containing those blanks were not generated. |
| 1977 | * The 2.x and later version will switch to the XML standard way and |
| 1978 | * ignorableWhitespace() are only generated when running the parser in |
| 1979 | * validating mode and when the current element doesn't allow CDATA or |
| 1980 | * mixed content. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1981 | * This function is provided as a way to force the standard behavior |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 1982 | * on 1.X libs and to switch back to the old mode for compatibility when |
| 1983 | * running 1.X client code on 2.X . Upgrade of 1.X code should be done |
| 1984 | * by using xmlIsBlankNode() commodity function to detect the "empty" |
| 1985 | * nodes generated. |
| 1986 | * This value also affect autogeneration of indentation when saving code |
| 1987 | * if blanks sections are kept, indentation is not generated. |
| 1988 | * |
| 1989 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 1990 | */ |
| 1991 | |
| 1992 | int |
| 1993 | xmlKeepBlanksDefault(int val) { |
| 1994 | int old = xmlKeepBlanksDefaultValue; |
| 1995 | |
| 1996 | xmlKeepBlanksDefaultValue = val; |
| 1997 | xmlIndentTreeOutput = !val; |
| 1998 | return(old); |
| 1999 | } |
| 2000 | |