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