Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * parser.c : an XML 1.0 parser, namespaces and validity support are mostly |
| 3 | * implemented on top of the SAX interfaces |
| 4 | * |
| 5 | * References: |
| 6 | * The XML specification: |
| 7 | * http://www.w3.org/TR/REC-xml |
| 8 | * Original 1.0 version: |
| 9 | * http://www.w3.org/TR/1998/REC-xml-19980210 |
| 10 | * XML second edition working draft |
| 11 | * http://www.w3.org/TR/2000/WD-xml-2e-20000814 |
| 12 | * |
| 13 | * Okay this is a big file, the parser core is around 7000 lines, then it |
| 14 | * is followed by the progressive parser top routines, then the various |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 15 | * high level APIs to call the parser and a few miscellaneous functions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | * A number of helper functions and deprecated ones have been moved to |
| 17 | * parserInternals.c to reduce this file size. |
| 18 | * As much as possible the functions are associated with their relative |
| 19 | * production in the XML specification. A few productions defining the |
| 20 | * different ranges of character are actually implanted either in |
| 21 | * parserInternals.h or parserInternals.c |
| 22 | * The DOM tree build is realized from the default SAX callbacks in |
| 23 | * the module SAX.c. |
| 24 | * The routines doing the validation checks are in valid.c and called either |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 25 | * from the SAX callbacks or as standalone functions using a preparsed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 26 | * document. |
| 27 | * |
| 28 | * See Copyright for the status of this software. |
| 29 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 30 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 33 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 34 | #include "libxml.h" |
| 35 | |
Daniel Veillard | 3c5ed91 | 2002-01-08 10:36:16 +0000 | [diff] [blame] | 36 | #if defined(WIN32) && !defined (__CYGWIN__) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | #define XML_DIR_SEP '\\' |
| 38 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 39 | #define XML_DIR_SEP '/' |
| 40 | #endif |
| 41 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 45 | #include <libxml/threads.h> |
| 46 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 47 | #include <libxml/tree.h> |
| 48 | #include <libxml/parser.h> |
| 49 | #include <libxml/parserInternals.h> |
| 50 | #include <libxml/valid.h> |
| 51 | #include <libxml/entities.h> |
| 52 | #include <libxml/xmlerror.h> |
| 53 | #include <libxml/encoding.h> |
| 54 | #include <libxml/xmlIO.h> |
| 55 | #include <libxml/uri.h> |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 56 | #ifdef LIBXML_CATALOG_ENABLED |
| 57 | #include <libxml/catalog.h> |
| 58 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 59 | |
| 60 | #ifdef HAVE_CTYPE_H |
| 61 | #include <ctype.h> |
| 62 | #endif |
| 63 | #ifdef HAVE_STDLIB_H |
| 64 | #include <stdlib.h> |
| 65 | #endif |
| 66 | #ifdef HAVE_SYS_STAT_H |
| 67 | #include <sys/stat.h> |
| 68 | #endif |
| 69 | #ifdef HAVE_FCNTL_H |
| 70 | #include <fcntl.h> |
| 71 | #endif |
| 72 | #ifdef HAVE_UNISTD_H |
| 73 | #include <unistd.h> |
| 74 | #endif |
| 75 | #ifdef HAVE_ZLIB_H |
| 76 | #include <zlib.h> |
| 77 | #endif |
| 78 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 79 | /** |
| 80 | * MAX_DEPTH: |
| 81 | * |
| 82 | * arbitrary depth limit for the XML documents that we allow to |
| 83 | * process. This is not a limitation of the parser but a safety |
| 84 | * boundary feature. |
| 85 | */ |
| 86 | #define MAX_DEPTH 1024 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 87 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 88 | #define SAX2 1 |
| 89 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 90 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 91 | #define XML_PARSER_BUFFER_SIZE 100 |
| 92 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 93 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 94 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 95 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | * List of XML prefixed PI allowed by W3C specs |
| 97 | */ |
| 98 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 99 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 100 | "xml-stylesheet", |
| 101 | NULL |
| 102 | }; |
| 103 | |
| 104 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 105 | xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 106 | const xmlChar **str); |
| 107 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 108 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 109 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 110 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 111 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 112 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 113 | |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 114 | static void |
| 115 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 116 | xmlNodePtr lastNode); |
| 117 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 118 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 119 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 120 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 121 | |
| 122 | /************************************************************************ |
| 123 | * * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 124 | * Some factorized error routines * |
| 125 | * * |
| 126 | ************************************************************************/ |
| 127 | |
| 128 | /** |
| 129 | * xmlErrMemory: |
| 130 | * @ctxt: an XML parser context |
| 131 | * @extra: extra informations |
| 132 | * |
| 133 | * Handle a redefinition of attribute error |
| 134 | */ |
| 135 | static void |
| 136 | xmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra) |
| 137 | { |
| 138 | if (ctxt != NULL) { |
| 139 | ctxt->errNo = XML_ERR_NO_MEMORY; |
| 140 | ctxt->instate = XML_PARSER_EOF; |
| 141 | ctxt->disableSAX = 1; |
| 142 | } |
| 143 | if ((ctxt != NULL) && (ctxt->sax != NULL) |
| 144 | && (ctxt->sax->error != NULL)) { |
| 145 | if (extra) |
| 146 | ctxt->sax->error(ctxt->userData, |
| 147 | "Memory allocation failed : %s\n", extra); |
| 148 | else |
| 149 | ctxt->sax->error(ctxt->userData, |
| 150 | "Memory allocation failed !\n"); |
| 151 | } else { |
| 152 | if (extra) |
| 153 | xmlGenericError(xmlGenericErrorContext, |
| 154 | "Memory allocation failed : %s\n", extra); |
| 155 | else |
| 156 | xmlGenericError(xmlGenericErrorContext, |
| 157 | "Memory allocation failed !\n"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * xmlErrAttributeDup: |
| 163 | * @ctxt: an XML parser context |
| 164 | * @prefix: the attribute prefix |
| 165 | * @localname: the attribute localname |
| 166 | * |
| 167 | * Handle a redefinition of attribute error |
| 168 | */ |
| 169 | static void |
| 170 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 171 | const xmlChar * localname) |
| 172 | { |
| 173 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
| 174 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
| 175 | if (prefix == NULL) |
| 176 | ctxt->sax->error(ctxt->userData, |
| 177 | "Attribute %s redefined\n", localname); |
| 178 | else |
| 179 | ctxt->sax->error(ctxt->userData, |
| 180 | "Attribute %s:%s redefined\n", prefix, |
| 181 | localname); |
| 182 | } |
| 183 | ctxt->wellFormed = 0; |
| 184 | if (ctxt->recovery == 0) |
| 185 | ctxt->disableSAX = 1; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * xmlFatalErr: |
| 190 | * @ctxt: an XML parser context |
| 191 | * @error: the error number |
| 192 | * @extra: extra information string |
| 193 | * |
| 194 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 195 | */ |
| 196 | static void |
| 197 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char * info) |
| 198 | { |
| 199 | const char *errmsg; |
| 200 | |
| 201 | if (ctxt == NULL) { |
| 202 | xmlGenericError(xmlGenericErrorContext, |
| 203 | "xmlFatalErr: no context !\n"); |
| 204 | return; |
| 205 | } |
| 206 | if ((ctxt->sax == NULL) || (ctxt->sax->error == NULL)) |
| 207 | return; |
| 208 | switch (error) { |
| 209 | case XML_ERR_INVALID_HEX_CHARREF: |
| 210 | errmsg = "CharRef: invalid hexadecimal value\n"; |
| 211 | break; |
| 212 | case XML_ERR_INVALID_DEC_CHARREF: |
| 213 | errmsg = "CharRef: invalid decimal value\n"; |
| 214 | break; |
| 215 | case XML_ERR_INVALID_CHARREF: |
| 216 | errmsg = "CharRef: invalid value\n"; |
| 217 | break; |
| 218 | case XML_ERR_INTERNAL_ERROR: |
| 219 | errmsg = "internal error"; |
| 220 | break; |
| 221 | case XML_ERR_PEREF_AT_EOF: |
| 222 | errmsg = "PEReference at end of document\n"; |
| 223 | break; |
| 224 | case XML_ERR_PEREF_IN_PROLOG: |
| 225 | errmsg = "PEReference in prolog\n"; |
| 226 | break; |
| 227 | case XML_ERR_PEREF_IN_EPILOG: |
| 228 | errmsg = "PEReference in epilog\n"; |
| 229 | break; |
| 230 | case XML_ERR_PEREF_NO_NAME: |
| 231 | errmsg = "PEReference: no name\n"; |
| 232 | break; |
| 233 | case XML_ERR_PEREF_SEMICOL_MISSING: |
| 234 | errmsg = "PEReference: expecting ';'\n"; |
| 235 | break; |
| 236 | case XML_ERR_ENTITY_LOOP: |
| 237 | errmsg = "Detected an entity reference loop\n"; |
| 238 | break; |
| 239 | case XML_ERR_ENTITY_NOT_STARTED: |
| 240 | errmsg = "EntityValue: \" or ' expected\n"; |
| 241 | break; |
| 242 | case XML_ERR_ENTITY_PE_INTERNAL: |
| 243 | errmsg = "PEReferences forbidden in internal subset\n"; |
| 244 | break; |
| 245 | case XML_ERR_ENTITY_NOT_FINISHED: |
| 246 | errmsg = "EntityValue: \" or ' expected\n"; |
| 247 | break; |
| 248 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
| 249 | errmsg = "AttValue: \" or ' expected\n"; |
| 250 | break; |
| 251 | case XML_ERR_LT_IN_ATTRIBUTE: |
| 252 | errmsg = "Unescaped '<' not allowed in attributes values\n"; |
| 253 | break; |
| 254 | case XML_ERR_LITERAL_NOT_STARTED: |
| 255 | errmsg = "SystemLiteral \" or ' expected\n"; |
| 256 | break; |
| 257 | case XML_ERR_LITERAL_NOT_FINISHED: |
| 258 | errmsg = "Unfinished System or Public ID \" or ' expected\n"; |
| 259 | break; |
| 260 | case XML_ERR_MISPLACED_CDATA_END: |
| 261 | errmsg = "Sequence ']]>' not allowed in content\n"; |
| 262 | break; |
| 263 | case XML_ERR_URI_REQUIRED: |
| 264 | errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; |
| 265 | break; |
| 266 | case XML_ERR_PUBID_REQUIRED: |
| 267 | errmsg = "PUBLIC, the Public Identifier is missing\n"; |
| 268 | break; |
| 269 | case XML_ERR_HYPHEN_IN_COMMENT: |
| 270 | errmsg = "Comment must not contain '--' (double-hyphen)\n"; |
| 271 | break; |
| 272 | case XML_ERR_PI_NOT_STARTED: |
| 273 | errmsg = "xmlParsePI : no target name\n"; |
| 274 | break; |
| 275 | case XML_ERR_RESERVED_XML_NAME: |
| 276 | errmsg = "Invalid PI name\n"; |
| 277 | break; |
| 278 | case XML_ERR_NOTATION_NOT_STARTED: |
| 279 | errmsg = "NOTATION: Name expected here\n"; |
| 280 | break; |
| 281 | case XML_ERR_NOTATION_NOT_FINISHED: |
| 282 | errmsg = "'>' required to close NOTATION declaration\n"; |
| 283 | break; |
| 284 | case XML_ERR_VALUE_REQUIRED: |
| 285 | errmsg = "Entity value required\n"; |
| 286 | break; |
| 287 | case XML_ERR_URI_FRAGMENT: |
| 288 | errmsg = "Fragment not allowed"; |
| 289 | break; |
| 290 | case XML_ERR_ATTLIST_NOT_STARTED: |
| 291 | errmsg = "'(' required to start ATTLIST enumeration\n"; |
| 292 | break; |
| 293 | case XML_ERR_NMTOKEN_REQUIRED: |
| 294 | errmsg = "NmToken expected in ATTLIST enumeration\n"; |
| 295 | break; |
| 296 | case XML_ERR_ATTLIST_NOT_FINISHED: |
| 297 | errmsg = "')' required to finish ATTLIST enumeration\n"; |
| 298 | break; |
| 299 | case XML_ERR_MIXED_NOT_STARTED: |
| 300 | errmsg = "MixedContentDecl : '|' or ')*' expected\n"; |
| 301 | break; |
| 302 | case XML_ERR_PCDATA_REQUIRED: |
| 303 | errmsg = "MixedContentDecl : '#PCDATA' expected\n"; |
| 304 | break; |
| 305 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
| 306 | errmsg = "ContentDecl : Name or '(' expected\n"; |
| 307 | break; |
| 308 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
| 309 | errmsg = "ContentDecl : ',' '|' or ')' expected\n"; |
| 310 | break; |
| 311 | case XML_ERR_PEREF_IN_INT_SUBSET: |
| 312 | errmsg = "PEReference: forbidden within markup decl in internal subset\n"; |
| 313 | break; |
| 314 | case XML_ERR_GT_REQUIRED: |
| 315 | errmsg = "expected '>'\n"; |
| 316 | break; |
| 317 | case XML_ERR_CONDSEC_INVALID: |
| 318 | errmsg = "XML conditional section '[' expected\n"; |
| 319 | break; |
| 320 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
| 321 | errmsg = "Content error in the external subset\n"; |
| 322 | break; |
| 323 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 324 | errmsg = "conditional section INCLUDE or IGNORE keyword expected\n"; |
| 325 | break; |
| 326 | case XML_ERR_CONDSEC_NOT_FINISHED: |
| 327 | errmsg = "XML conditional section not closed\n"; |
| 328 | break; |
| 329 | case XML_ERR_XMLDECL_NOT_STARTED: |
| 330 | errmsg = "Text declaration '<?xml' required\n"; |
| 331 | break; |
| 332 | case XML_ERR_XMLDECL_NOT_FINISHED: |
| 333 | errmsg = "parsing XML declaration: '?>' expected\n"; |
| 334 | break; |
| 335 | case XML_ERR_EXT_ENTITY_STANDALONE: |
| 336 | errmsg = "external parsed entities cannot be standalone\n"; |
| 337 | break; |
| 338 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
| 339 | errmsg = "EntityRef: expecting ';'\n"; |
| 340 | break; |
| 341 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
| 342 | errmsg = "DOCTYPE improperly terminated\n"; |
| 343 | break; |
| 344 | case XML_ERR_LTSLASH_REQUIRED: |
| 345 | errmsg = "EndTag: '</' not found\n"; |
| 346 | break; |
| 347 | case XML_ERR_EQUAL_REQUIRED: |
| 348 | errmsg = "expected '='\n"; |
| 349 | break; |
| 350 | case XML_ERR_STRING_NOT_CLOSED: |
| 351 | errmsg = "String not closed expecting \" or '\n"; |
| 352 | break; |
| 353 | case XML_ERR_STRING_NOT_STARTED: |
| 354 | errmsg = "String not started expecting ' or \"\n"; |
| 355 | break; |
| 356 | case XML_ERR_ENCODING_NAME: |
| 357 | errmsg = "Invalid XML encoding name\n"; |
| 358 | break; |
| 359 | case XML_ERR_STANDALONE_VALUE: |
| 360 | errmsg = "standalone accepts only 'yes' or 'no'\n"; |
| 361 | break; |
| 362 | case XML_ERR_DOCUMENT_EMPTY: |
| 363 | errmsg = "Document is empty\n"; |
| 364 | break; |
| 365 | case XML_ERR_DOCUMENT_END: |
| 366 | errmsg = "Extra content at the end of the document\n"; |
| 367 | break; |
| 368 | case XML_ERR_NOT_WELL_BALANCED: |
| 369 | errmsg = "chunk is not well balanced\n"; |
| 370 | break; |
| 371 | case XML_ERR_EXTRA_CONTENT: |
| 372 | errmsg = "extra content at the end of well balanced chunk\n"; |
| 373 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 374 | case XML_ERR_VERSION_MISSING: |
| 375 | errmsg = "Malformed declaration expecting version\n"; |
| 376 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 377 | #if 0 |
| 378 | case : |
| 379 | errmsg = "\n"; |
| 380 | break; |
| 381 | #endif |
| 382 | default: |
| 383 | errmsg = "Unregistered error message\n"; |
| 384 | } |
| 385 | ctxt->errNo = error; |
| 386 | if (info == NULL) { |
| 387 | ctxt->sax->error(ctxt->userData, errmsg); |
| 388 | } else { |
| 389 | ctxt->sax->error(ctxt->userData, "%s: %s", errmsg, info); |
| 390 | } |
| 391 | ctxt->wellFormed = 0; |
| 392 | if (ctxt->recovery == 0) |
| 393 | ctxt->disableSAX = 1; |
| 394 | } |
| 395 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 396 | /** |
| 397 | * xmlFatalErrMsg: |
| 398 | * @ctxt: an XML parser context |
| 399 | * @error: the error number |
| 400 | * @msg: the error message |
| 401 | * |
| 402 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 403 | */ |
| 404 | static void |
| 405 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *msg) |
| 406 | { |
| 407 | if (ctxt == NULL) { |
| 408 | xmlGenericError(xmlGenericErrorContext, |
| 409 | "xmlFatalErr: no context !\n"); |
| 410 | return; |
| 411 | } |
| 412 | ctxt->errNo = error; |
| 413 | if ((ctxt->sax == NULL) || (ctxt->sax->error == NULL)) |
| 414 | return; |
| 415 | ctxt->sax->error(ctxt->userData, msg); |
| 416 | ctxt->wellFormed = 0; |
| 417 | if (ctxt->recovery == 0) |
| 418 | ctxt->disableSAX = 1; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * xmlFatalErrMsgInt: |
| 423 | * @ctxt: an XML parser context |
| 424 | * @error: the error number |
| 425 | * @msg: the error message |
| 426 | * @val: an integer value |
| 427 | * |
| 428 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 429 | */ |
| 430 | static void |
| 431 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 432 | const char *msg, int val) |
| 433 | { |
| 434 | if (ctxt == NULL) { |
| 435 | xmlGenericError(xmlGenericErrorContext, |
| 436 | "xmlFatalErr: no context !\n"); |
| 437 | return; |
| 438 | } |
| 439 | ctxt->errNo = error; |
| 440 | if ((ctxt->sax == NULL) || (ctxt->sax->error == NULL)) |
| 441 | return; |
| 442 | ctxt->sax->error(ctxt->userData, msg, val); |
| 443 | ctxt->wellFormed = 0; |
| 444 | if (ctxt->recovery == 0) |
| 445 | ctxt->disableSAX = 1; |
| 446 | } |
| 447 | |
| 448 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 449 | * xmlFatalErrMsgStr: |
| 450 | * @ctxt: an XML parser context |
| 451 | * @error: the error number |
| 452 | * @msg: the error message |
| 453 | * @val: a string value |
| 454 | * |
| 455 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 456 | */ |
| 457 | static void |
| 458 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 459 | const char *msg, const xmlChar *val) |
| 460 | { |
| 461 | if (ctxt == NULL) { |
| 462 | xmlGenericError(xmlGenericErrorContext, |
| 463 | "xmlFatalErr: no context !\n"); |
| 464 | return; |
| 465 | } |
| 466 | ctxt->errNo = error; |
| 467 | if ((ctxt->sax == NULL) || (ctxt->sax->error == NULL)) |
| 468 | return; |
| 469 | ctxt->sax->error(ctxt->userData, msg, val); |
| 470 | ctxt->wellFormed = 0; |
| 471 | if (ctxt->recovery == 0) |
| 472 | ctxt->disableSAX = 1; |
| 473 | } |
| 474 | |
| 475 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 476 | * xmlNsErr: |
| 477 | * @ctxt: an XML parser context |
| 478 | * @error: the error number |
| 479 | * @msg: the message |
| 480 | * @info1: extra information string |
| 481 | * @info2: extra information string |
| 482 | * |
| 483 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 484 | */ |
| 485 | static void |
| 486 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 487 | const char *msg, |
| 488 | const xmlChar *info1, const xmlChar *info2, const xmlChar *info3) |
| 489 | { |
| 490 | if (ctxt == NULL) |
| 491 | return; |
| 492 | if ((ctxt->sax == NULL) || (ctxt->sax->error == NULL)) |
| 493 | return; |
| 494 | |
| 495 | ctxt->errNo = error; |
| 496 | if (info1 == NULL) { |
| 497 | ctxt->sax->error(ctxt->userData, msg); |
| 498 | } else if (info2 == NULL) { |
| 499 | ctxt->sax->error(ctxt->userData, msg, info1); |
| 500 | } else if (info3 == NULL) { |
| 501 | ctxt->sax->error(ctxt->userData, msg, info1, info2); |
| 502 | } else { |
| 503 | ctxt->sax->error(ctxt->userData, msg, info1, info2, info3); |
| 504 | } |
| 505 | ctxt->nsWellFormed = 0; |
| 506 | } |
| 507 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 508 | /************************************************************************ |
| 509 | * * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 510 | * SAX2 defaulted attributes handling * |
| 511 | * * |
| 512 | ************************************************************************/ |
| 513 | |
| 514 | /** |
| 515 | * xmlDetectSAX2: |
| 516 | * @ctxt: an XML parser context |
| 517 | * |
| 518 | * Do the SAX2 detection and specific intialization |
| 519 | */ |
| 520 | static void |
| 521 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 522 | if (ctxt == NULL) return; |
| 523 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 524 | ((ctxt->sax->startElementNs != NULL) || |
| 525 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
| 526 | |
| 527 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 528 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 529 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 530 | } |
| 531 | |
| 532 | #ifdef SAX2 |
| 533 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 534 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 535 | struct _xmlDefAttrs { |
| 536 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 537 | int maxAttrs; /* the size of the array */ |
| 538 | const xmlChar *values[4]; /* array of localname/prefix/values */ |
| 539 | }; |
| 540 | #endif |
| 541 | |
| 542 | /** |
| 543 | * xmlAddDefAttrs: |
| 544 | * @ctxt: an XML parser context |
| 545 | * @fullname: the element fullname |
| 546 | * @fullattr: the attribute fullname |
| 547 | * @value: the attribute value |
| 548 | * |
| 549 | * Add a defaulted attribute for an element |
| 550 | */ |
| 551 | static void |
| 552 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 553 | const xmlChar *fullname, |
| 554 | const xmlChar *fullattr, |
| 555 | const xmlChar *value) { |
| 556 | xmlDefAttrsPtr defaults; |
| 557 | int len; |
| 558 | const xmlChar *name; |
| 559 | const xmlChar *prefix; |
| 560 | |
| 561 | if (ctxt->attsDefault == NULL) { |
| 562 | ctxt->attsDefault = xmlHashCreate(10); |
| 563 | if (ctxt->attsDefault == NULL) |
| 564 | goto mem_error; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * plit the element name into prefix:localname , the string found |
| 569 | * are within the DTD and hen not associated to namespace names. |
| 570 | */ |
| 571 | name = xmlSplitQName3(fullname, &len); |
| 572 | if (name == NULL) { |
| 573 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 574 | prefix = NULL; |
| 575 | } else { |
| 576 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 577 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * make sure there is some storage |
| 582 | */ |
| 583 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 584 | if (defaults == NULL) { |
| 585 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
| 586 | 12 * sizeof(const xmlChar *)); |
| 587 | if (defaults == NULL) |
| 588 | goto mem_error; |
| 589 | defaults->maxAttrs = 4; |
| 590 | defaults->nbAttrs = 0; |
| 591 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 592 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
| 593 | defaults = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
| 594 | (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *)); |
| 595 | if (defaults == NULL) |
| 596 | goto mem_error; |
| 597 | defaults->maxAttrs *= 2; |
| 598 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * plit the element name into prefix:localname , the string found |
| 603 | * are within the DTD and hen not associated to namespace names. |
| 604 | */ |
| 605 | name = xmlSplitQName3(fullattr, &len); |
| 606 | if (name == NULL) { |
| 607 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 608 | prefix = NULL; |
| 609 | } else { |
| 610 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 611 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 612 | } |
| 613 | |
| 614 | defaults->values[4 * defaults->nbAttrs] = name; |
| 615 | defaults->values[4 * defaults->nbAttrs + 1] = prefix; |
| 616 | /* intern the string and precompute the end */ |
| 617 | len = xmlStrlen(value); |
| 618 | value = xmlDictLookup(ctxt->dict, value, len); |
| 619 | defaults->values[4 * defaults->nbAttrs + 2] = value; |
| 620 | defaults->values[4 * defaults->nbAttrs + 3] = value + len; |
| 621 | defaults->nbAttrs++; |
| 622 | |
| 623 | return; |
| 624 | |
| 625 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 626 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 630 | /** |
| 631 | * xmlAddSpecialAttr: |
| 632 | * @ctxt: an XML parser context |
| 633 | * @fullname: the element fullname |
| 634 | * @fullattr: the attribute fullname |
| 635 | * @type: the attribute type |
| 636 | * |
| 637 | * Register that this attribute is not CDATA |
| 638 | */ |
| 639 | static void |
| 640 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 641 | const xmlChar *fullname, |
| 642 | const xmlChar *fullattr, |
| 643 | int type) |
| 644 | { |
| 645 | if (ctxt->attsSpecial == NULL) { |
| 646 | ctxt->attsSpecial = xmlHashCreate(10); |
| 647 | if (ctxt->attsSpecial == NULL) |
| 648 | goto mem_error; |
| 649 | } |
| 650 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 651 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 652 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 653 | return; |
| 654 | |
| 655 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 656 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 657 | return; |
| 658 | } |
| 659 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 660 | /************************************************************************ |
| 661 | * * |
| 662 | * Parser stacks related functions and macros * |
| 663 | * * |
| 664 | ************************************************************************/ |
| 665 | |
| 666 | xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 667 | const xmlChar ** str); |
| 668 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 669 | #ifdef SAX2 |
| 670 | /** |
| 671 | * nsPush: |
| 672 | * @ctxt: an XML parser context |
| 673 | * @prefix: the namespace prefix or NULL |
| 674 | * @URL: the namespace name |
| 675 | * |
| 676 | * Pushes a new parser namespace on top of the ns stack |
| 677 | * |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 678 | * Returns -1 in case of error, the index in the stack otherwise, |
| 679 | * and -2 if the namespace should be discarded. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 680 | */ |
| 681 | static int |
| 682 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 683 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 684 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 685 | int i; |
| 686 | for (i = 0;i < ctxt->nsNr;i += 2) { |
| 687 | if (ctxt->nsTab[i] == prefix) { |
| 688 | /* in scope */ |
| 689 | if (ctxt->nsTab[i + 1] == URL) |
| 690 | return(-2); |
| 691 | /* out of scope keep it */ |
| 692 | break; |
| 693 | } |
| 694 | } |
| 695 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 696 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 697 | ctxt->nsMax = 10; |
| 698 | ctxt->nsNr = 0; |
| 699 | ctxt->nsTab = (const xmlChar **) |
| 700 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 701 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 702 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 703 | ctxt->nsMax = 0; |
| 704 | return (-1); |
| 705 | } |
| 706 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
| 707 | ctxt->nsMax *= 2; |
| 708 | ctxt->nsTab = (const xmlChar **) |
| 709 | xmlRealloc(ctxt->nsTab, |
| 710 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 711 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 712 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 713 | ctxt->nsMax /= 2; |
| 714 | return (-1); |
| 715 | } |
| 716 | } |
| 717 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 718 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 719 | return (ctxt->nsNr); |
| 720 | } |
| 721 | /** |
| 722 | * nsPop: |
| 723 | * @ctxt: an XML parser context |
| 724 | * @nr: the number to pop |
| 725 | * |
| 726 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 727 | * |
| 728 | * Returns the number of namespaces removed |
| 729 | */ |
| 730 | static int |
| 731 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 732 | { |
| 733 | int i; |
| 734 | |
| 735 | if (ctxt->nsTab == NULL) return(0); |
| 736 | if (ctxt->nsNr < nr) { |
| 737 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 738 | nr = ctxt->nsNr; |
| 739 | } |
| 740 | if (ctxt->nsNr <= 0) |
| 741 | return (0); |
| 742 | |
| 743 | for (i = 0;i < nr;i++) { |
| 744 | ctxt->nsNr--; |
| 745 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 746 | } |
| 747 | return(nr); |
| 748 | } |
| 749 | #endif |
| 750 | |
| 751 | static int |
| 752 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 753 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 754 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 755 | int maxatts; |
| 756 | |
| 757 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 758 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 759 | atts = (const xmlChar **) |
| 760 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 761 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 762 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 763 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 764 | if (attallocs == NULL) goto mem_error; |
| 765 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 766 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 767 | } else if (nr + 5 > ctxt->maxatts) { |
| 768 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 769 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 770 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 771 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 772 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 773 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 774 | (maxatts / 5) * sizeof(int)); |
| 775 | if (attallocs == NULL) goto mem_error; |
| 776 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 777 | ctxt->maxatts = maxatts; |
| 778 | } |
| 779 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 780 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 781 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 782 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 785 | /** |
| 786 | * inputPush: |
| 787 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 788 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 789 | * |
| 790 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 791 | * |
| 792 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 793 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 794 | extern int |
| 795 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 796 | { |
| 797 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 798 | ctxt->inputMax *= 2; |
| 799 | ctxt->inputTab = |
| 800 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 801 | ctxt->inputMax * |
| 802 | sizeof(ctxt->inputTab[0])); |
| 803 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 804 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 805 | return (0); |
| 806 | } |
| 807 | } |
| 808 | ctxt->inputTab[ctxt->inputNr] = value; |
| 809 | ctxt->input = value; |
| 810 | return (ctxt->inputNr++); |
| 811 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 812 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 813 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 814 | * @ctxt: an XML parser context |
| 815 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 816 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 817 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 818 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 819 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 820 | extern xmlParserInputPtr |
| 821 | inputPop(xmlParserCtxtPtr ctxt) |
| 822 | { |
| 823 | xmlParserInputPtr ret; |
| 824 | |
| 825 | if (ctxt->inputNr <= 0) |
| 826 | return (0); |
| 827 | ctxt->inputNr--; |
| 828 | if (ctxt->inputNr > 0) |
| 829 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 830 | else |
| 831 | ctxt->input = NULL; |
| 832 | ret = ctxt->inputTab[ctxt->inputNr]; |
| 833 | ctxt->inputTab[ctxt->inputNr] = 0; |
| 834 | return (ret); |
| 835 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 836 | /** |
| 837 | * nodePush: |
| 838 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 839 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 840 | * |
| 841 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 842 | * |
| 843 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 844 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 845 | extern int |
| 846 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 847 | { |
| 848 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
| 849 | ctxt->nodeMax *= 2; |
| 850 | ctxt->nodeTab = |
| 851 | (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 852 | ctxt->nodeMax * |
| 853 | sizeof(ctxt->nodeTab[0])); |
| 854 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 855 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 856 | return (0); |
| 857 | } |
| 858 | } |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 859 | #ifdef MAX_DEPTH |
| 860 | if (ctxt->nodeNr > MAX_DEPTH) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 861 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 862 | "Excessive depth in document: change MAX_DEPTH = %d\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 863 | MAX_DEPTH); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 864 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 865 | return(0); |
| 866 | } |
| 867 | #endif |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 868 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 869 | ctxt->node = value; |
| 870 | return (ctxt->nodeNr++); |
| 871 | } |
| 872 | /** |
| 873 | * nodePop: |
| 874 | * @ctxt: an XML parser context |
| 875 | * |
| 876 | * Pops the top element node from the node stack |
| 877 | * |
| 878 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 879 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 880 | extern xmlNodePtr |
| 881 | nodePop(xmlParserCtxtPtr ctxt) |
| 882 | { |
| 883 | xmlNodePtr ret; |
| 884 | |
| 885 | if (ctxt->nodeNr <= 0) |
| 886 | return (0); |
| 887 | ctxt->nodeNr--; |
| 888 | if (ctxt->nodeNr > 0) |
| 889 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 890 | else |
| 891 | ctxt->node = NULL; |
| 892 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 893 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 894 | return (ret); |
| 895 | } |
| 896 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 897 | * nameNsPush: |
| 898 | * @ctxt: an XML parser context |
| 899 | * @value: the element name |
| 900 | * @prefix: the element prefix |
| 901 | * @URI: the element namespace name |
| 902 | * |
| 903 | * Pushes a new element name/prefix/URL on top of the name stack |
| 904 | * |
| 905 | * Returns -1 in case of error, the index in the stack otherwise |
| 906 | */ |
| 907 | static int |
| 908 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 909 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 910 | { |
| 911 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 912 | const xmlChar * *tmp; |
| 913 | void **tmp2; |
| 914 | ctxt->nameMax *= 2; |
| 915 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 916 | ctxt->nameMax * |
| 917 | sizeof(ctxt->nameTab[0])); |
| 918 | if (tmp == NULL) { |
| 919 | ctxt->nameMax /= 2; |
| 920 | goto mem_error; |
| 921 | } |
| 922 | ctxt->nameTab = tmp; |
| 923 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 924 | ctxt->nameMax * 3 * |
| 925 | sizeof(ctxt->pushTab[0])); |
| 926 | if (tmp2 == NULL) { |
| 927 | ctxt->nameMax /= 2; |
| 928 | goto mem_error; |
| 929 | } |
| 930 | ctxt->pushTab = tmp2; |
| 931 | } |
| 932 | ctxt->nameTab[ctxt->nameNr] = value; |
| 933 | ctxt->name = value; |
| 934 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 935 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 936 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 937 | return (ctxt->nameNr++); |
| 938 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 939 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 940 | return (-1); |
| 941 | } |
| 942 | /** |
| 943 | * nameNsPop: |
| 944 | * @ctxt: an XML parser context |
| 945 | * |
| 946 | * Pops the top element/prefix/URI name from the name stack |
| 947 | * |
| 948 | * Returns the name just removed |
| 949 | */ |
| 950 | static const xmlChar * |
| 951 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 952 | { |
| 953 | const xmlChar *ret; |
| 954 | |
| 955 | if (ctxt->nameNr <= 0) |
| 956 | return (0); |
| 957 | ctxt->nameNr--; |
| 958 | if (ctxt->nameNr > 0) |
| 959 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 960 | else |
| 961 | ctxt->name = NULL; |
| 962 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 963 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 964 | return (ret); |
| 965 | } |
| 966 | |
| 967 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 968 | * namePush: |
| 969 | * @ctxt: an XML parser context |
| 970 | * @value: the element name |
| 971 | * |
| 972 | * Pushes a new element name on top of the name stack |
| 973 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 974 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 975 | */ |
| 976 | extern int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 977 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 978 | { |
| 979 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 980 | const xmlChar * *tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 981 | ctxt->nameMax *= 2; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 982 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 983 | ctxt->nameMax * |
| 984 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 985 | if (tmp == NULL) { |
| 986 | ctxt->nameMax /= 2; |
| 987 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 988 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 989 | ctxt->nameTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 990 | } |
| 991 | ctxt->nameTab[ctxt->nameNr] = value; |
| 992 | ctxt->name = value; |
| 993 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 994 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 995 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 996 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 997 | } |
| 998 | /** |
| 999 | * namePop: |
| 1000 | * @ctxt: an XML parser context |
| 1001 | * |
| 1002 | * Pops the top element name from the name stack |
| 1003 | * |
| 1004 | * Returns the name just removed |
| 1005 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1006 | extern const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1007 | namePop(xmlParserCtxtPtr ctxt) |
| 1008 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1009 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1010 | |
| 1011 | if (ctxt->nameNr <= 0) |
| 1012 | return (0); |
| 1013 | ctxt->nameNr--; |
| 1014 | if (ctxt->nameNr > 0) |
| 1015 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1016 | else |
| 1017 | ctxt->name = NULL; |
| 1018 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1019 | ctxt->nameTab[ctxt->nameNr] = 0; |
| 1020 | return (ret); |
| 1021 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1022 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1023 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1024 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
| 1025 | ctxt->spaceMax *= 2; |
| 1026 | ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab, |
| 1027 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1028 | if (ctxt->spaceTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1029 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1030 | return(0); |
| 1031 | } |
| 1032 | } |
| 1033 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1034 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1035 | return(ctxt->spaceNr++); |
| 1036 | } |
| 1037 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1038 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1039 | int ret; |
| 1040 | if (ctxt->spaceNr <= 0) return(0); |
| 1041 | ctxt->spaceNr--; |
| 1042 | if (ctxt->spaceNr > 0) |
| 1043 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1044 | else |
| 1045 | ctxt->space = NULL; |
| 1046 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1047 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1048 | return(ret); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Macros for accessing the content. Those should be used only by the parser, |
| 1053 | * and not exported. |
| 1054 | * |
| 1055 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1056 | * use them |
| 1057 | * |
| 1058 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1059 | * To be used with extreme caution since operations consuming |
| 1060 | * characters may move the input buffer to a different location ! |
| 1061 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1062 | * This should be used internally by the parser |
| 1063 | * only to compare to ASCII values otherwise it would break when |
| 1064 | * running with UTF-8 encoding. |
| 1065 | * RAW same as CUR but in the input buffer, bypass any token |
| 1066 | * extraction that may have been done |
| 1067 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1068 | * to compare on ASCII based substring. |
| 1069 | * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1070 | * strings without newlines within the parser. |
| 1071 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
| 1072 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1073 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1074 | * |
| 1075 | * NEXT Skip to the next character, this does the proper decoding |
| 1076 | * in UTF-8 mode. It also pop-up unfinished entities on the fly. |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1077 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1078 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1079 | * to the number of xmlChars used for the encoding [0-5]. |
| 1080 | * CUR_SCHAR same but operate on a string instead of the context |
| 1081 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1082 | * the index |
| 1083 | * GROW, SHRINK handling of input buffers |
| 1084 | */ |
| 1085 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1086 | #define RAW (*ctxt->input->cur) |
| 1087 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1088 | #define NXT(val) ctxt->input->cur[(val)] |
| 1089 | #define CUR_PTR ctxt->input->cur |
| 1090 | |
| 1091 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1092 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1093 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1094 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1095 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1096 | xmlPopInput(ctxt); \ |
| 1097 | } while (0) |
| 1098 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1099 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1100 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 1101 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1102 | xmlSHRINK (ctxt); |
| 1103 | |
| 1104 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 1105 | xmlParserInputShrink(ctxt->input); |
| 1106 | if ((*ctxt->input->cur == 0) && |
| 1107 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1108 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1109 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1110 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1111 | #define GROW if ((ctxt->progressive == 0) && \ |
| 1112 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1113 | xmlGROW (ctxt); |
| 1114 | |
| 1115 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
| 1116 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1117 | if ((*ctxt->input->cur == 0) && |
| 1118 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1119 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1120 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1121 | |
| 1122 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 1123 | |
| 1124 | #define NEXT xmlNextChar(ctxt) |
| 1125 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1126 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1127 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1128 | ctxt->input->cur++; \ |
| 1129 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1130 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1131 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 1132 | } |
| 1133 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1134 | #define NEXTL(l) do { \ |
| 1135 | if (*(ctxt->input->cur) == '\n') { \ |
| 1136 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1137 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1138 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1139 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1140 | } while (0) |
| 1141 | |
| 1142 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 1143 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 1144 | |
| 1145 | #define COPY_BUF(l,b,i,v) \ |
| 1146 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1147 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1148 | |
| 1149 | /** |
| 1150 | * xmlSkipBlankChars: |
| 1151 | * @ctxt: the XML parser context |
| 1152 | * |
| 1153 | * skip all blanks character found at that point in the input streams. |
| 1154 | * It pops up finished entities in the process if allowable at that point. |
| 1155 | * |
| 1156 | * Returns the number of space chars skipped |
| 1157 | */ |
| 1158 | |
| 1159 | int |
| 1160 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1161 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1162 | |
| 1163 | /* |
| 1164 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 1165 | * the ASCII range. |
| 1166 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1167 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 1168 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1169 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1170 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1171 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1172 | cur = ctxt->input->cur; |
| 1173 | while (IS_BLANK(*cur)) { |
| 1174 | if (*cur == '\n') { |
| 1175 | ctxt->input->line++; ctxt->input->col = 1; |
| 1176 | } |
| 1177 | cur++; |
| 1178 | res++; |
| 1179 | if (*cur == 0) { |
| 1180 | ctxt->input->cur = cur; |
| 1181 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1182 | cur = ctxt->input->cur; |
| 1183 | } |
| 1184 | } |
| 1185 | ctxt->input->cur = cur; |
| 1186 | } else { |
| 1187 | int cur; |
| 1188 | do { |
| 1189 | cur = CUR; |
| 1190 | while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */ |
| 1191 | NEXT; |
| 1192 | cur = CUR; |
| 1193 | res++; |
| 1194 | } |
| 1195 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 1196 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 1197 | xmlPopInput(ctxt); |
| 1198 | cur = CUR; |
| 1199 | } |
| 1200 | /* |
| 1201 | * Need to handle support of entities branching here |
| 1202 | */ |
| 1203 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 1204 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 1205 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1206 | return(res); |
| 1207 | } |
| 1208 | |
| 1209 | /************************************************************************ |
| 1210 | * * |
| 1211 | * Commodity functions to handle entities * |
| 1212 | * * |
| 1213 | ************************************************************************/ |
| 1214 | |
| 1215 | /** |
| 1216 | * xmlPopInput: |
| 1217 | * @ctxt: an XML parser context |
| 1218 | * |
| 1219 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 1220 | * pop it and return the next char. |
| 1221 | * |
| 1222 | * Returns the current xmlChar in the parser context |
| 1223 | */ |
| 1224 | xmlChar |
| 1225 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
| 1226 | if (ctxt->inputNr == 1) return(0); /* End of main Input */ |
| 1227 | if (xmlParserDebugEntities) |
| 1228 | xmlGenericError(xmlGenericErrorContext, |
| 1229 | "Popping input %d\n", ctxt->inputNr); |
| 1230 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1231 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1232 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1233 | return(xmlPopInput(ctxt)); |
| 1234 | return(CUR); |
| 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * xmlPushInput: |
| 1239 | * @ctxt: an XML parser context |
| 1240 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 1241 | * |
| 1242 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 1243 | * of the previous one(s). |
| 1244 | */ |
| 1245 | void |
| 1246 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
| 1247 | if (input == NULL) return; |
| 1248 | |
| 1249 | if (xmlParserDebugEntities) { |
| 1250 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 1251 | xmlGenericError(xmlGenericErrorContext, |
| 1252 | "%s(%d): ", ctxt->input->filename, |
| 1253 | ctxt->input->line); |
| 1254 | xmlGenericError(xmlGenericErrorContext, |
| 1255 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 1256 | } |
| 1257 | inputPush(ctxt, input); |
| 1258 | GROW; |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * xmlParseCharRef: |
| 1263 | * @ctxt: an XML parser context |
| 1264 | * |
| 1265 | * parse Reference declarations |
| 1266 | * |
| 1267 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1268 | * '&#x' [0-9a-fA-F]+ ';' |
| 1269 | * |
| 1270 | * [ WFC: Legal Character ] |
| 1271 | * Characters referred to using character references must match the |
| 1272 | * production for Char. |
| 1273 | * |
| 1274 | * Returns the value parsed (as an int), 0 in case of error |
| 1275 | */ |
| 1276 | int |
| 1277 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 1278 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1279 | int count = 0; |
| 1280 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1281 | /* |
| 1282 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 1283 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1284 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1285 | (NXT(2) == 'x')) { |
| 1286 | SKIP(3); |
| 1287 | GROW; |
| 1288 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1289 | if (count++ > 20) { |
| 1290 | count = 0; |
| 1291 | GROW; |
| 1292 | } |
| 1293 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1294 | val = val * 16 + (CUR - '0'); |
| 1295 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 1296 | val = val * 16 + (CUR - 'a') + 10; |
| 1297 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 1298 | val = val * 16 + (CUR - 'A') + 10; |
| 1299 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1300 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1301 | val = 0; |
| 1302 | break; |
| 1303 | } |
| 1304 | NEXT; |
| 1305 | count++; |
| 1306 | } |
| 1307 | if (RAW == ';') { |
| 1308 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1309 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1310 | ctxt->nbChars ++; |
| 1311 | ctxt->input->cur++; |
| 1312 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1313 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1314 | SKIP(2); |
| 1315 | GROW; |
| 1316 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1317 | if (count++ > 20) { |
| 1318 | count = 0; |
| 1319 | GROW; |
| 1320 | } |
| 1321 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1322 | val = val * 10 + (CUR - '0'); |
| 1323 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1324 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1325 | val = 0; |
| 1326 | break; |
| 1327 | } |
| 1328 | NEXT; |
| 1329 | count++; |
| 1330 | } |
| 1331 | if (RAW == ';') { |
| 1332 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1333 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1334 | ctxt->nbChars ++; |
| 1335 | ctxt->input->cur++; |
| 1336 | } |
| 1337 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1338 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * [ WFC: Legal Character ] |
| 1343 | * Characters referred to using character references must match the |
| 1344 | * production for Char. |
| 1345 | */ |
| 1346 | if (IS_CHAR(val)) { |
| 1347 | return(val); |
| 1348 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1349 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1350 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 1351 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1352 | } |
| 1353 | return(0); |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * xmlParseStringCharRef: |
| 1358 | * @ctxt: an XML parser context |
| 1359 | * @str: a pointer to an index in the string |
| 1360 | * |
| 1361 | * parse Reference declarations, variant parsing from a string rather |
| 1362 | * than an an input flow. |
| 1363 | * |
| 1364 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1365 | * '&#x' [0-9a-fA-F]+ ';' |
| 1366 | * |
| 1367 | * [ WFC: Legal Character ] |
| 1368 | * Characters referred to using character references must match the |
| 1369 | * production for Char. |
| 1370 | * |
| 1371 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 1372 | * updated to the current value of the index |
| 1373 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1374 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1375 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 1376 | const xmlChar *ptr; |
| 1377 | xmlChar cur; |
| 1378 | int val = 0; |
| 1379 | |
| 1380 | if ((str == NULL) || (*str == NULL)) return(0); |
| 1381 | ptr = *str; |
| 1382 | cur = *ptr; |
| 1383 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 1384 | ptr += 3; |
| 1385 | cur = *ptr; |
| 1386 | while (cur != ';') { /* Non input consuming loop */ |
| 1387 | if ((cur >= '0') && (cur <= '9')) |
| 1388 | val = val * 16 + (cur - '0'); |
| 1389 | else if ((cur >= 'a') && (cur <= 'f')) |
| 1390 | val = val * 16 + (cur - 'a') + 10; |
| 1391 | else if ((cur >= 'A') && (cur <= 'F')) |
| 1392 | val = val * 16 + (cur - 'A') + 10; |
| 1393 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1394 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1395 | val = 0; |
| 1396 | break; |
| 1397 | } |
| 1398 | ptr++; |
| 1399 | cur = *ptr; |
| 1400 | } |
| 1401 | if (cur == ';') |
| 1402 | ptr++; |
| 1403 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 1404 | ptr += 2; |
| 1405 | cur = *ptr; |
| 1406 | while (cur != ';') { /* Non input consuming loops */ |
| 1407 | if ((cur >= '0') && (cur <= '9')) |
| 1408 | val = val * 10 + (cur - '0'); |
| 1409 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1410 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1411 | val = 0; |
| 1412 | break; |
| 1413 | } |
| 1414 | ptr++; |
| 1415 | cur = *ptr; |
| 1416 | } |
| 1417 | if (cur == ';') |
| 1418 | ptr++; |
| 1419 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1420 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1421 | return(0); |
| 1422 | } |
| 1423 | *str = ptr; |
| 1424 | |
| 1425 | /* |
| 1426 | * [ WFC: Legal Character ] |
| 1427 | * Characters referred to using character references must match the |
| 1428 | * production for Char. |
| 1429 | */ |
| 1430 | if (IS_CHAR(val)) { |
| 1431 | return(val); |
| 1432 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1433 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1434 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 1435 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1436 | } |
| 1437 | return(0); |
| 1438 | } |
| 1439 | |
| 1440 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1441 | * xmlNewBlanksWrapperInputStream: |
| 1442 | * @ctxt: an XML parser context |
| 1443 | * @entity: an Entity pointer |
| 1444 | * |
| 1445 | * Create a new input stream for wrapping |
| 1446 | * blanks around a PEReference |
| 1447 | * |
| 1448 | * Returns the new input stream or NULL |
| 1449 | */ |
| 1450 | |
| 1451 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
| 1452 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 1453 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1454 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 1455 | xmlParserInputPtr input; |
| 1456 | xmlChar *buffer; |
| 1457 | size_t length; |
| 1458 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1459 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1460 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1461 | return(NULL); |
| 1462 | } |
| 1463 | if (xmlParserDebugEntities) |
| 1464 | xmlGenericError(xmlGenericErrorContext, |
| 1465 | "new blanks wrapper for entity: %s\n", entity->name); |
| 1466 | input = xmlNewInputStream(ctxt); |
| 1467 | if (input == NULL) { |
| 1468 | return(NULL); |
| 1469 | } |
| 1470 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1471 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1472 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1473 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1474 | return(NULL); |
| 1475 | } |
| 1476 | buffer [0] = ' '; |
| 1477 | buffer [1] = '%'; |
| 1478 | buffer [length-3] = ';'; |
| 1479 | buffer [length-2] = ' '; |
| 1480 | buffer [length-1] = 0; |
| 1481 | memcpy(buffer + 2, entity->name, length - 5); |
| 1482 | input->free = deallocblankswrapper; |
| 1483 | input->base = buffer; |
| 1484 | input->cur = buffer; |
| 1485 | input->length = length; |
| 1486 | input->end = &buffer[length]; |
| 1487 | return(input); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1491 | * xmlParserHandlePEReference: |
| 1492 | * @ctxt: the parser context |
| 1493 | * |
| 1494 | * [69] PEReference ::= '%' Name ';' |
| 1495 | * |
| 1496 | * [ WFC: No Recursion ] |
| 1497 | * A parsed entity must not contain a recursive |
| 1498 | * reference to itself, either directly or indirectly. |
| 1499 | * |
| 1500 | * [ WFC: Entity Declared ] |
| 1501 | * In a document without any DTD, a document with only an internal DTD |
| 1502 | * subset which contains no parameter entity references, or a document |
| 1503 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 1504 | * entity must precede any reference to it... |
| 1505 | * |
| 1506 | * [ VC: Entity Declared ] |
| 1507 | * In a document with an external subset or external parameter entities |
| 1508 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 1509 | * must precede any reference to it... |
| 1510 | * |
| 1511 | * [ WFC: In DTD ] |
| 1512 | * Parameter-entity references may only appear in the DTD. |
| 1513 | * NOTE: misleading but this is handled. |
| 1514 | * |
| 1515 | * A PEReference may have been detected in the current input stream |
| 1516 | * the handling is done accordingly to |
| 1517 | * http://www.w3.org/TR/REC-xml#entproc |
| 1518 | * i.e. |
| 1519 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1520 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | */ |
| 1522 | void |
| 1523 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1524 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1525 | xmlEntityPtr entity = NULL; |
| 1526 | xmlParserInputPtr input; |
| 1527 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1528 | if (RAW != '%') return; |
| 1529 | switch(ctxt->instate) { |
| 1530 | case XML_PARSER_CDATA_SECTION: |
| 1531 | return; |
| 1532 | case XML_PARSER_COMMENT: |
| 1533 | return; |
| 1534 | case XML_PARSER_START_TAG: |
| 1535 | return; |
| 1536 | case XML_PARSER_END_TAG: |
| 1537 | return; |
| 1538 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1539 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | case XML_PARSER_PROLOG: |
| 1542 | case XML_PARSER_START: |
| 1543 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1544 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | return; |
| 1546 | case XML_PARSER_ENTITY_DECL: |
| 1547 | case XML_PARSER_CONTENT: |
| 1548 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 1549 | case XML_PARSER_PI: |
| 1550 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 1551 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1552 | /* we just ignore it there */ |
| 1553 | return; |
| 1554 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1555 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1556 | return; |
| 1557 | case XML_PARSER_ENTITY_VALUE: |
| 1558 | /* |
| 1559 | * NOTE: in the case of entity values, we don't do the |
| 1560 | * substitution here since we need the literal |
| 1561 | * entity value to be able to save the internal |
| 1562 | * subset of the document. |
| 1563 | * This will be handled by xmlStringDecodeEntities |
| 1564 | */ |
| 1565 | return; |
| 1566 | case XML_PARSER_DTD: |
| 1567 | /* |
| 1568 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 1569 | * In the internal DTD subset, parameter-entity references |
| 1570 | * can occur only where markup declarations can occur, not |
| 1571 | * within markup declarations. |
| 1572 | * In that case this is handled in xmlParseMarkupDecl |
| 1573 | */ |
| 1574 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 1575 | return; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1576 | if (IS_BLANK(NXT(1)) || NXT(1) == 0) |
| 1577 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | case XML_PARSER_IGNORE: |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | NEXT; |
| 1584 | name = xmlParseName(ctxt); |
| 1585 | if (xmlParserDebugEntities) |
| 1586 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1587 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1589 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1590 | } else { |
| 1591 | if (RAW == ';') { |
| 1592 | NEXT; |
| 1593 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 1594 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 1595 | if (entity == NULL) { |
| 1596 | |
| 1597 | /* |
| 1598 | * [ WFC: Entity Declared ] |
| 1599 | * In a document without any DTD, a document with only an |
| 1600 | * internal DTD subset which contains no parameter entity |
| 1601 | * references, or a document with "standalone='yes'", ... |
| 1602 | * ... The declaration of a parameter entity must precede |
| 1603 | * any reference to it... |
| 1604 | */ |
| 1605 | if ((ctxt->standalone == 1) || |
| 1606 | ((ctxt->hasExternalSubset == 0) && |
| 1607 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1608 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1609 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | } else { |
| 1611 | /* |
| 1612 | * [ VC: Entity Declared ] |
| 1613 | * In a document with an external subset or external |
| 1614 | * parameter entities with "standalone='no'", ... |
| 1615 | * ... The declaration of a parameter entity must precede |
| 1616 | * any reference to it... |
| 1617 | */ |
| 1618 | if ((!ctxt->disableSAX) && |
| 1619 | (ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 1620 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 1621 | "PEReference: %%%s; not found\n", name); |
| 1622 | } else if ((!ctxt->disableSAX) && |
| 1623 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 1624 | ctxt->sax->warning(ctxt->userData, |
| 1625 | "PEReference: %%%s; not found\n", name); |
| 1626 | ctxt->valid = 0; |
| 1627 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1628 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 1629 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 1630 | xmlPushInput(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1631 | } else { |
| 1632 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 1633 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1634 | xmlChar start[4]; |
| 1635 | xmlCharEncoding enc; |
| 1636 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1637 | /* |
| 1638 | * handle the extra spaces added before and after |
| 1639 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1640 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1641 | */ |
| 1642 | input = xmlNewEntityInputStream(ctxt, entity); |
| 1643 | xmlPushInput(ctxt, input); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1644 | |
| 1645 | /* |
| 1646 | * Get the 4 first bytes and decode the charset |
| 1647 | * if enc != XML_CHAR_ENCODING_NONE |
| 1648 | * plug some encoding conversion routines. |
| 1649 | */ |
| 1650 | GROW |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 1651 | if (entity->length >= 4) { |
| 1652 | start[0] = RAW; |
| 1653 | start[1] = NXT(1); |
| 1654 | start[2] = NXT(2); |
| 1655 | start[3] = NXT(3); |
| 1656 | enc = xmlDetectCharEncoding(start, 4); |
| 1657 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 1658 | xmlSwitchEncoding(ctxt, enc); |
| 1659 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1662 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 1663 | (RAW == '<') && (NXT(1) == '?') && |
| 1664 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 1665 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 1666 | xmlParseTextDecl(ctxt); |
| 1667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1668 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1669 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 1670 | "PEReference: %s is not a parameter entity\n", |
| 1671 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1675 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1676 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /* |
| 1681 | * Macro used to grow the current buffer. |
| 1682 | */ |
| 1683 | #define growBuffer(buffer) { \ |
| 1684 | buffer##_size *= 2; \ |
| 1685 | buffer = (xmlChar *) \ |
| 1686 | xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1687 | if (buffer == NULL) goto mem_error; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 1691 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1692 | * @ctxt: the parser context |
| 1693 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1694 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1695 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 1696 | * @end: an end marker xmlChar, 0 if none |
| 1697 | * @end2: an end marker xmlChar, 0 if none |
| 1698 | * @end3: an end marker xmlChar, 0 if none |
| 1699 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1700 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1701 | * |
| 1702 | * [67] Reference ::= EntityRef | CharRef |
| 1703 | * |
| 1704 | * [69] PEReference ::= '%' Name ';' |
| 1705 | * |
| 1706 | * Returns A newly allocated string with the substitution done. The caller |
| 1707 | * must deallocate it ! |
| 1708 | */ |
| 1709 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1710 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 1711 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1712 | xmlChar *buffer = NULL; |
| 1713 | int buffer_size = 0; |
| 1714 | |
| 1715 | xmlChar *current = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1716 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1717 | xmlEntityPtr ent; |
| 1718 | int c,l; |
| 1719 | int nbchars = 0; |
| 1720 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1721 | if ((str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1722 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1723 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1724 | |
| 1725 | if (ctxt->depth > 40) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1726 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1727 | return(NULL); |
| 1728 | } |
| 1729 | |
| 1730 | /* |
| 1731 | * allocate a translation buffer. |
| 1732 | */ |
| 1733 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1734 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1735 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1736 | |
| 1737 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1738 | * OK loop until we reach one of the ending char or a size limit. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1739 | * we are operating on already parsed values. |
| 1740 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1741 | if (str < last) |
| 1742 | c = CUR_SCHAR(str, l); |
| 1743 | else |
| 1744 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1745 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 1746 | (c != end2) && (c != end3)) { |
| 1747 | |
| 1748 | if (c == 0) break; |
| 1749 | if ((c == '&') && (str[1] == '#')) { |
| 1750 | int val = xmlParseStringCharRef(ctxt, &str); |
| 1751 | if (val != 0) { |
| 1752 | COPY_BUF(0,buffer,nbchars,val); |
| 1753 | } |
| 1754 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 1755 | if (xmlParserDebugEntities) |
| 1756 | xmlGenericError(xmlGenericErrorContext, |
| 1757 | "String decoding Entity Reference: %.30s\n", |
| 1758 | str); |
| 1759 | ent = xmlParseStringEntityRef(ctxt, &str); |
| 1760 | if ((ent != NULL) && |
| 1761 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1762 | if (ent->content != NULL) { |
| 1763 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
| 1764 | } else { |
| 1765 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 1766 | ctxt->sax->error(ctxt->userData, |
| 1767 | "internal error entity has no content\n"); |
| 1768 | } |
| 1769 | } else if ((ent != NULL) && (ent->content != NULL)) { |
| 1770 | xmlChar *rep; |
| 1771 | |
| 1772 | ctxt->depth++; |
| 1773 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 1774 | 0, 0, 0); |
| 1775 | ctxt->depth--; |
| 1776 | if (rep != NULL) { |
| 1777 | current = rep; |
| 1778 | while (*current != 0) { /* non input consuming loop */ |
| 1779 | buffer[nbchars++] = *current++; |
| 1780 | if (nbchars > |
| 1781 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 1782 | growBuffer(buffer); |
| 1783 | } |
| 1784 | } |
| 1785 | xmlFree(rep); |
| 1786 | } |
| 1787 | } else if (ent != NULL) { |
| 1788 | int i = xmlStrlen(ent->name); |
| 1789 | const xmlChar *cur = ent->name; |
| 1790 | |
| 1791 | buffer[nbchars++] = '&'; |
| 1792 | if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) { |
| 1793 | growBuffer(buffer); |
| 1794 | } |
| 1795 | for (;i > 0;i--) |
| 1796 | buffer[nbchars++] = *cur++; |
| 1797 | buffer[nbchars++] = ';'; |
| 1798 | } |
| 1799 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 1800 | if (xmlParserDebugEntities) |
| 1801 | xmlGenericError(xmlGenericErrorContext, |
| 1802 | "String decoding PE Reference: %.30s\n", str); |
| 1803 | ent = xmlParseStringPEReference(ctxt, &str); |
| 1804 | if (ent != NULL) { |
| 1805 | xmlChar *rep; |
| 1806 | |
| 1807 | ctxt->depth++; |
| 1808 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 1809 | 0, 0, 0); |
| 1810 | ctxt->depth--; |
| 1811 | if (rep != NULL) { |
| 1812 | current = rep; |
| 1813 | while (*current != 0) { /* non input consuming loop */ |
| 1814 | buffer[nbchars++] = *current++; |
| 1815 | if (nbchars > |
| 1816 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 1817 | growBuffer(buffer); |
| 1818 | } |
| 1819 | } |
| 1820 | xmlFree(rep); |
| 1821 | } |
| 1822 | } |
| 1823 | } else { |
| 1824 | COPY_BUF(l,buffer,nbchars,c); |
| 1825 | str += l; |
| 1826 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 1827 | growBuffer(buffer); |
| 1828 | } |
| 1829 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1830 | if (str < last) |
| 1831 | c = CUR_SCHAR(str, l); |
| 1832 | else |
| 1833 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1834 | } |
| 1835 | buffer[nbchars++] = 0; |
| 1836 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1837 | |
| 1838 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1839 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1840 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1843 | /** |
| 1844 | * xmlStringDecodeEntities: |
| 1845 | * @ctxt: the parser context |
| 1846 | * @str: the input string |
| 1847 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 1848 | * @end: an end marker xmlChar, 0 if none |
| 1849 | * @end2: an end marker xmlChar, 0 if none |
| 1850 | * @end3: an end marker xmlChar, 0 if none |
| 1851 | * |
| 1852 | * Takes a entity string content and process to do the adequate substitutions. |
| 1853 | * |
| 1854 | * [67] Reference ::= EntityRef | CharRef |
| 1855 | * |
| 1856 | * [69] PEReference ::= '%' Name ';' |
| 1857 | * |
| 1858 | * Returns A newly allocated string with the substitution done. The caller |
| 1859 | * must deallocate it ! |
| 1860 | */ |
| 1861 | xmlChar * |
| 1862 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 1863 | xmlChar end, xmlChar end2, xmlChar end3) { |
| 1864 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 1865 | end, end2, end3)); |
| 1866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1867 | |
| 1868 | /************************************************************************ |
| 1869 | * * |
| 1870 | * Commodity functions to handle xmlChars * |
| 1871 | * * |
| 1872 | ************************************************************************/ |
| 1873 | |
| 1874 | /** |
| 1875 | * xmlStrndup: |
| 1876 | * @cur: the input xmlChar * |
| 1877 | * @len: the len of @cur |
| 1878 | * |
| 1879 | * a strndup for array of xmlChar's |
| 1880 | * |
| 1881 | * Returns a new xmlChar * or NULL |
| 1882 | */ |
| 1883 | xmlChar * |
| 1884 | xmlStrndup(const xmlChar *cur, int len) { |
| 1885 | xmlChar *ret; |
| 1886 | |
| 1887 | if ((cur == NULL) || (len < 0)) return(NULL); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1888 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1889 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1890 | xmlErrMemory(NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1891 | return(NULL); |
| 1892 | } |
| 1893 | memcpy(ret, cur, len * sizeof(xmlChar)); |
| 1894 | ret[len] = 0; |
| 1895 | return(ret); |
| 1896 | } |
| 1897 | |
| 1898 | /** |
| 1899 | * xmlStrdup: |
| 1900 | * @cur: the input xmlChar * |
| 1901 | * |
| 1902 | * a strdup for array of xmlChar's. Since they are supposed to be |
| 1903 | * encoded in UTF-8 or an encoding with 8bit based chars, we assume |
| 1904 | * a termination mark of '0'. |
| 1905 | * |
| 1906 | * Returns a new xmlChar * or NULL |
| 1907 | */ |
| 1908 | xmlChar * |
| 1909 | xmlStrdup(const xmlChar *cur) { |
| 1910 | const xmlChar *p = cur; |
| 1911 | |
| 1912 | if (cur == NULL) return(NULL); |
| 1913 | while (*p != 0) p++; /* non input consuming */ |
| 1914 | return(xmlStrndup(cur, p - cur)); |
| 1915 | } |
| 1916 | |
| 1917 | /** |
| 1918 | * xmlCharStrndup: |
| 1919 | * @cur: the input char * |
| 1920 | * @len: the len of @cur |
| 1921 | * |
| 1922 | * a strndup for char's to xmlChar's |
| 1923 | * |
| 1924 | * Returns a new xmlChar * or NULL |
| 1925 | */ |
| 1926 | |
| 1927 | xmlChar * |
| 1928 | xmlCharStrndup(const char *cur, int len) { |
| 1929 | int i; |
| 1930 | xmlChar *ret; |
| 1931 | |
| 1932 | if ((cur == NULL) || (len < 0)) return(NULL); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1933 | ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1934 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1935 | xmlErrMemory(NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1936 | return(NULL); |
| 1937 | } |
| 1938 | for (i = 0;i < len;i++) |
| 1939 | ret[i] = (xmlChar) cur[i]; |
| 1940 | ret[len] = 0; |
| 1941 | return(ret); |
| 1942 | } |
| 1943 | |
| 1944 | /** |
| 1945 | * xmlCharStrdup: |
| 1946 | * @cur: the input char * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1947 | * |
| 1948 | * a strdup for char's to xmlChar's |
| 1949 | * |
| 1950 | * Returns a new xmlChar * or NULL |
| 1951 | */ |
| 1952 | |
| 1953 | xmlChar * |
| 1954 | xmlCharStrdup(const char *cur) { |
| 1955 | const char *p = cur; |
| 1956 | |
| 1957 | if (cur == NULL) return(NULL); |
| 1958 | while (*p != '\0') p++; /* non input consuming */ |
| 1959 | return(xmlCharStrndup(cur, p - cur)); |
| 1960 | } |
| 1961 | |
| 1962 | /** |
| 1963 | * xmlStrcmp: |
| 1964 | * @str1: the first xmlChar * |
| 1965 | * @str2: the second xmlChar * |
| 1966 | * |
| 1967 | * a strcmp for xmlChar's |
| 1968 | * |
| 1969 | * Returns the integer result of the comparison |
| 1970 | */ |
| 1971 | |
| 1972 | int |
| 1973 | xmlStrcmp(const xmlChar *str1, const xmlChar *str2) { |
| 1974 | register int tmp; |
| 1975 | |
| 1976 | if (str1 == str2) return(0); |
| 1977 | if (str1 == NULL) return(-1); |
| 1978 | if (str2 == NULL) return(1); |
| 1979 | do { |
| 1980 | tmp = *str1++ - *str2; |
| 1981 | if (tmp != 0) return(tmp); |
| 1982 | } while (*str2++ != 0); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | /** |
| 1987 | * xmlStrEqual: |
| 1988 | * @str1: the first xmlChar * |
| 1989 | * @str2: the second xmlChar * |
| 1990 | * |
| 1991 | * Check if both string are equal of have same content |
| 1992 | * Should be a bit more readable and faster than xmlStrEqual() |
| 1993 | * |
| 1994 | * Returns 1 if they are equal, 0 if they are different |
| 1995 | */ |
| 1996 | |
| 1997 | int |
| 1998 | xmlStrEqual(const xmlChar *str1, const xmlChar *str2) { |
| 1999 | if (str1 == str2) return(1); |
| 2000 | if (str1 == NULL) return(0); |
| 2001 | if (str2 == NULL) return(0); |
| 2002 | do { |
| 2003 | if (*str1++ != *str2) return(0); |
| 2004 | } while (*str2++); |
| 2005 | return(1); |
| 2006 | } |
| 2007 | |
| 2008 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2009 | * xmlStrQEqual: |
| 2010 | * @pref: the prefix of the QName |
| 2011 | * @name: the localname of the QName |
| 2012 | * @str: the second xmlChar * |
| 2013 | * |
| 2014 | * Check if a QName is Equal to a given string |
| 2015 | * |
| 2016 | * Returns 1 if they are equal, 0 if they are different |
| 2017 | */ |
| 2018 | |
| 2019 | int |
| 2020 | xmlStrQEqual(const xmlChar *pref, const xmlChar *name, const xmlChar *str) { |
| 2021 | if (pref == NULL) return(xmlStrEqual(name, str)); |
| 2022 | if (name == NULL) return(0); |
| 2023 | if (str == NULL) return(0); |
| 2024 | |
| 2025 | do { |
| 2026 | if (*pref++ != *str) return(0); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2027 | } while ((*str++) && (*pref)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2028 | if (*str++ != ':') return(0); |
| 2029 | do { |
| 2030 | if (*name++ != *str) return(0); |
| 2031 | } while (*str++); |
| 2032 | return(1); |
| 2033 | } |
| 2034 | |
| 2035 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2036 | * xmlStrncmp: |
| 2037 | * @str1: the first xmlChar * |
| 2038 | * @str2: the second xmlChar * |
| 2039 | * @len: the max comparison length |
| 2040 | * |
| 2041 | * a strncmp for xmlChar's |
| 2042 | * |
| 2043 | * Returns the integer result of the comparison |
| 2044 | */ |
| 2045 | |
| 2046 | int |
| 2047 | xmlStrncmp(const xmlChar *str1, const xmlChar *str2, int len) { |
| 2048 | register int tmp; |
| 2049 | |
| 2050 | if (len <= 0) return(0); |
| 2051 | if (str1 == str2) return(0); |
| 2052 | if (str1 == NULL) return(-1); |
| 2053 | if (str2 == NULL) return(1); |
| 2054 | do { |
| 2055 | tmp = *str1++ - *str2; |
| 2056 | if (tmp != 0 || --len == 0) return(tmp); |
| 2057 | } while (*str2++ != 0); |
| 2058 | return 0; |
| 2059 | } |
| 2060 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 2061 | static const xmlChar casemap[256] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2062 | 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, |
| 2063 | 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F, |
| 2064 | 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, |
| 2065 | 0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, |
| 2066 | 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27, |
| 2067 | 0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F, |
| 2068 | 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, |
| 2069 | 0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F, |
| 2070 | 0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67, |
| 2071 | 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, |
| 2072 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, |
| 2073 | 0x78,0x79,0x7A,0x7B,0x5C,0x5D,0x5E,0x5F, |
| 2074 | 0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67, |
| 2075 | 0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F, |
| 2076 | 0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77, |
| 2077 | 0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F, |
| 2078 | 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, |
| 2079 | 0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F, |
| 2080 | 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, |
| 2081 | 0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, |
| 2082 | 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7, |
| 2083 | 0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF, |
| 2084 | 0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7, |
| 2085 | 0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, |
| 2086 | 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7, |
| 2087 | 0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF, |
| 2088 | 0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7, |
| 2089 | 0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, |
| 2090 | 0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7, |
| 2091 | 0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF, |
| 2092 | 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7, |
| 2093 | 0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF |
| 2094 | }; |
| 2095 | |
| 2096 | /** |
| 2097 | * xmlStrcasecmp: |
| 2098 | * @str1: the first xmlChar * |
| 2099 | * @str2: the second xmlChar * |
| 2100 | * |
| 2101 | * a strcasecmp for xmlChar's |
| 2102 | * |
| 2103 | * Returns the integer result of the comparison |
| 2104 | */ |
| 2105 | |
| 2106 | int |
| 2107 | xmlStrcasecmp(const xmlChar *str1, const xmlChar *str2) { |
| 2108 | register int tmp; |
| 2109 | |
| 2110 | if (str1 == str2) return(0); |
| 2111 | if (str1 == NULL) return(-1); |
| 2112 | if (str2 == NULL) return(1); |
| 2113 | do { |
| 2114 | tmp = casemap[*str1++] - casemap[*str2]; |
| 2115 | if (tmp != 0) return(tmp); |
| 2116 | } while (*str2++ != 0); |
| 2117 | return 0; |
| 2118 | } |
| 2119 | |
| 2120 | /** |
| 2121 | * xmlStrncasecmp: |
| 2122 | * @str1: the first xmlChar * |
| 2123 | * @str2: the second xmlChar * |
| 2124 | * @len: the max comparison length |
| 2125 | * |
| 2126 | * a strncasecmp for xmlChar's |
| 2127 | * |
| 2128 | * Returns the integer result of the comparison |
| 2129 | */ |
| 2130 | |
| 2131 | int |
| 2132 | xmlStrncasecmp(const xmlChar *str1, const xmlChar *str2, int len) { |
| 2133 | register int tmp; |
| 2134 | |
| 2135 | if (len <= 0) return(0); |
| 2136 | if (str1 == str2) return(0); |
| 2137 | if (str1 == NULL) return(-1); |
| 2138 | if (str2 == NULL) return(1); |
| 2139 | do { |
| 2140 | tmp = casemap[*str1++] - casemap[*str2]; |
| 2141 | if (tmp != 0 || --len == 0) return(tmp); |
| 2142 | } while (*str2++ != 0); |
| 2143 | return 0; |
| 2144 | } |
| 2145 | |
| 2146 | /** |
| 2147 | * xmlStrchr: |
| 2148 | * @str: the xmlChar * array |
| 2149 | * @val: the xmlChar to search |
| 2150 | * |
| 2151 | * a strchr for xmlChar's |
| 2152 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2153 | * Returns the xmlChar * for the first occurrence or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2154 | */ |
| 2155 | |
| 2156 | const xmlChar * |
| 2157 | xmlStrchr(const xmlChar *str, xmlChar val) { |
| 2158 | if (str == NULL) return(NULL); |
| 2159 | while (*str != 0) { /* non input consuming */ |
| 2160 | if (*str == val) return((xmlChar *) str); |
| 2161 | str++; |
| 2162 | } |
| 2163 | return(NULL); |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * xmlStrstr: |
| 2168 | * @str: the xmlChar * array (haystack) |
| 2169 | * @val: the xmlChar to search (needle) |
| 2170 | * |
| 2171 | * a strstr for xmlChar's |
| 2172 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2173 | * Returns the xmlChar * for the first occurrence or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2174 | */ |
| 2175 | |
| 2176 | const xmlChar * |
Daniel Veillard | 7704473 | 2001-06-29 21:31:07 +0000 | [diff] [blame] | 2177 | xmlStrstr(const xmlChar *str, const xmlChar *val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2178 | int n; |
| 2179 | |
| 2180 | if (str == NULL) return(NULL); |
| 2181 | if (val == NULL) return(NULL); |
| 2182 | n = xmlStrlen(val); |
| 2183 | |
| 2184 | if (n == 0) return(str); |
| 2185 | while (*str != 0) { /* non input consuming */ |
| 2186 | if (*str == *val) { |
| 2187 | if (!xmlStrncmp(str, val, n)) return((const xmlChar *) str); |
| 2188 | } |
| 2189 | str++; |
| 2190 | } |
| 2191 | return(NULL); |
| 2192 | } |
| 2193 | |
| 2194 | /** |
| 2195 | * xmlStrcasestr: |
| 2196 | * @str: the xmlChar * array (haystack) |
| 2197 | * @val: the xmlChar to search (needle) |
| 2198 | * |
| 2199 | * a case-ignoring strstr for xmlChar's |
| 2200 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2201 | * Returns the xmlChar * for the first occurrence or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2202 | */ |
| 2203 | |
| 2204 | const xmlChar * |
| 2205 | xmlStrcasestr(const xmlChar *str, xmlChar *val) { |
| 2206 | int n; |
| 2207 | |
| 2208 | if (str == NULL) return(NULL); |
| 2209 | if (val == NULL) return(NULL); |
| 2210 | n = xmlStrlen(val); |
| 2211 | |
| 2212 | if (n == 0) return(str); |
| 2213 | while (*str != 0) { /* non input consuming */ |
| 2214 | if (casemap[*str] == casemap[*val]) |
| 2215 | if (!xmlStrncasecmp(str, val, n)) return(str); |
| 2216 | str++; |
| 2217 | } |
| 2218 | return(NULL); |
| 2219 | } |
| 2220 | |
| 2221 | /** |
| 2222 | * xmlStrsub: |
| 2223 | * @str: the xmlChar * array (haystack) |
| 2224 | * @start: the index of the first char (zero based) |
| 2225 | * @len: the length of the substring |
| 2226 | * |
| 2227 | * Extract a substring of a given string |
| 2228 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2229 | * Returns the xmlChar * for the first occurrence or NULL. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2230 | */ |
| 2231 | |
| 2232 | xmlChar * |
| 2233 | xmlStrsub(const xmlChar *str, int start, int len) { |
| 2234 | int i; |
| 2235 | |
| 2236 | if (str == NULL) return(NULL); |
| 2237 | if (start < 0) return(NULL); |
| 2238 | if (len < 0) return(NULL); |
| 2239 | |
| 2240 | for (i = 0;i < start;i++) { |
| 2241 | if (*str == 0) return(NULL); |
| 2242 | str++; |
| 2243 | } |
| 2244 | if (*str == 0) return(NULL); |
| 2245 | return(xmlStrndup(str, len)); |
| 2246 | } |
| 2247 | |
| 2248 | /** |
| 2249 | * xmlStrlen: |
| 2250 | * @str: the xmlChar * array |
| 2251 | * |
| 2252 | * length of a xmlChar's string |
| 2253 | * |
| 2254 | * Returns the number of xmlChar contained in the ARRAY. |
| 2255 | */ |
| 2256 | |
| 2257 | int |
| 2258 | xmlStrlen(const xmlChar *str) { |
| 2259 | int len = 0; |
| 2260 | |
| 2261 | if (str == NULL) return(0); |
| 2262 | while (*str != 0) { /* non input consuming */ |
| 2263 | str++; |
| 2264 | len++; |
| 2265 | } |
| 2266 | return(len); |
| 2267 | } |
| 2268 | |
| 2269 | /** |
| 2270 | * xmlStrncat: |
| 2271 | * @cur: the original xmlChar * array |
| 2272 | * @add: the xmlChar * array added |
| 2273 | * @len: the length of @add |
| 2274 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2275 | * a strncat for array of xmlChar's, it will extend @cur with the len |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | * first bytes of @add. |
| 2277 | * |
| 2278 | * Returns a new xmlChar *, the original @cur is reallocated if needed |
| 2279 | * and should not be freed |
| 2280 | */ |
| 2281 | |
| 2282 | xmlChar * |
| 2283 | xmlStrncat(xmlChar *cur, const xmlChar *add, int len) { |
| 2284 | int size; |
| 2285 | xmlChar *ret; |
| 2286 | |
| 2287 | if ((add == NULL) || (len == 0)) |
| 2288 | return(cur); |
| 2289 | if (cur == NULL) |
| 2290 | return(xmlStrndup(add, len)); |
| 2291 | |
| 2292 | size = xmlStrlen(cur); |
| 2293 | ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar)); |
| 2294 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2295 | xmlErrMemory(NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2296 | return(cur); |
| 2297 | } |
| 2298 | memcpy(&ret[size], add, len * sizeof(xmlChar)); |
| 2299 | ret[size + len] = 0; |
| 2300 | return(ret); |
| 2301 | } |
| 2302 | |
| 2303 | /** |
| 2304 | * xmlStrcat: |
| 2305 | * @cur: the original xmlChar * array |
| 2306 | * @add: the xmlChar * array added |
| 2307 | * |
| 2308 | * a strcat for array of xmlChar's. Since they are supposed to be |
| 2309 | * encoded in UTF-8 or an encoding with 8bit based chars, we assume |
| 2310 | * a termination mark of '0'. |
| 2311 | * |
| 2312 | * Returns a new xmlChar * containing the concatenated string. |
| 2313 | */ |
| 2314 | xmlChar * |
| 2315 | xmlStrcat(xmlChar *cur, const xmlChar *add) { |
| 2316 | const xmlChar *p = add; |
| 2317 | |
| 2318 | if (add == NULL) return(cur); |
| 2319 | if (cur == NULL) |
| 2320 | return(xmlStrdup(add)); |
| 2321 | |
| 2322 | while (*p != 0) p++; /* non input consuming */ |
| 2323 | return(xmlStrncat(cur, add, p - add)); |
| 2324 | } |
| 2325 | |
| 2326 | /************************************************************************ |
| 2327 | * * |
| 2328 | * Commodity functions, cleanup needed ? * |
| 2329 | * * |
| 2330 | ************************************************************************/ |
| 2331 | |
| 2332 | /** |
| 2333 | * areBlanks: |
| 2334 | * @ctxt: an XML parser context |
| 2335 | * @str: a xmlChar * |
| 2336 | * @len: the size of @str |
| 2337 | * |
| 2338 | * Is this a sequence of blank chars that one can ignore ? |
| 2339 | * |
| 2340 | * Returns 1 if ignorable 0 otherwise. |
| 2341 | */ |
| 2342 | |
| 2343 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) { |
| 2344 | int i, ret; |
| 2345 | xmlNodePtr lastChild; |
| 2346 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2347 | /* |
| 2348 | * Don't spend time trying to differentiate them, the same callback is |
| 2349 | * used ! |
| 2350 | */ |
| 2351 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2352 | return(0); |
| 2353 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2354 | /* |
| 2355 | * Check for xml:space value. |
| 2356 | */ |
| 2357 | if (*(ctxt->space) == 1) |
| 2358 | return(0); |
| 2359 | |
| 2360 | /* |
| 2361 | * Check that the string is made of blanks |
| 2362 | */ |
| 2363 | for (i = 0;i < len;i++) |
| 2364 | if (!(IS_BLANK(str[i]))) return(0); |
| 2365 | |
| 2366 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2367 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2368 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2369 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2370 | if (ctxt->myDoc != NULL) { |
| 2371 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2372 | if (ret == 0) return(1); |
| 2373 | if (ret == 1) return(0); |
| 2374 | } |
| 2375 | |
| 2376 | /* |
| 2377 | * Otherwise, heuristic :-\ |
| 2378 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2379 | if (RAW != '<') return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2380 | if ((ctxt->node->children == NULL) && |
| 2381 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2382 | |
| 2383 | lastChild = xmlGetLastChild(ctxt->node); |
| 2384 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2385 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2386 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2387 | } else if (xmlNodeIsText(lastChild)) |
| 2388 | return(0); |
| 2389 | else if ((ctxt->node->children != NULL) && |
| 2390 | (xmlNodeIsText(ctxt->node->children))) |
| 2391 | return(0); |
| 2392 | return(1); |
| 2393 | } |
| 2394 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2395 | /************************************************************************ |
| 2396 | * * |
| 2397 | * Extra stuff for namespace support * |
| 2398 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2399 | * * |
| 2400 | ************************************************************************/ |
| 2401 | |
| 2402 | /** |
| 2403 | * xmlSplitQName: |
| 2404 | * @ctxt: an XML parser context |
| 2405 | * @name: an XML parser context |
| 2406 | * @prefix: a xmlChar ** |
| 2407 | * |
| 2408 | * parse an UTF8 encoded XML qualified name string |
| 2409 | * |
| 2410 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2411 | * |
| 2412 | * [NS 6] Prefix ::= NCName |
| 2413 | * |
| 2414 | * [NS 7] LocalPart ::= NCName |
| 2415 | * |
| 2416 | * Returns the local part, and prefix is updated |
| 2417 | * to get the Prefix if any. |
| 2418 | */ |
| 2419 | |
| 2420 | xmlChar * |
| 2421 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2422 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2423 | xmlChar *buffer = NULL; |
| 2424 | int len = 0; |
| 2425 | int max = XML_MAX_NAMELEN; |
| 2426 | xmlChar *ret = NULL; |
| 2427 | const xmlChar *cur = name; |
| 2428 | int c; |
| 2429 | |
| 2430 | *prefix = NULL; |
| 2431 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2432 | if (cur == NULL) return(NULL); |
| 2433 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2434 | #ifndef XML_XML_NAMESPACE |
| 2435 | /* xml: prefix is not really a namespace */ |
| 2436 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2437 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2438 | return(xmlStrdup(name)); |
| 2439 | #endif |
| 2440 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2441 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2442 | if (cur[0] == ':') |
| 2443 | return(xmlStrdup(name)); |
| 2444 | |
| 2445 | c = *cur++; |
| 2446 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2447 | buf[len++] = c; |
| 2448 | c = *cur++; |
| 2449 | } |
| 2450 | if (len >= max) { |
| 2451 | /* |
| 2452 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2453 | * for the processing speed. |
| 2454 | */ |
| 2455 | max = len * 2; |
| 2456 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2457 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2458 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2459 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2460 | return(NULL); |
| 2461 | } |
| 2462 | memcpy(buffer, buf, len); |
| 2463 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2464 | if (len + 10 > max) { |
| 2465 | max *= 2; |
| 2466 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2467 | max * sizeof(xmlChar)); |
| 2468 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2469 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2470 | return(NULL); |
| 2471 | } |
| 2472 | } |
| 2473 | buffer[len++] = c; |
| 2474 | c = *cur++; |
| 2475 | } |
| 2476 | buffer[len] = 0; |
| 2477 | } |
| 2478 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2479 | /* nasty but well=formed |
| 2480 | if ((c == ':') && (*cur == 0)) { |
| 2481 | return(xmlStrdup(name)); |
| 2482 | } */ |
| 2483 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | if (buffer == NULL) |
| 2485 | ret = xmlStrndup(buf, len); |
| 2486 | else { |
| 2487 | ret = buffer; |
| 2488 | buffer = NULL; |
| 2489 | max = XML_MAX_NAMELEN; |
| 2490 | } |
| 2491 | |
| 2492 | |
| 2493 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2494 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2495 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2496 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 2497 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2498 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2499 | len = 0; |
| 2500 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2501 | /* |
| 2502 | * Check that the first character is proper to start |
| 2503 | * a new name |
| 2504 | */ |
| 2505 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 2506 | ((c >= 0x41) && (c <= 0x5A)) || |
| 2507 | (c == '_') || (c == ':'))) { |
| 2508 | int l; |
| 2509 | int first = CUR_SCHAR(cur, l); |
| 2510 | |
| 2511 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2512 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2513 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2514 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2515 | } |
| 2516 | } |
| 2517 | cur++; |
| 2518 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2519 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 2520 | buf[len++] = c; |
| 2521 | c = *cur++; |
| 2522 | } |
| 2523 | if (len >= max) { |
| 2524 | /* |
| 2525 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2526 | * for the processing speed. |
| 2527 | */ |
| 2528 | max = len * 2; |
| 2529 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2530 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2531 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2532 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2533 | return(NULL); |
| 2534 | } |
| 2535 | memcpy(buffer, buf, len); |
| 2536 | while (c != 0) { /* tested bigname2.xml */ |
| 2537 | if (len + 10 > max) { |
| 2538 | max *= 2; |
| 2539 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2540 | max * sizeof(xmlChar)); |
| 2541 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2542 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2543 | return(NULL); |
| 2544 | } |
| 2545 | } |
| 2546 | buffer[len++] = c; |
| 2547 | c = *cur++; |
| 2548 | } |
| 2549 | buffer[len] = 0; |
| 2550 | } |
| 2551 | |
| 2552 | if (buffer == NULL) |
| 2553 | ret = xmlStrndup(buf, len); |
| 2554 | else { |
| 2555 | ret = buffer; |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | return(ret); |
| 2560 | } |
| 2561 | |
| 2562 | /************************************************************************ |
| 2563 | * * |
| 2564 | * The parser itself * |
| 2565 | * Relates to http://www.w3.org/TR/REC-xml * |
| 2566 | * * |
| 2567 | ************************************************************************/ |
| 2568 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2569 | static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2570 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2571 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2572 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2573 | /** |
| 2574 | * xmlParseName: |
| 2575 | * @ctxt: an XML parser context |
| 2576 | * |
| 2577 | * parse an XML name. |
| 2578 | * |
| 2579 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2580 | * CombiningChar | Extender |
| 2581 | * |
| 2582 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2583 | * |
| 2584 | * [6] Names ::= Name (S Name)* |
| 2585 | * |
| 2586 | * Returns the Name parsed or NULL |
| 2587 | */ |
| 2588 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2589 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2590 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2591 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2592 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2593 | int count = 0; |
| 2594 | |
| 2595 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2596 | |
| 2597 | /* |
| 2598 | * Accelerator for simple ASCII names |
| 2599 | */ |
| 2600 | in = ctxt->input->cur; |
| 2601 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2602 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2603 | (*in == '_') || (*in == ':')) { |
| 2604 | in++; |
| 2605 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2606 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2607 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2608 | (*in == '_') || (*in == '-') || |
| 2609 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2610 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2611 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2612 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2613 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2614 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2615 | ctxt->nbChars += count; |
| 2616 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2617 | if (ret == NULL) |
| 2618 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2619 | return(ret); |
| 2620 | } |
| 2621 | } |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2622 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2623 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2624 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2625 | /** |
| 2626 | * xmlParseNameAndCompare: |
| 2627 | * @ctxt: an XML parser context |
| 2628 | * |
| 2629 | * parse an XML name and compares for match |
| 2630 | * (specialized for endtag parsing) |
| 2631 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2632 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 2633 | * and the name for mismatch |
| 2634 | */ |
| 2635 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2636 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2637 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
| 2638 | const xmlChar *cmp = other; |
| 2639 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2640 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2641 | |
| 2642 | GROW; |
| 2643 | |
| 2644 | in = ctxt->input->cur; |
| 2645 | while (*in != 0 && *in == *cmp) { |
| 2646 | ++in; |
| 2647 | ++cmp; |
| 2648 | } |
| 2649 | if (*cmp == 0 && (*in == '>' || IS_BLANK (*in))) { |
| 2650 | /* success */ |
| 2651 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2652 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2653 | } |
| 2654 | /* failure (or end of input buffer), check with full function */ |
| 2655 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2656 | /* strings coming from the dictionnary direct compare possible */ |
| 2657 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2658 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2659 | } |
| 2660 | return ret; |
| 2661 | } |
| 2662 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2663 | static const xmlChar * |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2664 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2665 | int len = 0, l; |
| 2666 | int c; |
| 2667 | int count = 0; |
| 2668 | |
| 2669 | /* |
| 2670 | * Handler for more complex cases |
| 2671 | */ |
| 2672 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2673 | c = CUR_CHAR(l); |
| 2674 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 2675 | (!IS_LETTER(c) && (c != '_') && |
| 2676 | (c != ':'))) { |
| 2677 | return(NULL); |
| 2678 | } |
| 2679 | |
| 2680 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 2681 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 2682 | (c == '.') || (c == '-') || |
| 2683 | (c == '_') || (c == ':') || |
| 2684 | (IS_COMBINING(c)) || |
| 2685 | (IS_EXTENDER(c)))) { |
| 2686 | if (count++ > 100) { |
| 2687 | count = 0; |
| 2688 | GROW; |
| 2689 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2690 | len += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2691 | NEXTL(l); |
| 2692 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2693 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2694 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | /** |
| 2698 | * xmlParseStringName: |
| 2699 | * @ctxt: an XML parser context |
| 2700 | * @str: a pointer to the string pointer (IN/OUT) |
| 2701 | * |
| 2702 | * parse an XML name. |
| 2703 | * |
| 2704 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2705 | * CombiningChar | Extender |
| 2706 | * |
| 2707 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2708 | * |
| 2709 | * [6] Names ::= Name (S Name)* |
| 2710 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2711 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | * is updated to the current location in the string. |
| 2713 | */ |
| 2714 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2715 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 2717 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2718 | const xmlChar *cur = *str; |
| 2719 | int len = 0, l; |
| 2720 | int c; |
| 2721 | |
| 2722 | c = CUR_SCHAR(cur, l); |
| 2723 | if (!IS_LETTER(c) && (c != '_') && |
| 2724 | (c != ':')) { |
| 2725 | return(NULL); |
| 2726 | } |
| 2727 | |
| 2728 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */ |
| 2729 | (c == '.') || (c == '-') || |
| 2730 | (c == '_') || (c == ':') || |
| 2731 | (IS_COMBINING(c)) || |
| 2732 | (IS_EXTENDER(c))) { |
| 2733 | COPY_BUF(l,buf,len,c); |
| 2734 | cur += l; |
| 2735 | c = CUR_SCHAR(cur, l); |
| 2736 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 2737 | /* |
| 2738 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2739 | * for the processing speed. |
| 2740 | */ |
| 2741 | xmlChar *buffer; |
| 2742 | int max = len * 2; |
| 2743 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2744 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2745 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2746 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | return(NULL); |
| 2748 | } |
| 2749 | memcpy(buffer, buf, len); |
| 2750 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */ |
| 2751 | (c == '.') || (c == '-') || |
| 2752 | (c == '_') || (c == ':') || |
| 2753 | (IS_COMBINING(c)) || |
| 2754 | (IS_EXTENDER(c))) { |
| 2755 | if (len + 10 > max) { |
| 2756 | max *= 2; |
| 2757 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2758 | max * sizeof(xmlChar)); |
| 2759 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2760 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2761 | return(NULL); |
| 2762 | } |
| 2763 | } |
| 2764 | COPY_BUF(l,buffer,len,c); |
| 2765 | cur += l; |
| 2766 | c = CUR_SCHAR(cur, l); |
| 2767 | } |
| 2768 | buffer[len] = 0; |
| 2769 | *str = cur; |
| 2770 | return(buffer); |
| 2771 | } |
| 2772 | } |
| 2773 | *str = cur; |
| 2774 | return(xmlStrndup(buf, len)); |
| 2775 | } |
| 2776 | |
| 2777 | /** |
| 2778 | * xmlParseNmtoken: |
| 2779 | * @ctxt: an XML parser context |
| 2780 | * |
| 2781 | * parse an XML Nmtoken. |
| 2782 | * |
| 2783 | * [7] Nmtoken ::= (NameChar)+ |
| 2784 | * |
| 2785 | * [8] Nmtokens ::= Nmtoken (S Nmtoken)* |
| 2786 | * |
| 2787 | * Returns the Nmtoken parsed or NULL |
| 2788 | */ |
| 2789 | |
| 2790 | xmlChar * |
| 2791 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 2792 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2793 | int len = 0, l; |
| 2794 | int c; |
| 2795 | int count = 0; |
| 2796 | |
| 2797 | GROW; |
| 2798 | c = CUR_CHAR(l); |
| 2799 | |
| 2800 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
| 2801 | (c == '.') || (c == '-') || |
| 2802 | (c == '_') || (c == ':') || |
| 2803 | (IS_COMBINING(c)) || |
| 2804 | (IS_EXTENDER(c))) { |
| 2805 | if (count++ > 100) { |
| 2806 | count = 0; |
| 2807 | GROW; |
| 2808 | } |
| 2809 | COPY_BUF(l,buf,len,c); |
| 2810 | NEXTL(l); |
| 2811 | c = CUR_CHAR(l); |
| 2812 | if (len >= XML_MAX_NAMELEN) { |
| 2813 | /* |
| 2814 | * Okay someone managed to make a huge token, so he's ready to pay |
| 2815 | * for the processing speed. |
| 2816 | */ |
| 2817 | xmlChar *buffer; |
| 2818 | int max = len * 2; |
| 2819 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2820 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2821 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2822 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2823 | return(NULL); |
| 2824 | } |
| 2825 | memcpy(buffer, buf, len); |
| 2826 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
| 2827 | (c == '.') || (c == '-') || |
| 2828 | (c == '_') || (c == ':') || |
| 2829 | (IS_COMBINING(c)) || |
| 2830 | (IS_EXTENDER(c))) { |
| 2831 | if (count++ > 100) { |
| 2832 | count = 0; |
| 2833 | GROW; |
| 2834 | } |
| 2835 | if (len + 10 > max) { |
| 2836 | max *= 2; |
| 2837 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2838 | max * sizeof(xmlChar)); |
| 2839 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2840 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2841 | return(NULL); |
| 2842 | } |
| 2843 | } |
| 2844 | COPY_BUF(l,buffer,len,c); |
| 2845 | NEXTL(l); |
| 2846 | c = CUR_CHAR(l); |
| 2847 | } |
| 2848 | buffer[len] = 0; |
| 2849 | return(buffer); |
| 2850 | } |
| 2851 | } |
| 2852 | if (len == 0) |
| 2853 | return(NULL); |
| 2854 | return(xmlStrndup(buf, len)); |
| 2855 | } |
| 2856 | |
| 2857 | /** |
| 2858 | * xmlParseEntityValue: |
| 2859 | * @ctxt: an XML parser context |
| 2860 | * @orig: if non-NULL store a copy of the original entity value |
| 2861 | * |
| 2862 | * parse a value for ENTITY declarations |
| 2863 | * |
| 2864 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 2865 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 2866 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2867 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2868 | */ |
| 2869 | |
| 2870 | xmlChar * |
| 2871 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 2872 | xmlChar *buf = NULL; |
| 2873 | int len = 0; |
| 2874 | int size = XML_PARSER_BUFFER_SIZE; |
| 2875 | int c, l; |
| 2876 | xmlChar stop; |
| 2877 | xmlChar *ret = NULL; |
| 2878 | const xmlChar *cur = NULL; |
| 2879 | xmlParserInputPtr input; |
| 2880 | |
| 2881 | if (RAW == '"') stop = '"'; |
| 2882 | else if (RAW == '\'') stop = '\''; |
| 2883 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2884 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2885 | return(NULL); |
| 2886 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2887 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2888 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2889 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2890 | return(NULL); |
| 2891 | } |
| 2892 | |
| 2893 | /* |
| 2894 | * The content of the entity definition is copied in a buffer. |
| 2895 | */ |
| 2896 | |
| 2897 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 2898 | input = ctxt->input; |
| 2899 | GROW; |
| 2900 | NEXT; |
| 2901 | c = CUR_CHAR(l); |
| 2902 | /* |
| 2903 | * NOTE: 4.4.5 Included in Literal |
| 2904 | * When a parameter entity reference appears in a literal entity |
| 2905 | * value, ... a single or double quote character in the replacement |
| 2906 | * text is always treated as a normal data character and will not |
| 2907 | * terminate the literal. |
| 2908 | * In practice it means we stop the loop only when back at parsing |
| 2909 | * the initial entity and the quote is found |
| 2910 | */ |
| 2911 | while ((IS_CHAR(c)) && ((c != stop) || /* checked */ |
| 2912 | (ctxt->input != input))) { |
| 2913 | if (len + 5 >= size) { |
| 2914 | size *= 2; |
| 2915 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 2916 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2917 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2918 | return(NULL); |
| 2919 | } |
| 2920 | } |
| 2921 | COPY_BUF(l,buf,len,c); |
| 2922 | NEXTL(l); |
| 2923 | /* |
| 2924 | * Pop-up of finished entities. |
| 2925 | */ |
| 2926 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 2927 | xmlPopInput(ctxt); |
| 2928 | |
| 2929 | GROW; |
| 2930 | c = CUR_CHAR(l); |
| 2931 | if (c == 0) { |
| 2932 | GROW; |
| 2933 | c = CUR_CHAR(l); |
| 2934 | } |
| 2935 | } |
| 2936 | buf[len] = 0; |
| 2937 | |
| 2938 | /* |
| 2939 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 2940 | * reference constructs. Note Charref will be handled in |
| 2941 | * xmlStringDecodeEntities() |
| 2942 | */ |
| 2943 | cur = buf; |
| 2944 | while (*cur != 0) { /* non input consuming */ |
| 2945 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 2946 | xmlChar *name; |
| 2947 | xmlChar tmp = *cur; |
| 2948 | |
| 2949 | cur++; |
| 2950 | name = xmlParseStringName(ctxt, &cur); |
| 2951 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2952 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2953 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2954 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2955 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 2956 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 2957 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2958 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2959 | } |
| 2960 | if (name != NULL) |
| 2961 | xmlFree(name); |
| 2962 | } |
| 2963 | cur++; |
| 2964 | } |
| 2965 | |
| 2966 | /* |
| 2967 | * Then PEReference entities are substituted. |
| 2968 | */ |
| 2969 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2970 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2971 | xmlFree(buf); |
| 2972 | } else { |
| 2973 | NEXT; |
| 2974 | /* |
| 2975 | * NOTE: 4.4.7 Bypassed |
| 2976 | * When a general entity reference appears in the EntityValue in |
| 2977 | * an entity declaration, it is bypassed and left as is. |
| 2978 | * so XML_SUBSTITUTE_REF is not set here. |
| 2979 | */ |
| 2980 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 2981 | 0, 0, 0); |
| 2982 | if (orig != NULL) |
| 2983 | *orig = buf; |
| 2984 | else |
| 2985 | xmlFree(buf); |
| 2986 | } |
| 2987 | |
| 2988 | return(ret); |
| 2989 | } |
| 2990 | |
| 2991 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2992 | * xmlParseAttValueComplex: |
| 2993 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2994 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2995 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2996 | * |
| 2997 | * parse a value for an attribute, this is the fallback function |
| 2998 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2999 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3000 | * |
| 3001 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3002 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3003 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3004 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3005 | xmlChar limit = 0; |
| 3006 | xmlChar *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3007 | int len = 0; |
| 3008 | int buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3009 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3010 | xmlChar *current = NULL; |
| 3011 | xmlEntityPtr ent; |
| 3012 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3013 | if (NXT(0) == '"') { |
| 3014 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3015 | limit = '"'; |
| 3016 | NEXT; |
| 3017 | } else if (NXT(0) == '\'') { |
| 3018 | limit = '\''; |
| 3019 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3020 | NEXT; |
| 3021 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3022 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3023 | return(NULL); |
| 3024 | } |
| 3025 | |
| 3026 | /* |
| 3027 | * allocate a translation buffer. |
| 3028 | */ |
| 3029 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3030 | buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3031 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3032 | |
| 3033 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3034 | * OK loop until we reach one of the ending char or a size limit. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3035 | */ |
| 3036 | c = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3037 | while ((NXT(0) != limit) && /* checked */ |
| 3038 | (c != '<')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3040 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3041 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3042 | if (NXT(1) == '#') { |
| 3043 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3044 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3045 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3046 | if (ctxt->replaceEntities) { |
| 3047 | if (len > buf_size - 10) { |
| 3048 | growBuffer(buf); |
| 3049 | } |
| 3050 | buf[len++] = '&'; |
| 3051 | } else { |
| 3052 | /* |
| 3053 | * The reparsing will be done in xmlStringGetNodeList() |
| 3054 | * called by the attribute() function in SAX.c |
| 3055 | */ |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3056 | if (len > buf_size - 10) { |
| 3057 | growBuffer(buf); |
| 3058 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3059 | buf[len++] = '&'; |
| 3060 | buf[len++] = '#'; |
| 3061 | buf[len++] = '3'; |
| 3062 | buf[len++] = '8'; |
| 3063 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3064 | } |
| 3065 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3066 | if (len > buf_size - 10) { |
| 3067 | growBuffer(buf); |
| 3068 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3069 | len += xmlCopyChar(0, &buf[len], val); |
| 3070 | } |
| 3071 | } else { |
| 3072 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3073 | if ((ent != NULL) && |
| 3074 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 3075 | if (len > buf_size - 10) { |
| 3076 | growBuffer(buf); |
| 3077 | } |
| 3078 | if ((ctxt->replaceEntities == 0) && |
| 3079 | (ent->content[0] == '&')) { |
| 3080 | buf[len++] = '&'; |
| 3081 | buf[len++] = '#'; |
| 3082 | buf[len++] = '3'; |
| 3083 | buf[len++] = '8'; |
| 3084 | buf[len++] = ';'; |
| 3085 | } else { |
| 3086 | buf[len++] = ent->content[0]; |
| 3087 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3088 | } else if ((ent != NULL) && |
| 3089 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3090 | xmlChar *rep; |
| 3091 | |
| 3092 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 3093 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3094 | XML_SUBSTITUTE_REF, |
| 3095 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3096 | if (rep != NULL) { |
| 3097 | current = rep; |
| 3098 | while (*current != 0) { /* non input consuming */ |
| 3099 | buf[len++] = *current++; |
| 3100 | if (len > buf_size - 10) { |
| 3101 | growBuffer(buf); |
| 3102 | } |
| 3103 | } |
| 3104 | xmlFree(rep); |
| 3105 | } |
| 3106 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3107 | if (len > buf_size - 10) { |
| 3108 | growBuffer(buf); |
| 3109 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3110 | if (ent->content != NULL) |
| 3111 | buf[len++] = ent->content[0]; |
| 3112 | } |
| 3113 | } else if (ent != NULL) { |
| 3114 | int i = xmlStrlen(ent->name); |
| 3115 | const xmlChar *cur = ent->name; |
| 3116 | |
| 3117 | /* |
| 3118 | * This may look absurd but is needed to detect |
| 3119 | * entities problems |
| 3120 | */ |
| 3121 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 3122 | (ent->content != NULL)) { |
| 3123 | xmlChar *rep; |
| 3124 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
| 3125 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 3126 | if (rep != NULL) |
| 3127 | xmlFree(rep); |
| 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Just output the reference |
| 3132 | */ |
| 3133 | buf[len++] = '&'; |
| 3134 | if (len > buf_size - i - 10) { |
| 3135 | growBuffer(buf); |
| 3136 | } |
| 3137 | for (;i > 0;i--) |
| 3138 | buf[len++] = *cur++; |
| 3139 | buf[len++] = ';'; |
| 3140 | } |
| 3141 | } |
| 3142 | } else { |
| 3143 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3144 | if ((len != 0) || (!normalize)) { |
| 3145 | if ((!normalize) || (!in_space)) { |
| 3146 | COPY_BUF(l,buf,len,0x20); |
| 3147 | if (len > buf_size - 10) { |
| 3148 | growBuffer(buf); |
| 3149 | } |
| 3150 | } |
| 3151 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3152 | } |
| 3153 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3154 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3155 | COPY_BUF(l,buf,len,c); |
| 3156 | if (len > buf_size - 10) { |
| 3157 | growBuffer(buf); |
| 3158 | } |
| 3159 | } |
| 3160 | NEXTL(l); |
| 3161 | } |
| 3162 | GROW; |
| 3163 | c = CUR_CHAR(l); |
| 3164 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3165 | if ((in_space) && (normalize)) { |
| 3166 | while (buf[len - 1] == 0x20) len--; |
| 3167 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3168 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3169 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3170 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3171 | } else if (RAW != limit) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3172 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 3173 | "AttValue: ' expected\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3174 | } else |
| 3175 | NEXT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3176 | if (attlen != NULL) *attlen = len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3177 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3178 | |
| 3179 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3180 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3181 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3185 | * xmlParseAttValue: |
| 3186 | * @ctxt: an XML parser context |
| 3187 | * |
| 3188 | * parse a value for an attribute |
| 3189 | * Note: the parser won't do substitution of entities here, this |
| 3190 | * will be handled later in xmlStringGetNodeList |
| 3191 | * |
| 3192 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 3193 | * "'" ([^<&'] | Reference)* "'" |
| 3194 | * |
| 3195 | * 3.3.3 Attribute-Value Normalization: |
| 3196 | * Before the value of an attribute is passed to the application or |
| 3197 | * checked for validity, the XML processor must normalize it as follows: |
| 3198 | * - a character reference is processed by appending the referenced |
| 3199 | * character to the attribute value |
| 3200 | * - an entity reference is processed by recursively processing the |
| 3201 | * replacement text of the entity |
| 3202 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 3203 | * appending #x20 to the normalized value, except that only a single |
| 3204 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 3205 | * parsed entity or the literal entity value of an internal parsed entity |
| 3206 | * - other characters are processed by appending them to the normalized value |
| 3207 | * If the declared value is not CDATA, then the XML processor must further |
| 3208 | * process the normalized attribute value by discarding any leading and |
| 3209 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3210 | * (#x20) characters by a single space (#x20) character. |
| 3211 | * All attributes for which no declaration has been read should be treated |
| 3212 | * by a non-validating parser as if declared CDATA. |
| 3213 | * |
| 3214 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3215 | */ |
| 3216 | |
| 3217 | |
| 3218 | xmlChar * |
| 3219 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3220 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3224 | * xmlParseSystemLiteral: |
| 3225 | * @ctxt: an XML parser context |
| 3226 | * |
| 3227 | * parse an XML Literal |
| 3228 | * |
| 3229 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 3230 | * |
| 3231 | * Returns the SystemLiteral parsed or NULL |
| 3232 | */ |
| 3233 | |
| 3234 | xmlChar * |
| 3235 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 3236 | xmlChar *buf = NULL; |
| 3237 | int len = 0; |
| 3238 | int size = XML_PARSER_BUFFER_SIZE; |
| 3239 | int cur, l; |
| 3240 | xmlChar stop; |
| 3241 | int state = ctxt->instate; |
| 3242 | int count = 0; |
| 3243 | |
| 3244 | SHRINK; |
| 3245 | if (RAW == '"') { |
| 3246 | NEXT; |
| 3247 | stop = '"'; |
| 3248 | } else if (RAW == '\'') { |
| 3249 | NEXT; |
| 3250 | stop = '\''; |
| 3251 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3252 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3253 | return(NULL); |
| 3254 | } |
| 3255 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3256 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3257 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3258 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3259 | return(NULL); |
| 3260 | } |
| 3261 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 3262 | cur = CUR_CHAR(l); |
| 3263 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
| 3264 | if (len + 5 >= size) { |
| 3265 | size *= 2; |
| 3266 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3267 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3268 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3269 | ctxt->instate = (xmlParserInputState) state; |
| 3270 | return(NULL); |
| 3271 | } |
| 3272 | } |
| 3273 | count++; |
| 3274 | if (count > 50) { |
| 3275 | GROW; |
| 3276 | count = 0; |
| 3277 | } |
| 3278 | COPY_BUF(l,buf,len,cur); |
| 3279 | NEXTL(l); |
| 3280 | cur = CUR_CHAR(l); |
| 3281 | if (cur == 0) { |
| 3282 | GROW; |
| 3283 | SHRINK; |
| 3284 | cur = CUR_CHAR(l); |
| 3285 | } |
| 3286 | } |
| 3287 | buf[len] = 0; |
| 3288 | ctxt->instate = (xmlParserInputState) state; |
| 3289 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3290 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3291 | } else { |
| 3292 | NEXT; |
| 3293 | } |
| 3294 | return(buf); |
| 3295 | } |
| 3296 | |
| 3297 | /** |
| 3298 | * xmlParsePubidLiteral: |
| 3299 | * @ctxt: an XML parser context |
| 3300 | * |
| 3301 | * parse an XML public literal |
| 3302 | * |
| 3303 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 3304 | * |
| 3305 | * Returns the PubidLiteral parsed or NULL. |
| 3306 | */ |
| 3307 | |
| 3308 | xmlChar * |
| 3309 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 3310 | xmlChar *buf = NULL; |
| 3311 | int len = 0; |
| 3312 | int size = XML_PARSER_BUFFER_SIZE; |
| 3313 | xmlChar cur; |
| 3314 | xmlChar stop; |
| 3315 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3316 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3317 | |
| 3318 | SHRINK; |
| 3319 | if (RAW == '"') { |
| 3320 | NEXT; |
| 3321 | stop = '"'; |
| 3322 | } else if (RAW == '\'') { |
| 3323 | NEXT; |
| 3324 | stop = '\''; |
| 3325 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3326 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3327 | return(NULL); |
| 3328 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3329 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3330 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3331 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3332 | return(NULL); |
| 3333 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3334 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3335 | cur = CUR; |
| 3336 | while ((IS_PUBIDCHAR(cur)) && (cur != stop)) { /* checked */ |
| 3337 | if (len + 1 >= size) { |
| 3338 | size *= 2; |
| 3339 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3340 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3341 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3342 | return(NULL); |
| 3343 | } |
| 3344 | } |
| 3345 | buf[len++] = cur; |
| 3346 | count++; |
| 3347 | if (count > 50) { |
| 3348 | GROW; |
| 3349 | count = 0; |
| 3350 | } |
| 3351 | NEXT; |
| 3352 | cur = CUR; |
| 3353 | if (cur == 0) { |
| 3354 | GROW; |
| 3355 | SHRINK; |
| 3356 | cur = CUR; |
| 3357 | } |
| 3358 | } |
| 3359 | buf[len] = 0; |
| 3360 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3361 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3362 | } else { |
| 3363 | NEXT; |
| 3364 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3365 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3366 | return(buf); |
| 3367 | } |
| 3368 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3369 | void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3370 | /** |
| 3371 | * xmlParseCharData: |
| 3372 | * @ctxt: an XML parser context |
| 3373 | * @cdata: int indicating whether we are within a CDATA section |
| 3374 | * |
| 3375 | * parse a CharData section. |
| 3376 | * if we are within a CDATA section ']]>' marks an end of section. |
| 3377 | * |
| 3378 | * The right angle bracket (>) may be represented using the string ">", |
| 3379 | * and must, for compatibility, be escaped using ">" or a character |
| 3380 | * reference when it appears in the string "]]>" in content, when that |
| 3381 | * string is not marking the end of a CDATA section. |
| 3382 | * |
| 3383 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 3384 | */ |
| 3385 | |
| 3386 | void |
| 3387 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3388 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3389 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3390 | int line = ctxt->input->line; |
| 3391 | int col = ctxt->input->col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3392 | |
| 3393 | SHRINK; |
| 3394 | GROW; |
| 3395 | /* |
| 3396 | * Accelerated common case where input don't need to be |
| 3397 | * modified before passing it to the handler. |
| 3398 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3399 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3400 | in = ctxt->input->cur; |
| 3401 | do { |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3402 | get_more: |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3403 | while (((*in >= 0x20) && (*in != '<') && (*in != ']') && |
| 3404 | (*in != '&') && (*in <= 0x7F)) || (*in == 0x09)) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3405 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3406 | if (*in == 0xA) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3407 | ctxt->input->line++; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3408 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3409 | while (*in == 0xA) { |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3410 | ctxt->input->line++; |
| 3411 | in++; |
| 3412 | } |
| 3413 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3414 | } |
| 3415 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3416 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3417 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3418 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3419 | return; |
| 3420 | } |
| 3421 | in++; |
| 3422 | goto get_more; |
| 3423 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3424 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3425 | if (nbchar > 0) { |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3426 | if ((ctxt->sax->ignorableWhitespace != |
| 3427 | ctxt->sax->characters) && |
| 3428 | (IS_BLANK(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3429 | const xmlChar *tmp = ctxt->input->cur; |
| 3430 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3431 | |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3432 | if (areBlanks(ctxt, tmp, nbchar)) { |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3433 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3434 | tmp, nbchar); |
| 3435 | } else if (ctxt->sax->characters != NULL) |
| 3436 | ctxt->sax->characters(ctxt->userData, |
| 3437 | tmp, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3438 | line = ctxt->input->line; |
| 3439 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3440 | } else { |
| 3441 | if (ctxt->sax->characters != NULL) |
| 3442 | ctxt->sax->characters(ctxt->userData, |
| 3443 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3444 | line = ctxt->input->line; |
| 3445 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3446 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3447 | } |
| 3448 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3449 | if (*in == 0xD) { |
| 3450 | in++; |
| 3451 | if (*in == 0xA) { |
| 3452 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3453 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3454 | ctxt->input->line++; |
| 3455 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3456 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3457 | in--; |
| 3458 | } |
| 3459 | if (*in == '<') { |
| 3460 | return; |
| 3461 | } |
| 3462 | if (*in == '&') { |
| 3463 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3464 | } |
| 3465 | SHRINK; |
| 3466 | GROW; |
| 3467 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 3468 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3469 | nbchar = 0; |
| 3470 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3471 | ctxt->input->line = line; |
| 3472 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3473 | xmlParseCharDataComplex(ctxt, cdata); |
| 3474 | } |
| 3475 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3476 | /** |
| 3477 | * xmlParseCharDataComplex: |
| 3478 | * @ctxt: an XML parser context |
| 3479 | * @cdata: int indicating whether we are within a CDATA section |
| 3480 | * |
| 3481 | * parse a CharData section.this is the fallback function |
| 3482 | * of xmlParseCharData() when the parsing requires handling |
| 3483 | * of non-ASCII characters. |
| 3484 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3485 | void |
| 3486 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3487 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 3488 | int nbchar = 0; |
| 3489 | int cur, l; |
| 3490 | int count = 0; |
| 3491 | |
| 3492 | SHRINK; |
| 3493 | GROW; |
| 3494 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3495 | while ((cur != '<') && /* checked */ |
| 3496 | (cur != '&') && |
| 3497 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3498 | if ((cur == ']') && (NXT(1) == ']') && |
| 3499 | (NXT(2) == '>')) { |
| 3500 | if (cdata) break; |
| 3501 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3502 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3503 | } |
| 3504 | } |
| 3505 | COPY_BUF(l,buf,nbchar,cur); |
| 3506 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3507 | buf[nbchar] = 0; |
| 3508 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3509 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3510 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3511 | */ |
| 3512 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 3513 | if (areBlanks(ctxt, buf, nbchar)) { |
| 3514 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3515 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3516 | buf, nbchar); |
| 3517 | } else { |
| 3518 | if (ctxt->sax->characters != NULL) |
| 3519 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3520 | } |
| 3521 | } |
| 3522 | nbchar = 0; |
| 3523 | } |
| 3524 | count++; |
| 3525 | if (count > 50) { |
| 3526 | GROW; |
| 3527 | count = 0; |
| 3528 | } |
| 3529 | NEXTL(l); |
| 3530 | cur = CUR_CHAR(l); |
| 3531 | } |
| 3532 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3533 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3534 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3535 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | */ |
| 3537 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 3538 | if (areBlanks(ctxt, buf, nbchar)) { |
| 3539 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3540 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 3541 | } else { |
| 3542 | if (ctxt->sax->characters != NULL) |
| 3543 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3544 | } |
| 3545 | } |
| 3546 | } |
| 3547 | } |
| 3548 | |
| 3549 | /** |
| 3550 | * xmlParseExternalID: |
| 3551 | * @ctxt: an XML parser context |
| 3552 | * @publicID: a xmlChar** receiving PubidLiteral |
| 3553 | * @strict: indicate whether we should restrict parsing to only |
| 3554 | * production [75], see NOTE below |
| 3555 | * |
| 3556 | * Parse an External ID or a Public ID |
| 3557 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3558 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3560 | * |
| 3561 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 3562 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3563 | * |
| 3564 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 3565 | * |
| 3566 | * Returns the function returns SystemLiteral and in the second |
| 3567 | * case publicID receives PubidLiteral, is strict is off |
| 3568 | * it is possible to return NULL and have publicID set. |
| 3569 | */ |
| 3570 | |
| 3571 | xmlChar * |
| 3572 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 3573 | xmlChar *URI = NULL; |
| 3574 | |
| 3575 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 3576 | |
| 3577 | *publicID = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3578 | if ((RAW == 'S') && (NXT(1) == 'Y') && |
| 3579 | (NXT(2) == 'S') && (NXT(3) == 'T') && |
| 3580 | (NXT(4) == 'E') && (NXT(5) == 'M')) { |
| 3581 | SKIP(6); |
| 3582 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3583 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3584 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3585 | } |
| 3586 | SKIP_BLANKS; |
| 3587 | URI = xmlParseSystemLiteral(ctxt); |
| 3588 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3589 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3590 | } |
| 3591 | } else if ((RAW == 'P') && (NXT(1) == 'U') && |
| 3592 | (NXT(2) == 'B') && (NXT(3) == 'L') && |
| 3593 | (NXT(4) == 'I') && (NXT(5) == 'C')) { |
| 3594 | SKIP(6); |
| 3595 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3596 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3597 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3598 | } |
| 3599 | SKIP_BLANKS; |
| 3600 | *publicID = xmlParsePubidLiteral(ctxt); |
| 3601 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3602 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3603 | } |
| 3604 | if (strict) { |
| 3605 | /* |
| 3606 | * We don't handle [83] so "S SystemLiteral" is required. |
| 3607 | */ |
| 3608 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3609 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3610 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3611 | } |
| 3612 | } else { |
| 3613 | /* |
| 3614 | * We handle [83] so we return immediately, if |
| 3615 | * "S SystemLiteral" is not detected. From a purely parsing |
| 3616 | * point of view that's a nice mess. |
| 3617 | */ |
| 3618 | const xmlChar *ptr; |
| 3619 | GROW; |
| 3620 | |
| 3621 | ptr = CUR_PTR; |
| 3622 | if (!IS_BLANK(*ptr)) return(NULL); |
| 3623 | |
| 3624 | while (IS_BLANK(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
| 3625 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 3626 | } |
| 3627 | SKIP_BLANKS; |
| 3628 | URI = xmlParseSystemLiteral(ctxt); |
| 3629 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3630 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3631 | } |
| 3632 | } |
| 3633 | return(URI); |
| 3634 | } |
| 3635 | |
| 3636 | /** |
| 3637 | * xmlParseComment: |
| 3638 | * @ctxt: an XML parser context |
| 3639 | * |
| 3640 | * Skip an XML (SGML) comment <!-- .... --> |
| 3641 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 3642 | * must not occur within comments. " |
| 3643 | * |
| 3644 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 3645 | */ |
| 3646 | void |
| 3647 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 3648 | xmlChar *buf = NULL; |
| 3649 | int len; |
| 3650 | int size = XML_PARSER_BUFFER_SIZE; |
| 3651 | int q, ql; |
| 3652 | int r, rl; |
| 3653 | int cur, l; |
| 3654 | xmlParserInputState state; |
| 3655 | xmlParserInputPtr input = ctxt->input; |
| 3656 | int count = 0; |
| 3657 | |
| 3658 | /* |
| 3659 | * Check that there is a comment right here. |
| 3660 | */ |
| 3661 | if ((RAW != '<') || (NXT(1) != '!') || |
| 3662 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
| 3663 | |
| 3664 | state = ctxt->instate; |
| 3665 | ctxt->instate = XML_PARSER_COMMENT; |
| 3666 | SHRINK; |
| 3667 | SKIP(4); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3668 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3669 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3670 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3671 | ctxt->instate = state; |
| 3672 | return; |
| 3673 | } |
| 3674 | q = CUR_CHAR(ql); |
| 3675 | NEXTL(ql); |
| 3676 | r = CUR_CHAR(rl); |
| 3677 | NEXTL(rl); |
| 3678 | cur = CUR_CHAR(l); |
| 3679 | len = 0; |
| 3680 | while (IS_CHAR(cur) && /* checked */ |
| 3681 | ((cur != '>') || |
| 3682 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3683 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3684 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3685 | } |
| 3686 | if (len + 5 >= size) { |
| 3687 | size *= 2; |
| 3688 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3689 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3690 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3691 | ctxt->instate = state; |
| 3692 | return; |
| 3693 | } |
| 3694 | } |
| 3695 | COPY_BUF(ql,buf,len,q); |
| 3696 | q = r; |
| 3697 | ql = rl; |
| 3698 | r = cur; |
| 3699 | rl = l; |
| 3700 | |
| 3701 | count++; |
| 3702 | if (count > 50) { |
| 3703 | GROW; |
| 3704 | count = 0; |
| 3705 | } |
| 3706 | NEXTL(l); |
| 3707 | cur = CUR_CHAR(l); |
| 3708 | if (cur == 0) { |
| 3709 | SHRINK; |
| 3710 | GROW; |
| 3711 | cur = CUR_CHAR(l); |
| 3712 | } |
| 3713 | } |
| 3714 | buf[len] = 0; |
| 3715 | if (!IS_CHAR(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3716 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3717 | "Comment not terminated \n<!--%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3718 | xmlFree(buf); |
| 3719 | } else { |
| 3720 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3721 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3722 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3723 | } |
| 3724 | NEXT; |
| 3725 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3726 | (!ctxt->disableSAX)) |
| 3727 | ctxt->sax->comment(ctxt->userData, buf); |
| 3728 | xmlFree(buf); |
| 3729 | } |
| 3730 | ctxt->instate = state; |
| 3731 | } |
| 3732 | |
| 3733 | /** |
| 3734 | * xmlParsePITarget: |
| 3735 | * @ctxt: an XML parser context |
| 3736 | * |
| 3737 | * parse the name of a PI |
| 3738 | * |
| 3739 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 3740 | * |
| 3741 | * Returns the PITarget name or NULL |
| 3742 | */ |
| 3743 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3744 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3745 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3746 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3747 | |
| 3748 | name = xmlParseName(ctxt); |
| 3749 | if ((name != NULL) && |
| 3750 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 3751 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 3752 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 3753 | int i; |
| 3754 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 3755 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3756 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3757 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3758 | return(name); |
| 3759 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3760 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3761 | return(name); |
| 3762 | } |
| 3763 | for (i = 0;;i++) { |
| 3764 | if (xmlW3CPIs[i] == NULL) break; |
| 3765 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 3766 | return(name); |
| 3767 | } |
| 3768 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) { |
| 3769 | ctxt->errNo = XML_ERR_RESERVED_XML_NAME; |
| 3770 | ctxt->sax->warning(ctxt->userData, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3771 | "xmlParsePITarget: invalid name prefix 'xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | return(name); |
| 3775 | } |
| 3776 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3777 | #ifdef LIBXML_CATALOG_ENABLED |
| 3778 | /** |
| 3779 | * xmlParseCatalogPI: |
| 3780 | * @ctxt: an XML parser context |
| 3781 | * @catalog: the PI value string |
| 3782 | * |
| 3783 | * parse an XML Catalog Processing Instruction. |
| 3784 | * |
| 3785 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 3786 | * |
| 3787 | * Occurs only if allowed by the user and if happening in the Misc |
| 3788 | * part of the document before any doctype informations |
| 3789 | * This will add the given catalog to the parsing context in order |
| 3790 | * to be used if there is a resolution need further down in the document |
| 3791 | */ |
| 3792 | |
| 3793 | static void |
| 3794 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 3795 | xmlChar *URL = NULL; |
| 3796 | const xmlChar *tmp, *base; |
| 3797 | xmlChar marker; |
| 3798 | |
| 3799 | tmp = catalog; |
| 3800 | while (IS_BLANK(*tmp)) tmp++; |
| 3801 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 3802 | goto error; |
| 3803 | tmp += 7; |
| 3804 | while (IS_BLANK(*tmp)) tmp++; |
| 3805 | if (*tmp != '=') { |
| 3806 | return; |
| 3807 | } |
| 3808 | tmp++; |
| 3809 | while (IS_BLANK(*tmp)) tmp++; |
| 3810 | marker = *tmp; |
| 3811 | if ((marker != '\'') && (marker != '"')) |
| 3812 | goto error; |
| 3813 | tmp++; |
| 3814 | base = tmp; |
| 3815 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 3816 | if (*tmp == 0) |
| 3817 | goto error; |
| 3818 | URL = xmlStrndup(base, tmp - base); |
| 3819 | tmp++; |
| 3820 | while (IS_BLANK(*tmp)) tmp++; |
| 3821 | if (*tmp != 0) |
| 3822 | goto error; |
| 3823 | |
| 3824 | if (URL != NULL) { |
| 3825 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 3826 | xmlFree(URL); |
| 3827 | } |
| 3828 | return; |
| 3829 | |
| 3830 | error: |
| 3831 | ctxt->errNo = XML_WAR_CATALOG_PI; |
| 3832 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 3833 | ctxt->sax->warning(ctxt->userData, |
| 3834 | "Catalog PI syntax error: %s\n", catalog); |
| 3835 | if (URL != NULL) |
| 3836 | xmlFree(URL); |
| 3837 | } |
| 3838 | #endif |
| 3839 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3840 | /** |
| 3841 | * xmlParsePI: |
| 3842 | * @ctxt: an XML parser context |
| 3843 | * |
| 3844 | * parse an XML Processing Instruction. |
| 3845 | * |
| 3846 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 3847 | * |
| 3848 | * The processing is transfered to SAX once parsed. |
| 3849 | */ |
| 3850 | |
| 3851 | void |
| 3852 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 3853 | xmlChar *buf = NULL; |
| 3854 | int len = 0; |
| 3855 | int size = XML_PARSER_BUFFER_SIZE; |
| 3856 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3857 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3858 | xmlParserInputState state; |
| 3859 | int count = 0; |
| 3860 | |
| 3861 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 3862 | xmlParserInputPtr input = ctxt->input; |
| 3863 | state = ctxt->instate; |
| 3864 | ctxt->instate = XML_PARSER_PI; |
| 3865 | /* |
| 3866 | * this is a Processing Instruction. |
| 3867 | */ |
| 3868 | SKIP(2); |
| 3869 | SHRINK; |
| 3870 | |
| 3871 | /* |
| 3872 | * Parse the target name and check for special support like |
| 3873 | * namespace. |
| 3874 | */ |
| 3875 | target = xmlParsePITarget(ctxt); |
| 3876 | if (target != NULL) { |
| 3877 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 3878 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3879 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3880 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3881 | } |
| 3882 | SKIP(2); |
| 3883 | |
| 3884 | /* |
| 3885 | * SAX: PI detected. |
| 3886 | */ |
| 3887 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3888 | (ctxt->sax->processingInstruction != NULL)) |
| 3889 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3890 | target, NULL); |
| 3891 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3894 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3895 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3896 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3897 | ctxt->instate = state; |
| 3898 | return; |
| 3899 | } |
| 3900 | cur = CUR; |
| 3901 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3902 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3903 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3904 | } |
| 3905 | SKIP_BLANKS; |
| 3906 | cur = CUR_CHAR(l); |
| 3907 | while (IS_CHAR(cur) && /* checked */ |
| 3908 | ((cur != '?') || (NXT(1) != '>'))) { |
| 3909 | if (len + 5 >= size) { |
| 3910 | size *= 2; |
| 3911 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3912 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3913 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3914 | ctxt->instate = state; |
| 3915 | return; |
| 3916 | } |
| 3917 | } |
| 3918 | count++; |
| 3919 | if (count > 50) { |
| 3920 | GROW; |
| 3921 | count = 0; |
| 3922 | } |
| 3923 | COPY_BUF(l,buf,len,cur); |
| 3924 | NEXTL(l); |
| 3925 | cur = CUR_CHAR(l); |
| 3926 | if (cur == 0) { |
| 3927 | SHRINK; |
| 3928 | GROW; |
| 3929 | cur = CUR_CHAR(l); |
| 3930 | } |
| 3931 | } |
| 3932 | buf[len] = 0; |
| 3933 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3934 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 3935 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3936 | } else { |
| 3937 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3938 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3939 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | } |
| 3941 | SKIP(2); |
| 3942 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3943 | #ifdef LIBXML_CATALOG_ENABLED |
| 3944 | if (((state == XML_PARSER_MISC) || |
| 3945 | (state == XML_PARSER_START)) && |
| 3946 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 3947 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 3948 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 3949 | (allow == XML_CATA_ALLOW_ALL)) |
| 3950 | xmlParseCatalogPI(ctxt, buf); |
| 3951 | } |
| 3952 | #endif |
| 3953 | |
| 3954 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3955 | /* |
| 3956 | * SAX: PI detected. |
| 3957 | */ |
| 3958 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3959 | (ctxt->sax->processingInstruction != NULL)) |
| 3960 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3961 | target, buf); |
| 3962 | } |
| 3963 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3964 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3965 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3966 | } |
| 3967 | ctxt->instate = state; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | /** |
| 3972 | * xmlParseNotationDecl: |
| 3973 | * @ctxt: an XML parser context |
| 3974 | * |
| 3975 | * parse a notation declaration |
| 3976 | * |
| 3977 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 3978 | * |
| 3979 | * Hence there is actually 3 choices: |
| 3980 | * 'PUBLIC' S PubidLiteral |
| 3981 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3982 | * and 'SYSTEM' S SystemLiteral |
| 3983 | * |
| 3984 | * See the NOTE on xmlParseExternalID(). |
| 3985 | */ |
| 3986 | |
| 3987 | void |
| 3988 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3989 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3990 | xmlChar *Pubid; |
| 3991 | xmlChar *Systemid; |
| 3992 | |
| 3993 | if ((RAW == '<') && (NXT(1) == '!') && |
| 3994 | (NXT(2) == 'N') && (NXT(3) == 'O') && |
| 3995 | (NXT(4) == 'T') && (NXT(5) == 'A') && |
| 3996 | (NXT(6) == 'T') && (NXT(7) == 'I') && |
| 3997 | (NXT(8) == 'O') && (NXT(9) == 'N')) { |
| 3998 | xmlParserInputPtr input = ctxt->input; |
| 3999 | SHRINK; |
| 4000 | SKIP(10); |
| 4001 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4002 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4003 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4004 | return; |
| 4005 | } |
| 4006 | SKIP_BLANKS; |
| 4007 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4008 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4009 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4010 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | } |
| 4013 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4014 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4015 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4016 | return; |
| 4017 | } |
| 4018 | SKIP_BLANKS; |
| 4019 | |
| 4020 | /* |
| 4021 | * Parse the IDs. |
| 4022 | */ |
| 4023 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 4024 | SKIP_BLANKS; |
| 4025 | |
| 4026 | if (RAW == '>') { |
| 4027 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4028 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4029 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4030 | } |
| 4031 | NEXT; |
| 4032 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4033 | (ctxt->sax->notationDecl != NULL)) |
| 4034 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 4035 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4036 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | if (Systemid != NULL) xmlFree(Systemid); |
| 4039 | if (Pubid != NULL) xmlFree(Pubid); |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /** |
| 4044 | * xmlParseEntityDecl: |
| 4045 | * @ctxt: an XML parser context |
| 4046 | * |
| 4047 | * parse <!ENTITY declarations |
| 4048 | * |
| 4049 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 4050 | * |
| 4051 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 4052 | * |
| 4053 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 4054 | * |
| 4055 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 4056 | * |
| 4057 | * [74] PEDef ::= EntityValue | ExternalID |
| 4058 | * |
| 4059 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 4060 | * |
| 4061 | * [ VC: Notation Declared ] |
| 4062 | * The Name must match the declared name of a notation. |
| 4063 | */ |
| 4064 | |
| 4065 | void |
| 4066 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4067 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4068 | xmlChar *value = NULL; |
| 4069 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4070 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4071 | int isParameter = 0; |
| 4072 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4073 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4074 | |
| 4075 | GROW; |
| 4076 | if ((RAW == '<') && (NXT(1) == '!') && |
| 4077 | (NXT(2) == 'E') && (NXT(3) == 'N') && |
| 4078 | (NXT(4) == 'T') && (NXT(5) == 'I') && |
| 4079 | (NXT(6) == 'T') && (NXT(7) == 'Y')) { |
| 4080 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4081 | SHRINK; |
| 4082 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4083 | skipped = SKIP_BLANKS; |
| 4084 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4085 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4086 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4088 | |
| 4089 | if (RAW == '%') { |
| 4090 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4091 | skipped = SKIP_BLANKS; |
| 4092 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4093 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4094 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4095 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4096 | isParameter = 1; |
| 4097 | } |
| 4098 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4099 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4100 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4101 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4102 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4103 | return; |
| 4104 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4105 | skipped = SKIP_BLANKS; |
| 4106 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4107 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4108 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4109 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4110 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4111 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4112 | /* |
| 4113 | * handle the various case of definitions... |
| 4114 | */ |
| 4115 | if (isParameter) { |
| 4116 | if ((RAW == '"') || (RAW == '\'')) { |
| 4117 | value = xmlParseEntityValue(ctxt, &orig); |
| 4118 | if (value) { |
| 4119 | if ((ctxt->sax != NULL) && |
| 4120 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4121 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4122 | XML_INTERNAL_PARAMETER_ENTITY, |
| 4123 | NULL, NULL, value); |
| 4124 | } |
| 4125 | } else { |
| 4126 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 4127 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4128 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4129 | } |
| 4130 | if (URI) { |
| 4131 | xmlURIPtr uri; |
| 4132 | |
| 4133 | uri = xmlParseURI((const char *) URI); |
| 4134 | if (uri == NULL) { |
| 4135 | ctxt->errNo = XML_ERR_INVALID_URI; |
| 4136 | if ((ctxt->sax != NULL) && |
| 4137 | (!ctxt->disableSAX) && |
| 4138 | (ctxt->sax->error != NULL)) |
| 4139 | ctxt->sax->error(ctxt->userData, |
| 4140 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4141 | /* |
| 4142 | * This really ought to be a well formedness error |
| 4143 | * but the XML Core WG decided otherwise c.f. issue |
| 4144 | * E26 of the XML erratas. |
| 4145 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4146 | } else { |
| 4147 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4148 | /* |
| 4149 | * Okay this is foolish to block those but not |
| 4150 | * invalid URIs. |
| 4151 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4152 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4153 | } else { |
| 4154 | if ((ctxt->sax != NULL) && |
| 4155 | (!ctxt->disableSAX) && |
| 4156 | (ctxt->sax->entityDecl != NULL)) |
| 4157 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4158 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 4159 | literal, URI, NULL); |
| 4160 | } |
| 4161 | xmlFreeURI(uri); |
| 4162 | } |
| 4163 | } |
| 4164 | } |
| 4165 | } else { |
| 4166 | if ((RAW == '"') || (RAW == '\'')) { |
| 4167 | value = xmlParseEntityValue(ctxt, &orig); |
| 4168 | if ((ctxt->sax != NULL) && |
| 4169 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4170 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4171 | XML_INTERNAL_GENERAL_ENTITY, |
| 4172 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4173 | /* |
| 4174 | * For expat compatibility in SAX mode. |
| 4175 | */ |
| 4176 | if ((ctxt->myDoc == NULL) || |
| 4177 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 4178 | if (ctxt->myDoc == NULL) { |
| 4179 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 4180 | } |
| 4181 | if (ctxt->myDoc->intSubset == NULL) |
| 4182 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 4183 | BAD_CAST "fake", NULL, NULL); |
| 4184 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4185 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 4186 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4187 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4188 | } else { |
| 4189 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 4190 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4191 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4192 | } |
| 4193 | if (URI) { |
| 4194 | xmlURIPtr uri; |
| 4195 | |
| 4196 | uri = xmlParseURI((const char *)URI); |
| 4197 | if (uri == NULL) { |
| 4198 | ctxt->errNo = XML_ERR_INVALID_URI; |
| 4199 | if ((ctxt->sax != NULL) && |
| 4200 | (!ctxt->disableSAX) && |
| 4201 | (ctxt->sax->error != NULL)) |
| 4202 | ctxt->sax->error(ctxt->userData, |
| 4203 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4204 | /* |
| 4205 | * This really ought to be a well formedness error |
| 4206 | * but the XML Core WG decided otherwise c.f. issue |
| 4207 | * E26 of the XML erratas. |
| 4208 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | } else { |
| 4210 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4211 | /* |
| 4212 | * Okay this is foolish to block those but not |
| 4213 | * invalid URIs. |
| 4214 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4215 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4216 | } |
| 4217 | xmlFreeURI(uri); |
| 4218 | } |
| 4219 | } |
| 4220 | if ((RAW != '>') && (!IS_BLANK(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4221 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4222 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4223 | } |
| 4224 | SKIP_BLANKS; |
| 4225 | if ((RAW == 'N') && (NXT(1) == 'D') && |
| 4226 | (NXT(2) == 'A') && (NXT(3) == 'T') && |
| 4227 | (NXT(4) == 'A')) { |
| 4228 | SKIP(5); |
| 4229 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4230 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4231 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4232 | } |
| 4233 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4234 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4235 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4236 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 4237 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 4238 | literal, URI, ndata); |
| 4239 | } else { |
| 4240 | if ((ctxt->sax != NULL) && |
| 4241 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4242 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4243 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4244 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4245 | /* |
| 4246 | * For expat compatibility in SAX mode. |
| 4247 | * assuming the entity repalcement was asked for |
| 4248 | */ |
| 4249 | if ((ctxt->replaceEntities != 0) && |
| 4250 | ((ctxt->myDoc == NULL) || |
| 4251 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 4252 | if (ctxt->myDoc == NULL) { |
| 4253 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 4254 | } |
| 4255 | |
| 4256 | if (ctxt->myDoc->intSubset == NULL) |
| 4257 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 4258 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4259 | xmlSAX2EntityDecl(ctxt, name, |
| 4260 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4261 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4262 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4263 | } |
| 4264 | } |
| 4265 | } |
| 4266 | SKIP_BLANKS; |
| 4267 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4268 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4269 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4270 | } else { |
| 4271 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4272 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4273 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4274 | } |
| 4275 | NEXT; |
| 4276 | } |
| 4277 | if (orig != NULL) { |
| 4278 | /* |
| 4279 | * Ugly mechanism to save the raw entity value. |
| 4280 | */ |
| 4281 | xmlEntityPtr cur = NULL; |
| 4282 | |
| 4283 | if (isParameter) { |
| 4284 | if ((ctxt->sax != NULL) && |
| 4285 | (ctxt->sax->getParameterEntity != NULL)) |
| 4286 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 4287 | } else { |
| 4288 | if ((ctxt->sax != NULL) && |
| 4289 | (ctxt->sax->getEntity != NULL)) |
| 4290 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4291 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4292 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4293 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4294 | } |
| 4295 | if (cur != NULL) { |
| 4296 | if (cur->orig != NULL) |
| 4297 | xmlFree(orig); |
| 4298 | else |
| 4299 | cur->orig = orig; |
| 4300 | } else |
| 4301 | xmlFree(orig); |
| 4302 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4303 | if (value != NULL) xmlFree(value); |
| 4304 | if (URI != NULL) xmlFree(URI); |
| 4305 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | /** |
| 4310 | * xmlParseDefaultDecl: |
| 4311 | * @ctxt: an XML parser context |
| 4312 | * @value: Receive a possible fixed default value for the attribute |
| 4313 | * |
| 4314 | * Parse an attribute default declaration |
| 4315 | * |
| 4316 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 4317 | * |
| 4318 | * [ VC: Required Attribute ] |
| 4319 | * if the default declaration is the keyword #REQUIRED, then the |
| 4320 | * attribute must be specified for all elements of the type in the |
| 4321 | * attribute-list declaration. |
| 4322 | * |
| 4323 | * [ VC: Attribute Default Legal ] |
| 4324 | * The declared default value must meet the lexical constraints of |
| 4325 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 4326 | * |
| 4327 | * [ VC: Fixed Attribute Default ] |
| 4328 | * if an attribute has a default value declared with the #FIXED |
| 4329 | * keyword, instances of that attribute must match the default value. |
| 4330 | * |
| 4331 | * [ WFC: No < in Attribute Values ] |
| 4332 | * handled in xmlParseAttValue() |
| 4333 | * |
| 4334 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 4335 | * or XML_ATTRIBUTE_FIXED. |
| 4336 | */ |
| 4337 | |
| 4338 | int |
| 4339 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 4340 | int val; |
| 4341 | xmlChar *ret; |
| 4342 | |
| 4343 | *value = NULL; |
| 4344 | if ((RAW == '#') && (NXT(1) == 'R') && |
| 4345 | (NXT(2) == 'E') && (NXT(3) == 'Q') && |
| 4346 | (NXT(4) == 'U') && (NXT(5) == 'I') && |
| 4347 | (NXT(6) == 'R') && (NXT(7) == 'E') && |
| 4348 | (NXT(8) == 'D')) { |
| 4349 | SKIP(9); |
| 4350 | return(XML_ATTRIBUTE_REQUIRED); |
| 4351 | } |
| 4352 | if ((RAW == '#') && (NXT(1) == 'I') && |
| 4353 | (NXT(2) == 'M') && (NXT(3) == 'P') && |
| 4354 | (NXT(4) == 'L') && (NXT(5) == 'I') && |
| 4355 | (NXT(6) == 'E') && (NXT(7) == 'D')) { |
| 4356 | SKIP(8); |
| 4357 | return(XML_ATTRIBUTE_IMPLIED); |
| 4358 | } |
| 4359 | val = XML_ATTRIBUTE_NONE; |
| 4360 | if ((RAW == '#') && (NXT(1) == 'F') && |
| 4361 | (NXT(2) == 'I') && (NXT(3) == 'X') && |
| 4362 | (NXT(4) == 'E') && (NXT(5) == 'D')) { |
| 4363 | SKIP(6); |
| 4364 | val = XML_ATTRIBUTE_FIXED; |
| 4365 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4366 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4367 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4368 | } |
| 4369 | SKIP_BLANKS; |
| 4370 | } |
| 4371 | ret = xmlParseAttValue(ctxt); |
| 4372 | ctxt->instate = XML_PARSER_DTD; |
| 4373 | if (ret == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4374 | xmlFatalErrMsg(ctxt, ctxt->errNo, |
| 4375 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | } else |
| 4377 | *value = ret; |
| 4378 | return(val); |
| 4379 | } |
| 4380 | |
| 4381 | /** |
| 4382 | * xmlParseNotationType: |
| 4383 | * @ctxt: an XML parser context |
| 4384 | * |
| 4385 | * parse an Notation attribute type. |
| 4386 | * |
| 4387 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 4388 | * |
| 4389 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4390 | * |
| 4391 | * [ VC: Notation Attributes ] |
| 4392 | * Values of this type must match one of the notation names included |
| 4393 | * in the declaration; all notation names in the declaration must be declared. |
| 4394 | * |
| 4395 | * Returns: the notation attribute tree built while parsing |
| 4396 | */ |
| 4397 | |
| 4398 | xmlEnumerationPtr |
| 4399 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4400 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4401 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4402 | |
| 4403 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4404 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4405 | return(NULL); |
| 4406 | } |
| 4407 | SHRINK; |
| 4408 | do { |
| 4409 | NEXT; |
| 4410 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4411 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4412 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4413 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4414 | "Name expected in NOTATION declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4415 | return(ret); |
| 4416 | } |
| 4417 | cur = xmlCreateEnumeration(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4418 | if (cur == NULL) return(ret); |
| 4419 | if (last == NULL) ret = last = cur; |
| 4420 | else { |
| 4421 | last->next = cur; |
| 4422 | last = cur; |
| 4423 | } |
| 4424 | SKIP_BLANKS; |
| 4425 | } while (RAW == '|'); |
| 4426 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4427 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4428 | if ((last != NULL) && (last != ret)) |
| 4429 | xmlFreeEnumeration(last); |
| 4430 | return(ret); |
| 4431 | } |
| 4432 | NEXT; |
| 4433 | return(ret); |
| 4434 | } |
| 4435 | |
| 4436 | /** |
| 4437 | * xmlParseEnumerationType: |
| 4438 | * @ctxt: an XML parser context |
| 4439 | * |
| 4440 | * parse an Enumeration attribute type. |
| 4441 | * |
| 4442 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 4443 | * |
| 4444 | * [ VC: Enumeration ] |
| 4445 | * Values of this type must match one of the Nmtoken tokens in |
| 4446 | * the declaration |
| 4447 | * |
| 4448 | * Returns: the enumeration attribute tree built while parsing |
| 4449 | */ |
| 4450 | |
| 4451 | xmlEnumerationPtr |
| 4452 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 4453 | xmlChar *name; |
| 4454 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4455 | |
| 4456 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4457 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4458 | return(NULL); |
| 4459 | } |
| 4460 | SHRINK; |
| 4461 | do { |
| 4462 | NEXT; |
| 4463 | SKIP_BLANKS; |
| 4464 | name = xmlParseNmtoken(ctxt); |
| 4465 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4466 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4467 | return(ret); |
| 4468 | } |
| 4469 | cur = xmlCreateEnumeration(name); |
| 4470 | xmlFree(name); |
| 4471 | if (cur == NULL) return(ret); |
| 4472 | if (last == NULL) ret = last = cur; |
| 4473 | else { |
| 4474 | last->next = cur; |
| 4475 | last = cur; |
| 4476 | } |
| 4477 | SKIP_BLANKS; |
| 4478 | } while (RAW == '|'); |
| 4479 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4480 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4481 | return(ret); |
| 4482 | } |
| 4483 | NEXT; |
| 4484 | return(ret); |
| 4485 | } |
| 4486 | |
| 4487 | /** |
| 4488 | * xmlParseEnumeratedType: |
| 4489 | * @ctxt: an XML parser context |
| 4490 | * @tree: the enumeration tree built while parsing |
| 4491 | * |
| 4492 | * parse an Enumerated attribute type. |
| 4493 | * |
| 4494 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 4495 | * |
| 4496 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4497 | * |
| 4498 | * |
| 4499 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 4500 | */ |
| 4501 | |
| 4502 | int |
| 4503 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 4504 | if ((RAW == 'N') && (NXT(1) == 'O') && |
| 4505 | (NXT(2) == 'T') && (NXT(3) == 'A') && |
| 4506 | (NXT(4) == 'T') && (NXT(5) == 'I') && |
| 4507 | (NXT(6) == 'O') && (NXT(7) == 'N')) { |
| 4508 | SKIP(8); |
| 4509 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4510 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4511 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4512 | return(0); |
| 4513 | } |
| 4514 | SKIP_BLANKS; |
| 4515 | *tree = xmlParseNotationType(ctxt); |
| 4516 | if (*tree == NULL) return(0); |
| 4517 | return(XML_ATTRIBUTE_NOTATION); |
| 4518 | } |
| 4519 | *tree = xmlParseEnumerationType(ctxt); |
| 4520 | if (*tree == NULL) return(0); |
| 4521 | return(XML_ATTRIBUTE_ENUMERATION); |
| 4522 | } |
| 4523 | |
| 4524 | /** |
| 4525 | * xmlParseAttributeType: |
| 4526 | * @ctxt: an XML parser context |
| 4527 | * @tree: the enumeration tree built while parsing |
| 4528 | * |
| 4529 | * parse the Attribute list def for an element |
| 4530 | * |
| 4531 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 4532 | * |
| 4533 | * [55] StringType ::= 'CDATA' |
| 4534 | * |
| 4535 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 4536 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 4537 | * |
| 4538 | * Validity constraints for attribute values syntax are checked in |
| 4539 | * xmlValidateAttributeValue() |
| 4540 | * |
| 4541 | * [ VC: ID ] |
| 4542 | * Values of type ID must match the Name production. A name must not |
| 4543 | * appear more than once in an XML document as a value of this type; |
| 4544 | * i.e., ID values must uniquely identify the elements which bear them. |
| 4545 | * |
| 4546 | * [ VC: One ID per Element Type ] |
| 4547 | * No element type may have more than one ID attribute specified. |
| 4548 | * |
| 4549 | * [ VC: ID Attribute Default ] |
| 4550 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 4551 | * |
| 4552 | * [ VC: IDREF ] |
| 4553 | * Values of type IDREF must match the Name production, and values |
| 4554 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 4555 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 4556 | * values must match the value of some ID attribute. |
| 4557 | * |
| 4558 | * [ VC: Entity Name ] |
| 4559 | * Values of type ENTITY must match the Name production, values |
| 4560 | * of type ENTITIES must match Names; each Entity Name must match the |
| 4561 | * name of an unparsed entity declared in the DTD. |
| 4562 | * |
| 4563 | * [ VC: Name Token ] |
| 4564 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 4565 | * of type NMTOKENS must match Nmtokens. |
| 4566 | * |
| 4567 | * Returns the attribute type |
| 4568 | */ |
| 4569 | int |
| 4570 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 4571 | SHRINK; |
| 4572 | if ((RAW == 'C') && (NXT(1) == 'D') && |
| 4573 | (NXT(2) == 'A') && (NXT(3) == 'T') && |
| 4574 | (NXT(4) == 'A')) { |
| 4575 | SKIP(5); |
| 4576 | return(XML_ATTRIBUTE_CDATA); |
| 4577 | } else if ((RAW == 'I') && (NXT(1) == 'D') && |
| 4578 | (NXT(2) == 'R') && (NXT(3) == 'E') && |
| 4579 | (NXT(4) == 'F') && (NXT(5) == 'S')) { |
| 4580 | SKIP(6); |
| 4581 | return(XML_ATTRIBUTE_IDREFS); |
| 4582 | } else if ((RAW == 'I') && (NXT(1) == 'D') && |
| 4583 | (NXT(2) == 'R') && (NXT(3) == 'E') && |
| 4584 | (NXT(4) == 'F')) { |
| 4585 | SKIP(5); |
| 4586 | return(XML_ATTRIBUTE_IDREF); |
| 4587 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 4588 | SKIP(2); |
| 4589 | return(XML_ATTRIBUTE_ID); |
| 4590 | } else if ((RAW == 'E') && (NXT(1) == 'N') && |
| 4591 | (NXT(2) == 'T') && (NXT(3) == 'I') && |
| 4592 | (NXT(4) == 'T') && (NXT(5) == 'Y')) { |
| 4593 | SKIP(6); |
| 4594 | return(XML_ATTRIBUTE_ENTITY); |
| 4595 | } else if ((RAW == 'E') && (NXT(1) == 'N') && |
| 4596 | (NXT(2) == 'T') && (NXT(3) == 'I') && |
| 4597 | (NXT(4) == 'T') && (NXT(5) == 'I') && |
| 4598 | (NXT(6) == 'E') && (NXT(7) == 'S')) { |
| 4599 | SKIP(8); |
| 4600 | return(XML_ATTRIBUTE_ENTITIES); |
| 4601 | } else if ((RAW == 'N') && (NXT(1) == 'M') && |
| 4602 | (NXT(2) == 'T') && (NXT(3) == 'O') && |
| 4603 | (NXT(4) == 'K') && (NXT(5) == 'E') && |
| 4604 | (NXT(6) == 'N') && (NXT(7) == 'S')) { |
| 4605 | SKIP(8); |
| 4606 | return(XML_ATTRIBUTE_NMTOKENS); |
| 4607 | } else if ((RAW == 'N') && (NXT(1) == 'M') && |
| 4608 | (NXT(2) == 'T') && (NXT(3) == 'O') && |
| 4609 | (NXT(4) == 'K') && (NXT(5) == 'E') && |
| 4610 | (NXT(6) == 'N')) { |
| 4611 | SKIP(7); |
| 4612 | return(XML_ATTRIBUTE_NMTOKEN); |
| 4613 | } |
| 4614 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 4615 | } |
| 4616 | |
| 4617 | /** |
| 4618 | * xmlParseAttributeListDecl: |
| 4619 | * @ctxt: an XML parser context |
| 4620 | * |
| 4621 | * : parse the Attribute list def for an element |
| 4622 | * |
| 4623 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 4624 | * |
| 4625 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 4626 | * |
| 4627 | */ |
| 4628 | void |
| 4629 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4630 | const xmlChar *elemName; |
| 4631 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4632 | xmlEnumerationPtr tree; |
| 4633 | |
| 4634 | if ((RAW == '<') && (NXT(1) == '!') && |
| 4635 | (NXT(2) == 'A') && (NXT(3) == 'T') && |
| 4636 | (NXT(4) == 'T') && (NXT(5) == 'L') && |
| 4637 | (NXT(6) == 'I') && (NXT(7) == 'S') && |
| 4638 | (NXT(8) == 'T')) { |
| 4639 | xmlParserInputPtr input = ctxt->input; |
| 4640 | |
| 4641 | SKIP(9); |
| 4642 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4643 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4644 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4645 | } |
| 4646 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4647 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4648 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4649 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4650 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4651 | return; |
| 4652 | } |
| 4653 | SKIP_BLANKS; |
| 4654 | GROW; |
| 4655 | while (RAW != '>') { |
| 4656 | const xmlChar *check = CUR_PTR; |
| 4657 | int type; |
| 4658 | int def; |
| 4659 | xmlChar *defaultValue = NULL; |
| 4660 | |
| 4661 | GROW; |
| 4662 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4663 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4664 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4665 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4666 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4667 | break; |
| 4668 | } |
| 4669 | GROW; |
| 4670 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4671 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4672 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4673 | if (defaultValue != NULL) |
| 4674 | xmlFree(defaultValue); |
| 4675 | break; |
| 4676 | } |
| 4677 | SKIP_BLANKS; |
| 4678 | |
| 4679 | type = xmlParseAttributeType(ctxt, &tree); |
| 4680 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4681 | if (defaultValue != NULL) |
| 4682 | xmlFree(defaultValue); |
| 4683 | break; |
| 4684 | } |
| 4685 | |
| 4686 | GROW; |
| 4687 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4688 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4689 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4690 | if (defaultValue != NULL) |
| 4691 | xmlFree(defaultValue); |
| 4692 | if (tree != NULL) |
| 4693 | xmlFreeEnumeration(tree); |
| 4694 | break; |
| 4695 | } |
| 4696 | SKIP_BLANKS; |
| 4697 | |
| 4698 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 4699 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4700 | if (defaultValue != NULL) |
| 4701 | xmlFree(defaultValue); |
| 4702 | if (tree != NULL) |
| 4703 | xmlFreeEnumeration(tree); |
| 4704 | break; |
| 4705 | } |
| 4706 | |
| 4707 | GROW; |
| 4708 | if (RAW != '>') { |
| 4709 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4710 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4711 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4712 | if (defaultValue != NULL) |
| 4713 | xmlFree(defaultValue); |
| 4714 | if (tree != NULL) |
| 4715 | xmlFreeEnumeration(tree); |
| 4716 | break; |
| 4717 | } |
| 4718 | SKIP_BLANKS; |
| 4719 | } |
| 4720 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4721 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4722 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4723 | if (defaultValue != NULL) |
| 4724 | xmlFree(defaultValue); |
| 4725 | if (tree != NULL) |
| 4726 | xmlFreeEnumeration(tree); |
| 4727 | break; |
| 4728 | } |
| 4729 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4730 | (ctxt->sax->attributeDecl != NULL)) |
| 4731 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 4732 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4733 | else if (tree != NULL) |
| 4734 | xmlFreeEnumeration(tree); |
| 4735 | |
| 4736 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 4737 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 4738 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 4739 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 4740 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4741 | if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) { |
| 4742 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 4743 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4744 | if (defaultValue != NULL) |
| 4745 | xmlFree(defaultValue); |
| 4746 | GROW; |
| 4747 | } |
| 4748 | if (RAW == '>') { |
| 4749 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4750 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4751 | "Attribute list declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4752 | } |
| 4753 | NEXT; |
| 4754 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | /** |
| 4759 | * xmlParseElementMixedContentDecl: |
| 4760 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4761 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | * |
| 4763 | * parse the declaration for a Mixed Element content |
| 4764 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4765 | * |
| 4766 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 4767 | * '(' S? '#PCDATA' S? ')' |
| 4768 | * |
| 4769 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 4770 | * |
| 4771 | * [ VC: No Duplicate Types ] |
| 4772 | * The same name must not appear more than once in a single |
| 4773 | * mixed-content declaration. |
| 4774 | * |
| 4775 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 4776 | */ |
| 4777 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4778 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4780 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4781 | |
| 4782 | GROW; |
| 4783 | if ((RAW == '#') && (NXT(1) == 'P') && |
| 4784 | (NXT(2) == 'C') && (NXT(3) == 'D') && |
| 4785 | (NXT(4) == 'A') && (NXT(5) == 'T') && |
| 4786 | (NXT(6) == 'A')) { |
| 4787 | SKIP(7); |
| 4788 | SKIP_BLANKS; |
| 4789 | SHRINK; |
| 4790 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4791 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4792 | ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; |
| 4793 | if (ctxt->vctxt.error != NULL) |
| 4794 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 4795 | "Element content declaration doesn't start and stop in the same entity\n"); |
| 4796 | ctxt->valid = 0; |
| 4797 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4798 | NEXT; |
| 4799 | ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA); |
| 4800 | if (RAW == '*') { |
| 4801 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4802 | NEXT; |
| 4803 | } |
| 4804 | return(ret); |
| 4805 | } |
| 4806 | if ((RAW == '(') || (RAW == '|')) { |
| 4807 | ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA); |
| 4808 | if (ret == NULL) return(NULL); |
| 4809 | } |
| 4810 | while (RAW == '|') { |
| 4811 | NEXT; |
| 4812 | if (elem == NULL) { |
| 4813 | ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 4814 | if (ret == NULL) return(NULL); |
| 4815 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4816 | if (cur != NULL) |
| 4817 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4818 | cur = ret; |
| 4819 | } else { |
| 4820 | n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 4821 | if (n == NULL) return(NULL); |
| 4822 | n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4823 | if (n->c1 != NULL) |
| 4824 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4825 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4826 | if (n != NULL) |
| 4827 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4828 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4829 | } |
| 4830 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4831 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4832 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4833 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4834 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4835 | xmlFreeElementContent(cur); |
| 4836 | return(NULL); |
| 4837 | } |
| 4838 | SKIP_BLANKS; |
| 4839 | GROW; |
| 4840 | } |
| 4841 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 4842 | if (elem != NULL) { |
| 4843 | cur->c2 = xmlNewElementContent(elem, |
| 4844 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4845 | if (cur->c2 != NULL) |
| 4846 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4847 | } |
| 4848 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4849 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4850 | ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; |
| 4851 | if (ctxt->vctxt.error != NULL) |
| 4852 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 4853 | "Element content declaration doesn't start and stop in the same entity\n"); |
| 4854 | ctxt->valid = 0; |
| 4855 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4856 | SKIP(2); |
| 4857 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4858 | xmlFreeElementContent(ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4859 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4860 | return(NULL); |
| 4861 | } |
| 4862 | |
| 4863 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4864 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4865 | } |
| 4866 | return(ret); |
| 4867 | } |
| 4868 | |
| 4869 | /** |
| 4870 | * xmlParseElementChildrenContentDecl: |
| 4871 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4872 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4873 | * |
| 4874 | * parse the declaration for a Mixed Element content |
| 4875 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4876 | * |
| 4877 | * |
| 4878 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 4879 | * |
| 4880 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 4881 | * |
| 4882 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 4883 | * |
| 4884 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 4885 | * |
| 4886 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 4887 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4888 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4889 | * opening or closing parentheses in a choice, seq, or Mixed |
| 4890 | * construct is contained in the replacement text for a parameter |
| 4891 | * entity, both must be contained in the same replacement text. For |
| 4892 | * interoperability, if a parameter-entity reference appears in a |
| 4893 | * choice, seq, or Mixed construct, its replacement text should not |
| 4894 | * be empty, and neither the first nor last non-blank character of |
| 4895 | * the replacement text should be a connector (| or ,). |
| 4896 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 4897 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4898 | * hierarchy. |
| 4899 | */ |
| 4900 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4901 | xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4902 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4903 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4904 | xmlChar type = 0; |
| 4905 | |
| 4906 | SKIP_BLANKS; |
| 4907 | GROW; |
| 4908 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4909 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4910 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4911 | /* Recurse on first child */ |
| 4912 | NEXT; |
| 4913 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4914 | cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4915 | SKIP_BLANKS; |
| 4916 | GROW; |
| 4917 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4918 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4919 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4920 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4921 | return(NULL); |
| 4922 | } |
| 4923 | cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4924 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4925 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4926 | return(NULL); |
| 4927 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4928 | GROW; |
| 4929 | if (RAW == '?') { |
| 4930 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4931 | NEXT; |
| 4932 | } else if (RAW == '*') { |
| 4933 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4934 | NEXT; |
| 4935 | } else if (RAW == '+') { |
| 4936 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 4937 | NEXT; |
| 4938 | } else { |
| 4939 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4940 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4941 | GROW; |
| 4942 | } |
| 4943 | SKIP_BLANKS; |
| 4944 | SHRINK; |
| 4945 | while (RAW != ')') { |
| 4946 | /* |
| 4947 | * Each loop we parse one separator and one element. |
| 4948 | */ |
| 4949 | if (RAW == ',') { |
| 4950 | if (type == 0) type = CUR; |
| 4951 | |
| 4952 | /* |
| 4953 | * Detect "Name | Name , Name" error |
| 4954 | */ |
| 4955 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4956 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4957 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4958 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4959 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4960 | xmlFreeElementContent(last); |
| 4961 | if (ret != NULL) |
| 4962 | xmlFreeElementContent(ret); |
| 4963 | return(NULL); |
| 4964 | } |
| 4965 | NEXT; |
| 4966 | |
| 4967 | op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ); |
| 4968 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4969 | if ((last != NULL) && (last != ret)) |
| 4970 | xmlFreeElementContent(last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4971 | xmlFreeElementContent(ret); |
| 4972 | return(NULL); |
| 4973 | } |
| 4974 | if (last == NULL) { |
| 4975 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4976 | if (ret != NULL) |
| 4977 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4978 | ret = cur = op; |
| 4979 | } else { |
| 4980 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4981 | if (op != NULL) |
| 4982 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4983 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4984 | if (last != NULL) |
| 4985 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4986 | cur =op; |
| 4987 | last = NULL; |
| 4988 | } |
| 4989 | } else if (RAW == '|') { |
| 4990 | if (type == 0) type = CUR; |
| 4991 | |
| 4992 | /* |
| 4993 | * Detect "Name , Name | Name" error |
| 4994 | */ |
| 4995 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4996 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4997 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4998 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4999 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5000 | xmlFreeElementContent(last); |
| 5001 | if (ret != NULL) |
| 5002 | xmlFreeElementContent(ret); |
| 5003 | return(NULL); |
| 5004 | } |
| 5005 | NEXT; |
| 5006 | |
| 5007 | op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 5008 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5009 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5010 | xmlFreeElementContent(last); |
| 5011 | if (ret != NULL) |
| 5012 | xmlFreeElementContent(ret); |
| 5013 | return(NULL); |
| 5014 | } |
| 5015 | if (last == NULL) { |
| 5016 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5017 | if (ret != NULL) |
| 5018 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5019 | ret = cur = op; |
| 5020 | } else { |
| 5021 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5022 | if (op != NULL) |
| 5023 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5024 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5025 | if (last != NULL) |
| 5026 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5027 | cur =op; |
| 5028 | last = NULL; |
| 5029 | } |
| 5030 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5031 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5032 | if (ret != NULL) |
| 5033 | xmlFreeElementContent(ret); |
| 5034 | return(NULL); |
| 5035 | } |
| 5036 | GROW; |
| 5037 | SKIP_BLANKS; |
| 5038 | GROW; |
| 5039 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5040 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5041 | /* Recurse on second child */ |
| 5042 | NEXT; |
| 5043 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5044 | last = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | SKIP_BLANKS; |
| 5046 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5047 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5048 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5049 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5050 | if (ret != NULL) |
| 5051 | xmlFreeElementContent(ret); |
| 5052 | return(NULL); |
| 5053 | } |
| 5054 | last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5055 | if (RAW == '?') { |
| 5056 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5057 | NEXT; |
| 5058 | } else if (RAW == '*') { |
| 5059 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5060 | NEXT; |
| 5061 | } else if (RAW == '+') { |
| 5062 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5063 | NEXT; |
| 5064 | } else { |
| 5065 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5066 | } |
| 5067 | } |
| 5068 | SKIP_BLANKS; |
| 5069 | GROW; |
| 5070 | } |
| 5071 | if ((cur != NULL) && (last != NULL)) { |
| 5072 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5073 | if (last != NULL) |
| 5074 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5075 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5076 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5077 | ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; |
| 5078 | if (ctxt->vctxt.error != NULL) |
| 5079 | ctxt->vctxt.error(ctxt->vctxt.userData, |
| 5080 | "Element content declaration doesn't start and stop in the same entity\n"); |
| 5081 | ctxt->valid = 0; |
| 5082 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5083 | NEXT; |
| 5084 | if (RAW == '?') { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5085 | if (ret != NULL) |
| 5086 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5087 | NEXT; |
| 5088 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5089 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5090 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5091 | cur = ret; |
| 5092 | /* |
| 5093 | * Some normalization: |
| 5094 | * (a | b* | c?)* == (a | b | c)* |
| 5095 | */ |
| 5096 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 5097 | if ((cur->c1 != NULL) && |
| 5098 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5099 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5100 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5101 | if ((cur->c2 != NULL) && |
| 5102 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5103 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5104 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5105 | cur = cur->c2; |
| 5106 | } |
| 5107 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5108 | NEXT; |
| 5109 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5110 | if (ret != NULL) { |
| 5111 | int found = 0; |
| 5112 | |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5113 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5114 | /* |
| 5115 | * Some normalization: |
| 5116 | * (a | b*)+ == (a | b)* |
| 5117 | * (a | b?)+ == (a | b)* |
| 5118 | */ |
| 5119 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 5120 | if ((cur->c1 != NULL) && |
| 5121 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5122 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 5123 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5124 | found = 1; |
| 5125 | } |
| 5126 | if ((cur->c2 != NULL) && |
| 5127 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5128 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 5129 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5130 | found = 1; |
| 5131 | } |
| 5132 | cur = cur->c2; |
| 5133 | } |
| 5134 | if (found) |
| 5135 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5136 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5137 | NEXT; |
| 5138 | } |
| 5139 | return(ret); |
| 5140 | } |
| 5141 | |
| 5142 | /** |
| 5143 | * xmlParseElementContentDecl: |
| 5144 | * @ctxt: an XML parser context |
| 5145 | * @name: the name of the element being defined. |
| 5146 | * @result: the Element Content pointer will be stored here if any |
| 5147 | * |
| 5148 | * parse the declaration for an Element content either Mixed or Children, |
| 5149 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 5150 | * |
| 5151 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 5152 | * |
| 5153 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 5154 | */ |
| 5155 | |
| 5156 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5157 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5158 | xmlElementContentPtr *result) { |
| 5159 | |
| 5160 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5161 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5162 | int res; |
| 5163 | |
| 5164 | *result = NULL; |
| 5165 | |
| 5166 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5167 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5168 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5169 | return(-1); |
| 5170 | } |
| 5171 | NEXT; |
| 5172 | GROW; |
| 5173 | SKIP_BLANKS; |
| 5174 | if ((RAW == '#') && (NXT(1) == 'P') && |
| 5175 | (NXT(2) == 'C') && (NXT(3) == 'D') && |
| 5176 | (NXT(4) == 'A') && (NXT(5) == 'T') && |
| 5177 | (NXT(6) == 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5178 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5179 | res = XML_ELEMENT_TYPE_MIXED; |
| 5180 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5181 | tree = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5182 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 5183 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5184 | SKIP_BLANKS; |
| 5185 | *result = tree; |
| 5186 | return(res); |
| 5187 | } |
| 5188 | |
| 5189 | /** |
| 5190 | * xmlParseElementDecl: |
| 5191 | * @ctxt: an XML parser context |
| 5192 | * |
| 5193 | * parse an Element declaration. |
| 5194 | * |
| 5195 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 5196 | * |
| 5197 | * [ VC: Unique Element Type Declaration ] |
| 5198 | * No element type may be declared more than once |
| 5199 | * |
| 5200 | * Returns the type of the element, or -1 in case of error |
| 5201 | */ |
| 5202 | int |
| 5203 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5204 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5205 | int ret = -1; |
| 5206 | xmlElementContentPtr content = NULL; |
| 5207 | |
| 5208 | GROW; |
| 5209 | if ((RAW == '<') && (NXT(1) == '!') && |
| 5210 | (NXT(2) == 'E') && (NXT(3) == 'L') && |
| 5211 | (NXT(4) == 'E') && (NXT(5) == 'M') && |
| 5212 | (NXT(6) == 'E') && (NXT(7) == 'N') && |
| 5213 | (NXT(8) == 'T')) { |
| 5214 | xmlParserInputPtr input = ctxt->input; |
| 5215 | |
| 5216 | SKIP(9); |
| 5217 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5218 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5219 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5220 | } |
| 5221 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5222 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5223 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5224 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5225 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5226 | return(-1); |
| 5227 | } |
| 5228 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5229 | xmlPopInput(ctxt); |
| 5230 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5231 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5232 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5233 | } |
| 5234 | SKIP_BLANKS; |
| 5235 | if ((RAW == 'E') && (NXT(1) == 'M') && |
| 5236 | (NXT(2) == 'P') && (NXT(3) == 'T') && |
| 5237 | (NXT(4) == 'Y')) { |
| 5238 | SKIP(5); |
| 5239 | /* |
| 5240 | * Element must always be empty. |
| 5241 | */ |
| 5242 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 5243 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 5244 | (NXT(2) == 'Y')) { |
| 5245 | SKIP(3); |
| 5246 | /* |
| 5247 | * Element is a generic container. |
| 5248 | */ |
| 5249 | ret = XML_ELEMENT_TYPE_ANY; |
| 5250 | } else if (RAW == '(') { |
| 5251 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 5252 | } else { |
| 5253 | /* |
| 5254 | * [ WFC: PEs in Internal Subset ] error handling. |
| 5255 | */ |
| 5256 | if ((RAW == '%') && (ctxt->external == 0) && |
| 5257 | (ctxt->inputNr == 1)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5258 | ctxt->errNo = XML_ERR_PEREF_IN_INT_SUBSET; |
| 5259 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 5260 | ctxt->sax->error(ctxt->userData, |
| 5261 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | } else { |
| 5263 | ctxt->errNo = XML_ERR_ELEMCONTENT_NOT_STARTED; |
| 5264 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 5265 | ctxt->sax->error(ctxt->userData, |
| 5266 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 5267 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5268 | ctxt->wellFormed = 0; |
| 5269 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5270 | return(-1); |
| 5271 | } |
| 5272 | |
| 5273 | SKIP_BLANKS; |
| 5274 | /* |
| 5275 | * Pop-up of finished entities. |
| 5276 | */ |
| 5277 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5278 | xmlPopInput(ctxt); |
| 5279 | SKIP_BLANKS; |
| 5280 | |
| 5281 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5282 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5283 | } else { |
| 5284 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5285 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5286 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | NEXT; |
| 5290 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5291 | (ctxt->sax->elementDecl != NULL)) |
| 5292 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 5293 | content); |
| 5294 | } |
| 5295 | if (content != NULL) { |
| 5296 | xmlFreeElementContent(content); |
| 5297 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5298 | } |
| 5299 | return(ret); |
| 5300 | } |
| 5301 | |
| 5302 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5303 | * xmlParseConditionalSections |
| 5304 | * @ctxt: an XML parser context |
| 5305 | * |
| 5306 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 5307 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 5308 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 5309 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 5310 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 5311 | */ |
| 5312 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5313 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5314 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
| 5315 | SKIP(3); |
| 5316 | SKIP_BLANKS; |
| 5317 | if ((RAW == 'I') && (NXT(1) == 'N') && (NXT(2) == 'C') && |
| 5318 | (NXT(3) == 'L') && (NXT(4) == 'U') && (NXT(5) == 'D') && |
| 5319 | (NXT(6) == 'E')) { |
| 5320 | SKIP(7); |
| 5321 | SKIP_BLANKS; |
| 5322 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5323 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5324 | } else { |
| 5325 | NEXT; |
| 5326 | } |
| 5327 | if (xmlParserDebugEntities) { |
| 5328 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5329 | xmlGenericError(xmlGenericErrorContext, |
| 5330 | "%s(%d): ", ctxt->input->filename, |
| 5331 | ctxt->input->line); |
| 5332 | xmlGenericError(xmlGenericErrorContext, |
| 5333 | "Entering INCLUDE Conditional Section\n"); |
| 5334 | } |
| 5335 | |
| 5336 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 5337 | (NXT(2) != '>'))) { |
| 5338 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5339 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5340 | |
| 5341 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5342 | xmlParseConditionalSections(ctxt); |
| 5343 | } else if (IS_BLANK(CUR)) { |
| 5344 | NEXT; |
| 5345 | } else if (RAW == '%') { |
| 5346 | xmlParsePEReference(ctxt); |
| 5347 | } else |
| 5348 | xmlParseMarkupDecl(ctxt); |
| 5349 | |
| 5350 | /* |
| 5351 | * Pop-up of finished entities. |
| 5352 | */ |
| 5353 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5354 | xmlPopInput(ctxt); |
| 5355 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5356 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5357 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5358 | break; |
| 5359 | } |
| 5360 | } |
| 5361 | if (xmlParserDebugEntities) { |
| 5362 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5363 | xmlGenericError(xmlGenericErrorContext, |
| 5364 | "%s(%d): ", ctxt->input->filename, |
| 5365 | ctxt->input->line); |
| 5366 | xmlGenericError(xmlGenericErrorContext, |
| 5367 | "Leaving INCLUDE Conditional Section\n"); |
| 5368 | } |
| 5369 | |
| 5370 | } else if ((RAW == 'I') && (NXT(1) == 'G') && (NXT(2) == 'N') && |
| 5371 | (NXT(3) == 'O') && (NXT(4) == 'R') && (NXT(5) == 'E')) { |
| 5372 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 5373 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5374 | int depth = 0; |
| 5375 | |
| 5376 | SKIP(6); |
| 5377 | SKIP_BLANKS; |
| 5378 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5379 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5380 | } else { |
| 5381 | NEXT; |
| 5382 | } |
| 5383 | if (xmlParserDebugEntities) { |
| 5384 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5385 | xmlGenericError(xmlGenericErrorContext, |
| 5386 | "%s(%d): ", ctxt->input->filename, |
| 5387 | ctxt->input->line); |
| 5388 | xmlGenericError(xmlGenericErrorContext, |
| 5389 | "Entering IGNORE Conditional Section\n"); |
| 5390 | } |
| 5391 | |
| 5392 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5393 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5394 | * But disable SAX event generating DTD building in the meantime |
| 5395 | */ |
| 5396 | state = ctxt->disableSAX; |
| 5397 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 5398 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5399 | ctxt->instate = XML_PARSER_IGNORE; |
| 5400 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5401 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5402 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5403 | depth++; |
| 5404 | SKIP(3); |
| 5405 | continue; |
| 5406 | } |
| 5407 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 5408 | if (--depth >= 0) SKIP(3); |
| 5409 | continue; |
| 5410 | } |
| 5411 | NEXT; |
| 5412 | continue; |
| 5413 | } |
| 5414 | |
| 5415 | ctxt->disableSAX = state; |
| 5416 | ctxt->instate = instate; |
| 5417 | |
| 5418 | if (xmlParserDebugEntities) { |
| 5419 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5420 | xmlGenericError(xmlGenericErrorContext, |
| 5421 | "%s(%d): ", ctxt->input->filename, |
| 5422 | ctxt->input->line); |
| 5423 | xmlGenericError(xmlGenericErrorContext, |
| 5424 | "Leaving IGNORE Conditional Section\n"); |
| 5425 | } |
| 5426 | |
| 5427 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5428 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
| 5431 | if (RAW == 0) |
| 5432 | SHRINK; |
| 5433 | |
| 5434 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5435 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5436 | } else { |
| 5437 | SKIP(3); |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5442 | * xmlParseMarkupDecl: |
| 5443 | * @ctxt: an XML parser context |
| 5444 | * |
| 5445 | * parse Markup declarations |
| 5446 | * |
| 5447 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 5448 | * NotationDecl | PI | Comment |
| 5449 | * |
| 5450 | * [ VC: Proper Declaration/PE Nesting ] |
| 5451 | * Parameter-entity replacement text must be properly nested with |
| 5452 | * markup declarations. That is to say, if either the first character |
| 5453 | * or the last character of a markup declaration (markupdecl above) is |
| 5454 | * contained in the replacement text for a parameter-entity reference, |
| 5455 | * both must be contained in the same replacement text. |
| 5456 | * |
| 5457 | * [ WFC: PEs in Internal Subset ] |
| 5458 | * In the internal DTD subset, parameter-entity references can occur |
| 5459 | * only where markup declarations can occur, not within markup declarations. |
| 5460 | * (This does not apply to references that occur in external parameter |
| 5461 | * entities or to the external subset.) |
| 5462 | */ |
| 5463 | void |
| 5464 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 5465 | GROW; |
| 5466 | xmlParseElementDecl(ctxt); |
| 5467 | xmlParseAttributeListDecl(ctxt); |
| 5468 | xmlParseEntityDecl(ctxt); |
| 5469 | xmlParseNotationDecl(ctxt); |
| 5470 | xmlParsePI(ctxt); |
| 5471 | xmlParseComment(ctxt); |
| 5472 | /* |
| 5473 | * This is only for internal subset. On external entities, |
| 5474 | * the replacement is done before parsing stage |
| 5475 | */ |
| 5476 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 5477 | xmlParsePEReference(ctxt); |
| 5478 | |
| 5479 | /* |
| 5480 | * Conditional sections are allowed from entities included |
| 5481 | * by PE References in the internal subset. |
| 5482 | */ |
| 5483 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 5484 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5485 | xmlParseConditionalSections(ctxt); |
| 5486 | } |
| 5487 | } |
| 5488 | |
| 5489 | ctxt->instate = XML_PARSER_DTD; |
| 5490 | } |
| 5491 | |
| 5492 | /** |
| 5493 | * xmlParseTextDecl: |
| 5494 | * @ctxt: an XML parser context |
| 5495 | * |
| 5496 | * parse an XML declaration header for external entities |
| 5497 | * |
| 5498 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
| 5499 | * |
| 5500 | * Question: Seems that EncodingDecl is mandatory ? Is that a typo ? |
| 5501 | */ |
| 5502 | |
| 5503 | void |
| 5504 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 5505 | xmlChar *version; |
| 5506 | |
| 5507 | /* |
| 5508 | * We know that '<?xml' is here. |
| 5509 | */ |
| 5510 | if ((RAW == '<') && (NXT(1) == '?') && |
| 5511 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 5512 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 5513 | SKIP(5); |
| 5514 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5515 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5516 | return; |
| 5517 | } |
| 5518 | |
| 5519 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5520 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5521 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5522 | } |
| 5523 | SKIP_BLANKS; |
| 5524 | |
| 5525 | /* |
| 5526 | * We may have the VersionInfo here. |
| 5527 | */ |
| 5528 | version = xmlParseVersionInfo(ctxt); |
| 5529 | if (version == NULL) |
| 5530 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5531 | else { |
| 5532 | if (!IS_BLANK(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5533 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5534 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5535 | } |
| 5536 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5537 | ctxt->input->version = version; |
| 5538 | |
| 5539 | /* |
| 5540 | * We must have the encoding declaration |
| 5541 | */ |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5542 | xmlParseEncodingDecl(ctxt); |
| 5543 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5544 | /* |
| 5545 | * The XML REC instructs us to stop parsing right here |
| 5546 | */ |
| 5547 | return; |
| 5548 | } |
| 5549 | |
| 5550 | SKIP_BLANKS; |
| 5551 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5552 | SKIP(2); |
| 5553 | } else if (RAW == '>') { |
| 5554 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5555 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5556 | NEXT; |
| 5557 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5558 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5559 | MOVETO_ENDTAG(CUR_PTR); |
| 5560 | NEXT; |
| 5561 | } |
| 5562 | } |
| 5563 | |
| 5564 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5565 | * xmlParseExternalSubset: |
| 5566 | * @ctxt: an XML parser context |
| 5567 | * @ExternalID: the external identifier |
| 5568 | * @SystemID: the system identifier (or URL) |
| 5569 | * |
| 5570 | * parse Markup declarations from an external subset |
| 5571 | * |
| 5572 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 5573 | * |
| 5574 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 5575 | */ |
| 5576 | void |
| 5577 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 5578 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 5579 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5580 | GROW; |
| 5581 | if ((RAW == '<') && (NXT(1) == '?') && |
| 5582 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 5583 | (NXT(4) == 'l')) { |
| 5584 | xmlParseTextDecl(ctxt); |
| 5585 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5586 | /* |
| 5587 | * The XML REC instructs us to stop parsing right here |
| 5588 | */ |
| 5589 | ctxt->instate = XML_PARSER_EOF; |
| 5590 | return; |
| 5591 | } |
| 5592 | } |
| 5593 | if (ctxt->myDoc == NULL) { |
| 5594 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 5595 | } |
| 5596 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 5597 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 5598 | |
| 5599 | ctxt->instate = XML_PARSER_DTD; |
| 5600 | ctxt->external = 1; |
| 5601 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 5602 | ((RAW == '<') && (NXT(1) == '!')) || |
Daniel Veillard | 2454ab9 | 2001-07-25 21:39:46 +0000 | [diff] [blame] | 5603 | (RAW == '%') || IS_BLANK(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5604 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5605 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | |
| 5607 | GROW; |
| 5608 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5609 | xmlParseConditionalSections(ctxt); |
| 5610 | } else if (IS_BLANK(CUR)) { |
| 5611 | NEXT; |
| 5612 | } else if (RAW == '%') { |
| 5613 | xmlParsePEReference(ctxt); |
| 5614 | } else |
| 5615 | xmlParseMarkupDecl(ctxt); |
| 5616 | |
| 5617 | /* |
| 5618 | * Pop-up of finished entities. |
| 5619 | */ |
| 5620 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5621 | xmlPopInput(ctxt); |
| 5622 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5623 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5624 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5625 | break; |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5630 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | } |
| 5634 | |
| 5635 | /** |
| 5636 | * xmlParseReference: |
| 5637 | * @ctxt: an XML parser context |
| 5638 | * |
| 5639 | * parse and handle entity references in content, depending on the SAX |
| 5640 | * interface, this may end-up in a call to character() if this is a |
| 5641 | * CharRef, a predefined entity, if there is no reference() callback. |
| 5642 | * or if the parser was asked to switch to that mode. |
| 5643 | * |
| 5644 | * [67] Reference ::= EntityRef | CharRef |
| 5645 | */ |
| 5646 | void |
| 5647 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 5648 | xmlEntityPtr ent; |
| 5649 | xmlChar *val; |
| 5650 | if (RAW != '&') return; |
| 5651 | |
| 5652 | if (NXT(1) == '#') { |
| 5653 | int i = 0; |
| 5654 | xmlChar out[10]; |
| 5655 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5656 | int value = xmlParseCharRef(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5657 | |
| 5658 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 5659 | /* |
| 5660 | * So we are using non-UTF-8 buffers |
| 5661 | * Check that the char fit on 8bits, if not |
| 5662 | * generate a CharRef. |
| 5663 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5664 | if (value <= 0xFF) { |
| 5665 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5666 | out[1] = 0; |
| 5667 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5668 | (!ctxt->disableSAX)) |
| 5669 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 5670 | } else { |
| 5671 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5672 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5673 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5674 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5675 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5676 | (!ctxt->disableSAX)) |
| 5677 | ctxt->sax->reference(ctxt->userData, out); |
| 5678 | } |
| 5679 | } else { |
| 5680 | /* |
| 5681 | * Just encode the value in UTF-8 |
| 5682 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5683 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | out[i] = 0; |
| 5685 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5686 | (!ctxt->disableSAX)) |
| 5687 | ctxt->sax->characters(ctxt->userData, out, i); |
| 5688 | } |
| 5689 | } else { |
| 5690 | ent = xmlParseEntityRef(ctxt); |
| 5691 | if (ent == NULL) return; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5692 | if (!ctxt->wellFormed) |
| 5693 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | if ((ent->name != NULL) && |
| 5695 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 5696 | xmlNodePtr list = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 5697 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5698 | |
| 5699 | |
| 5700 | /* |
| 5701 | * The first reference to the entity trigger a parsing phase |
| 5702 | * where the ent->children is filled with the result from |
| 5703 | * the parsing. |
| 5704 | */ |
| 5705 | if (ent->children == NULL) { |
| 5706 | xmlChar *value; |
| 5707 | value = ent->content; |
| 5708 | |
| 5709 | /* |
| 5710 | * Check that this entity is well formed |
| 5711 | */ |
| 5712 | if ((value != NULL) && |
| 5713 | (value[1] == 0) && (value[0] == '<') && |
| 5714 | (xmlStrEqual(ent->name, BAD_CAST "lt"))) { |
| 5715 | /* |
| 5716 | * DONE: get definite answer on this !!! |
| 5717 | * Lots of entity decls are used to declare a single |
| 5718 | * char |
| 5719 | * <!ENTITY lt "<"> |
| 5720 | * Which seems to be valid since |
| 5721 | * 2.4: The ampersand character (&) and the left angle |
| 5722 | * bracket (<) may appear in their literal form only |
| 5723 | * when used ... They are also legal within the literal |
| 5724 | * entity value of an internal entity declaration;i |
| 5725 | * see "4.3.2 Well-Formed Parsed Entities". |
| 5726 | * IMHO 2.4 and 4.3.2 are directly in contradiction. |
| 5727 | * Looking at the OASIS test suite and James Clark |
| 5728 | * tests, this is broken. However the XML REC uses |
| 5729 | * it. Is the XML REC not well-formed ???? |
| 5730 | * This is a hack to avoid this problem |
| 5731 | * |
| 5732 | * ANSWER: since lt gt amp .. are already defined, |
| 5733 | * this is a redefinition and hence the fact that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5734 | * content is not well balanced is not a Wf error, this |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5735 | * is lousy but acceptable. |
| 5736 | */ |
| 5737 | list = xmlNewDocText(ctxt->myDoc, value); |
| 5738 | if (list != NULL) { |
| 5739 | if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) && |
| 5740 | (ent->children == NULL)) { |
| 5741 | ent->children = list; |
| 5742 | ent->last = list; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5743 | ent->owner = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5744 | list->parent = (xmlNodePtr) ent; |
| 5745 | } else { |
| 5746 | xmlFreeNodeList(list); |
| 5747 | } |
| 5748 | } else if (list != NULL) { |
| 5749 | xmlFreeNodeList(list); |
| 5750 | } |
| 5751 | } else { |
| 5752 | /* |
| 5753 | * 4.3.2: An internal general parsed entity is well-formed |
| 5754 | * if its replacement text matches the production labeled |
| 5755 | * content. |
| 5756 | */ |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5757 | |
| 5758 | void *user_data; |
| 5759 | /* |
| 5760 | * This is a bit hackish but this seems the best |
| 5761 | * way to make sure both SAX and DOM entity support |
| 5762 | * behaves okay. |
| 5763 | */ |
| 5764 | if (ctxt->userData == ctxt) |
| 5765 | user_data = NULL; |
| 5766 | else |
| 5767 | user_data = ctxt->userData; |
| 5768 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5769 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 5770 | ctxt->depth++; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 5771 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 5772 | value, user_data, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5773 | ctxt->depth--; |
| 5774 | } else if (ent->etype == |
| 5775 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 5776 | ctxt->depth++; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5777 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5778 | ctxt->sax, user_data, ctxt->depth, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5779 | ent->URI, ent->ExternalID, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5780 | ctxt->depth--; |
| 5781 | } else { |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 5782 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 5784 | ctxt->sax->error(ctxt->userData, |
| 5785 | "Internal: invalid entity type\n"); |
| 5786 | } |
| 5787 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5788 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5789 | return; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 5790 | } else if ((ret == XML_ERR_OK) && (list != NULL)) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5791 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 5792 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5793 | (ent->children == NULL)) { |
| 5794 | ent->children = list; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5795 | if (ctxt->replaceEntities) { |
| 5796 | /* |
| 5797 | * Prune it directly in the generated document |
| 5798 | * except for single text nodes. |
| 5799 | */ |
| 5800 | if ((list->type == XML_TEXT_NODE) && |
| 5801 | (list->next == NULL)) { |
| 5802 | list->parent = (xmlNodePtr) ent; |
| 5803 | list = NULL; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5804 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5805 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5806 | ent->owner = 0; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5807 | while (list != NULL) { |
| 5808 | list->parent = (xmlNodePtr) ctxt->node; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5809 | list->doc = ctxt->myDoc; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5810 | if (list->next == NULL) |
| 5811 | ent->last = list; |
| 5812 | list = list->next; |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5813 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5814 | list = ent->children; |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5815 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5816 | xmlAddEntityReference(ent, list, NULL); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5817 | } |
| 5818 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5819 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5820 | while (list != NULL) { |
| 5821 | list->parent = (xmlNodePtr) ent; |
| 5822 | if (list->next == NULL) |
| 5823 | ent->last = list; |
| 5824 | list = list->next; |
| 5825 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | } |
| 5827 | } else { |
| 5828 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5829 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5830 | } |
| 5831 | } else if (ret > 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5832 | xmlFatalErr(ctxt, ret, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5833 | } else if (list != NULL) { |
| 5834 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5835 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5836 | } |
| 5837 | } |
| 5838 | } |
| 5839 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5840 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 5841 | /* |
| 5842 | * Create a node. |
| 5843 | */ |
| 5844 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 5845 | return; |
| 5846 | } else if (ctxt->replaceEntities) { |
| 5847 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 5848 | /* |
| 5849 | * Seems we are generating the DOM content, do |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5850 | * a simple tree copy for all references except the first |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5851 | * In the first occurrence list contains the replacement |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5852 | */ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5853 | if ((list == NULL) && (ent->owner == 0)) { |
| 5854 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5855 | cur = ent->children; |
| 5856 | while (cur != NULL) { |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5857 | nw = xmlCopyNode(cur, 1); |
| 5858 | if (nw != NULL) { |
| 5859 | nw->_private = cur->_private; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5860 | if (firstChild == NULL){ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5861 | firstChild = nw; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5862 | } |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5863 | xmlAddChild(ctxt->node, nw); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5864 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5865 | if (cur == ent->last) |
| 5866 | break; |
| 5867 | cur = cur->next; |
| 5868 | } |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5869 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5870 | xmlAddEntityReference(ent, firstChild, nw); |
| 5871 | } else if (list == NULL) { |
| 5872 | xmlNodePtr nw = NULL, cur, next, last, |
| 5873 | firstChild = NULL; |
| 5874 | /* |
| 5875 | * Copy the entity child list and make it the new |
| 5876 | * entity child list. The goal is to make sure any |
| 5877 | * ID or REF referenced will be the one from the |
| 5878 | * document content and not the entity copy. |
| 5879 | */ |
| 5880 | cur = ent->children; |
| 5881 | ent->children = NULL; |
| 5882 | last = ent->last; |
| 5883 | ent->last = NULL; |
| 5884 | while (cur != NULL) { |
| 5885 | next = cur->next; |
| 5886 | cur->next = NULL; |
| 5887 | cur->parent = NULL; |
| 5888 | nw = xmlCopyNode(cur, 1); |
| 5889 | if (nw != NULL) { |
| 5890 | nw->_private = cur->_private; |
| 5891 | if (firstChild == NULL){ |
| 5892 | firstChild = cur; |
| 5893 | } |
| 5894 | xmlAddChild((xmlNodePtr) ent, nw); |
| 5895 | xmlAddChild(ctxt->node, cur); |
| 5896 | } |
| 5897 | if (cur == last) |
| 5898 | break; |
| 5899 | cur = next; |
| 5900 | } |
| 5901 | ent->owner = 1; |
| 5902 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5903 | xmlAddEntityReference(ent, firstChild, nw); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5904 | } else { |
| 5905 | /* |
| 5906 | * the name change is to avoid coalescing of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5907 | * node with a possible previous text one which |
| 5908 | * would make ent->children a dangling pointer |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5909 | */ |
| 5910 | if (ent->children->type == XML_TEXT_NODE) |
| 5911 | ent->children->name = xmlStrdup(BAD_CAST "nbktext"); |
| 5912 | if ((ent->last != ent->children) && |
| 5913 | (ent->last->type == XML_TEXT_NODE)) |
| 5914 | ent->last->name = xmlStrdup(BAD_CAST "nbktext"); |
| 5915 | xmlAddChildList(ctxt->node, ent->children); |
| 5916 | } |
| 5917 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5918 | /* |
| 5919 | * This is to avoid a nasty side effect, see |
| 5920 | * characters() in SAX.c |
| 5921 | */ |
| 5922 | ctxt->nodemem = 0; |
| 5923 | ctxt->nodelen = 0; |
| 5924 | return; |
| 5925 | } else { |
| 5926 | /* |
| 5927 | * Probably running in SAX mode |
| 5928 | */ |
| 5929 | xmlParserInputPtr input; |
| 5930 | |
| 5931 | input = xmlNewEntityInputStream(ctxt, ent); |
| 5932 | xmlPushInput(ctxt, input); |
| 5933 | if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) && |
| 5934 | (RAW == '<') && (NXT(1) == '?') && |
| 5935 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 5936 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 5937 | xmlParseTextDecl(ctxt); |
| 5938 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5939 | /* |
| 5940 | * The XML REC instructs us to stop parsing right here |
| 5941 | */ |
| 5942 | ctxt->instate = XML_PARSER_EOF; |
| 5943 | return; |
| 5944 | } |
| 5945 | if (input->standalone == 1) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5946 | xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE, |
| 5947 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5948 | } |
| 5949 | } |
| 5950 | return; |
| 5951 | } |
| 5952 | } |
| 5953 | } else { |
| 5954 | val = ent->content; |
| 5955 | if (val == NULL) return; |
| 5956 | /* |
| 5957 | * inline the entity. |
| 5958 | */ |
| 5959 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5960 | (!ctxt->disableSAX)) |
| 5961 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 5962 | } |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | /** |
| 5967 | * xmlParseEntityRef: |
| 5968 | * @ctxt: an XML parser context |
| 5969 | * |
| 5970 | * parse ENTITY references declarations |
| 5971 | * |
| 5972 | * [68] EntityRef ::= '&' Name ';' |
| 5973 | * |
| 5974 | * [ WFC: Entity Declared ] |
| 5975 | * In a document without any DTD, a document with only an internal DTD |
| 5976 | * subset which contains no parameter entity references, or a document |
| 5977 | * with "standalone='yes'", the Name given in the entity reference |
| 5978 | * must match that in an entity declaration, except that well-formed |
| 5979 | * documents need not declare any of the following entities: amp, lt, |
| 5980 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 5981 | * any reference to it. Similarly, the declaration of a general entity |
| 5982 | * must precede any reference to it which appears in a default value in an |
| 5983 | * attribute-list declaration. Note that if entities are declared in the |
| 5984 | * external subset or in external parameter entities, a non-validating |
| 5985 | * processor is not obligated to read and process their declarations; |
| 5986 | * for such documents, the rule that an entity must be declared is a |
| 5987 | * well-formedness constraint only if standalone='yes'. |
| 5988 | * |
| 5989 | * [ WFC: Parsed Entity ] |
| 5990 | * An entity reference must not contain the name of an unparsed entity |
| 5991 | * |
| 5992 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 5993 | */ |
| 5994 | xmlEntityPtr |
| 5995 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5996 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5997 | xmlEntityPtr ent = NULL; |
| 5998 | |
| 5999 | GROW; |
| 6000 | |
| 6001 | if (RAW == '&') { |
| 6002 | NEXT; |
| 6003 | name = xmlParseName(ctxt); |
| 6004 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6005 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6006 | "xmlParseEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6007 | } else { |
| 6008 | if (RAW == ';') { |
| 6009 | NEXT; |
| 6010 | /* |
| 6011 | * Ask first SAX for entity resolution, otherwise try the |
| 6012 | * predefined set. |
| 6013 | */ |
| 6014 | if (ctxt->sax != NULL) { |
| 6015 | if (ctxt->sax->getEntity != NULL) |
| 6016 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6017 | if ((ctxt->wellFormed == 1 ) && (ent == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6019 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 6020 | (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 6021 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6022 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6023 | } |
| 6024 | /* |
| 6025 | * [ WFC: Entity Declared ] |
| 6026 | * In a document without any DTD, a document with only an |
| 6027 | * internal DTD subset which contains no parameter entity |
| 6028 | * references, or a document with "standalone='yes'", the |
| 6029 | * Name given in the entity reference must match that in an |
| 6030 | * entity declaration, except that well-formed documents |
| 6031 | * need not declare any of the following entities: amp, lt, |
| 6032 | * gt, apos, quot. |
| 6033 | * The declaration of a parameter entity must precede any |
| 6034 | * reference to it. |
| 6035 | * Similarly, the declaration of a general entity must |
| 6036 | * precede any reference to it which appears in a default |
| 6037 | * value in an attribute-list declaration. Note that if |
| 6038 | * entities are declared in the external subset or in |
| 6039 | * external parameter entities, a non-validating processor |
| 6040 | * is not obligated to read and process their declarations; |
| 6041 | * for such documents, the rule that an entity must be |
| 6042 | * declared is a well-formedness constraint only if |
| 6043 | * standalone='yes'. |
| 6044 | */ |
| 6045 | if (ent == NULL) { |
| 6046 | if ((ctxt->standalone == 1) || |
| 6047 | ((ctxt->hasExternalSubset == 0) && |
| 6048 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6049 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6050 | "Entity '%s' not defined\n", name); |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 6051 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6052 | } else { |
| 6053 | ctxt->errNo = XML_WAR_UNDECLARED_ENTITY; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 6054 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
Daniel Veillard | 1164810 | 2001-06-26 16:08:24 +0000 | [diff] [blame] | 6055 | ctxt->sax->error(ctxt->userData, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6056 | "Entity '%s' not defined\n", name); |
Daniel Veillard | d01fd3e | 2002-02-18 22:27:47 +0000 | [diff] [blame] | 6057 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6058 | } |
| 6059 | } |
| 6060 | |
| 6061 | /* |
| 6062 | * [ WFC: Parsed Entity ] |
| 6063 | * An entity reference must not contain the name of an |
| 6064 | * unparsed entity |
| 6065 | */ |
| 6066 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6067 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6068 | "Entity reference to unparsed entity %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | } |
| 6070 | |
| 6071 | /* |
| 6072 | * [ WFC: No External Entity References ] |
| 6073 | * Attribute values cannot contain direct or indirect |
| 6074 | * entity references to external entities. |
| 6075 | */ |
| 6076 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6077 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6078 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 6079 | "Attribute references external entity '%s'\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6080 | } |
| 6081 | /* |
| 6082 | * [ WFC: No < in Attribute Values ] |
| 6083 | * The replacement text of any entity referred to directly or |
| 6084 | * indirectly in an attribute value (other than "<") must |
| 6085 | * not contain a <. |
| 6086 | */ |
| 6087 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6088 | (ent != NULL) && |
| 6089 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 6090 | (ent->content != NULL) && |
| 6091 | (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6092 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6093 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6094 | } |
| 6095 | |
| 6096 | /* |
| 6097 | * Internal check, no parameter entities here ... |
| 6098 | */ |
| 6099 | else { |
| 6100 | switch (ent->etype) { |
| 6101 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 6102 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6103 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 6104 | "Attempt to reference the parameter entity '%s'\n", |
| 6105 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6106 | break; |
| 6107 | default: |
| 6108 | break; |
| 6109 | } |
| 6110 | } |
| 6111 | |
| 6112 | /* |
| 6113 | * [ WFC: No Recursion ] |
| 6114 | * A parsed entity must not contain a recursive reference |
| 6115 | * to itself, either directly or indirectly. |
| 6116 | * Done somewhere else |
| 6117 | */ |
| 6118 | |
| 6119 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6120 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6121 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | return(ent); |
| 6125 | } |
| 6126 | |
| 6127 | /** |
| 6128 | * xmlParseStringEntityRef: |
| 6129 | * @ctxt: an XML parser context |
| 6130 | * @str: a pointer to an index in the string |
| 6131 | * |
| 6132 | * parse ENTITY references declarations, but this version parses it from |
| 6133 | * a string value. |
| 6134 | * |
| 6135 | * [68] EntityRef ::= '&' Name ';' |
| 6136 | * |
| 6137 | * [ WFC: Entity Declared ] |
| 6138 | * In a document without any DTD, a document with only an internal DTD |
| 6139 | * subset which contains no parameter entity references, or a document |
| 6140 | * with "standalone='yes'", the Name given in the entity reference |
| 6141 | * must match that in an entity declaration, except that well-formed |
| 6142 | * documents need not declare any of the following entities: amp, lt, |
| 6143 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 6144 | * any reference to it. Similarly, the declaration of a general entity |
| 6145 | * must precede any reference to it which appears in a default value in an |
| 6146 | * attribute-list declaration. Note that if entities are declared in the |
| 6147 | * external subset or in external parameter entities, a non-validating |
| 6148 | * processor is not obligated to read and process their declarations; |
| 6149 | * for such documents, the rule that an entity must be declared is a |
| 6150 | * well-formedness constraint only if standalone='yes'. |
| 6151 | * |
| 6152 | * [ WFC: Parsed Entity ] |
| 6153 | * An entity reference must not contain the name of an unparsed entity |
| 6154 | * |
| 6155 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 6156 | * is updated to the current location in the string. |
| 6157 | */ |
| 6158 | xmlEntityPtr |
| 6159 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 6160 | xmlChar *name; |
| 6161 | const xmlChar *ptr; |
| 6162 | xmlChar cur; |
| 6163 | xmlEntityPtr ent = NULL; |
| 6164 | |
| 6165 | if ((str == NULL) || (*str == NULL)) |
| 6166 | return(NULL); |
| 6167 | ptr = *str; |
| 6168 | cur = *ptr; |
| 6169 | if (cur == '&') { |
| 6170 | ptr++; |
| 6171 | cur = *ptr; |
| 6172 | name = xmlParseStringName(ctxt, &ptr); |
| 6173 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6174 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6175 | "xmlParseStringEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6176 | } else { |
| 6177 | if (*ptr == ';') { |
| 6178 | ptr++; |
| 6179 | /* |
| 6180 | * Ask first SAX for entity resolution, otherwise try the |
| 6181 | * predefined set. |
| 6182 | */ |
| 6183 | if (ctxt->sax != NULL) { |
| 6184 | if (ctxt->sax->getEntity != NULL) |
| 6185 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
| 6186 | if (ent == NULL) |
| 6187 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6188 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 6189 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6190 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6191 | } |
| 6192 | /* |
| 6193 | * [ WFC: Entity Declared ] |
| 6194 | * In a document without any DTD, a document with only an |
| 6195 | * internal DTD subset which contains no parameter entity |
| 6196 | * references, or a document with "standalone='yes'", the |
| 6197 | * Name given in the entity reference must match that in an |
| 6198 | * entity declaration, except that well-formed documents |
| 6199 | * need not declare any of the following entities: amp, lt, |
| 6200 | * gt, apos, quot. |
| 6201 | * The declaration of a parameter entity must precede any |
| 6202 | * reference to it. |
| 6203 | * Similarly, the declaration of a general entity must |
| 6204 | * precede any reference to it which appears in a default |
| 6205 | * value in an attribute-list declaration. Note that if |
| 6206 | * entities are declared in the external subset or in |
| 6207 | * external parameter entities, a non-validating processor |
| 6208 | * is not obligated to read and process their declarations; |
| 6209 | * for such documents, the rule that an entity must be |
| 6210 | * declared is a well-formedness constraint only if |
| 6211 | * standalone='yes'. |
| 6212 | */ |
| 6213 | if (ent == NULL) { |
| 6214 | if ((ctxt->standalone == 1) || |
| 6215 | ((ctxt->hasExternalSubset == 0) && |
| 6216 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6217 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | "Entity '%s' not defined\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6219 | } else { |
| 6220 | ctxt->errNo = XML_WAR_UNDECLARED_ENTITY; |
| 6221 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6222 | ctxt->sax->warning(ctxt->userData, |
| 6223 | "Entity '%s' not defined\n", name); |
| 6224 | } |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * [ WFC: Parsed Entity ] |
| 6229 | * An entity reference must not contain the name of an |
| 6230 | * unparsed entity |
| 6231 | */ |
| 6232 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 6233 | ctxt->errNo = XML_ERR_UNPARSED_ENTITY; |
| 6234 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 6235 | ctxt->sax->error(ctxt->userData, |
| 6236 | "Entity reference to unparsed entity %s\n", name); |
| 6237 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6238 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6239 | } |
| 6240 | |
| 6241 | /* |
| 6242 | * [ WFC: No External Entity References ] |
| 6243 | * Attribute values cannot contain direct or indirect |
| 6244 | * entity references to external entities. |
| 6245 | */ |
| 6246 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6247 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 6248 | ctxt->errNo = XML_ERR_ENTITY_IS_EXTERNAL; |
| 6249 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 6250 | ctxt->sax->error(ctxt->userData, |
| 6251 | "Attribute references external entity '%s'\n", name); |
| 6252 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6253 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | } |
| 6255 | /* |
| 6256 | * [ WFC: No < in Attribute Values ] |
| 6257 | * The replacement text of any entity referred to directly or |
| 6258 | * indirectly in an attribute value (other than "<") must |
| 6259 | * not contain a <. |
| 6260 | */ |
| 6261 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6262 | (ent != NULL) && |
| 6263 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 6264 | (ent->content != NULL) && |
| 6265 | (xmlStrchr(ent->content, '<'))) { |
| 6266 | ctxt->errNo = XML_ERR_LT_IN_ATTRIBUTE; |
| 6267 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 6268 | ctxt->sax->error(ctxt->userData, |
| 6269 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 6270 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6271 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | /* |
| 6275 | * Internal check, no parameter entities here ... |
| 6276 | */ |
| 6277 | else { |
| 6278 | switch (ent->etype) { |
| 6279 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 6280 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 6281 | ctxt->errNo = XML_ERR_ENTITY_IS_PARAMETER; |
| 6282 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 6283 | ctxt->sax->error(ctxt->userData, |
| 6284 | "Attempt to reference the parameter entity '%s'\n", name); |
| 6285 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6286 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6287 | break; |
| 6288 | default: |
| 6289 | break; |
| 6290 | } |
| 6291 | } |
| 6292 | |
| 6293 | /* |
| 6294 | * [ WFC: No Recursion ] |
| 6295 | * A parsed entity must not contain a recursive reference |
| 6296 | * to itself, either directly or indirectly. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6297 | * Done somewhere else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6298 | */ |
| 6299 | |
| 6300 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6301 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6302 | } |
| 6303 | xmlFree(name); |
| 6304 | } |
| 6305 | } |
| 6306 | *str = ptr; |
| 6307 | return(ent); |
| 6308 | } |
| 6309 | |
| 6310 | /** |
| 6311 | * xmlParsePEReference: |
| 6312 | * @ctxt: an XML parser context |
| 6313 | * |
| 6314 | * parse PEReference declarations |
| 6315 | * The entity content is handled directly by pushing it's content as |
| 6316 | * a new input stream. |
| 6317 | * |
| 6318 | * [69] PEReference ::= '%' Name ';' |
| 6319 | * |
| 6320 | * [ WFC: No Recursion ] |
| 6321 | * A parsed entity must not contain a recursive |
| 6322 | * reference to itself, either directly or indirectly. |
| 6323 | * |
| 6324 | * [ WFC: Entity Declared ] |
| 6325 | * In a document without any DTD, a document with only an internal DTD |
| 6326 | * subset which contains no parameter entity references, or a document |
| 6327 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6328 | * entity must precede any reference to it... |
| 6329 | * |
| 6330 | * [ VC: Entity Declared ] |
| 6331 | * In a document with an external subset or external parameter entities |
| 6332 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6333 | * must precede any reference to it... |
| 6334 | * |
| 6335 | * [ WFC: In DTD ] |
| 6336 | * Parameter-entity references may only appear in the DTD. |
| 6337 | * NOTE: misleading but this is handled. |
| 6338 | */ |
| 6339 | void |
| 6340 | xmlParsePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6341 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | xmlEntityPtr entity = NULL; |
| 6343 | xmlParserInputPtr input; |
| 6344 | |
| 6345 | if (RAW == '%') { |
| 6346 | NEXT; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6347 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6348 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6349 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6350 | "xmlParsePEReference: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6351 | } else { |
| 6352 | if (RAW == ';') { |
| 6353 | NEXT; |
| 6354 | if ((ctxt->sax != NULL) && |
| 6355 | (ctxt->sax->getParameterEntity != NULL)) |
| 6356 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6357 | name); |
| 6358 | if (entity == NULL) { |
| 6359 | /* |
| 6360 | * [ WFC: Entity Declared ] |
| 6361 | * In a document without any DTD, a document with only an |
| 6362 | * internal DTD subset which contains no parameter entity |
| 6363 | * references, or a document with "standalone='yes'", ... |
| 6364 | * ... The declaration of a parameter entity must precede |
| 6365 | * any reference to it... |
| 6366 | */ |
| 6367 | if ((ctxt->standalone == 1) || |
| 6368 | ((ctxt->hasExternalSubset == 0) && |
| 6369 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6370 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6371 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6372 | } else { |
| 6373 | /* |
| 6374 | * [ VC: Entity Declared ] |
| 6375 | * In a document with an external subset or external |
| 6376 | * parameter entities with "standalone='no'", ... |
| 6377 | * ... The declaration of a parameter entity must precede |
| 6378 | * any reference to it... |
| 6379 | */ |
| 6380 | if ((!ctxt->disableSAX) && |
| 6381 | (ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6382 | ctxt->sax->warning(ctxt->userData, |
| 6383 | "PEReference: %%%s; not found\n", name); |
| 6384 | ctxt->valid = 0; |
| 6385 | } |
| 6386 | } else { |
| 6387 | /* |
| 6388 | * Internal checking in case the entity quest barfed |
| 6389 | */ |
| 6390 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6391 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 6392 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6393 | ctxt->sax->warning(ctxt->userData, |
| 6394 | "Internal: %%%s; is not a parameter entity\n", name); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 6395 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 6396 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 6397 | xmlPushInput(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6398 | } else { |
| 6399 | /* |
| 6400 | * TODO !!! |
| 6401 | * handle the extra spaces added before and after |
| 6402 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 6403 | */ |
| 6404 | input = xmlNewEntityInputStream(ctxt, entity); |
| 6405 | xmlPushInput(ctxt, input); |
| 6406 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 6407 | (RAW == '<') && (NXT(1) == '?') && |
| 6408 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 6409 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 6410 | xmlParseTextDecl(ctxt); |
| 6411 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6412 | /* |
| 6413 | * The XML REC instructs us to stop parsing |
| 6414 | * right here |
| 6415 | */ |
| 6416 | ctxt->instate = XML_PARSER_EOF; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6417 | return; |
| 6418 | } |
| 6419 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6420 | } |
| 6421 | } |
| 6422 | ctxt->hasPErefs = 1; |
| 6423 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6424 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6425 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6426 | } |
| 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | /** |
| 6431 | * xmlParseStringPEReference: |
| 6432 | * @ctxt: an XML parser context |
| 6433 | * @str: a pointer to an index in the string |
| 6434 | * |
| 6435 | * parse PEReference declarations |
| 6436 | * |
| 6437 | * [69] PEReference ::= '%' Name ';' |
| 6438 | * |
| 6439 | * [ WFC: No Recursion ] |
| 6440 | * A parsed entity must not contain a recursive |
| 6441 | * reference to itself, either directly or indirectly. |
| 6442 | * |
| 6443 | * [ WFC: Entity Declared ] |
| 6444 | * In a document without any DTD, a document with only an internal DTD |
| 6445 | * subset which contains no parameter entity references, or a document |
| 6446 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6447 | * entity must precede any reference to it... |
| 6448 | * |
| 6449 | * [ VC: Entity Declared ] |
| 6450 | * In a document with an external subset or external parameter entities |
| 6451 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6452 | * must precede any reference to it... |
| 6453 | * |
| 6454 | * [ WFC: In DTD ] |
| 6455 | * Parameter-entity references may only appear in the DTD. |
| 6456 | * NOTE: misleading but this is handled. |
| 6457 | * |
| 6458 | * Returns the string of the entity content. |
| 6459 | * str is updated to the current value of the index |
| 6460 | */ |
| 6461 | xmlEntityPtr |
| 6462 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 6463 | const xmlChar *ptr; |
| 6464 | xmlChar cur; |
| 6465 | xmlChar *name; |
| 6466 | xmlEntityPtr entity = NULL; |
| 6467 | |
| 6468 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 6469 | ptr = *str; |
| 6470 | cur = *ptr; |
| 6471 | if (cur == '%') { |
| 6472 | ptr++; |
| 6473 | cur = *ptr; |
| 6474 | name = xmlParseStringName(ctxt, &ptr); |
| 6475 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6476 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6477 | "xmlParseStringPEReference: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6478 | } else { |
| 6479 | cur = *ptr; |
| 6480 | if (cur == ';') { |
| 6481 | ptr++; |
| 6482 | cur = *ptr; |
| 6483 | if ((ctxt->sax != NULL) && |
| 6484 | (ctxt->sax->getParameterEntity != NULL)) |
| 6485 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6486 | name); |
| 6487 | if (entity == NULL) { |
| 6488 | /* |
| 6489 | * [ WFC: Entity Declared ] |
| 6490 | * In a document without any DTD, a document with only an |
| 6491 | * internal DTD subset which contains no parameter entity |
| 6492 | * references, or a document with "standalone='yes'", ... |
| 6493 | * ... The declaration of a parameter entity must precede |
| 6494 | * any reference to it... |
| 6495 | */ |
| 6496 | if ((ctxt->standalone == 1) || |
| 6497 | ((ctxt->hasExternalSubset == 0) && |
| 6498 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6499 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6500 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6501 | } else { |
| 6502 | /* |
| 6503 | * [ VC: Entity Declared ] |
| 6504 | * In a document with an external subset or external |
| 6505 | * parameter entities with "standalone='no'", ... |
| 6506 | * ... The declaration of a parameter entity must |
| 6507 | * precede any reference to it... |
| 6508 | */ |
| 6509 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6510 | ctxt->sax->warning(ctxt->userData, |
| 6511 | "PEReference: %%%s; not found\n", name); |
| 6512 | ctxt->valid = 0; |
| 6513 | } |
| 6514 | } else { |
| 6515 | /* |
| 6516 | * Internal checking in case the entity quest barfed |
| 6517 | */ |
| 6518 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6519 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 6520 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6521 | ctxt->sax->warning(ctxt->userData, |
| 6522 | "Internal: %%%s; is not a parameter entity\n", name); |
| 6523 | } |
| 6524 | } |
| 6525 | ctxt->hasPErefs = 1; |
| 6526 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6527 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6528 | } |
| 6529 | xmlFree(name); |
| 6530 | } |
| 6531 | } |
| 6532 | *str = ptr; |
| 6533 | return(entity); |
| 6534 | } |
| 6535 | |
| 6536 | /** |
| 6537 | * xmlParseDocTypeDecl: |
| 6538 | * @ctxt: an XML parser context |
| 6539 | * |
| 6540 | * parse a DOCTYPE declaration |
| 6541 | * |
| 6542 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 6543 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6544 | * |
| 6545 | * [ VC: Root Element Type ] |
| 6546 | * The Name in the document type declaration must match the element |
| 6547 | * type of the root element. |
| 6548 | */ |
| 6549 | |
| 6550 | void |
| 6551 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6552 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6553 | xmlChar *ExternalID = NULL; |
| 6554 | xmlChar *URI = NULL; |
| 6555 | |
| 6556 | /* |
| 6557 | * We know that '<!DOCTYPE' has been detected. |
| 6558 | */ |
| 6559 | SKIP(9); |
| 6560 | |
| 6561 | SKIP_BLANKS; |
| 6562 | |
| 6563 | /* |
| 6564 | * Parse the DOCTYPE name. |
| 6565 | */ |
| 6566 | name = xmlParseName(ctxt); |
| 6567 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6568 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6569 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6570 | } |
| 6571 | ctxt->intSubName = name; |
| 6572 | |
| 6573 | SKIP_BLANKS; |
| 6574 | |
| 6575 | /* |
| 6576 | * Check for SystemID and ExternalID |
| 6577 | */ |
| 6578 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 6579 | |
| 6580 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 6581 | ctxt->hasExternalSubset = 1; |
| 6582 | } |
| 6583 | ctxt->extSubURI = URI; |
| 6584 | ctxt->extSubSystem = ExternalID; |
| 6585 | |
| 6586 | SKIP_BLANKS; |
| 6587 | |
| 6588 | /* |
| 6589 | * Create and update the internal subset. |
| 6590 | */ |
| 6591 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 6592 | (!ctxt->disableSAX)) |
| 6593 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 6594 | |
| 6595 | /* |
| 6596 | * Is there any internal subset declarations ? |
| 6597 | * they are handled separately in xmlParseInternalSubset() |
| 6598 | */ |
| 6599 | if (RAW == '[') |
| 6600 | return; |
| 6601 | |
| 6602 | /* |
| 6603 | * We should be at the end of the DOCTYPE declaration. |
| 6604 | */ |
| 6605 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6606 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6607 | } |
| 6608 | NEXT; |
| 6609 | } |
| 6610 | |
| 6611 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6612 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6613 | * @ctxt: an XML parser context |
| 6614 | * |
| 6615 | * parse the internal subset declaration |
| 6616 | * |
| 6617 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6618 | */ |
| 6619 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6620 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6621 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 6622 | /* |
| 6623 | * Is there any DTD definition ? |
| 6624 | */ |
| 6625 | if (RAW == '[') { |
| 6626 | ctxt->instate = XML_PARSER_DTD; |
| 6627 | NEXT; |
| 6628 | /* |
| 6629 | * Parse the succession of Markup declarations and |
| 6630 | * PEReferences. |
| 6631 | * Subsequence (markupdecl | PEReference | S)* |
| 6632 | */ |
| 6633 | while (RAW != ']') { |
| 6634 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6635 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6636 | |
| 6637 | SKIP_BLANKS; |
| 6638 | xmlParseMarkupDecl(ctxt); |
| 6639 | xmlParsePEReference(ctxt); |
| 6640 | |
| 6641 | /* |
| 6642 | * Pop-up of finished entities. |
| 6643 | */ |
| 6644 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6645 | xmlPopInput(ctxt); |
| 6646 | |
| 6647 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6648 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6649 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6650 | break; |
| 6651 | } |
| 6652 | } |
| 6653 | if (RAW == ']') { |
| 6654 | NEXT; |
| 6655 | SKIP_BLANKS; |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | /* |
| 6660 | * We should be at the end of the DOCTYPE declaration. |
| 6661 | */ |
| 6662 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6663 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6664 | } |
| 6665 | NEXT; |
| 6666 | } |
| 6667 | |
| 6668 | /** |
| 6669 | * xmlParseAttribute: |
| 6670 | * @ctxt: an XML parser context |
| 6671 | * @value: a xmlChar ** used to store the value of the attribute |
| 6672 | * |
| 6673 | * parse an attribute |
| 6674 | * |
| 6675 | * [41] Attribute ::= Name Eq AttValue |
| 6676 | * |
| 6677 | * [ WFC: No External Entity References ] |
| 6678 | * Attribute values cannot contain direct or indirect entity references |
| 6679 | * to external entities. |
| 6680 | * |
| 6681 | * [ WFC: No < in Attribute Values ] |
| 6682 | * The replacement text of any entity referred to directly or indirectly in |
| 6683 | * an attribute value (other than "<") must not contain a <. |
| 6684 | * |
| 6685 | * [ VC: Attribute Value Type ] |
| 6686 | * The attribute must have been declared; the value must be of the type |
| 6687 | * declared for it. |
| 6688 | * |
| 6689 | * [25] Eq ::= S? '=' S? |
| 6690 | * |
| 6691 | * With namespace: |
| 6692 | * |
| 6693 | * [NS 11] Attribute ::= QName Eq AttValue |
| 6694 | * |
| 6695 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 6696 | * definition. |
| 6697 | * |
| 6698 | * Returns the attribute name, and the value in *value. |
| 6699 | */ |
| 6700 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6701 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6702 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6703 | const xmlChar *name; |
| 6704 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6705 | |
| 6706 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6707 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6708 | name = xmlParseName(ctxt); |
| 6709 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6710 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6711 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6712 | return(NULL); |
| 6713 | } |
| 6714 | |
| 6715 | /* |
| 6716 | * read the value |
| 6717 | */ |
| 6718 | SKIP_BLANKS; |
| 6719 | if (RAW == '=') { |
| 6720 | NEXT; |
| 6721 | SKIP_BLANKS; |
| 6722 | val = xmlParseAttValue(ctxt); |
| 6723 | ctxt->instate = XML_PARSER_CONTENT; |
| 6724 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6725 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6726 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6727 | return(NULL); |
| 6728 | } |
| 6729 | |
| 6730 | /* |
| 6731 | * Check that xml:lang conforms to the specification |
| 6732 | * No more registered as an error, just generate a warning now |
| 6733 | * since this was deprecated in XML second edition |
| 6734 | */ |
| 6735 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 6736 | if (!xmlCheckLanguageID(val)) { |
| 6737 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 6738 | ctxt->sax->warning(ctxt->userData, |
| 6739 | "Malformed value for xml:lang : %s\n", val); |
| 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | /* |
| 6744 | * Check that xml:space conforms to the specification |
| 6745 | */ |
| 6746 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 6747 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 6748 | *(ctxt->space) = 0; |
| 6749 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 6750 | *(ctxt->space) = 1; |
| 6751 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6752 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 6753 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6754 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6755 | } |
| 6756 | } |
| 6757 | |
| 6758 | *value = val; |
| 6759 | return(name); |
| 6760 | } |
| 6761 | |
| 6762 | /** |
| 6763 | * xmlParseStartTag: |
| 6764 | * @ctxt: an XML parser context |
| 6765 | * |
| 6766 | * parse a start of tag either for rule element or |
| 6767 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 6768 | * |
| 6769 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 6770 | * |
| 6771 | * [ WFC: Unique Att Spec ] |
| 6772 | * No attribute name may appear more than once in the same start-tag or |
| 6773 | * empty-element tag. |
| 6774 | * |
| 6775 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 6776 | * |
| 6777 | * [ WFC: Unique Att Spec ] |
| 6778 | * No attribute name may appear more than once in the same start-tag or |
| 6779 | * empty-element tag. |
| 6780 | * |
| 6781 | * With namespace: |
| 6782 | * |
| 6783 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 6784 | * |
| 6785 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 6786 | * |
| 6787 | * Returns the element name parsed |
| 6788 | */ |
| 6789 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6790 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6791 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6792 | const xmlChar *name; |
| 6793 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6794 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6795 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6796 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6797 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6798 | int i; |
| 6799 | |
| 6800 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6801 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6802 | |
| 6803 | name = xmlParseName(ctxt); |
| 6804 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6805 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6806 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6807 | return(NULL); |
| 6808 | } |
| 6809 | |
| 6810 | /* |
| 6811 | * Now parse the attributes, it ends up with the ending |
| 6812 | * |
| 6813 | * (S Attribute)* S? |
| 6814 | */ |
| 6815 | SKIP_BLANKS; |
| 6816 | GROW; |
| 6817 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6818 | while ((RAW != '>') && |
| 6819 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 6820 | (IS_CHAR((unsigned int) RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6821 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6822 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6823 | |
| 6824 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 6825 | if ((attname != NULL) && (attvalue != NULL)) { |
| 6826 | /* |
| 6827 | * [ WFC: Unique Att Spec ] |
| 6828 | * No attribute name may appear more than once in the same |
| 6829 | * start-tag or empty-element tag. |
| 6830 | */ |
| 6831 | for (i = 0; i < nbatts;i += 2) { |
| 6832 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6833 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6834 | xmlFree(attvalue); |
| 6835 | goto failed; |
| 6836 | } |
| 6837 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6838 | /* |
| 6839 | * Add the pair to atts |
| 6840 | */ |
| 6841 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6842 | maxatts = 22; /* allow for 10 attrs by default */ |
| 6843 | atts = (const xmlChar **) |
| 6844 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6845 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6846 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6847 | if (attvalue != NULL) |
| 6848 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6849 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6850 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6851 | ctxt->atts = atts; |
| 6852 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6853 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6854 | const xmlChar **n; |
| 6855 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6856 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6857 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6858 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6859 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6860 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6861 | if (attvalue != NULL) |
| 6862 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6863 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6864 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6865 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6866 | ctxt->atts = atts; |
| 6867 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6868 | } |
| 6869 | atts[nbatts++] = attname; |
| 6870 | atts[nbatts++] = attvalue; |
| 6871 | atts[nbatts] = NULL; |
| 6872 | atts[nbatts + 1] = NULL; |
| 6873 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6874 | if (attvalue != NULL) |
| 6875 | xmlFree(attvalue); |
| 6876 | } |
| 6877 | |
| 6878 | failed: |
| 6879 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 6880 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6881 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 6882 | break; |
| 6883 | if (!IS_BLANK(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6884 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6885 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | } |
| 6887 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 6888 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 6889 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6890 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6891 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6892 | break; |
| 6893 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6894 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | GROW; |
| 6896 | } |
| 6897 | |
| 6898 | /* |
| 6899 | * SAX: Start of Element ! |
| 6900 | */ |
| 6901 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6902 | (!ctxt->disableSAX)) { |
| 6903 | if (nbatts > 0) |
| 6904 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 6905 | else |
| 6906 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 6907 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6908 | |
| 6909 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6910 | /* Free only the content strings */ |
| 6911 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6912 | if (atts[i] != NULL) |
| 6913 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6914 | } |
| 6915 | return(name); |
| 6916 | } |
| 6917 | |
| 6918 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6919 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6920 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6921 | * @line: line of the start tag |
| 6922 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6923 | * |
| 6924 | * parse an end of tag |
| 6925 | * |
| 6926 | * [42] ETag ::= '</' Name S? '>' |
| 6927 | * |
| 6928 | * With namespace |
| 6929 | * |
| 6930 | * [NS 9] ETag ::= '</' QName S? '>' |
| 6931 | */ |
| 6932 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6933 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6934 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6935 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6936 | |
| 6937 | GROW; |
| 6938 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6939 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6940 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6941 | return; |
| 6942 | } |
| 6943 | SKIP(2); |
| 6944 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6945 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6946 | |
| 6947 | /* |
| 6948 | * We should definitely be at the ending "S? '>'" part |
| 6949 | */ |
| 6950 | GROW; |
| 6951 | SKIP_BLANKS; |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 6952 | if ((!IS_CHAR((unsigned int) RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6953 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6954 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6955 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6956 | |
| 6957 | /* |
| 6958 | * [ WFC: Element Type Match ] |
| 6959 | * The Name in an element's end-tag must match the element type in the |
| 6960 | * start-tag. |
| 6961 | * |
| 6962 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6963 | if (name != (xmlChar*)1) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6964 | ctxt->errNo = XML_ERR_TAG_NAME_MISMATCH; |
| 6965 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6966 | if (name != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6967 | ctxt->sax->error(ctxt->userData, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6968 | "Opening and ending tag mismatch: %s line %d and %s\n", |
| 6969 | ctxt->name, line, name); |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6970 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6971 | ctxt->sax->error(ctxt->userData, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6972 | "Ending tag error for: %s line %d\n", ctxt->name, line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | } |
| 6976 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6977 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6978 | } |
| 6979 | |
| 6980 | /* |
| 6981 | * SAX: End of Tag |
| 6982 | */ |
| 6983 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 6984 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6985 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6986 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6987 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6988 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6989 | return; |
| 6990 | } |
| 6991 | |
| 6992 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6993 | * xmlParseEndTag: |
| 6994 | * @ctxt: an XML parser context |
| 6995 | * |
| 6996 | * parse an end of tag |
| 6997 | * |
| 6998 | * [42] ETag ::= '</' Name S? '>' |
| 6999 | * |
| 7000 | * With namespace |
| 7001 | * |
| 7002 | * [NS 9] ETag ::= '</' QName S? '>' |
| 7003 | */ |
| 7004 | |
| 7005 | void |
| 7006 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7007 | xmlParseEndTag1(ctxt, 0); |
| 7008 | } |
| 7009 | |
| 7010 | /************************************************************************ |
| 7011 | * * |
| 7012 | * SAX 2 specific operations * |
| 7013 | * * |
| 7014 | ************************************************************************/ |
| 7015 | |
| 7016 | static const xmlChar * |
| 7017 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 7018 | int len = 0, l; |
| 7019 | int c; |
| 7020 | int count = 0; |
| 7021 | |
| 7022 | /* |
| 7023 | * Handler for more complex cases |
| 7024 | */ |
| 7025 | GROW; |
| 7026 | c = CUR_CHAR(l); |
| 7027 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7028 | (!IS_LETTER(c) && (c != '_'))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7029 | return(NULL); |
| 7030 | } |
| 7031 | |
| 7032 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 7033 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7034 | (c == '.') || (c == '-') || (c == '_') || |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7035 | (IS_COMBINING(c)) || |
| 7036 | (IS_EXTENDER(c)))) { |
| 7037 | if (count++ > 100) { |
| 7038 | count = 0; |
| 7039 | GROW; |
| 7040 | } |
| 7041 | len += l; |
| 7042 | NEXTL(l); |
| 7043 | c = CUR_CHAR(l); |
| 7044 | } |
| 7045 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 7046 | } |
| 7047 | |
| 7048 | /* |
| 7049 | * xmlGetNamespace: |
| 7050 | * @ctxt: an XML parser context |
| 7051 | * @prefix: the prefix to lookup |
| 7052 | * |
| 7053 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 7054 | * The prefix must come from the @ctxt->dict dictionnary |
| 7055 | * |
| 7056 | * Returns the namespace name or NULL if not bound |
| 7057 | */ |
| 7058 | static const xmlChar * |
| 7059 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 7060 | int i; |
| 7061 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7062 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7063 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7064 | if (ctxt->nsTab[i] == prefix) { |
| 7065 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 7066 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7067 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7068 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7069 | return(NULL); |
| 7070 | } |
| 7071 | |
| 7072 | /** |
| 7073 | * xmlParseNCName: |
| 7074 | * @ctxt: an XML parser context |
| 7075 | * |
| 7076 | * parse an XML name. |
| 7077 | * |
| 7078 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7079 | * CombiningChar | Extender |
| 7080 | * |
| 7081 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 7082 | * |
| 7083 | * Returns the Name parsed or NULL |
| 7084 | */ |
| 7085 | |
| 7086 | static const xmlChar * |
| 7087 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 7088 | const xmlChar *in; |
| 7089 | const xmlChar *ret; |
| 7090 | int count = 0; |
| 7091 | |
| 7092 | /* |
| 7093 | * Accelerator for simple ASCII names |
| 7094 | */ |
| 7095 | in = ctxt->input->cur; |
| 7096 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7097 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7098 | (*in == '_')) { |
| 7099 | in++; |
| 7100 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7101 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7102 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 7103 | (*in == '_') || (*in == '-') || |
| 7104 | (*in == '.')) |
| 7105 | in++; |
| 7106 | if ((*in > 0) && (*in < 0x80)) { |
| 7107 | count = in - ctxt->input->cur; |
| 7108 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 7109 | ctxt->input->cur = in; |
| 7110 | ctxt->nbChars += count; |
| 7111 | ctxt->input->col += count; |
| 7112 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7113 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7114 | } |
| 7115 | return(ret); |
| 7116 | } |
| 7117 | } |
| 7118 | return(xmlParseNCNameComplex(ctxt)); |
| 7119 | } |
| 7120 | |
| 7121 | /** |
| 7122 | * xmlParseQName: |
| 7123 | * @ctxt: an XML parser context |
| 7124 | * @prefix: pointer to store the prefix part |
| 7125 | * |
| 7126 | * parse an XML Namespace QName |
| 7127 | * |
| 7128 | * [6] QName ::= (Prefix ':')? LocalPart |
| 7129 | * [7] Prefix ::= NCName |
| 7130 | * [8] LocalPart ::= NCName |
| 7131 | * |
| 7132 | * Returns the Name parsed or NULL |
| 7133 | */ |
| 7134 | |
| 7135 | static const xmlChar * |
| 7136 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 7137 | const xmlChar *l, *p; |
| 7138 | |
| 7139 | GROW; |
| 7140 | |
| 7141 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7142 | if (l == NULL) { |
| 7143 | if (CUR == ':') { |
| 7144 | l = xmlParseName(ctxt); |
| 7145 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7146 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7147 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7148 | *prefix = NULL; |
| 7149 | return(l); |
| 7150 | } |
| 7151 | } |
| 7152 | return(NULL); |
| 7153 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7154 | if (CUR == ':') { |
| 7155 | NEXT; |
| 7156 | p = l; |
| 7157 | l = xmlParseNCName(ctxt); |
| 7158 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7159 | xmlChar *tmp; |
| 7160 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7161 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7162 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7163 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 7164 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7165 | if (tmp != NULL) xmlFree(tmp); |
| 7166 | *prefix = NULL; |
| 7167 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7168 | } |
| 7169 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7170 | xmlChar *tmp; |
| 7171 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7172 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7173 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7174 | NEXT; |
| 7175 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 7176 | if (tmp != NULL) { |
| 7177 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 7178 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7179 | if (tmp != NULL) xmlFree(tmp); |
| 7180 | *prefix = p; |
| 7181 | return(l); |
| 7182 | } |
| 7183 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 7184 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7185 | if (tmp != NULL) xmlFree(tmp); |
| 7186 | *prefix = p; |
| 7187 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7188 | } |
| 7189 | *prefix = p; |
| 7190 | } else |
| 7191 | *prefix = NULL; |
| 7192 | return(l); |
| 7193 | } |
| 7194 | |
| 7195 | /** |
| 7196 | * xmlParseQNameAndCompare: |
| 7197 | * @ctxt: an XML parser context |
| 7198 | * @name: the localname |
| 7199 | * @prefix: the prefix, if any. |
| 7200 | * |
| 7201 | * parse an XML name and compares for match |
| 7202 | * (specialized for endtag parsing) |
| 7203 | * |
| 7204 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 7205 | * and the name for mismatch |
| 7206 | */ |
| 7207 | |
| 7208 | static const xmlChar * |
| 7209 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 7210 | xmlChar const *prefix) { |
| 7211 | const xmlChar *cmp = name; |
| 7212 | const xmlChar *in; |
| 7213 | const xmlChar *ret; |
| 7214 | const xmlChar *prefix2; |
| 7215 | |
| 7216 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 7217 | |
| 7218 | GROW; |
| 7219 | in = ctxt->input->cur; |
| 7220 | |
| 7221 | cmp = prefix; |
| 7222 | while (*in != 0 && *in == *cmp) { |
| 7223 | ++in; |
| 7224 | ++cmp; |
| 7225 | } |
| 7226 | if ((*cmp == 0) && (*in == ':')) { |
| 7227 | in++; |
| 7228 | cmp = name; |
| 7229 | while (*in != 0 && *in == *cmp) { |
| 7230 | ++in; |
| 7231 | ++cmp; |
| 7232 | } |
| 7233 | if (*cmp == 0 && (*in == '>' || IS_BLANK (*in))) { |
| 7234 | /* success */ |
| 7235 | ctxt->input->cur = in; |
| 7236 | return((const xmlChar*) 1); |
| 7237 | } |
| 7238 | } |
| 7239 | /* |
| 7240 | * all strings coms from the dictionary, equality can be done directly |
| 7241 | */ |
| 7242 | ret = xmlParseQName (ctxt, &prefix2); |
| 7243 | if ((ret == name) && (prefix == prefix2)) |
| 7244 | return((const xmlChar*) 1); |
| 7245 | return ret; |
| 7246 | } |
| 7247 | |
| 7248 | /** |
| 7249 | * xmlParseAttValueInternal: |
| 7250 | * @ctxt: an XML parser context |
| 7251 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7252 | * @alloc: whether the attribute was reallocated as a new string |
| 7253 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7254 | * |
| 7255 | * parse a value for an attribute. |
| 7256 | * NOTE: if no normalization is needed, the routine will return pointers |
| 7257 | * directly from the data buffer. |
| 7258 | * |
| 7259 | * 3.3.3 Attribute-Value Normalization: |
| 7260 | * Before the value of an attribute is passed to the application or |
| 7261 | * checked for validity, the XML processor must normalize it as follows: |
| 7262 | * - a character reference is processed by appending the referenced |
| 7263 | * character to the attribute value |
| 7264 | * - an entity reference is processed by recursively processing the |
| 7265 | * replacement text of the entity |
| 7266 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 7267 | * appending #x20 to the normalized value, except that only a single |
| 7268 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 7269 | * parsed entity or the literal entity value of an internal parsed entity |
| 7270 | * - other characters are processed by appending them to the normalized value |
| 7271 | * If the declared value is not CDATA, then the XML processor must further |
| 7272 | * process the normalized attribute value by discarding any leading and |
| 7273 | * trailing space (#x20) characters, and by replacing sequences of space |
| 7274 | * (#x20) characters by a single space (#x20) character. |
| 7275 | * All attributes for which no declaration has been read should be treated |
| 7276 | * by a non-validating parser as if declared CDATA. |
| 7277 | * |
| 7278 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 7279 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 7280 | */ |
| 7281 | |
| 7282 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7283 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 7284 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7285 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7286 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7287 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7288 | xmlChar *ret = NULL; |
| 7289 | |
| 7290 | GROW; |
| 7291 | in = (xmlChar *) CUR_PTR; |
| 7292 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7293 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7294 | return (NULL); |
| 7295 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7296 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7297 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7298 | /* |
| 7299 | * try to handle in this routine the most common case where no |
| 7300 | * allocation of a new string is required and where content is |
| 7301 | * pure ASCII. |
| 7302 | */ |
| 7303 | limit = *in++; |
| 7304 | end = ctxt->input->end; |
| 7305 | start = in; |
| 7306 | if (in >= end) { |
| 7307 | const xmlChar *oldbase = ctxt->input->base; |
| 7308 | GROW; |
| 7309 | if (oldbase != ctxt->input->base) { |
| 7310 | long delta = ctxt->input->base - oldbase; |
| 7311 | start = start + delta; |
| 7312 | in = in + delta; |
| 7313 | } |
| 7314 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7315 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7316 | if (normalize) { |
| 7317 | /* |
| 7318 | * Skip any leading spaces |
| 7319 | */ |
| 7320 | while ((in < end) && (*in != limit) && |
| 7321 | ((*in == 0x20) || (*in == 0x9) || |
| 7322 | (*in == 0xA) || (*in == 0xD))) { |
| 7323 | in++; |
| 7324 | start = in; |
| 7325 | if (in >= end) { |
| 7326 | const xmlChar *oldbase = ctxt->input->base; |
| 7327 | GROW; |
| 7328 | if (oldbase != ctxt->input->base) { |
| 7329 | long delta = ctxt->input->base - oldbase; |
| 7330 | start = start + delta; |
| 7331 | in = in + delta; |
| 7332 | } |
| 7333 | end = ctxt->input->end; |
| 7334 | } |
| 7335 | } |
| 7336 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7337 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7338 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 7339 | if (in >= end) { |
| 7340 | const xmlChar *oldbase = ctxt->input->base; |
| 7341 | GROW; |
| 7342 | if (oldbase != ctxt->input->base) { |
| 7343 | long delta = ctxt->input->base - oldbase; |
| 7344 | start = start + delta; |
| 7345 | in = in + delta; |
| 7346 | } |
| 7347 | end = ctxt->input->end; |
| 7348 | } |
| 7349 | } |
| 7350 | last = in; |
| 7351 | /* |
| 7352 | * skip the trailing blanks |
| 7353 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 7354 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7355 | while ((in < end) && (*in != limit) && |
| 7356 | ((*in == 0x20) || (*in == 0x9) || |
| 7357 | (*in == 0xA) || (*in == 0xD))) { |
| 7358 | in++; |
| 7359 | if (in >= end) { |
| 7360 | const xmlChar *oldbase = ctxt->input->base; |
| 7361 | GROW; |
| 7362 | if (oldbase != ctxt->input->base) { |
| 7363 | long delta = ctxt->input->base - oldbase; |
| 7364 | start = start + delta; |
| 7365 | in = in + delta; |
| 7366 | last = last + delta; |
| 7367 | } |
| 7368 | end = ctxt->input->end; |
| 7369 | } |
| 7370 | } |
| 7371 | if (*in != limit) goto need_complex; |
| 7372 | } else { |
| 7373 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7374 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7375 | in++; |
| 7376 | if (in >= end) { |
| 7377 | const xmlChar *oldbase = ctxt->input->base; |
| 7378 | GROW; |
| 7379 | if (oldbase != ctxt->input->base) { |
| 7380 | long delta = ctxt->input->base - oldbase; |
| 7381 | start = start + delta; |
| 7382 | in = in + delta; |
| 7383 | } |
| 7384 | end = ctxt->input->end; |
| 7385 | } |
| 7386 | } |
| 7387 | last = in; |
| 7388 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7389 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7390 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7391 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7392 | *len = last - start; |
| 7393 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7394 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7395 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7396 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7397 | } |
| 7398 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7399 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7400 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7401 | need_complex: |
| 7402 | if (alloc) *alloc = 1; |
| 7403 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | /** |
| 7407 | * xmlParseAttribute2: |
| 7408 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7409 | * @pref: the element prefix |
| 7410 | * @elem: the element name |
| 7411 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7412 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7413 | * @len: an int * to save the length of the attribute |
| 7414 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7415 | * |
| 7416 | * parse an attribute in the new SAX2 framework. |
| 7417 | * |
| 7418 | * Returns the attribute name, and the value in *value, . |
| 7419 | */ |
| 7420 | |
| 7421 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7422 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
| 7423 | const xmlChar *pref, const xmlChar *elem, |
| 7424 | const xmlChar **prefix, xmlChar **value, |
| 7425 | int *len, int *alloc) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7426 | const xmlChar *name; |
| 7427 | xmlChar *val; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7428 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7429 | |
| 7430 | *value = NULL; |
| 7431 | GROW; |
| 7432 | name = xmlParseQName(ctxt, prefix); |
| 7433 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7434 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7435 | "error parsing attribute name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7436 | return(NULL); |
| 7437 | } |
| 7438 | |
| 7439 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7440 | * get the type if needed |
| 7441 | */ |
| 7442 | if (ctxt->attsSpecial != NULL) { |
| 7443 | int type; |
| 7444 | |
| 7445 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
| 7446 | pref, elem, *prefix, name); |
| 7447 | if (type != 0) normalize = 1; |
| 7448 | } |
| 7449 | |
| 7450 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7451 | * read the value |
| 7452 | */ |
| 7453 | SKIP_BLANKS; |
| 7454 | if (RAW == '=') { |
| 7455 | NEXT; |
| 7456 | SKIP_BLANKS; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7457 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7458 | ctxt->instate = XML_PARSER_CONTENT; |
| 7459 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7460 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7461 | "Specification mandate value for attribute %s\n", name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7462 | return(NULL); |
| 7463 | } |
| 7464 | |
| 7465 | /* |
| 7466 | * Check that xml:lang conforms to the specification |
| 7467 | * No more registered as an error, just generate a warning now |
| 7468 | * since this was deprecated in XML second edition |
| 7469 | */ |
| 7470 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 7471 | if (!xmlCheckLanguageID(val)) { |
| 7472 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 7473 | ctxt->sax->warning(ctxt->userData, |
| 7474 | "Malformed value for xml:lang : %s\n", val); |
| 7475 | } |
| 7476 | } |
| 7477 | |
| 7478 | /* |
| 7479 | * Check that xml:space conforms to the specification |
| 7480 | */ |
| 7481 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 7482 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 7483 | *(ctxt->space) = 0; |
| 7484 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 7485 | *(ctxt->space) = 1; |
| 7486 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7487 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7488 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 7489 | val); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7490 | } |
| 7491 | } |
| 7492 | |
| 7493 | *value = val; |
| 7494 | return(name); |
| 7495 | } |
| 7496 | |
| 7497 | /** |
| 7498 | * xmlParseStartTag2: |
| 7499 | * @ctxt: an XML parser context |
| 7500 | * |
| 7501 | * parse a start of tag either for rule element or |
| 7502 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 7503 | * This routine is called when running SAX2 parsing |
| 7504 | * |
| 7505 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 7506 | * |
| 7507 | * [ WFC: Unique Att Spec ] |
| 7508 | * No attribute name may appear more than once in the same start-tag or |
| 7509 | * empty-element tag. |
| 7510 | * |
| 7511 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 7512 | * |
| 7513 | * [ WFC: Unique Att Spec ] |
| 7514 | * No attribute name may appear more than once in the same start-tag or |
| 7515 | * empty-element tag. |
| 7516 | * |
| 7517 | * With namespace: |
| 7518 | * |
| 7519 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 7520 | * |
| 7521 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 7522 | * |
| 7523 | * Returns the element name parsed |
| 7524 | */ |
| 7525 | |
| 7526 | static const xmlChar * |
| 7527 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
| 7528 | const xmlChar **URI) { |
| 7529 | const xmlChar *localname; |
| 7530 | const xmlChar *prefix; |
| 7531 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7532 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7533 | const xmlChar *nsname; |
| 7534 | xmlChar *attvalue; |
| 7535 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7536 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7537 | int nratts, nbatts, nbdef; |
| 7538 | int i, j, nbNs, attval; |
| 7539 | const xmlChar *base; |
| 7540 | unsigned long cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7541 | |
| 7542 | if (RAW != '<') return(NULL); |
| 7543 | NEXT1; |
| 7544 | |
| 7545 | /* |
| 7546 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 7547 | * point since the attribute values may be stored as pointers to |
| 7548 | * the buffer and calling SHRINK would destroy them ! |
| 7549 | * The Shrinking is only possible once the full set of attribute |
| 7550 | * callbacks have been done. |
| 7551 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7552 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7553 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7554 | base = ctxt->input->base; |
| 7555 | cur = ctxt->input->cur - ctxt->input->base; |
| 7556 | nbatts = 0; |
| 7557 | nratts = 0; |
| 7558 | nbdef = 0; |
| 7559 | nbNs = 0; |
| 7560 | attval = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7561 | |
| 7562 | localname = xmlParseQName(ctxt, &prefix); |
| 7563 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7564 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7565 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7566 | return(NULL); |
| 7567 | } |
| 7568 | |
| 7569 | /* |
| 7570 | * Now parse the attributes, it ends up with the ending |
| 7571 | * |
| 7572 | * (S Attribute)* S? |
| 7573 | */ |
| 7574 | SKIP_BLANKS; |
| 7575 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7576 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7577 | |
| 7578 | while ((RAW != '>') && |
| 7579 | ((RAW != '/') || (NXT(1) != '>')) && |
| 7580 | (IS_CHAR((unsigned int) RAW))) { |
| 7581 | const xmlChar *q = CUR_PTR; |
| 7582 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7583 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7584 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7585 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 7586 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7587 | if ((attname != NULL) && (attvalue != NULL)) { |
| 7588 | if (len < 0) len = xmlStrlen(attvalue); |
| 7589 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7590 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7591 | xmlURIPtr uri; |
| 7592 | |
| 7593 | if (*URL != 0) { |
| 7594 | uri = xmlParseURI((const char *) URL); |
| 7595 | if (uri == NULL) { |
| 7596 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 7597 | ctxt->sax->warning(ctxt->userData, |
| 7598 | "xmlns: %s not a valid URI\n", URL); |
| 7599 | } else { |
| 7600 | if (uri->scheme == NULL) { |
| 7601 | if ((ctxt->sax != NULL) && |
| 7602 | (ctxt->sax->warning != NULL)) |
| 7603 | ctxt->sax->warning(ctxt->userData, |
| 7604 | "xmlns: URI %s is not absolute\n", URL); |
| 7605 | } |
| 7606 | xmlFreeURI(uri); |
| 7607 | } |
| 7608 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7609 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7610 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7611 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7612 | for (j = 1;j <= nbNs;j++) |
| 7613 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7614 | break; |
| 7615 | if (j <= nbNs) |
| 7616 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 7617 | else |
| 7618 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7619 | if (alloc != 0) xmlFree(attvalue); |
| 7620 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7621 | continue; |
| 7622 | } |
| 7623 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7624 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7625 | xmlURIPtr uri; |
| 7626 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7627 | if (attname == ctxt->str_xml) { |
| 7628 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7629 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 7630 | "xml namespace prefix mapped to wrong URI\n", |
| 7631 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7632 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7633 | /* |
| 7634 | * Do not keep a namespace definition node |
| 7635 | */ |
| 7636 | if (alloc != 0) xmlFree(attvalue); |
| 7637 | SKIP_BLANKS; |
| 7638 | continue; |
| 7639 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7640 | uri = xmlParseURI((const char *) URL); |
| 7641 | if (uri == NULL) { |
| 7642 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 7643 | ctxt->sax->warning(ctxt->userData, |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7644 | "xmlns:%s: '%s' is not a valid URI\n", |
| 7645 | attname, URL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7646 | } else { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7647 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7648 | if ((ctxt->sax != NULL) && |
| 7649 | (ctxt->sax->warning != NULL)) |
| 7650 | ctxt->sax->warning(ctxt->userData, |
| 7651 | "xmlns:%s: URI %s is not absolute\n", |
| 7652 | attname, URL); |
| 7653 | } |
| 7654 | xmlFreeURI(uri); |
| 7655 | } |
| 7656 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7657 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7658 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7659 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7660 | for (j = 1;j <= nbNs;j++) |
| 7661 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7662 | break; |
| 7663 | if (j <= nbNs) |
| 7664 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 7665 | else |
| 7666 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7667 | if (alloc != 0) xmlFree(attvalue); |
| 7668 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7669 | continue; |
| 7670 | } |
| 7671 | |
| 7672 | /* |
| 7673 | * Add the pair to atts |
| 7674 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7675 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7676 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7677 | if (attvalue[len] == 0) |
| 7678 | xmlFree(attvalue); |
| 7679 | goto failed; |
| 7680 | } |
| 7681 | maxatts = ctxt->maxatts; |
| 7682 | atts = ctxt->atts; |
| 7683 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7684 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7685 | atts[nbatts++] = attname; |
| 7686 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7687 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7688 | atts[nbatts++] = attvalue; |
| 7689 | attvalue += len; |
| 7690 | atts[nbatts++] = attvalue; |
| 7691 | /* |
| 7692 | * tag if some deallocation is needed |
| 7693 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7694 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7695 | } else { |
| 7696 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 7697 | xmlFree(attvalue); |
| 7698 | } |
| 7699 | |
| 7700 | failed: |
| 7701 | |
| 7702 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7703 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7704 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 7705 | break; |
| 7706 | if (!IS_BLANK(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7707 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 7708 | "attributes construct error\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7709 | } |
| 7710 | SKIP_BLANKS; |
| 7711 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 7712 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7713 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7714 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7715 | break; |
| 7716 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7717 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7718 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7721 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7722 | * The attributes checkings |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7723 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7724 | for (i = 0; i < nbatts;i += 5) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7725 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 7726 | if ((atts[i + 1] != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7727 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7728 | "Namespace prefix %s for %s on %s is not defined\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7729 | atts[i + 1], atts[i], localname); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7730 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7731 | atts[i + 2] = nsname; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7732 | /* |
| 7733 | * [ WFC: Unique Att Spec ] |
| 7734 | * No attribute name may appear more than once in the same |
| 7735 | * start-tag or empty-element tag. |
| 7736 | * As extended by the Namespace in XML REC. |
| 7737 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7738 | for (j = 0; j < i;j += 5) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7739 | if (atts[i] == atts[j]) { |
| 7740 | if (atts[i+1] == atts[j+1]) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7741 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7742 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7743 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7744 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7745 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7746 | "Namespaced Attribute %s in '%s' redefined\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7747 | atts[i], nsname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7748 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7749 | } |
| 7750 | } |
| 7751 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | /* |
| 7755 | * The attributes defaulting |
| 7756 | */ |
| 7757 | if (ctxt->attsDefault != NULL) { |
| 7758 | xmlDefAttrsPtr defaults; |
| 7759 | |
| 7760 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 7761 | if (defaults != NULL) { |
| 7762 | for (i = 0;i < defaults->nbAttrs;i++) { |
| 7763 | attname = defaults->values[4 * i]; |
| 7764 | aprefix = defaults->values[4 * i + 1]; |
| 7765 | |
| 7766 | /* |
| 7767 | * special work for namespaces defaulted defs |
| 7768 | */ |
| 7769 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 7770 | /* |
| 7771 | * check that it's not a defined namespace |
| 7772 | */ |
| 7773 | for (j = 1;j <= nbNs;j++) |
| 7774 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7775 | break; |
| 7776 | if (j <= nbNs) continue; |
| 7777 | |
| 7778 | nsname = xmlGetNamespace(ctxt, NULL); |
| 7779 | if (nsname != defaults->values[4 * i + 2]) { |
| 7780 | if (nsPush(ctxt, NULL, |
| 7781 | defaults->values[4 * i + 2]) > 0) |
| 7782 | nbNs++; |
| 7783 | } |
| 7784 | } else if (aprefix == ctxt->str_xmlns) { |
| 7785 | /* |
| 7786 | * check that it's not a defined namespace |
| 7787 | */ |
| 7788 | for (j = 1;j <= nbNs;j++) |
| 7789 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7790 | break; |
| 7791 | if (j <= nbNs) continue; |
| 7792 | |
| 7793 | nsname = xmlGetNamespace(ctxt, attname); |
| 7794 | if (nsname != defaults->values[2]) { |
| 7795 | if (nsPush(ctxt, attname, |
| 7796 | defaults->values[4 * i + 2]) > 0) |
| 7797 | nbNs++; |
| 7798 | } |
| 7799 | } else { |
| 7800 | /* |
| 7801 | * check that it's not a defined attribute |
| 7802 | */ |
| 7803 | for (j = 0;j < nbatts;j+=5) { |
| 7804 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 7805 | break; |
| 7806 | } |
| 7807 | if (j < nbatts) continue; |
| 7808 | |
| 7809 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7810 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
| 7811 | goto failed; |
| 7812 | } |
| 7813 | maxatts = ctxt->maxatts; |
| 7814 | atts = ctxt->atts; |
| 7815 | } |
| 7816 | atts[nbatts++] = attname; |
| 7817 | atts[nbatts++] = aprefix; |
| 7818 | if (aprefix == NULL) |
| 7819 | atts[nbatts++] = NULL; |
| 7820 | else |
| 7821 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
| 7822 | atts[nbatts++] = defaults->values[4 * i + 2]; |
| 7823 | atts[nbatts++] = defaults->values[4 * i + 3]; |
| 7824 | nbdef++; |
| 7825 | } |
| 7826 | } |
| 7827 | } |
| 7828 | } |
| 7829 | |
| 7830 | nsname = xmlGetNamespace(ctxt, prefix); |
| 7831 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7832 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 7833 | "Namespace prefix %s on %s is not defined\n", |
| 7834 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7835 | } |
| 7836 | *pref = prefix; |
| 7837 | *URI = nsname; |
| 7838 | |
| 7839 | /* |
| 7840 | * SAX: Start of Element ! |
| 7841 | */ |
| 7842 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 7843 | (!ctxt->disableSAX)) { |
| 7844 | if (nbNs > 0) |
| 7845 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7846 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 7847 | nbatts / 5, nbdef, atts); |
| 7848 | else |
| 7849 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7850 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 7851 | } |
| 7852 | |
| 7853 | /* |
| 7854 | * Free up attribute allocated strings if needed |
| 7855 | */ |
| 7856 | if (attval != 0) { |
| 7857 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7858 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7859 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
| 7862 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7863 | |
| 7864 | base_changed: |
| 7865 | /* |
| 7866 | * the attribute strings are valid iif the base didn't changed |
| 7867 | */ |
| 7868 | if (attval != 0) { |
| 7869 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7870 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7871 | xmlFree((xmlChar *) atts[i]); |
| 7872 | } |
| 7873 | ctxt->input->cur = ctxt->input->base + cur; |
| 7874 | if (ctxt->wellFormed == 1) { |
| 7875 | goto reparse; |
| 7876 | } |
| 7877 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7878 | } |
| 7879 | |
| 7880 | /** |
| 7881 | * xmlParseEndTag2: |
| 7882 | * @ctxt: an XML parser context |
| 7883 | * @line: line of the start tag |
| 7884 | * @nsNr: number of namespaces on the start tag |
| 7885 | * |
| 7886 | * parse an end of tag |
| 7887 | * |
| 7888 | * [42] ETag ::= '</' Name S? '>' |
| 7889 | * |
| 7890 | * With namespace |
| 7891 | * |
| 7892 | * [NS 9] ETag ::= '</' QName S? '>' |
| 7893 | */ |
| 7894 | |
| 7895 | static void |
| 7896 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
| 7897 | const xmlChar *URI, int line, int nsNr) { |
| 7898 | const xmlChar *name; |
| 7899 | |
| 7900 | GROW; |
| 7901 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7902 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7903 | return; |
| 7904 | } |
| 7905 | SKIP(2); |
| 7906 | |
| 7907 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 7908 | |
| 7909 | /* |
| 7910 | * We should definitely be at the ending "S? '>'" part |
| 7911 | */ |
| 7912 | GROW; |
| 7913 | SKIP_BLANKS; |
| 7914 | if ((!IS_CHAR((unsigned int) RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7915 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7916 | } else |
| 7917 | NEXT1; |
| 7918 | |
| 7919 | /* |
| 7920 | * [ WFC: Element Type Match ] |
| 7921 | * The Name in an element's end-tag must match the element type in the |
| 7922 | * start-tag. |
| 7923 | * |
| 7924 | */ |
| 7925 | if (name != (xmlChar*)1) { |
| 7926 | ctxt->errNo = XML_ERR_TAG_NAME_MISMATCH; |
| 7927 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) { |
| 7928 | if (name != NULL) { |
| 7929 | ctxt->sax->error(ctxt->userData, |
| 7930 | "Opening and ending tag mismatch: %s line %d and %s\n", |
| 7931 | ctxt->name, line, name); |
| 7932 | } else { |
| 7933 | ctxt->sax->error(ctxt->userData, |
| 7934 | "Ending tag error for: %s line %d\n", ctxt->name, line); |
| 7935 | } |
| 7936 | |
| 7937 | } |
| 7938 | ctxt->wellFormed = 0; |
| 7939 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
| 7940 | } |
| 7941 | |
| 7942 | /* |
| 7943 | * SAX: End of Tag |
| 7944 | */ |
| 7945 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 7946 | (!ctxt->disableSAX)) |
| 7947 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 7948 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7949 | spacePop(ctxt); |
| 7950 | if (nsNr != 0) |
| 7951 | nsPop(ctxt, nsNr); |
| 7952 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 7953 | } |
| 7954 | |
| 7955 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7956 | * xmlParseCDSect: |
| 7957 | * @ctxt: an XML parser context |
| 7958 | * |
| 7959 | * Parse escaped pure raw content. |
| 7960 | * |
| 7961 | * [18] CDSect ::= CDStart CData CDEnd |
| 7962 | * |
| 7963 | * [19] CDStart ::= '<![CDATA[' |
| 7964 | * |
| 7965 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 7966 | * |
| 7967 | * [21] CDEnd ::= ']]>' |
| 7968 | */ |
| 7969 | void |
| 7970 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 7971 | xmlChar *buf = NULL; |
| 7972 | int len = 0; |
| 7973 | int size = XML_PARSER_BUFFER_SIZE; |
| 7974 | int r, rl; |
| 7975 | int s, sl; |
| 7976 | int cur, l; |
| 7977 | int count = 0; |
| 7978 | |
| 7979 | if ((NXT(0) == '<') && (NXT(1) == '!') && |
| 7980 | (NXT(2) == '[') && (NXT(3) == 'C') && |
| 7981 | (NXT(4) == 'D') && (NXT(5) == 'A') && |
| 7982 | (NXT(6) == 'T') && (NXT(7) == 'A') && |
| 7983 | (NXT(8) == '[')) { |
| 7984 | SKIP(9); |
| 7985 | } else |
| 7986 | return; |
| 7987 | |
| 7988 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 7989 | r = CUR_CHAR(rl); |
| 7990 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7991 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7992 | ctxt->instate = XML_PARSER_CONTENT; |
| 7993 | return; |
| 7994 | } |
| 7995 | NEXTL(rl); |
| 7996 | s = CUR_CHAR(sl); |
| 7997 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7998 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7999 | ctxt->instate = XML_PARSER_CONTENT; |
| 8000 | return; |
| 8001 | } |
| 8002 | NEXTL(sl); |
| 8003 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8004 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8005 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8006 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8007 | return; |
| 8008 | } |
| 8009 | while (IS_CHAR(cur) && |
| 8010 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 8011 | if (len + 5 >= size) { |
| 8012 | size *= 2; |
| 8013 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8014 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8015 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8016 | return; |
| 8017 | } |
| 8018 | } |
| 8019 | COPY_BUF(rl,buf,len,r); |
| 8020 | r = s; |
| 8021 | rl = sl; |
| 8022 | s = cur; |
| 8023 | sl = l; |
| 8024 | count++; |
| 8025 | if (count > 50) { |
| 8026 | GROW; |
| 8027 | count = 0; |
| 8028 | } |
| 8029 | NEXTL(l); |
| 8030 | cur = CUR_CHAR(l); |
| 8031 | } |
| 8032 | buf[len] = 0; |
| 8033 | ctxt->instate = XML_PARSER_CONTENT; |
| 8034 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8035 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8036 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8037 | xmlFree(buf); |
| 8038 | return; |
| 8039 | } |
| 8040 | NEXTL(l); |
| 8041 | |
| 8042 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8043 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8044 | */ |
| 8045 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 8046 | if (ctxt->sax->cdataBlock != NULL) |
| 8047 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 8048 | else if (ctxt->sax->characters != NULL) |
| 8049 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8050 | } |
| 8051 | xmlFree(buf); |
| 8052 | } |
| 8053 | |
| 8054 | /** |
| 8055 | * xmlParseContent: |
| 8056 | * @ctxt: an XML parser context |
| 8057 | * |
| 8058 | * Parse a content: |
| 8059 | * |
| 8060 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 8061 | */ |
| 8062 | |
| 8063 | void |
| 8064 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 8065 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8066 | while ((RAW != 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8067 | ((RAW != '<') || (NXT(1) != '/'))) { |
| 8068 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8069 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8070 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8071 | |
| 8072 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8073 | * First case : a Processing Instruction. |
| 8074 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8075 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8076 | xmlParsePI(ctxt); |
| 8077 | } |
| 8078 | |
| 8079 | /* |
| 8080 | * Second case : a CDSection |
| 8081 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8082 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8083 | (NXT(2) == '[') && (NXT(3) == 'C') && |
| 8084 | (NXT(4) == 'D') && (NXT(5) == 'A') && |
| 8085 | (NXT(6) == 'T') && (NXT(7) == 'A') && |
| 8086 | (NXT(8) == '[')) { |
| 8087 | xmlParseCDSect(ctxt); |
| 8088 | } |
| 8089 | |
| 8090 | /* |
| 8091 | * Third case : a comment |
| 8092 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8093 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8094 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 8095 | xmlParseComment(ctxt); |
| 8096 | ctxt->instate = XML_PARSER_CONTENT; |
| 8097 | } |
| 8098 | |
| 8099 | /* |
| 8100 | * Fourth case : a sub-element. |
| 8101 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8102 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8103 | xmlParseElement(ctxt); |
| 8104 | } |
| 8105 | |
| 8106 | /* |
| 8107 | * Fifth case : a reference. If if has not been resolved, |
| 8108 | * parsing returns it's Name, create the node |
| 8109 | */ |
| 8110 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8111 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8112 | xmlParseReference(ctxt); |
| 8113 | } |
| 8114 | |
| 8115 | /* |
| 8116 | * Last case, text. Note that References are handled directly. |
| 8117 | */ |
| 8118 | else { |
| 8119 | xmlParseCharData(ctxt, 0); |
| 8120 | } |
| 8121 | |
| 8122 | GROW; |
| 8123 | /* |
| 8124 | * Pop-up of finished entities. |
| 8125 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8126 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8127 | xmlPopInput(ctxt); |
| 8128 | SHRINK; |
| 8129 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8130 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8131 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8132 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8133 | ctxt->instate = XML_PARSER_EOF; |
| 8134 | break; |
| 8135 | } |
| 8136 | } |
| 8137 | } |
| 8138 | |
| 8139 | /** |
| 8140 | * xmlParseElement: |
| 8141 | * @ctxt: an XML parser context |
| 8142 | * |
| 8143 | * parse an XML element, this is highly recursive |
| 8144 | * |
| 8145 | * [39] element ::= EmptyElemTag | STag content ETag |
| 8146 | * |
| 8147 | * [ WFC: Element Type Match ] |
| 8148 | * The Name in an element's end-tag must match the element type in the |
| 8149 | * start-tag. |
| 8150 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8151 | */ |
| 8152 | |
| 8153 | void |
| 8154 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8155 | const xmlChar *name; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8156 | const xmlChar *prefix; |
| 8157 | const xmlChar *URI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8158 | xmlParserNodeInfo node_info; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8159 | int line; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8160 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8161 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8162 | |
| 8163 | /* Capture start position */ |
| 8164 | if (ctxt->record_info) { |
| 8165 | node_info.begin_pos = ctxt->input->consumed + |
| 8166 | (CUR_PTR - ctxt->input->base); |
| 8167 | node_info.begin_line = ctxt->input->line; |
| 8168 | } |
| 8169 | |
| 8170 | if (ctxt->spaceNr == 0) |
| 8171 | spacePush(ctxt, -1); |
| 8172 | else |
| 8173 | spacePush(ctxt, *ctxt->space); |
| 8174 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8175 | line = ctxt->input->line; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8176 | if (ctxt->sax2) |
| 8177 | name = xmlParseStartTag2(ctxt, &prefix, &URI); |
| 8178 | else |
| 8179 | name = xmlParseStartTag(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8180 | if (name == NULL) { |
| 8181 | spacePop(ctxt); |
| 8182 | return; |
| 8183 | } |
| 8184 | namePush(ctxt, name); |
| 8185 | ret = ctxt->node; |
| 8186 | |
| 8187 | /* |
| 8188 | * [ VC: Root Element Type ] |
| 8189 | * The Name in the document type declaration must match the element |
| 8190 | * type of the root element. |
| 8191 | */ |
| 8192 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 8193 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 8194 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
| 8195 | |
| 8196 | /* |
| 8197 | * Check for an Empty Element. |
| 8198 | */ |
| 8199 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 8200 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8201 | if (ctxt->sax2) { |
| 8202 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 8203 | (!ctxt->disableSAX)) |
| 8204 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
| 8205 | } else { |
| 8206 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8207 | (!ctxt->disableSAX)) |
| 8208 | ctxt->sax->endElement(ctxt->userData, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8209 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8210 | namePop(ctxt); |
| 8211 | spacePop(ctxt); |
| 8212 | if (nsNr != ctxt->nsNr) |
| 8213 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8214 | if ( ret != NULL && ctxt->record_info ) { |
| 8215 | node_info.end_pos = ctxt->input->consumed + |
| 8216 | (CUR_PTR - ctxt->input->base); |
| 8217 | node_info.end_line = ctxt->input->line; |
| 8218 | node_info.node = ret; |
| 8219 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8220 | } |
| 8221 | return; |
| 8222 | } |
| 8223 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8224 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8225 | } else { |
| 8226 | ctxt->errNo = XML_ERR_GT_REQUIRED; |
| 8227 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 8228 | ctxt->sax->error(ctxt->userData, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8229 | "Couldn't find end of Start Tag %s line %d\n", |
| 8230 | name, line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8231 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 8232 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8233 | |
| 8234 | /* |
| 8235 | * end of parsing of this node. |
| 8236 | */ |
| 8237 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8238 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8239 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8240 | if (nsNr != ctxt->nsNr) |
| 8241 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8242 | |
| 8243 | /* |
| 8244 | * Capture end position and add node |
| 8245 | */ |
| 8246 | if ( ret != NULL && ctxt->record_info ) { |
| 8247 | node_info.end_pos = ctxt->input->consumed + |
| 8248 | (CUR_PTR - ctxt->input->base); |
| 8249 | node_info.end_line = ctxt->input->line; |
| 8250 | node_info.node = ret; |
| 8251 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8252 | } |
| 8253 | return; |
| 8254 | } |
| 8255 | |
| 8256 | /* |
| 8257 | * Parse the content of the element: |
| 8258 | */ |
| 8259 | xmlParseContent(ctxt); |
Daniel Veillard | 34ba387 | 2003-07-15 13:34:05 +0000 | [diff] [blame] | 8260 | if (!IS_CHAR((unsigned int) RAW)) { |
Daniel Veillard | 5344c60 | 2001-12-31 16:37:34 +0000 | [diff] [blame] | 8261 | ctxt->errNo = XML_ERR_TAG_NOT_FINISHED; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8262 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 8263 | ctxt->sax->error(ctxt->userData, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8264 | "Premature end of data in tag %s line %d\n", name, line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8265 | ctxt->wellFormed = 0; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 8266 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8267 | |
| 8268 | /* |
| 8269 | * end of parsing of this node. |
| 8270 | */ |
| 8271 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8272 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8273 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8274 | if (nsNr != ctxt->nsNr) |
| 8275 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8276 | return; |
| 8277 | } |
| 8278 | |
| 8279 | /* |
| 8280 | * parse the end of tag: '</' should be here. |
| 8281 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8282 | if (ctxt->sax2) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8283 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8284 | namePop(ctxt); |
| 8285 | } else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8286 | xmlParseEndTag1(ctxt, line); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8287 | |
| 8288 | /* |
| 8289 | * Capture end position and add node |
| 8290 | */ |
| 8291 | if ( ret != NULL && ctxt->record_info ) { |
| 8292 | node_info.end_pos = ctxt->input->consumed + |
| 8293 | (CUR_PTR - ctxt->input->base); |
| 8294 | node_info.end_line = ctxt->input->line; |
| 8295 | node_info.node = ret; |
| 8296 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8297 | } |
| 8298 | } |
| 8299 | |
| 8300 | /** |
| 8301 | * xmlParseVersionNum: |
| 8302 | * @ctxt: an XML parser context |
| 8303 | * |
| 8304 | * parse the XML version value. |
| 8305 | * |
| 8306 | * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+ |
| 8307 | * |
| 8308 | * Returns the string giving the XML version number, or NULL |
| 8309 | */ |
| 8310 | xmlChar * |
| 8311 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 8312 | xmlChar *buf = NULL; |
| 8313 | int len = 0; |
| 8314 | int size = 10; |
| 8315 | xmlChar cur; |
| 8316 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8317 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8318 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8319 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8320 | return(NULL); |
| 8321 | } |
| 8322 | cur = CUR; |
| 8323 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8324 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8325 | ((cur >= '0') && (cur <= '9')) || |
| 8326 | (cur == '_') || (cur == '.') || |
| 8327 | (cur == ':') || (cur == '-')) { |
| 8328 | if (len + 1 >= size) { |
| 8329 | size *= 2; |
| 8330 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8331 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8332 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | return(NULL); |
| 8334 | } |
| 8335 | } |
| 8336 | buf[len++] = cur; |
| 8337 | NEXT; |
| 8338 | cur=CUR; |
| 8339 | } |
| 8340 | buf[len] = 0; |
| 8341 | return(buf); |
| 8342 | } |
| 8343 | |
| 8344 | /** |
| 8345 | * xmlParseVersionInfo: |
| 8346 | * @ctxt: an XML parser context |
| 8347 | * |
| 8348 | * parse the XML version. |
| 8349 | * |
| 8350 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
| 8351 | * |
| 8352 | * [25] Eq ::= S? '=' S? |
| 8353 | * |
| 8354 | * Returns the version string, e.g. "1.0" |
| 8355 | */ |
| 8356 | |
| 8357 | xmlChar * |
| 8358 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 8359 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8360 | |
| 8361 | if ((RAW == 'v') && (NXT(1) == 'e') && |
| 8362 | (NXT(2) == 'r') && (NXT(3) == 's') && |
| 8363 | (NXT(4) == 'i') && (NXT(5) == 'o') && |
| 8364 | (NXT(6) == 'n')) { |
| 8365 | SKIP(7); |
| 8366 | SKIP_BLANKS; |
| 8367 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8368 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8369 | return(NULL); |
| 8370 | } |
| 8371 | NEXT; |
| 8372 | SKIP_BLANKS; |
| 8373 | if (RAW == '"') { |
| 8374 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8375 | version = xmlParseVersionNum(ctxt); |
| 8376 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8377 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8378 | } else |
| 8379 | NEXT; |
| 8380 | } else if (RAW == '\''){ |
| 8381 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8382 | version = xmlParseVersionNum(ctxt); |
| 8383 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8384 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8385 | } else |
| 8386 | NEXT; |
| 8387 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8388 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8389 | } |
| 8390 | } |
| 8391 | return(version); |
| 8392 | } |
| 8393 | |
| 8394 | /** |
| 8395 | * xmlParseEncName: |
| 8396 | * @ctxt: an XML parser context |
| 8397 | * |
| 8398 | * parse the XML encoding name |
| 8399 | * |
| 8400 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 8401 | * |
| 8402 | * Returns the encoding name value or NULL |
| 8403 | */ |
| 8404 | xmlChar * |
| 8405 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 8406 | xmlChar *buf = NULL; |
| 8407 | int len = 0; |
| 8408 | int size = 10; |
| 8409 | xmlChar cur; |
| 8410 | |
| 8411 | cur = CUR; |
| 8412 | if (((cur >= 'a') && (cur <= 'z')) || |
| 8413 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8414 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8415 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8416 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8417 | return(NULL); |
| 8418 | } |
| 8419 | |
| 8420 | buf[len++] = cur; |
| 8421 | NEXT; |
| 8422 | cur = CUR; |
| 8423 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8424 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8425 | ((cur >= '0') && (cur <= '9')) || |
| 8426 | (cur == '.') || (cur == '_') || |
| 8427 | (cur == '-')) { |
| 8428 | if (len + 1 >= size) { |
| 8429 | size *= 2; |
| 8430 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8431 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8432 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8433 | return(NULL); |
| 8434 | } |
| 8435 | } |
| 8436 | buf[len++] = cur; |
| 8437 | NEXT; |
| 8438 | cur = CUR; |
| 8439 | if (cur == 0) { |
| 8440 | SHRINK; |
| 8441 | GROW; |
| 8442 | cur = CUR; |
| 8443 | } |
| 8444 | } |
| 8445 | buf[len] = 0; |
| 8446 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8447 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8448 | } |
| 8449 | return(buf); |
| 8450 | } |
| 8451 | |
| 8452 | /** |
| 8453 | * xmlParseEncodingDecl: |
| 8454 | * @ctxt: an XML parser context |
| 8455 | * |
| 8456 | * parse the XML encoding declaration |
| 8457 | * |
| 8458 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 8459 | * |
| 8460 | * this setups the conversion filters. |
| 8461 | * |
| 8462 | * Returns the encoding value or NULL |
| 8463 | */ |
| 8464 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8465 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8466 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 8467 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8468 | |
| 8469 | SKIP_BLANKS; |
| 8470 | if ((RAW == 'e') && (NXT(1) == 'n') && |
| 8471 | (NXT(2) == 'c') && (NXT(3) == 'o') && |
| 8472 | (NXT(4) == 'd') && (NXT(5) == 'i') && |
| 8473 | (NXT(6) == 'n') && (NXT(7) == 'g')) { |
| 8474 | SKIP(8); |
| 8475 | SKIP_BLANKS; |
| 8476 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8477 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8478 | return(NULL); |
| 8479 | } |
| 8480 | NEXT; |
| 8481 | SKIP_BLANKS; |
| 8482 | if (RAW == '"') { |
| 8483 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | encoding = xmlParseEncName(ctxt); |
| 8485 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8486 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8487 | } else |
| 8488 | NEXT; |
| 8489 | } else if (RAW == '\''){ |
| 8490 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8491 | encoding = xmlParseEncName(ctxt); |
| 8492 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8493 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8494 | } else |
| 8495 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 8496 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8497 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8498 | } |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8499 | /* |
| 8500 | * UTF-16 encoding stwich has already taken place at this stage, |
| 8501 | * more over the little-endian/big-endian selection is already done |
| 8502 | */ |
| 8503 | if ((encoding != NULL) && |
| 8504 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 8505 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8506 | if (ctxt->encoding != NULL) |
| 8507 | xmlFree((xmlChar *) ctxt->encoding); |
| 8508 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8509 | } |
| 8510 | /* |
| 8511 | * UTF-8 encoding is handled natively |
| 8512 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8513 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8514 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 8515 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8516 | if (ctxt->encoding != NULL) |
| 8517 | xmlFree((xmlChar *) ctxt->encoding); |
| 8518 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8519 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8520 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8521 | xmlCharEncodingHandlerPtr handler; |
| 8522 | |
| 8523 | if (ctxt->input->encoding != NULL) |
| 8524 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 8525 | ctxt->input->encoding = encoding; |
| 8526 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8527 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 8528 | if (handler != NULL) { |
| 8529 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8530 | } else { |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8531 | ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING; |
| 8532 | if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) |
| 8533 | ctxt->sax->error(ctxt->userData, |
| 8534 | "Unsupported encoding %s\n", encoding); |
| 8535 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8536 | } |
| 8537 | } |
| 8538 | } |
| 8539 | return(encoding); |
| 8540 | } |
| 8541 | |
| 8542 | /** |
| 8543 | * xmlParseSDDecl: |
| 8544 | * @ctxt: an XML parser context |
| 8545 | * |
| 8546 | * parse the XML standalone declaration |
| 8547 | * |
| 8548 | * [32] SDDecl ::= S 'standalone' Eq |
| 8549 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 8550 | * |
| 8551 | * [ VC: Standalone Document Declaration ] |
| 8552 | * TODO The standalone document declaration must have the value "no" |
| 8553 | * if any external markup declarations contain declarations of: |
| 8554 | * - attributes with default values, if elements to which these |
| 8555 | * attributes apply appear in the document without specifications |
| 8556 | * of values for these attributes, or |
| 8557 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 8558 | * to those entities appear in the document, or |
| 8559 | * - attributes with values subject to normalization, where the |
| 8560 | * attribute appears in the document with a value which will change |
| 8561 | * as a result of normalization, or |
| 8562 | * - element types with element content, if white space occurs directly |
| 8563 | * within any instance of those types. |
| 8564 | * |
| 8565 | * Returns 1 if standalone, 0 otherwise |
| 8566 | */ |
| 8567 | |
| 8568 | int |
| 8569 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
| 8570 | int standalone = -1; |
| 8571 | |
| 8572 | SKIP_BLANKS; |
| 8573 | if ((RAW == 's') && (NXT(1) == 't') && |
| 8574 | (NXT(2) == 'a') && (NXT(3) == 'n') && |
| 8575 | (NXT(4) == 'd') && (NXT(5) == 'a') && |
| 8576 | (NXT(6) == 'l') && (NXT(7) == 'o') && |
| 8577 | (NXT(8) == 'n') && (NXT(9) == 'e')) { |
| 8578 | SKIP(10); |
| 8579 | SKIP_BLANKS; |
| 8580 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8581 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8582 | return(standalone); |
| 8583 | } |
| 8584 | NEXT; |
| 8585 | SKIP_BLANKS; |
| 8586 | if (RAW == '\''){ |
| 8587 | NEXT; |
| 8588 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8589 | standalone = 0; |
| 8590 | SKIP(2); |
| 8591 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8592 | (NXT(2) == 's')) { |
| 8593 | standalone = 1; |
| 8594 | SKIP(3); |
| 8595 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8596 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8597 | } |
| 8598 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8599 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8600 | } else |
| 8601 | NEXT; |
| 8602 | } else if (RAW == '"'){ |
| 8603 | NEXT; |
| 8604 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8605 | standalone = 0; |
| 8606 | SKIP(2); |
| 8607 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8608 | (NXT(2) == 's')) { |
| 8609 | standalone = 1; |
| 8610 | SKIP(3); |
| 8611 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8612 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8613 | } |
| 8614 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8615 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8616 | } else |
| 8617 | NEXT; |
| 8618 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8619 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8620 | } |
| 8621 | } |
| 8622 | return(standalone); |
| 8623 | } |
| 8624 | |
| 8625 | /** |
| 8626 | * xmlParseXMLDecl: |
| 8627 | * @ctxt: an XML parser context |
| 8628 | * |
| 8629 | * parse an XML declaration header |
| 8630 | * |
| 8631 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 8632 | */ |
| 8633 | |
| 8634 | void |
| 8635 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 8636 | xmlChar *version; |
| 8637 | |
| 8638 | /* |
| 8639 | * We know that '<?xml' is here. |
| 8640 | */ |
| 8641 | SKIP(5); |
| 8642 | |
| 8643 | if (!IS_BLANK(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8644 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8645 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8646 | } |
| 8647 | SKIP_BLANKS; |
| 8648 | |
| 8649 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8650 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8651 | */ |
| 8652 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8653 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8654 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8655 | } else { |
| 8656 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 8657 | /* |
| 8658 | * TODO: Blueberry should be detected here |
| 8659 | */ |
| 8660 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
| 8661 | ctxt->sax->warning(ctxt->userData, "Unsupported version '%s'\n", |
| 8662 | version); |
| 8663 | } |
| 8664 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 8665 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8666 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 8667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8668 | |
| 8669 | /* |
| 8670 | * We may have the encoding declaration |
| 8671 | */ |
| 8672 | if (!IS_BLANK(RAW)) { |
| 8673 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8674 | SKIP(2); |
| 8675 | return; |
| 8676 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8677 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8678 | } |
| 8679 | xmlParseEncodingDecl(ctxt); |
| 8680 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8681 | /* |
| 8682 | * The XML REC instructs us to stop parsing right here |
| 8683 | */ |
| 8684 | return; |
| 8685 | } |
| 8686 | |
| 8687 | /* |
| 8688 | * We may have the standalone status. |
| 8689 | */ |
| 8690 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK(RAW))) { |
| 8691 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8692 | SKIP(2); |
| 8693 | return; |
| 8694 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8695 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8696 | } |
| 8697 | SKIP_BLANKS; |
| 8698 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 8699 | |
| 8700 | SKIP_BLANKS; |
| 8701 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8702 | SKIP(2); |
| 8703 | } else if (RAW == '>') { |
| 8704 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8705 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8706 | NEXT; |
| 8707 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8708 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8709 | MOVETO_ENDTAG(CUR_PTR); |
| 8710 | NEXT; |
| 8711 | } |
| 8712 | } |
| 8713 | |
| 8714 | /** |
| 8715 | * xmlParseMisc: |
| 8716 | * @ctxt: an XML parser context |
| 8717 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8718 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8719 | * |
| 8720 | * [27] Misc ::= Comment | PI | S |
| 8721 | */ |
| 8722 | |
| 8723 | void |
| 8724 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8725 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 8726 | ((RAW == '<') && (NXT(1) == '!') && |
| 8727 | (NXT(2) == '-') && (NXT(3) == '-')) || |
| 8728 | IS_BLANK(CUR)) { |
| 8729 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8730 | xmlParsePI(ctxt); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8731 | } else if (IS_BLANK(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8732 | NEXT; |
| 8733 | } else |
| 8734 | xmlParseComment(ctxt); |
| 8735 | } |
| 8736 | } |
| 8737 | |
| 8738 | /** |
| 8739 | * xmlParseDocument: |
| 8740 | * @ctxt: an XML parser context |
| 8741 | * |
| 8742 | * parse an XML document (and build a tree if using the standard SAX |
| 8743 | * interface). |
| 8744 | * |
| 8745 | * [1] document ::= prolog element Misc* |
| 8746 | * |
| 8747 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 8748 | * |
| 8749 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8750 | * as a result of the parsing. |
| 8751 | */ |
| 8752 | |
| 8753 | int |
| 8754 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 8755 | xmlChar start[4]; |
| 8756 | xmlCharEncoding enc; |
| 8757 | |
| 8758 | xmlInitParser(); |
| 8759 | |
| 8760 | GROW; |
| 8761 | |
| 8762 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8763 | * SAX: detecting the level. |
| 8764 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8765 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8766 | |
| 8767 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8768 | * SAX: beginning of the document processing. |
| 8769 | */ |
| 8770 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8771 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8772 | |
Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 8773 | if (ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 8774 | /* |
| 8775 | * Get the 4 first bytes and decode the charset |
| 8776 | * if enc != XML_CHAR_ENCODING_NONE |
| 8777 | * plug some encoding conversion routines. |
| 8778 | */ |
| 8779 | start[0] = RAW; |
| 8780 | start[1] = NXT(1); |
| 8781 | start[2] = NXT(2); |
| 8782 | start[3] = NXT(3); |
| 8783 | enc = xmlDetectCharEncoding(start, 4); |
| 8784 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8785 | xmlSwitchEncoding(ctxt, enc); |
| 8786 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8787 | } |
| 8788 | |
| 8789 | |
| 8790 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8791 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8792 | } |
| 8793 | |
| 8794 | /* |
| 8795 | * Check for the XMLDecl in the Prolog. |
| 8796 | */ |
| 8797 | GROW; |
| 8798 | if ((RAW == '<') && (NXT(1) == '?') && |
| 8799 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 8800 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 8801 | |
| 8802 | /* |
| 8803 | * Note that we will switch encoding on the fly. |
| 8804 | */ |
| 8805 | xmlParseXMLDecl(ctxt); |
| 8806 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8807 | /* |
| 8808 | * The XML REC instructs us to stop parsing right here |
| 8809 | */ |
| 8810 | return(-1); |
| 8811 | } |
| 8812 | ctxt->standalone = ctxt->input->standalone; |
| 8813 | SKIP_BLANKS; |
| 8814 | } else { |
| 8815 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8816 | } |
| 8817 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8818 | ctxt->sax->startDocument(ctxt->userData); |
| 8819 | |
| 8820 | /* |
| 8821 | * The Misc part of the Prolog |
| 8822 | */ |
| 8823 | GROW; |
| 8824 | xmlParseMisc(ctxt); |
| 8825 | |
| 8826 | /* |
| 8827 | * Then possibly doc type declaration(s) and more Misc |
| 8828 | * (doctypedecl Misc*)? |
| 8829 | */ |
| 8830 | GROW; |
| 8831 | if ((RAW == '<') && (NXT(1) == '!') && |
| 8832 | (NXT(2) == 'D') && (NXT(3) == 'O') && |
| 8833 | (NXT(4) == 'C') && (NXT(5) == 'T') && |
| 8834 | (NXT(6) == 'Y') && (NXT(7) == 'P') && |
| 8835 | (NXT(8) == 'E')) { |
| 8836 | |
| 8837 | ctxt->inSubset = 1; |
| 8838 | xmlParseDocTypeDecl(ctxt); |
| 8839 | if (RAW == '[') { |
| 8840 | ctxt->instate = XML_PARSER_DTD; |
| 8841 | xmlParseInternalSubset(ctxt); |
| 8842 | } |
| 8843 | |
| 8844 | /* |
| 8845 | * Create and update the external subset. |
| 8846 | */ |
| 8847 | ctxt->inSubset = 2; |
| 8848 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 8849 | (!ctxt->disableSAX)) |
| 8850 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 8851 | ctxt->extSubSystem, ctxt->extSubURI); |
| 8852 | ctxt->inSubset = 0; |
| 8853 | |
| 8854 | |
| 8855 | ctxt->instate = XML_PARSER_PROLOG; |
| 8856 | xmlParseMisc(ctxt); |
| 8857 | } |
| 8858 | |
| 8859 | /* |
| 8860 | * Time to start parsing the tree itself |
| 8861 | */ |
| 8862 | GROW; |
| 8863 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8864 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 8865 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8866 | } else { |
| 8867 | ctxt->instate = XML_PARSER_CONTENT; |
| 8868 | xmlParseElement(ctxt); |
| 8869 | ctxt->instate = XML_PARSER_EPILOG; |
| 8870 | |
| 8871 | |
| 8872 | /* |
| 8873 | * The Misc part at the end |
| 8874 | */ |
| 8875 | xmlParseMisc(ctxt); |
| 8876 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8877 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8878 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8879 | } |
| 8880 | ctxt->instate = XML_PARSER_EOF; |
| 8881 | } |
| 8882 | |
| 8883 | /* |
| 8884 | * SAX: end of the document processing. |
| 8885 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8886 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8887 | ctxt->sax->endDocument(ctxt->userData); |
| 8888 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 8889 | /* |
| 8890 | * Remove locally kept entity definitions if the tree was not built |
| 8891 | */ |
| 8892 | if ((ctxt->myDoc != NULL) && |
| 8893 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 8894 | xmlFreeDoc(ctxt->myDoc); |
| 8895 | ctxt->myDoc = NULL; |
| 8896 | } |
| 8897 | |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 8898 | if (! ctxt->wellFormed) { |
| 8899 | ctxt->valid = 0; |
| 8900 | return(-1); |
| 8901 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8902 | return(0); |
| 8903 | } |
| 8904 | |
| 8905 | /** |
| 8906 | * xmlParseExtParsedEnt: |
| 8907 | * @ctxt: an XML parser context |
| 8908 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8909 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8910 | * An external general parsed entity is well-formed if it matches the |
| 8911 | * production labeled extParsedEnt. |
| 8912 | * |
| 8913 | * [78] extParsedEnt ::= TextDecl? content |
| 8914 | * |
| 8915 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8916 | * as a result of the parsing. |
| 8917 | */ |
| 8918 | |
| 8919 | int |
| 8920 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 8921 | xmlChar start[4]; |
| 8922 | xmlCharEncoding enc; |
| 8923 | |
| 8924 | xmlDefaultSAXHandlerInit(); |
| 8925 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 8926 | xmlDetectSAX2(ctxt); |
| 8927 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8928 | GROW; |
| 8929 | |
| 8930 | /* |
| 8931 | * SAX: beginning of the document processing. |
| 8932 | */ |
| 8933 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8934 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8935 | |
| 8936 | /* |
| 8937 | * Get the 4 first bytes and decode the charset |
| 8938 | * if enc != XML_CHAR_ENCODING_NONE |
| 8939 | * plug some encoding conversion routines. |
| 8940 | */ |
| 8941 | start[0] = RAW; |
| 8942 | start[1] = NXT(1); |
| 8943 | start[2] = NXT(2); |
| 8944 | start[3] = NXT(3); |
| 8945 | enc = xmlDetectCharEncoding(start, 4); |
| 8946 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8947 | xmlSwitchEncoding(ctxt, enc); |
| 8948 | } |
| 8949 | |
| 8950 | |
| 8951 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8952 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8953 | } |
| 8954 | |
| 8955 | /* |
| 8956 | * Check for the XMLDecl in the Prolog. |
| 8957 | */ |
| 8958 | GROW; |
| 8959 | if ((RAW == '<') && (NXT(1) == '?') && |
| 8960 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 8961 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 8962 | |
| 8963 | /* |
| 8964 | * Note that we will switch encoding on the fly. |
| 8965 | */ |
| 8966 | xmlParseXMLDecl(ctxt); |
| 8967 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8968 | /* |
| 8969 | * The XML REC instructs us to stop parsing right here |
| 8970 | */ |
| 8971 | return(-1); |
| 8972 | } |
| 8973 | SKIP_BLANKS; |
| 8974 | } else { |
| 8975 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8976 | } |
| 8977 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8978 | ctxt->sax->startDocument(ctxt->userData); |
| 8979 | |
| 8980 | /* |
| 8981 | * Doing validity checking on chunk doesn't make sense |
| 8982 | */ |
| 8983 | ctxt->instate = XML_PARSER_CONTENT; |
| 8984 | ctxt->validate = 0; |
| 8985 | ctxt->loadsubset = 0; |
| 8986 | ctxt->depth = 0; |
| 8987 | |
| 8988 | xmlParseContent(ctxt); |
| 8989 | |
| 8990 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8991 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8992 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8993 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8994 | } |
| 8995 | |
| 8996 | /* |
| 8997 | * SAX: end of the document processing. |
| 8998 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8999 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9000 | ctxt->sax->endDocument(ctxt->userData); |
| 9001 | |
| 9002 | if (! ctxt->wellFormed) return(-1); |
| 9003 | return(0); |
| 9004 | } |
| 9005 | |
| 9006 | /************************************************************************ |
| 9007 | * * |
| 9008 | * Progressive parsing interfaces * |
| 9009 | * * |
| 9010 | ************************************************************************/ |
| 9011 | |
| 9012 | /** |
| 9013 | * xmlParseLookupSequence: |
| 9014 | * @ctxt: an XML parser context |
| 9015 | * @first: the first char to lookup |
| 9016 | * @next: the next char to lookup or zero |
| 9017 | * @third: the next char to lookup or zero |
| 9018 | * |
| 9019 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 9020 | * (first) is available in the input stream. |
| 9021 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 9022 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 9023 | * parser, do not use liberally. |
| 9024 | * |
| 9025 | * Returns the index to the current parsing point if the full sequence |
| 9026 | * is available, -1 otherwise. |
| 9027 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9028 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9029 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 9030 | xmlChar next, xmlChar third) { |
| 9031 | int base, len; |
| 9032 | xmlParserInputPtr in; |
| 9033 | const xmlChar *buf; |
| 9034 | |
| 9035 | in = ctxt->input; |
| 9036 | if (in == NULL) return(-1); |
| 9037 | base = in->cur - in->base; |
| 9038 | if (base < 0) return(-1); |
| 9039 | if (ctxt->checkIndex > base) |
| 9040 | base = ctxt->checkIndex; |
| 9041 | if (in->buf == NULL) { |
| 9042 | buf = in->base; |
| 9043 | len = in->length; |
| 9044 | } else { |
| 9045 | buf = in->buf->buffer->content; |
| 9046 | len = in->buf->buffer->use; |
| 9047 | } |
| 9048 | /* take into account the sequence length */ |
| 9049 | if (third) len -= 2; |
| 9050 | else if (next) len --; |
| 9051 | for (;base < len;base++) { |
| 9052 | if (buf[base] == first) { |
| 9053 | if (third != 0) { |
| 9054 | if ((buf[base + 1] != next) || |
| 9055 | (buf[base + 2] != third)) continue; |
| 9056 | } else if (next != 0) { |
| 9057 | if (buf[base + 1] != next) continue; |
| 9058 | } |
| 9059 | ctxt->checkIndex = 0; |
| 9060 | #ifdef DEBUG_PUSH |
| 9061 | if (next == 0) |
| 9062 | xmlGenericError(xmlGenericErrorContext, |
| 9063 | "PP: lookup '%c' found at %d\n", |
| 9064 | first, base); |
| 9065 | else if (third == 0) |
| 9066 | xmlGenericError(xmlGenericErrorContext, |
| 9067 | "PP: lookup '%c%c' found at %d\n", |
| 9068 | first, next, base); |
| 9069 | else |
| 9070 | xmlGenericError(xmlGenericErrorContext, |
| 9071 | "PP: lookup '%c%c%c' found at %d\n", |
| 9072 | first, next, third, base); |
| 9073 | #endif |
| 9074 | return(base - (in->cur - in->base)); |
| 9075 | } |
| 9076 | } |
| 9077 | ctxt->checkIndex = base; |
| 9078 | #ifdef DEBUG_PUSH |
| 9079 | if (next == 0) |
| 9080 | xmlGenericError(xmlGenericErrorContext, |
| 9081 | "PP: lookup '%c' failed\n", first); |
| 9082 | else if (third == 0) |
| 9083 | xmlGenericError(xmlGenericErrorContext, |
| 9084 | "PP: lookup '%c%c' failed\n", first, next); |
| 9085 | else |
| 9086 | xmlGenericError(xmlGenericErrorContext, |
| 9087 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 9088 | #endif |
| 9089 | return(-1); |
| 9090 | } |
| 9091 | |
| 9092 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9093 | * xmlParseGetLasts: |
| 9094 | * @ctxt: an XML parser context |
| 9095 | * @lastlt: pointer to store the last '<' from the input |
| 9096 | * @lastgt: pointer to store the last '>' from the input |
| 9097 | * |
| 9098 | * Lookup the last < and > in the current chunk |
| 9099 | */ |
| 9100 | static void |
| 9101 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 9102 | const xmlChar **lastgt) { |
| 9103 | const xmlChar *tmp; |
| 9104 | |
| 9105 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 9106 | xmlGenericError(xmlGenericErrorContext, |
| 9107 | "Internal error: xmlParseGetLasts\n"); |
| 9108 | return; |
| 9109 | } |
| 9110 | if ((ctxt->progressive == 1) && (ctxt->inputNr == 1)) { |
| 9111 | tmp = ctxt->input->end; |
| 9112 | tmp--; |
| 9113 | while ((tmp >= ctxt->input->base) && (*tmp != '<') && |
| 9114 | (*tmp != '>')) tmp--; |
| 9115 | if (tmp < ctxt->input->base) { |
| 9116 | *lastlt = NULL; |
| 9117 | *lastgt = NULL; |
| 9118 | } else if (*tmp == '<') { |
| 9119 | *lastlt = tmp; |
| 9120 | tmp--; |
| 9121 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 9122 | if (tmp < ctxt->input->base) |
| 9123 | *lastgt = NULL; |
| 9124 | else |
| 9125 | *lastgt = tmp; |
| 9126 | } else { |
| 9127 | *lastgt = tmp; |
| 9128 | tmp--; |
| 9129 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
| 9130 | if (tmp < ctxt->input->base) |
| 9131 | *lastlt = NULL; |
| 9132 | else |
| 9133 | *lastlt = tmp; |
| 9134 | } |
| 9135 | |
| 9136 | } else { |
| 9137 | *lastlt = NULL; |
| 9138 | *lastgt = NULL; |
| 9139 | } |
| 9140 | } |
| 9141 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9142 | * xmlParseTryOrFinish: |
| 9143 | * @ctxt: an XML parser context |
| 9144 | * @terminate: last chunk indicator |
| 9145 | * |
| 9146 | * Try to progress on parsing |
| 9147 | * |
| 9148 | * Returns zero if no parsing was possible |
| 9149 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9150 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9151 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 9152 | int ret = 0; |
| 9153 | int avail; |
| 9154 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9155 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9156 | |
| 9157 | #ifdef DEBUG_PUSH |
| 9158 | switch (ctxt->instate) { |
| 9159 | case XML_PARSER_EOF: |
| 9160 | xmlGenericError(xmlGenericErrorContext, |
| 9161 | "PP: try EOF\n"); break; |
| 9162 | case XML_PARSER_START: |
| 9163 | xmlGenericError(xmlGenericErrorContext, |
| 9164 | "PP: try START\n"); break; |
| 9165 | case XML_PARSER_MISC: |
| 9166 | xmlGenericError(xmlGenericErrorContext, |
| 9167 | "PP: try MISC\n");break; |
| 9168 | case XML_PARSER_COMMENT: |
| 9169 | xmlGenericError(xmlGenericErrorContext, |
| 9170 | "PP: try COMMENT\n");break; |
| 9171 | case XML_PARSER_PROLOG: |
| 9172 | xmlGenericError(xmlGenericErrorContext, |
| 9173 | "PP: try PROLOG\n");break; |
| 9174 | case XML_PARSER_START_TAG: |
| 9175 | xmlGenericError(xmlGenericErrorContext, |
| 9176 | "PP: try START_TAG\n");break; |
| 9177 | case XML_PARSER_CONTENT: |
| 9178 | xmlGenericError(xmlGenericErrorContext, |
| 9179 | "PP: try CONTENT\n");break; |
| 9180 | case XML_PARSER_CDATA_SECTION: |
| 9181 | xmlGenericError(xmlGenericErrorContext, |
| 9182 | "PP: try CDATA_SECTION\n");break; |
| 9183 | case XML_PARSER_END_TAG: |
| 9184 | xmlGenericError(xmlGenericErrorContext, |
| 9185 | "PP: try END_TAG\n");break; |
| 9186 | case XML_PARSER_ENTITY_DECL: |
| 9187 | xmlGenericError(xmlGenericErrorContext, |
| 9188 | "PP: try ENTITY_DECL\n");break; |
| 9189 | case XML_PARSER_ENTITY_VALUE: |
| 9190 | xmlGenericError(xmlGenericErrorContext, |
| 9191 | "PP: try ENTITY_VALUE\n");break; |
| 9192 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 9193 | xmlGenericError(xmlGenericErrorContext, |
| 9194 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 9195 | case XML_PARSER_DTD: |
| 9196 | xmlGenericError(xmlGenericErrorContext, |
| 9197 | "PP: try DTD\n");break; |
| 9198 | case XML_PARSER_EPILOG: |
| 9199 | xmlGenericError(xmlGenericErrorContext, |
| 9200 | "PP: try EPILOG\n");break; |
| 9201 | case XML_PARSER_PI: |
| 9202 | xmlGenericError(xmlGenericErrorContext, |
| 9203 | "PP: try PI\n");break; |
| 9204 | case XML_PARSER_IGNORE: |
| 9205 | xmlGenericError(xmlGenericErrorContext, |
| 9206 | "PP: try IGNORE\n");break; |
| 9207 | } |
| 9208 | #endif |
| 9209 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9210 | if (ctxt->input->cur - ctxt->input->base > 4096) { |
| 9211 | xmlSHRINK(ctxt); |
| 9212 | ctxt->checkIndex = 0; |
| 9213 | } |
| 9214 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 9215 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9216 | while (1) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9217 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 9218 | return(0); |
| 9219 | |
| 9220 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9221 | /* |
| 9222 | * Pop-up of finished entities. |
| 9223 | */ |
| 9224 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 9225 | xmlPopInput(ctxt); |
| 9226 | |
| 9227 | if (ctxt->input ==NULL) break; |
| 9228 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9229 | avail = ctxt->input->length - |
| 9230 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 9231 | else { |
| 9232 | /* |
| 9233 | * If we are operating on converted input, try to flush |
| 9234 | * remainng chars to avoid them stalling in the non-converted |
| 9235 | * buffer. |
| 9236 | */ |
| 9237 | if ((ctxt->input->buf->raw != NULL) && |
| 9238 | (ctxt->input->buf->raw->use > 0)) { |
| 9239 | int base = ctxt->input->base - |
| 9240 | ctxt->input->buf->buffer->content; |
| 9241 | int current = ctxt->input->cur - ctxt->input->base; |
| 9242 | |
| 9243 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 9244 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 9245 | ctxt->input->cur = ctxt->input->base + current; |
| 9246 | ctxt->input->end = |
| 9247 | &ctxt->input->buf->buffer->content[ |
| 9248 | ctxt->input->buf->buffer->use]; |
| 9249 | } |
| 9250 | avail = ctxt->input->buf->buffer->use - |
| 9251 | (ctxt->input->cur - ctxt->input->base); |
| 9252 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9253 | if (avail < 1) |
| 9254 | goto done; |
| 9255 | switch (ctxt->instate) { |
| 9256 | case XML_PARSER_EOF: |
| 9257 | /* |
| 9258 | * Document parsing is done ! |
| 9259 | */ |
| 9260 | goto done; |
| 9261 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 9262 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 9263 | xmlChar start[4]; |
| 9264 | xmlCharEncoding enc; |
| 9265 | |
| 9266 | /* |
| 9267 | * Very first chars read from the document flow. |
| 9268 | */ |
| 9269 | if (avail < 4) |
| 9270 | goto done; |
| 9271 | |
| 9272 | /* |
| 9273 | * Get the 4 first bytes and decode the charset |
| 9274 | * if enc != XML_CHAR_ENCODING_NONE |
| 9275 | * plug some encoding conversion routines. |
| 9276 | */ |
| 9277 | start[0] = RAW; |
| 9278 | start[1] = NXT(1); |
| 9279 | start[2] = NXT(2); |
| 9280 | start[3] = NXT(3); |
| 9281 | enc = xmlDetectCharEncoding(start, 4); |
| 9282 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 9283 | xmlSwitchEncoding(ctxt, enc); |
| 9284 | } |
| 9285 | break; |
| 9286 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9287 | |
| 9288 | cur = ctxt->input->cur[0]; |
| 9289 | next = ctxt->input->cur[1]; |
| 9290 | if (cur == 0) { |
| 9291 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9292 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9293 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9294 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9295 | ctxt->instate = XML_PARSER_EOF; |
| 9296 | #ifdef DEBUG_PUSH |
| 9297 | xmlGenericError(xmlGenericErrorContext, |
| 9298 | "PP: entering EOF\n"); |
| 9299 | #endif |
| 9300 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9301 | ctxt->sax->endDocument(ctxt->userData); |
| 9302 | goto done; |
| 9303 | } |
| 9304 | if ((cur == '<') && (next == '?')) { |
| 9305 | /* PI or XML decl */ |
| 9306 | if (avail < 5) return(ret); |
| 9307 | if ((!terminate) && |
| 9308 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9309 | return(ret); |
| 9310 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9311 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9312 | &xmlDefaultSAXLocator); |
| 9313 | if ((ctxt->input->cur[2] == 'x') && |
| 9314 | (ctxt->input->cur[3] == 'm') && |
| 9315 | (ctxt->input->cur[4] == 'l') && |
| 9316 | (IS_BLANK(ctxt->input->cur[5]))) { |
| 9317 | ret += 5; |
| 9318 | #ifdef DEBUG_PUSH |
| 9319 | xmlGenericError(xmlGenericErrorContext, |
| 9320 | "PP: Parsing XML Decl\n"); |
| 9321 | #endif |
| 9322 | xmlParseXMLDecl(ctxt); |
| 9323 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 9324 | /* |
| 9325 | * The XML REC instructs us to stop parsing right |
| 9326 | * here |
| 9327 | */ |
| 9328 | ctxt->instate = XML_PARSER_EOF; |
| 9329 | return(0); |
| 9330 | } |
| 9331 | ctxt->standalone = ctxt->input->standalone; |
| 9332 | if ((ctxt->encoding == NULL) && |
| 9333 | (ctxt->input->encoding != NULL)) |
| 9334 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 9335 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9336 | (!ctxt->disableSAX)) |
| 9337 | ctxt->sax->startDocument(ctxt->userData); |
| 9338 | ctxt->instate = XML_PARSER_MISC; |
| 9339 | #ifdef DEBUG_PUSH |
| 9340 | xmlGenericError(xmlGenericErrorContext, |
| 9341 | "PP: entering MISC\n"); |
| 9342 | #endif |
| 9343 | } else { |
| 9344 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 9345 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9346 | (!ctxt->disableSAX)) |
| 9347 | ctxt->sax->startDocument(ctxt->userData); |
| 9348 | ctxt->instate = XML_PARSER_MISC; |
| 9349 | #ifdef DEBUG_PUSH |
| 9350 | xmlGenericError(xmlGenericErrorContext, |
| 9351 | "PP: entering MISC\n"); |
| 9352 | #endif |
| 9353 | } |
| 9354 | } else { |
| 9355 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9356 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9357 | &xmlDefaultSAXLocator); |
| 9358 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 9359 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9360 | (!ctxt->disableSAX)) |
| 9361 | ctxt->sax->startDocument(ctxt->userData); |
| 9362 | ctxt->instate = XML_PARSER_MISC; |
| 9363 | #ifdef DEBUG_PUSH |
| 9364 | xmlGenericError(xmlGenericErrorContext, |
| 9365 | "PP: entering MISC\n"); |
| 9366 | #endif |
| 9367 | } |
| 9368 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9369 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9370 | const xmlChar *name; |
| 9371 | const xmlChar *prefix; |
| 9372 | const xmlChar *URI; |
| 9373 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9374 | |
| 9375 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9376 | goto done; |
| 9377 | cur = ctxt->input->cur[0]; |
| 9378 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9379 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9380 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9381 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9382 | ctxt->sax->endDocument(ctxt->userData); |
| 9383 | goto done; |
| 9384 | } |
| 9385 | if (!terminate) { |
| 9386 | if (ctxt->progressive) { |
| 9387 | if ((lastgt == NULL) || (ctxt->input->cur > lastgt)) |
| 9388 | goto done; |
| 9389 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9390 | goto done; |
| 9391 | } |
| 9392 | } |
| 9393 | if (ctxt->spaceNr == 0) |
| 9394 | spacePush(ctxt, -1); |
| 9395 | else |
| 9396 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9397 | if (ctxt->sax2) |
| 9398 | name = xmlParseStartTag2(ctxt, &prefix, &URI); |
| 9399 | else |
| 9400 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9401 | if (name == NULL) { |
| 9402 | spacePop(ctxt); |
| 9403 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9404 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9405 | ctxt->sax->endDocument(ctxt->userData); |
| 9406 | goto done; |
| 9407 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9408 | /* |
| 9409 | * [ VC: Root Element Type ] |
| 9410 | * The Name in the document type declaration must match |
| 9411 | * the element type of the root element. |
| 9412 | */ |
| 9413 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9414 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9415 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
| 9416 | |
| 9417 | /* |
| 9418 | * Check for an Empty Element. |
| 9419 | */ |
| 9420 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9421 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9422 | |
| 9423 | if (ctxt->sax2) { |
| 9424 | if ((ctxt->sax != NULL) && |
| 9425 | (ctxt->sax->endElementNs != NULL) && |
| 9426 | (!ctxt->disableSAX)) |
| 9427 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 9428 | prefix, URI); |
| 9429 | } else { |
| 9430 | if ((ctxt->sax != NULL) && |
| 9431 | (ctxt->sax->endElement != NULL) && |
| 9432 | (!ctxt->disableSAX)) |
| 9433 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9434 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9435 | spacePop(ctxt); |
| 9436 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9437 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9438 | } else { |
| 9439 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9440 | } |
| 9441 | break; |
| 9442 | } |
| 9443 | if (RAW == '>') { |
| 9444 | NEXT; |
| 9445 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9446 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9447 | "Couldn't find end of Start Tag %s\n", |
| 9448 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9449 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9450 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9451 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9452 | if (ctxt->sax2) |
| 9453 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
| 9454 | else |
| 9455 | namePush(ctxt, name); |
| 9456 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9457 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9458 | break; |
| 9459 | } |
| 9460 | case XML_PARSER_CONTENT: { |
| 9461 | const xmlChar *test; |
| 9462 | unsigned int cons; |
| 9463 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9464 | goto done; |
| 9465 | cur = ctxt->input->cur[0]; |
| 9466 | next = ctxt->input->cur[1]; |
| 9467 | |
| 9468 | test = CUR_PTR; |
| 9469 | cons = ctxt->input->consumed; |
| 9470 | if ((cur == '<') && (next == '/')) { |
| 9471 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9472 | break; |
| 9473 | } else if ((cur == '<') && (next == '?')) { |
| 9474 | if ((!terminate) && |
| 9475 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9476 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9477 | xmlParsePI(ctxt); |
| 9478 | } else if ((cur == '<') && (next != '!')) { |
| 9479 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9480 | break; |
| 9481 | } else if ((cur == '<') && (next == '!') && |
| 9482 | (ctxt->input->cur[2] == '-') && |
| 9483 | (ctxt->input->cur[3] == '-')) { |
| 9484 | if ((!terminate) && |
| 9485 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9486 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9487 | xmlParseComment(ctxt); |
| 9488 | ctxt->instate = XML_PARSER_CONTENT; |
| 9489 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 9490 | (ctxt->input->cur[2] == '[') && |
| 9491 | (ctxt->input->cur[3] == 'C') && |
| 9492 | (ctxt->input->cur[4] == 'D') && |
| 9493 | (ctxt->input->cur[5] == 'A') && |
| 9494 | (ctxt->input->cur[6] == 'T') && |
| 9495 | (ctxt->input->cur[7] == 'A') && |
| 9496 | (ctxt->input->cur[8] == '[')) { |
| 9497 | SKIP(9); |
| 9498 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9499 | break; |
| 9500 | } else if ((cur == '<') && (next == '!') && |
| 9501 | (avail < 9)) { |
| 9502 | goto done; |
| 9503 | } else if (cur == '&') { |
| 9504 | if ((!terminate) && |
| 9505 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 9506 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9507 | xmlParseReference(ctxt); |
| 9508 | } else { |
| 9509 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 9510 | /* |
| 9511 | * Goal of the following test is: |
| 9512 | * - minimize calls to the SAX 'character' callback |
| 9513 | * when they are mergeable |
| 9514 | * - handle an problem for isBlank when we only parse |
| 9515 | * a sequence of blank chars and the next one is |
| 9516 | * not available to check against '<' presence. |
| 9517 | * - tries to homogenize the differences in SAX |
| 9518 | * callbacks between the push and pull versions |
| 9519 | * of the parser. |
| 9520 | */ |
| 9521 | if ((ctxt->inputNr == 1) && |
| 9522 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 9523 | if (!terminate) { |
| 9524 | if (ctxt->progressive) { |
| 9525 | if ((lastlt == NULL) || |
| 9526 | (ctxt->input->cur > lastlt)) |
| 9527 | goto done; |
| 9528 | } else if (xmlParseLookupSequence(ctxt, |
| 9529 | '<', 0, 0) < 0) { |
| 9530 | goto done; |
| 9531 | } |
| 9532 | } |
| 9533 | } |
| 9534 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9535 | xmlParseCharData(ctxt, 0); |
| 9536 | } |
| 9537 | /* |
| 9538 | * Pop-up of finished entities. |
| 9539 | */ |
| 9540 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 9541 | xmlPopInput(ctxt); |
| 9542 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9543 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9544 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9545 | ctxt->instate = XML_PARSER_EOF; |
| 9546 | break; |
| 9547 | } |
| 9548 | break; |
| 9549 | } |
| 9550 | case XML_PARSER_END_TAG: |
| 9551 | if (avail < 2) |
| 9552 | goto done; |
| 9553 | if (!terminate) { |
| 9554 | if (ctxt->progressive) { |
| 9555 | if ((lastgt == NULL) || (ctxt->input->cur > lastgt)) |
| 9556 | goto done; |
| 9557 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9558 | goto done; |
| 9559 | } |
| 9560 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9561 | if (ctxt->sax2) { |
| 9562 | xmlParseEndTag2(ctxt, |
| 9563 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 9564 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
| 9565 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1]); |
| 9566 | nameNsPop(ctxt); |
| 9567 | } else |
| 9568 | xmlParseEndTag1(ctxt, 0); |
| 9569 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9570 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9571 | } else { |
| 9572 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9573 | } |
| 9574 | break; |
| 9575 | case XML_PARSER_CDATA_SECTION: { |
| 9576 | /* |
| 9577 | * The Push mode need to have the SAX callback for |
| 9578 | * cdataBlock merge back contiguous callbacks. |
| 9579 | */ |
| 9580 | int base; |
| 9581 | |
| 9582 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 9583 | if (base < 0) { |
| 9584 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
| 9585 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9586 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9587 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9588 | ctxt->input->cur, |
| 9589 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9590 | else if (ctxt->sax->characters != NULL) |
| 9591 | ctxt->sax->characters(ctxt->userData, |
| 9592 | ctxt->input->cur, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9593 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9594 | } |
| 9595 | SKIP(XML_PARSER_BIG_BUFFER_SIZE); |
| 9596 | ctxt->checkIndex = 0; |
| 9597 | } |
| 9598 | goto done; |
| 9599 | } else { |
| 9600 | if ((ctxt->sax != NULL) && (base > 0) && |
| 9601 | (!ctxt->disableSAX)) { |
| 9602 | if (ctxt->sax->cdataBlock != NULL) |
| 9603 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9604 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9605 | else if (ctxt->sax->characters != NULL) |
| 9606 | ctxt->sax->characters(ctxt->userData, |
| 9607 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9608 | } |
| 9609 | SKIP(base + 3); |
| 9610 | ctxt->checkIndex = 0; |
| 9611 | ctxt->instate = XML_PARSER_CONTENT; |
| 9612 | #ifdef DEBUG_PUSH |
| 9613 | xmlGenericError(xmlGenericErrorContext, |
| 9614 | "PP: entering CONTENT\n"); |
| 9615 | #endif |
| 9616 | } |
| 9617 | break; |
| 9618 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9619 | case XML_PARSER_MISC: |
| 9620 | SKIP_BLANKS; |
| 9621 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9622 | avail = ctxt->input->length - |
| 9623 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9624 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9625 | avail = ctxt->input->buf->buffer->use - |
| 9626 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9627 | if (avail < 2) |
| 9628 | goto done; |
| 9629 | cur = ctxt->input->cur[0]; |
| 9630 | next = ctxt->input->cur[1]; |
| 9631 | if ((cur == '<') && (next == '?')) { |
| 9632 | if ((!terminate) && |
| 9633 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9634 | goto done; |
| 9635 | #ifdef DEBUG_PUSH |
| 9636 | xmlGenericError(xmlGenericErrorContext, |
| 9637 | "PP: Parsing PI\n"); |
| 9638 | #endif |
| 9639 | xmlParsePI(ctxt); |
| 9640 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9641 | (ctxt->input->cur[2] == '-') && |
| 9642 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9643 | if ((!terminate) && |
| 9644 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9645 | goto done; |
| 9646 | #ifdef DEBUG_PUSH |
| 9647 | xmlGenericError(xmlGenericErrorContext, |
| 9648 | "PP: Parsing Comment\n"); |
| 9649 | #endif |
| 9650 | xmlParseComment(ctxt); |
| 9651 | ctxt->instate = XML_PARSER_MISC; |
| 9652 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9653 | (ctxt->input->cur[2] == 'D') && |
| 9654 | (ctxt->input->cur[3] == 'O') && |
| 9655 | (ctxt->input->cur[4] == 'C') && |
| 9656 | (ctxt->input->cur[5] == 'T') && |
| 9657 | (ctxt->input->cur[6] == 'Y') && |
| 9658 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9659 | (ctxt->input->cur[8] == 'E')) { |
| 9660 | if ((!terminate) && |
| 9661 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 9662 | goto done; |
| 9663 | #ifdef DEBUG_PUSH |
| 9664 | xmlGenericError(xmlGenericErrorContext, |
| 9665 | "PP: Parsing internal subset\n"); |
| 9666 | #endif |
| 9667 | ctxt->inSubset = 1; |
| 9668 | xmlParseDocTypeDecl(ctxt); |
| 9669 | if (RAW == '[') { |
| 9670 | ctxt->instate = XML_PARSER_DTD; |
| 9671 | #ifdef DEBUG_PUSH |
| 9672 | xmlGenericError(xmlGenericErrorContext, |
| 9673 | "PP: entering DTD\n"); |
| 9674 | #endif |
| 9675 | } else { |
| 9676 | /* |
| 9677 | * Create and update the external subset. |
| 9678 | */ |
| 9679 | ctxt->inSubset = 2; |
| 9680 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9681 | (ctxt->sax->externalSubset != NULL)) |
| 9682 | ctxt->sax->externalSubset(ctxt->userData, |
| 9683 | ctxt->intSubName, ctxt->extSubSystem, |
| 9684 | ctxt->extSubURI); |
| 9685 | ctxt->inSubset = 0; |
| 9686 | ctxt->instate = XML_PARSER_PROLOG; |
| 9687 | #ifdef DEBUG_PUSH |
| 9688 | xmlGenericError(xmlGenericErrorContext, |
| 9689 | "PP: entering PROLOG\n"); |
| 9690 | #endif |
| 9691 | } |
| 9692 | } else if ((cur == '<') && (next == '!') && |
| 9693 | (avail < 9)) { |
| 9694 | goto done; |
| 9695 | } else { |
| 9696 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9697 | ctxt->progressive = 1; |
| 9698 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9699 | #ifdef DEBUG_PUSH |
| 9700 | xmlGenericError(xmlGenericErrorContext, |
| 9701 | "PP: entering START_TAG\n"); |
| 9702 | #endif |
| 9703 | } |
| 9704 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9705 | case XML_PARSER_PROLOG: |
| 9706 | SKIP_BLANKS; |
| 9707 | if (ctxt->input->buf == NULL) |
| 9708 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9709 | else |
| 9710 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9711 | if (avail < 2) |
| 9712 | goto done; |
| 9713 | cur = ctxt->input->cur[0]; |
| 9714 | next = ctxt->input->cur[1]; |
| 9715 | if ((cur == '<') && (next == '?')) { |
| 9716 | if ((!terminate) && |
| 9717 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9718 | goto done; |
| 9719 | #ifdef DEBUG_PUSH |
| 9720 | xmlGenericError(xmlGenericErrorContext, |
| 9721 | "PP: Parsing PI\n"); |
| 9722 | #endif |
| 9723 | xmlParsePI(ctxt); |
| 9724 | } else if ((cur == '<') && (next == '!') && |
| 9725 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9726 | if ((!terminate) && |
| 9727 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9728 | goto done; |
| 9729 | #ifdef DEBUG_PUSH |
| 9730 | xmlGenericError(xmlGenericErrorContext, |
| 9731 | "PP: Parsing Comment\n"); |
| 9732 | #endif |
| 9733 | xmlParseComment(ctxt); |
| 9734 | ctxt->instate = XML_PARSER_PROLOG; |
| 9735 | } else if ((cur == '<') && (next == '!') && |
| 9736 | (avail < 4)) { |
| 9737 | goto done; |
| 9738 | } else { |
| 9739 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9740 | ctxt->progressive = 1; |
| 9741 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9742 | #ifdef DEBUG_PUSH |
| 9743 | xmlGenericError(xmlGenericErrorContext, |
| 9744 | "PP: entering START_TAG\n"); |
| 9745 | #endif |
| 9746 | } |
| 9747 | break; |
| 9748 | case XML_PARSER_EPILOG: |
| 9749 | SKIP_BLANKS; |
| 9750 | if (ctxt->input->buf == NULL) |
| 9751 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9752 | else |
| 9753 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9754 | if (avail < 2) |
| 9755 | goto done; |
| 9756 | cur = ctxt->input->cur[0]; |
| 9757 | next = ctxt->input->cur[1]; |
| 9758 | if ((cur == '<') && (next == '?')) { |
| 9759 | if ((!terminate) && |
| 9760 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9761 | goto done; |
| 9762 | #ifdef DEBUG_PUSH |
| 9763 | xmlGenericError(xmlGenericErrorContext, |
| 9764 | "PP: Parsing PI\n"); |
| 9765 | #endif |
| 9766 | xmlParsePI(ctxt); |
| 9767 | ctxt->instate = XML_PARSER_EPILOG; |
| 9768 | } else if ((cur == '<') && (next == '!') && |
| 9769 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9770 | if ((!terminate) && |
| 9771 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9772 | goto done; |
| 9773 | #ifdef DEBUG_PUSH |
| 9774 | xmlGenericError(xmlGenericErrorContext, |
| 9775 | "PP: Parsing Comment\n"); |
| 9776 | #endif |
| 9777 | xmlParseComment(ctxt); |
| 9778 | ctxt->instate = XML_PARSER_EPILOG; |
| 9779 | } else if ((cur == '<') && (next == '!') && |
| 9780 | (avail < 4)) { |
| 9781 | goto done; |
| 9782 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9783 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9784 | ctxt->instate = XML_PARSER_EOF; |
| 9785 | #ifdef DEBUG_PUSH |
| 9786 | xmlGenericError(xmlGenericErrorContext, |
| 9787 | "PP: entering EOF\n"); |
| 9788 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 9789 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9790 | ctxt->sax->endDocument(ctxt->userData); |
| 9791 | goto done; |
| 9792 | } |
| 9793 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9794 | case XML_PARSER_DTD: { |
| 9795 | /* |
| 9796 | * Sorry but progressive parsing of the internal subset |
| 9797 | * is not expected to be supported. We first check that |
| 9798 | * the full content of the internal subset is available and |
| 9799 | * the parsing is launched only at that point. |
| 9800 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 9801 | * section and not in a ']]>' sequence which are conditional |
| 9802 | * sections (whoever argued to keep that crap in XML deserve |
| 9803 | * a place in hell !). |
| 9804 | */ |
| 9805 | int base, i; |
| 9806 | xmlChar *buf; |
| 9807 | xmlChar quote = 0; |
| 9808 | |
| 9809 | base = ctxt->input->cur - ctxt->input->base; |
| 9810 | if (base < 0) return(0); |
| 9811 | if (ctxt->checkIndex > base) |
| 9812 | base = ctxt->checkIndex; |
| 9813 | buf = ctxt->input->buf->buffer->content; |
| 9814 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 9815 | base++) { |
| 9816 | if (quote != 0) { |
| 9817 | if (buf[base] == quote) |
| 9818 | quote = 0; |
| 9819 | continue; |
| 9820 | } |
| 9821 | if (buf[base] == '"') { |
| 9822 | quote = '"'; |
| 9823 | continue; |
| 9824 | } |
| 9825 | if (buf[base] == '\'') { |
| 9826 | quote = '\''; |
| 9827 | continue; |
| 9828 | } |
| 9829 | if (buf[base] == ']') { |
| 9830 | if ((unsigned int) base +1 >= |
| 9831 | ctxt->input->buf->buffer->use) |
| 9832 | break; |
| 9833 | if (buf[base + 1] == ']') { |
| 9834 | /* conditional crap, skip both ']' ! */ |
| 9835 | base++; |
| 9836 | continue; |
| 9837 | } |
| 9838 | for (i = 0; |
| 9839 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 9840 | i++) { |
| 9841 | if (buf[base + i] == '>') |
| 9842 | goto found_end_int_subset; |
| 9843 | } |
| 9844 | break; |
| 9845 | } |
| 9846 | } |
| 9847 | /* |
| 9848 | * We didn't found the end of the Internal subset |
| 9849 | */ |
| 9850 | if (quote == 0) |
| 9851 | ctxt->checkIndex = base; |
| 9852 | #ifdef DEBUG_PUSH |
| 9853 | if (next == 0) |
| 9854 | xmlGenericError(xmlGenericErrorContext, |
| 9855 | "PP: lookup of int subset end filed\n"); |
| 9856 | #endif |
| 9857 | goto done; |
| 9858 | |
| 9859 | found_end_int_subset: |
| 9860 | xmlParseInternalSubset(ctxt); |
| 9861 | ctxt->inSubset = 2; |
| 9862 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9863 | (ctxt->sax->externalSubset != NULL)) |
| 9864 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 9865 | ctxt->extSubSystem, ctxt->extSubURI); |
| 9866 | ctxt->inSubset = 0; |
| 9867 | ctxt->instate = XML_PARSER_PROLOG; |
| 9868 | ctxt->checkIndex = 0; |
| 9869 | #ifdef DEBUG_PUSH |
| 9870 | xmlGenericError(xmlGenericErrorContext, |
| 9871 | "PP: entering PROLOG\n"); |
| 9872 | #endif |
| 9873 | break; |
| 9874 | } |
| 9875 | case XML_PARSER_COMMENT: |
| 9876 | xmlGenericError(xmlGenericErrorContext, |
| 9877 | "PP: internal error, state == COMMENT\n"); |
| 9878 | ctxt->instate = XML_PARSER_CONTENT; |
| 9879 | #ifdef DEBUG_PUSH |
| 9880 | xmlGenericError(xmlGenericErrorContext, |
| 9881 | "PP: entering CONTENT\n"); |
| 9882 | #endif |
| 9883 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9884 | case XML_PARSER_IGNORE: |
| 9885 | xmlGenericError(xmlGenericErrorContext, |
| 9886 | "PP: internal error, state == IGNORE"); |
| 9887 | ctxt->instate = XML_PARSER_DTD; |
| 9888 | #ifdef DEBUG_PUSH |
| 9889 | xmlGenericError(xmlGenericErrorContext, |
| 9890 | "PP: entering DTD\n"); |
| 9891 | #endif |
| 9892 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9893 | case XML_PARSER_PI: |
| 9894 | xmlGenericError(xmlGenericErrorContext, |
| 9895 | "PP: internal error, state == PI\n"); |
| 9896 | ctxt->instate = XML_PARSER_CONTENT; |
| 9897 | #ifdef DEBUG_PUSH |
| 9898 | xmlGenericError(xmlGenericErrorContext, |
| 9899 | "PP: entering CONTENT\n"); |
| 9900 | #endif |
| 9901 | break; |
| 9902 | case XML_PARSER_ENTITY_DECL: |
| 9903 | xmlGenericError(xmlGenericErrorContext, |
| 9904 | "PP: internal error, state == ENTITY_DECL\n"); |
| 9905 | ctxt->instate = XML_PARSER_DTD; |
| 9906 | #ifdef DEBUG_PUSH |
| 9907 | xmlGenericError(xmlGenericErrorContext, |
| 9908 | "PP: entering DTD\n"); |
| 9909 | #endif |
| 9910 | break; |
| 9911 | case XML_PARSER_ENTITY_VALUE: |
| 9912 | xmlGenericError(xmlGenericErrorContext, |
| 9913 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 9914 | ctxt->instate = XML_PARSER_CONTENT; |
| 9915 | #ifdef DEBUG_PUSH |
| 9916 | xmlGenericError(xmlGenericErrorContext, |
| 9917 | "PP: entering DTD\n"); |
| 9918 | #endif |
| 9919 | break; |
| 9920 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 9921 | xmlGenericError(xmlGenericErrorContext, |
| 9922 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 9923 | ctxt->instate = XML_PARSER_START_TAG; |
| 9924 | #ifdef DEBUG_PUSH |
| 9925 | xmlGenericError(xmlGenericErrorContext, |
| 9926 | "PP: entering START_TAG\n"); |
| 9927 | #endif |
| 9928 | break; |
| 9929 | case XML_PARSER_SYSTEM_LITERAL: |
| 9930 | xmlGenericError(xmlGenericErrorContext, |
| 9931 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 9932 | ctxt->instate = XML_PARSER_START_TAG; |
| 9933 | #ifdef DEBUG_PUSH |
| 9934 | xmlGenericError(xmlGenericErrorContext, |
| 9935 | "PP: entering START_TAG\n"); |
| 9936 | #endif |
| 9937 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 9938 | case XML_PARSER_PUBLIC_LITERAL: |
| 9939 | xmlGenericError(xmlGenericErrorContext, |
| 9940 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 9941 | ctxt->instate = XML_PARSER_START_TAG; |
| 9942 | #ifdef DEBUG_PUSH |
| 9943 | xmlGenericError(xmlGenericErrorContext, |
| 9944 | "PP: entering START_TAG\n"); |
| 9945 | #endif |
| 9946 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9947 | } |
| 9948 | } |
| 9949 | done: |
| 9950 | #ifdef DEBUG_PUSH |
| 9951 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 9952 | #endif |
| 9953 | return(ret); |
| 9954 | } |
| 9955 | |
| 9956 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9957 | * xmlParseChunk: |
| 9958 | * @ctxt: an XML parser context |
| 9959 | * @chunk: an char array |
| 9960 | * @size: the size in byte of the chunk |
| 9961 | * @terminate: last chunk indicator |
| 9962 | * |
| 9963 | * Parse a Chunk of memory |
| 9964 | * |
| 9965 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 9966 | */ |
| 9967 | int |
| 9968 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 9969 | int terminate) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9970 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 9971 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 9972 | if (ctxt->instate == XML_PARSER_START) |
| 9973 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9974 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 9975 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 9976 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 9977 | int cur = ctxt->input->cur - ctxt->input->base; |
| 9978 | |
| 9979 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 9980 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 9981 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 9982 | ctxt->input->end = |
| 9983 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9984 | #ifdef DEBUG_PUSH |
| 9985 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 9986 | #endif |
| 9987 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9988 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 9989 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 9990 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 9991 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 9992 | (in->raw != NULL)) { |
| 9993 | int nbchars; |
| 9994 | |
| 9995 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 9996 | if (nbchars < 0) { |
| 9997 | xmlGenericError(xmlGenericErrorContext, |
| 9998 | "xmlParseChunk: encoder error\n"); |
| 9999 | return(XML_ERR_INVALID_ENCODING); |
| 10000 | } |
| 10001 | } |
| 10002 | } |
| 10003 | } |
| 10004 | xmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10005 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 10006 | return(ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10007 | if (terminate) { |
| 10008 | /* |
| 10009 | * Check for termination |
| 10010 | */ |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10011 | int avail = 0; |
| 10012 | if (ctxt->input->buf == NULL) |
| 10013 | avail = ctxt->input->length - |
| 10014 | (ctxt->input->cur - ctxt->input->base); |
| 10015 | else |
| 10016 | avail = ctxt->input->buf->buffer->use - |
| 10017 | (ctxt->input->cur - ctxt->input->base); |
| 10018 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10019 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 10020 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10021 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10022 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10023 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10024 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10025 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10026 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10027 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10028 | ctxt->sax->endDocument(ctxt->userData); |
| 10029 | } |
| 10030 | ctxt->instate = XML_PARSER_EOF; |
| 10031 | } |
| 10032 | return((xmlParserErrors) ctxt->errNo); |
| 10033 | } |
| 10034 | |
| 10035 | /************************************************************************ |
| 10036 | * * |
| 10037 | * I/O front end functions to the parser * |
| 10038 | * * |
| 10039 | ************************************************************************/ |
| 10040 | |
| 10041 | /** |
| 10042 | * xmlStopParser: |
| 10043 | * @ctxt: an XML parser context |
| 10044 | * |
| 10045 | * Blocks further parser processing |
| 10046 | */ |
| 10047 | void |
| 10048 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 10049 | ctxt->instate = XML_PARSER_EOF; |
| 10050 | if (ctxt->input != NULL) |
| 10051 | ctxt->input->cur = BAD_CAST""; |
| 10052 | } |
| 10053 | |
| 10054 | /** |
| 10055 | * xmlCreatePushParserCtxt: |
| 10056 | * @sax: a SAX handler |
| 10057 | * @user_data: The user data returned on SAX callbacks |
| 10058 | * @chunk: a pointer to an array of chars |
| 10059 | * @size: number of chars in the array |
| 10060 | * @filename: an optional file name or URI |
| 10061 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 10062 | * Create a parser context for using the XML parser in push mode. |
| 10063 | * If @buffer and @size are non-NULL, the data is used to detect |
| 10064 | * the encoding. The remaining characters will be parsed so they |
| 10065 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10066 | * To allow content encoding detection, @size should be >= 4 |
| 10067 | * The value of @filename is used for fetching external entities |
| 10068 | * and error/warning reports. |
| 10069 | * |
| 10070 | * Returns the new parser context or NULL |
| 10071 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 10072 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10073 | xmlParserCtxtPtr |
| 10074 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 10075 | const char *chunk, int size, const char *filename) { |
| 10076 | xmlParserCtxtPtr ctxt; |
| 10077 | xmlParserInputPtr inputStream; |
| 10078 | xmlParserInputBufferPtr buf; |
| 10079 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 10080 | |
| 10081 | /* |
| 10082 | * plug some encoding conversion routines |
| 10083 | */ |
| 10084 | if ((chunk != NULL) && (size >= 4)) |
| 10085 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 10086 | |
| 10087 | buf = xmlAllocParserInputBuffer(enc); |
| 10088 | if (buf == NULL) return(NULL); |
| 10089 | |
| 10090 | ctxt = xmlNewParserCtxt(); |
| 10091 | if (ctxt == NULL) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10092 | xmlGenericError(xmlGenericErrorContext, |
| 10093 | "xml parser: out of memory\n"); |
| 10094 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10095 | return(NULL); |
| 10096 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10097 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 10098 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10099 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10100 | xmlFreeParserInputBuffer(buf); |
| 10101 | xmlFreeParserCtxt(ctxt); |
| 10102 | return(NULL); |
| 10103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10104 | if (sax != NULL) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 10105 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10106 | xmlFree(ctxt->sax); |
| 10107 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 10108 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10109 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10110 | xmlFreeParserInputBuffer(buf); |
| 10111 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10112 | return(NULL); |
| 10113 | } |
| 10114 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 10115 | if (user_data != NULL) |
| 10116 | ctxt->userData = user_data; |
| 10117 | } |
| 10118 | if (filename == NULL) { |
| 10119 | ctxt->directory = NULL; |
| 10120 | } else { |
| 10121 | ctxt->directory = xmlParserGetDirectory(filename); |
| 10122 | } |
| 10123 | |
| 10124 | inputStream = xmlNewInputStream(ctxt); |
| 10125 | if (inputStream == NULL) { |
| 10126 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10127 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10128 | return(NULL); |
| 10129 | } |
| 10130 | |
| 10131 | if (filename == NULL) |
| 10132 | inputStream->filename = NULL; |
| 10133 | else |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 10134 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 10135 | xmlCanonicPath((const xmlChar *) filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10136 | inputStream->buf = buf; |
| 10137 | inputStream->base = inputStream->buf->buffer->content; |
| 10138 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 10139 | inputStream->end = |
| 10140 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10141 | |
| 10142 | inputPush(ctxt, inputStream); |
| 10143 | |
| 10144 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 10145 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 10146 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 10147 | int cur = ctxt->input->cur - ctxt->input->base; |
| 10148 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10149 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 10150 | |
| 10151 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10152 | ctxt->input->cur = ctxt->input->base + cur; |
| 10153 | ctxt->input->end = |
| 10154 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10155 | #ifdef DEBUG_PUSH |
| 10156 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 10157 | #endif |
| 10158 | } |
| 10159 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10160 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10161 | xmlSwitchEncoding(ctxt, enc); |
| 10162 | } |
| 10163 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10164 | return(ctxt); |
| 10165 | } |
| 10166 | |
| 10167 | /** |
| 10168 | * xmlCreateIOParserCtxt: |
| 10169 | * @sax: a SAX handler |
| 10170 | * @user_data: The user data returned on SAX callbacks |
| 10171 | * @ioread: an I/O read function |
| 10172 | * @ioclose: an I/O close function |
| 10173 | * @ioctx: an I/O handler |
| 10174 | * @enc: the charset encoding if known |
| 10175 | * |
| 10176 | * Create a parser context for using the XML parser with an existing |
| 10177 | * I/O stream |
| 10178 | * |
| 10179 | * Returns the new parser context or NULL |
| 10180 | */ |
| 10181 | xmlParserCtxtPtr |
| 10182 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 10183 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 10184 | void *ioctx, xmlCharEncoding enc) { |
| 10185 | xmlParserCtxtPtr ctxt; |
| 10186 | xmlParserInputPtr inputStream; |
| 10187 | xmlParserInputBufferPtr buf; |
| 10188 | |
| 10189 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
| 10190 | if (buf == NULL) return(NULL); |
| 10191 | |
| 10192 | ctxt = xmlNewParserCtxt(); |
| 10193 | if (ctxt == NULL) { |
| 10194 | xmlFree(buf); |
| 10195 | return(NULL); |
| 10196 | } |
| 10197 | if (sax != NULL) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 10198 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10199 | xmlFree(ctxt->sax); |
| 10200 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 10201 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10202 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10203 | xmlFree(ctxt); |
| 10204 | return(NULL); |
| 10205 | } |
| 10206 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 10207 | if (user_data != NULL) |
| 10208 | ctxt->userData = user_data; |
| 10209 | } |
| 10210 | |
| 10211 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 10212 | if (inputStream == NULL) { |
| 10213 | xmlFreeParserCtxt(ctxt); |
| 10214 | return(NULL); |
| 10215 | } |
| 10216 | inputPush(ctxt, inputStream); |
| 10217 | |
| 10218 | return(ctxt); |
| 10219 | } |
| 10220 | |
| 10221 | /************************************************************************ |
| 10222 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10223 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10224 | * * |
| 10225 | ************************************************************************/ |
| 10226 | |
| 10227 | /** |
| 10228 | * xmlIOParseDTD: |
| 10229 | * @sax: the SAX handler block or NULL |
| 10230 | * @input: an Input Buffer |
| 10231 | * @enc: the charset encoding if known |
| 10232 | * |
| 10233 | * Load and parse a DTD |
| 10234 | * |
| 10235 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10236 | * @input will be freed at parsing end. |
| 10237 | */ |
| 10238 | |
| 10239 | xmlDtdPtr |
| 10240 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 10241 | xmlCharEncoding enc) { |
| 10242 | xmlDtdPtr ret = NULL; |
| 10243 | xmlParserCtxtPtr ctxt; |
| 10244 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10245 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10246 | |
| 10247 | if (input == NULL) |
| 10248 | return(NULL); |
| 10249 | |
| 10250 | ctxt = xmlNewParserCtxt(); |
| 10251 | if (ctxt == NULL) { |
| 10252 | return(NULL); |
| 10253 | } |
| 10254 | |
| 10255 | /* |
| 10256 | * Set-up the SAX context |
| 10257 | */ |
| 10258 | if (sax != NULL) { |
| 10259 | if (ctxt->sax != NULL) |
| 10260 | xmlFree(ctxt->sax); |
| 10261 | ctxt->sax = sax; |
| 10262 | ctxt->userData = NULL; |
| 10263 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10264 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10265 | |
| 10266 | /* |
| 10267 | * generate a parser input from the I/O handler |
| 10268 | */ |
| 10269 | |
| 10270 | pinput = xmlNewIOInputStream(ctxt, input, enc); |
| 10271 | if (pinput == NULL) { |
| 10272 | if (sax != NULL) ctxt->sax = NULL; |
| 10273 | xmlFreeParserCtxt(ctxt); |
| 10274 | return(NULL); |
| 10275 | } |
| 10276 | |
| 10277 | /* |
| 10278 | * plug some encoding conversion routines here. |
| 10279 | */ |
| 10280 | xmlPushInput(ctxt, pinput); |
| 10281 | |
| 10282 | pinput->filename = NULL; |
| 10283 | pinput->line = 1; |
| 10284 | pinput->col = 1; |
| 10285 | pinput->base = ctxt->input->cur; |
| 10286 | pinput->cur = ctxt->input->cur; |
| 10287 | pinput->free = NULL; |
| 10288 | |
| 10289 | /* |
| 10290 | * let's parse that entity knowing it's an external subset. |
| 10291 | */ |
| 10292 | ctxt->inSubset = 2; |
| 10293 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10294 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10295 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10296 | |
| 10297 | if (enc == XML_CHAR_ENCODING_NONE) { |
| 10298 | /* |
| 10299 | * Get the 4 first bytes and decode the charset |
| 10300 | * if enc != XML_CHAR_ENCODING_NONE |
| 10301 | * plug some encoding conversion routines. |
| 10302 | */ |
| 10303 | start[0] = RAW; |
| 10304 | start[1] = NXT(1); |
| 10305 | start[2] = NXT(2); |
| 10306 | start[3] = NXT(3); |
| 10307 | enc = xmlDetectCharEncoding(start, 4); |
| 10308 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10309 | xmlSwitchEncoding(ctxt, enc); |
| 10310 | } |
| 10311 | } |
| 10312 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10313 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 10314 | |
| 10315 | if (ctxt->myDoc != NULL) { |
| 10316 | if (ctxt->wellFormed) { |
| 10317 | ret = ctxt->myDoc->extSubset; |
| 10318 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 10319 | if (ret != NULL) { |
| 10320 | xmlNodePtr tmp; |
| 10321 | |
| 10322 | ret->doc = NULL; |
| 10323 | tmp = ret->children; |
| 10324 | while (tmp != NULL) { |
| 10325 | tmp->doc = NULL; |
| 10326 | tmp = tmp->next; |
| 10327 | } |
| 10328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10329 | } else { |
| 10330 | ret = NULL; |
| 10331 | } |
| 10332 | xmlFreeDoc(ctxt->myDoc); |
| 10333 | ctxt->myDoc = NULL; |
| 10334 | } |
| 10335 | if (sax != NULL) ctxt->sax = NULL; |
| 10336 | xmlFreeParserCtxt(ctxt); |
| 10337 | |
| 10338 | return(ret); |
| 10339 | } |
| 10340 | |
| 10341 | /** |
| 10342 | * xmlSAXParseDTD: |
| 10343 | * @sax: the SAX handler block |
| 10344 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10345 | * @SystemID: a NAME* containing the URL to the DTD |
| 10346 | * |
| 10347 | * Load and parse an external subset. |
| 10348 | * |
| 10349 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10350 | */ |
| 10351 | |
| 10352 | xmlDtdPtr |
| 10353 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 10354 | const xmlChar *SystemID) { |
| 10355 | xmlDtdPtr ret = NULL; |
| 10356 | xmlParserCtxtPtr ctxt; |
| 10357 | xmlParserInputPtr input = NULL; |
| 10358 | xmlCharEncoding enc; |
| 10359 | |
| 10360 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 10361 | |
| 10362 | ctxt = xmlNewParserCtxt(); |
| 10363 | if (ctxt == NULL) { |
| 10364 | return(NULL); |
| 10365 | } |
| 10366 | |
| 10367 | /* |
| 10368 | * Set-up the SAX context |
| 10369 | */ |
| 10370 | if (sax != NULL) { |
| 10371 | if (ctxt->sax != NULL) |
| 10372 | xmlFree(ctxt->sax); |
| 10373 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 10374 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10375 | } |
| 10376 | |
| 10377 | /* |
| 10378 | * Ask the Entity resolver to load the damn thing |
| 10379 | */ |
| 10380 | |
| 10381 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | c6abc3d | 2003-04-26 13:27:30 +0000 | [diff] [blame] | 10382 | input = ctxt->sax->resolveEntity(ctxt, ExternalID, SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10383 | if (input == NULL) { |
| 10384 | if (sax != NULL) ctxt->sax = NULL; |
| 10385 | xmlFreeParserCtxt(ctxt); |
| 10386 | return(NULL); |
| 10387 | } |
| 10388 | |
| 10389 | /* |
| 10390 | * plug some encoding conversion routines here. |
| 10391 | */ |
| 10392 | xmlPushInput(ctxt, input); |
| 10393 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 10394 | xmlSwitchEncoding(ctxt, enc); |
| 10395 | |
| 10396 | if (input->filename == NULL) |
Daniel Veillard | 85095e2 | 2003-04-23 13:56:44 +0000 | [diff] [blame] | 10397 | input->filename = (char *) xmlCanonicPath(SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10398 | input->line = 1; |
| 10399 | input->col = 1; |
| 10400 | input->base = ctxt->input->cur; |
| 10401 | input->cur = ctxt->input->cur; |
| 10402 | input->free = NULL; |
| 10403 | |
| 10404 | /* |
| 10405 | * let's parse that entity knowing it's an external subset. |
| 10406 | */ |
| 10407 | ctxt->inSubset = 2; |
| 10408 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10409 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10410 | ExternalID, SystemID); |
| 10411 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 10412 | |
| 10413 | if (ctxt->myDoc != NULL) { |
| 10414 | if (ctxt->wellFormed) { |
| 10415 | ret = ctxt->myDoc->extSubset; |
| 10416 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 10417 | if (ret != NULL) { |
| 10418 | xmlNodePtr tmp; |
| 10419 | |
| 10420 | ret->doc = NULL; |
| 10421 | tmp = ret->children; |
| 10422 | while (tmp != NULL) { |
| 10423 | tmp->doc = NULL; |
| 10424 | tmp = tmp->next; |
| 10425 | } |
| 10426 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10427 | } else { |
| 10428 | ret = NULL; |
| 10429 | } |
| 10430 | xmlFreeDoc(ctxt->myDoc); |
| 10431 | ctxt->myDoc = NULL; |
| 10432 | } |
| 10433 | if (sax != NULL) ctxt->sax = NULL; |
| 10434 | xmlFreeParserCtxt(ctxt); |
| 10435 | |
| 10436 | return(ret); |
| 10437 | } |
| 10438 | |
| 10439 | /** |
| 10440 | * xmlParseDTD: |
| 10441 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10442 | * @SystemID: a NAME* containing the URL to the DTD |
| 10443 | * |
| 10444 | * Load and parse an external subset. |
| 10445 | * |
| 10446 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10447 | */ |
| 10448 | |
| 10449 | xmlDtdPtr |
| 10450 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 10451 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 10452 | } |
| 10453 | |
| 10454 | /************************************************************************ |
| 10455 | * * |
| 10456 | * Front ends when parsing an Entity * |
| 10457 | * * |
| 10458 | ************************************************************************/ |
| 10459 | |
| 10460 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10461 | * xmlParseCtxtExternalEntity: |
| 10462 | * @ctx: the existing parsing context |
| 10463 | * @URL: the URL for the entity to load |
| 10464 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10465 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10466 | * |
| 10467 | * Parse an external general entity within an existing parsing context |
| 10468 | * An external general parsed entity is well-formed if it matches the |
| 10469 | * production labeled extParsedEnt. |
| 10470 | * |
| 10471 | * [78] extParsedEnt ::= TextDecl? content |
| 10472 | * |
| 10473 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10474 | * the parser error code otherwise |
| 10475 | */ |
| 10476 | |
| 10477 | int |
| 10478 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10479 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10480 | xmlParserCtxtPtr ctxt; |
| 10481 | xmlDocPtr newDoc; |
| 10482 | xmlSAXHandlerPtr oldsax = NULL; |
| 10483 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10484 | xmlChar start[4]; |
| 10485 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10486 | |
| 10487 | if (ctx->depth > 40) { |
| 10488 | return(XML_ERR_ENTITY_LOOP); |
| 10489 | } |
| 10490 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10491 | if (lst != NULL) |
| 10492 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10493 | if ((URL == NULL) && (ID == NULL)) |
| 10494 | return(-1); |
| 10495 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 10496 | return(-1); |
| 10497 | |
| 10498 | |
| 10499 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
| 10500 | if (ctxt == NULL) return(-1); |
| 10501 | ctxt->userData = ctxt; |
Daniel Veillard | e2830f1 | 2003-01-08 17:47:49 +0000 | [diff] [blame] | 10502 | ctxt->_private = ctx->_private; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10503 | oldsax = ctxt->sax; |
| 10504 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10505 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10506 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10507 | if (newDoc == NULL) { |
| 10508 | xmlFreeParserCtxt(ctxt); |
| 10509 | return(-1); |
| 10510 | } |
| 10511 | if (ctx->myDoc != NULL) { |
| 10512 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 10513 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 10514 | } |
| 10515 | if (ctx->myDoc->URL != NULL) { |
| 10516 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 10517 | } |
| 10518 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10519 | if (newDoc->children == NULL) { |
| 10520 | ctxt->sax = oldsax; |
| 10521 | xmlFreeParserCtxt(ctxt); |
| 10522 | newDoc->intSubset = NULL; |
| 10523 | newDoc->extSubset = NULL; |
| 10524 | xmlFreeDoc(newDoc); |
| 10525 | return(-1); |
| 10526 | } |
| 10527 | nodePush(ctxt, newDoc->children); |
| 10528 | if (ctx->myDoc == NULL) { |
| 10529 | ctxt->myDoc = newDoc; |
| 10530 | } else { |
| 10531 | ctxt->myDoc = ctx->myDoc; |
| 10532 | newDoc->children->doc = ctx->myDoc; |
| 10533 | } |
| 10534 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10535 | /* |
| 10536 | * Get the 4 first bytes and decode the charset |
| 10537 | * if enc != XML_CHAR_ENCODING_NONE |
| 10538 | * plug some encoding conversion routines. |
| 10539 | */ |
| 10540 | GROW |
| 10541 | start[0] = RAW; |
| 10542 | start[1] = NXT(1); |
| 10543 | start[2] = NXT(2); |
| 10544 | start[3] = NXT(3); |
| 10545 | enc = xmlDetectCharEncoding(start, 4); |
| 10546 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10547 | xmlSwitchEncoding(ctxt, enc); |
| 10548 | } |
| 10549 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10550 | /* |
| 10551 | * Parse a possible text declaration first |
| 10552 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10553 | if ((RAW == '<') && (NXT(1) == '?') && |
| 10554 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 10555 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 10556 | xmlParseTextDecl(ctxt); |
| 10557 | } |
| 10558 | |
| 10559 | /* |
| 10560 | * Doing validity checking on chunk doesn't make sense |
| 10561 | */ |
| 10562 | ctxt->instate = XML_PARSER_CONTENT; |
| 10563 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10564 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10565 | ctxt->loadsubset = ctx->loadsubset; |
| 10566 | ctxt->depth = ctx->depth + 1; |
| 10567 | ctxt->replaceEntities = ctx->replaceEntities; |
| 10568 | if (ctxt->validate) { |
| 10569 | ctxt->vctxt.error = ctx->vctxt.error; |
| 10570 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10571 | } else { |
| 10572 | ctxt->vctxt.error = NULL; |
| 10573 | ctxt->vctxt.warning = NULL; |
| 10574 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 10575 | ctxt->vctxt.nodeTab = NULL; |
| 10576 | ctxt->vctxt.nodeNr = 0; |
| 10577 | ctxt->vctxt.nodeMax = 0; |
| 10578 | ctxt->vctxt.node = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10579 | |
| 10580 | xmlParseContent(ctxt); |
| 10581 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10582 | ctx->validate = ctxt->validate; |
| 10583 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10584 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10585 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10586 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10587 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10588 | } |
| 10589 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10590 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10591 | } |
| 10592 | |
| 10593 | if (!ctxt->wellFormed) { |
| 10594 | if (ctxt->errNo == 0) |
| 10595 | ret = 1; |
| 10596 | else |
| 10597 | ret = ctxt->errNo; |
| 10598 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10599 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10600 | xmlNodePtr cur; |
| 10601 | |
| 10602 | /* |
| 10603 | * Return the newly created nodeset after unlinking it from |
| 10604 | * they pseudo parent. |
| 10605 | */ |
| 10606 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10607 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10608 | while (cur != NULL) { |
| 10609 | cur->parent = NULL; |
| 10610 | cur = cur->next; |
| 10611 | } |
| 10612 | newDoc->children->children = NULL; |
| 10613 | } |
| 10614 | ret = 0; |
| 10615 | } |
| 10616 | ctxt->sax = oldsax; |
| 10617 | xmlFreeParserCtxt(ctxt); |
| 10618 | newDoc->intSubset = NULL; |
| 10619 | newDoc->extSubset = NULL; |
| 10620 | xmlFreeDoc(newDoc); |
| 10621 | |
| 10622 | return(ret); |
| 10623 | } |
| 10624 | |
| 10625 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10626 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10627 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10628 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10629 | * @sax: the SAX handler bloc (possibly NULL) |
| 10630 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10631 | * @depth: Used for loop detection, use 0 |
| 10632 | * @URL: the URL for the entity to load |
| 10633 | * @ID: the System ID for the entity to load |
| 10634 | * @list: the return value for the set of parsed nodes |
| 10635 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10636 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10637 | * |
| 10638 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10639 | * the parser error code otherwise |
| 10640 | */ |
| 10641 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10642 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10643 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 10644 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10645 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10646 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10647 | xmlParserCtxtPtr ctxt; |
| 10648 | xmlDocPtr newDoc; |
| 10649 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10650 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10651 | xmlChar start[4]; |
| 10652 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10653 | |
| 10654 | if (depth > 40) { |
| 10655 | return(XML_ERR_ENTITY_LOOP); |
| 10656 | } |
| 10657 | |
| 10658 | |
| 10659 | |
| 10660 | if (list != NULL) |
| 10661 | *list = NULL; |
| 10662 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10663 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10664 | if (doc == NULL) /* @@ relax but check for dereferences */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10665 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10666 | |
| 10667 | |
| 10668 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
| 10669 | if (ctxt == NULL) return(-1); |
| 10670 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10671 | if (oldctxt != NULL) { |
| 10672 | ctxt->_private = oldctxt->_private; |
| 10673 | ctxt->loadsubset = oldctxt->loadsubset; |
| 10674 | ctxt->validate = oldctxt->validate; |
| 10675 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10676 | ctxt->record_info = oldctxt->record_info; |
| 10677 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 10678 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 10679 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10680 | } else { |
| 10681 | /* |
| 10682 | * Doing validity checking on chunk without context |
| 10683 | * doesn't make sense |
| 10684 | */ |
| 10685 | ctxt->_private = NULL; |
| 10686 | ctxt->validate = 0; |
| 10687 | ctxt->external = 2; |
| 10688 | ctxt->loadsubset = 0; |
| 10689 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10690 | if (sax != NULL) { |
| 10691 | oldsax = ctxt->sax; |
| 10692 | ctxt->sax = sax; |
| 10693 | if (user_data != NULL) |
| 10694 | ctxt->userData = user_data; |
| 10695 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10696 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10697 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10698 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10699 | ctxt->node_seq.maximum = 0; |
| 10700 | ctxt->node_seq.length = 0; |
| 10701 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10702 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10703 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10704 | } |
| 10705 | if (doc != NULL) { |
| 10706 | newDoc->intSubset = doc->intSubset; |
| 10707 | newDoc->extSubset = doc->extSubset; |
| 10708 | } |
| 10709 | if (doc->URL != NULL) { |
| 10710 | newDoc->URL = xmlStrdup(doc->URL); |
| 10711 | } |
| 10712 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10713 | if (newDoc->children == NULL) { |
| 10714 | if (sax != NULL) |
| 10715 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10716 | ctxt->node_seq.maximum = 0; |
| 10717 | ctxt->node_seq.length = 0; |
| 10718 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10719 | xmlFreeParserCtxt(ctxt); |
| 10720 | newDoc->intSubset = NULL; |
| 10721 | newDoc->extSubset = NULL; |
| 10722 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10723 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10724 | } |
| 10725 | nodePush(ctxt, newDoc->children); |
| 10726 | if (doc == NULL) { |
| 10727 | ctxt->myDoc = newDoc; |
| 10728 | } else { |
| 10729 | ctxt->myDoc = doc; |
| 10730 | newDoc->children->doc = doc; |
| 10731 | } |
| 10732 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10733 | /* |
| 10734 | * Get the 4 first bytes and decode the charset |
| 10735 | * if enc != XML_CHAR_ENCODING_NONE |
| 10736 | * plug some encoding conversion routines. |
| 10737 | */ |
| 10738 | GROW; |
| 10739 | start[0] = RAW; |
| 10740 | start[1] = NXT(1); |
| 10741 | start[2] = NXT(2); |
| 10742 | start[3] = NXT(3); |
| 10743 | enc = xmlDetectCharEncoding(start, 4); |
| 10744 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10745 | xmlSwitchEncoding(ctxt, enc); |
| 10746 | } |
| 10747 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10748 | /* |
| 10749 | * Parse a possible text declaration first |
| 10750 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10751 | if ((RAW == '<') && (NXT(1) == '?') && |
| 10752 | (NXT(2) == 'x') && (NXT(3) == 'm') && |
| 10753 | (NXT(4) == 'l') && (IS_BLANK(NXT(5)))) { |
| 10754 | xmlParseTextDecl(ctxt); |
| 10755 | } |
| 10756 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10757 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10758 | ctxt->depth = depth; |
| 10759 | |
| 10760 | xmlParseContent(ctxt); |
| 10761 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10762 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10763 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10764 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10765 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10766 | } |
| 10767 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10768 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10769 | } |
| 10770 | |
| 10771 | if (!ctxt->wellFormed) { |
| 10772 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10773 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10774 | else |
| 10775 | ret = ctxt->errNo; |
| 10776 | } else { |
| 10777 | if (list != NULL) { |
| 10778 | xmlNodePtr cur; |
| 10779 | |
| 10780 | /* |
| 10781 | * Return the newly created nodeset after unlinking it from |
| 10782 | * they pseudo parent. |
| 10783 | */ |
| 10784 | cur = newDoc->children->children; |
| 10785 | *list = cur; |
| 10786 | while (cur != NULL) { |
| 10787 | cur->parent = NULL; |
| 10788 | cur = cur->next; |
| 10789 | } |
| 10790 | newDoc->children->children = NULL; |
| 10791 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10792 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10793 | } |
| 10794 | if (sax != NULL) |
| 10795 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 10796 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 10797 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 10798 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10799 | ctxt->node_seq.maximum = 0; |
| 10800 | ctxt->node_seq.length = 0; |
| 10801 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10802 | xmlFreeParserCtxt(ctxt); |
| 10803 | newDoc->intSubset = NULL; |
| 10804 | newDoc->extSubset = NULL; |
| 10805 | xmlFreeDoc(newDoc); |
| 10806 | |
| 10807 | return(ret); |
| 10808 | } |
| 10809 | |
| 10810 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10811 | * xmlParseExternalEntity: |
| 10812 | * @doc: the document the chunk pertains to |
| 10813 | * @sax: the SAX handler bloc (possibly NULL) |
| 10814 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10815 | * @depth: Used for loop detection, use 0 |
| 10816 | * @URL: the URL for the entity to load |
| 10817 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10818 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10819 | * |
| 10820 | * Parse an external general entity |
| 10821 | * An external general parsed entity is well-formed if it matches the |
| 10822 | * production labeled extParsedEnt. |
| 10823 | * |
| 10824 | * [78] extParsedEnt ::= TextDecl? content |
| 10825 | * |
| 10826 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10827 | * the parser error code otherwise |
| 10828 | */ |
| 10829 | |
| 10830 | int |
| 10831 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10832 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10833 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10834 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10835 | } |
| 10836 | |
| 10837 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 10838 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10839 | * @doc: the document the chunk pertains to |
| 10840 | * @sax: the SAX handler bloc (possibly NULL) |
| 10841 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10842 | * @depth: Used for loop detection, use 0 |
| 10843 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10844 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10845 | * |
| 10846 | * Parse a well-balanced chunk of an XML document |
| 10847 | * called by the parser |
| 10848 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 10849 | * the content production in the XML grammar: |
| 10850 | * |
| 10851 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 10852 | * |
| 10853 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 10854 | * the parser error code otherwise |
| 10855 | */ |
| 10856 | |
| 10857 | int |
| 10858 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10859 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 10860 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 10861 | depth, string, lst, 0 ); |
| 10862 | } |
| 10863 | |
| 10864 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10865 | * xmlParseBalancedChunkMemoryInternal: |
| 10866 | * @oldctxt: the existing parsing context |
| 10867 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 10868 | * @user_data: the user data field for the parser context |
| 10869 | * @lst: the return value for the set of parsed nodes |
| 10870 | * |
| 10871 | * |
| 10872 | * Parse a well-balanced chunk of an XML document |
| 10873 | * called by the parser |
| 10874 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 10875 | * the content production in the XML grammar: |
| 10876 | * |
| 10877 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 10878 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10879 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 10880 | * error code otherwise |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10881 | * |
| 10882 | * In case recover is set to 1, the nodelist will not be empty even if |
| 10883 | * the parsed chunk is not well balanced. |
| 10884 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10885 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10886 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 10887 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 10888 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10889 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10890 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10891 | xmlNodePtr content = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10892 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10893 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10894 | |
| 10895 | if (oldctxt->depth > 40) { |
| 10896 | return(XML_ERR_ENTITY_LOOP); |
| 10897 | } |
| 10898 | |
| 10899 | |
| 10900 | if (lst != NULL) |
| 10901 | *lst = NULL; |
| 10902 | if (string == NULL) |
| 10903 | return(-1); |
| 10904 | |
| 10905 | size = xmlStrlen(string); |
| 10906 | |
| 10907 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 10908 | if (ctxt == NULL) return(-1); |
| 10909 | if (user_data != NULL) |
| 10910 | ctxt->userData = user_data; |
| 10911 | else |
| 10912 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10913 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 10914 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10915 | |
| 10916 | oldsax = ctxt->sax; |
| 10917 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10918 | xmlDetectSAX2(ctxt); |
| 10919 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 10920 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10921 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10922 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10923 | if (newDoc == NULL) { |
| 10924 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10925 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10926 | xmlFreeParserCtxt(ctxt); |
| 10927 | return(-1); |
| 10928 | } |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10929 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10930 | } else { |
| 10931 | ctxt->myDoc = oldctxt->myDoc; |
| 10932 | content = ctxt->myDoc->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10933 | } |
Daniel Veillard | 9bc5310 | 2002-11-25 13:20:04 +0000 | [diff] [blame] | 10934 | ctxt->myDoc->children = xmlNewDocNode(ctxt->myDoc, NULL, |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10935 | BAD_CAST "pseudoroot", NULL); |
| 10936 | if (ctxt->myDoc->children == NULL) { |
| 10937 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10938 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10939 | xmlFreeParserCtxt(ctxt); |
| 10940 | if (newDoc != NULL) |
| 10941 | xmlFreeDoc(newDoc); |
| 10942 | return(-1); |
| 10943 | } |
| 10944 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10945 | ctxt->instate = XML_PARSER_CONTENT; |
| 10946 | ctxt->depth = oldctxt->depth + 1; |
| 10947 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10948 | ctxt->validate = 0; |
| 10949 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 10950 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 10951 | /* |
| 10952 | * ID/IDREF registration will be done in xmlValidateElement below |
| 10953 | */ |
| 10954 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 10955 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10956 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10957 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10958 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10959 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10960 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10961 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10962 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10963 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10964 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10965 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10966 | } |
| 10967 | |
| 10968 | if (!ctxt->wellFormed) { |
| 10969 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame^] | 10970 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10971 | else |
| 10972 | ret = ctxt->errNo; |
| 10973 | } else { |
| 10974 | ret = 0; |
| 10975 | } |
| 10976 | |
| 10977 | if ((lst != NULL) && (ret == 0)) { |
| 10978 | xmlNodePtr cur; |
| 10979 | |
| 10980 | /* |
| 10981 | * Return the newly created nodeset after unlinking it from |
| 10982 | * they pseudo parent. |
| 10983 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10984 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10985 | *lst = cur; |
| 10986 | while (cur != NULL) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 10987 | if (oldctxt->validate && oldctxt->wellFormed && |
| 10988 | oldctxt->myDoc && oldctxt->myDoc->intSubset) { |
| 10989 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 10990 | oldctxt->myDoc, cur); |
| 10991 | } |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10992 | cur->parent = NULL; |
| 10993 | cur = cur->next; |
| 10994 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10995 | ctxt->myDoc->children->children = NULL; |
| 10996 | } |
| 10997 | if (ctxt->myDoc != NULL) { |
| 10998 | xmlFreeNode(ctxt->myDoc->children); |
| 10999 | ctxt->myDoc->children = content; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11000 | } |
| 11001 | |
| 11002 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11003 | ctxt->dict = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11004 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11005 | if (newDoc != NULL) |
| 11006 | xmlFreeDoc(newDoc); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11007 | |
| 11008 | return(ret); |
| 11009 | } |
| 11010 | |
| 11011 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11012 | * xmlParseBalancedChunkMemoryRecover: |
| 11013 | * @doc: the document the chunk pertains to |
| 11014 | * @sax: the SAX handler bloc (possibly NULL) |
| 11015 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 11016 | * @depth: Used for loop detection, use 0 |
| 11017 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 11018 | * @lst: the return value for the set of parsed nodes |
| 11019 | * @recover: return nodes even if the data is broken (use 0) |
| 11020 | * |
| 11021 | * |
| 11022 | * Parse a well-balanced chunk of an XML document |
| 11023 | * called by the parser |
| 11024 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 11025 | * the content production in the XML grammar: |
| 11026 | * |
| 11027 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11028 | * |
| 11029 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 11030 | * the parser error code otherwise |
| 11031 | * |
| 11032 | * In case recover is set to 1, the nodelist will not be empty even if |
| 11033 | * the parsed chunk is not well balanced. |
| 11034 | */ |
| 11035 | int |
| 11036 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
| 11037 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
| 11038 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11039 | xmlParserCtxtPtr ctxt; |
| 11040 | xmlDocPtr newDoc; |
| 11041 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 935494a | 2002-10-22 14:22:46 +0000 | [diff] [blame] | 11042 | xmlNodePtr content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11043 | int size; |
| 11044 | int ret = 0; |
| 11045 | |
| 11046 | if (depth > 40) { |
| 11047 | return(XML_ERR_ENTITY_LOOP); |
| 11048 | } |
| 11049 | |
| 11050 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11051 | if (lst != NULL) |
| 11052 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11053 | if (string == NULL) |
| 11054 | return(-1); |
| 11055 | |
| 11056 | size = xmlStrlen(string); |
| 11057 | |
| 11058 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 11059 | if (ctxt == NULL) return(-1); |
| 11060 | ctxt->userData = ctxt; |
| 11061 | if (sax != NULL) { |
| 11062 | oldsax = ctxt->sax; |
| 11063 | ctxt->sax = sax; |
| 11064 | if (user_data != NULL) |
| 11065 | ctxt->userData = user_data; |
| 11066 | } |
| 11067 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 11068 | if (newDoc == NULL) { |
| 11069 | xmlFreeParserCtxt(ctxt); |
| 11070 | return(-1); |
| 11071 | } |
| 11072 | if (doc != NULL) { |
| 11073 | newDoc->intSubset = doc->intSubset; |
| 11074 | newDoc->extSubset = doc->extSubset; |
| 11075 | } |
| 11076 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 11077 | if (newDoc->children == NULL) { |
| 11078 | if (sax != NULL) |
| 11079 | ctxt->sax = oldsax; |
| 11080 | xmlFreeParserCtxt(ctxt); |
| 11081 | newDoc->intSubset = NULL; |
| 11082 | newDoc->extSubset = NULL; |
| 11083 | xmlFreeDoc(newDoc); |
| 11084 | return(-1); |
| 11085 | } |
| 11086 | nodePush(ctxt, newDoc->children); |
| 11087 | if (doc == NULL) { |
| 11088 | ctxt->myDoc = newDoc; |
| 11089 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11090 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11091 | newDoc->children->doc = doc; |
| 11092 | } |
| 11093 | ctxt->instate = XML_PARSER_CONTENT; |
| 11094 | ctxt->depth = depth; |
| 11095 | |
| 11096 | /* |
| 11097 | * Doing validity checking on chunk doesn't make sense |
| 11098 | */ |
| 11099 | ctxt->validate = 0; |
| 11100 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11101 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11102 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 11103 | if ( doc != NULL ){ |
| 11104 | content = doc->children; |
| 11105 | doc->children = NULL; |
| 11106 | xmlParseContent(ctxt); |
| 11107 | doc->children = content; |
| 11108 | } |
| 11109 | else { |
| 11110 | xmlParseContent(ctxt); |
| 11111 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11112 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11113 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11114 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11115 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11116 | } |
| 11117 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11118 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11119 | } |
| 11120 | |
| 11121 | if (!ctxt->wellFormed) { |
| 11122 | if (ctxt->errNo == 0) |
| 11123 | ret = 1; |
| 11124 | else |
| 11125 | ret = ctxt->errNo; |
| 11126 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11127 | ret = 0; |
| 11128 | } |
| 11129 | |
| 11130 | if (lst != NULL && (ret == 0 || recover == 1)) { |
| 11131 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11132 | |
| 11133 | /* |
| 11134 | * Return the newly created nodeset after unlinking it from |
| 11135 | * they pseudo parent. |
| 11136 | */ |
| 11137 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11138 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11139 | while (cur != NULL) { |
| 11140 | cur->parent = NULL; |
| 11141 | cur = cur->next; |
| 11142 | } |
| 11143 | newDoc->children->children = NULL; |
| 11144 | } |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11145 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11146 | if (sax != NULL) |
| 11147 | ctxt->sax = oldsax; |
| 11148 | xmlFreeParserCtxt(ctxt); |
| 11149 | newDoc->intSubset = NULL; |
| 11150 | newDoc->extSubset = NULL; |
| 11151 | xmlFreeDoc(newDoc); |
| 11152 | |
| 11153 | return(ret); |
| 11154 | } |
| 11155 | |
| 11156 | /** |
| 11157 | * xmlSAXParseEntity: |
| 11158 | * @sax: the SAX handler block |
| 11159 | * @filename: the filename |
| 11160 | * |
| 11161 | * parse an XML external entity out of context and build a tree. |
| 11162 | * It use the given SAX function block to handle the parsing callback. |
| 11163 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11164 | * |
| 11165 | * [78] extParsedEnt ::= TextDecl? content |
| 11166 | * |
| 11167 | * This correspond to a "Well Balanced" chunk |
| 11168 | * |
| 11169 | * Returns the resulting document tree |
| 11170 | */ |
| 11171 | |
| 11172 | xmlDocPtr |
| 11173 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 11174 | xmlDocPtr ret; |
| 11175 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11176 | |
| 11177 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11178 | if (ctxt == NULL) { |
| 11179 | return(NULL); |
| 11180 | } |
| 11181 | if (sax != NULL) { |
| 11182 | if (ctxt->sax != NULL) |
| 11183 | xmlFree(ctxt->sax); |
| 11184 | ctxt->sax = sax; |
| 11185 | ctxt->userData = NULL; |
| 11186 | } |
| 11187 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11188 | xmlParseExtParsedEnt(ctxt); |
| 11189 | |
| 11190 | if (ctxt->wellFormed) |
| 11191 | ret = ctxt->myDoc; |
| 11192 | else { |
| 11193 | ret = NULL; |
| 11194 | xmlFreeDoc(ctxt->myDoc); |
| 11195 | ctxt->myDoc = NULL; |
| 11196 | } |
| 11197 | if (sax != NULL) |
| 11198 | ctxt->sax = NULL; |
| 11199 | xmlFreeParserCtxt(ctxt); |
| 11200 | |
| 11201 | return(ret); |
| 11202 | } |
| 11203 | |
| 11204 | /** |
| 11205 | * xmlParseEntity: |
| 11206 | * @filename: the filename |
| 11207 | * |
| 11208 | * parse an XML external entity out of context and build a tree. |
| 11209 | * |
| 11210 | * [78] extParsedEnt ::= TextDecl? content |
| 11211 | * |
| 11212 | * This correspond to a "Well Balanced" chunk |
| 11213 | * |
| 11214 | * Returns the resulting document tree |
| 11215 | */ |
| 11216 | |
| 11217 | xmlDocPtr |
| 11218 | xmlParseEntity(const char *filename) { |
| 11219 | return(xmlSAXParseEntity(NULL, filename)); |
| 11220 | } |
| 11221 | |
| 11222 | /** |
| 11223 | * xmlCreateEntityParserCtxt: |
| 11224 | * @URL: the entity URL |
| 11225 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11226 | * @base: a possible base for the target URI |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11227 | * |
| 11228 | * Create a parser context for an external entity |
| 11229 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11230 | * by default if found at compile-time. |
| 11231 | * |
| 11232 | * Returns the new parser context or NULL |
| 11233 | */ |
| 11234 | xmlParserCtxtPtr |
| 11235 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 11236 | const xmlChar *base) { |
| 11237 | xmlParserCtxtPtr ctxt; |
| 11238 | xmlParserInputPtr inputStream; |
| 11239 | char *directory = NULL; |
| 11240 | xmlChar *uri; |
| 11241 | |
| 11242 | ctxt = xmlNewParserCtxt(); |
| 11243 | if (ctxt == NULL) { |
| 11244 | return(NULL); |
| 11245 | } |
| 11246 | |
| 11247 | uri = xmlBuildURI(URL, base); |
| 11248 | |
| 11249 | if (uri == NULL) { |
| 11250 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 11251 | if (inputStream == NULL) { |
| 11252 | xmlFreeParserCtxt(ctxt); |
| 11253 | return(NULL); |
| 11254 | } |
| 11255 | |
| 11256 | inputPush(ctxt, inputStream); |
| 11257 | |
| 11258 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11259 | directory = xmlParserGetDirectory((char *)URL); |
| 11260 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11261 | ctxt->directory = directory; |
| 11262 | } else { |
| 11263 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 11264 | if (inputStream == NULL) { |
| 11265 | xmlFree(uri); |
| 11266 | xmlFreeParserCtxt(ctxt); |
| 11267 | return(NULL); |
| 11268 | } |
| 11269 | |
| 11270 | inputPush(ctxt, inputStream); |
| 11271 | |
| 11272 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11273 | directory = xmlParserGetDirectory((char *)uri); |
| 11274 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11275 | ctxt->directory = directory; |
| 11276 | xmlFree(uri); |
| 11277 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11278 | return(ctxt); |
| 11279 | } |
| 11280 | |
| 11281 | /************************************************************************ |
| 11282 | * * |
| 11283 | * Front ends when parsing from a file * |
| 11284 | * * |
| 11285 | ************************************************************************/ |
| 11286 | |
| 11287 | /** |
| 11288 | * xmlCreateFileParserCtxt: |
| 11289 | * @filename: the filename |
| 11290 | * |
| 11291 | * Create a parser context for a file content. |
| 11292 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11293 | * by default if found at compile-time. |
| 11294 | * |
| 11295 | * Returns the new parser context or NULL |
| 11296 | */ |
| 11297 | xmlParserCtxtPtr |
| 11298 | xmlCreateFileParserCtxt(const char *filename) |
| 11299 | { |
| 11300 | xmlParserCtxtPtr ctxt; |
| 11301 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11302 | char *directory = NULL; |
| 11303 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11304 | ctxt = xmlNewParserCtxt(); |
| 11305 | if (ctxt == NULL) { |
| 11306 | if (xmlDefaultSAXHandler.error != NULL) { |
| 11307 | xmlDefaultSAXHandler.error(NULL, "out of memory\n"); |
| 11308 | } |
| 11309 | return(NULL); |
| 11310 | } |
| 11311 | |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 11312 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 11313 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11314 | if (inputStream == NULL) { |
| 11315 | xmlFreeParserCtxt(ctxt); |
| 11316 | return(NULL); |
| 11317 | } |
| 11318 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11319 | inputPush(ctxt, inputStream); |
| 11320 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11321 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11322 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11323 | ctxt->directory = directory; |
| 11324 | |
| 11325 | return(ctxt); |
| 11326 | } |
| 11327 | |
| 11328 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11329 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11330 | * @sax: the SAX handler block |
| 11331 | * @filename: the filename |
| 11332 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11333 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11334 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11335 | * |
| 11336 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11337 | * compressed document is provided by default if found at compile-time. |
| 11338 | * It use the given SAX function block to handle the parsing callback. |
| 11339 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11340 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 11341 | * User data (void *) is stored within the parser context in the |
| 11342 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11343 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11344 | * Returns the resulting document tree |
| 11345 | */ |
| 11346 | |
| 11347 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11348 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 11349 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11350 | xmlDocPtr ret; |
| 11351 | xmlParserCtxtPtr ctxt; |
| 11352 | char *directory = NULL; |
| 11353 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 11354 | xmlInitParser(); |
| 11355 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11356 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11357 | if (ctxt == NULL) { |
| 11358 | return(NULL); |
| 11359 | } |
| 11360 | if (sax != NULL) { |
| 11361 | if (ctxt->sax != NULL) |
| 11362 | xmlFree(ctxt->sax); |
| 11363 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11364 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11365 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11366 | if (data!=NULL) { |
| 11367 | ctxt->_private=data; |
| 11368 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11369 | |
| 11370 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11371 | directory = xmlParserGetDirectory(filename); |
| 11372 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11373 | ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 11374 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 11375 | ctxt->recovery = recovery; |
| 11376 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11377 | xmlParseDocument(ctxt); |
| 11378 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 11379 | if ((ctxt->wellFormed) || recovery) { |
| 11380 | ret = ctxt->myDoc; |
| 11381 | if (ctxt->input->buf->compressed > 0) |
| 11382 | ret->compression = 9; |
| 11383 | else |
| 11384 | ret->compression = ctxt->input->buf->compressed; |
| 11385 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11386 | else { |
| 11387 | ret = NULL; |
| 11388 | xmlFreeDoc(ctxt->myDoc); |
| 11389 | ctxt->myDoc = NULL; |
| 11390 | } |
| 11391 | if (sax != NULL) |
| 11392 | ctxt->sax = NULL; |
| 11393 | xmlFreeParserCtxt(ctxt); |
| 11394 | |
| 11395 | return(ret); |
| 11396 | } |
| 11397 | |
| 11398 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11399 | * xmlSAXParseFile: |
| 11400 | * @sax: the SAX handler block |
| 11401 | * @filename: the filename |
| 11402 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11403 | * documents |
| 11404 | * |
| 11405 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11406 | * compressed document is provided by default if found at compile-time. |
| 11407 | * It use the given SAX function block to handle the parsing callback. |
| 11408 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11409 | * |
| 11410 | * Returns the resulting document tree |
| 11411 | */ |
| 11412 | |
| 11413 | xmlDocPtr |
| 11414 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 11415 | int recovery) { |
| 11416 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 11417 | } |
| 11418 | |
| 11419 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11420 | * xmlRecoverDoc: |
| 11421 | * @cur: a pointer to an array of xmlChar |
| 11422 | * |
| 11423 | * parse an XML in-memory document and build a tree. |
| 11424 | * In the case the document is not Well Formed, a tree is built anyway |
| 11425 | * |
| 11426 | * Returns the resulting document tree |
| 11427 | */ |
| 11428 | |
| 11429 | xmlDocPtr |
| 11430 | xmlRecoverDoc(xmlChar *cur) { |
| 11431 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 11432 | } |
| 11433 | |
| 11434 | /** |
| 11435 | * xmlParseFile: |
| 11436 | * @filename: the filename |
| 11437 | * |
| 11438 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11439 | * compressed document is provided by default if found at compile-time. |
| 11440 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 11441 | * Returns the resulting document tree if the file was wellformed, |
| 11442 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11443 | */ |
| 11444 | |
| 11445 | xmlDocPtr |
| 11446 | xmlParseFile(const char *filename) { |
| 11447 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 11448 | } |
| 11449 | |
| 11450 | /** |
| 11451 | * xmlRecoverFile: |
| 11452 | * @filename: the filename |
| 11453 | * |
| 11454 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11455 | * compressed document is provided by default if found at compile-time. |
| 11456 | * In the case the document is not Well Formed, a tree is built anyway |
| 11457 | * |
| 11458 | * Returns the resulting document tree |
| 11459 | */ |
| 11460 | |
| 11461 | xmlDocPtr |
| 11462 | xmlRecoverFile(const char *filename) { |
| 11463 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 11464 | } |
| 11465 | |
| 11466 | |
| 11467 | /** |
| 11468 | * xmlSetupParserForBuffer: |
| 11469 | * @ctxt: an XML parser context |
| 11470 | * @buffer: a xmlChar * buffer |
| 11471 | * @filename: a file name |
| 11472 | * |
| 11473 | * Setup the parser context to parse a new buffer; Clears any prior |
| 11474 | * contents from the parser context. The buffer parameter must not be |
| 11475 | * NULL, but the filename parameter can be |
| 11476 | */ |
| 11477 | void |
| 11478 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 11479 | const char* filename) |
| 11480 | { |
| 11481 | xmlParserInputPtr input; |
| 11482 | |
| 11483 | input = xmlNewInputStream(ctxt); |
| 11484 | if (input == NULL) { |
Daniel Veillard | 3487c8d | 2002-09-05 11:33:25 +0000 | [diff] [blame] | 11485 | xmlGenericError(xmlGenericErrorContext, |
| 11486 | "malloc"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11487 | xmlFree(ctxt); |
| 11488 | return; |
| 11489 | } |
| 11490 | |
| 11491 | xmlClearParserCtxt(ctxt); |
| 11492 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 11493 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11494 | input->base = buffer; |
| 11495 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11496 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11497 | inputPush(ctxt, input); |
| 11498 | } |
| 11499 | |
| 11500 | /** |
| 11501 | * xmlSAXUserParseFile: |
| 11502 | * @sax: a SAX handler |
| 11503 | * @user_data: The user data returned on SAX callbacks |
| 11504 | * @filename: a file name |
| 11505 | * |
| 11506 | * parse an XML file and call the given SAX handler routines. |
| 11507 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11508 | * |
| 11509 | * Returns 0 in case of success or a error number otherwise |
| 11510 | */ |
| 11511 | int |
| 11512 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 11513 | const char *filename) { |
| 11514 | int ret = 0; |
| 11515 | xmlParserCtxtPtr ctxt; |
| 11516 | |
| 11517 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11518 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11519 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11520 | xmlFree(ctxt->sax); |
| 11521 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11522 | xmlDetectSAX2(ctxt); |
| 11523 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11524 | if (user_data != NULL) |
| 11525 | ctxt->userData = user_data; |
| 11526 | |
| 11527 | xmlParseDocument(ctxt); |
| 11528 | |
| 11529 | if (ctxt->wellFormed) |
| 11530 | ret = 0; |
| 11531 | else { |
| 11532 | if (ctxt->errNo != 0) |
| 11533 | ret = ctxt->errNo; |
| 11534 | else |
| 11535 | ret = -1; |
| 11536 | } |
| 11537 | if (sax != NULL) |
| 11538 | ctxt->sax = NULL; |
| 11539 | xmlFreeParserCtxt(ctxt); |
| 11540 | |
| 11541 | return ret; |
| 11542 | } |
| 11543 | |
| 11544 | /************************************************************************ |
| 11545 | * * |
| 11546 | * Front ends when parsing from memory * |
| 11547 | * * |
| 11548 | ************************************************************************/ |
| 11549 | |
| 11550 | /** |
| 11551 | * xmlCreateMemoryParserCtxt: |
| 11552 | * @buffer: a pointer to a char array |
| 11553 | * @size: the size of the array |
| 11554 | * |
| 11555 | * Create a parser context for an XML in-memory document. |
| 11556 | * |
| 11557 | * Returns the new parser context or NULL |
| 11558 | */ |
| 11559 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 11560 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11561 | xmlParserCtxtPtr ctxt; |
| 11562 | xmlParserInputPtr input; |
| 11563 | xmlParserInputBufferPtr buf; |
| 11564 | |
| 11565 | if (buffer == NULL) |
| 11566 | return(NULL); |
| 11567 | if (size <= 0) |
| 11568 | return(NULL); |
| 11569 | |
| 11570 | ctxt = xmlNewParserCtxt(); |
| 11571 | if (ctxt == NULL) |
| 11572 | return(NULL); |
| 11573 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 11574 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11575 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 11576 | if (buf == NULL) { |
| 11577 | xmlFreeParserCtxt(ctxt); |
| 11578 | return(NULL); |
| 11579 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11580 | |
| 11581 | input = xmlNewInputStream(ctxt); |
| 11582 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 11583 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11584 | xmlFreeParserCtxt(ctxt); |
| 11585 | return(NULL); |
| 11586 | } |
| 11587 | |
| 11588 | input->filename = NULL; |
| 11589 | input->buf = buf; |
| 11590 | input->base = input->buf->buffer->content; |
| 11591 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11592 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11593 | |
| 11594 | inputPush(ctxt, input); |
| 11595 | return(ctxt); |
| 11596 | } |
| 11597 | |
| 11598 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11599 | * xmlSAXParseMemoryWithData: |
| 11600 | * @sax: the SAX handler block |
| 11601 | * @buffer: an pointer to a char array |
| 11602 | * @size: the size of the array |
| 11603 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11604 | * documents |
| 11605 | * @data: the userdata |
| 11606 | * |
| 11607 | * parse an XML in-memory block and use the given SAX function block |
| 11608 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 11609 | * DOM tree building routines. |
| 11610 | * |
| 11611 | * User data (void *) is stored within the parser context in the |
| 11612 | * context's _private member, so it is available nearly everywhere in libxml |
| 11613 | * |
| 11614 | * Returns the resulting document tree |
| 11615 | */ |
| 11616 | |
| 11617 | xmlDocPtr |
| 11618 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 11619 | int size, int recovery, void *data) { |
| 11620 | xmlDocPtr ret; |
| 11621 | xmlParserCtxtPtr ctxt; |
| 11622 | |
| 11623 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 11624 | if (ctxt == NULL) return(NULL); |
| 11625 | if (sax != NULL) { |
| 11626 | if (ctxt->sax != NULL) |
| 11627 | xmlFree(ctxt->sax); |
| 11628 | ctxt->sax = sax; |
| 11629 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11630 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11631 | if (data!=NULL) { |
| 11632 | ctxt->_private=data; |
| 11633 | } |
| 11634 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 11635 | ctxt->recovery = recovery; |
| 11636 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11637 | xmlParseDocument(ctxt); |
| 11638 | |
| 11639 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 11640 | else { |
| 11641 | ret = NULL; |
| 11642 | xmlFreeDoc(ctxt->myDoc); |
| 11643 | ctxt->myDoc = NULL; |
| 11644 | } |
| 11645 | if (sax != NULL) |
| 11646 | ctxt->sax = NULL; |
| 11647 | xmlFreeParserCtxt(ctxt); |
| 11648 | |
| 11649 | return(ret); |
| 11650 | } |
| 11651 | |
| 11652 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11653 | * xmlSAXParseMemory: |
| 11654 | * @sax: the SAX handler block |
| 11655 | * @buffer: an pointer to a char array |
| 11656 | * @size: the size of the array |
| 11657 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 11658 | * documents |
| 11659 | * |
| 11660 | * parse an XML in-memory block and use the given SAX function block |
| 11661 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 11662 | * DOM tree building routines. |
| 11663 | * |
| 11664 | * Returns the resulting document tree |
| 11665 | */ |
| 11666 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11667 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 11668 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11669 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11670 | } |
| 11671 | |
| 11672 | /** |
| 11673 | * xmlParseMemory: |
| 11674 | * @buffer: an pointer to a char array |
| 11675 | * @size: the size of the array |
| 11676 | * |
| 11677 | * parse an XML in-memory block and build a tree. |
| 11678 | * |
| 11679 | * Returns the resulting document tree |
| 11680 | */ |
| 11681 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11682 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11683 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 11684 | } |
| 11685 | |
| 11686 | /** |
| 11687 | * xmlRecoverMemory: |
| 11688 | * @buffer: an pointer to a char array |
| 11689 | * @size: the size of the array |
| 11690 | * |
| 11691 | * parse an XML in-memory block and build a tree. |
| 11692 | * In the case the document is not Well Formed, a tree is built anyway |
| 11693 | * |
| 11694 | * Returns the resulting document tree |
| 11695 | */ |
| 11696 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11697 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11698 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 11699 | } |
| 11700 | |
| 11701 | /** |
| 11702 | * xmlSAXUserParseMemory: |
| 11703 | * @sax: a SAX handler |
| 11704 | * @user_data: The user data returned on SAX callbacks |
| 11705 | * @buffer: an in-memory XML document input |
| 11706 | * @size: the length of the XML document in bytes |
| 11707 | * |
| 11708 | * A better SAX parsing routine. |
| 11709 | * parse an XML in-memory buffer and call the given SAX handler routines. |
| 11710 | * |
| 11711 | * Returns 0 in case of success or a error number otherwise |
| 11712 | */ |
| 11713 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 11714 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11715 | int ret = 0; |
| 11716 | xmlParserCtxtPtr ctxt; |
| 11717 | xmlSAXHandlerPtr oldsax = NULL; |
| 11718 | |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11719 | if (sax == NULL) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11720 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 11721 | if (ctxt == NULL) return -1; |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11722 | oldsax = ctxt->sax; |
| 11723 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11724 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 11725 | if (user_data != NULL) |
| 11726 | ctxt->userData = user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11727 | |
| 11728 | xmlParseDocument(ctxt); |
| 11729 | |
| 11730 | if (ctxt->wellFormed) |
| 11731 | ret = 0; |
| 11732 | else { |
| 11733 | if (ctxt->errNo != 0) |
| 11734 | ret = ctxt->errNo; |
| 11735 | else |
| 11736 | ret = -1; |
| 11737 | } |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11738 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11739 | xmlFreeParserCtxt(ctxt); |
| 11740 | |
| 11741 | return ret; |
| 11742 | } |
| 11743 | |
| 11744 | /** |
| 11745 | * xmlCreateDocParserCtxt: |
| 11746 | * @cur: a pointer to an array of xmlChar |
| 11747 | * |
| 11748 | * Creates a parser context for an XML in-memory document. |
| 11749 | * |
| 11750 | * Returns the new parser context or NULL |
| 11751 | */ |
| 11752 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11753 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11754 | int len; |
| 11755 | |
| 11756 | if (cur == NULL) |
| 11757 | return(NULL); |
| 11758 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11759 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11760 | } |
| 11761 | |
| 11762 | /** |
| 11763 | * xmlSAXParseDoc: |
| 11764 | * @sax: the SAX handler block |
| 11765 | * @cur: a pointer to an array of xmlChar |
| 11766 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11767 | * documents |
| 11768 | * |
| 11769 | * parse an XML in-memory document and build a tree. |
| 11770 | * It use the given SAX function block to handle the parsing callback. |
| 11771 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11772 | * |
| 11773 | * Returns the resulting document tree |
| 11774 | */ |
| 11775 | |
| 11776 | xmlDocPtr |
| 11777 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) { |
| 11778 | xmlDocPtr ret; |
| 11779 | xmlParserCtxtPtr ctxt; |
| 11780 | |
| 11781 | if (cur == NULL) return(NULL); |
| 11782 | |
| 11783 | |
| 11784 | ctxt = xmlCreateDocParserCtxt(cur); |
| 11785 | if (ctxt == NULL) return(NULL); |
| 11786 | if (sax != NULL) { |
| 11787 | ctxt->sax = sax; |
| 11788 | ctxt->userData = NULL; |
| 11789 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11790 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11791 | |
| 11792 | xmlParseDocument(ctxt); |
| 11793 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 11794 | else { |
| 11795 | ret = NULL; |
| 11796 | xmlFreeDoc(ctxt->myDoc); |
| 11797 | ctxt->myDoc = NULL; |
| 11798 | } |
| 11799 | if (sax != NULL) |
| 11800 | ctxt->sax = NULL; |
| 11801 | xmlFreeParserCtxt(ctxt); |
| 11802 | |
| 11803 | return(ret); |
| 11804 | } |
| 11805 | |
| 11806 | /** |
| 11807 | * xmlParseDoc: |
| 11808 | * @cur: a pointer to an array of xmlChar |
| 11809 | * |
| 11810 | * parse an XML in-memory document and build a tree. |
| 11811 | * |
| 11812 | * Returns the resulting document tree |
| 11813 | */ |
| 11814 | |
| 11815 | xmlDocPtr |
| 11816 | xmlParseDoc(xmlChar *cur) { |
| 11817 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 11818 | } |
| 11819 | |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 11820 | /************************************************************************ |
| 11821 | * * |
| 11822 | * Specific function to keep track of entities references * |
| 11823 | * and used by the XSLT debugger * |
| 11824 | * * |
| 11825 | ************************************************************************/ |
| 11826 | |
| 11827 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 11828 | |
| 11829 | /** |
| 11830 | * xmlAddEntityReference: |
| 11831 | * @ent : A valid entity |
| 11832 | * @firstNode : A valid first node for children of entity |
| 11833 | * @lastNode : A valid last node of children entity |
| 11834 | * |
| 11835 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 11836 | */ |
| 11837 | static void |
| 11838 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 11839 | xmlNodePtr lastNode) |
| 11840 | { |
| 11841 | if (xmlEntityRefFunc != NULL) { |
| 11842 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 11843 | } |
| 11844 | } |
| 11845 | |
| 11846 | |
| 11847 | /** |
| 11848 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 11849 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 11850 | * |
| 11851 | * Set the function to call call back when a xml reference has been made |
| 11852 | */ |
| 11853 | void |
| 11854 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 11855 | { |
| 11856 | xmlEntityRefFunc = func; |
| 11857 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11858 | |
| 11859 | /************************************************************************ |
| 11860 | * * |
| 11861 | * Miscellaneous * |
| 11862 | * * |
| 11863 | ************************************************************************/ |
| 11864 | |
| 11865 | #ifdef LIBXML_XPATH_ENABLED |
| 11866 | #include <libxml/xpath.h> |
| 11867 | #endif |
| 11868 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 11869 | extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11870 | static int xmlParserInitialized = 0; |
| 11871 | |
| 11872 | /** |
| 11873 | * xmlInitParser: |
| 11874 | * |
| 11875 | * Initialization function for the XML parser. |
| 11876 | * This is not reentrant. Call once before processing in case of |
| 11877 | * use in multithreaded programs. |
| 11878 | */ |
| 11879 | |
| 11880 | void |
| 11881 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 11882 | if (xmlParserInitialized != 0) |
| 11883 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11884 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 11885 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 11886 | (xmlGenericError == NULL)) |
| 11887 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 11888 | xmlInitGlobals(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 11889 | xmlInitThreads(); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 11890 | xmlInitMemory(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11891 | xmlInitCharEncodingHandlers(); |
| 11892 | xmlInitializePredefinedEntities(); |
| 11893 | xmlDefaultSAXHandlerInit(); |
| 11894 | xmlRegisterDefaultInputCallbacks(); |
| 11895 | xmlRegisterDefaultOutputCallbacks(); |
| 11896 | #ifdef LIBXML_HTML_ENABLED |
| 11897 | htmlInitAutoClose(); |
| 11898 | htmlDefaultSAXHandlerInit(); |
| 11899 | #endif |
| 11900 | #ifdef LIBXML_XPATH_ENABLED |
| 11901 | xmlXPathInit(); |
| 11902 | #endif |
| 11903 | xmlParserInitialized = 1; |
| 11904 | } |
| 11905 | |
| 11906 | /** |
| 11907 | * xmlCleanupParser: |
| 11908 | * |
| 11909 | * Cleanup function for the XML parser. It tries to reclaim all |
| 11910 | * parsing related global memory allocated for the parser processing. |
| 11911 | * It doesn't deallocate any document related memory. Calling this |
| 11912 | * function should not prevent reusing the parser. |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 11913 | * One should call xmlCleanupParser() only when the process has |
| 11914 | * finished using the library or XML document built with it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11915 | */ |
| 11916 | |
| 11917 | void |
| 11918 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 11919 | if (!xmlParserInitialized) |
| 11920 | return; |
| 11921 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11922 | xmlCleanupCharEncodingHandlers(); |
| 11923 | xmlCleanupPredefinedEntities(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 11924 | #ifdef LIBXML_CATALOG_ENABLED |
| 11925 | xmlCatalogCleanup(); |
| 11926 | #endif |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 11927 | xmlCleanupThreads(); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 11928 | xmlCleanupGlobals(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 11929 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11930 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11931 | |
| 11932 | /************************************************************************ |
| 11933 | * * |
| 11934 | * New set (2.6.0) of simpler and more flexible APIs * |
| 11935 | * * |
| 11936 | ************************************************************************/ |
| 11937 | |
| 11938 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11939 | * DICT_FREE: |
| 11940 | * @str: a string |
| 11941 | * |
| 11942 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 11943 | * current scope |
| 11944 | */ |
| 11945 | #define DICT_FREE(str) \ |
| 11946 | if ((str) && ((!dict) || \ |
| 11947 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 11948 | xmlFree((char *)(str)); |
| 11949 | |
| 11950 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11951 | * xmlCtxtReset: |
| 11952 | * @ctxt: an XML parser context |
| 11953 | * |
| 11954 | * Reset a parser context |
| 11955 | */ |
| 11956 | void |
| 11957 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 11958 | { |
| 11959 | xmlParserInputPtr input; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11960 | xmlDictPtr dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11961 | |
| 11962 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 11963 | xmlFreeInputStream(input); |
| 11964 | } |
| 11965 | ctxt->inputNr = 0; |
| 11966 | ctxt->input = NULL; |
| 11967 | |
| 11968 | ctxt->spaceNr = 0; |
| 11969 | ctxt->spaceTab[0] = -1; |
| 11970 | ctxt->space = &ctxt->spaceTab[0]; |
| 11971 | |
| 11972 | |
| 11973 | ctxt->nodeNr = 0; |
| 11974 | ctxt->node = NULL; |
| 11975 | |
| 11976 | ctxt->nameNr = 0; |
| 11977 | ctxt->name = NULL; |
| 11978 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11979 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11980 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11981 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11982 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11983 | DICT_FREE(ctxt->directory); |
| 11984 | ctxt->directory = NULL; |
| 11985 | DICT_FREE(ctxt->extSubURI); |
| 11986 | ctxt->extSubURI = NULL; |
| 11987 | DICT_FREE(ctxt->extSubSystem); |
| 11988 | ctxt->extSubSystem = NULL; |
| 11989 | if (ctxt->myDoc != NULL) |
| 11990 | xmlFreeDoc(ctxt->myDoc); |
| 11991 | ctxt->myDoc = NULL; |
| 11992 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11993 | ctxt->standalone = -1; |
| 11994 | ctxt->hasExternalSubset = 0; |
| 11995 | ctxt->hasPErefs = 0; |
| 11996 | ctxt->html = 0; |
| 11997 | ctxt->external = 0; |
| 11998 | ctxt->instate = XML_PARSER_START; |
| 11999 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12000 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12001 | ctxt->wellFormed = 1; |
| 12002 | ctxt->nsWellFormed = 1; |
| 12003 | ctxt->valid = 1; |
| 12004 | ctxt->vctxt.userData = ctxt; |
| 12005 | ctxt->vctxt.error = xmlParserValidityError; |
| 12006 | ctxt->vctxt.warning = xmlParserValidityWarning; |
| 12007 | ctxt->record_info = 0; |
| 12008 | ctxt->nbChars = 0; |
| 12009 | ctxt->checkIndex = 0; |
| 12010 | ctxt->inSubset = 0; |
| 12011 | ctxt->errNo = XML_ERR_OK; |
| 12012 | ctxt->depth = 0; |
| 12013 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 12014 | ctxt->catalogs = NULL; |
| 12015 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 12016 | |
| 12017 | if (ctxt->attsDefault != NULL) { |
| 12018 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 12019 | ctxt->attsDefault = NULL; |
| 12020 | } |
| 12021 | if (ctxt->attsSpecial != NULL) { |
| 12022 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 12023 | ctxt->attsSpecial = NULL; |
| 12024 | } |
| 12025 | |
| 12026 | if (ctxt->catalogs != NULL) |
| 12027 | xmlCatalogFreeLocal(ctxt->catalogs); |
| 12028 | } |
| 12029 | |
| 12030 | /** |
| 12031 | * xmlCtxtUseOptions: |
| 12032 | * @ctxt: an XML parser context |
| 12033 | * @options: a combination of xmlParserOption(s) |
| 12034 | * |
| 12035 | * Applies the options to the parser context |
| 12036 | * |
| 12037 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 12038 | * in case of error. |
| 12039 | */ |
| 12040 | int |
| 12041 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 12042 | { |
| 12043 | if (options & XML_PARSE_RECOVER) { |
| 12044 | ctxt->recovery = 1; |
| 12045 | options -= XML_PARSE_RECOVER; |
| 12046 | } else |
| 12047 | ctxt->recovery = 0; |
| 12048 | if (options & XML_PARSE_DTDLOAD) { |
| 12049 | ctxt->loadsubset = XML_DETECT_IDS; |
| 12050 | options -= XML_PARSE_DTDLOAD; |
| 12051 | } else |
| 12052 | ctxt->loadsubset = 0; |
| 12053 | if (options & XML_PARSE_DTDATTR) { |
| 12054 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 12055 | options -= XML_PARSE_DTDATTR; |
| 12056 | } |
| 12057 | if (options & XML_PARSE_NOENT) { |
| 12058 | ctxt->replaceEntities = 1; |
| 12059 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 12060 | options -= XML_PARSE_NOENT; |
| 12061 | } else |
| 12062 | ctxt->replaceEntities = 0; |
| 12063 | if (options & XML_PARSE_NOWARNING) { |
| 12064 | ctxt->sax->warning = NULL; |
| 12065 | options -= XML_PARSE_NOWARNING; |
| 12066 | } |
| 12067 | if (options & XML_PARSE_NOERROR) { |
| 12068 | ctxt->sax->error = NULL; |
| 12069 | ctxt->sax->fatalError = NULL; |
| 12070 | options -= XML_PARSE_NOERROR; |
| 12071 | } |
| 12072 | if (options & XML_PARSE_PEDANTIC) { |
| 12073 | ctxt->pedantic = 1; |
| 12074 | options -= XML_PARSE_PEDANTIC; |
| 12075 | } else |
| 12076 | ctxt->pedantic = 0; |
| 12077 | if (options & XML_PARSE_NOBLANKS) { |
| 12078 | ctxt->keepBlanks = 0; |
| 12079 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 12080 | options -= XML_PARSE_NOBLANKS; |
| 12081 | } else |
| 12082 | ctxt->keepBlanks = 1; |
| 12083 | if (options & XML_PARSE_DTDVALID) { |
| 12084 | ctxt->validate = 1; |
| 12085 | if (options & XML_PARSE_NOWARNING) |
| 12086 | ctxt->vctxt.warning = NULL; |
| 12087 | if (options & XML_PARSE_NOERROR) |
| 12088 | ctxt->vctxt.error = NULL; |
| 12089 | options -= XML_PARSE_DTDVALID; |
| 12090 | } else |
| 12091 | ctxt->validate = 0; |
| 12092 | if (options & XML_PARSE_SAX1) { |
| 12093 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 12094 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 12095 | ctxt->sax->startElementNs = NULL; |
| 12096 | ctxt->sax->endElementNs = NULL; |
| 12097 | ctxt->sax->initialized = 1; |
| 12098 | options -= XML_PARSE_SAX1; |
| 12099 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12100 | if (options & XML_PARSE_NODICT) { |
| 12101 | ctxt->dictNames = 0; |
| 12102 | options -= XML_PARSE_NODICT; |
| 12103 | } else { |
| 12104 | ctxt->dictNames = 1; |
| 12105 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 12106 | if (options & XML_PARSE_NOCDATA) { |
| 12107 | ctxt->sax->cdataBlock = NULL; |
| 12108 | options -= XML_PARSE_NOCDATA; |
| 12109 | } |
| 12110 | if (options & XML_PARSE_NSCLEAN) { |
| 12111 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 12112 | options -= XML_PARSE_NSCLEAN; |
| 12113 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12114 | return (options); |
| 12115 | } |
| 12116 | |
| 12117 | /** |
| 12118 | * xmlDoRead: |
| 12119 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12120 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12121 | * @encoding: the document encoding, or NULL |
| 12122 | * @options: a combination of xmlParserOption(s) |
| 12123 | * @reuse: keep the context for reuse |
| 12124 | * |
| 12125 | * Common front-end for the xmlRead functions |
| 12126 | * |
| 12127 | * Returns the resulting document tree or NULL |
| 12128 | */ |
| 12129 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12130 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 12131 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12132 | { |
| 12133 | xmlDocPtr ret; |
| 12134 | |
| 12135 | xmlCtxtUseOptions(ctxt, options); |
| 12136 | if (encoding != NULL) { |
| 12137 | xmlCharEncodingHandlerPtr hdlr; |
| 12138 | |
| 12139 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 12140 | if (hdlr != NULL) |
| 12141 | xmlSwitchToEncoding(ctxt, hdlr); |
| 12142 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12143 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 12144 | (ctxt->input->filename == NULL)) |
| 12145 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12146 | xmlParseDocument(ctxt); |
| 12147 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 12148 | ret = ctxt->myDoc; |
| 12149 | else { |
| 12150 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12151 | if (ctxt->myDoc != NULL) { |
| 12152 | ctxt->myDoc->dict = NULL; |
| 12153 | xmlFreeDoc(ctxt->myDoc); |
| 12154 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12155 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12156 | ctxt->myDoc = NULL; |
| 12157 | if (!reuse) { |
| 12158 | if ((ctxt->dictNames) && |
| 12159 | (ret != NULL) && |
| 12160 | (ret->dict == ctxt->dict)) |
| 12161 | ctxt->dict = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12162 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12163 | } else { |
| 12164 | /* Must duplicate the reference to the dictionary */ |
| 12165 | if ((ctxt->dictNames) && |
| 12166 | (ret != NULL) && |
| 12167 | (ret->dict == ctxt->dict)) |
| 12168 | xmlDictReference(ctxt->dict); |
| 12169 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12170 | |
| 12171 | return (ret); |
| 12172 | } |
| 12173 | |
| 12174 | /** |
| 12175 | * xmlReadDoc: |
| 12176 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12177 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12178 | * @encoding: the document encoding, or NULL |
| 12179 | * @options: a combination of xmlParserOption(s) |
| 12180 | * |
| 12181 | * parse an XML in-memory document and build a tree. |
| 12182 | * |
| 12183 | * Returns the resulting document tree |
| 12184 | */ |
| 12185 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12186 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12187 | { |
| 12188 | xmlParserCtxtPtr ctxt; |
| 12189 | |
| 12190 | if (cur == NULL) |
| 12191 | return (NULL); |
| 12192 | |
| 12193 | ctxt = xmlCreateDocParserCtxt(cur); |
| 12194 | if (ctxt == NULL) |
| 12195 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12196 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12197 | } |
| 12198 | |
| 12199 | /** |
| 12200 | * xmlReadFile: |
| 12201 | * @filename: a file or URL |
| 12202 | * @encoding: the document encoding, or NULL |
| 12203 | * @options: a combination of xmlParserOption(s) |
| 12204 | * |
| 12205 | * parse an XML file from the filesystem or the network. |
| 12206 | * |
| 12207 | * Returns the resulting document tree |
| 12208 | */ |
| 12209 | xmlDocPtr |
| 12210 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 12211 | { |
| 12212 | xmlParserCtxtPtr ctxt; |
| 12213 | |
| 12214 | ctxt = xmlCreateFileParserCtxt(filename); |
| 12215 | if (ctxt == NULL) |
| 12216 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12217 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12218 | } |
| 12219 | |
| 12220 | /** |
| 12221 | * xmlReadMemory: |
| 12222 | * @buffer: a pointer to a char array |
| 12223 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12224 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12225 | * @encoding: the document encoding, or NULL |
| 12226 | * @options: a combination of xmlParserOption(s) |
| 12227 | * |
| 12228 | * parse an XML in-memory document and build a tree. |
| 12229 | * |
| 12230 | * Returns the resulting document tree |
| 12231 | */ |
| 12232 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12233 | xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12234 | { |
| 12235 | xmlParserCtxtPtr ctxt; |
| 12236 | |
| 12237 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 12238 | if (ctxt == NULL) |
| 12239 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12240 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12241 | } |
| 12242 | |
| 12243 | /** |
| 12244 | * xmlReadFd: |
| 12245 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12246 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12247 | * @encoding: the document encoding, or NULL |
| 12248 | * @options: a combination of xmlParserOption(s) |
| 12249 | * |
| 12250 | * parse an XML from a file descriptor and build a tree. |
| 12251 | * |
| 12252 | * Returns the resulting document tree |
| 12253 | */ |
| 12254 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12255 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12256 | { |
| 12257 | xmlParserCtxtPtr ctxt; |
| 12258 | xmlParserInputBufferPtr input; |
| 12259 | xmlParserInputPtr stream; |
| 12260 | |
| 12261 | if (fd < 0) |
| 12262 | return (NULL); |
| 12263 | |
| 12264 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 12265 | if (input == NULL) |
| 12266 | return (NULL); |
| 12267 | ctxt = xmlNewParserCtxt(); |
| 12268 | if (ctxt == NULL) { |
| 12269 | xmlFreeParserInputBuffer(input); |
| 12270 | return (NULL); |
| 12271 | } |
| 12272 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12273 | if (stream == NULL) { |
| 12274 | xmlFreeParserInputBuffer(input); |
| 12275 | xmlFreeParserCtxt(ctxt); |
| 12276 | return (NULL); |
| 12277 | } |
| 12278 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12279 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12280 | } |
| 12281 | |
| 12282 | /** |
| 12283 | * xmlReadIO: |
| 12284 | * @ioread: an I/O read function |
| 12285 | * @ioclose: an I/O close function |
| 12286 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12287 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12288 | * @encoding: the document encoding, or NULL |
| 12289 | * @options: a combination of xmlParserOption(s) |
| 12290 | * |
| 12291 | * parse an XML document from I/O functions and source and build a tree. |
| 12292 | * |
| 12293 | * Returns the resulting document tree |
| 12294 | */ |
| 12295 | xmlDocPtr |
| 12296 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12297 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12298 | { |
| 12299 | xmlParserCtxtPtr ctxt; |
| 12300 | xmlParserInputBufferPtr input; |
| 12301 | xmlParserInputPtr stream; |
| 12302 | |
| 12303 | if (ioread == NULL) |
| 12304 | return (NULL); |
| 12305 | |
| 12306 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 12307 | XML_CHAR_ENCODING_NONE); |
| 12308 | if (input == NULL) |
| 12309 | return (NULL); |
| 12310 | ctxt = xmlNewParserCtxt(); |
| 12311 | if (ctxt == NULL) { |
| 12312 | xmlFreeParserInputBuffer(input); |
| 12313 | return (NULL); |
| 12314 | } |
| 12315 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12316 | if (stream == NULL) { |
| 12317 | xmlFreeParserInputBuffer(input); |
| 12318 | xmlFreeParserCtxt(ctxt); |
| 12319 | return (NULL); |
| 12320 | } |
| 12321 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12322 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12323 | } |
| 12324 | |
| 12325 | /** |
| 12326 | * xmlCtxtReadDoc: |
| 12327 | * @ctxt: an XML parser context |
| 12328 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12329 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12330 | * @encoding: the document encoding, or NULL |
| 12331 | * @options: a combination of xmlParserOption(s) |
| 12332 | * |
| 12333 | * parse an XML in-memory document and build a tree. |
| 12334 | * This reuses the existing @ctxt parser context |
| 12335 | * |
| 12336 | * Returns the resulting document tree |
| 12337 | */ |
| 12338 | xmlDocPtr |
| 12339 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12340 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12341 | { |
| 12342 | xmlParserInputPtr stream; |
| 12343 | |
| 12344 | if (cur == NULL) |
| 12345 | return (NULL); |
| 12346 | if (ctxt == NULL) |
| 12347 | return (NULL); |
| 12348 | |
| 12349 | xmlCtxtReset(ctxt); |
| 12350 | |
| 12351 | stream = xmlNewStringInputStream(ctxt, cur); |
| 12352 | if (stream == NULL) { |
| 12353 | return (NULL); |
| 12354 | } |
| 12355 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12356 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12357 | } |
| 12358 | |
| 12359 | /** |
| 12360 | * xmlCtxtReadFile: |
| 12361 | * @ctxt: an XML parser context |
| 12362 | * @filename: a file or URL |
| 12363 | * @encoding: the document encoding, or NULL |
| 12364 | * @options: a combination of xmlParserOption(s) |
| 12365 | * |
| 12366 | * parse an XML file from the filesystem or the network. |
| 12367 | * This reuses the existing @ctxt parser context |
| 12368 | * |
| 12369 | * Returns the resulting document tree |
| 12370 | */ |
| 12371 | xmlDocPtr |
| 12372 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 12373 | const char *encoding, int options) |
| 12374 | { |
| 12375 | xmlParserInputPtr stream; |
| 12376 | |
| 12377 | if (filename == NULL) |
| 12378 | return (NULL); |
| 12379 | if (ctxt == NULL) |
| 12380 | return (NULL); |
| 12381 | |
| 12382 | xmlCtxtReset(ctxt); |
| 12383 | |
| 12384 | stream = xmlNewInputFromFile(ctxt, filename); |
| 12385 | if (stream == NULL) { |
| 12386 | return (NULL); |
| 12387 | } |
| 12388 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12389 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12390 | } |
| 12391 | |
| 12392 | /** |
| 12393 | * xmlCtxtReadMemory: |
| 12394 | * @ctxt: an XML parser context |
| 12395 | * @buffer: a pointer to a char array |
| 12396 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12397 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12398 | * @encoding: the document encoding, or NULL |
| 12399 | * @options: a combination of xmlParserOption(s) |
| 12400 | * |
| 12401 | * parse an XML in-memory document and build a tree. |
| 12402 | * This reuses the existing @ctxt parser context |
| 12403 | * |
| 12404 | * Returns the resulting document tree |
| 12405 | */ |
| 12406 | xmlDocPtr |
| 12407 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12408 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12409 | { |
| 12410 | xmlParserInputBufferPtr input; |
| 12411 | xmlParserInputPtr stream; |
| 12412 | |
| 12413 | if (ctxt == NULL) |
| 12414 | return (NULL); |
| 12415 | if (buffer == NULL) |
| 12416 | return (NULL); |
| 12417 | |
| 12418 | xmlCtxtReset(ctxt); |
| 12419 | |
| 12420 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 12421 | if (input == NULL) { |
| 12422 | return(NULL); |
| 12423 | } |
| 12424 | |
| 12425 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12426 | if (stream == NULL) { |
| 12427 | xmlFreeParserInputBuffer(input); |
| 12428 | return(NULL); |
| 12429 | } |
| 12430 | |
| 12431 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12432 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12433 | } |
| 12434 | |
| 12435 | /** |
| 12436 | * xmlCtxtReadFd: |
| 12437 | * @ctxt: an XML parser context |
| 12438 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12439 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12440 | * @encoding: the document encoding, or NULL |
| 12441 | * @options: a combination of xmlParserOption(s) |
| 12442 | * |
| 12443 | * parse an XML from a file descriptor and build a tree. |
| 12444 | * This reuses the existing @ctxt parser context |
| 12445 | * |
| 12446 | * Returns the resulting document tree |
| 12447 | */ |
| 12448 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12449 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 12450 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12451 | { |
| 12452 | xmlParserInputBufferPtr input; |
| 12453 | xmlParserInputPtr stream; |
| 12454 | |
| 12455 | if (fd < 0) |
| 12456 | return (NULL); |
| 12457 | if (ctxt == NULL) |
| 12458 | return (NULL); |
| 12459 | |
| 12460 | xmlCtxtReset(ctxt); |
| 12461 | |
| 12462 | |
| 12463 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 12464 | if (input == NULL) |
| 12465 | return (NULL); |
| 12466 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12467 | if (stream == NULL) { |
| 12468 | xmlFreeParserInputBuffer(input); |
| 12469 | return (NULL); |
| 12470 | } |
| 12471 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12472 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12473 | } |
| 12474 | |
| 12475 | /** |
| 12476 | * xmlCtxtReadIO: |
| 12477 | * @ctxt: an XML parser context |
| 12478 | * @ioread: an I/O read function |
| 12479 | * @ioclose: an I/O close function |
| 12480 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12481 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12482 | * @encoding: the document encoding, or NULL |
| 12483 | * @options: a combination of xmlParserOption(s) |
| 12484 | * |
| 12485 | * parse an XML document from I/O functions and source and build a tree. |
| 12486 | * This reuses the existing @ctxt parser context |
| 12487 | * |
| 12488 | * Returns the resulting document tree |
| 12489 | */ |
| 12490 | xmlDocPtr |
| 12491 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 12492 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12493 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12494 | const char *encoding, int options) |
| 12495 | { |
| 12496 | xmlParserInputBufferPtr input; |
| 12497 | xmlParserInputPtr stream; |
| 12498 | |
| 12499 | if (ioread == NULL) |
| 12500 | return (NULL); |
| 12501 | if (ctxt == NULL) |
| 12502 | return (NULL); |
| 12503 | |
| 12504 | xmlCtxtReset(ctxt); |
| 12505 | |
| 12506 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 12507 | XML_CHAR_ENCODING_NONE); |
| 12508 | if (input == NULL) |
| 12509 | return (NULL); |
| 12510 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12511 | if (stream == NULL) { |
| 12512 | xmlFreeParserInputBuffer(input); |
| 12513 | return (NULL); |
| 12514 | } |
| 12515 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12516 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12517 | } |