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