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> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 56 | |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Various global defaults for parsing |
| 59 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 60 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 61 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 62 | * xmlCheckVersion: |
| 63 | * @version: the include version number |
| 64 | * |
| 65 | * check the compiled lib version against the include one. |
| 66 | * This can warn or immediately kill the application |
| 67 | */ |
| 68 | void |
| 69 | xmlCheckVersion(int version) { |
| 70 | int myversion = (int) LIBXML_VERSION; |
| 71 | |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 72 | xmlInitParser(); |
Daniel Veillard | 4de4d3b | 2001-05-07 20:50:47 +0000 | [diff] [blame] | 73 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 74 | if ((myversion / 10000) != (version / 10000)) { |
| 75 | xmlGenericError(xmlGenericErrorContext, |
| 76 | "Fatal: program compiled against libxml %d using libxml %d\n", |
| 77 | (version / 10000), (myversion / 10000)); |
Daniel Veillard | c69e0b1 | 2001-11-20 08:35:07 +0000 | [diff] [blame] | 78 | fprintf(stderr, |
| 79 | "Fatal: program compiled against libxml %d using libxml %d\n", |
| 80 | (version / 10000), (myversion / 10000)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 81 | } |
| 82 | if ((myversion / 100) < (version / 100)) { |
| 83 | xmlGenericError(xmlGenericErrorContext, |
| 84 | "Warning: program compiled against libxml %d using older %d\n", |
| 85 | (version / 100), (myversion / 100)); |
| 86 | } |
| 87 | } |
| 88 | |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 89 | |
| 90 | /************************************************************************ |
| 91 | * * |
| 92 | * Some factorized error routines * |
| 93 | * * |
| 94 | ************************************************************************/ |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * xmlErrMemory: |
| 99 | * @ctxt: an XML parser context |
| 100 | * @extra: extra informations |
| 101 | * |
| 102 | * Handle a redefinition of attribute error |
| 103 | */ |
| 104 | void |
| 105 | xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra) |
| 106 | { |
| 107 | if (ctxt != NULL) { |
| 108 | ctxt->errNo = XML_ERR_NO_MEMORY; |
| 109 | ctxt->instate = XML_PARSER_EOF; |
| 110 | ctxt->disableSAX = 1; |
| 111 | } |
| 112 | if (extra) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame^] | 113 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 114 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 115 | NULL, NULL, 0, 0, |
| 116 | "Memory allocation failed : %s\n", extra); |
| 117 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame^] | 118 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 119 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL, |
| 120 | NULL, NULL, 0, 0, "Memory allocation failed\n"); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * xmlErrEncoding: |
| 125 | * @ctxt: an XML parser context |
| 126 | * @error: the error number |
| 127 | * @msg: the error message |
| 128 | * @str1: an string info |
| 129 | * @str2: an string info |
| 130 | * |
| 131 | * Handle an encoding error |
| 132 | */ |
| 133 | static void |
| 134 | xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 135 | const char *msg, const xmlChar * str1, const xmlChar * str2) |
| 136 | { |
| 137 | if (ctxt != NULL) |
| 138 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame^] | 139 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 140 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 141 | NULL, 0, (const char *) str1, (const char *) str2, |
| 142 | NULL, 0, 0, msg, str1, str2); |
| 143 | if (ctxt != NULL) { |
| 144 | ctxt->wellFormed = 0; |
| 145 | if (ctxt->recovery == 0) |
| 146 | ctxt->disableSAX = 1; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * xmlErrInternal: |
| 152 | * @ctxt: an XML parser context |
| 153 | * @msg: the error message |
| 154 | * @str: error informations |
| 155 | * |
| 156 | * Handle an internal error |
| 157 | */ |
| 158 | static void |
| 159 | xmlErrInternal(xmlParserCtxtPtr ctxt, const char *msg, const xmlChar * str) |
| 160 | { |
| 161 | if (ctxt != NULL) |
| 162 | ctxt->errNo = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame^] | 163 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 164 | ctxt, NULL, XML_FROM_PARSER, XML_ERR_INTERNAL_ERROR, |
| 165 | XML_ERR_FATAL, NULL, 0, (const char *) str, NULL, NULL, |
| 166 | 0, 0, msg, str); |
| 167 | if (ctxt != NULL) { |
| 168 | ctxt->wellFormed = 0; |
| 169 | if (ctxt->recovery == 0) |
| 170 | ctxt->disableSAX = 1; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * xmlErrEncodingInt: |
| 176 | * @ctxt: an XML parser context |
| 177 | * @error: the error number |
| 178 | * @msg: the error message |
| 179 | * @val: an integer value |
| 180 | * |
| 181 | * n encoding error |
| 182 | */ |
| 183 | static void |
| 184 | xmlErrEncodingInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 185 | const char *msg, int val) |
| 186 | { |
| 187 | if (ctxt != NULL) |
| 188 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame^] | 189 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 190 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 191 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
| 192 | if (ctxt != NULL) { |
| 193 | ctxt->wellFormed = 0; |
| 194 | if (ctxt->recovery == 0) |
| 195 | ctxt->disableSAX = 1; |
| 196 | } |
| 197 | } |
| 198 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 199 | /************************************************************************ |
| 200 | * * |
| 201 | * Some functions to avoid too large macros * |
| 202 | * * |
| 203 | ************************************************************************/ |
| 204 | |
| 205 | /** |
| 206 | * xmlIsChar: |
| 207 | * @c: an unicode character (int) |
| 208 | * |
| 209 | * Check whether the character is allowed by the production |
| 210 | * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
| 211 | * | [#x10000-#x10FFFF] |
| 212 | * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. |
| 213 | * Also available as a macro IS_CHAR() |
| 214 | * |
| 215 | * Returns 0 if not, non-zero otherwise |
| 216 | */ |
| 217 | int |
| 218 | xmlIsChar(int c) { |
| 219 | return( |
| 220 | ((c) == 0x09) || ((c) == 0x0A) || ((c) == 0x0D) || |
| 221 | (((c) >= 0x20) && ((c) <= 0xD7FF)) || |
| 222 | (((c) >= 0xE000) && ((c) <= 0xFFFD)) || |
| 223 | (((c) >= 0x10000) && ((c) <= 0x10FFFF))); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * xmlIsBlank: |
| 228 | * @c: an unicode character (int) |
| 229 | * |
| 230 | * Check whether the character is allowed by the production |
| 231 | * [3] S ::= (#x20 | #x9 | #xD | #xA)+ |
| 232 | * Also available as a macro IS_BLANK() |
| 233 | * |
| 234 | * Returns 0 if not, non-zero otherwise |
| 235 | */ |
| 236 | int |
| 237 | xmlIsBlank(int c) { |
| 238 | return(((c) == 0x20) || ((c) == 0x09) || ((c) == 0xA) || ((c) == 0x0D)); |
| 239 | } |
| 240 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 241 | static int xmlBaseArray[] = { |
| 242 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0000 - 0x000F */ |
| 243 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0010 - 0x001F */ |
| 244 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0020 - 0x002F */ |
| 245 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0030 - 0x003F */ |
| 246 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0040 - 0x004F */ |
| 247 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0050 - 0x005F */ |
| 248 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0060 - 0x006F */ |
| 249 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0070 - 0x007F */ |
| 250 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0080 - 0x008F */ |
| 251 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0090 - 0x009F */ |
| 252 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00A0 - 0x00AF */ |
| 253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00B0 - 0x00BF */ |
| 254 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00C0 - 0x00CF */ |
| 255 | 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00D0 - 0x00DF */ |
| 256 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00E0 - 0x00EF */ |
| 257 | 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00F0 - 0x00FF */ |
| 258 | }; |
| 259 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 260 | /** |
| 261 | * xmlIsBaseChar: |
| 262 | * @c: an unicode character (int) |
| 263 | * |
| 264 | * Check whether the character is allowed by the production |
| 265 | * [85] BaseChar ::= ... long list see REC ... |
| 266 | * |
| 267 | * VI is your friend ! |
| 268 | * :1,$ s/\[#x\([0-9A-Z]*\)-#x\([0-9A-Z]*\)\]/ (((c) >= 0x\1) \&\& ((c) <= 0x\2)) ||/ |
| 269 | * and |
| 270 | * :1,$ s/#x\([0-9A-Z]*\)/ ((c) == 0x\1) ||/ |
| 271 | * |
| 272 | * Returns 0 if not, non-zero otherwise |
| 273 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 274 | int |
| 275 | xmlIsBaseChar(int c) { |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 276 | if (c < 0x0100) return(xmlBaseArray[c]); |
| 277 | return((((c) >= 0x0100) && ((c) <= 0x0131)) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 278 | (((c) >= 0x0134) && ((c) <= 0x013E)) || |
| 279 | (((c) >= 0x0141) && ((c) <= 0x0148)) || |
| 280 | (((c) >= 0x014A) && ((c) <= 0x017E)) || |
| 281 | (((c) >= 0x0180) && ((c) <= 0x01C3)) || |
| 282 | (((c) >= 0x01CD) && ((c) <= 0x01F0)) || |
| 283 | (((c) >= 0x01F4) && ((c) <= 0x01F5)) || |
| 284 | (((c) >= 0x01FA) && ((c) <= 0x0217)) || |
| 285 | (((c) >= 0x0250) && ((c) <= 0x02A8)) || |
| 286 | (((c) >= 0x02BB) && ((c) <= 0x02C1)) || |
| 287 | ((c) == 0x0386) || |
| 288 | (((c) >= 0x0388) && ((c) <= 0x038A)) || |
| 289 | ((c) == 0x038C) || |
| 290 | (((c) >= 0x038E) && ((c) <= 0x03A1)) || |
| 291 | (((c) >= 0x03A3) && ((c) <= 0x03CE)) || |
| 292 | (((c) >= 0x03D0) && ((c) <= 0x03D6)) || |
| 293 | ((c) == 0x03DA) || |
| 294 | ((c) == 0x03DC) || |
| 295 | ((c) == 0x03DE) || |
| 296 | ((c) == 0x03E0) || |
| 297 | (((c) >= 0x03E2) && ((c) <= 0x03F3)) || |
| 298 | (((c) >= 0x0401) && ((c) <= 0x040C)) || |
| 299 | (((c) >= 0x040E) && ((c) <= 0x044F)) || |
| 300 | (((c) >= 0x0451) && ((c) <= 0x045C)) || |
| 301 | (((c) >= 0x045E) && ((c) <= 0x0481)) || |
| 302 | (((c) >= 0x0490) && ((c) <= 0x04C4)) || |
| 303 | (((c) >= 0x04C7) && ((c) <= 0x04C8)) || |
| 304 | (((c) >= 0x04CB) && ((c) <= 0x04CC)) || |
| 305 | (((c) >= 0x04D0) && ((c) <= 0x04EB)) || |
| 306 | (((c) >= 0x04EE) && ((c) <= 0x04F5)) || |
| 307 | (((c) >= 0x04F8) && ((c) <= 0x04F9)) || |
| 308 | (((c) >= 0x0531) && ((c) <= 0x0556)) || |
| 309 | ((c) == 0x0559) || |
| 310 | (((c) >= 0x0561) && ((c) <= 0x0586)) || |
| 311 | (((c) >= 0x05D0) && ((c) <= 0x05EA)) || |
| 312 | (((c) >= 0x05F0) && ((c) <= 0x05F2)) || |
| 313 | (((c) >= 0x0621) && ((c) <= 0x063A)) || |
| 314 | (((c) >= 0x0641) && ((c) <= 0x064A)) || |
| 315 | (((c) >= 0x0671) && ((c) <= 0x06B7)) || |
| 316 | (((c) >= 0x06BA) && ((c) <= 0x06BE)) || |
| 317 | (((c) >= 0x06C0) && ((c) <= 0x06CE)) || |
| 318 | (((c) >= 0x06D0) && ((c) <= 0x06D3)) || |
| 319 | ((c) == 0x06D5) || |
| 320 | (((c) >= 0x06E5) && ((c) <= 0x06E6)) || |
| 321 | (((c) >= 0x905) && ( /* accelerator */ |
| 322 | (((c) >= 0x0905) && ((c) <= 0x0939)) || |
| 323 | ((c) == 0x093D) || |
| 324 | (((c) >= 0x0958) && ((c) <= 0x0961)) || |
| 325 | (((c) >= 0x0985) && ((c) <= 0x098C)) || |
| 326 | (((c) >= 0x098F) && ((c) <= 0x0990)) || |
| 327 | (((c) >= 0x0993) && ((c) <= 0x09A8)) || |
| 328 | (((c) >= 0x09AA) && ((c) <= 0x09B0)) || |
| 329 | ((c) == 0x09B2) || |
| 330 | (((c) >= 0x09B6) && ((c) <= 0x09B9)) || |
| 331 | (((c) >= 0x09DC) && ((c) <= 0x09DD)) || |
| 332 | (((c) >= 0x09DF) && ((c) <= 0x09E1)) || |
| 333 | (((c) >= 0x09F0) && ((c) <= 0x09F1)) || |
| 334 | (((c) >= 0x0A05) && ((c) <= 0x0A0A)) || |
| 335 | (((c) >= 0x0A0F) && ((c) <= 0x0A10)) || |
| 336 | (((c) >= 0x0A13) && ((c) <= 0x0A28)) || |
| 337 | (((c) >= 0x0A2A) && ((c) <= 0x0A30)) || |
| 338 | (((c) >= 0x0A32) && ((c) <= 0x0A33)) || |
| 339 | (((c) >= 0x0A35) && ((c) <= 0x0A36)) || |
| 340 | (((c) >= 0x0A38) && ((c) <= 0x0A39)) || |
| 341 | (((c) >= 0x0A59) && ((c) <= 0x0A5C)) || |
| 342 | ((c) == 0x0A5E) || |
| 343 | (((c) >= 0x0A72) && ((c) <= 0x0A74)) || |
| 344 | (((c) >= 0x0A85) && ((c) <= 0x0A8B)) || |
| 345 | ((c) == 0x0A8D) || |
| 346 | (((c) >= 0x0A8F) && ((c) <= 0x0A91)) || |
| 347 | (((c) >= 0x0A93) && ((c) <= 0x0AA8)) || |
| 348 | (((c) >= 0x0AAA) && ((c) <= 0x0AB0)) || |
| 349 | (((c) >= 0x0AB2) && ((c) <= 0x0AB3)) || |
| 350 | (((c) >= 0x0AB5) && ((c) <= 0x0AB9)) || |
| 351 | ((c) == 0x0ABD) || |
| 352 | ((c) == 0x0AE0) || |
| 353 | (((c) >= 0x0B05) && ((c) <= 0x0B0C)) || |
| 354 | (((c) >= 0x0B0F) && ((c) <= 0x0B10)) || |
| 355 | (((c) >= 0x0B13) && ((c) <= 0x0B28)) || |
| 356 | (((c) >= 0x0B2A) && ((c) <= 0x0B30)) || |
| 357 | (((c) >= 0x0B32) && ((c) <= 0x0B33)) || |
| 358 | (((c) >= 0x0B36) && ((c) <= 0x0B39)) || |
| 359 | ((c) == 0x0B3D) || |
| 360 | (((c) >= 0x0B5C) && ((c) <= 0x0B5D)) || |
| 361 | (((c) >= 0x0B5F) && ((c) <= 0x0B61)) || |
| 362 | (((c) >= 0x0B85) && ((c) <= 0x0B8A)) || |
| 363 | (((c) >= 0x0B8E) && ((c) <= 0x0B90)) || |
| 364 | (((c) >= 0x0B92) && ((c) <= 0x0B95)) || |
| 365 | (((c) >= 0x0B99) && ((c) <= 0x0B9A)) || |
| 366 | ((c) == 0x0B9C) || |
| 367 | (((c) >= 0x0B9E) && ((c) <= 0x0B9F)) || |
| 368 | (((c) >= 0x0BA3) && ((c) <= 0x0BA4)) || |
| 369 | (((c) >= 0x0BA8) && ((c) <= 0x0BAA)) || |
| 370 | (((c) >= 0x0BAE) && ((c) <= 0x0BB5)) || |
| 371 | (((c) >= 0x0BB7) && ((c) <= 0x0BB9)) || |
| 372 | (((c) >= 0x0C05) && ((c) <= 0x0C0C)) || |
| 373 | (((c) >= 0x0C0E) && ((c) <= 0x0C10)) || |
| 374 | (((c) >= 0x0C12) && ((c) <= 0x0C28)) || |
| 375 | (((c) >= 0x0C2A) && ((c) <= 0x0C33)) || |
| 376 | (((c) >= 0x0C35) && ((c) <= 0x0C39)) || |
| 377 | (((c) >= 0x0C60) && ((c) <= 0x0C61)) || |
| 378 | (((c) >= 0x0C85) && ((c) <= 0x0C8C)) || |
| 379 | (((c) >= 0x0C8E) && ((c) <= 0x0C90)) || |
| 380 | (((c) >= 0x0C92) && ((c) <= 0x0CA8)) || |
| 381 | (((c) >= 0x0CAA) && ((c) <= 0x0CB3)) || |
| 382 | (((c) >= 0x0CB5) && ((c) <= 0x0CB9)) || |
| 383 | ((c) == 0x0CDE) || |
| 384 | (((c) >= 0x0CE0) && ((c) <= 0x0CE1)) || |
| 385 | (((c) >= 0x0D05) && ((c) <= 0x0D0C)) || |
| 386 | (((c) >= 0x0D0E) && ((c) <= 0x0D10)) || |
| 387 | (((c) >= 0x0D12) && ((c) <= 0x0D28)) || |
| 388 | (((c) >= 0x0D2A) && ((c) <= 0x0D39)) || |
| 389 | (((c) >= 0x0D60) && ((c) <= 0x0D61)) || |
| 390 | (((c) >= 0x0E01) && ((c) <= 0x0E2E)) || |
| 391 | ((c) == 0x0E30) || |
| 392 | (((c) >= 0x0E32) && ((c) <= 0x0E33)) || |
| 393 | (((c) >= 0x0E40) && ((c) <= 0x0E45)) || |
| 394 | (((c) >= 0x0E81) && ((c) <= 0x0E82)) || |
| 395 | ((c) == 0x0E84) || |
| 396 | (((c) >= 0x0E87) && ((c) <= 0x0E88)) || |
| 397 | ((c) == 0x0E8A) || |
| 398 | ((c) == 0x0E8D) || |
| 399 | (((c) >= 0x0E94) && ((c) <= 0x0E97)) || |
| 400 | (((c) >= 0x0E99) && ((c) <= 0x0E9F)) || |
| 401 | (((c) >= 0x0EA1) && ((c) <= 0x0EA3)) || |
| 402 | ((c) == 0x0EA5) || |
| 403 | ((c) == 0x0EA7) || |
| 404 | (((c) >= 0x0EAA) && ((c) <= 0x0EAB)) || |
| 405 | (((c) >= 0x0EAD) && ((c) <= 0x0EAE)) || |
| 406 | ((c) == 0x0EB0) || |
| 407 | (((c) >= 0x0EB2) && ((c) <= 0x0EB3)) || |
| 408 | ((c) == 0x0EBD) || |
| 409 | (((c) >= 0x0EC0) && ((c) <= 0x0EC4)) || |
| 410 | (((c) >= 0x0F40) && ((c) <= 0x0F47)) || |
| 411 | (((c) >= 0x0F49) && ((c) <= 0x0F69)) || |
| 412 | (((c) >= 0x10A0) && ( /* accelerator */ |
| 413 | (((c) >= 0x10A0) && ((c) <= 0x10C5)) || |
| 414 | (((c) >= 0x10D0) && ((c) <= 0x10F6)) || |
| 415 | ((c) == 0x1100) || |
| 416 | (((c) >= 0x1102) && ((c) <= 0x1103)) || |
| 417 | (((c) >= 0x1105) && ((c) <= 0x1107)) || |
| 418 | ((c) == 0x1109) || |
| 419 | (((c) >= 0x110B) && ((c) <= 0x110C)) || |
| 420 | (((c) >= 0x110E) && ((c) <= 0x1112)) || |
| 421 | ((c) == 0x113C) || |
| 422 | ((c) == 0x113E) || |
| 423 | ((c) == 0x1140) || |
| 424 | ((c) == 0x114C) || |
| 425 | ((c) == 0x114E) || |
| 426 | ((c) == 0x1150) || |
| 427 | (((c) >= 0x1154) && ((c) <= 0x1155)) || |
| 428 | ((c) == 0x1159) || |
| 429 | (((c) >= 0x115F) && ((c) <= 0x1161)) || |
| 430 | ((c) == 0x1163) || |
| 431 | ((c) == 0x1165) || |
| 432 | ((c) == 0x1167) || |
| 433 | ((c) == 0x1169) || |
| 434 | (((c) >= 0x116D) && ((c) <= 0x116E)) || |
| 435 | (((c) >= 0x1172) && ((c) <= 0x1173)) || |
| 436 | ((c) == 0x1175) || |
| 437 | ((c) == 0x119E) || |
| 438 | ((c) == 0x11A8) || |
| 439 | ((c) == 0x11AB) || |
| 440 | (((c) >= 0x11AE) && ((c) <= 0x11AF)) || |
| 441 | (((c) >= 0x11B7) && ((c) <= 0x11B8)) || |
| 442 | ((c) == 0x11BA) || |
| 443 | (((c) >= 0x11BC) && ((c) <= 0x11C2)) || |
| 444 | ((c) == 0x11EB) || |
| 445 | ((c) == 0x11F0) || |
| 446 | ((c) == 0x11F9) || |
| 447 | (((c) >= 0x1E00) && ((c) <= 0x1E9B)) || |
| 448 | (((c) >= 0x1EA0) && ((c) <= 0x1EF9)) || |
| 449 | (((c) >= 0x1F00) && ((c) <= 0x1F15)) || |
| 450 | (((c) >= 0x1F18) && ((c) <= 0x1F1D)) || |
| 451 | (((c) >= 0x1F20) && ((c) <= 0x1F45)) || |
| 452 | (((c) >= 0x1F48) && ((c) <= 0x1F4D)) || |
| 453 | (((c) >= 0x1F50) && ((c) <= 0x1F57)) || |
| 454 | ((c) == 0x1F59) || |
| 455 | ((c) == 0x1F5B) || |
| 456 | ((c) == 0x1F5D) || |
| 457 | (((c) >= 0x1F5F) && ((c) <= 0x1F7D)) || |
| 458 | (((c) >= 0x1F80) && ((c) <= 0x1FB4)) || |
| 459 | (((c) >= 0x1FB6) && ((c) <= 0x1FBC)) || |
| 460 | ((c) == 0x1FBE) || |
| 461 | (((c) >= 0x1FC2) && ((c) <= 0x1FC4)) || |
| 462 | (((c) >= 0x1FC6) && ((c) <= 0x1FCC)) || |
| 463 | (((c) >= 0x1FD0) && ((c) <= 0x1FD3)) || |
| 464 | (((c) >= 0x1FD6) && ((c) <= 0x1FDB)) || |
| 465 | (((c) >= 0x1FE0) && ((c) <= 0x1FEC)) || |
| 466 | (((c) >= 0x1FF2) && ((c) <= 0x1FF4)) || |
| 467 | (((c) >= 0x1FF6) && ((c) <= 0x1FFC)) || |
| 468 | ((c) == 0x2126) || |
| 469 | (((c) >= 0x212A) && ((c) <= 0x212B)) || |
| 470 | ((c) == 0x212E) || |
| 471 | (((c) >= 0x2180) && ((c) <= 0x2182)) || |
| 472 | (((c) >= 0x3041) && ((c) <= 0x3094)) || |
| 473 | (((c) >= 0x30A1) && ((c) <= 0x30FA)) || |
| 474 | (((c) >= 0x3105) && ((c) <= 0x312C)) || |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 475 | (((c) >= 0xAC00) && ((c) <= 0xD7A3))) /* accelerators */ |
| 476 | )))); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /** |
| 480 | * xmlIsDigit: |
| 481 | * @c: an unicode character (int) |
| 482 | * |
| 483 | * Check whether the character is allowed by the production |
| 484 | * [88] Digit ::= ... long list see REC ... |
| 485 | * |
| 486 | * Returns 0 if not, non-zero otherwise |
| 487 | */ |
| 488 | int |
| 489 | xmlIsDigit(int c) { |
| 490 | return( |
| 491 | (((c) >= 0x0030) && ((c) <= 0x0039)) || |
| 492 | (((c) >= 0x660) && ( /* accelerator */ |
| 493 | (((c) >= 0x0660) && ((c) <= 0x0669)) || |
| 494 | (((c) >= 0x06F0) && ((c) <= 0x06F9)) || |
| 495 | (((c) >= 0x0966) && ((c) <= 0x096F)) || |
| 496 | (((c) >= 0x09E6) && ((c) <= 0x09EF)) || |
| 497 | (((c) >= 0x0A66) && ((c) <= 0x0A6F)) || |
| 498 | (((c) >= 0x0AE6) && ((c) <= 0x0AEF)) || |
| 499 | (((c) >= 0x0B66) && ((c) <= 0x0B6F)) || |
| 500 | (((c) >= 0x0BE7) && ((c) <= 0x0BEF)) || |
| 501 | (((c) >= 0x0C66) && ((c) <= 0x0C6F)) || |
| 502 | (((c) >= 0x0CE6) && ((c) <= 0x0CEF)) || |
| 503 | (((c) >= 0x0D66) && ((c) <= 0x0D6F)) || |
| 504 | (((c) >= 0x0E50) && ((c) <= 0x0E59)) || |
| 505 | (((c) >= 0x0ED0) && ((c) <= 0x0ED9)) || |
| 506 | (((c) >= 0x0F20) && ((c) <= 0x0F29))) /* accelerator */ )); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * xmlIsCombining: |
| 511 | * @c: an unicode character (int) |
| 512 | * |
| 513 | * Check whether the character is allowed by the production |
| 514 | * [87] CombiningChar ::= ... long list see REC ... |
| 515 | * |
| 516 | * Returns 0 if not, non-zero otherwise |
| 517 | */ |
| 518 | int |
| 519 | xmlIsCombining(int c) { |
| 520 | return( |
| 521 | (((c) >= 0x300) && ( /* accelerator */ |
| 522 | (((c) >= 0x0300) && ((c) <= 0x0345)) || |
| 523 | (((c) >= 0x0360) && ((c) <= 0x0361)) || |
| 524 | (((c) >= 0x0483) && ((c) <= 0x0486)) || |
| 525 | (((c) >= 0x0591) && ((c) <= 0x05A1)) || |
| 526 | (((c) >= 0x05A3) && ((c) <= 0x05B9)) || |
| 527 | (((c) >= 0x05BB) && ((c) <= 0x05BD)) || |
| 528 | ((c) == 0x05BF) || |
| 529 | (((c) >= 0x05C1) && ((c) <= 0x05C2)) || |
| 530 | ((c) == 0x05C4) || |
| 531 | (((c) >= 0x064B) && ((c) <= 0x0652)) || |
| 532 | ((c) == 0x0670) || |
| 533 | (((c) >= 0x06D6) && ((c) <= 0x06DC)) || |
| 534 | (((c) >= 0x06DD) && ((c) <= 0x06DF)) || |
| 535 | (((c) >= 0x06E0) && ((c) <= 0x06E4)) || |
| 536 | (((c) >= 0x06E7) && ((c) <= 0x06E8)) || |
| 537 | (((c) >= 0x06EA) && ((c) <= 0x06ED)) || |
| 538 | (((c) >= 0x0901) && ( /* accelerator */ |
| 539 | (((c) >= 0x0901) && ((c) <= 0x0903)) || |
| 540 | ((c) == 0x093C) || |
| 541 | (((c) >= 0x093E) && ((c) <= 0x094C)) || |
| 542 | ((c) == 0x094D) || |
| 543 | (((c) >= 0x0951) && ((c) <= 0x0954)) || |
| 544 | (((c) >= 0x0962) && ((c) <= 0x0963)) || |
| 545 | (((c) >= 0x0981) && ((c) <= 0x0983)) || |
| 546 | ((c) == 0x09BC) || |
| 547 | ((c) == 0x09BE) || |
| 548 | ((c) == 0x09BF) || |
| 549 | (((c) >= 0x09C0) && ((c) <= 0x09C4)) || |
| 550 | (((c) >= 0x09C7) && ((c) <= 0x09C8)) || |
| 551 | (((c) >= 0x09CB) && ((c) <= 0x09CD)) || |
| 552 | ((c) == 0x09D7) || |
| 553 | (((c) >= 0x09E2) && ((c) <= 0x09E3)) || |
| 554 | (((c) >= 0x0A02) && ( /* accelerator */ |
| 555 | ((c) == 0x0A02) || |
| 556 | ((c) == 0x0A3C) || |
| 557 | ((c) == 0x0A3E) || |
| 558 | ((c) == 0x0A3F) || |
| 559 | (((c) >= 0x0A40) && ((c) <= 0x0A42)) || |
| 560 | (((c) >= 0x0A47) && ((c) <= 0x0A48)) || |
| 561 | (((c) >= 0x0A4B) && ((c) <= 0x0A4D)) || |
| 562 | (((c) >= 0x0A70) && ((c) <= 0x0A71)) || |
| 563 | (((c) >= 0x0A81) && ((c) <= 0x0A83)) || |
| 564 | ((c) == 0x0ABC) || |
| 565 | (((c) >= 0x0ABE) && ((c) <= 0x0AC5)) || |
| 566 | (((c) >= 0x0AC7) && ((c) <= 0x0AC9)) || |
| 567 | (((c) >= 0x0ACB) && ((c) <= 0x0ACD)) || |
| 568 | (((c) >= 0x0B01) && ((c) <= 0x0B03)) || |
| 569 | ((c) == 0x0B3C) || |
| 570 | (((c) >= 0x0B3E) && ((c) <= 0x0B43)) || |
| 571 | (((c) >= 0x0B47) && ((c) <= 0x0B48)) || |
| 572 | (((c) >= 0x0B4B) && ((c) <= 0x0B4D)) || |
| 573 | (((c) >= 0x0B56) && ((c) <= 0x0B57)) || |
| 574 | (((c) >= 0x0B82) && ((c) <= 0x0B83)) || |
| 575 | (((c) >= 0x0BBE) && ((c) <= 0x0BC2)) || |
| 576 | (((c) >= 0x0BC6) && ((c) <= 0x0BC8)) || |
| 577 | (((c) >= 0x0BCA) && ((c) <= 0x0BCD)) || |
| 578 | ((c) == 0x0BD7) || |
| 579 | (((c) >= 0x0C01) && ((c) <= 0x0C03)) || |
| 580 | (((c) >= 0x0C3E) && ((c) <= 0x0C44)) || |
| 581 | (((c) >= 0x0C46) && ((c) <= 0x0C48)) || |
| 582 | (((c) >= 0x0C4A) && ((c) <= 0x0C4D)) || |
| 583 | (((c) >= 0x0C55) && ((c) <= 0x0C56)) || |
| 584 | (((c) >= 0x0C82) && ((c) <= 0x0C83)) || |
| 585 | (((c) >= 0x0CBE) && ((c) <= 0x0CC4)) || |
| 586 | (((c) >= 0x0CC6) && ((c) <= 0x0CC8)) || |
| 587 | (((c) >= 0x0CCA) && ((c) <= 0x0CCD)) || |
| 588 | (((c) >= 0x0CD5) && ((c) <= 0x0CD6)) || |
| 589 | (((c) >= 0x0D02) && ((c) <= 0x0D03)) || |
| 590 | (((c) >= 0x0D3E) && ((c) <= 0x0D43)) || |
| 591 | (((c) >= 0x0D46) && ((c) <= 0x0D48)) || |
| 592 | (((c) >= 0x0D4A) && ((c) <= 0x0D4D)) || |
| 593 | ((c) == 0x0D57) || |
| 594 | (((c) >= 0x0E31) && ( /* accelerator */ |
| 595 | ((c) == 0x0E31) || |
| 596 | (((c) >= 0x0E34) && ((c) <= 0x0E3A)) || |
| 597 | (((c) >= 0x0E47) && ((c) <= 0x0E4E)) || |
| 598 | ((c) == 0x0EB1) || |
| 599 | (((c) >= 0x0EB4) && ((c) <= 0x0EB9)) || |
| 600 | (((c) >= 0x0EBB) && ((c) <= 0x0EBC)) || |
| 601 | (((c) >= 0x0EC8) && ((c) <= 0x0ECD)) || |
| 602 | (((c) >= 0x0F18) && ((c) <= 0x0F19)) || |
| 603 | ((c) == 0x0F35) || |
| 604 | ((c) == 0x0F37) || |
| 605 | ((c) == 0x0F39) || |
| 606 | ((c) == 0x0F3E) || |
| 607 | ((c) == 0x0F3F) || |
| 608 | (((c) >= 0x0F71) && ((c) <= 0x0F84)) || |
| 609 | (((c) >= 0x0F86) && ((c) <= 0x0F8B)) || |
| 610 | (((c) >= 0x0F90) && ((c) <= 0x0F95)) || |
| 611 | ((c) == 0x0F97) || |
| 612 | (((c) >= 0x0F99) && ((c) <= 0x0FAD)) || |
| 613 | (((c) >= 0x0FB1) && ((c) <= 0x0FB7)) || |
| 614 | ((c) == 0x0FB9) || |
| 615 | (((c) >= 0x20D0) && ((c) <= 0x20DC)) || |
| 616 | ((c) == 0x20E1) || |
| 617 | (((c) >= 0x302A) && ((c) <= 0x302F)) || |
| 618 | ((c) == 0x3099) || |
| 619 | ((c) == 0x309A)))))))))); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * xmlIsExtender: |
| 624 | * @c: an unicode character (int) |
| 625 | * |
| 626 | * Check whether the character is allowed by the production |
| 627 | * [89] Extender ::= #x00B7 | #x02D0 | #x02D1 | #x0387 | #x0640 | |
| 628 | * #x0E46 | #x0EC6 | #x3005 | [#x3031-#x3035] | |
| 629 | * [#x309D-#x309E] | [#x30FC-#x30FE] |
| 630 | * |
| 631 | * Returns 0 if not, non-zero otherwise |
| 632 | */ |
| 633 | int |
| 634 | xmlIsExtender(int c) { |
| 635 | switch (c) { |
| 636 | case 0x00B7: case 0x02D0: case 0x02D1: case 0x0387: |
| 637 | case 0x0640: case 0x0E46: case 0x0EC6: case 0x3005: |
| 638 | case 0x3031: case 0x3032: case 0x3033: case 0x3034: |
| 639 | case 0x3035: case 0x309D: case 0x309E: case 0x30FC: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 640 | case 0x30FD: case 0x30FE: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 641 | return 1; |
| 642 | default: |
| 643 | return 0; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * xmlIsIdeographic: |
| 649 | * @c: an unicode character (int) |
| 650 | * |
| 651 | * Check whether the character is allowed by the production |
| 652 | * [86] Ideographic ::= [#x4E00-#x9FA5] | #x3007 | [#x3021-#x3029] |
| 653 | * |
| 654 | * Returns 0 if not, non-zero otherwise |
| 655 | */ |
| 656 | int |
| 657 | xmlIsIdeographic(int c) { |
| 658 | return(((c) < 0x0100) ? 0 : |
| 659 | (((c) >= 0x4e00) && ((c) <= 0x9fa5)) || |
| 660 | (((c) >= 0xf900) && ((c) <= 0xfa2d)) || |
| 661 | (((c) >= 0x3021) && ((c) <= 0x3029)) || |
| 662 | ((c) == 0x3007)); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * xmlIsLetter: |
| 667 | * @c: an unicode character (int) |
| 668 | * |
| 669 | * Check whether the character is allowed by the production |
| 670 | * [84] Letter ::= BaseChar | Ideographic |
| 671 | * |
| 672 | * Returns 0 if not, non-zero otherwise |
| 673 | */ |
| 674 | int |
| 675 | xmlIsLetter(int c) { |
| 676 | return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c)); |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * xmlIsPubidChar: |
| 681 | * @c: an unicode character (int) |
| 682 | * |
| 683 | * Check whether the character is allowed by the production |
| 684 | * [13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%] |
| 685 | * |
| 686 | * Returns 0 if not, non-zero otherwise |
| 687 | */ |
| 688 | int |
| 689 | xmlIsPubidChar(int c) { |
| 690 | return( |
| 691 | ((c) == 0x20) || ((c) == 0x0D) || ((c) == 0x0A) || |
| 692 | (((c) >= 'a') && ((c) <= 'z')) || |
| 693 | (((c) >= 'A') && ((c) <= 'Z')) || |
| 694 | (((c) >= '0') && ((c) <= '9')) || |
| 695 | ((c) == '-') || ((c) == '\'') || ((c) == '(') || ((c) == ')') || |
| 696 | ((c) == '+') || ((c) == ',') || ((c) == '.') || ((c) == '/') || |
| 697 | ((c) == ':') || ((c) == '=') || ((c) == '?') || ((c) == ';') || |
| 698 | ((c) == '!') || ((c) == '*') || ((c) == '#') || ((c) == '@') || |
| 699 | ((c) == '$') || ((c) == '_') || ((c) == '%')); |
| 700 | } |
| 701 | |
| 702 | /************************************************************************ |
| 703 | * * |
| 704 | * Input handling functions for progressive parsing * |
| 705 | * * |
| 706 | ************************************************************************/ |
| 707 | |
| 708 | /* #define DEBUG_INPUT */ |
| 709 | /* #define DEBUG_STACK */ |
| 710 | /* #define DEBUG_PUSH */ |
| 711 | |
| 712 | |
| 713 | /* we need to keep enough input to show errors in context */ |
| 714 | #define LINE_LEN 80 |
| 715 | |
| 716 | #ifdef DEBUG_INPUT |
| 717 | #define CHECK_BUFFER(in) check_buffer(in) |
| 718 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 719 | static |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 720 | void check_buffer(xmlParserInputPtr in) { |
| 721 | if (in->base != in->buf->buffer->content) { |
| 722 | xmlGenericError(xmlGenericErrorContext, |
| 723 | "xmlParserInput: base mismatch problem\n"); |
| 724 | } |
| 725 | if (in->cur < in->base) { |
| 726 | xmlGenericError(xmlGenericErrorContext, |
| 727 | "xmlParserInput: cur < base problem\n"); |
| 728 | } |
| 729 | if (in->cur > in->base + in->buf->buffer->use) { |
| 730 | xmlGenericError(xmlGenericErrorContext, |
| 731 | "xmlParserInput: cur > base + use problem\n"); |
| 732 | } |
| 733 | xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n", |
| 734 | (int) in, (int) in->buf->buffer->content, in->cur - in->base, |
| 735 | in->buf->buffer->use, in->buf->buffer->size); |
| 736 | } |
| 737 | |
| 738 | #else |
| 739 | #define CHECK_BUFFER(in) |
| 740 | #endif |
| 741 | |
| 742 | |
| 743 | /** |
| 744 | * xmlParserInputRead: |
| 745 | * @in: an XML parser input |
| 746 | * @len: an indicative size for the lookahead |
| 747 | * |
| 748 | * This function refresh the input for the parser. It doesn't try to |
| 749 | * preserve pointers to the input buffer, and discard already read data |
| 750 | * |
| 751 | * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the |
| 752 | * end of this entity |
| 753 | */ |
| 754 | int |
| 755 | xmlParserInputRead(xmlParserInputPtr in, int len) { |
| 756 | int ret; |
| 757 | int used; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 758 | int indx; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 759 | |
| 760 | #ifdef DEBUG_INPUT |
| 761 | xmlGenericError(xmlGenericErrorContext, "Read\n"); |
| 762 | #endif |
| 763 | if (in->buf == NULL) return(-1); |
| 764 | if (in->base == NULL) return(-1); |
| 765 | if (in->cur == NULL) return(-1); |
| 766 | if (in->buf->buffer == NULL) return(-1); |
| 767 | if (in->buf->readcallback == NULL) return(-1); |
| 768 | |
| 769 | CHECK_BUFFER(in); |
| 770 | |
| 771 | used = in->cur - in->buf->buffer->content; |
| 772 | ret = xmlBufferShrink(in->buf->buffer, used); |
| 773 | if (ret > 0) { |
| 774 | in->cur -= ret; |
| 775 | in->consumed += ret; |
| 776 | } |
| 777 | ret = xmlParserInputBufferRead(in->buf, len); |
| 778 | if (in->base != in->buf->buffer->content) { |
| 779 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 780 | * the buffer has been reallocated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 781 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 782 | indx = in->cur - in->base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 783 | in->base = in->buf->buffer->content; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 784 | in->cur = &in->buf->buffer->content[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 785 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 786 | in->end = &in->buf->buffer->content[in->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 787 | |
| 788 | CHECK_BUFFER(in); |
| 789 | |
| 790 | return(ret); |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * xmlParserInputGrow: |
| 795 | * @in: an XML parser input |
| 796 | * @len: an indicative size for the lookahead |
| 797 | * |
| 798 | * This function increase the input for the parser. It tries to |
| 799 | * preserve pointers to the input buffer, and keep already read data |
| 800 | * |
| 801 | * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the |
| 802 | * end of this entity |
| 803 | */ |
| 804 | int |
| 805 | xmlParserInputGrow(xmlParserInputPtr in, int len) { |
| 806 | int ret; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 807 | int indx; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 808 | |
| 809 | #ifdef DEBUG_INPUT |
| 810 | xmlGenericError(xmlGenericErrorContext, "Grow\n"); |
| 811 | #endif |
| 812 | if (in->buf == NULL) return(-1); |
| 813 | if (in->base == NULL) return(-1); |
| 814 | if (in->cur == NULL) return(-1); |
| 815 | if (in->buf->buffer == NULL) return(-1); |
| 816 | |
| 817 | CHECK_BUFFER(in); |
| 818 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 819 | indx = in->cur - in->base; |
| 820 | if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 821 | |
| 822 | CHECK_BUFFER(in); |
| 823 | |
| 824 | return(0); |
| 825 | } |
| 826 | if (in->buf->readcallback != NULL) |
| 827 | ret = xmlParserInputBufferGrow(in->buf, len); |
| 828 | else |
| 829 | return(0); |
| 830 | |
| 831 | /* |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 832 | * 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] | 833 | * block, but we use it really as an integer to do some |
| 834 | * pointer arithmetic. Insure will raise it as a bug but in |
| 835 | * that specific case, that's not ! |
| 836 | */ |
| 837 | if (in->base != in->buf->buffer->content) { |
| 838 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 839 | * the buffer has been reallocated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 840 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 841 | indx = in->cur - in->base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 842 | in->base = in->buf->buffer->content; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 843 | in->cur = &in->buf->buffer->content[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 844 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 845 | in->end = &in->buf->buffer->content[in->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 846 | |
| 847 | CHECK_BUFFER(in); |
| 848 | |
| 849 | return(ret); |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * xmlParserInputShrink: |
| 854 | * @in: an XML parser input |
| 855 | * |
| 856 | * This function removes used input for the parser. |
| 857 | */ |
| 858 | void |
| 859 | xmlParserInputShrink(xmlParserInputPtr in) { |
| 860 | int used; |
| 861 | int ret; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 862 | int indx; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 863 | |
| 864 | #ifdef DEBUG_INPUT |
| 865 | xmlGenericError(xmlGenericErrorContext, "Shrink\n"); |
| 866 | #endif |
| 867 | if (in->buf == NULL) return; |
| 868 | if (in->base == NULL) return; |
| 869 | if (in->cur == NULL) return; |
| 870 | if (in->buf->buffer == NULL) return; |
| 871 | |
| 872 | CHECK_BUFFER(in); |
| 873 | |
| 874 | used = in->cur - in->buf->buffer->content; |
| 875 | /* |
| 876 | * Do not shrink on large buffers whose only a tiny fraction |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 877 | * was consumed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 878 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 879 | if (used > INPUT_CHUNK) { |
| 880 | ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN); |
| 881 | if (ret > 0) { |
| 882 | in->cur -= ret; |
| 883 | in->consumed += ret; |
| 884 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 885 | in->end = &in->buf->buffer->content[in->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | CHECK_BUFFER(in); |
| 889 | |
| 890 | if (in->buf->buffer->use > INPUT_CHUNK) { |
| 891 | return; |
| 892 | } |
| 893 | xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK); |
| 894 | if (in->base != in->buf->buffer->content) { |
| 895 | /* |
Daniel Veillard | 5e5c2d0 | 2002-02-09 18:03:01 +0000 | [diff] [blame] | 896 | * the buffer has been reallocated |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 897 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 898 | indx = in->cur - in->base; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 899 | in->base = in->buf->buffer->content; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 900 | in->cur = &in->buf->buffer->content[indx]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 901 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 902 | in->end = &in->buf->buffer->content[in->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 903 | |
| 904 | CHECK_BUFFER(in); |
| 905 | } |
| 906 | |
| 907 | /************************************************************************ |
| 908 | * * |
| 909 | * UTF8 character input and related functions * |
| 910 | * * |
| 911 | ************************************************************************/ |
| 912 | |
| 913 | /** |
| 914 | * xmlNextChar: |
| 915 | * @ctxt: the XML parser context |
| 916 | * |
| 917 | * Skip to the next char input char. |
| 918 | */ |
| 919 | |
| 920 | void |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 921 | xmlNextChar(xmlParserCtxtPtr ctxt) |
| 922 | { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 923 | if (ctxt->instate == XML_PARSER_EOF) |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 924 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 925 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 926 | if (ctxt->charset == XML_CHAR_ENCODING_UTF8) { |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 927 | if ((*ctxt->input->cur == 0) && |
| 928 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) && |
| 929 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 930 | /* |
| 931 | * If we are at the end of the current entity and |
| 932 | * the context allows it, we pop consumed entities |
| 933 | * automatically. |
| 934 | * the auto closing should be blocked in other cases |
| 935 | */ |
| 936 | xmlPopInput(ctxt); |
| 937 | } else { |
| 938 | const unsigned char *cur; |
| 939 | unsigned char c; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 940 | |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 941 | /* |
| 942 | * 2.11 End-of-Line Handling |
| 943 | * the literal two-character sequence "#xD#xA" or a standalone |
| 944 | * literal #xD, an XML processor must pass to the application |
| 945 | * the single character #xA. |
| 946 | */ |
| 947 | if (*(ctxt->input->cur) == '\n') { |
| 948 | ctxt->input->line++; |
| 949 | ctxt->input->col = 1; |
| 950 | } else |
| 951 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 952 | |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 953 | /* |
| 954 | * We are supposed to handle UTF8, check it's valid |
| 955 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 956 | * |
| 957 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 958 | * 0000 0000-0000 007F 0xxxxxxx |
| 959 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 960 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 961 | * |
| 962 | * Check for the 0x110000 limit too |
| 963 | */ |
| 964 | cur = ctxt->input->cur; |
| 965 | |
| 966 | c = *cur; |
| 967 | if (c & 0x80) { |
Daniel Veillard | 0e0f37a | 2003-05-20 12:22:41 +0000 | [diff] [blame] | 968 | if (c == 0xC0) |
| 969 | goto encoding_error; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 970 | if (cur[1] == 0) |
| 971 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 972 | if ((cur[1] & 0xc0) != 0x80) |
| 973 | goto encoding_error; |
| 974 | if ((c & 0xe0) == 0xe0) { |
| 975 | unsigned int val; |
| 976 | |
| 977 | if (cur[2] == 0) |
| 978 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 979 | if ((cur[2] & 0xc0) != 0x80) |
| 980 | goto encoding_error; |
| 981 | if ((c & 0xf0) == 0xf0) { |
| 982 | if (cur[3] == 0) |
| 983 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 984 | if (((c & 0xf8) != 0xf0) || |
| 985 | ((cur[3] & 0xc0) != 0x80)) |
| 986 | goto encoding_error; |
| 987 | /* 4-byte code */ |
| 988 | ctxt->input->cur += 4; |
| 989 | val = (cur[0] & 0x7) << 18; |
| 990 | val |= (cur[1] & 0x3f) << 12; |
| 991 | val |= (cur[2] & 0x3f) << 6; |
| 992 | val |= cur[3] & 0x3f; |
| 993 | } else { |
| 994 | /* 3-byte code */ |
| 995 | ctxt->input->cur += 3; |
| 996 | val = (cur[0] & 0xf) << 12; |
| 997 | val |= (cur[1] & 0x3f) << 6; |
| 998 | val |= cur[2] & 0x3f; |
| 999 | } |
| 1000 | if (((val > 0xd7ff) && (val < 0xe000)) || |
| 1001 | ((val > 0xfffd) && (val < 0x10000)) || |
| 1002 | (val >= 0x110000)) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1003 | xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1004 | "Char 0x%X out of allowed range\n", |
| 1005 | val); |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1006 | } |
| 1007 | } else |
| 1008 | /* 2-byte code */ |
| 1009 | ctxt->input->cur += 2; |
| 1010 | } else |
| 1011 | /* 1-byte code */ |
| 1012 | ctxt->input->cur++; |
| 1013 | |
| 1014 | ctxt->nbChars++; |
| 1015 | if (*ctxt->input->cur == 0) |
| 1016 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1017 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1018 | } else { |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1019 | /* |
| 1020 | * Assume it's a fixed length encoding (1) with |
| 1021 | * a compatible encoding for the ASCII set, since |
| 1022 | * XML constructs only use < 128 chars |
| 1023 | */ |
| 1024 | |
| 1025 | if (*(ctxt->input->cur) == '\n') { |
| 1026 | ctxt->input->line++; |
| 1027 | ctxt->input->col = 1; |
| 1028 | } else |
| 1029 | ctxt->input->col++; |
| 1030 | ctxt->input->cur++; |
| 1031 | ctxt->nbChars++; |
| 1032 | if (*ctxt->input->cur == 0) |
| 1033 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1034 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1035 | if ((*ctxt->input->cur == '%') && (!ctxt->html)) |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1036 | xmlParserHandlePEReference(ctxt); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1037 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1038 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1039 | xmlPopInput(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1040 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1041 | encoding_error: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1042 | /* |
| 1043 | * If we detect an UTF8 error that probably mean that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1044 | * input encoding didn't get properly advertised in the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1045 | * declaration header. Report the error and switch the encoding |
| 1046 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 1047 | * encoding !) |
| 1048 | */ |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1049 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 1050 | "Input is not proper UTF-8, indicate encoding !\n", |
| 1051 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1052 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1053 | ctxt->sax->error(ctxt->userData, |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1054 | "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 1055 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 1056 | ctxt->input->cur[2], ctxt->input->cur[3]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1057 | } |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1058 | ctxt->charset = XML_CHAR_ENCODING_8859_1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1059 | ctxt->input->cur++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1060 | return; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * xmlCurrentChar: |
| 1065 | * @ctxt: the XML parser context |
| 1066 | * @len: pointer to the length of the char read |
| 1067 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1068 | * 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] | 1069 | * bytes in the input buffer. Implement the end of line normalization: |
| 1070 | * 2.11 End-of-Line Handling |
| 1071 | * Wherever an external parsed entity or the literal entity value |
| 1072 | * of an internal parsed entity contains either the literal two-character |
| 1073 | * sequence "#xD#xA" or a standalone literal #xD, an XML processor |
| 1074 | * must pass to the application the single character #xA. |
| 1075 | * This behavior can conveniently be produced by normalizing all |
| 1076 | * line breaks to #xA on input, before parsing.) |
| 1077 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1078 | * Returns the current char value and its length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1079 | */ |
| 1080 | |
| 1081 | int |
| 1082 | xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) { |
| 1083 | if (ctxt->instate == XML_PARSER_EOF) |
| 1084 | return(0); |
| 1085 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1086 | if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) { |
| 1087 | *len = 1; |
| 1088 | return((int) *ctxt->input->cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1089 | } |
| 1090 | if (ctxt->charset == XML_CHAR_ENCODING_UTF8) { |
| 1091 | /* |
| 1092 | * We are supposed to handle UTF8, check it's valid |
| 1093 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 1094 | * |
| 1095 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 1096 | * 0000 0000-0000 007F 0xxxxxxx |
| 1097 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 1098 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 1099 | * |
| 1100 | * Check for the 0x110000 limit too |
| 1101 | */ |
| 1102 | const unsigned char *cur = ctxt->input->cur; |
| 1103 | unsigned char c; |
| 1104 | unsigned int val; |
| 1105 | |
| 1106 | c = *cur; |
| 1107 | if (c & 0x80) { |
Daniel Veillard | 0e0f37a | 2003-05-20 12:22:41 +0000 | [diff] [blame] | 1108 | if (c == 0xC0) |
| 1109 | goto encoding_error; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1110 | if (cur[1] == 0) |
| 1111 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1112 | if ((cur[1] & 0xc0) != 0x80) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1113 | goto encoding_error; |
| 1114 | if ((c & 0xe0) == 0xe0) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1115 | |
| 1116 | if (cur[2] == 0) |
| 1117 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1118 | if ((cur[2] & 0xc0) != 0x80) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1119 | goto encoding_error; |
| 1120 | if ((c & 0xf0) == 0xf0) { |
| 1121 | if (cur[3] == 0) |
| 1122 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1123 | if (((c & 0xf8) != 0xf0) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1124 | ((cur[3] & 0xc0) != 0x80)) |
| 1125 | goto encoding_error; |
| 1126 | /* 4-byte code */ |
| 1127 | *len = 4; |
| 1128 | val = (cur[0] & 0x7) << 18; |
| 1129 | val |= (cur[1] & 0x3f) << 12; |
| 1130 | val |= (cur[2] & 0x3f) << 6; |
| 1131 | val |= cur[3] & 0x3f; |
| 1132 | } else { |
| 1133 | /* 3-byte code */ |
| 1134 | *len = 3; |
| 1135 | val = (cur[0] & 0xf) << 12; |
| 1136 | val |= (cur[1] & 0x3f) << 6; |
| 1137 | val |= cur[2] & 0x3f; |
| 1138 | } |
| 1139 | } else { |
| 1140 | /* 2-byte code */ |
| 1141 | *len = 2; |
| 1142 | val = (cur[0] & 0x1f) << 6; |
| 1143 | val |= cur[1] & 0x3f; |
| 1144 | } |
| 1145 | if (!IS_CHAR(val)) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1146 | xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1147 | "Char 0x%X out of allowed range\n", val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1148 | } |
| 1149 | return(val); |
| 1150 | } else { |
| 1151 | /* 1-byte code */ |
| 1152 | *len = 1; |
| 1153 | if (*ctxt->input->cur == 0xD) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1154 | if (ctxt->input->cur[1] == 0xA) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1155 | ctxt->nbChars++; |
| 1156 | ctxt->input->cur++; |
| 1157 | } |
| 1158 | return(0xA); |
| 1159 | } |
| 1160 | return((int) *ctxt->input->cur); |
| 1161 | } |
| 1162 | } |
| 1163 | /* |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1164 | * Assume it's a fixed length encoding (1) with |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1165 | * a compatible encoding for the ASCII set, since |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1166 | * XML constructs only use < 128 chars |
| 1167 | */ |
| 1168 | *len = 1; |
| 1169 | if (*ctxt->input->cur == 0xD) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1170 | if (ctxt->input->cur[1] == 0xA) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1171 | ctxt->nbChars++; |
| 1172 | ctxt->input->cur++; |
| 1173 | } |
| 1174 | return(0xA); |
| 1175 | } |
| 1176 | return((int) *ctxt->input->cur); |
| 1177 | encoding_error: |
| 1178 | /* |
Daniel Veillard | d2ff039 | 2002-11-22 12:28:38 +0000 | [diff] [blame] | 1179 | * An encoding problem may arise from a truncated input buffer |
| 1180 | * splitting a character in the middle. In that case do not raise |
| 1181 | * an error but return 0 to endicate an end of stream problem |
| 1182 | */ |
| 1183 | if (ctxt->input->end - ctxt->input->cur < 4) { |
| 1184 | *len = 0; |
| 1185 | return(0); |
| 1186 | } |
| 1187 | |
| 1188 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1189 | * If we detect an UTF8 error that probably mean that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1190 | * input encoding didn't get properly advertised in the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1191 | * declaration header. Report the error and switch the encoding |
| 1192 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 1193 | * encoding !) |
| 1194 | */ |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1195 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 1196 | "Input is not proper UTF-8, indicate encoding !\n", |
| 1197 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1198 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1199 | 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] | 1200 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 1201 | ctxt->input->cur[2], ctxt->input->cur[3]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1202 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1203 | ctxt->charset = XML_CHAR_ENCODING_8859_1; |
| 1204 | *len = 1; |
| 1205 | return((int) *ctxt->input->cur); |
| 1206 | } |
| 1207 | |
| 1208 | /** |
| 1209 | * xmlStringCurrentChar: |
| 1210 | * @ctxt: the XML parser context |
| 1211 | * @cur: pointer to the beginning of the char |
| 1212 | * @len: pointer to the length of the char read |
| 1213 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1214 | * 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] | 1215 | * bytes in the input buffer. |
| 1216 | * |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1217 | * Returns the current char value and its length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1218 | */ |
| 1219 | |
| 1220 | int |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1221 | xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len) |
| 1222 | { |
Daniel Veillard | 61d80a2 | 2001-04-27 17:13:01 +0000 | [diff] [blame] | 1223 | if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) { |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1224 | /* |
| 1225 | * We are supposed to handle UTF8, check it's valid |
| 1226 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 1227 | * |
| 1228 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 1229 | * 0000 0000-0000 007F 0xxxxxxx |
| 1230 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 1231 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 1232 | * |
| 1233 | * Check for the 0x110000 limit too |
| 1234 | */ |
| 1235 | unsigned char c; |
| 1236 | unsigned int val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1237 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1238 | c = *cur; |
| 1239 | if (c & 0x80) { |
| 1240 | if ((cur[1] & 0xc0) != 0x80) |
| 1241 | goto encoding_error; |
| 1242 | if ((c & 0xe0) == 0xe0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1243 | |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1244 | if ((cur[2] & 0xc0) != 0x80) |
| 1245 | goto encoding_error; |
| 1246 | if ((c & 0xf0) == 0xf0) { |
| 1247 | if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80)) |
| 1248 | goto encoding_error; |
| 1249 | /* 4-byte code */ |
| 1250 | *len = 4; |
| 1251 | val = (cur[0] & 0x7) << 18; |
| 1252 | val |= (cur[1] & 0x3f) << 12; |
| 1253 | val |= (cur[2] & 0x3f) << 6; |
| 1254 | val |= cur[3] & 0x3f; |
| 1255 | } else { |
| 1256 | /* 3-byte code */ |
| 1257 | *len = 3; |
| 1258 | val = (cur[0] & 0xf) << 12; |
| 1259 | val |= (cur[1] & 0x3f) << 6; |
| 1260 | val |= cur[2] & 0x3f; |
| 1261 | } |
| 1262 | } else { |
| 1263 | /* 2-byte code */ |
| 1264 | *len = 2; |
| 1265 | val = (cur[0] & 0x1f) << 6; |
| 1266 | val |= cur[1] & 0x3f; |
| 1267 | } |
| 1268 | if (!IS_CHAR(val)) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1269 | xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1270 | "Char 0x%X out of allowed range\n", val); |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1271 | } |
| 1272 | return (val); |
| 1273 | } else { |
| 1274 | /* 1-byte code */ |
| 1275 | *len = 1; |
| 1276 | return ((int) *cur); |
| 1277 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1278 | } |
| 1279 | /* |
Daniel Veillard | 60087f3 | 2001-10-10 09:45:09 +0000 | [diff] [blame] | 1280 | * Assume it's a fixed length encoding (1) with |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1281 | * a compatible encoding for the ASCII set, since |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1282 | * XML constructs only use < 128 chars |
| 1283 | */ |
| 1284 | *len = 1; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1285 | return ((int) *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1286 | encoding_error: |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1287 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1288 | /* |
| 1289 | * If we detect an UTF8 error that probably mean that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1290 | * input encoding didn't get properly advertised in the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1291 | * declaration header. Report the error and switch the encoding |
| 1292 | * to ISO-Latin-1 (if you don't like this policy, just declare the |
| 1293 | * encoding !) |
| 1294 | */ |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1295 | xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 1296 | "Input is not proper UTF-8, indicate encoding !\n", |
| 1297 | NULL, NULL); |
| 1298 | if ((ctxt != NULL) && (ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
| 1299 | ctxt->sax->error(ctxt->userData, |
| 1300 | "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 1301 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 1302 | ctxt->input->cur[2], ctxt->input->cur[3]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1303 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1304 | *len = 1; |
Daniel Veillard | d8224e0 | 2002-01-13 15:43:22 +0000 | [diff] [blame] | 1305 | return ((int) *cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /** |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1309 | * xmlCopyCharMultiByte: |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1310 | * @out: pointer to an array of xmlChar |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1311 | * @val: the char value |
| 1312 | * |
| 1313 | * append the char value in the array |
| 1314 | * |
| 1315 | * Returns the number of xmlChar written |
| 1316 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1317 | int |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1318 | xmlCopyCharMultiByte(xmlChar *out, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1319 | /* |
| 1320 | * We are supposed to handle UTF8, check it's valid |
| 1321 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 1322 | * |
| 1323 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 1324 | * 0000 0000-0000 007F 0xxxxxxx |
| 1325 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 1326 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 1327 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1328 | if (val >= 0x80) { |
| 1329 | xmlChar *savedout = out; |
| 1330 | int bits; |
| 1331 | if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; } |
| 1332 | else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;} |
| 1333 | else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; } |
| 1334 | else { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1335 | xmlErrEncodingInt(NULL, XML_ERR_INVALID_CHAR, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1336 | "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n", |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1337 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1338 | return(0); |
| 1339 | } |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1340 | for ( ; bits >= 0; bits-= 6) |
| 1341 | *out++= ((val >> bits) & 0x3F) | 0x80 ; |
| 1342 | return (out - savedout); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1343 | } |
| 1344 | *out = (xmlChar) val; |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1345 | return 1; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * xmlCopyChar: |
| 1350 | * @len: Ignored, compatibility |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1351 | * @out: pointer to an array of xmlChar |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1352 | * @val: the char value |
| 1353 | * |
| 1354 | * append the char value in the array |
| 1355 | * |
| 1356 | * Returns the number of xmlChar written |
| 1357 | */ |
| 1358 | |
| 1359 | int |
Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 1360 | xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) { |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1361 | /* the len parameter is ignored */ |
| 1362 | if (val >= 0x80) { |
| 1363 | return(xmlCopyCharMultiByte (out, val)); |
| 1364 | } |
| 1365 | *out = (xmlChar) val; |
| 1366 | return 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /************************************************************************ |
| 1370 | * * |
| 1371 | * Commodity functions to switch encodings * |
| 1372 | * * |
| 1373 | ************************************************************************/ |
| 1374 | |
| 1375 | /** |
| 1376 | * xmlSwitchEncoding: |
| 1377 | * @ctxt: the parser context |
| 1378 | * @enc: the encoding value (number) |
| 1379 | * |
| 1380 | * change the input functions when discovering the character encoding |
| 1381 | * of a given entity. |
| 1382 | * |
| 1383 | * Returns 0 in case of success, -1 otherwise |
| 1384 | */ |
| 1385 | int |
| 1386 | xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc) |
| 1387 | { |
| 1388 | xmlCharEncodingHandlerPtr handler; |
| 1389 | |
| 1390 | switch (enc) { |
| 1391 | case XML_CHAR_ENCODING_ERROR: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1392 | xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING, |
| 1393 | "encoding unknown\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1394 | break; |
| 1395 | case XML_CHAR_ENCODING_NONE: |
| 1396 | /* let's assume it's UTF-8 without the XML decl */ |
| 1397 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1398 | return(0); |
| 1399 | case XML_CHAR_ENCODING_UTF8: |
| 1400 | /* default encoding, no conversion should be needed */ |
| 1401 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1402 | |
| 1403 | /* |
| 1404 | * Errata on XML-1.0 June 20 2001 |
| 1405 | * Specific handling of the Byte Order Mark for |
| 1406 | * UTF-8 |
| 1407 | */ |
Daniel Veillard | 3e5bb8e | 2001-06-27 16:34:34 +0000 | [diff] [blame] | 1408 | if ((ctxt->input != NULL) && |
| 1409 | (ctxt->input->cur[0] == 0xEF) && |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1410 | (ctxt->input->cur[1] == 0xBB) && |
| 1411 | (ctxt->input->cur[2] == 0xBF)) { |
| 1412 | ctxt->input->cur += 3; |
| 1413 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1414 | return(0); |
Daniel Veillard | 2dcb937 | 2003-07-16 21:18:19 +0000 | [diff] [blame] | 1415 | case XML_CHAR_ENCODING_UTF16LE: |
| 1416 | case XML_CHAR_ENCODING_UTF16BE: |
| 1417 | /*The raw input characters are encoded |
| 1418 | *in UTF-16. As we expect this function |
| 1419 | *to be called after xmlCharEncInFunc, we expect |
| 1420 | *ctxt->input->cur to contain UTF-8 encoded characters. |
| 1421 | *So the raw UTF16 Byte Order Mark |
| 1422 | *has also been converted into |
| 1423 | *an UTF-8 BOM. Let's skip that BOM. |
| 1424 | */ |
| 1425 | if ((ctxt->input != NULL) && |
| 1426 | (ctxt->input->cur[0] == 0xEF) && |
| 1427 | (ctxt->input->cur[1] == 0xBB) && |
| 1428 | (ctxt->input->cur[2] == 0xBF)) { |
| 1429 | ctxt->input->cur += 3; |
| 1430 | } |
| 1431 | break ; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1432 | default: |
| 1433 | break; |
| 1434 | } |
| 1435 | handler = xmlGetCharEncodingHandler(enc); |
| 1436 | if (handler == NULL) { |
| 1437 | /* |
| 1438 | * Default handlers. |
| 1439 | */ |
| 1440 | switch (enc) { |
| 1441 | case XML_CHAR_ENCODING_ERROR: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1442 | xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING, |
| 1443 | "encoding unknown\n", NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | case XML_CHAR_ENCODING_NONE: |
| 1446 | /* let's assume it's UTF-8 without the XML decl */ |
| 1447 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1448 | return(0); |
| 1449 | case XML_CHAR_ENCODING_UTF8: |
| 1450 | case XML_CHAR_ENCODING_ASCII: |
| 1451 | /* default encoding, no conversion should be needed */ |
| 1452 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1453 | return(0); |
| 1454 | case XML_CHAR_ENCODING_UTF16LE: |
| 1455 | break; |
| 1456 | case XML_CHAR_ENCODING_UTF16BE: |
| 1457 | break; |
| 1458 | case XML_CHAR_ENCODING_UCS4LE: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1459 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1460 | "encoding not supported %s\n", |
| 1461 | BAD_CAST "USC4 little endian", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1462 | break; |
| 1463 | case XML_CHAR_ENCODING_UCS4BE: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1464 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1465 | "encoding not supported %s\n", |
| 1466 | BAD_CAST "USC4 big endian", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1467 | break; |
| 1468 | case XML_CHAR_ENCODING_EBCDIC: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1469 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1470 | "encoding not supported %s\n", |
| 1471 | BAD_CAST "EBCDIC", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1472 | break; |
| 1473 | case XML_CHAR_ENCODING_UCS4_2143: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1474 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1475 | "encoding not supported %s\n", |
| 1476 | BAD_CAST "UCS4 2143", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1477 | break; |
| 1478 | case XML_CHAR_ENCODING_UCS4_3412: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1479 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1480 | "encoding not supported %s\n", |
| 1481 | BAD_CAST "UCS4 3412", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1482 | break; |
| 1483 | case XML_CHAR_ENCODING_UCS2: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1484 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1485 | "encoding not supported %s\n", |
| 1486 | BAD_CAST "UCS2", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | case XML_CHAR_ENCODING_8859_1: |
| 1489 | case XML_CHAR_ENCODING_8859_2: |
| 1490 | case XML_CHAR_ENCODING_8859_3: |
| 1491 | case XML_CHAR_ENCODING_8859_4: |
| 1492 | case XML_CHAR_ENCODING_8859_5: |
| 1493 | case XML_CHAR_ENCODING_8859_6: |
| 1494 | case XML_CHAR_ENCODING_8859_7: |
| 1495 | case XML_CHAR_ENCODING_8859_8: |
| 1496 | case XML_CHAR_ENCODING_8859_9: |
| 1497 | /* |
| 1498 | * We used to keep the internal content in the |
| 1499 | * document encoding however this turns being unmaintainable |
| 1500 | * So xmlGetCharEncodingHandler() will return non-null |
| 1501 | * values for this now. |
| 1502 | */ |
| 1503 | if ((ctxt->inputNr == 1) && |
| 1504 | (ctxt->encoding == NULL) && |
| 1505 | (ctxt->input->encoding != NULL)) { |
| 1506 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 1507 | } |
| 1508 | ctxt->charset = enc; |
| 1509 | return(0); |
| 1510 | case XML_CHAR_ENCODING_2022_JP: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1511 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1512 | "encoding not supported %s\n", |
| 1513 | BAD_CAST "ISO-2022-JP", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | case XML_CHAR_ENCODING_SHIFT_JIS: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1516 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1517 | "encoding not supported %s\n", |
| 1518 | BAD_CAST "Shift_JIS", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1519 | break; |
| 1520 | case XML_CHAR_ENCODING_EUC_JP: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1521 | xmlErrEncoding(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 1522 | "encoding not supported %s\n", |
| 1523 | BAD_CAST "EUC-JP", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | } |
| 1526 | } |
| 1527 | if (handler == NULL) |
| 1528 | return(-1); |
| 1529 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1530 | return(xmlSwitchToEncoding(ctxt, handler)); |
| 1531 | } |
| 1532 | |
| 1533 | /** |
| 1534 | * xmlSwitchToEncoding: |
| 1535 | * @ctxt: the parser context |
| 1536 | * @handler: the encoding handler |
| 1537 | * |
| 1538 | * change the input functions when discovering the character encoding |
| 1539 | * of a given entity. |
| 1540 | * |
| 1541 | * Returns 0 in case of success, -1 otherwise |
| 1542 | */ |
| 1543 | int |
| 1544 | xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler) |
| 1545 | { |
| 1546 | int nbchars; |
| 1547 | |
| 1548 | if (handler != NULL) { |
| 1549 | if (ctxt->input != NULL) { |
| 1550 | if (ctxt->input->buf != NULL) { |
| 1551 | if (ctxt->input->buf->encoder != NULL) { |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1552 | /* |
| 1553 | * Check in case the auto encoding detetection triggered |
| 1554 | * in already. |
| 1555 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1556 | if (ctxt->input->buf->encoder == handler) |
| 1557 | return(0); |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * "UTF-16" can be used for both LE and BE |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1561 | if ((!xmlStrncmp(BAD_CAST ctxt->input->buf->encoder->name, |
| 1562 | BAD_CAST "UTF-16", 6)) && |
| 1563 | (!xmlStrncmp(BAD_CAST handler->name, |
| 1564 | BAD_CAST "UTF-16", 6))) { |
| 1565 | return(0); |
| 1566 | } |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 1567 | */ |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 1568 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | /* |
| 1570 | * Note: this is a bit dangerous, but that's what it |
| 1571 | * takes to use nearly compatible signature for different |
| 1572 | * encodings. |
| 1573 | */ |
| 1574 | xmlCharEncCloseFunc(ctxt->input->buf->encoder); |
| 1575 | ctxt->input->buf->encoder = handler; |
| 1576 | return(0); |
| 1577 | } |
| 1578 | ctxt->input->buf->encoder = handler; |
| 1579 | |
| 1580 | /* |
| 1581 | * Is there already some content down the pipe to convert ? |
| 1582 | */ |
| 1583 | if ((ctxt->input->buf->buffer != NULL) && |
| 1584 | (ctxt->input->buf->buffer->use > 0)) { |
| 1585 | int processed; |
| 1586 | |
| 1587 | /* |
| 1588 | * Specific handling of the Byte Order Mark for |
| 1589 | * UTF-16 |
| 1590 | */ |
| 1591 | if ((handler->name != NULL) && |
| 1592 | (!strcmp(handler->name, "UTF-16LE")) && |
| 1593 | (ctxt->input->cur[0] == 0xFF) && |
| 1594 | (ctxt->input->cur[1] == 0xFE)) { |
| 1595 | ctxt->input->cur += 2; |
| 1596 | } |
| 1597 | if ((handler->name != NULL) && |
| 1598 | (!strcmp(handler->name, "UTF-16BE")) && |
| 1599 | (ctxt->input->cur[0] == 0xFE) && |
| 1600 | (ctxt->input->cur[1] == 0xFF)) { |
| 1601 | ctxt->input->cur += 2; |
| 1602 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1603 | /* |
| 1604 | * Errata on XML-1.0 June 20 2001 |
| 1605 | * Specific handling of the Byte Order Mark for |
| 1606 | * UTF-8 |
| 1607 | */ |
| 1608 | if ((handler->name != NULL) && |
| 1609 | (!strcmp(handler->name, "UTF-8")) && |
| 1610 | (ctxt->input->cur[0] == 0xEF) && |
| 1611 | (ctxt->input->cur[1] == 0xBB) && |
Daniel Veillard | 7dd0570 | 2001-10-04 14:25:12 +0000 | [diff] [blame] | 1612 | (ctxt->input->cur[2] == 0xBF)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1613 | ctxt->input->cur += 3; |
| 1614 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1615 | |
| 1616 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1617 | * Shrink the current input buffer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1618 | * Move it as the raw buffer and create a new input buffer |
| 1619 | */ |
| 1620 | processed = ctxt->input->cur - ctxt->input->base; |
| 1621 | xmlBufferShrink(ctxt->input->buf->buffer, processed); |
| 1622 | ctxt->input->buf->raw = ctxt->input->buf->buffer; |
| 1623 | ctxt->input->buf->buffer = xmlBufferCreate(); |
| 1624 | |
| 1625 | if (ctxt->html) { |
| 1626 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1627 | * convert as much as possible of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1628 | */ |
| 1629 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 1630 | ctxt->input->buf->buffer, |
| 1631 | ctxt->input->buf->raw); |
| 1632 | } else { |
| 1633 | /* |
| 1634 | * convert just enough to get |
| 1635 | * '<?xml version="1.0" encoding="xxx"?>' |
| 1636 | * parsed with the autodetected encoding |
| 1637 | * into the parser reading buffer. |
| 1638 | */ |
| 1639 | nbchars = xmlCharEncFirstLine(ctxt->input->buf->encoder, |
| 1640 | ctxt->input->buf->buffer, |
| 1641 | ctxt->input->buf->raw); |
| 1642 | } |
| 1643 | if (nbchars < 0) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1644 | xmlErrInternal(ctxt, |
| 1645 | "xmlSwitchToEncoding: encoder error\n", |
| 1646 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1647 | return(-1); |
| 1648 | } |
| 1649 | ctxt->input->base = |
| 1650 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1651 | ctxt->input->end = |
| 1652 | &ctxt->input->base[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1653 | |
| 1654 | } |
| 1655 | return(0); |
| 1656 | } else { |
| 1657 | if ((ctxt->input->length == 0) || (ctxt->input->buf == NULL)) { |
| 1658 | /* |
| 1659 | * When parsing a static memory array one must know the |
| 1660 | * size to be able to convert the buffer. |
| 1661 | */ |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1662 | xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n", |
| 1663 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1664 | return(-1); |
| 1665 | } else { |
| 1666 | int processed; |
| 1667 | |
| 1668 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1669 | * Shrink the current input buffer. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1670 | * Move it as the raw buffer and create a new input buffer |
| 1671 | */ |
| 1672 | processed = ctxt->input->cur - ctxt->input->base; |
| 1673 | |
| 1674 | ctxt->input->buf->raw = xmlBufferCreate(); |
| 1675 | xmlBufferAdd(ctxt->input->buf->raw, ctxt->input->cur, |
| 1676 | ctxt->input->length - processed); |
| 1677 | ctxt->input->buf->buffer = xmlBufferCreate(); |
| 1678 | |
| 1679 | /* |
| 1680 | * convert as much as possible of the raw input |
| 1681 | * to the parser reading buffer. |
| 1682 | */ |
| 1683 | nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder, |
| 1684 | ctxt->input->buf->buffer, |
| 1685 | ctxt->input->buf->raw); |
| 1686 | if (nbchars < 0) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1687 | xmlErrInternal(ctxt, |
| 1688 | "xmlSwitchToEncoding: encoder error\n", |
| 1689 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1690 | return(-1); |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * Conversion succeeded, get rid of the old buffer |
| 1695 | */ |
| 1696 | if ((ctxt->input->free != NULL) && |
| 1697 | (ctxt->input->base != NULL)) |
| 1698 | ctxt->input->free((xmlChar *) ctxt->input->base); |
| 1699 | ctxt->input->base = |
| 1700 | ctxt->input->cur = ctxt->input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1701 | ctxt->input->end = |
| 1702 | &ctxt->input->base[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | } else { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1706 | xmlErrInternal(ctxt, "xmlSwitchToEncoding : no input\n", |
| 1707 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1708 | return(-1); |
| 1709 | } |
| 1710 | /* |
| 1711 | * The parsing is now done in UTF8 natively |
| 1712 | */ |
| 1713 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 1714 | } else |
| 1715 | return(-1); |
| 1716 | return(0); |
| 1717 | |
| 1718 | } |
| 1719 | |
| 1720 | /************************************************************************ |
| 1721 | * * |
| 1722 | * Commodity functions to handle entities processing * |
| 1723 | * * |
| 1724 | ************************************************************************/ |
| 1725 | |
| 1726 | /** |
| 1727 | * xmlFreeInputStream: |
| 1728 | * @input: an xmlParserInputPtr |
| 1729 | * |
| 1730 | * Free up an input stream. |
| 1731 | */ |
| 1732 | void |
| 1733 | xmlFreeInputStream(xmlParserInputPtr input) { |
| 1734 | if (input == NULL) return; |
| 1735 | |
| 1736 | if (input->filename != NULL) xmlFree((char *) input->filename); |
| 1737 | if (input->directory != NULL) xmlFree((char *) input->directory); |
| 1738 | if (input->encoding != NULL) xmlFree((char *) input->encoding); |
| 1739 | if (input->version != NULL) xmlFree((char *) input->version); |
| 1740 | if ((input->free != NULL) && (input->base != NULL)) |
| 1741 | input->free((xmlChar *) input->base); |
| 1742 | if (input->buf != NULL) |
| 1743 | xmlFreeParserInputBuffer(input->buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1744 | xmlFree(input); |
| 1745 | } |
| 1746 | |
| 1747 | /** |
| 1748 | * xmlNewInputStream: |
| 1749 | * @ctxt: an XML parser context |
| 1750 | * |
| 1751 | * Create a new input stream structure |
| 1752 | * Returns the new input stream or NULL |
| 1753 | */ |
| 1754 | xmlParserInputPtr |
| 1755 | xmlNewInputStream(xmlParserCtxtPtr ctxt) { |
| 1756 | xmlParserInputPtr input; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1757 | static int id = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1758 | |
| 1759 | input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput)); |
| 1760 | if (input == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1761 | xmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1762 | return(NULL); |
| 1763 | } |
| 1764 | memset(input, 0, sizeof(xmlParserInput)); |
| 1765 | input->line = 1; |
| 1766 | input->col = 1; |
| 1767 | input->standalone = -1; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1768 | /* |
| 1769 | * we don't care about thread reentrancy unicity for a single |
| 1770 | * parser context (and hence thread) is sufficient. |
| 1771 | */ |
| 1772 | input->id = id++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | return(input); |
| 1774 | } |
| 1775 | |
| 1776 | /** |
| 1777 | * xmlNewIOInputStream: |
| 1778 | * @ctxt: an XML parser context |
| 1779 | * @input: an I/O Input |
| 1780 | * @enc: the charset encoding if known |
| 1781 | * |
| 1782 | * Create a new input stream structure encapsulating the @input into |
| 1783 | * a stream suitable for the parser. |
| 1784 | * |
| 1785 | * Returns the new input stream or NULL |
| 1786 | */ |
| 1787 | xmlParserInputPtr |
| 1788 | xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input, |
| 1789 | xmlCharEncoding enc) { |
| 1790 | xmlParserInputPtr inputStream; |
| 1791 | |
| 1792 | if (xmlParserDebugEntities) |
| 1793 | xmlGenericError(xmlGenericErrorContext, "new input from I/O\n"); |
| 1794 | inputStream = xmlNewInputStream(ctxt); |
| 1795 | if (inputStream == NULL) { |
| 1796 | return(NULL); |
| 1797 | } |
| 1798 | inputStream->filename = NULL; |
| 1799 | inputStream->buf = input; |
| 1800 | inputStream->base = inputStream->buf->buffer->content; |
| 1801 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1802 | inputStream->end = &inputStream->base[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1803 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 1804 | xmlSwitchEncoding(ctxt, enc); |
| 1805 | } |
| 1806 | |
| 1807 | return(inputStream); |
| 1808 | } |
| 1809 | |
| 1810 | /** |
| 1811 | * xmlNewEntityInputStream: |
| 1812 | * @ctxt: an XML parser context |
| 1813 | * @entity: an Entity pointer |
| 1814 | * |
| 1815 | * Create a new input stream based on an xmlEntityPtr |
| 1816 | * |
| 1817 | * Returns the new input stream or NULL |
| 1818 | */ |
| 1819 | xmlParserInputPtr |
| 1820 | xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 1821 | xmlParserInputPtr input; |
| 1822 | |
| 1823 | if (entity == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1824 | xmlErrInternal(ctxt, "xmlNewEntityInputStream entity = NULL\n", |
| 1825 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1826 | return(NULL); |
| 1827 | } |
| 1828 | if (xmlParserDebugEntities) |
| 1829 | xmlGenericError(xmlGenericErrorContext, |
| 1830 | "new input from entity: %s\n", entity->name); |
| 1831 | if (entity->content == NULL) { |
| 1832 | switch (entity->etype) { |
| 1833 | case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1834 | xmlErrInternal(ctxt, "Cannot parse entity %s\n", |
| 1835 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1836 | break; |
| 1837 | case XML_EXTERNAL_GENERAL_PARSED_ENTITY: |
| 1838 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 1839 | return(xmlLoadExternalEntity((char *) entity->URI, |
| 1840 | (char *) entity->ExternalID, ctxt)); |
| 1841 | case XML_INTERNAL_GENERAL_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1842 | xmlErrInternal(ctxt, |
| 1843 | "Internal entity %s without content !\n", |
| 1844 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1845 | break; |
| 1846 | case XML_INTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1847 | xmlErrInternal(ctxt, |
| 1848 | "Internal parameter entity %s without content !\n", |
| 1849 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1850 | break; |
| 1851 | case XML_INTERNAL_PREDEFINED_ENTITY: |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1852 | xmlErrInternal(ctxt, |
| 1853 | "Predefined entity %s without content !\n", |
| 1854 | entity->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | return(NULL); |
| 1858 | } |
| 1859 | input = xmlNewInputStream(ctxt); |
| 1860 | if (input == NULL) { |
| 1861 | return(NULL); |
| 1862 | } |
| 1863 | input->filename = (char *) entity->URI; |
| 1864 | input->base = entity->content; |
| 1865 | input->cur = entity->content; |
| 1866 | input->length = entity->length; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1867 | input->end = &entity->content[input->length]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1868 | return(input); |
| 1869 | } |
| 1870 | |
| 1871 | /** |
| 1872 | * xmlNewStringInputStream: |
| 1873 | * @ctxt: an XML parser context |
| 1874 | * @buffer: an memory buffer |
| 1875 | * |
| 1876 | * Create a new input stream based on a memory buffer. |
| 1877 | * Returns the new input stream |
| 1878 | */ |
| 1879 | xmlParserInputPtr |
| 1880 | xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) { |
| 1881 | xmlParserInputPtr input; |
| 1882 | |
| 1883 | if (buffer == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1884 | xmlErrInternal(ctxt, "xmlNewStringInputStream string = NULL\n", |
| 1885 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | return(NULL); |
| 1887 | } |
| 1888 | if (xmlParserDebugEntities) |
| 1889 | xmlGenericError(xmlGenericErrorContext, |
| 1890 | "new fixed input: %.30s\n", buffer); |
| 1891 | input = xmlNewInputStream(ctxt); |
| 1892 | if (input == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1893 | xmlErrMemory(ctxt, "couldn't allocate a new input stream\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | return(NULL); |
| 1895 | } |
| 1896 | input->base = buffer; |
| 1897 | input->cur = buffer; |
| 1898 | input->length = xmlStrlen(buffer); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1899 | input->end = &buffer[input->length]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1900 | return(input); |
| 1901 | } |
| 1902 | |
| 1903 | /** |
| 1904 | * xmlNewInputFromFile: |
| 1905 | * @ctxt: an XML parser context |
| 1906 | * @filename: the filename to use as entity |
| 1907 | * |
| 1908 | * Create a new input stream based on a file. |
| 1909 | * |
| 1910 | * Returns the new input stream or NULL in case of error |
| 1911 | */ |
| 1912 | xmlParserInputPtr |
| 1913 | xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) { |
| 1914 | xmlParserInputBufferPtr buf; |
| 1915 | xmlParserInputPtr inputStream; |
| 1916 | char *directory = NULL; |
| 1917 | xmlChar *URI = NULL; |
| 1918 | |
| 1919 | if (xmlParserDebugEntities) |
| 1920 | xmlGenericError(xmlGenericErrorContext, |
| 1921 | "new input from file: %s\n", filename); |
| 1922 | if (ctxt == NULL) return(NULL); |
| 1923 | buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE); |
| 1924 | if (buf == NULL) |
| 1925 | return(NULL); |
| 1926 | |
| 1927 | URI = xmlStrdup((xmlChar *) filename); |
| 1928 | directory = xmlParserGetDirectory((const char *) URI); |
| 1929 | |
| 1930 | inputStream = xmlNewInputStream(ctxt); |
| 1931 | if (inputStream == NULL) { |
| 1932 | if (directory != NULL) xmlFree((char *) directory); |
| 1933 | if (URI != NULL) xmlFree((char *) URI); |
| 1934 | return(NULL); |
| 1935 | } |
| 1936 | |
Daniel Veillard | 8d8bf2c | 2003-09-17 19:36:25 +0000 | [diff] [blame] | 1937 | inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI); |
Daniel Veillard | a66b1d1 | 2003-09-17 20:54:38 +0000 | [diff] [blame] | 1938 | if (URI != NULL) xmlFree((char *) URI); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | inputStream->directory = directory; |
| 1940 | inputStream->buf = buf; |
| 1941 | |
| 1942 | inputStream->base = inputStream->buf->buffer->content; |
| 1943 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1944 | inputStream->end = &inputStream->base[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1945 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 1946 | ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory); |
| 1947 | return(inputStream); |
| 1948 | } |
| 1949 | |
| 1950 | /************************************************************************ |
| 1951 | * * |
| 1952 | * Commodity functions to handle parser contexts * |
| 1953 | * * |
| 1954 | ************************************************************************/ |
| 1955 | |
| 1956 | /** |
| 1957 | * xmlInitParserCtxt: |
| 1958 | * @ctxt: an XML parser context |
| 1959 | * |
| 1960 | * Initialize a parser context |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1961 | * |
| 1962 | * Returns 0 in case of success and -1 in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | */ |
| 1964 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1965 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | xmlInitParserCtxt(xmlParserCtxtPtr ctxt) |
| 1967 | { |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 1968 | if(ctxt==NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1969 | xmlErrInternal(NULL, "Got NULL parser context\n", NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1970 | return(-1); |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1973 | xmlDefaultSAXHandlerInit(); |
| 1974 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1975 | ctxt->dict = xmlDictCreate(); |
| 1976 | if (ctxt->dict == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1977 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1978 | return(-1); |
| 1979 | } |
William M. Brack | 8b2c7f1 | 2002-11-22 05:07:29 +0000 | [diff] [blame] | 1980 | ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
| 1981 | if (ctxt->sax == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1982 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1983 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1984 | } |
| 1985 | else |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 1986 | xmlSAXVersion(ctxt->sax, 2); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1987 | |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1988 | ctxt->maxatts = 0; |
| 1989 | ctxt->atts = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1990 | /* Allocate the Input stack */ |
| 1991 | ctxt->inputTab = (xmlParserInputPtr *) |
| 1992 | xmlMalloc(5 * sizeof(xmlParserInputPtr)); |
| 1993 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 1994 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1995 | ctxt->inputNr = 0; |
| 1996 | ctxt->inputMax = 0; |
| 1997 | ctxt->input = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1998 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1999 | } |
| 2000 | ctxt->inputNr = 0; |
| 2001 | ctxt->inputMax = 5; |
| 2002 | ctxt->input = NULL; |
| 2003 | |
| 2004 | ctxt->version = NULL; |
| 2005 | ctxt->encoding = NULL; |
| 2006 | ctxt->standalone = -1; |
| 2007 | ctxt->hasExternalSubset = 0; |
| 2008 | ctxt->hasPErefs = 0; |
| 2009 | ctxt->html = 0; |
| 2010 | ctxt->external = 0; |
| 2011 | ctxt->instate = XML_PARSER_START; |
| 2012 | ctxt->token = 0; |
| 2013 | ctxt->directory = NULL; |
| 2014 | |
| 2015 | /* Allocate the Node stack */ |
| 2016 | ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr)); |
| 2017 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2018 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2019 | ctxt->nodeNr = 0; |
| 2020 | ctxt->nodeMax = 0; |
| 2021 | ctxt->node = NULL; |
| 2022 | ctxt->inputNr = 0; |
| 2023 | ctxt->inputMax = 0; |
| 2024 | ctxt->input = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2025 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2026 | } |
| 2027 | ctxt->nodeNr = 0; |
| 2028 | ctxt->nodeMax = 10; |
| 2029 | ctxt->node = NULL; |
| 2030 | |
| 2031 | /* Allocate the Name stack */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2032 | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2033 | if (ctxt->nameTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2034 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2035 | ctxt->nodeNr = 0; |
| 2036 | ctxt->nodeMax = 0; |
| 2037 | ctxt->node = NULL; |
| 2038 | ctxt->inputNr = 0; |
| 2039 | ctxt->inputMax = 0; |
| 2040 | ctxt->input = NULL; |
| 2041 | ctxt->nameNr = 0; |
| 2042 | ctxt->nameMax = 0; |
| 2043 | ctxt->name = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2044 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | } |
| 2046 | ctxt->nameNr = 0; |
| 2047 | ctxt->nameMax = 10; |
| 2048 | ctxt->name = NULL; |
| 2049 | |
| 2050 | /* Allocate the space stack */ |
| 2051 | ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int)); |
| 2052 | if (ctxt->spaceTab == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2053 | xmlErrMemory(NULL, "cannot initialize parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2054 | ctxt->nodeNr = 0; |
| 2055 | ctxt->nodeMax = 0; |
| 2056 | ctxt->node = NULL; |
| 2057 | ctxt->inputNr = 0; |
| 2058 | ctxt->inputMax = 0; |
| 2059 | ctxt->input = NULL; |
| 2060 | ctxt->nameNr = 0; |
| 2061 | ctxt->nameMax = 0; |
| 2062 | ctxt->name = NULL; |
| 2063 | ctxt->spaceNr = 0; |
| 2064 | ctxt->spaceMax = 0; |
| 2065 | ctxt->space = NULL; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2066 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2067 | } |
| 2068 | ctxt->spaceNr = 1; |
| 2069 | ctxt->spaceMax = 10; |
| 2070 | ctxt->spaceTab[0] = -1; |
| 2071 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2072 | ctxt->userData = ctxt; |
| 2073 | ctxt->myDoc = NULL; |
| 2074 | ctxt->wellFormed = 1; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 2075 | ctxt->nsWellFormed = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2076 | ctxt->valid = 1; |
| 2077 | ctxt->loadsubset = xmlLoadExtDtdDefaultValue; |
| 2078 | ctxt->validate = xmlDoValidityCheckingDefaultValue; |
| 2079 | ctxt->pedantic = xmlPedanticParserDefaultValue; |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 2080 | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2081 | ctxt->keepBlanks = xmlKeepBlanksDefaultValue; |
Daniel Veillard | 1669828 | 2001-09-14 10:29:27 +0000 | [diff] [blame] | 2082 | if (ctxt->keepBlanks == 0) |
Daniel Veillard | 11476b4 | 2003-09-26 14:51:39 +0000 | [diff] [blame] | 2083 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
Daniel Veillard | 1669828 | 2001-09-14 10:29:27 +0000 | [diff] [blame] | 2084 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2085 | ctxt->vctxt.userData = ctxt; |
Daniel Veillard | 4e1b26c | 2002-02-03 20:13:06 +0000 | [diff] [blame] | 2086 | ctxt->vctxt.error = xmlParserValidityError; |
| 2087 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | if (ctxt->validate) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2089 | if (xmlGetWarningsDefaultValue == 0) |
| 2090 | ctxt->vctxt.warning = NULL; |
| 2091 | else |
| 2092 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 34b1b3a | 2001-04-21 14:16:10 +0000 | [diff] [blame] | 2093 | ctxt->vctxt.nodeMax = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2094 | } |
| 2095 | ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue; |
| 2096 | ctxt->record_info = 0; |
| 2097 | ctxt->nbChars = 0; |
| 2098 | ctxt->checkIndex = 0; |
| 2099 | ctxt->inSubset = 0; |
| 2100 | ctxt->errNo = XML_ERR_OK; |
| 2101 | ctxt->depth = 0; |
| 2102 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2103 | ctxt->catalogs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2104 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2105 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /** |
| 2109 | * xmlFreeParserCtxt: |
| 2110 | * @ctxt: an XML parser context |
| 2111 | * |
| 2112 | * Free all the memory used by a parser context. However the parsed |
| 2113 | * document in ctxt->myDoc is not freed. |
| 2114 | */ |
| 2115 | |
| 2116 | void |
| 2117 | xmlFreeParserCtxt(xmlParserCtxtPtr ctxt) |
| 2118 | { |
| 2119 | xmlParserInputPtr input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2120 | |
| 2121 | if (ctxt == NULL) return; |
| 2122 | |
| 2123 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 2124 | xmlFreeInputStream(input); |
| 2125 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2126 | if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab); |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 2127 | if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2128 | if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab); |
| 2129 | if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab); |
| 2130 | if (ctxt->version != NULL) xmlFree((char *) ctxt->version); |
| 2131 | if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2132 | if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI); |
| 2133 | if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2134 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 2135 | if ((ctxt->sax != NULL) && |
| 2136 | (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 2137 | #else |
| 2138 | if (ctxt->sax != NULL) |
| 2139 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | xmlFree(ctxt->sax); |
| 2141 | if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory); |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 2142 | if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab); |
Igor Zlatkovic | d37c139 | 2003-08-28 10:34:33 +0000 | [diff] [blame] | 2143 | if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts); |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2144 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2145 | if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2146 | if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab); |
| 2147 | if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs); |
| 2148 | if (ctxt->attsDefault != NULL) |
| 2149 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2150 | if (ctxt->attsSpecial != NULL) |
| 2151 | xmlHashFree(ctxt->attsSpecial, NULL); |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 2152 | if (ctxt->freeElems != NULL) { |
| 2153 | xmlNodePtr cur, next; |
| 2154 | |
| 2155 | cur = ctxt->freeElems; |
| 2156 | while (cur != NULL) { |
| 2157 | next = cur->next; |
| 2158 | xmlFree(cur); |
| 2159 | cur = next; |
| 2160 | } |
| 2161 | } |
| 2162 | if (ctxt->freeAttrs != NULL) { |
| 2163 | xmlAttrPtr cur, next; |
| 2164 | |
| 2165 | cur = ctxt->freeAttrs; |
| 2166 | while (cur != NULL) { |
| 2167 | next = cur->next; |
| 2168 | xmlFree(cur); |
| 2169 | cur = next; |
| 2170 | } |
| 2171 | } |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 2172 | /* |
| 2173 | * cleanup the error strings |
| 2174 | */ |
| 2175 | if (ctxt->lastError.message != NULL) |
| 2176 | xmlFree(ctxt->lastError.message); |
| 2177 | if (ctxt->lastError.file != NULL) |
| 2178 | xmlFree(ctxt->lastError.file); |
| 2179 | if (ctxt->lastError.str1 != NULL) |
| 2180 | xmlFree(ctxt->lastError.str1); |
| 2181 | if (ctxt->lastError.str2 != NULL) |
| 2182 | xmlFree(ctxt->lastError.str2); |
| 2183 | if (ctxt->lastError.str3 != NULL) |
| 2184 | xmlFree(ctxt->lastError.str3); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2185 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 2186 | #ifdef LIBXML_CATALOG_ENABLED |
| 2187 | if (ctxt->catalogs != NULL) |
| 2188 | xmlCatalogFreeLocal(ctxt->catalogs); |
| 2189 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2190 | xmlFree(ctxt); |
| 2191 | } |
| 2192 | |
| 2193 | /** |
| 2194 | * xmlNewParserCtxt: |
| 2195 | * |
| 2196 | * Allocate and initialize a new parser context. |
| 2197 | * |
| 2198 | * Returns the xmlParserCtxtPtr or NULL |
| 2199 | */ |
| 2200 | |
| 2201 | xmlParserCtxtPtr |
| 2202 | xmlNewParserCtxt() |
| 2203 | { |
| 2204 | xmlParserCtxtPtr ctxt; |
| 2205 | |
| 2206 | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
| 2207 | if (ctxt == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2208 | xmlErrMemory(NULL, "cannot allocate parser context\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | return(NULL); |
| 2210 | } |
| 2211 | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2212 | if (xmlInitParserCtxt(ctxt) < 0) { |
| 2213 | xmlFreeParserCtxt(ctxt); |
| 2214 | return(NULL); |
| 2215 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2216 | return(ctxt); |
| 2217 | } |
| 2218 | |
| 2219 | /************************************************************************ |
| 2220 | * * |
| 2221 | * Handling of node informations * |
| 2222 | * * |
| 2223 | ************************************************************************/ |
| 2224 | |
| 2225 | /** |
| 2226 | * xmlClearParserCtxt: |
| 2227 | * @ctxt: an XML parser context |
| 2228 | * |
| 2229 | * Clear (release owned resources) and reinitialize a parser context |
| 2230 | */ |
| 2231 | |
| 2232 | void |
| 2233 | xmlClearParserCtxt(xmlParserCtxtPtr ctxt) |
| 2234 | { |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 2235 | if (ctxt==NULL) |
| 2236 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | xmlClearNodeInfoSeq(&ctxt->node_seq); |
| 2238 | xmlInitParserCtxt(ctxt); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * xmlParserFindNodeInfo: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2243 | * @ctx: an XML parser context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2244 | * @node: an XML node within the tree |
| 2245 | * |
| 2246 | * Find the parser node info struct for a given node |
| 2247 | * |
| 2248 | * Returns an xmlParserNodeInfo block pointer or NULL |
| 2249 | */ |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2250 | const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx, |
| 2251 | const xmlNodePtr node) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2252 | { |
| 2253 | unsigned long pos; |
| 2254 | |
| 2255 | /* Find position where node should be at */ |
| 2256 | pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node); |
Daniel Veillard | b1d6287 | 2001-09-21 09:47:08 +0000 | [diff] [blame] | 2257 | 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] | 2258 | return &ctx->node_seq.buffer[pos]; |
| 2259 | else |
| 2260 | return NULL; |
| 2261 | } |
| 2262 | |
| 2263 | |
| 2264 | /** |
| 2265 | * xmlInitNodeInfoSeq: |
| 2266 | * @seq: a node info sequence pointer |
| 2267 | * |
| 2268 | * -- Initialize (set to initial state) node info sequence |
| 2269 | */ |
| 2270 | void |
| 2271 | xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
| 2272 | { |
| 2273 | seq->length = 0; |
| 2274 | seq->maximum = 0; |
| 2275 | seq->buffer = NULL; |
| 2276 | } |
| 2277 | |
| 2278 | /** |
| 2279 | * xmlClearNodeInfoSeq: |
| 2280 | * @seq: a node info sequence pointer |
| 2281 | * |
| 2282 | * -- Clear (release memory and reinitialize) node |
| 2283 | * info sequence |
| 2284 | */ |
| 2285 | void |
| 2286 | xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
| 2287 | { |
| 2288 | if ( seq->buffer != NULL ) |
| 2289 | xmlFree(seq->buffer); |
| 2290 | xmlInitNodeInfoSeq(seq); |
| 2291 | } |
| 2292 | |
| 2293 | |
| 2294 | /** |
| 2295 | * xmlParserFindNodeInfoIndex: |
| 2296 | * @seq: a node info sequence pointer |
| 2297 | * @node: an XML node pointer |
| 2298 | * |
| 2299 | * |
| 2300 | * xmlParserFindNodeInfoIndex : Find the index that the info record for |
| 2301 | * the given node is or should be at in a sorted sequence |
| 2302 | * |
| 2303 | * Returns a long indicating the position of the record |
| 2304 | */ |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2305 | unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq, |
| 2306 | const xmlNodePtr node) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | { |
| 2308 | unsigned long upper, lower, middle; |
| 2309 | int found = 0; |
| 2310 | |
| 2311 | /* Do a binary search for the key */ |
| 2312 | lower = 1; |
| 2313 | upper = seq->length; |
| 2314 | middle = 0; |
| 2315 | while ( lower <= upper && !found) { |
| 2316 | middle = lower + (upper - lower) / 2; |
| 2317 | if ( node == seq->buffer[middle - 1].node ) |
| 2318 | found = 1; |
| 2319 | else if ( node < seq->buffer[middle - 1].node ) |
| 2320 | upper = middle - 1; |
| 2321 | else |
| 2322 | lower = middle + 1; |
| 2323 | } |
| 2324 | |
| 2325 | /* Return position */ |
| 2326 | if ( middle == 0 || seq->buffer[middle - 1].node < node ) |
| 2327 | return middle; |
| 2328 | else |
| 2329 | return middle - 1; |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /** |
| 2334 | * xmlParserAddNodeInfo: |
| 2335 | * @ctxt: an XML parser context |
| 2336 | * @info: a node info sequence pointer |
| 2337 | * |
| 2338 | * Insert node info record into the sorted sequence |
| 2339 | */ |
| 2340 | void |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2341 | xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 963d2ae | 2002-01-20 22:08:18 +0000 | [diff] [blame] | 2342 | const xmlParserNodeInfoPtr info) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | { |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2344 | unsigned long pos; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2345 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2346 | /* 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] | 2347 | pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr) |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2348 | info->node); |
| 2349 | if (pos < ctxt->node_seq.length |
| 2350 | && ctxt->node_seq.buffer[pos].node == info->node) { |
| 2351 | ctxt->node_seq.buffer[pos] = *info; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2354 | /* Otherwise, we need to add new node to buffer */ |
| 2355 | else { |
| 2356 | if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) { |
| 2357 | xmlParserNodeInfo *tmp_buffer; |
| 2358 | unsigned int byte_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2359 | |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2360 | if (ctxt->node_seq.maximum == 0) |
| 2361 | ctxt->node_seq.maximum = 2; |
| 2362 | byte_size = (sizeof(*ctxt->node_seq.buffer) * |
| 2363 | (2 * ctxt->node_seq.maximum)); |
| 2364 | |
| 2365 | if (ctxt->node_seq.buffer == NULL) |
Daniel Veillard | c4f65ab | 2003-04-21 23:07:45 +0000 | [diff] [blame] | 2366 | tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size); |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2367 | else |
| 2368 | tmp_buffer = |
| 2369 | (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer, |
| 2370 | byte_size); |
| 2371 | |
| 2372 | if (tmp_buffer == NULL) { |
Daniel Veillard | ce9457f | 2003-10-05 21:33:18 +0000 | [diff] [blame] | 2373 | xmlErrMemory(ctxt, "failed to allocate buffer\n"); |
Daniel Veillard | c8c7be4 | 2002-01-23 17:53:44 +0000 | [diff] [blame] | 2374 | return; |
| 2375 | } |
| 2376 | ctxt->node_seq.buffer = tmp_buffer; |
| 2377 | ctxt->node_seq.maximum *= 2; |
| 2378 | } |
| 2379 | |
| 2380 | /* If position is not at end, move elements out of the way */ |
| 2381 | if (pos != ctxt->node_seq.length) { |
| 2382 | unsigned long i; |
| 2383 | |
| 2384 | for (i = ctxt->node_seq.length; i > pos; i--) |
| 2385 | ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1]; |
| 2386 | } |
| 2387 | |
| 2388 | /* Copy element and increase length */ |
| 2389 | ctxt->node_seq.buffer[pos] = *info; |
| 2390 | ctxt->node_seq.length++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | /************************************************************************ |
| 2395 | * * |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 2396 | * Defaults settings * |
| 2397 | * * |
| 2398 | ************************************************************************/ |
| 2399 | /** |
| 2400 | * xmlPedanticParserDefault: |
| 2401 | * @val: int 0 or 1 |
| 2402 | * |
| 2403 | * Set and return the previous value for enabling pedantic warnings. |
| 2404 | * |
| 2405 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 2406 | */ |
| 2407 | |
| 2408 | int |
| 2409 | xmlPedanticParserDefault(int val) { |
| 2410 | int old = xmlPedanticParserDefaultValue; |
| 2411 | |
| 2412 | xmlPedanticParserDefaultValue = val; |
| 2413 | return(old); |
| 2414 | } |
| 2415 | |
| 2416 | /** |
| 2417 | * xmlLineNumbersDefault: |
| 2418 | * @val: int 0 or 1 |
| 2419 | * |
| 2420 | * Set and return the previous value for enabling line numbers in elements |
| 2421 | * contents. This may break on old application and is turned off by default. |
| 2422 | * |
| 2423 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 2424 | */ |
| 2425 | |
| 2426 | int |
| 2427 | xmlLineNumbersDefault(int val) { |
| 2428 | int old = xmlLineNumbersDefaultValue; |
| 2429 | |
| 2430 | xmlLineNumbersDefaultValue = val; |
| 2431 | return(old); |
| 2432 | } |
| 2433 | |
| 2434 | /** |
| 2435 | * xmlSubstituteEntitiesDefault: |
| 2436 | * @val: int 0 or 1 |
| 2437 | * |
| 2438 | * Set and return the previous value for default entity support. |
| 2439 | * Initially the parser always keep entity references instead of substituting |
| 2440 | * 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] | 2441 | * default parser behavior |
| 2442 | * 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] | 2443 | * file basis. |
| 2444 | * |
| 2445 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 2446 | */ |
| 2447 | |
| 2448 | int |
| 2449 | xmlSubstituteEntitiesDefault(int val) { |
| 2450 | int old = xmlSubstituteEntitiesDefaultValue; |
| 2451 | |
| 2452 | xmlSubstituteEntitiesDefaultValue = val; |
| 2453 | return(old); |
| 2454 | } |
| 2455 | |
| 2456 | /** |
| 2457 | * xmlKeepBlanksDefault: |
| 2458 | * @val: int 0 or 1 |
| 2459 | * |
| 2460 | * Set and return the previous value for default blanks text nodes support. |
| 2461 | * The 1.x version of the parser used an heuristic to try to detect |
| 2462 | * ignorable white spaces. As a result the SAX callback was generating |
Daniel Veillard | 11476b4 | 2003-09-26 14:51:39 +0000 | [diff] [blame] | 2463 | * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 2464 | * using the DOM output text nodes containing those blanks were not generated. |
| 2465 | * The 2.x and later version will switch to the XML standard way and |
| 2466 | * ignorableWhitespace() are only generated when running the parser in |
| 2467 | * validating mode and when the current element doesn't allow CDATA or |
| 2468 | * mixed content. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2469 | * This function is provided as a way to force the standard behavior |
Daniel Veillard | a53c688 | 2001-07-25 17:18:57 +0000 | [diff] [blame] | 2470 | * on 1.X libs and to switch back to the old mode for compatibility when |
| 2471 | * running 1.X client code on 2.X . Upgrade of 1.X code should be done |
| 2472 | * by using xmlIsBlankNode() commodity function to detect the "empty" |
| 2473 | * nodes generated. |
| 2474 | * This value also affect autogeneration of indentation when saving code |
| 2475 | * if blanks sections are kept, indentation is not generated. |
| 2476 | * |
| 2477 | * Returns the last value for 0 for no substitution, 1 for substitution. |
| 2478 | */ |
| 2479 | |
| 2480 | int |
| 2481 | xmlKeepBlanksDefault(int val) { |
| 2482 | int old = xmlKeepBlanksDefaultValue; |
| 2483 | |
| 2484 | xmlKeepBlanksDefaultValue = val; |
| 2485 | xmlIndentTreeOutput = !val; |
| 2486 | return(old); |
| 2487 | } |
| 2488 | |