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