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 |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 20 | * different ranges of character are actually implanted either in |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 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> |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 43 | #include <limits.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 44 | #include <string.h> |
Aleksey Sanin | e7acf43 | 2003-10-02 20:05:27 +0000 | [diff] [blame] | 45 | #include <stdarg.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 47 | #include <libxml/threads.h> |
| 48 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 49 | #include <libxml/tree.h> |
| 50 | #include <libxml/parser.h> |
| 51 | #include <libxml/parserInternals.h> |
| 52 | #include <libxml/valid.h> |
| 53 | #include <libxml/entities.h> |
| 54 | #include <libxml/xmlerror.h> |
| 55 | #include <libxml/encoding.h> |
| 56 | #include <libxml/xmlIO.h> |
| 57 | #include <libxml/uri.h> |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 58 | #ifdef LIBXML_CATALOG_ENABLED |
| 59 | #include <libxml/catalog.h> |
| 60 | #endif |
William M. Brack | 1d8c9b2 | 2004-12-25 10:14:57 +0000 | [diff] [blame] | 61 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 62 | #include <libxml/xmlschemastypes.h> |
| 63 | #include <libxml/relaxng.h> |
| 64 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 65 | #ifdef HAVE_CTYPE_H |
| 66 | #include <ctype.h> |
| 67 | #endif |
| 68 | #ifdef HAVE_STDLIB_H |
| 69 | #include <stdlib.h> |
| 70 | #endif |
| 71 | #ifdef HAVE_SYS_STAT_H |
| 72 | #include <sys/stat.h> |
| 73 | #endif |
| 74 | #ifdef HAVE_FCNTL_H |
| 75 | #include <fcntl.h> |
| 76 | #endif |
| 77 | #ifdef HAVE_UNISTD_H |
| 78 | #include <unistd.h> |
| 79 | #endif |
| 80 | #ifdef HAVE_ZLIB_H |
| 81 | #include <zlib.h> |
| 82 | #endif |
Anders F Bjorklund | eae5261 | 2011-09-18 16:59:13 +0200 | [diff] [blame] | 83 | #ifdef HAVE_LZMA_H |
| 84 | #include <lzma.h> |
| 85 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 86 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 87 | #include "buf.h" |
| 88 | #include "enc.h" |
| 89 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 90 | static void |
| 91 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info); |
| 92 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 93 | static xmlParserCtxtPtr |
| 94 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 95 | const xmlChar *base, xmlParserCtxtPtr pctx); |
| 96 | |
Daniel Veillard | 28cd9cb | 2015-11-20 14:55:30 +0800 | [diff] [blame] | 97 | static void xmlHaltParser(xmlParserCtxtPtr ctxt); |
| 98 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 99 | /************************************************************************ |
| 100 | * * |
| 101 | * Arbitrary limits set in the parser. See XML_PARSE_HUGE * |
| 102 | * * |
| 103 | ************************************************************************/ |
| 104 | |
| 105 | #define XML_PARSER_BIG_ENTITY 1000 |
| 106 | #define XML_PARSER_LOT_ENTITY 5000 |
| 107 | |
| 108 | /* |
| 109 | * XML_PARSER_NON_LINEAR is the threshold where the ratio of parsed entity |
| 110 | * replacement over the size in byte of the input indicates that you have |
| 111 | * and eponential behaviour. A value of 10 correspond to at least 3 entity |
| 112 | * replacement per byte of input. |
| 113 | */ |
| 114 | #define XML_PARSER_NON_LINEAR 10 |
| 115 | |
| 116 | /* |
| 117 | * xmlParserEntityCheck |
| 118 | * |
| 119 | * Function to check non-linear entity expansion behaviour |
| 120 | * This is here to detect and stop exponential linear entity expansion |
| 121 | * This is not a limitation of the parser but a safety |
| 122 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 123 | * parser option. |
| 124 | */ |
| 125 | static int |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 126 | xmlParserEntityCheck(xmlParserCtxtPtr ctxt, size_t size, |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 127 | xmlEntityPtr ent, size_t replacement) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 128 | { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 129 | size_t consumed = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 130 | |
| 131 | if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE)) |
| 132 | return (0); |
| 133 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 134 | return (1); |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * This may look absurd but is needed to detect |
| 138 | * entities problems |
| 139 | */ |
| 140 | if ((ent != NULL) && (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 141 | (ent->content != NULL) && (ent->checked == 0)) { |
| 142 | unsigned long oldnbent = ctxt->nbentities; |
| 143 | xmlChar *rep; |
| 144 | |
| 145 | ent->checked = 1; |
| 146 | |
| 147 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
| 148 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 149 | |
| 150 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
| 151 | if (rep != NULL) { |
| 152 | if (xmlStrchr(rep, '<')) |
| 153 | ent->checked |= 1; |
| 154 | xmlFree(rep); |
| 155 | rep = NULL; |
| 156 | } |
| 157 | } |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 158 | if (replacement != 0) { |
| 159 | if (replacement < XML_MAX_TEXT_LENGTH) |
| 160 | return(0); |
| 161 | |
| 162 | /* |
| 163 | * If the volume of entity copy reaches 10 times the |
| 164 | * amount of parsed data and over the large text threshold |
| 165 | * then that's very likely to be an abuse. |
| 166 | */ |
| 167 | if (ctxt->input != NULL) { |
| 168 | consumed = ctxt->input->consumed + |
| 169 | (ctxt->input->cur - ctxt->input->base); |
| 170 | } |
| 171 | consumed += ctxt->sizeentities; |
| 172 | |
| 173 | if (replacement < XML_PARSER_NON_LINEAR * consumed) |
| 174 | return(0); |
| 175 | } else if (size != 0) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Do the check based on the replacement size of the entity |
| 178 | */ |
| 179 | if (size < XML_PARSER_BIG_ENTITY) |
| 180 | return(0); |
| 181 | |
| 182 | /* |
| 183 | * A limit on the amount of text data reasonably used |
| 184 | */ |
| 185 | if (ctxt->input != NULL) { |
| 186 | consumed = ctxt->input->consumed + |
| 187 | (ctxt->input->cur - ctxt->input->base); |
| 188 | } |
| 189 | consumed += ctxt->sizeentities; |
| 190 | |
| 191 | if ((size < XML_PARSER_NON_LINEAR * consumed) && |
| 192 | (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed)) |
| 193 | return (0); |
| 194 | } else if (ent != NULL) { |
| 195 | /* |
| 196 | * use the number of parsed entities in the replacement |
| 197 | */ |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 198 | size = ent->checked / 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * The amount of data parsed counting entities size only once |
| 202 | */ |
| 203 | if (ctxt->input != NULL) { |
| 204 | consumed = ctxt->input->consumed + |
| 205 | (ctxt->input->cur - ctxt->input->base); |
| 206 | } |
| 207 | consumed += ctxt->sizeentities; |
| 208 | |
| 209 | /* |
| 210 | * Check the density of entities for the amount of data |
| 211 | * knowing an entity reference will take at least 3 bytes |
| 212 | */ |
| 213 | if (size * 3 < consumed * XML_PARSER_NON_LINEAR) |
| 214 | return (0); |
| 215 | } else { |
| 216 | /* |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 217 | * strange we got no data for checking |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 218 | */ |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 219 | if (((ctxt->lastError.code != XML_ERR_UNDECLARED_ENTITY) && |
| 220 | (ctxt->lastError.code != XML_WAR_UNDECLARED_ENTITY)) || |
| 221 | (ctxt->nbentities <= 10000)) |
| 222 | return (0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 223 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 224 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 225 | return (1); |
| 226 | } |
| 227 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 228 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 229 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 230 | * |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 231 | * arbitrary depth limit for the XML documents that we allow to |
| 232 | * process. This is not a limitation of the parser but a safety |
| 233 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 234 | * parser option. |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 235 | */ |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 236 | unsigned int xmlParserMaxDepth = 256; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 237 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 238 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 239 | |
| 240 | #define SAX2 1 |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 241 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 242 | #define XML_PARSER_BUFFER_SIZE 100 |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 243 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 244 | |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 245 | /** |
| 246 | * XML_PARSER_CHUNK_SIZE |
| 247 | * |
| 248 | * When calling GROW that's the minimal amount of data |
| 249 | * the parser expected to have received. It is not a hard |
| 250 | * limit but an optimization when reading strings like Names |
| 251 | * It is not strictly needed as long as inputs available characters |
| 252 | * are followed by 0, which should be provided by the I/O level |
| 253 | */ |
| 254 | #define XML_PARSER_CHUNK_SIZE 100 |
| 255 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 256 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 257 | * List of XML prefixed PI allowed by W3C specs |
| 258 | */ |
| 259 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 260 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 261 | "xml-stylesheet", |
Daniel Veillard | 4c4653e | 2011-06-05 11:29:29 +0800 | [diff] [blame] | 262 | "xml-model", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 263 | NULL |
| 264 | }; |
| 265 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 266 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 267 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 268 | static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 269 | const xmlChar **str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 270 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 271 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 272 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 273 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 274 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 275 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 276 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 277 | static int |
| 278 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, |
| 279 | const char *encoding); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 280 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 281 | static void |
| 282 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 283 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 284 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 285 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 286 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 287 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 288 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 289 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 290 | static int |
| 291 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity); |
| 292 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 293 | /************************************************************************ |
| 294 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 295 | * Some factorized error routines * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 296 | * * |
| 297 | ************************************************************************/ |
| 298 | |
| 299 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 300 | * xmlErrAttributeDup: |
| 301 | * @ctxt: an XML parser context |
| 302 | * @prefix: the attribute prefix |
| 303 | * @localname: the attribute localname |
| 304 | * |
| 305 | * Handle a redefinition of attribute error |
| 306 | */ |
| 307 | static void |
| 308 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 309 | const xmlChar * localname) |
| 310 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 311 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 312 | (ctxt->instate == XML_PARSER_EOF)) |
| 313 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 314 | if (ctxt != NULL) |
| 315 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 316 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 317 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 318 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 319 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 320 | (const char *) localname, NULL, NULL, 0, 0, |
| 321 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 322 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 323 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 324 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 325 | (const char *) prefix, (const char *) localname, |
| 326 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 327 | localname); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 328 | if (ctxt != NULL) { |
| 329 | ctxt->wellFormed = 0; |
| 330 | if (ctxt->recovery == 0) |
| 331 | ctxt->disableSAX = 1; |
| 332 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * xmlFatalErr: |
| 337 | * @ctxt: an XML parser context |
| 338 | * @error: the error number |
| 339 | * @extra: extra information string |
| 340 | * |
| 341 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 342 | */ |
| 343 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 344 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 345 | { |
| 346 | const char *errmsg; |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 347 | char errstr[129] = ""; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 348 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 349 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 350 | (ctxt->instate == XML_PARSER_EOF)) |
| 351 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 352 | switch (error) { |
| 353 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 354 | errmsg = "CharRef: invalid hexadecimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 355 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 356 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 357 | errmsg = "CharRef: invalid decimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 358 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 359 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 360 | errmsg = "CharRef: invalid value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 361 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 362 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 363 | errmsg = "internal error"; |
| 364 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 365 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 366 | errmsg = "PEReference at end of document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 367 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 368 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 369 | errmsg = "PEReference in prolog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 370 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 371 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 372 | errmsg = "PEReference in epilog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 373 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 374 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 375 | errmsg = "PEReference: no name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 376 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 377 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 378 | errmsg = "PEReference: expecting ';'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 379 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 380 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 381 | errmsg = "Detected an entity reference loop"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 382 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 383 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 384 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 385 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 386 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 387 | errmsg = "PEReferences forbidden in internal subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 388 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 389 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 390 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 391 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 392 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 393 | errmsg = "AttValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 394 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 395 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 396 | errmsg = "Unescaped '<' not allowed in attributes values"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 397 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 398 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 399 | errmsg = "SystemLiteral \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 400 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 401 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 402 | errmsg = "Unfinished System or Public ID \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 403 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 404 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 405 | errmsg = "Sequence ']]>' not allowed in content"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 406 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 407 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 408 | errmsg = "SYSTEM or PUBLIC, the URI is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 409 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 410 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 411 | errmsg = "PUBLIC, the Public Identifier is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 412 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 413 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 414 | errmsg = "Comment must not contain '--' (double-hyphen)"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 415 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 416 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 417 | errmsg = "xmlParsePI : no target name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 418 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 419 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 420 | errmsg = "Invalid PI name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 421 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 422 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 423 | errmsg = "NOTATION: Name expected here"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 424 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 425 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 426 | errmsg = "'>' required to close NOTATION declaration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 427 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 428 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 429 | errmsg = "Entity value required"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 430 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 431 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 432 | errmsg = "Fragment not allowed"; |
| 433 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 434 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 435 | errmsg = "'(' required to start ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 436 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 437 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 438 | errmsg = "NmToken expected in ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 439 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 440 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 441 | errmsg = "')' required to finish ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 442 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 443 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 444 | errmsg = "MixedContentDecl : '|' or ')*' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 445 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 446 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 447 | errmsg = "MixedContentDecl : '#PCDATA' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 448 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 449 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 450 | errmsg = "ContentDecl : Name or '(' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 451 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 452 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 453 | errmsg = "ContentDecl : ',' '|' or ')' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 454 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 455 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 456 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 457 | "PEReference: forbidden within markup decl in internal subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 458 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 459 | case XML_ERR_GT_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 460 | errmsg = "expected '>'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 461 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 462 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 463 | errmsg = "XML conditional section '[' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 464 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 465 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 466 | errmsg = "Content error in the external subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 467 | break; |
| 468 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 469 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 470 | "conditional section INCLUDE or IGNORE keyword expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 471 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 472 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 473 | errmsg = "XML conditional section not closed"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 474 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 475 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 476 | errmsg = "Text declaration '<?xml' required"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 477 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 478 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 479 | errmsg = "parsing XML declaration: '?>' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 480 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 481 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 482 | errmsg = "external parsed entities cannot be standalone"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 483 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 484 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 485 | errmsg = "EntityRef: expecting ';'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 486 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 487 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 488 | errmsg = "DOCTYPE improperly terminated"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 489 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 490 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 491 | errmsg = "EndTag: '</' not found"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 492 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 493 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 494 | errmsg = "expected '='"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 495 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 496 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 497 | errmsg = "String not closed expecting \" or '"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 498 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 499 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 500 | errmsg = "String not started expecting ' or \""; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 501 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 502 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 503 | errmsg = "Invalid XML encoding name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 504 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 505 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 506 | errmsg = "standalone accepts only 'yes' or 'no'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 507 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 508 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 509 | errmsg = "Document is empty"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 510 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 511 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 512 | errmsg = "Extra content at the end of the document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 513 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 514 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 515 | errmsg = "chunk is not well balanced"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 516 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 517 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 518 | errmsg = "extra content at the end of well balanced chunk"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 519 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 520 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 521 | errmsg = "Malformed declaration expecting version"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 522 | break; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 523 | case XML_ERR_NAME_TOO_LONG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 524 | errmsg = "Name too long use XML_PARSE_HUGE option"; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 525 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 526 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 527 | case: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 528 | errmsg = ""; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 529 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 530 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 531 | default: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 532 | errmsg = "Unregistered error message"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 533 | } |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 534 | if (info == NULL) |
| 535 | snprintf(errstr, 128, "%s\n", errmsg); |
| 536 | else |
| 537 | snprintf(errstr, 128, "%s: %%s\n", errmsg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 538 | if (ctxt != NULL) |
| 539 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 540 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 541 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, &errstr[0], |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 542 | info); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 543 | if (ctxt != NULL) { |
| 544 | ctxt->wellFormed = 0; |
| 545 | if (ctxt->recovery == 0) |
| 546 | ctxt->disableSAX = 1; |
| 547 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 550 | /** |
| 551 | * xmlFatalErrMsg: |
| 552 | * @ctxt: an XML parser context |
| 553 | * @error: the error number |
| 554 | * @msg: the error message |
| 555 | * |
| 556 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 557 | */ |
| 558 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 559 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 560 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 561 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 562 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 563 | (ctxt->instate == XML_PARSER_EOF)) |
| 564 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 565 | if (ctxt != NULL) |
| 566 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 567 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 568 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 569 | if (ctxt != NULL) { |
| 570 | ctxt->wellFormed = 0; |
| 571 | if (ctxt->recovery == 0) |
| 572 | ctxt->disableSAX = 1; |
| 573 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 577 | * xmlWarningMsg: |
| 578 | * @ctxt: an XML parser context |
| 579 | * @error: the error number |
| 580 | * @msg: the error message |
| 581 | * @str1: extra data |
| 582 | * @str2: extra data |
| 583 | * |
| 584 | * Handle a warning. |
| 585 | */ |
| 586 | static void |
| 587 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 588 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 589 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 590 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 591 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 592 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 593 | (ctxt->instate == XML_PARSER_EOF)) |
| 594 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 595 | if ((ctxt != NULL) && (ctxt->sax != NULL) && |
| 596 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 597 | schannel = ctxt->sax->serror; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 598 | if (ctxt != NULL) { |
| 599 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 600 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 601 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 602 | ctxt, NULL, XML_FROM_PARSER, error, |
| 603 | XML_ERR_WARNING, NULL, 0, |
| 604 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 605 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 606 | } else { |
| 607 | __xmlRaiseError(schannel, NULL, NULL, |
| 608 | ctxt, NULL, XML_FROM_PARSER, error, |
| 609 | XML_ERR_WARNING, NULL, 0, |
| 610 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 611 | msg, (const char *) str1, (const char *) str2); |
| 612 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /** |
| 616 | * xmlValidityError: |
| 617 | * @ctxt: an XML parser context |
| 618 | * @error: the error number |
| 619 | * @msg: the error message |
| 620 | * @str1: extra data |
| 621 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 622 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 623 | */ |
| 624 | static void |
| 625 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 626 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 627 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 628 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 629 | |
| 630 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 631 | (ctxt->instate == XML_PARSER_EOF)) |
| 632 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 633 | if (ctxt != NULL) { |
| 634 | ctxt->errNo = error; |
| 635 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 636 | schannel = ctxt->sax->serror; |
| 637 | } |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 638 | if (ctxt != NULL) { |
| 639 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 640 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 641 | ctxt, NULL, XML_FROM_DTD, error, |
| 642 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 643 | (const char *) str2, NULL, 0, 0, |
| 644 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 645 | ctxt->valid = 0; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 646 | } else { |
| 647 | __xmlRaiseError(schannel, NULL, NULL, |
| 648 | ctxt, NULL, XML_FROM_DTD, error, |
| 649 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 650 | (const char *) str2, NULL, 0, 0, |
| 651 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 652 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 656 | * xmlFatalErrMsgInt: |
| 657 | * @ctxt: an XML parser context |
| 658 | * @error: the error number |
| 659 | * @msg: the error message |
| 660 | * @val: an integer value |
| 661 | * |
| 662 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 663 | */ |
| 664 | static void |
| 665 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 666 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 667 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 668 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 669 | (ctxt->instate == XML_PARSER_EOF)) |
| 670 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 671 | if (ctxt != NULL) |
| 672 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 673 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 674 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 675 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 676 | if (ctxt != NULL) { |
| 677 | ctxt->wellFormed = 0; |
| 678 | if (ctxt->recovery == 0) |
| 679 | ctxt->disableSAX = 1; |
| 680 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 684 | * xmlFatalErrMsgStrIntStr: |
| 685 | * @ctxt: an XML parser context |
| 686 | * @error: the error number |
| 687 | * @msg: the error message |
| 688 | * @str1: an string info |
| 689 | * @val: an integer value |
| 690 | * @str2: an string info |
| 691 | * |
| 692 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 693 | */ |
| 694 | static void |
| 695 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 696 | const char *msg, const xmlChar *str1, int val, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 697 | const xmlChar *str2) |
| 698 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 699 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 700 | (ctxt->instate == XML_PARSER_EOF)) |
| 701 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 702 | if (ctxt != NULL) |
| 703 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 704 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 705 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 706 | NULL, 0, (const char *) str1, (const char *) str2, |
| 707 | NULL, val, 0, msg, str1, val, str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 708 | if (ctxt != NULL) { |
| 709 | ctxt->wellFormed = 0; |
| 710 | if (ctxt->recovery == 0) |
| 711 | ctxt->disableSAX = 1; |
| 712 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 716 | * xmlFatalErrMsgStr: |
| 717 | * @ctxt: an XML parser context |
| 718 | * @error: the error number |
| 719 | * @msg: the error message |
| 720 | * @val: a string value |
| 721 | * |
| 722 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 723 | */ |
| 724 | static void |
| 725 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 726 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 727 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 728 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 729 | (ctxt->instate == XML_PARSER_EOF)) |
| 730 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 731 | if (ctxt != NULL) |
| 732 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 733 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 734 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 735 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 736 | val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 737 | if (ctxt != NULL) { |
| 738 | ctxt->wellFormed = 0; |
| 739 | if (ctxt->recovery == 0) |
| 740 | ctxt->disableSAX = 1; |
| 741 | } |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 745 | * xmlErrMsgStr: |
| 746 | * @ctxt: an XML parser context |
| 747 | * @error: the error number |
| 748 | * @msg: the error message |
| 749 | * @val: a string value |
| 750 | * |
| 751 | * Handle a non fatal parser error |
| 752 | */ |
| 753 | static void |
| 754 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 755 | const char *msg, const xmlChar * val) |
| 756 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 757 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 758 | (ctxt->instate == XML_PARSER_EOF)) |
| 759 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 760 | if (ctxt != NULL) |
| 761 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 762 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 763 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 764 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 765 | val); |
| 766 | } |
| 767 | |
| 768 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 769 | * xmlNsErr: |
| 770 | * @ctxt: an XML parser context |
| 771 | * @error: the error number |
| 772 | * @msg: the message |
| 773 | * @info1: extra information string |
| 774 | * @info2: extra information string |
| 775 | * |
| 776 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 777 | */ |
| 778 | static void |
| 779 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 780 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 781 | const xmlChar * info1, const xmlChar * info2, |
| 782 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 783 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 784 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 785 | (ctxt->instate == XML_PARSER_EOF)) |
| 786 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 787 | if (ctxt != NULL) |
| 788 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 789 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 790 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 791 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 792 | info1, info2, info3); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 793 | if (ctxt != NULL) |
| 794 | ctxt->nsWellFormed = 0; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 797 | /** |
| 798 | * xmlNsWarn |
| 799 | * @ctxt: an XML parser context |
| 800 | * @error: the error number |
| 801 | * @msg: the message |
| 802 | * @info1: extra information string |
| 803 | * @info2: extra information string |
| 804 | * |
Daniel Veillard | 288bb62 | 2012-05-07 15:01:29 +0800 | [diff] [blame] | 805 | * Handle a namespace warning error |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 806 | */ |
| 807 | static void |
| 808 | xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 809 | const char *msg, |
| 810 | const xmlChar * info1, const xmlChar * info2, |
| 811 | const xmlChar * info3) |
| 812 | { |
| 813 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 814 | (ctxt->instate == XML_PARSER_EOF)) |
| 815 | return; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 816 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
| 817 | XML_ERR_WARNING, NULL, 0, (const char *) info1, |
| 818 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 819 | info1, info2, info3); |
| 820 | } |
| 821 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 822 | /************************************************************************ |
| 823 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 824 | * Library wide options * |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 825 | * * |
| 826 | ************************************************************************/ |
| 827 | |
| 828 | /** |
| 829 | * xmlHasFeature: |
| 830 | * @feature: the feature to be examined |
| 831 | * |
| 832 | * Examines if the library has been compiled with a given feature. |
| 833 | * |
| 834 | * Returns a non-zero value if the feature exist, otherwise zero. |
| 835 | * Returns zero (0) if the feature does not exist or an unknown |
| 836 | * unknown feature is requested, non-zero otherwise. |
| 837 | */ |
| 838 | int |
| 839 | xmlHasFeature(xmlFeature feature) |
| 840 | { |
| 841 | switch (feature) { |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 842 | case XML_WITH_THREAD: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 843 | #ifdef LIBXML_THREAD_ENABLED |
| 844 | return(1); |
| 845 | #else |
| 846 | return(0); |
| 847 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 848 | case XML_WITH_TREE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 849 | #ifdef LIBXML_TREE_ENABLED |
| 850 | return(1); |
| 851 | #else |
| 852 | return(0); |
| 853 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 854 | case XML_WITH_OUTPUT: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 855 | #ifdef LIBXML_OUTPUT_ENABLED |
| 856 | return(1); |
| 857 | #else |
| 858 | return(0); |
| 859 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 860 | case XML_WITH_PUSH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 861 | #ifdef LIBXML_PUSH_ENABLED |
| 862 | return(1); |
| 863 | #else |
| 864 | return(0); |
| 865 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 866 | case XML_WITH_READER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 867 | #ifdef LIBXML_READER_ENABLED |
| 868 | return(1); |
| 869 | #else |
| 870 | return(0); |
| 871 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 872 | case XML_WITH_PATTERN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 873 | #ifdef LIBXML_PATTERN_ENABLED |
| 874 | return(1); |
| 875 | #else |
| 876 | return(0); |
| 877 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 878 | case XML_WITH_WRITER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 879 | #ifdef LIBXML_WRITER_ENABLED |
| 880 | return(1); |
| 881 | #else |
| 882 | return(0); |
| 883 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 884 | case XML_WITH_SAX1: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 885 | #ifdef LIBXML_SAX1_ENABLED |
| 886 | return(1); |
| 887 | #else |
| 888 | return(0); |
| 889 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 890 | case XML_WITH_FTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 891 | #ifdef LIBXML_FTP_ENABLED |
| 892 | return(1); |
| 893 | #else |
| 894 | return(0); |
| 895 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 896 | case XML_WITH_HTTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 897 | #ifdef LIBXML_HTTP_ENABLED |
| 898 | return(1); |
| 899 | #else |
| 900 | return(0); |
| 901 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 902 | case XML_WITH_VALID: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 903 | #ifdef LIBXML_VALID_ENABLED |
| 904 | return(1); |
| 905 | #else |
| 906 | return(0); |
| 907 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 908 | case XML_WITH_HTML: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 909 | #ifdef LIBXML_HTML_ENABLED |
| 910 | return(1); |
| 911 | #else |
| 912 | return(0); |
| 913 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 914 | case XML_WITH_LEGACY: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 915 | #ifdef LIBXML_LEGACY_ENABLED |
| 916 | return(1); |
| 917 | #else |
| 918 | return(0); |
| 919 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 920 | case XML_WITH_C14N: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 921 | #ifdef LIBXML_C14N_ENABLED |
| 922 | return(1); |
| 923 | #else |
| 924 | return(0); |
| 925 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 926 | case XML_WITH_CATALOG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 927 | #ifdef LIBXML_CATALOG_ENABLED |
| 928 | return(1); |
| 929 | #else |
| 930 | return(0); |
| 931 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 932 | case XML_WITH_XPATH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 933 | #ifdef LIBXML_XPATH_ENABLED |
| 934 | return(1); |
| 935 | #else |
| 936 | return(0); |
| 937 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 938 | case XML_WITH_XPTR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 939 | #ifdef LIBXML_XPTR_ENABLED |
| 940 | return(1); |
| 941 | #else |
| 942 | return(0); |
| 943 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 944 | case XML_WITH_XINCLUDE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 945 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 946 | return(1); |
| 947 | #else |
| 948 | return(0); |
| 949 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 950 | case XML_WITH_ICONV: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 951 | #ifdef LIBXML_ICONV_ENABLED |
| 952 | return(1); |
| 953 | #else |
| 954 | return(0); |
| 955 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 956 | case XML_WITH_ISO8859X: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 957 | #ifdef LIBXML_ISO8859X_ENABLED |
| 958 | return(1); |
| 959 | #else |
| 960 | return(0); |
| 961 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 962 | case XML_WITH_UNICODE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 963 | #ifdef LIBXML_UNICODE_ENABLED |
| 964 | return(1); |
| 965 | #else |
| 966 | return(0); |
| 967 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 968 | case XML_WITH_REGEXP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 969 | #ifdef LIBXML_REGEXP_ENABLED |
| 970 | return(1); |
| 971 | #else |
| 972 | return(0); |
| 973 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 974 | case XML_WITH_AUTOMATA: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 975 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 976 | return(1); |
| 977 | #else |
| 978 | return(0); |
| 979 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 980 | case XML_WITH_EXPR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 981 | #ifdef LIBXML_EXPR_ENABLED |
| 982 | return(1); |
| 983 | #else |
| 984 | return(0); |
| 985 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 986 | case XML_WITH_SCHEMAS: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 987 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 988 | return(1); |
| 989 | #else |
| 990 | return(0); |
| 991 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 992 | case XML_WITH_SCHEMATRON: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 993 | #ifdef LIBXML_SCHEMATRON_ENABLED |
| 994 | return(1); |
| 995 | #else |
| 996 | return(0); |
| 997 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 998 | case XML_WITH_MODULES: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 999 | #ifdef LIBXML_MODULES_ENABLED |
| 1000 | return(1); |
| 1001 | #else |
| 1002 | return(0); |
| 1003 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 1004 | case XML_WITH_DEBUG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 1005 | #ifdef LIBXML_DEBUG_ENABLED |
| 1006 | return(1); |
| 1007 | #else |
| 1008 | return(0); |
| 1009 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 1010 | case XML_WITH_DEBUG_MEM: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 1011 | #ifdef DEBUG_MEMORY_LOCATION |
| 1012 | return(1); |
| 1013 | #else |
| 1014 | return(0); |
| 1015 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 1016 | case XML_WITH_DEBUG_RUN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 1017 | #ifdef LIBXML_DEBUG_RUNTIME |
| 1018 | return(1); |
| 1019 | #else |
| 1020 | return(0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1021 | #endif |
Daniel Veillard | 75acfee | 2006-07-13 06:29:56 +0000 | [diff] [blame] | 1022 | case XML_WITH_ZLIB: |
| 1023 | #ifdef LIBXML_ZLIB_ENABLED |
| 1024 | return(1); |
| 1025 | #else |
| 1026 | return(0); |
| 1027 | #endif |
Anders F Bjorklund | eae5261 | 2011-09-18 16:59:13 +0200 | [diff] [blame] | 1028 | case XML_WITH_LZMA: |
| 1029 | #ifdef LIBXML_LZMA_ENABLED |
| 1030 | return(1); |
| 1031 | #else |
| 1032 | return(0); |
| 1033 | #endif |
Giuseppe Iuculano | 48f7dcb | 2010-11-04 17:42:42 +0100 | [diff] [blame] | 1034 | case XML_WITH_ICU: |
| 1035 | #ifdef LIBXML_ICU_ENABLED |
| 1036 | return(1); |
| 1037 | #else |
| 1038 | return(0); |
| 1039 | #endif |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 1040 | default: |
| 1041 | break; |
| 1042 | } |
| 1043 | return(0); |
| 1044 | } |
| 1045 | |
| 1046 | /************************************************************************ |
| 1047 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1048 | * SAX2 defaulted attributes handling * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1049 | * * |
| 1050 | ************************************************************************/ |
| 1051 | |
| 1052 | /** |
| 1053 | * xmlDetectSAX2: |
| 1054 | * @ctxt: an XML parser context |
| 1055 | * |
| 1056 | * Do the SAX2 detection and specific intialization |
| 1057 | */ |
| 1058 | static void |
| 1059 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 1060 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1061 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1062 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 1063 | ((ctxt->sax->startElementNs != NULL) || |
| 1064 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1065 | #else |
| 1066 | ctxt->sax2 = 1; |
| 1067 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1068 | |
| 1069 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 1070 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 1071 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1072 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 1073 | (ctxt->str_xml_ns == NULL)) { |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 1074 | xmlErrMemory(ctxt, NULL); |
| 1075 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1078 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 1079 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 1080 | struct _xmlDefAttrs { |
| 1081 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 1082 | int maxAttrs; /* the size of the array */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1083 | const xmlChar *values[5]; /* array of localname/prefix/values/external */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1084 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1085 | |
| 1086 | /** |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1087 | * xmlAttrNormalizeSpace: |
| 1088 | * @src: the source string |
| 1089 | * @dst: the target string |
| 1090 | * |
| 1091 | * Normalize the space in non CDATA attribute values: |
| 1092 | * If the attribute type is not CDATA, then the XML processor MUST further |
| 1093 | * process the normalized attribute value by discarding any leading and |
| 1094 | * trailing space (#x20) characters, and by replacing sequences of space |
| 1095 | * (#x20) characters by a single space (#x20) character. |
| 1096 | * Note that the size of dst need to be at least src, and if one doesn't need |
| 1097 | * to preserve dst (and it doesn't come from a dictionary or read-only) then |
| 1098 | * passing src as dst is just fine. |
| 1099 | * |
| 1100 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1101 | * is needed. |
| 1102 | */ |
| 1103 | static xmlChar * |
| 1104 | xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst) |
| 1105 | { |
| 1106 | if ((src == NULL) || (dst == NULL)) |
| 1107 | return(NULL); |
| 1108 | |
| 1109 | while (*src == 0x20) src++; |
| 1110 | while (*src != 0) { |
| 1111 | if (*src == 0x20) { |
| 1112 | while (*src == 0x20) src++; |
| 1113 | if (*src != 0) |
| 1114 | *dst++ = 0x20; |
| 1115 | } else { |
| 1116 | *dst++ = *src++; |
| 1117 | } |
| 1118 | } |
| 1119 | *dst = 0; |
| 1120 | if (dst == src) |
| 1121 | return(NULL); |
| 1122 | return(dst); |
| 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * xmlAttrNormalizeSpace2: |
| 1127 | * @src: the source string |
| 1128 | * |
| 1129 | * Normalize the space in non CDATA attribute values, a slightly more complex |
| 1130 | * front end to avoid allocation problems when running on attribute values |
| 1131 | * coming from the input. |
| 1132 | * |
| 1133 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1134 | * is needed. |
| 1135 | */ |
| 1136 | static const xmlChar * |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1137 | xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len) |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1138 | { |
| 1139 | int i; |
| 1140 | int remove_head = 0; |
| 1141 | int need_realloc = 0; |
| 1142 | const xmlChar *cur; |
| 1143 | |
| 1144 | if ((ctxt == NULL) || (src == NULL) || (len == NULL)) |
| 1145 | return(NULL); |
| 1146 | i = *len; |
| 1147 | if (i <= 0) |
| 1148 | return(NULL); |
| 1149 | |
| 1150 | cur = src; |
| 1151 | while (*cur == 0x20) { |
| 1152 | cur++; |
| 1153 | remove_head++; |
| 1154 | } |
| 1155 | while (*cur != 0) { |
| 1156 | if (*cur == 0x20) { |
| 1157 | cur++; |
| 1158 | if ((*cur == 0x20) || (*cur == 0)) { |
| 1159 | need_realloc = 1; |
| 1160 | break; |
| 1161 | } |
| 1162 | } else |
| 1163 | cur++; |
| 1164 | } |
| 1165 | if (need_realloc) { |
| 1166 | xmlChar *ret; |
| 1167 | |
| 1168 | ret = xmlStrndup(src + remove_head, i - remove_head + 1); |
| 1169 | if (ret == NULL) { |
| 1170 | xmlErrMemory(ctxt, NULL); |
| 1171 | return(NULL); |
| 1172 | } |
| 1173 | xmlAttrNormalizeSpace(ret, ret); |
| 1174 | *len = (int) strlen((const char *)ret); |
| 1175 | return(ret); |
| 1176 | } else if (remove_head) { |
| 1177 | *len -= remove_head; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1178 | memmove(src, src + remove_head, 1 + *len); |
| 1179 | return(src); |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1180 | } |
| 1181 | return(NULL); |
| 1182 | } |
| 1183 | |
| 1184 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1185 | * xmlAddDefAttrs: |
| 1186 | * @ctxt: an XML parser context |
| 1187 | * @fullname: the element fullname |
| 1188 | * @fullattr: the attribute fullname |
| 1189 | * @value: the attribute value |
| 1190 | * |
| 1191 | * Add a defaulted attribute for an element |
| 1192 | */ |
| 1193 | static void |
| 1194 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 1195 | const xmlChar *fullname, |
| 1196 | const xmlChar *fullattr, |
| 1197 | const xmlChar *value) { |
| 1198 | xmlDefAttrsPtr defaults; |
| 1199 | int len; |
| 1200 | const xmlChar *name; |
| 1201 | const xmlChar *prefix; |
| 1202 | |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 1203 | /* |
| 1204 | * Allows to detect attribute redefinitions |
| 1205 | */ |
| 1206 | if (ctxt->attsSpecial != NULL) { |
| 1207 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1208 | return; |
| 1209 | } |
| 1210 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1211 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1212 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1213 | if (ctxt->attsDefault == NULL) |
| 1214 | goto mem_error; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1218 | * split the element name into prefix:localname , the string found |
| 1219 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1220 | */ |
| 1221 | name = xmlSplitQName3(fullname, &len); |
| 1222 | if (name == NULL) { |
| 1223 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 1224 | prefix = NULL; |
| 1225 | } else { |
| 1226 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1227 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 1228 | } |
| 1229 | |
| 1230 | /* |
| 1231 | * make sure there is some storage |
| 1232 | */ |
| 1233 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 1234 | if (defaults == NULL) { |
| 1235 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1236 | (4 * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1237 | if (defaults == NULL) |
| 1238 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1239 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1240 | defaults->maxAttrs = 4; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1241 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1242 | defaults, NULL) < 0) { |
| 1243 | xmlFree(defaults); |
| 1244 | goto mem_error; |
| 1245 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1246 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1247 | xmlDefAttrsPtr temp; |
| 1248 | |
| 1249 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1250 | (2 * defaults->maxAttrs * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1251 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1252 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1253 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1254 | defaults->maxAttrs *= 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1255 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1256 | defaults, NULL) < 0) { |
| 1257 | xmlFree(defaults); |
| 1258 | goto mem_error; |
| 1259 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 1263 | * Split the element name into prefix:localname , the string found |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1264 | * are within the DTD and hen not associated to namespace names. |
| 1265 | */ |
| 1266 | name = xmlSplitQName3(fullattr, &len); |
| 1267 | if (name == NULL) { |
| 1268 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 1269 | prefix = NULL; |
| 1270 | } else { |
| 1271 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1272 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 1273 | } |
| 1274 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1275 | defaults->values[5 * defaults->nbAttrs] = name; |
| 1276 | defaults->values[5 * defaults->nbAttrs + 1] = prefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1277 | /* intern the string and precompute the end */ |
| 1278 | len = xmlStrlen(value); |
| 1279 | value = xmlDictLookup(ctxt->dict, value, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1280 | defaults->values[5 * defaults->nbAttrs + 2] = value; |
| 1281 | defaults->values[5 * defaults->nbAttrs + 3] = value + len; |
| 1282 | if (ctxt->external) |
| 1283 | defaults->values[5 * defaults->nbAttrs + 4] = BAD_CAST "external"; |
| 1284 | else |
| 1285 | defaults->values[5 * defaults->nbAttrs + 4] = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1286 | defaults->nbAttrs++; |
| 1287 | |
| 1288 | return; |
| 1289 | |
| 1290 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1291 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1295 | /** |
| 1296 | * xmlAddSpecialAttr: |
| 1297 | * @ctxt: an XML parser context |
| 1298 | * @fullname: the element fullname |
| 1299 | * @fullattr: the attribute fullname |
| 1300 | * @type: the attribute type |
| 1301 | * |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1302 | * Register this attribute type |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1303 | */ |
| 1304 | static void |
| 1305 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 1306 | const xmlChar *fullname, |
| 1307 | const xmlChar *fullattr, |
| 1308 | int type) |
| 1309 | { |
| 1310 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1311 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1312 | if (ctxt->attsSpecial == NULL) |
| 1313 | goto mem_error; |
| 1314 | } |
| 1315 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1316 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1317 | return; |
| 1318 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1319 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 1320 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1321 | return; |
| 1322 | |
| 1323 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1324 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1328 | /** |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1329 | * xmlCleanSpecialAttrCallback: |
| 1330 | * |
| 1331 | * Removes CDATA attributes from the special attribute table |
| 1332 | */ |
| 1333 | static void |
| 1334 | xmlCleanSpecialAttrCallback(void *payload, void *data, |
| 1335 | const xmlChar *fullname, const xmlChar *fullattr, |
| 1336 | const xmlChar *unused ATTRIBUTE_UNUSED) { |
| 1337 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data; |
| 1338 | |
Daniel Veillard | b3edafd | 2008-01-11 08:00:57 +0000 | [diff] [blame] | 1339 | if (((long) payload) == XML_ATTRIBUTE_CDATA) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1340 | xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL); |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * xmlCleanSpecialAttr: |
| 1346 | * @ctxt: an XML parser context |
| 1347 | * |
| 1348 | * Trim the list of attributes defined to remove all those of type |
| 1349 | * CDATA as they are not special. This call should be done when finishing |
| 1350 | * to parse the DTD and before starting to parse the document root. |
| 1351 | */ |
| 1352 | static void |
| 1353 | xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt) |
| 1354 | { |
| 1355 | if (ctxt->attsSpecial == NULL) |
| 1356 | return; |
| 1357 | |
| 1358 | xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt); |
| 1359 | |
| 1360 | if (xmlHashSize(ctxt->attsSpecial) == 0) { |
| 1361 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 1362 | ctxt->attsSpecial = NULL; |
| 1363 | } |
| 1364 | return; |
| 1365 | } |
| 1366 | |
| 1367 | /** |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1368 | * xmlCheckLanguageID: |
| 1369 | * @lang: pointer to the string value |
| 1370 | * |
| 1371 | * Checks that the value conforms to the LanguageID production: |
| 1372 | * |
| 1373 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 1374 | * the XML Second edition. |
| 1375 | * |
| 1376 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 1377 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 1378 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 1379 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 1380 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 1381 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 1382 | * |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1383 | * The current REC reference the sucessors of RFC 1766, currently 5646 |
| 1384 | * |
| 1385 | * http://www.rfc-editor.org/rfc/rfc5646.txt |
| 1386 | * langtag = language |
| 1387 | * ["-" script] |
| 1388 | * ["-" region] |
| 1389 | * *("-" variant) |
| 1390 | * *("-" extension) |
| 1391 | * ["-" privateuse] |
| 1392 | * language = 2*3ALPHA ; shortest ISO 639 code |
| 1393 | * ["-" extlang] ; sometimes followed by |
| 1394 | * ; extended language subtags |
| 1395 | * / 4ALPHA ; or reserved for future use |
| 1396 | * / 5*8ALPHA ; or registered language subtag |
| 1397 | * |
| 1398 | * extlang = 3ALPHA ; selected ISO 639 codes |
| 1399 | * *2("-" 3ALPHA) ; permanently reserved |
| 1400 | * |
| 1401 | * script = 4ALPHA ; ISO 15924 code |
| 1402 | * |
| 1403 | * region = 2ALPHA ; ISO 3166-1 code |
| 1404 | * / 3DIGIT ; UN M.49 code |
| 1405 | * |
| 1406 | * variant = 5*8alphanum ; registered variants |
| 1407 | * / (DIGIT 3alphanum) |
| 1408 | * |
| 1409 | * extension = singleton 1*("-" (2*8alphanum)) |
| 1410 | * |
| 1411 | * ; Single alphanumerics |
| 1412 | * ; "x" reserved for private use |
| 1413 | * singleton = DIGIT ; 0 - 9 |
| 1414 | * / %x41-57 ; A - W |
| 1415 | * / %x59-5A ; Y - Z |
| 1416 | * / %x61-77 ; a - w |
| 1417 | * / %x79-7A ; y - z |
| 1418 | * |
| 1419 | * it sounds right to still allow Irregular i-xxx IANA and user codes too |
| 1420 | * The parser below doesn't try to cope with extension or privateuse |
| 1421 | * that could be added but that's not interoperable anyway |
| 1422 | * |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1423 | * Returns 1 if correct 0 otherwise |
| 1424 | **/ |
| 1425 | int |
| 1426 | xmlCheckLanguageID(const xmlChar * lang) |
| 1427 | { |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1428 | const xmlChar *cur = lang, *nxt; |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (cur == NULL) |
| 1431 | return (0); |
| 1432 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1433 | ((cur[0] == 'I') && (cur[1] == '-')) || |
| 1434 | ((cur[0] == 'x') && (cur[1] == '-')) || |
| 1435 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1436 | /* |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1437 | * Still allow IANA code and user code which were coming |
| 1438 | * from the previous version of the XML-1.0 specification |
| 1439 | * it's deprecated but we should not fail |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1440 | */ |
| 1441 | cur += 2; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1442 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1443 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1444 | cur++; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1445 | return(cur[0] == 0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1446 | } |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1447 | nxt = cur; |
| 1448 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1449 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1450 | nxt++; |
| 1451 | if (nxt - cur >= 4) { |
| 1452 | /* |
| 1453 | * Reserved |
| 1454 | */ |
| 1455 | if ((nxt - cur > 8) || (nxt[0] != 0)) |
| 1456 | return(0); |
| 1457 | return(1); |
| 1458 | } |
| 1459 | if (nxt - cur < 2) |
| 1460 | return(0); |
| 1461 | /* we got an ISO 639 code */ |
| 1462 | if (nxt[0] == 0) |
| 1463 | return(1); |
| 1464 | if (nxt[0] != '-') |
| 1465 | return(0); |
| 1466 | |
| 1467 | nxt++; |
| 1468 | cur = nxt; |
| 1469 | /* now we can have extlang or script or region or variant */ |
| 1470 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1471 | goto region_m49; |
| 1472 | |
| 1473 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1474 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1475 | nxt++; |
| 1476 | if (nxt - cur == 4) |
| 1477 | goto script; |
| 1478 | if (nxt - cur == 2) |
| 1479 | goto region; |
| 1480 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1481 | goto variant; |
| 1482 | if (nxt - cur != 3) |
| 1483 | return(0); |
| 1484 | /* we parsed an extlang */ |
| 1485 | if (nxt[0] == 0) |
| 1486 | return(1); |
| 1487 | if (nxt[0] != '-') |
| 1488 | return(0); |
| 1489 | |
| 1490 | nxt++; |
| 1491 | cur = nxt; |
| 1492 | /* now we can have script or region or variant */ |
| 1493 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1494 | goto region_m49; |
| 1495 | |
| 1496 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1497 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1498 | nxt++; |
| 1499 | if (nxt - cur == 2) |
| 1500 | goto region; |
| 1501 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1502 | goto variant; |
| 1503 | if (nxt - cur != 4) |
| 1504 | return(0); |
| 1505 | /* we parsed a script */ |
| 1506 | script: |
| 1507 | if (nxt[0] == 0) |
| 1508 | return(1); |
| 1509 | if (nxt[0] != '-') |
| 1510 | return(0); |
| 1511 | |
| 1512 | nxt++; |
| 1513 | cur = nxt; |
| 1514 | /* now we can have region or variant */ |
| 1515 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1516 | goto region_m49; |
| 1517 | |
| 1518 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1519 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1520 | nxt++; |
| 1521 | |
| 1522 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1523 | goto variant; |
| 1524 | if (nxt - cur != 2) |
| 1525 | return(0); |
| 1526 | /* we parsed a region */ |
| 1527 | region: |
| 1528 | if (nxt[0] == 0) |
| 1529 | return(1); |
| 1530 | if (nxt[0] != '-') |
| 1531 | return(0); |
| 1532 | |
| 1533 | nxt++; |
| 1534 | cur = nxt; |
| 1535 | /* now we can just have a variant */ |
| 1536 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1537 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1538 | nxt++; |
| 1539 | |
| 1540 | if ((nxt - cur < 5) || (nxt - cur > 8)) |
| 1541 | return(0); |
| 1542 | |
| 1543 | /* we parsed a variant */ |
| 1544 | variant: |
| 1545 | if (nxt[0] == 0) |
| 1546 | return(1); |
| 1547 | if (nxt[0] != '-') |
| 1548 | return(0); |
| 1549 | /* extensions and private use subtags not checked */ |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1550 | return (1); |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1551 | |
| 1552 | region_m49: |
| 1553 | if (((nxt[1] >= '0') && (nxt[1] <= '9')) && |
| 1554 | ((nxt[2] >= '0') && (nxt[2] <= '9'))) { |
| 1555 | nxt += 3; |
| 1556 | goto region; |
| 1557 | } |
| 1558 | return(0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1561 | /************************************************************************ |
| 1562 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 1563 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1564 | * * |
| 1565 | ************************************************************************/ |
| 1566 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 1567 | static xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 1568 | const xmlChar ** str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1570 | #ifdef SAX2 |
| 1571 | /** |
| 1572 | * nsPush: |
| 1573 | * @ctxt: an XML parser context |
| 1574 | * @prefix: the namespace prefix or NULL |
| 1575 | * @URL: the namespace name |
| 1576 | * |
| 1577 | * Pushes a new parser namespace on top of the ns stack |
| 1578 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 1579 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 1580 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1581 | */ |
| 1582 | static int |
| 1583 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 1584 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1585 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 1586 | int i; |
Daniel Veillard | 711b15d | 2012-10-25 19:23:26 +0800 | [diff] [blame] | 1587 | for (i = ctxt->nsNr - 2;i >= 0;i -= 2) { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1588 | if (ctxt->nsTab[i] == prefix) { |
| 1589 | /* in scope */ |
| 1590 | if (ctxt->nsTab[i + 1] == URL) |
| 1591 | return(-2); |
| 1592 | /* out of scope keep it */ |
| 1593 | break; |
| 1594 | } |
| 1595 | } |
| 1596 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1597 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 1598 | ctxt->nsMax = 10; |
| 1599 | ctxt->nsNr = 0; |
| 1600 | ctxt->nsTab = (const xmlChar **) |
| 1601 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 1602 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1603 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1604 | ctxt->nsMax = 0; |
| 1605 | return (-1); |
| 1606 | } |
| 1607 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1608 | const xmlChar ** tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1609 | ctxt->nsMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1610 | tmp = (const xmlChar **) xmlRealloc((char *) ctxt->nsTab, |
| 1611 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 1612 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1613 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1614 | ctxt->nsMax /= 2; |
| 1615 | return (-1); |
| 1616 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1617 | ctxt->nsTab = tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1618 | } |
| 1619 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 1620 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 1621 | return (ctxt->nsNr); |
| 1622 | } |
| 1623 | /** |
| 1624 | * nsPop: |
| 1625 | * @ctxt: an XML parser context |
| 1626 | * @nr: the number to pop |
| 1627 | * |
| 1628 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 1629 | * |
| 1630 | * Returns the number of namespaces removed |
| 1631 | */ |
| 1632 | static int |
| 1633 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 1634 | { |
| 1635 | int i; |
| 1636 | |
| 1637 | if (ctxt->nsTab == NULL) return(0); |
| 1638 | if (ctxt->nsNr < nr) { |
| 1639 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 1640 | nr = ctxt->nsNr; |
| 1641 | } |
| 1642 | if (ctxt->nsNr <= 0) |
| 1643 | return (0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1644 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1645 | for (i = 0;i < nr;i++) { |
| 1646 | ctxt->nsNr--; |
| 1647 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 1648 | } |
| 1649 | return(nr); |
| 1650 | } |
| 1651 | #endif |
| 1652 | |
| 1653 | static int |
| 1654 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 1655 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1656 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1657 | int maxatts; |
| 1658 | |
| 1659 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1660 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1661 | atts = (const xmlChar **) |
| 1662 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1663 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1664 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1665 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 1666 | if (attallocs == NULL) goto mem_error; |
| 1667 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1668 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1669 | } else if (nr + 5 > ctxt->maxatts) { |
| 1670 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1671 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 1672 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1673 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1674 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1675 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 1676 | (maxatts / 5) * sizeof(int)); |
| 1677 | if (attallocs == NULL) goto mem_error; |
| 1678 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1679 | ctxt->maxatts = maxatts; |
| 1680 | } |
| 1681 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1682 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1683 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1684 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1687 | /** |
| 1688 | * inputPush: |
| 1689 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1690 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1691 | * |
| 1692 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1693 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1694 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1695 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1696 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1697 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 1698 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1699 | if ((ctxt == NULL) || (value == NULL)) |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1700 | return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1701 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 1702 | ctxt->inputMax *= 2; |
| 1703 | ctxt->inputTab = |
| 1704 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 1705 | ctxt->inputMax * |
| 1706 | sizeof(ctxt->inputTab[0])); |
| 1707 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1708 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1709 | xmlFreeInputStream(value); |
| 1710 | ctxt->inputMax /= 2; |
| 1711 | value = NULL; |
| 1712 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1713 | } |
| 1714 | } |
| 1715 | ctxt->inputTab[ctxt->inputNr] = value; |
| 1716 | ctxt->input = value; |
| 1717 | return (ctxt->inputNr++); |
| 1718 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1719 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1720 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1721 | * @ctxt: an XML parser context |
| 1722 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1723 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1724 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1725 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1726 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1727 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1728 | inputPop(xmlParserCtxtPtr ctxt) |
| 1729 | { |
| 1730 | xmlParserInputPtr ret; |
| 1731 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1732 | if (ctxt == NULL) |
| 1733 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1734 | if (ctxt->inputNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1735 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1736 | ctxt->inputNr--; |
| 1737 | if (ctxt->inputNr > 0) |
| 1738 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 1739 | else |
| 1740 | ctxt->input = NULL; |
| 1741 | ret = ctxt->inputTab[ctxt->inputNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1742 | ctxt->inputTab[ctxt->inputNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1743 | return (ret); |
| 1744 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1745 | /** |
| 1746 | * nodePush: |
| 1747 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1748 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1749 | * |
| 1750 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1751 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1752 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1753 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1754 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1755 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1756 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1757 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1758 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1759 | xmlNodePtr *tmp; |
| 1760 | |
| 1761 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1762 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1763 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1764 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1765 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1766 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1767 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1768 | ctxt->nodeTab = tmp; |
| 1769 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1770 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1771 | if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) && |
| 1772 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1773 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1774 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1775 | xmlParserMaxDepth); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 1776 | xmlHaltParser(ctxt); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1777 | return(-1); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1778 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1779 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1780 | ctxt->node = value; |
| 1781 | return (ctxt->nodeNr++); |
| 1782 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1783 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1784 | /** |
| 1785 | * nodePop: |
| 1786 | * @ctxt: an XML parser context |
| 1787 | * |
| 1788 | * Pops the top element node from the node stack |
| 1789 | * |
| 1790 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1791 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1792 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1793 | nodePop(xmlParserCtxtPtr ctxt) |
| 1794 | { |
| 1795 | xmlNodePtr ret; |
| 1796 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1797 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1798 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1799 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1800 | ctxt->nodeNr--; |
| 1801 | if (ctxt->nodeNr > 0) |
| 1802 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1803 | else |
| 1804 | ctxt->node = NULL; |
| 1805 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1806 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1807 | return (ret); |
| 1808 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1809 | |
| 1810 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1811 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1812 | * nameNsPush: |
| 1813 | * @ctxt: an XML parser context |
| 1814 | * @value: the element name |
| 1815 | * @prefix: the element prefix |
| 1816 | * @URI: the element namespace name |
| 1817 | * |
| 1818 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1819 | * |
| 1820 | * Returns -1 in case of error, the index in the stack otherwise |
| 1821 | */ |
| 1822 | static int |
| 1823 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1824 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1825 | { |
| 1826 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1827 | const xmlChar * *tmp; |
| 1828 | void **tmp2; |
| 1829 | ctxt->nameMax *= 2; |
| 1830 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1831 | ctxt->nameMax * |
| 1832 | sizeof(ctxt->nameTab[0])); |
| 1833 | if (tmp == NULL) { |
| 1834 | ctxt->nameMax /= 2; |
| 1835 | goto mem_error; |
| 1836 | } |
| 1837 | ctxt->nameTab = tmp; |
| 1838 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1839 | ctxt->nameMax * 3 * |
| 1840 | sizeof(ctxt->pushTab[0])); |
| 1841 | if (tmp2 == NULL) { |
| 1842 | ctxt->nameMax /= 2; |
| 1843 | goto mem_error; |
| 1844 | } |
| 1845 | ctxt->pushTab = tmp2; |
| 1846 | } |
| 1847 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1848 | ctxt->name = value; |
| 1849 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1850 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1851 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1852 | return (ctxt->nameNr++); |
| 1853 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1854 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1855 | return (-1); |
| 1856 | } |
| 1857 | /** |
| 1858 | * nameNsPop: |
| 1859 | * @ctxt: an XML parser context |
| 1860 | * |
| 1861 | * Pops the top element/prefix/URI name from the name stack |
| 1862 | * |
| 1863 | * Returns the name just removed |
| 1864 | */ |
| 1865 | static const xmlChar * |
| 1866 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1867 | { |
| 1868 | const xmlChar *ret; |
| 1869 | |
| 1870 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1871 | return (NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1872 | ctxt->nameNr--; |
| 1873 | if (ctxt->nameNr > 0) |
| 1874 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1875 | else |
| 1876 | ctxt->name = NULL; |
| 1877 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1878 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1879 | return (ret); |
| 1880 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1881 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1882 | |
| 1883 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1884 | * namePush: |
| 1885 | * @ctxt: an XML parser context |
| 1886 | * @value: the element name |
| 1887 | * |
| 1888 | * Pushes a new element name on top of the name stack |
| 1889 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1890 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1891 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1892 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1893 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1894 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1895 | if (ctxt == NULL) return (-1); |
| 1896 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1897 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1898 | const xmlChar * *tmp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1899 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1900 | ctxt->nameMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1901 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1902 | if (tmp == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1903 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1904 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1905 | ctxt->nameTab = tmp; |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1906 | ctxt->nameMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1907 | } |
| 1908 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1909 | ctxt->name = value; |
| 1910 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1911 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1912 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1913 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1914 | } |
| 1915 | /** |
| 1916 | * namePop: |
| 1917 | * @ctxt: an XML parser context |
| 1918 | * |
| 1919 | * Pops the top element name from the name stack |
| 1920 | * |
| 1921 | * Returns the name just removed |
| 1922 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1923 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1924 | namePop(xmlParserCtxtPtr ctxt) |
| 1925 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1926 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1927 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1928 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1929 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1930 | ctxt->nameNr--; |
| 1931 | if (ctxt->nameNr > 0) |
| 1932 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1933 | else |
| 1934 | ctxt->name = NULL; |
| 1935 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1936 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1937 | return (ret); |
| 1938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1940 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1941 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1942 | int *tmp; |
| 1943 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | ctxt->spaceMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1945 | tmp = (int *) xmlRealloc(ctxt->spaceTab, |
| 1946 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1947 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1948 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1949 | ctxt->spaceMax /=2; |
| 1950 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1951 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1952 | ctxt->spaceTab = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1953 | } |
| 1954 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1955 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1956 | return(ctxt->spaceNr++); |
| 1957 | } |
| 1958 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1959 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | int ret; |
| 1961 | if (ctxt->spaceNr <= 0) return(0); |
| 1962 | ctxt->spaceNr--; |
| 1963 | if (ctxt->spaceNr > 0) |
| 1964 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1965 | else |
Daniel Veillard | dbcbbd2 | 2006-06-18 19:55:20 +0000 | [diff] [blame] | 1966 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1967 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1968 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1969 | return(ret); |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * Macros for accessing the content. Those should be used only by the parser, |
| 1974 | * and not exported. |
| 1975 | * |
| 1976 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1977 | * use them |
| 1978 | * |
| 1979 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1980 | * To be used with extreme caution since operations consuming |
| 1981 | * characters may move the input buffer to a different location ! |
| 1982 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1983 | * This should be used internally by the parser |
| 1984 | * only to compare to ASCII values otherwise it would break when |
| 1985 | * running with UTF-8 encoding. |
| 1986 | * RAW same as CUR but in the input buffer, bypass any token |
| 1987 | * extraction that may have been done |
| 1988 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1989 | * to compare on ASCII based substring. |
| 1990 | * 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] | 1991 | * strings without newlines within the parser. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1992 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1993 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1994 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1995 | * |
| 1996 | * NEXT Skip to the next character, this does the proper decoding |
| 1997 | * 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] | 1998 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1999 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 2000 | * to the number of xmlChars used for the encoding [0-5]. |
| 2001 | * CUR_SCHAR same but operate on a string instead of the context |
| 2002 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 2003 | * the index |
| 2004 | * GROW, SHRINK handling of input buffers |
| 2005 | */ |
| 2006 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2007 | #define RAW (*ctxt->input->cur) |
| 2008 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2009 | #define NXT(val) ctxt->input->cur[(val)] |
| 2010 | #define CUR_PTR ctxt->input->cur |
| 2011 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2012 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 2013 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 2014 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 2015 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 2016 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 2017 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 2018 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 2019 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 2020 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 2021 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 2022 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 2023 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 2024 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 2025 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 2026 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 2027 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 2028 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 2029 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2030 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2031 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2032 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2033 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2034 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 2035 | xmlPopInput(ctxt); \ |
| 2036 | } while (0) |
| 2037 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2038 | #define SKIPL(val) do { \ |
| 2039 | int skipl; \ |
| 2040 | for(skipl=0; skipl<val; skipl++) { \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2041 | if (*(ctxt->input->cur) == '\n') { \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2042 | ctxt->input->line++; ctxt->input->col = 1; \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2043 | } else ctxt->input->col++; \ |
| 2044 | ctxt->nbChars++; \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2045 | ctxt->input->cur++; \ |
| 2046 | } \ |
| 2047 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 2048 | if ((*ctxt->input->cur == 0) && \ |
| 2049 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 2050 | xmlPopInput(ctxt); \ |
| 2051 | } while (0) |
| 2052 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2053 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 2054 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 2055 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2056 | xmlSHRINK (ctxt); |
| 2057 | |
| 2058 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 2059 | xmlParserInputShrink(ctxt->input); |
| 2060 | if ((*ctxt->input->cur == 0) && |
| 2061 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2062 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2063 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2064 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2065 | #define GROW if ((ctxt->progressive == 0) && \ |
| 2066 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2067 | xmlGROW (ctxt); |
| 2068 | |
| 2069 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
Longstreth Jon | 190a0b8 | 2014-02-06 10:58:17 +0100 | [diff] [blame] | 2070 | unsigned long curEnd = ctxt->input->end - ctxt->input->cur; |
| 2071 | unsigned long curBase = ctxt->input->cur - ctxt->input->base; |
| 2072 | |
| 2073 | if (((curEnd > (unsigned long) XML_MAX_LOOKUP_LIMIT) || |
| 2074 | (curBase > (unsigned long) XML_MAX_LOOKUP_LIMIT)) && |
Patrick Gansterer | 9c8eaab | 2013-01-04 12:41:53 +0100 | [diff] [blame] | 2075 | ((ctxt->input->buf) && (ctxt->input->buf->readcallback != (xmlInputReadCallback) xmlNop)) && |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2076 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 2077 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
Daniel Veillard | 35bcb1d | 2015-11-20 15:04:09 +0800 | [diff] [blame^] | 2078 | xmlHaltParser(ctxt); |
| 2079 | return; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2080 | } |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2081 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Daniel Veillard | 35bcb1d | 2015-11-20 15:04:09 +0800 | [diff] [blame^] | 2082 | if ((ctxt->input->cur > ctxt->input->end) || |
| 2083 | (ctxt->input->cur < ctxt->input->base)) { |
| 2084 | xmlHaltParser(ctxt); |
| 2085 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "cur index out of bound"); |
| 2086 | return; |
| 2087 | } |
Daniel Veillard | 59df783 | 2010-02-02 10:24:01 +0100 | [diff] [blame] | 2088 | if ((ctxt->input->cur != NULL) && (*ctxt->input->cur == 0) && |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2089 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2090 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2091 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2092 | |
| 2093 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 2094 | |
| 2095 | #define NEXT xmlNextChar(ctxt) |
| 2096 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2097 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2098 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2099 | ctxt->input->cur++; \ |
| 2100 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2101 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2102 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 2103 | } |
| 2104 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | #define NEXTL(l) do { \ |
| 2106 | if (*(ctxt->input->cur) == '\n') { \ |
| 2107 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 2108 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2109 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2110 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2111 | } while (0) |
| 2112 | |
| 2113 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 2114 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 2115 | |
| 2116 | #define COPY_BUF(l,b,i,v) \ |
| 2117 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2118 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2119 | |
| 2120 | /** |
| 2121 | * xmlSkipBlankChars: |
| 2122 | * @ctxt: the XML parser context |
| 2123 | * |
| 2124 | * skip all blanks character found at that point in the input streams. |
| 2125 | * It pops up finished entities in the process if allowable at that point. |
| 2126 | * |
| 2127 | * Returns the number of space chars skipped |
| 2128 | */ |
| 2129 | |
| 2130 | int |
| 2131 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2132 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2133 | |
| 2134 | /* |
| 2135 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 2136 | * the ASCII range. |
| 2137 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2138 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 2139 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2140 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2141 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2142 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2143 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2144 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2145 | if (*cur == '\n') { |
| 2146 | ctxt->input->line++; ctxt->input->col = 1; |
Juergen Keil | 5d4310a | 2014-08-07 16:28:09 +0800 | [diff] [blame] | 2147 | } else { |
| 2148 | ctxt->input->col++; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2149 | } |
| 2150 | cur++; |
| 2151 | res++; |
| 2152 | if (*cur == 0) { |
| 2153 | ctxt->input->cur = cur; |
| 2154 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 2155 | cur = ctxt->input->cur; |
| 2156 | } |
| 2157 | } |
| 2158 | ctxt->input->cur = cur; |
| 2159 | } else { |
| 2160 | int cur; |
| 2161 | do { |
| 2162 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 2163 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2164 | NEXT; |
| 2165 | cur = CUR; |
| 2166 | res++; |
| 2167 | } |
| 2168 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 2169 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 2170 | xmlPopInput(ctxt); |
| 2171 | cur = CUR; |
| 2172 | } |
| 2173 | /* |
| 2174 | * Need to handle support of entities branching here |
| 2175 | */ |
| 2176 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 2177 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 2178 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | return(res); |
| 2180 | } |
| 2181 | |
| 2182 | /************************************************************************ |
| 2183 | * * |
| 2184 | * Commodity functions to handle entities * |
| 2185 | * * |
| 2186 | ************************************************************************/ |
| 2187 | |
| 2188 | /** |
| 2189 | * xmlPopInput: |
| 2190 | * @ctxt: an XML parser context |
| 2191 | * |
| 2192 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 2193 | * pop it and return the next char. |
| 2194 | * |
| 2195 | * Returns the current xmlChar in the parser context |
| 2196 | */ |
| 2197 | xmlChar |
| 2198 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2199 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | if (xmlParserDebugEntities) |
| 2201 | xmlGenericError(xmlGenericErrorContext, |
| 2202 | "Popping input %d\n", ctxt->inputNr); |
| 2203 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2204 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2205 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2206 | return(xmlPopInput(ctxt)); |
| 2207 | return(CUR); |
| 2208 | } |
| 2209 | |
| 2210 | /** |
| 2211 | * xmlPushInput: |
| 2212 | * @ctxt: an XML parser context |
| 2213 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 2214 | * |
| 2215 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 2216 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2217 | * Returns -1 in case of error or the index in the input stack |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2218 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2219 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2220 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2221 | int ret; |
| 2222 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2223 | |
| 2224 | if (xmlParserDebugEntities) { |
| 2225 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 2226 | xmlGenericError(xmlGenericErrorContext, |
| 2227 | "%s(%d): ", ctxt->input->filename, |
| 2228 | ctxt->input->line); |
| 2229 | xmlGenericError(xmlGenericErrorContext, |
| 2230 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 2231 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2232 | ret = inputPush(ctxt, input); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2233 | if (ctxt->instate == XML_PARSER_EOF) |
| 2234 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2235 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2236 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /** |
| 2240 | * xmlParseCharRef: |
| 2241 | * @ctxt: an XML parser context |
| 2242 | * |
| 2243 | * parse Reference declarations |
| 2244 | * |
| 2245 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2246 | * '&#x' [0-9a-fA-F]+ ';' |
| 2247 | * |
| 2248 | * [ WFC: Legal Character ] |
| 2249 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2250 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2251 | * |
| 2252 | * Returns the value parsed (as an int), 0 in case of error |
| 2253 | */ |
| 2254 | int |
| 2255 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2256 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2257 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2258 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2259 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2260 | /* |
| 2261 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2262 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2263 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2264 | (NXT(2) == 'x')) { |
| 2265 | SKIP(3); |
| 2266 | GROW; |
| 2267 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2268 | if (count++ > 20) { |
| 2269 | count = 0; |
| 2270 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2271 | if (ctxt->instate == XML_PARSER_EOF) |
| 2272 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2273 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2274 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2275 | val = val * 16 + (CUR - '0'); |
| 2276 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2277 | val = val * 16 + (CUR - 'a') + 10; |
| 2278 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2279 | val = val * 16 + (CUR - 'A') + 10; |
| 2280 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2281 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2282 | val = 0; |
| 2283 | break; |
| 2284 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2285 | if (val > 0x10FFFF) |
| 2286 | outofrange = val; |
| 2287 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | NEXT; |
| 2289 | count++; |
| 2290 | } |
| 2291 | if (RAW == ';') { |
| 2292 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2293 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2294 | ctxt->nbChars ++; |
| 2295 | ctxt->input->cur++; |
| 2296 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2297 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2298 | SKIP(2); |
| 2299 | GROW; |
| 2300 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2301 | if (count++ > 20) { |
| 2302 | count = 0; |
| 2303 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2304 | if (ctxt->instate == XML_PARSER_EOF) |
| 2305 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2306 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2307 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | val = val * 10 + (CUR - '0'); |
| 2309 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2310 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2311 | val = 0; |
| 2312 | break; |
| 2313 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2314 | if (val > 0x10FFFF) |
| 2315 | outofrange = val; |
| 2316 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2317 | NEXT; |
| 2318 | count++; |
| 2319 | } |
| 2320 | if (RAW == ';') { |
| 2321 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2322 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2323 | ctxt->nbChars ++; |
| 2324 | ctxt->input->cur++; |
| 2325 | } |
| 2326 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2327 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /* |
| 2331 | * [ WFC: Legal Character ] |
| 2332 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2333 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2334 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2335 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2336 | return(val); |
| 2337 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2338 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2339 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2340 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | } |
| 2342 | return(0); |
| 2343 | } |
| 2344 | |
| 2345 | /** |
| 2346 | * xmlParseStringCharRef: |
| 2347 | * @ctxt: an XML parser context |
| 2348 | * @str: a pointer to an index in the string |
| 2349 | * |
| 2350 | * parse Reference declarations, variant parsing from a string rather |
| 2351 | * than an an input flow. |
| 2352 | * |
| 2353 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2354 | * '&#x' [0-9a-fA-F]+ ';' |
| 2355 | * |
| 2356 | * [ WFC: Legal Character ] |
| 2357 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2358 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2359 | * |
| 2360 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2361 | * updated to the current value of the index |
| 2362 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2363 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2365 | const xmlChar *ptr; |
| 2366 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2367 | unsigned int val = 0; |
| 2368 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | |
| 2370 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2371 | ptr = *str; |
| 2372 | cur = *ptr; |
| 2373 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2374 | ptr += 3; |
| 2375 | cur = *ptr; |
| 2376 | while (cur != ';') { /* Non input consuming loop */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2377 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2378 | val = val * 16 + (cur - '0'); |
| 2379 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2380 | val = val * 16 + (cur - 'a') + 10; |
| 2381 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2382 | val = val * 16 + (cur - 'A') + 10; |
| 2383 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2384 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2385 | val = 0; |
| 2386 | break; |
| 2387 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2388 | if (val > 0x10FFFF) |
| 2389 | outofrange = val; |
| 2390 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2391 | ptr++; |
| 2392 | cur = *ptr; |
| 2393 | } |
| 2394 | if (cur == ';') |
| 2395 | ptr++; |
| 2396 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2397 | ptr += 2; |
| 2398 | cur = *ptr; |
| 2399 | while (cur != ';') { /* Non input consuming loops */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2400 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2401 | val = val * 10 + (cur - '0'); |
| 2402 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2403 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2404 | val = 0; |
| 2405 | break; |
| 2406 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2407 | if (val > 0x10FFFF) |
| 2408 | outofrange = val; |
| 2409 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2410 | ptr++; |
| 2411 | cur = *ptr; |
| 2412 | } |
| 2413 | if (cur == ';') |
| 2414 | ptr++; |
| 2415 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2416 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2417 | return(0); |
| 2418 | } |
| 2419 | *str = ptr; |
| 2420 | |
| 2421 | /* |
| 2422 | * [ WFC: Legal Character ] |
| 2423 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2424 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2425 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2426 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2427 | return(val); |
| 2428 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2429 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2430 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2431 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | } |
| 2433 | return(0); |
| 2434 | } |
| 2435 | |
| 2436 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2437 | * xmlNewBlanksWrapperInputStream: |
| 2438 | * @ctxt: an XML parser context |
| 2439 | * @entity: an Entity pointer |
| 2440 | * |
| 2441 | * Create a new input stream for wrapping |
| 2442 | * blanks around a PEReference |
| 2443 | * |
| 2444 | * Returns the new input stream or NULL |
| 2445 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2446 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2447 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2448 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2449 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2450 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2451 | xmlParserInputPtr input; |
| 2452 | xmlChar *buffer; |
| 2453 | size_t length; |
| 2454 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2455 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2456 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2457 | return(NULL); |
| 2458 | } |
| 2459 | if (xmlParserDebugEntities) |
| 2460 | xmlGenericError(xmlGenericErrorContext, |
| 2461 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2462 | input = xmlNewInputStream(ctxt); |
| 2463 | if (input == NULL) { |
| 2464 | return(NULL); |
| 2465 | } |
| 2466 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2467 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2468 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2469 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2470 | xmlFree(input); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2471 | return(NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2472 | } |
| 2473 | buffer [0] = ' '; |
| 2474 | buffer [1] = '%'; |
| 2475 | buffer [length-3] = ';'; |
| 2476 | buffer [length-2] = ' '; |
| 2477 | buffer [length-1] = 0; |
| 2478 | memcpy(buffer + 2, entity->name, length - 5); |
| 2479 | input->free = deallocblankswrapper; |
| 2480 | input->base = buffer; |
| 2481 | input->cur = buffer; |
| 2482 | input->length = length; |
| 2483 | input->end = &buffer[length]; |
| 2484 | return(input); |
| 2485 | } |
| 2486 | |
| 2487 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2488 | * xmlParserHandlePEReference: |
| 2489 | * @ctxt: the parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2490 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2491 | * [69] PEReference ::= '%' Name ';' |
| 2492 | * |
| 2493 | * [ WFC: No Recursion ] |
| 2494 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2495 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2496 | * |
| 2497 | * [ WFC: Entity Declared ] |
| 2498 | * In a document without any DTD, a document with only an internal DTD |
| 2499 | * subset which contains no parameter entity references, or a document |
| 2500 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2501 | * entity must precede any reference to it... |
| 2502 | * |
| 2503 | * [ VC: Entity Declared ] |
| 2504 | * In a document with an external subset or external parameter entities |
| 2505 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2506 | * must precede any reference to it... |
| 2507 | * |
| 2508 | * [ WFC: In DTD ] |
| 2509 | * Parameter-entity references may only appear in the DTD. |
| 2510 | * NOTE: misleading but this is handled. |
| 2511 | * |
| 2512 | * A PEReference may have been detected in the current input stream |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2513 | * the handling is done accordingly to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2514 | * http://www.w3.org/TR/REC-xml#entproc |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2515 | * i.e. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2516 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2517 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2518 | */ |
| 2519 | void |
| 2520 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2521 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2522 | xmlEntityPtr entity = NULL; |
| 2523 | xmlParserInputPtr input; |
| 2524 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2525 | if (RAW != '%') return; |
| 2526 | switch(ctxt->instate) { |
| 2527 | case XML_PARSER_CDATA_SECTION: |
| 2528 | return; |
| 2529 | case XML_PARSER_COMMENT: |
| 2530 | return; |
| 2531 | case XML_PARSER_START_TAG: |
| 2532 | return; |
| 2533 | case XML_PARSER_END_TAG: |
| 2534 | return; |
| 2535 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2536 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2537 | return; |
| 2538 | case XML_PARSER_PROLOG: |
| 2539 | case XML_PARSER_START: |
| 2540 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2541 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | case XML_PARSER_ENTITY_DECL: |
| 2544 | case XML_PARSER_CONTENT: |
| 2545 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2546 | case XML_PARSER_PI: |
| 2547 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2548 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2549 | /* we just ignore it there */ |
| 2550 | return; |
| 2551 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2552 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2553 | return; |
| 2554 | case XML_PARSER_ENTITY_VALUE: |
| 2555 | /* |
| 2556 | * NOTE: in the case of entity values, we don't do the |
| 2557 | * substitution here since we need the literal |
| 2558 | * entity value to be able to save the internal |
| 2559 | * subset of the document. |
| 2560 | * This will be handled by xmlStringDecodeEntities |
| 2561 | */ |
| 2562 | return; |
| 2563 | case XML_PARSER_DTD: |
| 2564 | /* |
| 2565 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2566 | * In the internal DTD subset, parameter-entity references |
| 2567 | * can occur only where markup declarations can occur, not |
| 2568 | * within markup declarations. |
| 2569 | * In that case this is handled in xmlParseMarkupDecl |
| 2570 | */ |
| 2571 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2572 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2573 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2574 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2575 | break; |
| 2576 | case XML_PARSER_IGNORE: |
| 2577 | return; |
| 2578 | } |
| 2579 | |
| 2580 | NEXT; |
| 2581 | name = xmlParseName(ctxt); |
| 2582 | if (xmlParserDebugEntities) |
| 2583 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2584 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2586 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2587 | } else { |
| 2588 | if (RAW == ';') { |
| 2589 | NEXT; |
| 2590 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2591 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 2592 | if (ctxt->instate == XML_PARSER_EOF) |
| 2593 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2594 | if (entity == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2595 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2596 | /* |
| 2597 | * [ WFC: Entity Declared ] |
| 2598 | * In a document without any DTD, a document with only an |
| 2599 | * internal DTD subset which contains no parameter entity |
| 2600 | * references, or a document with "standalone='yes'", ... |
| 2601 | * ... The declaration of a parameter entity must precede |
| 2602 | * any reference to it... |
| 2603 | */ |
| 2604 | if ((ctxt->standalone == 1) || |
| 2605 | ((ctxt->hasExternalSubset == 0) && |
| 2606 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2607 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2608 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2609 | } else { |
| 2610 | /* |
| 2611 | * [ VC: Entity Declared ] |
| 2612 | * In a document with an external subset or external |
| 2613 | * parameter entities with "standalone='no'", ... |
| 2614 | * ... The declaration of a parameter entity must precede |
| 2615 | * any reference to it... |
| 2616 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2617 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2618 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2619 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2620 | name, NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2621 | } else |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2622 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2623 | "PEReference: %%%s; not found\n", |
| 2624 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2625 | ctxt->valid = 0; |
| 2626 | } |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 2627 | xmlParserEntityCheck(ctxt, 0, NULL, 0); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2628 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2629 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2630 | if (xmlPushInput(ctxt, input) < 0) |
| 2631 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | } else { |
| 2633 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2634 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2635 | xmlChar start[4]; |
| 2636 | xmlCharEncoding enc; |
| 2637 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2638 | /* |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2639 | * Note: external parameter entities will not be loaded, it |
| 2640 | * is not required for a non-validating parser, unless the |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2641 | * option of validating, or substituting entities were |
| 2642 | * given. Doing so is far more secure as the parser will |
| 2643 | * only process data coming from the document entity by |
| 2644 | * default. |
| 2645 | */ |
| 2646 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 2647 | ((ctxt->options & XML_PARSE_NOENT) == 0) && |
| 2648 | ((ctxt->options & XML_PARSE_DTDVALID) == 0) && |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2649 | ((ctxt->options & XML_PARSE_DTDLOAD) == 0) && |
| 2650 | ((ctxt->options & XML_PARSE_DTDATTR) == 0) && |
| 2651 | (ctxt->replaceEntities == 0) && |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2652 | (ctxt->validate == 0)) |
| 2653 | return; |
| 2654 | |
| 2655 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2656 | * handle the extra spaces added before and after |
| 2657 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2658 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2659 | */ |
| 2660 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2661 | if (xmlPushInput(ctxt, input) < 0) |
| 2662 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2663 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2664 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2665 | * Get the 4 first bytes and decode the charset |
| 2666 | * if enc != XML_CHAR_ENCODING_NONE |
| 2667 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2668 | * Note that, since we may have some non-UTF8 |
| 2669 | * encoding (like UTF16, bug 135229), the 'length' |
| 2670 | * is not known, but we can calculate based upon |
| 2671 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2672 | */ |
| 2673 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2674 | if (ctxt->instate == XML_PARSER_EOF) |
| 2675 | return; |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2676 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2677 | start[0] = RAW; |
| 2678 | start[1] = NXT(1); |
| 2679 | start[2] = NXT(2); |
| 2680 | start[3] = NXT(3); |
| 2681 | enc = xmlDetectCharEncoding(start, 4); |
| 2682 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2683 | xmlSwitchEncoding(ctxt, enc); |
| 2684 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2687 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2688 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2689 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2690 | xmlParseTextDecl(ctxt); |
| 2691 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2692 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2693 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2694 | "PEReference: %s is not a parameter entity\n", |
| 2695 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2699 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * Macro used to grow the current buffer. |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2706 | * buffer##_size is expected to be a size_t |
| 2707 | * mem_error: is expected to handle memory allocation failures |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2708 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2709 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2710 | xmlChar *tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2711 | size_t new_size = buffer##_size * 2 + n; \ |
| 2712 | if (new_size < buffer##_size) goto mem_error; \ |
| 2713 | tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2714 | if (tmp == NULL) goto mem_error; \ |
| 2715 | buffer = tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2716 | buffer##_size = new_size; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2720 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2721 | * @ctxt: the parser context |
| 2722 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2723 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2724 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2725 | * @end: an end marker xmlChar, 0 if none |
| 2726 | * @end2: an end marker xmlChar, 0 if none |
| 2727 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2728 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2729 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | * |
| 2731 | * [67] Reference ::= EntityRef | CharRef |
| 2732 | * |
| 2733 | * [69] PEReference ::= '%' Name ';' |
| 2734 | * |
| 2735 | * Returns A newly allocated string with the substitution done. The caller |
| 2736 | * must deallocate it ! |
| 2737 | */ |
| 2738 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2739 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2740 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | xmlChar *buffer = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2742 | size_t buffer_size = 0; |
| 2743 | size_t nbchars = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2744 | |
| 2745 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2746 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2747 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2748 | xmlEntityPtr ent; |
| 2749 | int c,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2750 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2751 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2753 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2754 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2755 | if (((ctxt->depth > 40) && |
| 2756 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2757 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2758 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2759 | return(NULL); |
| 2760 | } |
| 2761 | |
| 2762 | /* |
| 2763 | * allocate a translation buffer. |
| 2764 | */ |
| 2765 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2766 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2767 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2768 | |
| 2769 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2770 | * 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] | 2771 | * we are operating on already parsed values. |
| 2772 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2773 | if (str < last) |
| 2774 | c = CUR_SCHAR(str, l); |
| 2775 | else |
| 2776 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2777 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2778 | (c != end2) && (c != end3)) { |
| 2779 | |
| 2780 | if (c == 0) break; |
| 2781 | if ((c == '&') && (str[1] == '#')) { |
| 2782 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2783 | if (val != 0) { |
| 2784 | COPY_BUF(0,buffer,nbchars,val); |
| 2785 | } |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2786 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2787 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2788 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2789 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2790 | if (xmlParserDebugEntities) |
| 2791 | xmlGenericError(xmlGenericErrorContext, |
| 2792 | "String decoding Entity Reference: %.30s\n", |
| 2793 | str); |
| 2794 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2795 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2796 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2797 | goto int_error; |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 2798 | xmlParserEntityCheck(ctxt, 0, ent, 0); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2799 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2800 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2801 | if ((ent != NULL) && |
| 2802 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2803 | if (ent->content != NULL) { |
| 2804 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2805 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2806 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2807 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2808 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2809 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2810 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2811 | } |
| 2812 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2813 | ctxt->depth++; |
| 2814 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2815 | 0, 0, 0); |
| 2816 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2817 | |
David Drysdale | 6903071 | 2015-11-20 11:13:45 +0800 | [diff] [blame] | 2818 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2819 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
| 2820 | goto int_error; |
| 2821 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2822 | if (rep != NULL) { |
| 2823 | current = rep; |
| 2824 | while (*current != 0) { /* non input consuming loop */ |
| 2825 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2826 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2827 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2828 | goto int_error; |
| 2829 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2833 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } else if (ent != NULL) { |
| 2836 | int i = xmlStrlen(ent->name); |
| 2837 | const xmlChar *cur = ent->name; |
| 2838 | |
| 2839 | buffer[nbchars++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2840 | if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 5bd3c06 | 2011-12-16 18:53:35 +0800 | [diff] [blame] | 2841 | growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2842 | } |
| 2843 | for (;i > 0;i--) |
| 2844 | buffer[nbchars++] = *cur++; |
| 2845 | buffer[nbchars++] = ';'; |
| 2846 | } |
| 2847 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2848 | if (xmlParserDebugEntities) |
| 2849 | xmlGenericError(xmlGenericErrorContext, |
| 2850 | "String decoding PE Reference: %.30s\n", str); |
| 2851 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2852 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2853 | goto int_error; |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 2854 | xmlParserEntityCheck(ctxt, 0, ent, 0); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2855 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2856 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2857 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2858 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2859 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2860 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | ctxt->depth++; |
| 2862 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2863 | 0, 0, 0); |
| 2864 | ctxt->depth--; |
| 2865 | if (rep != NULL) { |
| 2866 | current = rep; |
| 2867 | while (*current != 0) { /* non input consuming loop */ |
| 2868 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2869 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2870 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2871 | goto int_error; |
| 2872 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2876 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | } else { |
| 2880 | COPY_BUF(l,buffer,nbchars,c); |
| 2881 | str += l; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2882 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
| 2883 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2884 | } |
| 2885 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2886 | if (str < last) |
| 2887 | c = CUR_SCHAR(str, l); |
| 2888 | else |
| 2889 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2890 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2891 | buffer[nbchars] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2892 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2893 | |
| 2894 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2895 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2896 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2897 | if (rep != NULL) |
| 2898 | xmlFree(rep); |
| 2899 | if (buffer != NULL) |
| 2900 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2901 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2904 | /** |
| 2905 | * xmlStringDecodeEntities: |
| 2906 | * @ctxt: the parser context |
| 2907 | * @str: the input string |
| 2908 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2909 | * @end: an end marker xmlChar, 0 if none |
| 2910 | * @end2: an end marker xmlChar, 0 if none |
| 2911 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2912 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2913 | * Takes a entity string content and process to do the adequate substitutions. |
| 2914 | * |
| 2915 | * [67] Reference ::= EntityRef | CharRef |
| 2916 | * |
| 2917 | * [69] PEReference ::= '%' Name ';' |
| 2918 | * |
| 2919 | * Returns A newly allocated string with the substitution done. The caller |
| 2920 | * must deallocate it ! |
| 2921 | */ |
| 2922 | xmlChar * |
| 2923 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2924 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2925 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2926 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2927 | end, end2, end3)); |
| 2928 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2929 | |
| 2930 | /************************************************************************ |
| 2931 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2932 | * Commodity functions, cleanup needed ? * |
| 2933 | * * |
| 2934 | ************************************************************************/ |
| 2935 | |
| 2936 | /** |
| 2937 | * areBlanks: |
| 2938 | * @ctxt: an XML parser context |
| 2939 | * @str: a xmlChar * |
| 2940 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2941 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2942 | * |
| 2943 | * Is this a sequence of blank chars that one can ignore ? |
| 2944 | * |
| 2945 | * Returns 1 if ignorable 0 otherwise. |
| 2946 | */ |
| 2947 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2948 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2949 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2950 | int i, ret; |
| 2951 | xmlNodePtr lastChild; |
| 2952 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2953 | /* |
| 2954 | * Don't spend time trying to differentiate them, the same callback is |
| 2955 | * used ! |
| 2956 | */ |
| 2957 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2958 | return(0); |
| 2959 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2960 | /* |
| 2961 | * Check for xml:space value. |
| 2962 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2963 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2964 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2965 | return(0); |
| 2966 | |
| 2967 | /* |
| 2968 | * Check that the string is made of blanks |
| 2969 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2970 | if (blank_chars == 0) { |
| 2971 | for (i = 0;i < len;i++) |
| 2972 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2973 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2974 | |
| 2975 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2976 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2977 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2978 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2979 | if (ctxt->myDoc != NULL) { |
| 2980 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2981 | if (ret == 0) return(1); |
| 2982 | if (ret == 1) return(0); |
| 2983 | } |
| 2984 | |
| 2985 | /* |
| 2986 | * Otherwise, heuristic :-\ |
| 2987 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2988 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2989 | if ((ctxt->node->children == NULL) && |
| 2990 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2991 | |
| 2992 | lastChild = xmlGetLastChild(ctxt->node); |
| 2993 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2994 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2995 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | } else if (xmlNodeIsText(lastChild)) |
| 2997 | return(0); |
| 2998 | else if ((ctxt->node->children != NULL) && |
| 2999 | (xmlNodeIsText(ctxt->node->children))) |
| 3000 | return(0); |
| 3001 | return(1); |
| 3002 | } |
| 3003 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | /************************************************************************ |
| 3005 | * * |
| 3006 | * Extra stuff for namespace support * |
| 3007 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 3008 | * * |
| 3009 | ************************************************************************/ |
| 3010 | |
| 3011 | /** |
| 3012 | * xmlSplitQName: |
| 3013 | * @ctxt: an XML parser context |
| 3014 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3015 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3016 | * |
| 3017 | * parse an UTF8 encoded XML qualified name string |
| 3018 | * |
| 3019 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 3020 | * |
| 3021 | * [NS 6] Prefix ::= NCName |
| 3022 | * |
| 3023 | * [NS 7] LocalPart ::= NCName |
| 3024 | * |
| 3025 | * Returns the local part, and prefix is updated |
| 3026 | * to get the Prefix if any. |
| 3027 | */ |
| 3028 | |
| 3029 | xmlChar * |
| 3030 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 3031 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3032 | xmlChar *buffer = NULL; |
| 3033 | int len = 0; |
| 3034 | int max = XML_MAX_NAMELEN; |
| 3035 | xmlChar *ret = NULL; |
| 3036 | const xmlChar *cur = name; |
| 3037 | int c; |
| 3038 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 3039 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3040 | *prefix = NULL; |
| 3041 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3042 | if (cur == NULL) return(NULL); |
| 3043 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | #ifndef XML_XML_NAMESPACE |
| 3045 | /* xml: prefix is not really a namespace */ |
| 3046 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 3047 | (cur[2] == 'l') && (cur[3] == ':')) |
| 3048 | return(xmlStrdup(name)); |
| 3049 | #endif |
| 3050 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3051 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3052 | if (cur[0] == ':') |
| 3053 | return(xmlStrdup(name)); |
| 3054 | |
| 3055 | c = *cur++; |
| 3056 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 3057 | buf[len++] = c; |
| 3058 | c = *cur++; |
| 3059 | } |
| 3060 | if (len >= max) { |
| 3061 | /* |
| 3062 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3063 | * for the processing speed. |
| 3064 | */ |
| 3065 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3066 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3067 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3068 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3069 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3070 | return(NULL); |
| 3071 | } |
| 3072 | memcpy(buffer, buf, len); |
| 3073 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 3074 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3075 | xmlChar *tmp; |
| 3076 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3077 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3078 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3079 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3080 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3081 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3082 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3083 | return(NULL); |
| 3084 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3085 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3086 | } |
| 3087 | buffer[len++] = c; |
| 3088 | c = *cur++; |
| 3089 | } |
| 3090 | buffer[len] = 0; |
| 3091 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3092 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3093 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3094 | if (buffer != NULL) |
| 3095 | xmlFree(buffer); |
| 3096 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3097 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3098 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3099 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3100 | if (buffer == NULL) |
| 3101 | ret = xmlStrndup(buf, len); |
| 3102 | else { |
| 3103 | ret = buffer; |
| 3104 | buffer = NULL; |
| 3105 | max = XML_MAX_NAMELEN; |
| 3106 | } |
| 3107 | |
| 3108 | |
| 3109 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3110 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3111 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3112 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 3113 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3114 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3115 | len = 0; |
| 3116 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3117 | /* |
| 3118 | * Check that the first character is proper to start |
| 3119 | * a new name |
| 3120 | */ |
| 3121 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 3122 | ((c >= 0x41) && (c <= 0x5A)) || |
| 3123 | (c == '_') || (c == ':'))) { |
| 3124 | int l; |
| 3125 | int first = CUR_SCHAR(cur, l); |
| 3126 | |
| 3127 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3128 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3129 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3130 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3131 | } |
| 3132 | } |
| 3133 | cur++; |
| 3134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3135 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 3136 | buf[len++] = c; |
| 3137 | c = *cur++; |
| 3138 | } |
| 3139 | if (len >= max) { |
| 3140 | /* |
| 3141 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3142 | * for the processing speed. |
| 3143 | */ |
| 3144 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3145 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3146 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3147 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3148 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3149 | return(NULL); |
| 3150 | } |
| 3151 | memcpy(buffer, buf, len); |
| 3152 | while (c != 0) { /* tested bigname2.xml */ |
| 3153 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3154 | xmlChar *tmp; |
| 3155 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3156 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3157 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3158 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3159 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3160 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3161 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3162 | return(NULL); |
| 3163 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3164 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3165 | } |
| 3166 | buffer[len++] = c; |
| 3167 | c = *cur++; |
| 3168 | } |
| 3169 | buffer[len] = 0; |
| 3170 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3172 | if (buffer == NULL) |
| 3173 | ret = xmlStrndup(buf, len); |
| 3174 | else { |
| 3175 | ret = buffer; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | return(ret); |
| 3180 | } |
| 3181 | |
| 3182 | /************************************************************************ |
| 3183 | * * |
| 3184 | * The parser itself * |
| 3185 | * Relates to http://www.w3.org/TR/REC-xml * |
| 3186 | * * |
| 3187 | ************************************************************************/ |
| 3188 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3189 | /************************************************************************ |
| 3190 | * * |
| 3191 | * Routines to parse Name, NCName and NmToken * |
| 3192 | * * |
| 3193 | ************************************************************************/ |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3194 | #ifdef DEBUG |
| 3195 | static unsigned long nbParseName = 0; |
| 3196 | static unsigned long nbParseNmToken = 0; |
| 3197 | static unsigned long nbParseNCName = 0; |
| 3198 | static unsigned long nbParseNCNameComplex = 0; |
| 3199 | static unsigned long nbParseNameComplex = 0; |
| 3200 | static unsigned long nbParseStringName = 0; |
| 3201 | #endif |
| 3202 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3203 | /* |
| 3204 | * The two following functions are related to the change of accepted |
| 3205 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 3206 | * They correspond to the modified production [4] and the new production [4a] |
| 3207 | * changes in that revision. Also note that the macros used for the |
| 3208 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 3209 | * anymore. |
| 3210 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 3211 | * new XML_PARSE_OLD10 option is given to the parser. |
| 3212 | */ |
| 3213 | static int |
| 3214 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 3215 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3216 | /* |
| 3217 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3218 | * Update 5 of XML-1.0 |
| 3219 | */ |
| 3220 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3221 | (((c >= 'a') && (c <= 'z')) || |
| 3222 | ((c >= 'A') && (c <= 'Z')) || |
| 3223 | (c == '_') || (c == ':') || |
| 3224 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3225 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3226 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3227 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3228 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3229 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3230 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3231 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3232 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3233 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3234 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3235 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3236 | return(1); |
| 3237 | } else { |
| 3238 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3239 | return(1); |
| 3240 | } |
| 3241 | return(0); |
| 3242 | } |
| 3243 | |
| 3244 | static int |
| 3245 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 3246 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3247 | /* |
| 3248 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3249 | * Update 5 of XML-1.0 |
| 3250 | */ |
| 3251 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3252 | (((c >= 'a') && (c <= 'z')) || |
| 3253 | ((c >= 'A') && (c <= 'Z')) || |
| 3254 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3255 | (c == '_') || (c == ':') || |
| 3256 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3257 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3258 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3259 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3260 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3261 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3262 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3263 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3264 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3265 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3266 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3267 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3268 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3269 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3270 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3271 | return(1); |
| 3272 | } else { |
| 3273 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3274 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3275 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3276 | (IS_COMBINING(c)) || |
| 3277 | (IS_EXTENDER(c))) |
| 3278 | return(1); |
| 3279 | } |
| 3280 | return(0); |
| 3281 | } |
| 3282 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3283 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3284 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3285 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3286 | static const xmlChar * |
| 3287 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3288 | int len = 0, l; |
| 3289 | int c; |
| 3290 | int count = 0; |
| 3291 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3292 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3293 | nbParseNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3294 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3295 | |
| 3296 | /* |
| 3297 | * Handler for more complex cases |
| 3298 | */ |
| 3299 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3300 | if (ctxt->instate == XML_PARSER_EOF) |
| 3301 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3302 | c = CUR_CHAR(l); |
| 3303 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3304 | /* |
| 3305 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3306 | * Update 5 of XML-1.0 |
| 3307 | */ |
| 3308 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3309 | (!(((c >= 'a') && (c <= 'z')) || |
| 3310 | ((c >= 'A') && (c <= 'Z')) || |
| 3311 | (c == '_') || (c == ':') || |
| 3312 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3313 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3314 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3315 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3316 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3317 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3318 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3319 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3320 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3321 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3322 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3323 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3324 | return(NULL); |
| 3325 | } |
| 3326 | len += l; |
| 3327 | NEXTL(l); |
| 3328 | c = CUR_CHAR(l); |
| 3329 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3330 | (((c >= 'a') && (c <= 'z')) || |
| 3331 | ((c >= 'A') && (c <= 'Z')) || |
| 3332 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3333 | (c == '_') || (c == ':') || |
| 3334 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3335 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3336 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3337 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3338 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3339 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3340 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3341 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3342 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3343 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3344 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3345 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3346 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3347 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3348 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3349 | )) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3350 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3351 | count = 0; |
| 3352 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3353 | if (ctxt->instate == XML_PARSER_EOF) |
| 3354 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3355 | } |
| 3356 | len += l; |
| 3357 | NEXTL(l); |
| 3358 | c = CUR_CHAR(l); |
| 3359 | } |
| 3360 | } else { |
| 3361 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3362 | (!IS_LETTER(c) && (c != '_') && |
| 3363 | (c != ':'))) { |
| 3364 | return(NULL); |
| 3365 | } |
| 3366 | len += l; |
| 3367 | NEXTL(l); |
| 3368 | c = CUR_CHAR(l); |
| 3369 | |
| 3370 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3371 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3372 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3373 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3374 | (IS_COMBINING(c)) || |
| 3375 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3376 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3377 | count = 0; |
| 3378 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3379 | if (ctxt->instate == XML_PARSER_EOF) |
| 3380 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3381 | } |
| 3382 | len += l; |
| 3383 | NEXTL(l); |
| 3384 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3385 | if (c == 0) { |
| 3386 | count = 0; |
| 3387 | GROW; |
| 3388 | if (ctxt->instate == XML_PARSER_EOF) |
| 3389 | return(NULL); |
| 3390 | c = CUR_CHAR(l); |
| 3391 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3394 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3395 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3396 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3397 | return(NULL); |
| 3398 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3399 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3400 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3401 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3402 | } |
| 3403 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3404 | /** |
| 3405 | * xmlParseName: |
| 3406 | * @ctxt: an XML parser context |
| 3407 | * |
| 3408 | * parse an XML name. |
| 3409 | * |
| 3410 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3411 | * CombiningChar | Extender |
| 3412 | * |
| 3413 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3414 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3415 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3416 | * |
| 3417 | * Returns the Name parsed or NULL |
| 3418 | */ |
| 3419 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3420 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3421 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3422 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3423 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3424 | int count = 0; |
| 3425 | |
| 3426 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3427 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3428 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3429 | nbParseName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3430 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3431 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3432 | /* |
| 3433 | * Accelerator for simple ASCII names |
| 3434 | */ |
| 3435 | in = ctxt->input->cur; |
| 3436 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3437 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3438 | (*in == '_') || (*in == ':')) { |
| 3439 | in++; |
| 3440 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3441 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3442 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3443 | (*in == '_') || (*in == '-') || |
| 3444 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3445 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3446 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3447 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3448 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3449 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3450 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3451 | return(NULL); |
| 3452 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3453 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3454 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3455 | ctxt->nbChars += count; |
| 3456 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3457 | if (ret == NULL) |
| 3458 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3459 | return(ret); |
| 3460 | } |
| 3461 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3462 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3463 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3464 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3465 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3466 | static const xmlChar * |
| 3467 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3468 | int len = 0, l; |
| 3469 | int c; |
| 3470 | int count = 0; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3471 | const xmlChar *end; /* needed because CUR_CHAR() can move cur on \r\n */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3472 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3473 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3474 | nbParseNCNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3475 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3476 | |
| 3477 | /* |
| 3478 | * Handler for more complex cases |
| 3479 | */ |
| 3480 | GROW; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3481 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3482 | c = CUR_CHAR(l); |
| 3483 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3484 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3485 | return(NULL); |
| 3486 | } |
| 3487 | |
| 3488 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3489 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3490 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3491 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3492 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3493 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3494 | return(NULL); |
| 3495 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3496 | count = 0; |
| 3497 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3498 | if (ctxt->instate == XML_PARSER_EOF) |
| 3499 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3500 | } |
| 3501 | len += l; |
| 3502 | NEXTL(l); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3503 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3504 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3505 | if (c == 0) { |
| 3506 | count = 0; |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3507 | /* |
| 3508 | * when shrinking to extend the buffer we really need to preserve |
| 3509 | * the part of the name we already parsed. Hence rolling back |
| 3510 | * by current lenght. |
| 3511 | */ |
| 3512 | ctxt->input->cur -= l; |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3513 | GROW; |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3514 | ctxt->input->cur += l; |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3515 | if (ctxt->instate == XML_PARSER_EOF) |
| 3516 | return(NULL); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3517 | end = ctxt->input->cur; |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3518 | c = CUR_CHAR(l); |
| 3519 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3520 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3521 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3522 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3523 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3524 | return(NULL); |
| 3525 | } |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3526 | return(xmlDictLookup(ctxt->dict, end - len, len)); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | /** |
| 3530 | * xmlParseNCName: |
| 3531 | * @ctxt: an XML parser context |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3532 | * @len: length of the string parsed |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3533 | * |
| 3534 | * parse an XML name. |
| 3535 | * |
| 3536 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3537 | * CombiningChar | Extender |
| 3538 | * |
| 3539 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3540 | * |
| 3541 | * Returns the Name parsed or NULL |
| 3542 | */ |
| 3543 | |
| 3544 | static const xmlChar * |
| 3545 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3546 | const xmlChar *in, *e; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3547 | const xmlChar *ret; |
| 3548 | int count = 0; |
| 3549 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3550 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3551 | nbParseNCName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3552 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3553 | |
| 3554 | /* |
| 3555 | * Accelerator for simple ASCII names |
| 3556 | */ |
| 3557 | in = ctxt->input->cur; |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3558 | e = ctxt->input->end; |
| 3559 | if ((((*in >= 0x61) && (*in <= 0x7A)) || |
| 3560 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3561 | (*in == '_')) && (in < e)) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3562 | in++; |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3563 | while ((((*in >= 0x61) && (*in <= 0x7A)) || |
| 3564 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3565 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3566 | (*in == '_') || (*in == '-') || |
| 3567 | (*in == '.')) && (in < e)) |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3568 | in++; |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3569 | if (in >= e) |
| 3570 | goto complex; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3571 | if ((*in > 0) && (*in < 0x80)) { |
| 3572 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3573 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3574 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3575 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3576 | return(NULL); |
| 3577 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3578 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3579 | ctxt->input->cur = in; |
| 3580 | ctxt->nbChars += count; |
| 3581 | ctxt->input->col += count; |
| 3582 | if (ret == NULL) { |
| 3583 | xmlErrMemory(ctxt, NULL); |
| 3584 | } |
| 3585 | return(ret); |
| 3586 | } |
| 3587 | } |
Daniel Veillard | 51f02b0 | 2015-09-15 16:50:32 +0800 | [diff] [blame] | 3588 | complex: |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3589 | return(xmlParseNCNameComplex(ctxt)); |
| 3590 | } |
| 3591 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3592 | /** |
| 3593 | * xmlParseNameAndCompare: |
| 3594 | * @ctxt: an XML parser context |
| 3595 | * |
| 3596 | * parse an XML name and compares for match |
| 3597 | * (specialized for endtag parsing) |
| 3598 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3599 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3600 | * and the name for mismatch |
| 3601 | */ |
| 3602 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3603 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3604 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3605 | register const xmlChar *cmp = other; |
| 3606 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3607 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3608 | |
| 3609 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3610 | if (ctxt->instate == XML_PARSER_EOF) |
| 3611 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3612 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3613 | in = ctxt->input->cur; |
| 3614 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3615 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3616 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3617 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3618 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3619 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3620 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3621 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3622 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3623 | } |
| 3624 | /* failure (or end of input buffer), check with full function */ |
| 3625 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3626 | /* strings coming from the dictionnary direct compare possible */ |
| 3627 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3628 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3629 | } |
| 3630 | return ret; |
| 3631 | } |
| 3632 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3633 | /** |
| 3634 | * xmlParseStringName: |
| 3635 | * @ctxt: an XML parser context |
| 3636 | * @str: a pointer to the string pointer (IN/OUT) |
| 3637 | * |
| 3638 | * parse an XML name. |
| 3639 | * |
| 3640 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3641 | * CombiningChar | Extender |
| 3642 | * |
| 3643 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3644 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3645 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3646 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3647 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3648 | * is updated to the current location in the string. |
| 3649 | */ |
| 3650 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3651 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3652 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3653 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3654 | const xmlChar *cur = *str; |
| 3655 | int len = 0, l; |
| 3656 | int c; |
| 3657 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3658 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3659 | nbParseStringName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3660 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3661 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3662 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3663 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3664 | return(NULL); |
| 3665 | } |
| 3666 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3667 | COPY_BUF(l,buf,len,c); |
| 3668 | cur += l; |
| 3669 | c = CUR_SCHAR(cur, l); |
| 3670 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3671 | COPY_BUF(l,buf,len,c); |
| 3672 | cur += l; |
| 3673 | c = CUR_SCHAR(cur, l); |
| 3674 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3675 | /* |
| 3676 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3677 | * for the processing speed. |
| 3678 | */ |
| 3679 | xmlChar *buffer; |
| 3680 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3681 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3682 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3683 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3684 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3685 | return(NULL); |
| 3686 | } |
| 3687 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3688 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3689 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3690 | xmlChar *tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3691 | |
| 3692 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3693 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3694 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3695 | xmlFree(buffer); |
| 3696 | return(NULL); |
| 3697 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3698 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3699 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3700 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3701 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3702 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3703 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3704 | return(NULL); |
| 3705 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3706 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3707 | } |
| 3708 | COPY_BUF(l,buffer,len,c); |
| 3709 | cur += l; |
| 3710 | c = CUR_SCHAR(cur, l); |
| 3711 | } |
| 3712 | buffer[len] = 0; |
| 3713 | *str = cur; |
| 3714 | return(buffer); |
| 3715 | } |
| 3716 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3717 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3718 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3719 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3720 | return(NULL); |
| 3721 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3722 | *str = cur; |
| 3723 | return(xmlStrndup(buf, len)); |
| 3724 | } |
| 3725 | |
| 3726 | /** |
| 3727 | * xmlParseNmtoken: |
| 3728 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3729 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3730 | * parse an XML Nmtoken. |
| 3731 | * |
| 3732 | * [7] Nmtoken ::= (NameChar)+ |
| 3733 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3734 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3735 | * |
| 3736 | * Returns the Nmtoken parsed or NULL |
| 3737 | */ |
| 3738 | |
| 3739 | xmlChar * |
| 3740 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3741 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3742 | int len = 0, l; |
| 3743 | int c; |
| 3744 | int count = 0; |
| 3745 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3746 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3747 | nbParseNmToken++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3748 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3749 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3750 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3751 | if (ctxt->instate == XML_PARSER_EOF) |
| 3752 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3753 | c = CUR_CHAR(l); |
| 3754 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3755 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3756 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3757 | count = 0; |
| 3758 | GROW; |
| 3759 | } |
| 3760 | COPY_BUF(l,buf,len,c); |
| 3761 | NEXTL(l); |
| 3762 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3763 | if (c == 0) { |
| 3764 | count = 0; |
| 3765 | GROW; |
| 3766 | if (ctxt->instate == XML_PARSER_EOF) |
| 3767 | return(NULL); |
| 3768 | c = CUR_CHAR(l); |
| 3769 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3770 | if (len >= XML_MAX_NAMELEN) { |
| 3771 | /* |
| 3772 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3773 | * for the processing speed. |
| 3774 | */ |
| 3775 | xmlChar *buffer; |
| 3776 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3777 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3778 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3779 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3780 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3781 | return(NULL); |
| 3782 | } |
| 3783 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3784 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3785 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3786 | count = 0; |
| 3787 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3788 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3789 | xmlFree(buffer); |
| 3790 | return(NULL); |
| 3791 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3792 | } |
| 3793 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3794 | xmlChar *tmp; |
| 3795 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3796 | if ((max > XML_MAX_NAME_LENGTH) && |
| 3797 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3798 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3799 | xmlFree(buffer); |
| 3800 | return(NULL); |
| 3801 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3802 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3803 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3804 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3805 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3806 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3807 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3808 | return(NULL); |
| 3809 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3810 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3811 | } |
| 3812 | COPY_BUF(l,buffer,len,c); |
| 3813 | NEXTL(l); |
| 3814 | c = CUR_CHAR(l); |
| 3815 | } |
| 3816 | buffer[len] = 0; |
| 3817 | return(buffer); |
| 3818 | } |
| 3819 | } |
| 3820 | if (len == 0) |
| 3821 | return(NULL); |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3822 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3823 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3824 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3825 | return(NULL); |
| 3826 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3827 | return(xmlStrndup(buf, len)); |
| 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * xmlParseEntityValue: |
| 3832 | * @ctxt: an XML parser context |
| 3833 | * @orig: if non-NULL store a copy of the original entity value |
| 3834 | * |
| 3835 | * parse a value for ENTITY declarations |
| 3836 | * |
| 3837 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3838 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3839 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3840 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3841 | */ |
| 3842 | |
| 3843 | xmlChar * |
| 3844 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3845 | xmlChar *buf = NULL; |
| 3846 | int len = 0; |
| 3847 | int size = XML_PARSER_BUFFER_SIZE; |
| 3848 | int c, l; |
| 3849 | xmlChar stop; |
| 3850 | xmlChar *ret = NULL; |
| 3851 | const xmlChar *cur = NULL; |
| 3852 | xmlParserInputPtr input; |
| 3853 | |
| 3854 | if (RAW == '"') stop = '"'; |
| 3855 | else if (RAW == '\'') stop = '\''; |
| 3856 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3857 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3858 | return(NULL); |
| 3859 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3860 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3862 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3863 | return(NULL); |
| 3864 | } |
| 3865 | |
| 3866 | /* |
| 3867 | * The content of the entity definition is copied in a buffer. |
| 3868 | */ |
| 3869 | |
| 3870 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3871 | input = ctxt->input; |
| 3872 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3873 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3874 | xmlFree(buf); |
| 3875 | return(NULL); |
| 3876 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3877 | NEXT; |
| 3878 | c = CUR_CHAR(l); |
| 3879 | /* |
| 3880 | * NOTE: 4.4.5 Included in Literal |
| 3881 | * When a parameter entity reference appears in a literal entity |
| 3882 | * value, ... a single or double quote character in the replacement |
| 3883 | * text is always treated as a normal data character and will not |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3884 | * terminate the literal. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3885 | * In practice it means we stop the loop only when back at parsing |
| 3886 | * the initial entity and the quote is found |
| 3887 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3888 | while (((IS_CHAR(c)) && ((c != stop) || /* checked */ |
| 3889 | (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3890 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3891 | xmlChar *tmp; |
| 3892 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3893 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3894 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3895 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3896 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3897 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3898 | return(NULL); |
| 3899 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3900 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3901 | } |
| 3902 | COPY_BUF(l,buf,len,c); |
| 3903 | NEXTL(l); |
| 3904 | /* |
| 3905 | * Pop-up of finished entities. |
| 3906 | */ |
| 3907 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3908 | xmlPopInput(ctxt); |
| 3909 | |
| 3910 | GROW; |
| 3911 | c = CUR_CHAR(l); |
| 3912 | if (c == 0) { |
| 3913 | GROW; |
| 3914 | c = CUR_CHAR(l); |
| 3915 | } |
| 3916 | } |
| 3917 | buf[len] = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3918 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3919 | xmlFree(buf); |
| 3920 | return(NULL); |
| 3921 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3922 | |
| 3923 | /* |
| 3924 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3925 | * reference constructs. Note Charref will be handled in |
| 3926 | * xmlStringDecodeEntities() |
| 3927 | */ |
| 3928 | cur = buf; |
| 3929 | while (*cur != 0) { /* non input consuming */ |
| 3930 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3931 | xmlChar *name; |
| 3932 | xmlChar tmp = *cur; |
| 3933 | |
| 3934 | cur++; |
| 3935 | name = xmlParseStringName(ctxt, &cur); |
| 3936 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3937 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3939 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3941 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3942 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3943 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3944 | } |
| 3945 | if (name != NULL) |
| 3946 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3947 | if (*cur == 0) |
| 3948 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3949 | } |
| 3950 | cur++; |
| 3951 | } |
| 3952 | |
| 3953 | /* |
| 3954 | * Then PEReference entities are substituted. |
| 3955 | */ |
| 3956 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3957 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3958 | xmlFree(buf); |
| 3959 | } else { |
| 3960 | NEXT; |
| 3961 | /* |
| 3962 | * NOTE: 4.4.7 Bypassed |
| 3963 | * When a general entity reference appears in the EntityValue in |
| 3964 | * an entity declaration, it is bypassed and left as is. |
| 3965 | * so XML_SUBSTITUTE_REF is not set here. |
| 3966 | */ |
| 3967 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3968 | 0, 0, 0); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3969 | if (orig != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3970 | *orig = buf; |
| 3971 | else |
| 3972 | xmlFree(buf); |
| 3973 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3974 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3975 | return(ret); |
| 3976 | } |
| 3977 | |
| 3978 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3979 | * xmlParseAttValueComplex: |
| 3980 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3981 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3982 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3983 | * |
| 3984 | * parse a value for an attribute, this is the fallback function |
| 3985 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3986 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3987 | * |
| 3988 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3989 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3990 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3991 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3992 | xmlChar limit = 0; |
| 3993 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3994 | xmlChar *rep = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3995 | size_t len = 0; |
| 3996 | size_t buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3997 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3998 | xmlChar *current = NULL; |
| 3999 | xmlEntityPtr ent; |
| 4000 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4001 | if (NXT(0) == '"') { |
| 4002 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 4003 | limit = '"'; |
| 4004 | NEXT; |
| 4005 | } else if (NXT(0) == '\'') { |
| 4006 | limit = '\''; |
| 4007 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 4008 | NEXT; |
| 4009 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4010 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4011 | return(NULL); |
| 4012 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4013 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | /* |
| 4015 | * allocate a translation buffer. |
| 4016 | */ |
| 4017 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4018 | buf = (xmlChar *) xmlMallocAtomic(buf_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4019 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4020 | |
| 4021 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4022 | * 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] | 4023 | */ |
| 4024 | c = CUR_CHAR(l); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4025 | while (((NXT(0) != limit) && /* checked */ |
| 4026 | (IS_CHAR(c)) && (c != '<')) && |
| 4027 | (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 4028 | /* |
| 4029 | * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE |
| 4030 | * special option is given |
| 4031 | */ |
| 4032 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4033 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4034 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4035 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 4036 | goto mem_error; |
| 4037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4039 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 4040 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4041 | if (NXT(1) == '#') { |
| 4042 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4043 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4044 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 4045 | if (ctxt->replaceEntities) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4046 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4047 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 4048 | } |
| 4049 | buf[len++] = '&'; |
| 4050 | } else { |
| 4051 | /* |
| 4052 | * The reparsing will be done in xmlStringGetNodeList() |
| 4053 | * called by the attribute() function in SAX.c |
| 4054 | */ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4055 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4056 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 4057 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4058 | buf[len++] = '&'; |
| 4059 | buf[len++] = '#'; |
| 4060 | buf[len++] = '3'; |
| 4061 | buf[len++] = '8'; |
| 4062 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4063 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 4064 | } else if (val != 0) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4065 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4066 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4067 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4068 | len += xmlCopyChar(0, &buf[len], val); |
| 4069 | } |
| 4070 | } else { |
| 4071 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 4072 | ctxt->nbentities++; |
| 4073 | if (ent != NULL) |
| 4074 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4075 | if ((ent != NULL) && |
| 4076 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4077 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4078 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4079 | } |
| 4080 | if ((ctxt->replaceEntities == 0) && |
| 4081 | (ent->content[0] == '&')) { |
| 4082 | buf[len++] = '&'; |
| 4083 | buf[len++] = '#'; |
| 4084 | buf[len++] = '3'; |
| 4085 | buf[len++] = '8'; |
| 4086 | buf[len++] = ';'; |
| 4087 | } else { |
| 4088 | buf[len++] = ent->content[0]; |
| 4089 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4090 | } else if ((ent != NULL) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4091 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 4093 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4094 | XML_SUBSTITUTE_REF, |
| 4095 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4096 | if (rep != NULL) { |
| 4097 | current = rep; |
| 4098 | while (*current != 0) { /* non input consuming */ |
Daniel Veillard | 283d502 | 2009-08-25 17:18:39 +0200 | [diff] [blame] | 4099 | if ((*current == 0xD) || (*current == 0xA) || |
| 4100 | (*current == 0x9)) { |
| 4101 | buf[len++] = 0x20; |
| 4102 | current++; |
| 4103 | } else |
| 4104 | buf[len++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4105 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4106 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4110 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4111 | } |
| 4112 | } else { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4113 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4114 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4115 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4116 | if (ent->content != NULL) |
| 4117 | buf[len++] = ent->content[0]; |
| 4118 | } |
| 4119 | } else if (ent != NULL) { |
| 4120 | int i = xmlStrlen(ent->name); |
| 4121 | const xmlChar *cur = ent->name; |
| 4122 | |
| 4123 | /* |
| 4124 | * This may look absurd but is needed to detect |
| 4125 | * entities problems |
| 4126 | */ |
| 4127 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4128 | (ent->content != NULL) && (ent->checked == 0)) { |
| 4129 | unsigned long oldnbent = ctxt->nbentities; |
| 4130 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4131 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4132 | XML_SUBSTITUTE_REF, 0, 0, 0); |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4133 | |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4134 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4135 | if (rep != NULL) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4136 | if (xmlStrchr(rep, '<')) |
| 4137 | ent->checked |= 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4138 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4139 | rep = NULL; |
| 4140 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
| 4143 | /* |
| 4144 | * Just output the reference |
| 4145 | */ |
| 4146 | buf[len++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4147 | while (len + i + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4148 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4149 | } |
| 4150 | for (;i > 0;i--) |
| 4151 | buf[len++] = *cur++; |
| 4152 | buf[len++] = ';'; |
| 4153 | } |
| 4154 | } |
| 4155 | } else { |
| 4156 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4157 | if ((len != 0) || (!normalize)) { |
| 4158 | if ((!normalize) || (!in_space)) { |
| 4159 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4160 | while (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4161 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4162 | } |
| 4163 | } |
| 4164 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4165 | } |
| 4166 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4167 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4168 | COPY_BUF(l,buf,len,c); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4169 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4170 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | NEXTL(l); |
| 4174 | } |
| 4175 | GROW; |
| 4176 | c = CUR_CHAR(l); |
| 4177 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4178 | if (ctxt->instate == XML_PARSER_EOF) |
| 4179 | goto error; |
| 4180 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4181 | if ((in_space) && (normalize)) { |
Daniel Veillard | 6a36fbe | 2012-10-29 10:39:55 +0800 | [diff] [blame] | 4182 | while ((len > 0) && (buf[len - 1] == 0x20)) len--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4183 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4184 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4185 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4186 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4187 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 4188 | if ((c != 0) && (!IS_CHAR(c))) { |
| 4189 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 4190 | "invalid character in attribute value\n"); |
| 4191 | } else { |
| 4192 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 4193 | "AttValue: ' expected\n"); |
| 4194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4195 | } else |
| 4196 | NEXT; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4197 | |
| 4198 | /* |
| 4199 | * There we potentially risk an overflow, don't allow attribute value of |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4200 | * length more than INT_MAX it is a very reasonnable assumption ! |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4201 | */ |
| 4202 | if (len >= INT_MAX) { |
| 4203 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4204 | "AttValue length too long\n"); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4205 | goto mem_error; |
| 4206 | } |
| 4207 | |
| 4208 | if (attlen != NULL) *attlen = (int) len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4210 | |
| 4211 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4212 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4213 | error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4214 | if (buf != NULL) |
| 4215 | xmlFree(buf); |
| 4216 | if (rep != NULL) |
| 4217 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4218 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4222 | * xmlParseAttValue: |
| 4223 | * @ctxt: an XML parser context |
| 4224 | * |
| 4225 | * parse a value for an attribute |
| 4226 | * Note: the parser won't do substitution of entities here, this |
| 4227 | * will be handled later in xmlStringGetNodeList |
| 4228 | * |
| 4229 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 4230 | * "'" ([^<&'] | Reference)* "'" |
| 4231 | * |
| 4232 | * 3.3.3 Attribute-Value Normalization: |
| 4233 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4234 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4235 | * - a character reference is processed by appending the referenced |
| 4236 | * character to the attribute value |
| 4237 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4238 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4239 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 4240 | * appending #x20 to the normalized value, except that only a single |
| 4241 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4242 | * parsed entity or the literal entity value of an internal parsed entity |
| 4243 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4244 | * If the declared value is not CDATA, then the XML processor must further |
| 4245 | * process the normalized attribute value by discarding any leading and |
| 4246 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4247 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4248 | * All attributes for which no declaration has been read should be treated |
| 4249 | * by a non-validating parser as if declared CDATA. |
| 4250 | * |
| 4251 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 4252 | */ |
| 4253 | |
| 4254 | |
| 4255 | xmlChar * |
| 4256 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 4257 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4258 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4262 | * xmlParseSystemLiteral: |
| 4263 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4264 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4265 | * parse an XML Literal |
| 4266 | * |
| 4267 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 4268 | * |
| 4269 | * Returns the SystemLiteral parsed or NULL |
| 4270 | */ |
| 4271 | |
| 4272 | xmlChar * |
| 4273 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 4274 | xmlChar *buf = NULL; |
| 4275 | int len = 0; |
| 4276 | int size = XML_PARSER_BUFFER_SIZE; |
| 4277 | int cur, l; |
| 4278 | xmlChar stop; |
| 4279 | int state = ctxt->instate; |
| 4280 | int count = 0; |
| 4281 | |
| 4282 | SHRINK; |
| 4283 | if (RAW == '"') { |
| 4284 | NEXT; |
| 4285 | stop = '"'; |
| 4286 | } else if (RAW == '\'') { |
| 4287 | NEXT; |
| 4288 | stop = '\''; |
| 4289 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4290 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4291 | return(NULL); |
| 4292 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4293 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4294 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4295 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4296 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4297 | return(NULL); |
| 4298 | } |
| 4299 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 4300 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4301 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4302 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4303 | xmlChar *tmp; |
| 4304 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4305 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4306 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4307 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral"); |
| 4308 | xmlFree(buf); |
| 4309 | ctxt->instate = (xmlParserInputState) state; |
| 4310 | return(NULL); |
| 4311 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4312 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4313 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4314 | if (tmp == NULL) { |
| 4315 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4316 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4317 | ctxt->instate = (xmlParserInputState) state; |
| 4318 | return(NULL); |
| 4319 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4320 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4321 | } |
| 4322 | count++; |
| 4323 | if (count > 50) { |
| 4324 | GROW; |
| 4325 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4326 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4327 | xmlFree(buf); |
| 4328 | return(NULL); |
| 4329 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | } |
| 4331 | COPY_BUF(l,buf,len,cur); |
| 4332 | NEXTL(l); |
| 4333 | cur = CUR_CHAR(l); |
| 4334 | if (cur == 0) { |
| 4335 | GROW; |
| 4336 | SHRINK; |
| 4337 | cur = CUR_CHAR(l); |
| 4338 | } |
| 4339 | } |
| 4340 | buf[len] = 0; |
| 4341 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4342 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4343 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4344 | } else { |
| 4345 | NEXT; |
| 4346 | } |
| 4347 | return(buf); |
| 4348 | } |
| 4349 | |
| 4350 | /** |
| 4351 | * xmlParsePubidLiteral: |
| 4352 | * @ctxt: an XML parser context |
| 4353 | * |
| 4354 | * parse an XML public literal |
| 4355 | * |
| 4356 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 4357 | * |
| 4358 | * Returns the PubidLiteral parsed or NULL. |
| 4359 | */ |
| 4360 | |
| 4361 | xmlChar * |
| 4362 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 4363 | xmlChar *buf = NULL; |
| 4364 | int len = 0; |
| 4365 | int size = XML_PARSER_BUFFER_SIZE; |
| 4366 | xmlChar cur; |
| 4367 | xmlChar stop; |
| 4368 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4369 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4370 | |
| 4371 | SHRINK; |
| 4372 | if (RAW == '"') { |
| 4373 | NEXT; |
| 4374 | stop = '"'; |
| 4375 | } else if (RAW == '\'') { |
| 4376 | NEXT; |
| 4377 | stop = '\''; |
| 4378 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4379 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4380 | return(NULL); |
| 4381 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4382 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4383 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4384 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4385 | return(NULL); |
| 4386 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4387 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4388 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4389 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4390 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4391 | xmlChar *tmp; |
| 4392 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4393 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4394 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4395 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID"); |
| 4396 | xmlFree(buf); |
| 4397 | return(NULL); |
| 4398 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4399 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4400 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4401 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4402 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4403 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4404 | return(NULL); |
| 4405 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4406 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4407 | } |
| 4408 | buf[len++] = cur; |
| 4409 | count++; |
| 4410 | if (count > 50) { |
| 4411 | GROW; |
| 4412 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4413 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4414 | xmlFree(buf); |
| 4415 | return(NULL); |
| 4416 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4417 | } |
| 4418 | NEXT; |
| 4419 | cur = CUR; |
| 4420 | if (cur == 0) { |
| 4421 | GROW; |
| 4422 | SHRINK; |
| 4423 | cur = CUR; |
| 4424 | } |
| 4425 | } |
| 4426 | buf[len] = 0; |
| 4427 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4428 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4429 | } else { |
| 4430 | NEXT; |
| 4431 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4432 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4433 | return(buf); |
| 4434 | } |
| 4435 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4436 | static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4437 | |
| 4438 | /* |
| 4439 | * used for the test in the inner loop of the char data testing |
| 4440 | */ |
| 4441 | static const unsigned char test_char_data[256] = { |
| 4442 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4443 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 4444 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4445 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4446 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 4447 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 4448 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 4449 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 4450 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 4451 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 4452 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 4453 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 4454 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4455 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4456 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4457 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4458 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4459 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4460 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4461 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4462 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4463 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4464 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4465 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4466 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4467 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4468 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4469 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4470 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4471 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4472 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4473 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4474 | }; |
| 4475 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4476 | /** |
| 4477 | * xmlParseCharData: |
| 4478 | * @ctxt: an XML parser context |
| 4479 | * @cdata: int indicating whether we are within a CDATA section |
| 4480 | * |
| 4481 | * parse a CharData section. |
| 4482 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4483 | * |
| 4484 | * The right angle bracket (>) may be represented using the string ">", |
| 4485 | * and must, for compatibility, be escaped using ">" or a character |
| 4486 | * reference when it appears in the string "]]>" in content, when that |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4487 | * string is not marking the end of a CDATA section. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4488 | * |
| 4489 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4490 | */ |
| 4491 | |
| 4492 | void |
| 4493 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4494 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4495 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4496 | int line = ctxt->input->line; |
| 4497 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4498 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4499 | |
| 4500 | SHRINK; |
| 4501 | GROW; |
| 4502 | /* |
| 4503 | * Accelerated common case where input don't need to be |
| 4504 | * modified before passing it to the handler. |
| 4505 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4506 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4507 | in = ctxt->input->cur; |
| 4508 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4509 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4510 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4511 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4512 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4513 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4514 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4515 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4516 | goto get_more_space; |
| 4517 | } |
| 4518 | if (*in == '<') { |
| 4519 | nbchar = in - ctxt->input->cur; |
| 4520 | if (nbchar > 0) { |
| 4521 | const xmlChar *tmp = ctxt->input->cur; |
| 4522 | ctxt->input->cur = in; |
| 4523 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4524 | if ((ctxt->sax != NULL) && |
| 4525 | (ctxt->sax->ignorableWhitespace != |
| 4526 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4527 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4528 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4529 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4530 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4531 | } else { |
| 4532 | if (ctxt->sax->characters != NULL) |
| 4533 | ctxt->sax->characters(ctxt->userData, |
| 4534 | tmp, nbchar); |
| 4535 | if (*ctxt->space == -1) |
| 4536 | *ctxt->space = -2; |
| 4537 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4538 | } else if ((ctxt->sax != NULL) && |
| 4539 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4540 | ctxt->sax->characters(ctxt->userData, |
| 4541 | tmp, nbchar); |
| 4542 | } |
| 4543 | } |
| 4544 | return; |
| 4545 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4546 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4547 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4548 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4549 | while (test_char_data[*in]) { |
| 4550 | in++; |
| 4551 | ccol++; |
| 4552 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4553 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4554 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4555 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4556 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4557 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4558 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4559 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4560 | } |
| 4561 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4562 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4563 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4564 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4565 | return; |
| 4566 | } |
| 4567 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4568 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4569 | goto get_more; |
| 4570 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4571 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4572 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4573 | if ((ctxt->sax != NULL) && |
| 4574 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4575 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4576 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4577 | const xmlChar *tmp = ctxt->input->cur; |
| 4578 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4579 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4580 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4581 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4582 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4583 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4584 | } else { |
| 4585 | if (ctxt->sax->characters != NULL) |
| 4586 | ctxt->sax->characters(ctxt->userData, |
| 4587 | tmp, nbchar); |
| 4588 | if (*ctxt->space == -1) |
| 4589 | *ctxt->space = -2; |
| 4590 | } |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4591 | line = ctxt->input->line; |
| 4592 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4593 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4594 | if (ctxt->sax->characters != NULL) |
| 4595 | ctxt->sax->characters(ctxt->userData, |
| 4596 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4597 | line = ctxt->input->line; |
| 4598 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4599 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4600 | /* something really bad happened in the SAX callback */ |
| 4601 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4602 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4603 | } |
| 4604 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4605 | if (*in == 0xD) { |
| 4606 | in++; |
| 4607 | if (*in == 0xA) { |
| 4608 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4609 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4610 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4611 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4612 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4613 | in--; |
| 4614 | } |
| 4615 | if (*in == '<') { |
| 4616 | return; |
| 4617 | } |
| 4618 | if (*in == '&') { |
| 4619 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4620 | } |
| 4621 | SHRINK; |
| 4622 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4623 | if (ctxt->instate == XML_PARSER_EOF) |
| 4624 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4625 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4626 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4627 | nbchar = 0; |
| 4628 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4629 | ctxt->input->line = line; |
| 4630 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4631 | xmlParseCharDataComplex(ctxt, cdata); |
| 4632 | } |
| 4633 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4634 | /** |
| 4635 | * xmlParseCharDataComplex: |
| 4636 | * @ctxt: an XML parser context |
| 4637 | * @cdata: int indicating whether we are within a CDATA section |
| 4638 | * |
| 4639 | * parse a CharData section.this is the fallback function |
| 4640 | * of xmlParseCharData() when the parsing requires handling |
| 4641 | * of non-ASCII characters. |
| 4642 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4643 | static void |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4644 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4645 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4646 | int nbchar = 0; |
| 4647 | int cur, l; |
| 4648 | int count = 0; |
| 4649 | |
| 4650 | SHRINK; |
| 4651 | GROW; |
| 4652 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4653 | while ((cur != '<') && /* checked */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4654 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4655 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4656 | if ((cur == ']') && (NXT(1) == ']') && |
| 4657 | (NXT(2) == '>')) { |
| 4658 | if (cdata) break; |
| 4659 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4660 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | COPY_BUF(l,buf,nbchar,cur); |
| 4664 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4665 | buf[nbchar] = 0; |
| 4666 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4667 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4668 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4669 | */ |
| 4670 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4671 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4672 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4673 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4674 | buf, nbchar); |
| 4675 | } else { |
| 4676 | if (ctxt->sax->characters != NULL) |
| 4677 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4678 | if ((ctxt->sax->characters != |
| 4679 | ctxt->sax->ignorableWhitespace) && |
| 4680 | (*ctxt->space == -1)) |
| 4681 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4685 | /* something really bad happened in the SAX callback */ |
| 4686 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4687 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4688 | } |
| 4689 | count++; |
| 4690 | if (count > 50) { |
| 4691 | GROW; |
| 4692 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4693 | if (ctxt->instate == XML_PARSER_EOF) |
| 4694 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4695 | } |
| 4696 | NEXTL(l); |
| 4697 | cur = CUR_CHAR(l); |
| 4698 | } |
| 4699 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4700 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4702 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4703 | */ |
| 4704 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4705 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4706 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4707 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4708 | } else { |
| 4709 | if (ctxt->sax->characters != NULL) |
| 4710 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4711 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4712 | (*ctxt->space == -1)) |
| 4713 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4714 | } |
| 4715 | } |
| 4716 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4717 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4718 | /* Generate the error and skip the offending character */ |
| 4719 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4720 | "PCDATA invalid Char value %d\n", |
| 4721 | cur); |
| 4722 | NEXTL(l); |
| 4723 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | /** |
| 4727 | * xmlParseExternalID: |
| 4728 | * @ctxt: an XML parser context |
| 4729 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4730 | * @strict: indicate whether we should restrict parsing to only |
| 4731 | * production [75], see NOTE below |
| 4732 | * |
| 4733 | * Parse an External ID or a Public ID |
| 4734 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4735 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4736 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4737 | * |
| 4738 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4739 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4740 | * |
| 4741 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4742 | * |
| 4743 | * Returns the function returns SystemLiteral and in the second |
| 4744 | * case publicID receives PubidLiteral, is strict is off |
| 4745 | * it is possible to return NULL and have publicID set. |
| 4746 | */ |
| 4747 | |
| 4748 | xmlChar * |
| 4749 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4750 | xmlChar *URI = NULL; |
| 4751 | |
| 4752 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4753 | |
| 4754 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4755 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4756 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4757 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4758 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4759 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4760 | } |
| 4761 | SKIP_BLANKS; |
| 4762 | URI = xmlParseSystemLiteral(ctxt); |
| 4763 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4764 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4765 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4766 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4767 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4768 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4769 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4770 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4771 | } |
| 4772 | SKIP_BLANKS; |
| 4773 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4774 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4775 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4776 | } |
| 4777 | if (strict) { |
| 4778 | /* |
| 4779 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4780 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4781 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4782 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4783 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4784 | } |
| 4785 | } else { |
| 4786 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4787 | * We handle [83] so we return immediately, if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4789 | * point of view that's a nice mess. |
| 4790 | */ |
| 4791 | const xmlChar *ptr; |
| 4792 | GROW; |
| 4793 | |
| 4794 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4795 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4796 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4797 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4798 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4799 | } |
| 4800 | SKIP_BLANKS; |
| 4801 | URI = xmlParseSystemLiteral(ctxt); |
| 4802 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4803 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | return(URI); |
| 4807 | } |
| 4808 | |
| 4809 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4810 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4811 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4812 | * @buf: the already parsed part of the buffer |
| 4813 | * @len: number of bytes filles in the buffer |
| 4814 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4815 | * |
| 4816 | * Skip an XML (SGML) comment <!-- .... --> |
| 4817 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4818 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4819 | * This is the slow routine in case the accelerator for ascii didn't work |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4820 | * |
| 4821 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4822 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4823 | static void |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4824 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, |
| 4825 | size_t len, size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4826 | int q, ql; |
| 4827 | int r, rl; |
| 4828 | int cur, l; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4829 | size_t count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4830 | int inputid; |
| 4831 | |
| 4832 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4833 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4834 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4835 | len = 0; |
| 4836 | size = XML_PARSER_BUFFER_SIZE; |
| 4837 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4838 | if (buf == NULL) { |
| 4839 | xmlErrMemory(ctxt, NULL); |
| 4840 | return; |
| 4841 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4842 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4843 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4844 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4845 | if (q == 0) |
| 4846 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4847 | if (!IS_CHAR(q)) { |
| 4848 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4849 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4850 | q); |
| 4851 | xmlFree (buf); |
| 4852 | return; |
| 4853 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4854 | NEXTL(ql); |
| 4855 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4856 | if (r == 0) |
| 4857 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4858 | if (!IS_CHAR(r)) { |
| 4859 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4860 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4861 | q); |
| 4862 | xmlFree (buf); |
| 4863 | return; |
| 4864 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4865 | NEXTL(rl); |
| 4866 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4867 | if (cur == 0) |
| 4868 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4869 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4870 | ((cur != '>') || |
| 4871 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4872 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4873 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4874 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4875 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4876 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4877 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4878 | "Comment too big found", NULL); |
| 4879 | xmlFree (buf); |
| 4880 | return; |
| 4881 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4882 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4883 | xmlChar *new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4884 | size_t new_size; |
| 4885 | |
| 4886 | new_size = size * 2; |
| 4887 | new_buf = (xmlChar *) xmlRealloc(buf, new_size); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4888 | if (new_buf == NULL) { |
| 4889 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4890 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4891 | return; |
| 4892 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4893 | buf = new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4894 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4895 | } |
| 4896 | COPY_BUF(ql,buf,len,q); |
| 4897 | q = r; |
| 4898 | ql = rl; |
| 4899 | r = cur; |
| 4900 | rl = l; |
| 4901 | |
| 4902 | count++; |
| 4903 | if (count > 50) { |
| 4904 | GROW; |
| 4905 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4906 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4907 | xmlFree(buf); |
| 4908 | return; |
| 4909 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4910 | } |
| 4911 | NEXTL(l); |
| 4912 | cur = CUR_CHAR(l); |
| 4913 | if (cur == 0) { |
| 4914 | SHRINK; |
| 4915 | GROW; |
| 4916 | cur = CUR_CHAR(l); |
| 4917 | } |
| 4918 | } |
| 4919 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4920 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4921 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4922 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4923 | } else if (!IS_CHAR(cur)) { |
| 4924 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4925 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4926 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4927 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4928 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4929 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4930 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4931 | } |
| 4932 | NEXT; |
| 4933 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4934 | (!ctxt->disableSAX)) |
| 4935 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4936 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4937 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4938 | return; |
| 4939 | not_terminated: |
| 4940 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4941 | "Comment not terminated\n", NULL); |
| 4942 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4943 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4944 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4945 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4946 | /** |
| 4947 | * xmlParseComment: |
| 4948 | * @ctxt: an XML parser context |
| 4949 | * |
| 4950 | * Skip an XML (SGML) comment <!-- .... --> |
| 4951 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4952 | * must not occur within comments. " |
| 4953 | * |
| 4954 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4955 | */ |
| 4956 | void |
| 4957 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4958 | xmlChar *buf = NULL; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4959 | size_t size = XML_PARSER_BUFFER_SIZE; |
| 4960 | size_t len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4961 | xmlParserInputState state; |
| 4962 | const xmlChar *in; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4963 | size_t nbchar = 0; |
| 4964 | int ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4965 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4966 | |
| 4967 | /* |
| 4968 | * Check that there is a comment right here. |
| 4969 | */ |
| 4970 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4971 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4972 | state = ctxt->instate; |
| 4973 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4974 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4975 | SKIP(4); |
| 4976 | SHRINK; |
| 4977 | GROW; |
| 4978 | |
| 4979 | /* |
| 4980 | * Accelerated common case where input don't need to be |
| 4981 | * modified before passing it to the handler. |
| 4982 | */ |
| 4983 | in = ctxt->input->cur; |
| 4984 | do { |
| 4985 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4986 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4987 | ctxt->input->line++; ctxt->input->col = 1; |
| 4988 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4989 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4990 | } |
| 4991 | get_more: |
| 4992 | ccol = ctxt->input->col; |
| 4993 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4994 | ((*in >= 0x20) && (*in < '-')) || |
| 4995 | (*in == 0x09)) { |
| 4996 | in++; |
| 4997 | ccol++; |
| 4998 | } |
| 4999 | ctxt->input->col = ccol; |
| 5000 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 5001 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5002 | ctxt->input->line++; ctxt->input->col = 1; |
| 5003 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 5004 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5005 | goto get_more; |
| 5006 | } |
| 5007 | nbchar = in - ctxt->input->cur; |
| 5008 | /* |
| 5009 | * save current set of data |
| 5010 | */ |
| 5011 | if (nbchar > 0) { |
| 5012 | if ((ctxt->sax != NULL) && |
| 5013 | (ctxt->sax->comment != NULL)) { |
| 5014 | if (buf == NULL) { |
| 5015 | if ((*in == '-') && (in[1] == '-')) |
| 5016 | size = nbchar + 1; |
| 5017 | else |
| 5018 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 5019 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 5020 | if (buf == NULL) { |
| 5021 | xmlErrMemory(ctxt, NULL); |
| 5022 | ctxt->instate = state; |
| 5023 | return; |
| 5024 | } |
| 5025 | len = 0; |
| 5026 | } else if (len + nbchar + 1 >= size) { |
| 5027 | xmlChar *new_buf; |
| 5028 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 5029 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 5030 | size * sizeof(xmlChar)); |
| 5031 | if (new_buf == NULL) { |
| 5032 | xmlFree (buf); |
| 5033 | xmlErrMemory(ctxt, NULL); |
| 5034 | ctxt->instate = state; |
| 5035 | return; |
| 5036 | } |
| 5037 | buf = new_buf; |
| 5038 | } |
| 5039 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 5040 | len += nbchar; |
| 5041 | buf[len] = 0; |
| 5042 | } |
| 5043 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 5044 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5045 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5046 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 5047 | "Comment too big found", NULL); |
| 5048 | xmlFree (buf); |
| 5049 | return; |
| 5050 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5051 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 5052 | if (*in == 0xA) { |
| 5053 | in++; |
| 5054 | ctxt->input->line++; ctxt->input->col = 1; |
| 5055 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5056 | if (*in == 0xD) { |
| 5057 | in++; |
| 5058 | if (*in == 0xA) { |
| 5059 | ctxt->input->cur = in; |
| 5060 | in++; |
| 5061 | ctxt->input->line++; ctxt->input->col = 1; |
| 5062 | continue; /* while */ |
| 5063 | } |
| 5064 | in--; |
| 5065 | } |
| 5066 | SHRINK; |
| 5067 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5068 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5069 | xmlFree(buf); |
| 5070 | return; |
| 5071 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5072 | in = ctxt->input->cur; |
| 5073 | if (*in == '-') { |
| 5074 | if (in[1] == '-') { |
| 5075 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 5076 | if (ctxt->input->id != inputid) { |
| 5077 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5078 | "comment doesn't start and stop in the same entity\n"); |
| 5079 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5080 | SKIP(3); |
| 5081 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 5082 | (!ctxt->disableSAX)) { |
| 5083 | if (buf != NULL) |
| 5084 | ctxt->sax->comment(ctxt->userData, buf); |
| 5085 | else |
| 5086 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 5087 | } |
| 5088 | if (buf != NULL) |
| 5089 | xmlFree(buf); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5090 | if (ctxt->instate != XML_PARSER_EOF) |
| 5091 | ctxt->instate = state; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5092 | return; |
| 5093 | } |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5094 | if (buf != NULL) { |
| 5095 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5096 | "Double hyphen within comment: " |
| 5097 | "<!--%.50s\n", |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5098 | buf); |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5099 | } else |
| 5100 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5101 | "Double hyphen within comment\n", NULL); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5102 | in++; |
| 5103 | ctxt->input->col++; |
| 5104 | } |
| 5105 | in++; |
| 5106 | ctxt->input->col++; |
| 5107 | goto get_more; |
| 5108 | } |
| 5109 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 5110 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 5111 | ctxt->instate = state; |
| 5112 | return; |
| 5113 | } |
| 5114 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5115 | |
| 5116 | /** |
| 5117 | * xmlParsePITarget: |
| 5118 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5119 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5120 | * parse the name of a PI |
| 5121 | * |
| 5122 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 5123 | * |
| 5124 | * Returns the PITarget name or NULL |
| 5125 | */ |
| 5126 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5127 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5129 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | |
| 5131 | name = xmlParseName(ctxt); |
| 5132 | if ((name != NULL) && |
| 5133 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 5134 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 5135 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 5136 | int i; |
| 5137 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 5138 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5139 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5140 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5141 | return(name); |
| 5142 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5143 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5144 | return(name); |
| 5145 | } |
| 5146 | for (i = 0;;i++) { |
| 5147 | if (xmlW3CPIs[i] == NULL) break; |
| 5148 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 5149 | return(name); |
| 5150 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5151 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 5152 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 5153 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5154 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5155 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5156 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5157 | "colons are forbidden from PI names '%s'\n", name, NULL, NULL); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5158 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5159 | return(name); |
| 5160 | } |
| 5161 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5162 | #ifdef LIBXML_CATALOG_ENABLED |
| 5163 | /** |
| 5164 | * xmlParseCatalogPI: |
| 5165 | * @ctxt: an XML parser context |
| 5166 | * @catalog: the PI value string |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5167 | * |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5168 | * parse an XML Catalog Processing Instruction. |
| 5169 | * |
| 5170 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 5171 | * |
| 5172 | * Occurs only if allowed by the user and if happening in the Misc |
| 5173 | * part of the document before any doctype informations |
| 5174 | * This will add the given catalog to the parsing context in order |
| 5175 | * to be used if there is a resolution need further down in the document |
| 5176 | */ |
| 5177 | |
| 5178 | static void |
| 5179 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 5180 | xmlChar *URL = NULL; |
| 5181 | const xmlChar *tmp, *base; |
| 5182 | xmlChar marker; |
| 5183 | |
| 5184 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5185 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5186 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 5187 | goto error; |
| 5188 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5189 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5190 | if (*tmp != '=') { |
| 5191 | return; |
| 5192 | } |
| 5193 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5194 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5195 | marker = *tmp; |
| 5196 | if ((marker != '\'') && (marker != '"')) |
| 5197 | goto error; |
| 5198 | tmp++; |
| 5199 | base = tmp; |
| 5200 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 5201 | if (*tmp == 0) |
| 5202 | goto error; |
| 5203 | URL = xmlStrndup(base, tmp - base); |
| 5204 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5205 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5206 | if (*tmp != 0) |
| 5207 | goto error; |
| 5208 | |
| 5209 | if (URL != NULL) { |
| 5210 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 5211 | xmlFree(URL); |
| 5212 | } |
| 5213 | return; |
| 5214 | |
| 5215 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5216 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 5217 | "Catalog PI syntax error: %s\n", |
| 5218 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5219 | if (URL != NULL) |
| 5220 | xmlFree(URL); |
| 5221 | } |
| 5222 | #endif |
| 5223 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5224 | /** |
| 5225 | * xmlParsePI: |
| 5226 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5227 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5228 | * parse an XML Processing Instruction. |
| 5229 | * |
| 5230 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 5231 | * |
| 5232 | * The processing is transfered to SAX once parsed. |
| 5233 | */ |
| 5234 | |
| 5235 | void |
| 5236 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 5237 | xmlChar *buf = NULL; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5238 | size_t len = 0; |
| 5239 | size_t size = XML_PARSER_BUFFER_SIZE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5240 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5241 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5242 | xmlParserInputState state; |
| 5243 | int count = 0; |
| 5244 | |
| 5245 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 5246 | xmlParserInputPtr input = ctxt->input; |
| 5247 | state = ctxt->instate; |
| 5248 | ctxt->instate = XML_PARSER_PI; |
| 5249 | /* |
| 5250 | * this is a Processing Instruction. |
| 5251 | */ |
| 5252 | SKIP(2); |
| 5253 | SHRINK; |
| 5254 | |
| 5255 | /* |
| 5256 | * Parse the target name and check for special support like |
| 5257 | * namespace. |
| 5258 | */ |
| 5259 | target = xmlParsePITarget(ctxt); |
| 5260 | if (target != NULL) { |
| 5261 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5262 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5263 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5264 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5265 | } |
| 5266 | SKIP(2); |
| 5267 | |
| 5268 | /* |
| 5269 | * SAX: PI detected. |
| 5270 | */ |
| 5271 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5272 | (ctxt->sax->processingInstruction != NULL)) |
| 5273 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5274 | target, NULL); |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5275 | if (ctxt->instate != XML_PARSER_EOF) |
| 5276 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5277 | return; |
| 5278 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5279 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5280 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5281 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5282 | ctxt->instate = state; |
| 5283 | return; |
| 5284 | } |
| 5285 | cur = CUR; |
| 5286 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5287 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5288 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5289 | } |
| 5290 | SKIP_BLANKS; |
| 5291 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 5292 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5293 | ((cur != '?') || (NXT(1) != '>'))) { |
| 5294 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5295 | xmlChar *tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5296 | size_t new_size = size * 2; |
| 5297 | tmp = (xmlChar *) xmlRealloc(buf, new_size); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5298 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5299 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5300 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5301 | ctxt->instate = state; |
| 5302 | return; |
| 5303 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5304 | buf = tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5305 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5306 | } |
| 5307 | count++; |
| 5308 | if (count > 50) { |
| 5309 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5310 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5311 | xmlFree(buf); |
| 5312 | return; |
| 5313 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5314 | count = 0; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5315 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5316 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5317 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5318 | "PI %s too big found", target); |
| 5319 | xmlFree(buf); |
| 5320 | ctxt->instate = state; |
| 5321 | return; |
| 5322 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5323 | } |
| 5324 | COPY_BUF(l,buf,len,cur); |
| 5325 | NEXTL(l); |
| 5326 | cur = CUR_CHAR(l); |
| 5327 | if (cur == 0) { |
| 5328 | SHRINK; |
| 5329 | GROW; |
| 5330 | cur = CUR_CHAR(l); |
| 5331 | } |
| 5332 | } |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5333 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5334 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5335 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5336 | "PI %s too big found", target); |
| 5337 | xmlFree(buf); |
| 5338 | ctxt->instate = state; |
| 5339 | return; |
| 5340 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5341 | buf[len] = 0; |
| 5342 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5343 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5344 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5345 | } else { |
| 5346 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5347 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5348 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5349 | } |
| 5350 | SKIP(2); |
| 5351 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5352 | #ifdef LIBXML_CATALOG_ENABLED |
| 5353 | if (((state == XML_PARSER_MISC) || |
| 5354 | (state == XML_PARSER_START)) && |
| 5355 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 5356 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 5357 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 5358 | (allow == XML_CATA_ALLOW_ALL)) |
| 5359 | xmlParseCatalogPI(ctxt, buf); |
| 5360 | } |
| 5361 | #endif |
| 5362 | |
| 5363 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5364 | /* |
| 5365 | * SAX: PI detected. |
| 5366 | */ |
| 5367 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5368 | (ctxt->sax->processingInstruction != NULL)) |
| 5369 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5370 | target, buf); |
| 5371 | } |
| 5372 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5373 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5374 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5375 | } |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5376 | if (ctxt->instate != XML_PARSER_EOF) |
| 5377 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5378 | } |
| 5379 | } |
| 5380 | |
| 5381 | /** |
| 5382 | * xmlParseNotationDecl: |
| 5383 | * @ctxt: an XML parser context |
| 5384 | * |
| 5385 | * parse a notation declaration |
| 5386 | * |
| 5387 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 5388 | * |
| 5389 | * Hence there is actually 3 choices: |
| 5390 | * 'PUBLIC' S PubidLiteral |
| 5391 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 5392 | * and 'SYSTEM' S SystemLiteral |
| 5393 | * |
| 5394 | * See the NOTE on xmlParseExternalID(). |
| 5395 | */ |
| 5396 | |
| 5397 | void |
| 5398 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5399 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5400 | xmlChar *Pubid; |
| 5401 | xmlChar *Systemid; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5402 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5403 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5404 | xmlParserInputPtr input = ctxt->input; |
| 5405 | SHRINK; |
| 5406 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5407 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5408 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5409 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5410 | return; |
| 5411 | } |
| 5412 | SKIP_BLANKS; |
| 5413 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5414 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5415 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5416 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5417 | return; |
| 5418 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5419 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5420 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5421 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5422 | return; |
| 5423 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5424 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5425 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5426 | "colons are forbidden from notation names '%s'\n", |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5427 | name, NULL, NULL); |
| 5428 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5429 | SKIP_BLANKS; |
| 5430 | |
| 5431 | /* |
| 5432 | * Parse the IDs. |
| 5433 | */ |
| 5434 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 5435 | SKIP_BLANKS; |
| 5436 | |
| 5437 | if (RAW == '>') { |
| 5438 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5439 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5440 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5441 | } |
| 5442 | NEXT; |
| 5443 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5444 | (ctxt->sax->notationDecl != NULL)) |
| 5445 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 5446 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5447 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5448 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5449 | if (Systemid != NULL) xmlFree(Systemid); |
| 5450 | if (Pubid != NULL) xmlFree(Pubid); |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | /** |
| 5455 | * xmlParseEntityDecl: |
| 5456 | * @ctxt: an XML parser context |
| 5457 | * |
| 5458 | * parse <!ENTITY declarations |
| 5459 | * |
| 5460 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 5461 | * |
| 5462 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 5463 | * |
| 5464 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 5465 | * |
| 5466 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 5467 | * |
| 5468 | * [74] PEDef ::= EntityValue | ExternalID |
| 5469 | * |
| 5470 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 5471 | * |
| 5472 | * [ VC: Notation Declared ] |
| 5473 | * The Name must match the declared name of a notation. |
| 5474 | */ |
| 5475 | |
| 5476 | void |
| 5477 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5478 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5479 | xmlChar *value = NULL; |
| 5480 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5481 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5482 | int isParameter = 0; |
| 5483 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5484 | int skipped; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5485 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5486 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5487 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5488 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5489 | SHRINK; |
| 5490 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5491 | skipped = SKIP_BLANKS; |
| 5492 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5493 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5494 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5495 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5496 | |
| 5497 | if (RAW == '%') { |
| 5498 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5499 | skipped = SKIP_BLANKS; |
| 5500 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5501 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5502 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5503 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5504 | isParameter = 1; |
| 5505 | } |
| 5506 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5507 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5509 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5510 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5511 | return; |
| 5512 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5513 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5514 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5515 | "colons are forbidden from entities names '%s'\n", |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5516 | name, NULL, NULL); |
| 5517 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5518 | skipped = SKIP_BLANKS; |
| 5519 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5520 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5521 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5522 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5523 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5524 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5525 | /* |
| 5526 | * handle the various case of definitions... |
| 5527 | */ |
| 5528 | if (isParameter) { |
| 5529 | if ((RAW == '"') || (RAW == '\'')) { |
| 5530 | value = xmlParseEntityValue(ctxt, &orig); |
| 5531 | if (value) { |
| 5532 | if ((ctxt->sax != NULL) && |
| 5533 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5534 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5535 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5536 | NULL, NULL, value); |
| 5537 | } |
| 5538 | } else { |
| 5539 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5540 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5541 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5542 | } |
| 5543 | if (URI) { |
| 5544 | xmlURIPtr uri; |
| 5545 | |
| 5546 | uri = xmlParseURI((const char *) URI); |
| 5547 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5548 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5549 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5550 | /* |
| 5551 | * This really ought to be a well formedness error |
| 5552 | * but the XML Core WG decided otherwise c.f. issue |
| 5553 | * E26 of the XML erratas. |
| 5554 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5555 | } else { |
| 5556 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5557 | /* |
| 5558 | * Okay this is foolish to block those but not |
| 5559 | * invalid URIs. |
| 5560 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5561 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5562 | } else { |
| 5563 | if ((ctxt->sax != NULL) && |
| 5564 | (!ctxt->disableSAX) && |
| 5565 | (ctxt->sax->entityDecl != NULL)) |
| 5566 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5567 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5568 | literal, URI, NULL); |
| 5569 | } |
| 5570 | xmlFreeURI(uri); |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | } else { |
| 5575 | if ((RAW == '"') || (RAW == '\'')) { |
| 5576 | value = xmlParseEntityValue(ctxt, &orig); |
| 5577 | if ((ctxt->sax != NULL) && |
| 5578 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5579 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5580 | XML_INTERNAL_GENERAL_ENTITY, |
| 5581 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5582 | /* |
| 5583 | * For expat compatibility in SAX mode. |
| 5584 | */ |
| 5585 | if ((ctxt->myDoc == NULL) || |
| 5586 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5587 | if (ctxt->myDoc == NULL) { |
| 5588 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5589 | if (ctxt->myDoc == NULL) { |
| 5590 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5591 | return; |
| 5592 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5593 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5594 | } |
| 5595 | if (ctxt->myDoc->intSubset == NULL) |
| 5596 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5597 | BAD_CAST "fake", NULL, NULL); |
| 5598 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5599 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5600 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5601 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5602 | } else { |
| 5603 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5604 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5605 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | } |
| 5607 | if (URI) { |
| 5608 | xmlURIPtr uri; |
| 5609 | |
| 5610 | uri = xmlParseURI((const char *)URI); |
| 5611 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5612 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5613 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5614 | /* |
| 5615 | * This really ought to be a well formedness error |
| 5616 | * but the XML Core WG decided otherwise c.f. issue |
| 5617 | * E26 of the XML erratas. |
| 5618 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5619 | } else { |
| 5620 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5621 | /* |
| 5622 | * Okay this is foolish to block those but not |
| 5623 | * invalid URIs. |
| 5624 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5625 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5626 | } |
| 5627 | xmlFreeURI(uri); |
| 5628 | } |
| 5629 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5630 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5631 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5632 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | } |
| 5634 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5635 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5636 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5637 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5638 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5639 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5640 | } |
| 5641 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5642 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5643 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5644 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5645 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5646 | literal, URI, ndata); |
| 5647 | } else { |
| 5648 | if ((ctxt->sax != NULL) && |
| 5649 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5650 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5651 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5652 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5653 | /* |
| 5654 | * For expat compatibility in SAX mode. |
| 5655 | * assuming the entity repalcement was asked for |
| 5656 | */ |
| 5657 | if ((ctxt->replaceEntities != 0) && |
| 5658 | ((ctxt->myDoc == NULL) || |
| 5659 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5660 | if (ctxt->myDoc == NULL) { |
| 5661 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5662 | if (ctxt->myDoc == NULL) { |
| 5663 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5664 | return; |
| 5665 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5666 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
| 5669 | if (ctxt->myDoc->intSubset == NULL) |
| 5670 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5671 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5672 | xmlSAX2EntityDecl(ctxt, name, |
| 5673 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5674 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5675 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5676 | } |
| 5677 | } |
| 5678 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5679 | if (ctxt->instate == XML_PARSER_EOF) |
| 5680 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5681 | SKIP_BLANKS; |
| 5682 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5683 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5684 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 5685 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5686 | } else { |
| 5687 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5688 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5689 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5690 | } |
| 5691 | NEXT; |
| 5692 | } |
| 5693 | if (orig != NULL) { |
| 5694 | /* |
| 5695 | * Ugly mechanism to save the raw entity value. |
| 5696 | */ |
| 5697 | xmlEntityPtr cur = NULL; |
| 5698 | |
| 5699 | if (isParameter) { |
| 5700 | if ((ctxt->sax != NULL) && |
| 5701 | (ctxt->sax->getParameterEntity != NULL)) |
| 5702 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5703 | } else { |
| 5704 | if ((ctxt->sax != NULL) && |
| 5705 | (ctxt->sax->getEntity != NULL)) |
| 5706 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5707 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5708 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5709 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | } |
| 5711 | if (cur != NULL) { |
| 5712 | if (cur->orig != NULL) |
| 5713 | xmlFree(orig); |
| 5714 | else |
| 5715 | cur->orig = orig; |
| 5716 | } else |
| 5717 | xmlFree(orig); |
| 5718 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | if (value != NULL) xmlFree(value); |
| 5720 | if (URI != NULL) xmlFree(URI); |
| 5721 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5722 | } |
| 5723 | } |
| 5724 | |
| 5725 | /** |
| 5726 | * xmlParseDefaultDecl: |
| 5727 | * @ctxt: an XML parser context |
| 5728 | * @value: Receive a possible fixed default value for the attribute |
| 5729 | * |
| 5730 | * Parse an attribute default declaration |
| 5731 | * |
| 5732 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5733 | * |
| 5734 | * [ VC: Required Attribute ] |
| 5735 | * if the default declaration is the keyword #REQUIRED, then the |
| 5736 | * attribute must be specified for all elements of the type in the |
| 5737 | * attribute-list declaration. |
| 5738 | * |
| 5739 | * [ VC: Attribute Default Legal ] |
| 5740 | * The declared default value must meet the lexical constraints of |
| 5741 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5742 | * |
| 5743 | * [ VC: Fixed Attribute Default ] |
| 5744 | * if an attribute has a default value declared with the #FIXED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5745 | * keyword, instances of that attribute must match the default value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5746 | * |
| 5747 | * [ WFC: No < in Attribute Values ] |
| 5748 | * handled in xmlParseAttValue() |
| 5749 | * |
| 5750 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5751 | * or XML_ATTRIBUTE_FIXED. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5752 | */ |
| 5753 | |
| 5754 | int |
| 5755 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5756 | int val; |
| 5757 | xmlChar *ret; |
| 5758 | |
| 5759 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5760 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5761 | SKIP(9); |
| 5762 | return(XML_ATTRIBUTE_REQUIRED); |
| 5763 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5764 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5765 | SKIP(8); |
| 5766 | return(XML_ATTRIBUTE_IMPLIED); |
| 5767 | } |
| 5768 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5769 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5770 | SKIP(6); |
| 5771 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5772 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5773 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5774 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5775 | } |
| 5776 | SKIP_BLANKS; |
| 5777 | } |
| 5778 | ret = xmlParseAttValue(ctxt); |
| 5779 | ctxt->instate = XML_PARSER_DTD; |
| 5780 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5781 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5782 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | } else |
| 5784 | *value = ret; |
| 5785 | return(val); |
| 5786 | } |
| 5787 | |
| 5788 | /** |
| 5789 | * xmlParseNotationType: |
| 5790 | * @ctxt: an XML parser context |
| 5791 | * |
| 5792 | * parse an Notation attribute type. |
| 5793 | * |
| 5794 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5795 | * |
| 5796 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5797 | * |
| 5798 | * [ VC: Notation Attributes ] |
| 5799 | * Values of this type must match one of the notation names included |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5800 | * in the declaration; all notation names in the declaration must be declared. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5801 | * |
| 5802 | * Returns: the notation attribute tree built while parsing |
| 5803 | */ |
| 5804 | |
| 5805 | xmlEnumerationPtr |
| 5806 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5807 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5808 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5809 | |
| 5810 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5811 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5812 | return(NULL); |
| 5813 | } |
| 5814 | SHRINK; |
| 5815 | do { |
| 5816 | NEXT; |
| 5817 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5818 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5819 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5820 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5821 | "Name expected in NOTATION declaration\n"); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5822 | xmlFreeEnumeration(ret); |
| 5823 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5824 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5825 | tmp = ret; |
| 5826 | while (tmp != NULL) { |
| 5827 | if (xmlStrEqual(name, tmp->name)) { |
| 5828 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5829 | "standalone: attribute notation value token %s duplicated\n", |
| 5830 | name, NULL); |
| 5831 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5832 | xmlFree((xmlChar *) name); |
| 5833 | break; |
| 5834 | } |
| 5835 | tmp = tmp->next; |
| 5836 | } |
| 5837 | if (tmp == NULL) { |
| 5838 | cur = xmlCreateEnumeration(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5839 | if (cur == NULL) { |
| 5840 | xmlFreeEnumeration(ret); |
| 5841 | return(NULL); |
| 5842 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5843 | if (last == NULL) ret = last = cur; |
| 5844 | else { |
| 5845 | last->next = cur; |
| 5846 | last = cur; |
| 5847 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5848 | } |
| 5849 | SKIP_BLANKS; |
| 5850 | } while (RAW == '|'); |
| 5851 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5852 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5853 | xmlFreeEnumeration(ret); |
| 5854 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5855 | } |
| 5856 | NEXT; |
| 5857 | return(ret); |
| 5858 | } |
| 5859 | |
| 5860 | /** |
| 5861 | * xmlParseEnumerationType: |
| 5862 | * @ctxt: an XML parser context |
| 5863 | * |
| 5864 | * parse an Enumeration attribute type. |
| 5865 | * |
| 5866 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5867 | * |
| 5868 | * [ VC: Enumeration ] |
| 5869 | * Values of this type must match one of the Nmtoken tokens in |
| 5870 | * the declaration |
| 5871 | * |
| 5872 | * Returns: the enumeration attribute tree built while parsing |
| 5873 | */ |
| 5874 | |
| 5875 | xmlEnumerationPtr |
| 5876 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5877 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5878 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5879 | |
| 5880 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5881 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5882 | return(NULL); |
| 5883 | } |
| 5884 | SHRINK; |
| 5885 | do { |
| 5886 | NEXT; |
| 5887 | SKIP_BLANKS; |
| 5888 | name = xmlParseNmtoken(ctxt); |
| 5889 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5890 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5891 | return(ret); |
| 5892 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5893 | tmp = ret; |
| 5894 | while (tmp != NULL) { |
| 5895 | if (xmlStrEqual(name, tmp->name)) { |
| 5896 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5897 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5898 | name, NULL); |
| 5899 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5900 | xmlFree(name); |
| 5901 | break; |
| 5902 | } |
| 5903 | tmp = tmp->next; |
| 5904 | } |
| 5905 | if (tmp == NULL) { |
| 5906 | cur = xmlCreateEnumeration(name); |
| 5907 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5908 | xmlFree(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5909 | if (cur == NULL) { |
| 5910 | xmlFreeEnumeration(ret); |
| 5911 | return(NULL); |
| 5912 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5913 | if (last == NULL) ret = last = cur; |
| 5914 | else { |
| 5915 | last->next = cur; |
| 5916 | last = cur; |
| 5917 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5918 | } |
| 5919 | SKIP_BLANKS; |
| 5920 | } while (RAW == '|'); |
| 5921 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5922 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5923 | return(ret); |
| 5924 | } |
| 5925 | NEXT; |
| 5926 | return(ret); |
| 5927 | } |
| 5928 | |
| 5929 | /** |
| 5930 | * xmlParseEnumeratedType: |
| 5931 | * @ctxt: an XML parser context |
| 5932 | * @tree: the enumeration tree built while parsing |
| 5933 | * |
| 5934 | * parse an Enumerated attribute type. |
| 5935 | * |
| 5936 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5937 | * |
| 5938 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5939 | * |
| 5940 | * |
| 5941 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5942 | */ |
| 5943 | |
| 5944 | int |
| 5945 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5946 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5947 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5948 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5949 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5950 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | return(0); |
| 5952 | } |
| 5953 | SKIP_BLANKS; |
| 5954 | *tree = xmlParseNotationType(ctxt); |
| 5955 | if (*tree == NULL) return(0); |
| 5956 | return(XML_ATTRIBUTE_NOTATION); |
| 5957 | } |
| 5958 | *tree = xmlParseEnumerationType(ctxt); |
| 5959 | if (*tree == NULL) return(0); |
| 5960 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5961 | } |
| 5962 | |
| 5963 | /** |
| 5964 | * xmlParseAttributeType: |
| 5965 | * @ctxt: an XML parser context |
| 5966 | * @tree: the enumeration tree built while parsing |
| 5967 | * |
| 5968 | * parse the Attribute list def for an element |
| 5969 | * |
| 5970 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5971 | * |
| 5972 | * [55] StringType ::= 'CDATA' |
| 5973 | * |
| 5974 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5975 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5976 | * |
| 5977 | * Validity constraints for attribute values syntax are checked in |
| 5978 | * xmlValidateAttributeValue() |
| 5979 | * |
| 5980 | * [ VC: ID ] |
| 5981 | * Values of type ID must match the Name production. A name must not |
| 5982 | * appear more than once in an XML document as a value of this type; |
| 5983 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5984 | * |
| 5985 | * [ VC: One ID per Element Type ] |
| 5986 | * No element type may have more than one ID attribute specified. |
| 5987 | * |
| 5988 | * [ VC: ID Attribute Default ] |
| 5989 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5990 | * |
| 5991 | * [ VC: IDREF ] |
| 5992 | * Values of type IDREF must match the Name production, and values |
| 5993 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5994 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5995 | * values must match the value of some ID attribute. |
| 5996 | * |
| 5997 | * [ VC: Entity Name ] |
| 5998 | * Values of type ENTITY must match the Name production, values |
| 5999 | * of type ENTITIES must match Names; each Entity Name must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6000 | * name of an unparsed entity declared in the DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | * |
| 6002 | * [ VC: Name Token ] |
| 6003 | * Values of type NMTOKEN must match the Nmtoken production; values |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6004 | * of type NMTOKENS must match Nmtokens. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6005 | * |
| 6006 | * Returns the attribute type |
| 6007 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6008 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6009 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 6010 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6011 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6012 | SKIP(5); |
| 6013 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6014 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6015 | SKIP(6); |
| 6016 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6017 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | SKIP(5); |
| 6019 | return(XML_ATTRIBUTE_IDREF); |
| 6020 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 6021 | SKIP(2); |
| 6022 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6023 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6024 | SKIP(6); |
| 6025 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6026 | } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6027 | SKIP(8); |
| 6028 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6029 | } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | SKIP(8); |
| 6031 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6032 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6033 | SKIP(7); |
| 6034 | return(XML_ATTRIBUTE_NMTOKEN); |
| 6035 | } |
| 6036 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 6037 | } |
| 6038 | |
| 6039 | /** |
| 6040 | * xmlParseAttributeListDecl: |
| 6041 | * @ctxt: an XML parser context |
| 6042 | * |
| 6043 | * : parse the Attribute list def for an element |
| 6044 | * |
| 6045 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 6046 | * |
| 6047 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 6048 | * |
| 6049 | */ |
| 6050 | void |
| 6051 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6052 | const xmlChar *elemName; |
| 6053 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6054 | xmlEnumerationPtr tree; |
| 6055 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6056 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6057 | xmlParserInputPtr input = ctxt->input; |
| 6058 | |
| 6059 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6060 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6061 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6062 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6063 | } |
| 6064 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6065 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6066 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6067 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6068 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | return; |
| 6070 | } |
| 6071 | SKIP_BLANKS; |
| 6072 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6073 | while ((RAW != '>') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6074 | const xmlChar *check = CUR_PTR; |
| 6075 | int type; |
| 6076 | int def; |
| 6077 | xmlChar *defaultValue = NULL; |
| 6078 | |
| 6079 | GROW; |
| 6080 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6081 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6082 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6083 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6084 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6085 | break; |
| 6086 | } |
| 6087 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6088 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6089 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6090 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6091 | break; |
| 6092 | } |
| 6093 | SKIP_BLANKS; |
| 6094 | |
| 6095 | type = xmlParseAttributeType(ctxt, &tree); |
| 6096 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6097 | break; |
| 6098 | } |
| 6099 | |
| 6100 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6101 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6102 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6103 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | if (tree != NULL) |
| 6105 | xmlFreeEnumeration(tree); |
| 6106 | break; |
| 6107 | } |
| 6108 | SKIP_BLANKS; |
| 6109 | |
| 6110 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 6111 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6112 | if (defaultValue != NULL) |
| 6113 | xmlFree(defaultValue); |
| 6114 | if (tree != NULL) |
| 6115 | xmlFreeEnumeration(tree); |
| 6116 | break; |
| 6117 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 6118 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 6119 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6120 | |
| 6121 | GROW; |
| 6122 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6123 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6124 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6126 | if (defaultValue != NULL) |
| 6127 | xmlFree(defaultValue); |
| 6128 | if (tree != NULL) |
| 6129 | xmlFreeEnumeration(tree); |
| 6130 | break; |
| 6131 | } |
| 6132 | SKIP_BLANKS; |
| 6133 | } |
| 6134 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6135 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6136 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6137 | if (defaultValue != NULL) |
| 6138 | xmlFree(defaultValue); |
| 6139 | if (tree != NULL) |
| 6140 | xmlFreeEnumeration(tree); |
| 6141 | break; |
| 6142 | } |
| 6143 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 6144 | (ctxt->sax->attributeDecl != NULL)) |
| 6145 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 6146 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6147 | else if (tree != NULL) |
| 6148 | xmlFreeEnumeration(tree); |
| 6149 | |
| 6150 | if ((ctxt->sax2) && (defaultValue != NULL) && |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6151 | (def != XML_ATTRIBUTE_IMPLIED) && |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6152 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 6153 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 6154 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 6155 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6156 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 6157 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | if (defaultValue != NULL) |
| 6159 | xmlFree(defaultValue); |
| 6160 | GROW; |
| 6161 | } |
| 6162 | if (RAW == '>') { |
| 6163 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6164 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6165 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 6166 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6167 | } |
| 6168 | NEXT; |
| 6169 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | } |
| 6171 | } |
| 6172 | |
| 6173 | /** |
| 6174 | * xmlParseElementMixedContentDecl: |
| 6175 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6176 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6177 | * |
| 6178 | * parse the declaration for a Mixed Element content |
| 6179 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6180 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6181 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 6182 | * '(' S? '#PCDATA' S? ')' |
| 6183 | * |
| 6184 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 6185 | * |
| 6186 | * [ VC: No Duplicate Types ] |
| 6187 | * The same name must not appear more than once in a single |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6188 | * mixed-content declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6189 | * |
| 6190 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 6191 | */ |
| 6192 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6193 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6195 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6196 | |
| 6197 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6198 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6199 | SKIP(7); |
| 6200 | SKIP_BLANKS; |
| 6201 | SHRINK; |
| 6202 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6203 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6204 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6205 | "Element content declaration doesn't start and stop in the same entity\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6206 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6207 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6208 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6209 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6210 | if (ret == NULL) |
| 6211 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6212 | if (RAW == '*') { |
| 6213 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6214 | NEXT; |
| 6215 | } |
| 6216 | return(ret); |
| 6217 | } |
| 6218 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6219 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6220 | if (ret == NULL) return(NULL); |
| 6221 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6222 | while ((RAW == '|') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6223 | NEXT; |
| 6224 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6225 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6226 | if (ret == NULL) return(NULL); |
| 6227 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6228 | if (cur != NULL) |
| 6229 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6230 | cur = ret; |
| 6231 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6232 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6234 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6235 | if (n->c1 != NULL) |
| 6236 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6237 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6238 | if (n != NULL) |
| 6239 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6241 | } |
| 6242 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6243 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6244 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6245 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6246 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6247 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6248 | return(NULL); |
| 6249 | } |
| 6250 | SKIP_BLANKS; |
| 6251 | GROW; |
| 6252 | } |
| 6253 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 6254 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6255 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6257 | if (cur->c2 != NULL) |
| 6258 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | } |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6260 | if (ret != NULL) |
| 6261 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6262 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6263 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6264 | "Element content declaration doesn't start and stop in the same entity\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6265 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6266 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6267 | SKIP(2); |
| 6268 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6269 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6270 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6271 | return(NULL); |
| 6272 | } |
| 6273 | |
| 6274 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6275 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6276 | } |
| 6277 | return(ret); |
| 6278 | } |
| 6279 | |
| 6280 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6281 | * xmlParseElementChildrenContentDeclPriv: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6283 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6284 | * @depth: the level of recursion |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6285 | * |
| 6286 | * parse the declaration for a Mixed Element content |
| 6287 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6288 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6289 | * |
| 6290 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6291 | * |
| 6292 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6293 | * |
| 6294 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6295 | * |
| 6296 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6297 | * |
| 6298 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6299 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6300 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6301 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6302 | * construct is contained in the replacement text for a parameter |
| 6303 | * entity, both must be contained in the same replacement text. For |
| 6304 | * interoperability, if a parameter-entity reference appears in a |
| 6305 | * choice, seq, or Mixed construct, its replacement text should not |
| 6306 | * be empty, and neither the first nor last non-blank character of |
| 6307 | * the replacement text should be a connector (| or ,). |
| 6308 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6309 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | * hierarchy. |
| 6311 | */ |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6312 | static xmlElementContentPtr |
| 6313 | xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk, |
| 6314 | int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6315 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6316 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6317 | xmlChar type = 0; |
| 6318 | |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6319 | if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 6320 | (depth > 2048)) { |
| 6321 | xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, |
| 6322 | "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n", |
| 6323 | depth); |
| 6324 | return(NULL); |
| 6325 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6326 | SKIP_BLANKS; |
| 6327 | GROW; |
| 6328 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6329 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6330 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6331 | /* Recurse on first child */ |
| 6332 | NEXT; |
| 6333 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6334 | cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6335 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6336 | SKIP_BLANKS; |
| 6337 | GROW; |
| 6338 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6339 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6340 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6341 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | return(NULL); |
| 6343 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6344 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6345 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6346 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6347 | return(NULL); |
| 6348 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | GROW; |
| 6350 | if (RAW == '?') { |
| 6351 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6352 | NEXT; |
| 6353 | } else if (RAW == '*') { |
| 6354 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6355 | NEXT; |
| 6356 | } else if (RAW == '+') { |
| 6357 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6358 | NEXT; |
| 6359 | } else { |
| 6360 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6361 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6362 | GROW; |
| 6363 | } |
| 6364 | SKIP_BLANKS; |
| 6365 | SHRINK; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6366 | while ((RAW != ')') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6367 | /* |
| 6368 | * Each loop we parse one separator and one element. |
| 6369 | */ |
| 6370 | if (RAW == ',') { |
| 6371 | if (type == 0) type = CUR; |
| 6372 | |
| 6373 | /* |
| 6374 | * Detect "Name | Name , Name" error |
| 6375 | */ |
| 6376 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6377 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6378 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6379 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6380 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6381 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6382 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6383 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6384 | return(NULL); |
| 6385 | } |
| 6386 | NEXT; |
| 6387 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6388 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6389 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6390 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6391 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 6392 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6393 | return(NULL); |
| 6394 | } |
| 6395 | if (last == NULL) { |
| 6396 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6397 | if (ret != NULL) |
| 6398 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | ret = cur = op; |
| 6400 | } else { |
| 6401 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6402 | if (op != NULL) |
| 6403 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6404 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6405 | if (last != NULL) |
| 6406 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6407 | cur =op; |
| 6408 | last = NULL; |
| 6409 | } |
| 6410 | } else if (RAW == '|') { |
| 6411 | if (type == 0) type = CUR; |
| 6412 | |
| 6413 | /* |
| 6414 | * Detect "Name , Name | Name" error |
| 6415 | */ |
| 6416 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6417 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6418 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6419 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6420 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6421 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6422 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6423 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6424 | return(NULL); |
| 6425 | } |
| 6426 | NEXT; |
| 6427 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6428 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6429 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6430 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6431 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6432 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6433 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6434 | return(NULL); |
| 6435 | } |
| 6436 | if (last == NULL) { |
| 6437 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6438 | if (ret != NULL) |
| 6439 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6440 | ret = cur = op; |
| 6441 | } else { |
| 6442 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6443 | if (op != NULL) |
| 6444 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6445 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6446 | if (last != NULL) |
| 6447 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6448 | cur =op; |
| 6449 | last = NULL; |
| 6450 | } |
| 6451 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6452 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 6453 | if ((last != NULL) && (last != ret)) |
| 6454 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6455 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6456 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6457 | return(NULL); |
| 6458 | } |
| 6459 | GROW; |
| 6460 | SKIP_BLANKS; |
| 6461 | GROW; |
| 6462 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6463 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6464 | /* Recurse on second child */ |
| 6465 | NEXT; |
| 6466 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6467 | last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6468 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6469 | SKIP_BLANKS; |
| 6470 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6471 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6472 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6473 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6474 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6475 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6476 | return(NULL); |
| 6477 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6478 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6479 | if (last == NULL) { |
| 6480 | if (ret != NULL) |
| 6481 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 6482 | return(NULL); |
| 6483 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6484 | if (RAW == '?') { |
| 6485 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6486 | NEXT; |
| 6487 | } else if (RAW == '*') { |
| 6488 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6489 | NEXT; |
| 6490 | } else if (RAW == '+') { |
| 6491 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6492 | NEXT; |
| 6493 | } else { |
| 6494 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6495 | } |
| 6496 | } |
| 6497 | SKIP_BLANKS; |
| 6498 | GROW; |
| 6499 | } |
| 6500 | if ((cur != NULL) && (last != NULL)) { |
| 6501 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6502 | if (last != NULL) |
| 6503 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6505 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6506 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6507 | "Element content declaration doesn't start and stop in the same entity\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6508 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6509 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6510 | NEXT; |
| 6511 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6512 | if (ret != NULL) { |
| 6513 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 6514 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6515 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6516 | else |
| 6517 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6518 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6519 | NEXT; |
| 6520 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6521 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6522 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6523 | cur = ret; |
| 6524 | /* |
| 6525 | * Some normalization: |
| 6526 | * (a | b* | c?)* == (a | b | c)* |
| 6527 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6528 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6529 | if ((cur->c1 != NULL) && |
| 6530 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6531 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6532 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6533 | if ((cur->c2 != NULL) && |
| 6534 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6535 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6536 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6537 | cur = cur->c2; |
| 6538 | } |
| 6539 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6540 | NEXT; |
| 6541 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6542 | if (ret != NULL) { |
| 6543 | int found = 0; |
| 6544 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6545 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6546 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6547 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6548 | else |
| 6549 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6550 | /* |
| 6551 | * Some normalization: |
| 6552 | * (a | b*)+ == (a | b)* |
| 6553 | * (a | b?)+ == (a | b)* |
| 6554 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6555 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6556 | if ((cur->c1 != NULL) && |
| 6557 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6558 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6559 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6560 | found = 1; |
| 6561 | } |
| 6562 | if ((cur->c2 != NULL) && |
| 6563 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6564 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6565 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6566 | found = 1; |
| 6567 | } |
| 6568 | cur = cur->c2; |
| 6569 | } |
| 6570 | if (found) |
| 6571 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6572 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6573 | NEXT; |
| 6574 | } |
| 6575 | return(ret); |
| 6576 | } |
| 6577 | |
| 6578 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6579 | * xmlParseElementChildrenContentDecl: |
| 6580 | * @ctxt: an XML parser context |
| 6581 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6582 | * |
| 6583 | * parse the declaration for a Mixed Element content |
| 6584 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 6585 | * |
| 6586 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6587 | * |
| 6588 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6589 | * |
| 6590 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6591 | * |
| 6592 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6593 | * |
| 6594 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6595 | * TODO Parameter-entity replacement text must be properly nested |
| 6596 | * with parenthesized groups. That is to say, if either of the |
| 6597 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6598 | * construct is contained in the replacement text for a parameter |
| 6599 | * entity, both must be contained in the same replacement text. For |
| 6600 | * interoperability, if a parameter-entity reference appears in a |
| 6601 | * choice, seq, or Mixed construct, its replacement text should not |
| 6602 | * be empty, and neither the first nor last non-blank character of |
| 6603 | * the replacement text should be a connector (| or ,). |
| 6604 | * |
| 6605 | * Returns the tree of xmlElementContentPtr describing the element |
| 6606 | * hierarchy. |
| 6607 | */ |
| 6608 | xmlElementContentPtr |
| 6609 | xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
| 6610 | /* stub left for API/ABI compat */ |
| 6611 | return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1)); |
| 6612 | } |
| 6613 | |
| 6614 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6615 | * xmlParseElementContentDecl: |
| 6616 | * @ctxt: an XML parser context |
| 6617 | * @name: the name of the element being defined. |
| 6618 | * @result: the Element Content pointer will be stored here if any |
| 6619 | * |
| 6620 | * parse the declaration for an Element content either Mixed or Children, |
| 6621 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6622 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6623 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6624 | * |
| 6625 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6626 | */ |
| 6627 | |
| 6628 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6629 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6630 | xmlElementContentPtr *result) { |
| 6631 | |
| 6632 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6633 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6634 | int res; |
| 6635 | |
| 6636 | *result = NULL; |
| 6637 | |
| 6638 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6639 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6640 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | return(-1); |
| 6642 | } |
| 6643 | NEXT; |
| 6644 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6645 | if (ctxt->instate == XML_PARSER_EOF) |
| 6646 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6647 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6648 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6649 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6650 | res = XML_ELEMENT_TYPE_MIXED; |
| 6651 | } else { |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6652 | tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6653 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6654 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6655 | SKIP_BLANKS; |
| 6656 | *result = tree; |
| 6657 | return(res); |
| 6658 | } |
| 6659 | |
| 6660 | /** |
| 6661 | * xmlParseElementDecl: |
| 6662 | * @ctxt: an XML parser context |
| 6663 | * |
| 6664 | * parse an Element declaration. |
| 6665 | * |
| 6666 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6667 | * |
| 6668 | * [ VC: Unique Element Type Declaration ] |
| 6669 | * No element type may be declared more than once |
| 6670 | * |
| 6671 | * Returns the type of the element, or -1 in case of error |
| 6672 | */ |
| 6673 | int |
| 6674 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6675 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6676 | int ret = -1; |
| 6677 | xmlElementContentPtr content = NULL; |
| 6678 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6679 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6680 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6681 | xmlParserInputPtr input = ctxt->input; |
| 6682 | |
| 6683 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6684 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6685 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6686 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6687 | } |
| 6688 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6689 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6690 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6691 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6692 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6693 | return(-1); |
| 6694 | } |
| 6695 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6696 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6697 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6698 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6699 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | } |
| 6701 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6702 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6703 | SKIP(5); |
| 6704 | /* |
| 6705 | * Element must always be empty. |
| 6706 | */ |
| 6707 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6708 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6709 | (NXT(2) == 'Y')) { |
| 6710 | SKIP(3); |
| 6711 | /* |
| 6712 | * Element is a generic container. |
| 6713 | */ |
| 6714 | ret = XML_ELEMENT_TYPE_ANY; |
| 6715 | } else if (RAW == '(') { |
| 6716 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6717 | } else { |
| 6718 | /* |
| 6719 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6720 | */ |
| 6721 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6722 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6723 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6724 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6725 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6726 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6727 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6729 | return(-1); |
| 6730 | } |
| 6731 | |
| 6732 | SKIP_BLANKS; |
| 6733 | /* |
| 6734 | * Pop-up of finished entities. |
| 6735 | */ |
| 6736 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6737 | xmlPopInput(ctxt); |
| 6738 | SKIP_BLANKS; |
| 6739 | |
| 6740 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6741 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6742 | if (content != NULL) { |
| 6743 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6744 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6745 | } else { |
| 6746 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6747 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6748 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6749 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6750 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6751 | NEXT; |
| 6752 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6753 | (ctxt->sax->elementDecl != NULL)) { |
| 6754 | if (content != NULL) |
| 6755 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6756 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6757 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6758 | if ((content != NULL) && (content->parent == NULL)) { |
| 6759 | /* |
| 6760 | * this is a trick: if xmlAddElementDecl is called, |
| 6761 | * instead of copying the full tree it is plugged directly |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6762 | * if called from the parser. Avoid duplicating the |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6763 | * interfaces or change the API/ABI |
| 6764 | */ |
| 6765 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6766 | } |
| 6767 | } else if (content != NULL) { |
| 6768 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6769 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6770 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6771 | } |
| 6772 | return(ret); |
| 6773 | } |
| 6774 | |
| 6775 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6776 | * xmlParseConditionalSections |
| 6777 | * @ctxt: an XML parser context |
| 6778 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6779 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6780 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6781 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6782 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6783 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6784 | */ |
| 6785 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6786 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6787 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6788 | int id = ctxt->input->id; |
| 6789 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6790 | SKIP(3); |
| 6791 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6792 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6793 | SKIP(7); |
| 6794 | SKIP_BLANKS; |
| 6795 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6796 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 6797 | xmlHaltParser(ctxt); |
Daniel Veillard | 9b85123 | 2015-02-23 11:29:20 +0800 | [diff] [blame] | 6798 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6799 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6800 | if (ctxt->input->id != id) { |
| 6801 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6802 | "All markup of the conditional section is not in the same entity\n", |
| 6803 | NULL, NULL); |
| 6804 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | NEXT; |
| 6806 | } |
| 6807 | if (xmlParserDebugEntities) { |
| 6808 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6809 | xmlGenericError(xmlGenericErrorContext, |
| 6810 | "%s(%d): ", ctxt->input->filename, |
| 6811 | ctxt->input->line); |
| 6812 | xmlGenericError(xmlGenericErrorContext, |
| 6813 | "Entering INCLUDE Conditional Section\n"); |
| 6814 | } |
| 6815 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6816 | while (((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6817 | (NXT(2) != '>'))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6818 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6819 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6820 | |
| 6821 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6822 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6823 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6824 | NEXT; |
| 6825 | } else if (RAW == '%') { |
| 6826 | xmlParsePEReference(ctxt); |
| 6827 | } else |
| 6828 | xmlParseMarkupDecl(ctxt); |
| 6829 | |
| 6830 | /* |
| 6831 | * Pop-up of finished entities. |
| 6832 | */ |
| 6833 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6834 | xmlPopInput(ctxt); |
| 6835 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6836 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6837 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6838 | break; |
| 6839 | } |
| 6840 | } |
| 6841 | if (xmlParserDebugEntities) { |
| 6842 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6843 | xmlGenericError(xmlGenericErrorContext, |
| 6844 | "%s(%d): ", ctxt->input->filename, |
| 6845 | ctxt->input->line); |
| 6846 | xmlGenericError(xmlGenericErrorContext, |
| 6847 | "Leaving INCLUDE Conditional Section\n"); |
| 6848 | } |
| 6849 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6850 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6851 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6852 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6853 | int depth = 0; |
| 6854 | |
| 6855 | SKIP(6); |
| 6856 | SKIP_BLANKS; |
| 6857 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6858 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 6859 | xmlHaltParser(ctxt); |
Daniel Veillard | 9b85123 | 2015-02-23 11:29:20 +0800 | [diff] [blame] | 6860 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6861 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6862 | if (ctxt->input->id != id) { |
| 6863 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6864 | "All markup of the conditional section is not in the same entity\n", |
| 6865 | NULL, NULL); |
| 6866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6867 | NEXT; |
| 6868 | } |
| 6869 | if (xmlParserDebugEntities) { |
| 6870 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6871 | xmlGenericError(xmlGenericErrorContext, |
| 6872 | "%s(%d): ", ctxt->input->filename, |
| 6873 | ctxt->input->line); |
| 6874 | xmlGenericError(xmlGenericErrorContext, |
| 6875 | "Entering IGNORE Conditional Section\n"); |
| 6876 | } |
| 6877 | |
| 6878 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6879 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6880 | * But disable SAX event generating DTD building in the meantime |
| 6881 | */ |
| 6882 | state = ctxt->disableSAX; |
| 6883 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6884 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6885 | ctxt->instate = XML_PARSER_IGNORE; |
| 6886 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6887 | while (((depth >= 0) && (RAW != 0)) && |
| 6888 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6889 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6890 | depth++; |
| 6891 | SKIP(3); |
| 6892 | continue; |
| 6893 | } |
| 6894 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6895 | if (--depth >= 0) SKIP(3); |
| 6896 | continue; |
| 6897 | } |
| 6898 | NEXT; |
| 6899 | continue; |
| 6900 | } |
| 6901 | |
| 6902 | ctxt->disableSAX = state; |
| 6903 | ctxt->instate = instate; |
| 6904 | |
| 6905 | if (xmlParserDebugEntities) { |
| 6906 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6907 | xmlGenericError(xmlGenericErrorContext, |
| 6908 | "%s(%d): ", ctxt->input->filename, |
| 6909 | ctxt->input->line); |
| 6910 | xmlGenericError(xmlGenericErrorContext, |
| 6911 | "Leaving IGNORE Conditional Section\n"); |
| 6912 | } |
| 6913 | |
| 6914 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6915 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 6916 | xmlHaltParser(ctxt); |
Daniel Veillard | 9b85123 | 2015-02-23 11:29:20 +0800 | [diff] [blame] | 6917 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6918 | } |
| 6919 | |
| 6920 | if (RAW == 0) |
| 6921 | SHRINK; |
| 6922 | |
| 6923 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6924 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6925 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6926 | if (ctxt->input->id != id) { |
| 6927 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6928 | "All markup of the conditional section is not in the same entity\n", |
| 6929 | NULL, NULL); |
| 6930 | } |
Daniel Veillard | bd0526e | 2015-10-23 19:02:28 +0800 | [diff] [blame] | 6931 | if ((ctxt-> instate != XML_PARSER_EOF) && |
Daniel Veillard | 41ac904 | 2015-10-27 10:53:44 +0800 | [diff] [blame] | 6932 | ((ctxt->input->cur + 3) <= ctxt->input->end)) |
Daniel Veillard | bd0526e | 2015-10-23 19:02:28 +0800 | [diff] [blame] | 6933 | SKIP(3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | } |
| 6935 | } |
| 6936 | |
| 6937 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6938 | * xmlParseMarkupDecl: |
| 6939 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6940 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6941 | * parse Markup declarations |
| 6942 | * |
| 6943 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6944 | * NotationDecl | PI | Comment |
| 6945 | * |
| 6946 | * [ VC: Proper Declaration/PE Nesting ] |
| 6947 | * Parameter-entity replacement text must be properly nested with |
| 6948 | * markup declarations. That is to say, if either the first character |
| 6949 | * or the last character of a markup declaration (markupdecl above) is |
| 6950 | * contained in the replacement text for a parameter-entity reference, |
| 6951 | * both must be contained in the same replacement text. |
| 6952 | * |
| 6953 | * [ WFC: PEs in Internal Subset ] |
| 6954 | * In the internal DTD subset, parameter-entity references can occur |
| 6955 | * only where markup declarations can occur, not within markup declarations. |
| 6956 | * (This does not apply to references that occur in external parameter |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6957 | * entities or to the external subset.) |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6958 | */ |
| 6959 | void |
| 6960 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6961 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6962 | if (CUR == '<') { |
| 6963 | if (NXT(1) == '!') { |
| 6964 | switch (NXT(2)) { |
| 6965 | case 'E': |
| 6966 | if (NXT(3) == 'L') |
| 6967 | xmlParseElementDecl(ctxt); |
| 6968 | else if (NXT(3) == 'N') |
| 6969 | xmlParseEntityDecl(ctxt); |
| 6970 | break; |
| 6971 | case 'A': |
| 6972 | xmlParseAttributeListDecl(ctxt); |
| 6973 | break; |
| 6974 | case 'N': |
| 6975 | xmlParseNotationDecl(ctxt); |
| 6976 | break; |
| 6977 | case '-': |
| 6978 | xmlParseComment(ctxt); |
| 6979 | break; |
| 6980 | default: |
| 6981 | /* there is an error but it will be detected later */ |
| 6982 | break; |
| 6983 | } |
| 6984 | } else if (NXT(1) == '?') { |
| 6985 | xmlParsePI(ctxt); |
| 6986 | } |
| 6987 | } |
Hugh Davenport | ab2b9a9 | 2015-11-03 20:40:49 +0800 | [diff] [blame] | 6988 | |
| 6989 | /* |
| 6990 | * detect requirement to exit there and act accordingly |
| 6991 | * and avoid having instate overriden later on |
| 6992 | */ |
| 6993 | if (ctxt->instate == XML_PARSER_EOF) |
| 6994 | return; |
| 6995 | |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6996 | /* |
| 6997 | * This is only for internal subset. On external entities, |
| 6998 | * the replacement is done before parsing stage |
| 6999 | */ |
| 7000 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 7001 | xmlParsePEReference(ctxt); |
| 7002 | |
| 7003 | /* |
| 7004 | * Conditional sections are allowed from entities included |
| 7005 | * by PE References in the internal subset. |
| 7006 | */ |
| 7007 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 7008 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 7009 | xmlParseConditionalSections(ctxt); |
| 7010 | } |
| 7011 | } |
| 7012 | |
| 7013 | ctxt->instate = XML_PARSER_DTD; |
| 7014 | } |
| 7015 | |
| 7016 | /** |
| 7017 | * xmlParseTextDecl: |
| 7018 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 7019 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7020 | * parse an XML declaration header for external entities |
| 7021 | * |
| 7022 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7023 | */ |
| 7024 | |
| 7025 | void |
| 7026 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 7027 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 7028 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7029 | |
| 7030 | /* |
| 7031 | * We know that '<?xml' is here. |
| 7032 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7033 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7034 | SKIP(5); |
| 7035 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7036 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7037 | return; |
| 7038 | } |
| 7039 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7040 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7041 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 7042 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7043 | } |
| 7044 | SKIP_BLANKS; |
| 7045 | |
| 7046 | /* |
| 7047 | * We may have the VersionInfo here. |
| 7048 | */ |
| 7049 | version = xmlParseVersionInfo(ctxt); |
| 7050 | if (version == NULL) |
| 7051 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 7052 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7053 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7054 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 7055 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 7056 | } |
| 7057 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7058 | ctxt->input->version = version; |
| 7059 | |
| 7060 | /* |
| 7061 | * We must have the encoding declaration |
| 7062 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 7063 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7064 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 7065 | /* |
| 7066 | * The XML REC instructs us to stop parsing right here |
| 7067 | */ |
| 7068 | return; |
| 7069 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 7070 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 7071 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 7072 | "Missing encoding in text declaration\n"); |
| 7073 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7074 | |
| 7075 | SKIP_BLANKS; |
| 7076 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 7077 | SKIP(2); |
| 7078 | } else if (RAW == '>') { |
| 7079 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7080 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7081 | NEXT; |
| 7082 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7083 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7084 | MOVETO_ENDTAG(CUR_PTR); |
| 7085 | NEXT; |
| 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7090 | * xmlParseExternalSubset: |
| 7091 | * @ctxt: an XML parser context |
| 7092 | * @ExternalID: the external identifier |
| 7093 | * @SystemID: the system identifier (or URL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7094 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7095 | * parse Markup declarations from an external subset |
| 7096 | * |
| 7097 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 7098 | * |
| 7099 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 7100 | */ |
| 7101 | void |
| 7102 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 7103 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 7104 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7105 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7106 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 7107 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7108 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 7109 | xmlChar start[4]; |
| 7110 | xmlCharEncoding enc; |
| 7111 | |
| 7112 | start[0] = RAW; |
| 7113 | start[1] = NXT(1); |
| 7114 | start[2] = NXT(2); |
| 7115 | start[3] = NXT(3); |
| 7116 | enc = xmlDetectCharEncoding(start, 4); |
| 7117 | if (enc != XML_CHAR_ENCODING_NONE) |
| 7118 | xmlSwitchEncoding(ctxt, enc); |
| 7119 | } |
| 7120 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7121 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7122 | xmlParseTextDecl(ctxt); |
| 7123 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 7124 | /* |
| 7125 | * The XML REC instructs us to stop parsing right here |
| 7126 | */ |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 7127 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7128 | return; |
| 7129 | } |
| 7130 | } |
| 7131 | if (ctxt->myDoc == NULL) { |
| 7132 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 7133 | if (ctxt->myDoc == NULL) { |
| 7134 | xmlErrMemory(ctxt, "New Doc failed"); |
| 7135 | return; |
| 7136 | } |
| 7137 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7138 | } |
| 7139 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 7140 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 7141 | |
| 7142 | ctxt->instate = XML_PARSER_DTD; |
| 7143 | ctxt->external = 1; |
| 7144 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 7145 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7146 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7147 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7148 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7149 | |
| 7150 | GROW; |
| 7151 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 7152 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7153 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7154 | NEXT; |
| 7155 | } else if (RAW == '%') { |
| 7156 | xmlParsePEReference(ctxt); |
| 7157 | } else |
| 7158 | xmlParseMarkupDecl(ctxt); |
| 7159 | |
| 7160 | /* |
| 7161 | * Pop-up of finished entities. |
| 7162 | */ |
| 7163 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7164 | xmlPopInput(ctxt); |
| 7165 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7166 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7167 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7168 | break; |
| 7169 | } |
| 7170 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7171 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7172 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7173 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7174 | } |
| 7175 | |
| 7176 | } |
| 7177 | |
| 7178 | /** |
| 7179 | * xmlParseReference: |
| 7180 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7181 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7182 | * parse and handle entity references in content, depending on the SAX |
| 7183 | * interface, this may end-up in a call to character() if this is a |
| 7184 | * CharRef, a predefined entity, if there is no reference() callback. |
| 7185 | * or if the parser was asked to switch to that mode. |
| 7186 | * |
| 7187 | * [67] Reference ::= EntityRef | CharRef |
| 7188 | */ |
| 7189 | void |
| 7190 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 7191 | xmlEntityPtr ent; |
| 7192 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7193 | int was_checked; |
| 7194 | xmlNodePtr list = NULL; |
| 7195 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7196 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7197 | |
| 7198 | if (RAW != '&') |
| 7199 | return; |
| 7200 | |
| 7201 | /* |
| 7202 | * Simple case of a CharRef |
| 7203 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7204 | if (NXT(1) == '#') { |
| 7205 | int i = 0; |
| 7206 | xmlChar out[10]; |
| 7207 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7208 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7209 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 7210 | if (value == 0) |
| 7211 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7212 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 7213 | /* |
| 7214 | * So we are using non-UTF-8 buffers |
| 7215 | * Check that the char fit on 8bits, if not |
| 7216 | * generate a CharRef. |
| 7217 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7218 | if (value <= 0xFF) { |
| 7219 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7220 | out[1] = 0; |
| 7221 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7222 | (!ctxt->disableSAX)) |
| 7223 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 7224 | } else { |
| 7225 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7226 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7227 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7228 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7229 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7230 | (!ctxt->disableSAX)) |
| 7231 | ctxt->sax->reference(ctxt->userData, out); |
| 7232 | } |
| 7233 | } else { |
| 7234 | /* |
| 7235 | * Just encode the value in UTF-8 |
| 7236 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7237 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7238 | out[i] = 0; |
| 7239 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7240 | (!ctxt->disableSAX)) |
| 7241 | ctxt->sax->characters(ctxt->userData, out, i); |
| 7242 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7243 | return; |
| 7244 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7245 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7246 | /* |
| 7247 | * We are seeing an entity reference |
| 7248 | */ |
| 7249 | ent = xmlParseEntityRef(ctxt); |
| 7250 | if (ent == NULL) return; |
| 7251 | if (!ctxt->wellFormed) |
| 7252 | return; |
| 7253 | was_checked = ent->checked; |
| 7254 | |
| 7255 | /* special case of predefined entities */ |
| 7256 | if ((ent->name == NULL) || |
| 7257 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 7258 | val = ent->content; |
| 7259 | if (val == NULL) return; |
| 7260 | /* |
| 7261 | * inline the entity. |
| 7262 | */ |
| 7263 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7264 | (!ctxt->disableSAX)) |
| 7265 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 7266 | return; |
| 7267 | } |
| 7268 | |
| 7269 | /* |
| 7270 | * The first reference to the entity trigger a parsing phase |
| 7271 | * where the ent->children is filled with the result from |
| 7272 | * the parsing. |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7273 | * Note: external parsed entities will not be loaded, it is not |
| 7274 | * required for a non-validating parser, unless the parsing option |
| 7275 | * of validating, or substituting entities were given. Doing so is |
| 7276 | * far more secure as the parser will only process data coming from |
| 7277 | * the document entity by default. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7278 | */ |
Daniel Veillard | 72a46a5 | 2014-10-23 11:35:36 +0800 | [diff] [blame] | 7279 | if (((ent->checked == 0) || |
| 7280 | ((ent->children == NULL) && (ctxt->options & XML_PARSE_NOENT))) && |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7281 | ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
| 7282 | (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7283 | unsigned long oldnbent = ctxt->nbentities; |
| 7284 | |
| 7285 | /* |
| 7286 | * This is a bit hackish but this seems the best |
| 7287 | * way to make sure both SAX and DOM entity support |
| 7288 | * behaves okay. |
| 7289 | */ |
| 7290 | void *user_data; |
| 7291 | if (ctxt->userData == ctxt) |
| 7292 | user_data = NULL; |
| 7293 | else |
| 7294 | user_data = ctxt->userData; |
| 7295 | |
| 7296 | /* |
| 7297 | * Check that this entity is well formed |
| 7298 | * 4.3.2: An internal general parsed entity is well-formed |
| 7299 | * if its replacement text matches the production labeled |
| 7300 | * content. |
| 7301 | */ |
| 7302 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7303 | ctxt->depth++; |
| 7304 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 7305 | user_data, &list); |
| 7306 | ctxt->depth--; |
| 7307 | |
| 7308 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7309 | ctxt->depth++; |
| 7310 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 7311 | user_data, ctxt->depth, ent->URI, |
| 7312 | ent->ExternalID, &list); |
| 7313 | ctxt->depth--; |
| 7314 | } else { |
| 7315 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7316 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7317 | "invalid entity type found\n", NULL); |
| 7318 | } |
| 7319 | |
| 7320 | /* |
| 7321 | * Store the number of entities needing parsing for this entity |
| 7322 | * content and do checkings |
| 7323 | */ |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7324 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
| 7325 | if ((ent->content != NULL) && (xmlStrchr(ent->content, '<'))) |
| 7326 | ent->checked |= 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7327 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7328 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7329 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7330 | return; |
| 7331 | } |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7332 | if (xmlParserEntityCheck(ctxt, 0, ent, 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7333 | xmlFreeNodeList(list); |
| 7334 | return; |
| 7335 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7336 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7337 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 7338 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 7339 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 7340 | (ent->children == NULL)) { |
| 7341 | ent->children = list; |
| 7342 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7343 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7344 | * Prune it directly in the generated document |
| 7345 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7346 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7347 | if (((list->type == XML_TEXT_NODE) && |
| 7348 | (list->next == NULL)) || |
| 7349 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7350 | list->parent = (xmlNodePtr) ent; |
| 7351 | list = NULL; |
| 7352 | ent->owner = 1; |
| 7353 | } else { |
| 7354 | ent->owner = 0; |
| 7355 | while (list != NULL) { |
| 7356 | list->parent = (xmlNodePtr) ctxt->node; |
| 7357 | list->doc = ctxt->myDoc; |
| 7358 | if (list->next == NULL) |
| 7359 | ent->last = list; |
| 7360 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7361 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7362 | list = ent->children; |
| 7363 | #ifdef LIBXML_LEGACY_ENABLED |
| 7364 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7365 | xmlAddEntityReference(ent, list, NULL); |
| 7366 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7367 | } |
| 7368 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7369 | ent->owner = 1; |
| 7370 | while (list != NULL) { |
| 7371 | list->parent = (xmlNodePtr) ent; |
Rob Richards | c794eb5 | 2011-02-18 12:17:17 -0500 | [diff] [blame] | 7372 | xmlSetTreeDoc(list, ent->doc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7373 | if (list->next == NULL) |
| 7374 | ent->last = list; |
| 7375 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7376 | } |
| 7377 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7378 | } else { |
| 7379 | xmlFreeNodeList(list); |
| 7380 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7381 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7382 | } else if ((ret != XML_ERR_OK) && |
| 7383 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 7384 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7385 | "Entity '%s' failed to parse\n", ent->name); |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 7386 | xmlParserEntityCheck(ctxt, 0, ent, 0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7387 | } else if (list != NULL) { |
| 7388 | xmlFreeNodeList(list); |
| 7389 | list = NULL; |
| 7390 | } |
| 7391 | if (ent->checked == 0) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7392 | ent->checked = 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7393 | } else if (ent->checked != 1) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7394 | ctxt->nbentities += ent->checked / 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7395 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7396 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7397 | /* |
| 7398 | * Now that the entity content has been gathered |
| 7399 | * provide it to the application, this can take different forms based |
| 7400 | * on the parsing modes. |
| 7401 | */ |
| 7402 | if (ent->children == NULL) { |
| 7403 | /* |
| 7404 | * Probably running in SAX mode and the callbacks don't |
| 7405 | * build the entity content. So unless we already went |
| 7406 | * though parsing for first checking go though the entity |
| 7407 | * content to generate callbacks associated to the entity |
| 7408 | */ |
| 7409 | if (was_checked != 0) { |
| 7410 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7411 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7412 | * This is a bit hackish but this seems the best |
| 7413 | * way to make sure both SAX and DOM entity support |
| 7414 | * behaves okay. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7415 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7416 | if (ctxt->userData == ctxt) |
| 7417 | user_data = NULL; |
| 7418 | else |
| 7419 | user_data = ctxt->userData; |
| 7420 | |
| 7421 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7422 | ctxt->depth++; |
| 7423 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 7424 | ent->content, user_data, NULL); |
| 7425 | ctxt->depth--; |
| 7426 | } else if (ent->etype == |
| 7427 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7428 | ctxt->depth++; |
| 7429 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 7430 | ctxt->sax, user_data, ctxt->depth, |
| 7431 | ent->URI, ent->ExternalID, NULL); |
| 7432 | ctxt->depth--; |
| 7433 | } else { |
| 7434 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7435 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7436 | "invalid entity type found\n", NULL); |
| 7437 | } |
| 7438 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 7439 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 7440 | return; |
| 7441 | } |
| 7442 | } |
| 7443 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7444 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7445 | /* |
| 7446 | * Entity reference callback comes second, it's somewhat |
| 7447 | * superfluous but a compatibility to historical behaviour |
| 7448 | */ |
| 7449 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7450 | } |
| 7451 | return; |
| 7452 | } |
| 7453 | |
| 7454 | /* |
| 7455 | * If we didn't get any children for the entity being built |
| 7456 | */ |
| 7457 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7458 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7459 | /* |
| 7460 | * Create a node. |
| 7461 | */ |
| 7462 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7463 | return; |
| 7464 | } |
| 7465 | |
| 7466 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 7467 | /* |
| 7468 | * There is a problem on the handling of _private for entities |
| 7469 | * (bug 155816): Should we copy the content of the field from |
| 7470 | * the entity (possibly overwriting some value set by the user |
| 7471 | * when a copy is created), should we leave it alone, or should |
| 7472 | * we try to take care of different situations? The problem |
| 7473 | * is exacerbated by the usage of this field by the xmlReader. |
| 7474 | * To fix this bug, we look at _private on the created node |
| 7475 | * and, if it's NULL, we copy in whatever was in the entity. |
| 7476 | * If it's not NULL we leave it alone. This is somewhat of a |
| 7477 | * hack - maybe we should have further tests to determine |
| 7478 | * what to do. |
| 7479 | */ |
| 7480 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 7481 | /* |
| 7482 | * Seems we are generating the DOM content, do |
| 7483 | * a simple tree copy for all references except the first |
| 7484 | * In the first occurrence list contains the replacement. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7485 | */ |
| 7486 | if (((list == NULL) && (ent->owner == 0)) || |
| 7487 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7488 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 7489 | |
| 7490 | /* |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7491 | * We are copying here, make sure there is no abuse |
| 7492 | */ |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 7493 | ctxt->sizeentcopy += ent->length + 5; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7494 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7495 | return; |
| 7496 | |
| 7497 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7498 | * when operating on a reader, the entities definitions |
| 7499 | * are always owning the entities subtree. |
| 7500 | if (ctxt->parseMode == XML_PARSE_READER) |
| 7501 | ent->owner = 1; |
| 7502 | */ |
| 7503 | |
| 7504 | cur = ent->children; |
| 7505 | while (cur != NULL) { |
| 7506 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7507 | if (nw != NULL) { |
| 7508 | if (nw->_private == NULL) |
| 7509 | nw->_private = cur->_private; |
| 7510 | if (firstChild == NULL){ |
| 7511 | firstChild = nw; |
| 7512 | } |
| 7513 | nw = xmlAddChild(ctxt->node, nw); |
| 7514 | } |
| 7515 | if (cur == ent->last) { |
| 7516 | /* |
| 7517 | * needed to detect some strange empty |
| 7518 | * node cases in the reader tests |
| 7519 | */ |
| 7520 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 7521 | (nw != NULL) && |
| 7522 | (nw->type == XML_ELEMENT_NODE) && |
| 7523 | (nw->children == NULL)) |
| 7524 | nw->extra = 1; |
| 7525 | |
| 7526 | break; |
| 7527 | } |
| 7528 | cur = cur->next; |
| 7529 | } |
| 7530 | #ifdef LIBXML_LEGACY_ENABLED |
| 7531 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7532 | xmlAddEntityReference(ent, firstChild, nw); |
| 7533 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 28f5e1a | 2012-09-04 11:18:39 +0800 | [diff] [blame] | 7534 | } else if ((list == NULL) || (ctxt->inputNr > 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7535 | xmlNodePtr nw = NULL, cur, next, last, |
| 7536 | firstChild = NULL; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7537 | |
| 7538 | /* |
| 7539 | * We are copying here, make sure there is no abuse |
| 7540 | */ |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 7541 | ctxt->sizeentcopy += ent->length + 5; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7542 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7543 | return; |
| 7544 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7545 | /* |
| 7546 | * Copy the entity child list and make it the new |
| 7547 | * entity child list. The goal is to make sure any |
| 7548 | * ID or REF referenced will be the one from the |
| 7549 | * document content and not the entity copy. |
| 7550 | */ |
| 7551 | cur = ent->children; |
| 7552 | ent->children = NULL; |
| 7553 | last = ent->last; |
| 7554 | ent->last = NULL; |
| 7555 | while (cur != NULL) { |
| 7556 | next = cur->next; |
| 7557 | cur->next = NULL; |
| 7558 | cur->parent = NULL; |
| 7559 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7560 | if (nw != NULL) { |
| 7561 | if (nw->_private == NULL) |
| 7562 | nw->_private = cur->_private; |
| 7563 | if (firstChild == NULL){ |
| 7564 | firstChild = cur; |
| 7565 | } |
| 7566 | xmlAddChild((xmlNodePtr) ent, nw); |
| 7567 | xmlAddChild(ctxt->node, cur); |
| 7568 | } |
| 7569 | if (cur == last) |
| 7570 | break; |
| 7571 | cur = next; |
| 7572 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 7573 | if (ent->owner == 0) |
| 7574 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7575 | #ifdef LIBXML_LEGACY_ENABLED |
| 7576 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7577 | xmlAddEntityReference(ent, firstChild, nw); |
| 7578 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7579 | } else { |
| 7580 | const xmlChar *nbktext; |
| 7581 | |
| 7582 | /* |
| 7583 | * the name change is to avoid coalescing of the |
| 7584 | * node with a possible previous text one which |
| 7585 | * would make ent->children a dangling pointer |
| 7586 | */ |
| 7587 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 7588 | -1); |
| 7589 | if (ent->children->type == XML_TEXT_NODE) |
| 7590 | ent->children->name = nbktext; |
| 7591 | if ((ent->last != ent->children) && |
| 7592 | (ent->last->type == XML_TEXT_NODE)) |
| 7593 | ent->last->name = nbktext; |
| 7594 | xmlAddChildList(ctxt->node, ent->children); |
| 7595 | } |
| 7596 | |
| 7597 | /* |
| 7598 | * This is to avoid a nasty side effect, see |
| 7599 | * characters() in SAX.c |
| 7600 | */ |
| 7601 | ctxt->nodemem = 0; |
| 7602 | ctxt->nodelen = 0; |
| 7603 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7604 | } |
| 7605 | } |
| 7606 | } |
| 7607 | |
| 7608 | /** |
| 7609 | * xmlParseEntityRef: |
| 7610 | * @ctxt: an XML parser context |
| 7611 | * |
| 7612 | * parse ENTITY references declarations |
| 7613 | * |
| 7614 | * [68] EntityRef ::= '&' Name ';' |
| 7615 | * |
| 7616 | * [ WFC: Entity Declared ] |
| 7617 | * In a document without any DTD, a document with only an internal DTD |
| 7618 | * subset which contains no parameter entity references, or a document |
| 7619 | * with "standalone='yes'", the Name given in the entity reference |
| 7620 | * must match that in an entity declaration, except that well-formed |
| 7621 | * documents need not declare any of the following entities: amp, lt, |
| 7622 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7623 | * any reference to it. Similarly, the declaration of a general entity |
| 7624 | * must precede any reference to it which appears in a default value in an |
| 7625 | * attribute-list declaration. Note that if entities are declared in the |
| 7626 | * external subset or in external parameter entities, a non-validating |
| 7627 | * processor is not obligated to read and process their declarations; |
| 7628 | * for such documents, the rule that an entity must be declared is a |
| 7629 | * well-formedness constraint only if standalone='yes'. |
| 7630 | * |
| 7631 | * [ WFC: Parsed Entity ] |
| 7632 | * An entity reference must not contain the name of an unparsed entity |
| 7633 | * |
| 7634 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7635 | */ |
| 7636 | xmlEntityPtr |
| 7637 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7638 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7639 | xmlEntityPtr ent = NULL; |
| 7640 | |
| 7641 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7642 | if (ctxt->instate == XML_PARSER_EOF) |
| 7643 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7644 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7645 | if (RAW != '&') |
| 7646 | return(NULL); |
| 7647 | NEXT; |
| 7648 | name = xmlParseName(ctxt); |
| 7649 | if (name == NULL) { |
| 7650 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7651 | "xmlParseEntityRef: no name\n"); |
| 7652 | return(NULL); |
| 7653 | } |
| 7654 | if (RAW != ';') { |
| 7655 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7656 | return(NULL); |
| 7657 | } |
| 7658 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7659 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7660 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7661 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7662 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7663 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7664 | ent = xmlGetPredefinedEntity(name); |
| 7665 | if (ent != NULL) |
| 7666 | return(ent); |
| 7667 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7668 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7669 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7670 | * Increase the number of entity references parsed |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7671 | */ |
| 7672 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7673 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7674 | /* |
| 7675 | * Ask first SAX for entity resolution, otherwise try the |
| 7676 | * entities which may have stored in the parser context. |
| 7677 | */ |
| 7678 | if (ctxt->sax != NULL) { |
| 7679 | if (ctxt->sax->getEntity != NULL) |
| 7680 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7681 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7682 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7683 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7684 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7685 | (ctxt->userData==ctxt)) { |
| 7686 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7687 | } |
| 7688 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7689 | if (ctxt->instate == XML_PARSER_EOF) |
| 7690 | return(NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7691 | /* |
| 7692 | * [ WFC: Entity Declared ] |
| 7693 | * In a document without any DTD, a document with only an |
| 7694 | * internal DTD subset which contains no parameter entity |
| 7695 | * references, or a document with "standalone='yes'", the |
| 7696 | * Name given in the entity reference must match that in an |
| 7697 | * entity declaration, except that well-formed documents |
| 7698 | * need not declare any of the following entities: amp, lt, |
| 7699 | * gt, apos, quot. |
| 7700 | * The declaration of a parameter entity must precede any |
| 7701 | * reference to it. |
| 7702 | * Similarly, the declaration of a general entity must |
| 7703 | * precede any reference to it which appears in a default |
| 7704 | * value in an attribute-list declaration. Note that if |
| 7705 | * entities are declared in the external subset or in |
| 7706 | * external parameter entities, a non-validating processor |
| 7707 | * is not obligated to read and process their declarations; |
| 7708 | * for such documents, the rule that an entity must be |
| 7709 | * declared is a well-formedness constraint only if |
| 7710 | * standalone='yes'. |
| 7711 | */ |
| 7712 | if (ent == NULL) { |
| 7713 | if ((ctxt->standalone == 1) || |
| 7714 | ((ctxt->hasExternalSubset == 0) && |
| 7715 | (ctxt->hasPErefs == 0))) { |
| 7716 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7717 | "Entity '%s' not defined\n", name); |
| 7718 | } else { |
| 7719 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7720 | "Entity '%s' not defined\n", name); |
| 7721 | if ((ctxt->inSubset == 0) && |
| 7722 | (ctxt->sax != NULL) && |
| 7723 | (ctxt->sax->reference != NULL)) { |
| 7724 | ctxt->sax->reference(ctxt->userData, name); |
| 7725 | } |
| 7726 | } |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 7727 | xmlParserEntityCheck(ctxt, 0, ent, 0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7728 | ctxt->valid = 0; |
| 7729 | } |
| 7730 | |
| 7731 | /* |
| 7732 | * [ WFC: Parsed Entity ] |
| 7733 | * An entity reference must not contain the name of an |
| 7734 | * unparsed entity |
| 7735 | */ |
| 7736 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7737 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7738 | "Entity reference to unparsed entity %s\n", name); |
| 7739 | } |
| 7740 | |
| 7741 | /* |
| 7742 | * [ WFC: No External Entity References ] |
| 7743 | * Attribute values cannot contain direct or indirect |
| 7744 | * entity references to external entities. |
| 7745 | */ |
| 7746 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7747 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7748 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7749 | "Attribute references external entity '%s'\n", name); |
| 7750 | } |
| 7751 | /* |
| 7752 | * [ WFC: No < in Attribute Values ] |
| 7753 | * The replacement text of any entity referred to directly or |
| 7754 | * indirectly in an attribute value (other than "<") must |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7755 | * not contain a <. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7756 | */ |
| 7757 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7758 | (ent != NULL) && |
| 7759 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 7cf5738 | 2014-10-08 16:09:56 +0800 | [diff] [blame] | 7760 | if (((ent->checked & 1) || (ent->checked == 0)) && |
| 7761 | (ent->content != NULL) && (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7762 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7763 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7764 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7765 | } |
| 7766 | |
| 7767 | /* |
| 7768 | * Internal check, no parameter entities here ... |
| 7769 | */ |
| 7770 | else { |
| 7771 | switch (ent->etype) { |
| 7772 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7773 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7774 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7775 | "Attempt to reference the parameter entity '%s'\n", |
| 7776 | name); |
| 7777 | break; |
| 7778 | default: |
| 7779 | break; |
| 7780 | } |
| 7781 | } |
| 7782 | |
| 7783 | /* |
| 7784 | * [ WFC: No Recursion ] |
| 7785 | * A parsed entity must not contain a recursive reference |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7786 | * to itself, either directly or indirectly. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7787 | * Done somewhere else |
| 7788 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7789 | return(ent); |
| 7790 | } |
| 7791 | |
| 7792 | /** |
| 7793 | * xmlParseStringEntityRef: |
| 7794 | * @ctxt: an XML parser context |
| 7795 | * @str: a pointer to an index in the string |
| 7796 | * |
| 7797 | * parse ENTITY references declarations, but this version parses it from |
| 7798 | * a string value. |
| 7799 | * |
| 7800 | * [68] EntityRef ::= '&' Name ';' |
| 7801 | * |
| 7802 | * [ WFC: Entity Declared ] |
| 7803 | * In a document without any DTD, a document with only an internal DTD |
| 7804 | * subset which contains no parameter entity references, or a document |
| 7805 | * with "standalone='yes'", the Name given in the entity reference |
| 7806 | * must match that in an entity declaration, except that well-formed |
| 7807 | * documents need not declare any of the following entities: amp, lt, |
| 7808 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7809 | * any reference to it. Similarly, the declaration of a general entity |
| 7810 | * must precede any reference to it which appears in a default value in an |
| 7811 | * attribute-list declaration. Note that if entities are declared in the |
| 7812 | * external subset or in external parameter entities, a non-validating |
| 7813 | * processor is not obligated to read and process their declarations; |
| 7814 | * for such documents, the rule that an entity must be declared is a |
| 7815 | * well-formedness constraint only if standalone='yes'. |
| 7816 | * |
| 7817 | * [ WFC: Parsed Entity ] |
| 7818 | * An entity reference must not contain the name of an unparsed entity |
| 7819 | * |
| 7820 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7821 | * is updated to the current location in the string. |
| 7822 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7823 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7824 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7825 | xmlChar *name; |
| 7826 | const xmlChar *ptr; |
| 7827 | xmlChar cur; |
| 7828 | xmlEntityPtr ent = NULL; |
| 7829 | |
| 7830 | if ((str == NULL) || (*str == NULL)) |
| 7831 | return(NULL); |
| 7832 | ptr = *str; |
| 7833 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7834 | if (cur != '&') |
| 7835 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7836 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7837 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7838 | name = xmlParseStringName(ctxt, &ptr); |
| 7839 | if (name == NULL) { |
| 7840 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7841 | "xmlParseStringEntityRef: no name\n"); |
| 7842 | *str = ptr; |
| 7843 | return(NULL); |
| 7844 | } |
| 7845 | if (*ptr != ';') { |
| 7846 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7847 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7848 | *str = ptr; |
| 7849 | return(NULL); |
| 7850 | } |
| 7851 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7852 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7853 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7854 | /* |
Jan Pokorný | 9a85d40 | 2013-11-29 23:26:25 +0100 | [diff] [blame] | 7855 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7856 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7857 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7858 | ent = xmlGetPredefinedEntity(name); |
| 7859 | if (ent != NULL) { |
| 7860 | xmlFree(name); |
| 7861 | *str = ptr; |
| 7862 | return(ent); |
| 7863 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7864 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7865 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7866 | /* |
| 7867 | * Increate the number of entity references parsed |
| 7868 | */ |
| 7869 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7870 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7871 | /* |
| 7872 | * Ask first SAX for entity resolution, otherwise try the |
| 7873 | * entities which may have stored in the parser context. |
| 7874 | */ |
| 7875 | if (ctxt->sax != NULL) { |
| 7876 | if (ctxt->sax->getEntity != NULL) |
| 7877 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7878 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7879 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7880 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7881 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7882 | } |
| 7883 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7884 | if (ctxt->instate == XML_PARSER_EOF) { |
| 7885 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 7886 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7887 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7888 | |
| 7889 | /* |
| 7890 | * [ WFC: Entity Declared ] |
| 7891 | * In a document without any DTD, a document with only an |
| 7892 | * internal DTD subset which contains no parameter entity |
| 7893 | * references, or a document with "standalone='yes'", the |
| 7894 | * Name given in the entity reference must match that in an |
| 7895 | * entity declaration, except that well-formed documents |
| 7896 | * need not declare any of the following entities: amp, lt, |
| 7897 | * gt, apos, quot. |
| 7898 | * The declaration of a parameter entity must precede any |
| 7899 | * reference to it. |
| 7900 | * Similarly, the declaration of a general entity must |
| 7901 | * precede any reference to it which appears in a default |
| 7902 | * value in an attribute-list declaration. Note that if |
| 7903 | * entities are declared in the external subset or in |
| 7904 | * external parameter entities, a non-validating processor |
| 7905 | * is not obligated to read and process their declarations; |
| 7906 | * for such documents, the rule that an entity must be |
| 7907 | * declared is a well-formedness constraint only if |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7908 | * standalone='yes'. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7909 | */ |
| 7910 | if (ent == NULL) { |
| 7911 | if ((ctxt->standalone == 1) || |
| 7912 | ((ctxt->hasExternalSubset == 0) && |
| 7913 | (ctxt->hasPErefs == 0))) { |
| 7914 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7915 | "Entity '%s' not defined\n", name); |
| 7916 | } else { |
| 7917 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7918 | "Entity '%s' not defined\n", |
| 7919 | name); |
| 7920 | } |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 7921 | xmlParserEntityCheck(ctxt, 0, ent, 0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7922 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7923 | } |
| 7924 | |
| 7925 | /* |
| 7926 | * [ WFC: Parsed Entity ] |
| 7927 | * An entity reference must not contain the name of an |
| 7928 | * unparsed entity |
| 7929 | */ |
| 7930 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7931 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7932 | "Entity reference to unparsed entity %s\n", name); |
| 7933 | } |
| 7934 | |
| 7935 | /* |
| 7936 | * [ WFC: No External Entity References ] |
| 7937 | * Attribute values cannot contain direct or indirect |
| 7938 | * entity references to external entities. |
| 7939 | */ |
| 7940 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7941 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7942 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7943 | "Attribute references external entity '%s'\n", name); |
| 7944 | } |
| 7945 | /* |
| 7946 | * [ WFC: No < in Attribute Values ] |
| 7947 | * The replacement text of any entity referred to directly or |
| 7948 | * indirectly in an attribute value (other than "<") must |
| 7949 | * not contain a <. |
| 7950 | */ |
| 7951 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7952 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7953 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7954 | (xmlStrchr(ent->content, '<'))) { |
| 7955 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7956 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7957 | name); |
| 7958 | } |
| 7959 | |
| 7960 | /* |
| 7961 | * Internal check, no parameter entities here ... |
| 7962 | */ |
| 7963 | else { |
| 7964 | switch (ent->etype) { |
| 7965 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7966 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7967 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7968 | "Attempt to reference the parameter entity '%s'\n", |
| 7969 | name); |
| 7970 | break; |
| 7971 | default: |
| 7972 | break; |
| 7973 | } |
| 7974 | } |
| 7975 | |
| 7976 | /* |
| 7977 | * [ WFC: No Recursion ] |
| 7978 | * A parsed entity must not contain a recursive reference |
| 7979 | * to itself, either directly or indirectly. |
| 7980 | * Done somewhere else |
| 7981 | */ |
| 7982 | |
| 7983 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7984 | *str = ptr; |
| 7985 | return(ent); |
| 7986 | } |
| 7987 | |
| 7988 | /** |
| 7989 | * xmlParsePEReference: |
| 7990 | * @ctxt: an XML parser context |
| 7991 | * |
| 7992 | * parse PEReference declarations |
| 7993 | * The entity content is handled directly by pushing it's content as |
| 7994 | * a new input stream. |
| 7995 | * |
| 7996 | * [69] PEReference ::= '%' Name ';' |
| 7997 | * |
| 7998 | * [ WFC: No Recursion ] |
| 7999 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8000 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8001 | * |
| 8002 | * [ WFC: Entity Declared ] |
| 8003 | * In a document without any DTD, a document with only an internal DTD |
| 8004 | * subset which contains no parameter entity references, or a document |
| 8005 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 8006 | * entity must precede any reference to it... |
| 8007 | * |
| 8008 | * [ VC: Entity Declared ] |
| 8009 | * In a document with an external subset or external parameter entities |
| 8010 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 8011 | * must precede any reference to it... |
| 8012 | * |
| 8013 | * [ WFC: In DTD ] |
| 8014 | * Parameter-entity references may only appear in the DTD. |
| 8015 | * NOTE: misleading but this is handled. |
| 8016 | */ |
| 8017 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 8018 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 8019 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8020 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8021 | xmlEntityPtr entity = NULL; |
| 8022 | xmlParserInputPtr input; |
| 8023 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8024 | if (RAW != '%') |
| 8025 | return; |
| 8026 | NEXT; |
| 8027 | name = xmlParseName(ctxt); |
| 8028 | if (name == NULL) { |
| 8029 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8030 | "xmlParsePEReference: no name\n"); |
| 8031 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8032 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8033 | if (RAW != ';') { |
| 8034 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 8035 | return; |
| 8036 | } |
| 8037 | |
| 8038 | NEXT; |
| 8039 | |
| 8040 | /* |
| 8041 | * Increate the number of entity references parsed |
| 8042 | */ |
| 8043 | ctxt->nbentities++; |
| 8044 | |
| 8045 | /* |
| 8046 | * Request the entity from SAX |
| 8047 | */ |
| 8048 | if ((ctxt->sax != NULL) && |
| 8049 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8050 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 8051 | if (ctxt->instate == XML_PARSER_EOF) |
| 8052 | return; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8053 | if (entity == NULL) { |
| 8054 | /* |
| 8055 | * [ WFC: Entity Declared ] |
| 8056 | * In a document without any DTD, a document with only an |
| 8057 | * internal DTD subset which contains no parameter entity |
| 8058 | * references, or a document with "standalone='yes'", ... |
| 8059 | * ... The declaration of a parameter entity must precede |
| 8060 | * any reference to it... |
| 8061 | */ |
| 8062 | if ((ctxt->standalone == 1) || |
| 8063 | ((ctxt->hasExternalSubset == 0) && |
| 8064 | (ctxt->hasPErefs == 0))) { |
| 8065 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 8066 | "PEReference: %%%s; not found\n", |
| 8067 | name); |
| 8068 | } else { |
| 8069 | /* |
| 8070 | * [ VC: Entity Declared ] |
| 8071 | * In a document with an external subset or external |
| 8072 | * parameter entities with "standalone='no'", ... |
| 8073 | * ... The declaration of a parameter entity must |
| 8074 | * precede any reference to it... |
| 8075 | */ |
| 8076 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8077 | "PEReference: %%%s; not found\n", |
| 8078 | name, NULL); |
| 8079 | ctxt->valid = 0; |
| 8080 | } |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 8081 | xmlParserEntityCheck(ctxt, 0, NULL, 0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8082 | } else { |
| 8083 | /* |
| 8084 | * Internal checking in case the entity quest barfed |
| 8085 | */ |
| 8086 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8087 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8088 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8089 | "Internal: %%%s; is not a parameter entity\n", |
| 8090 | name, NULL); |
| 8091 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 8092 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 8093 | if (xmlPushInput(ctxt, input) < 0) |
| 8094 | return; |
| 8095 | } else { |
| 8096 | /* |
| 8097 | * TODO !!! |
| 8098 | * handle the extra spaces added before and after |
| 8099 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 8100 | */ |
| 8101 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8102 | if (xmlPushInput(ctxt, input) < 0) |
| 8103 | return; |
| 8104 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8105 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 8106 | (IS_BLANK_CH(NXT(5)))) { |
| 8107 | xmlParseTextDecl(ctxt); |
| 8108 | if (ctxt->errNo == |
| 8109 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 8110 | /* |
| 8111 | * The XML REC instructs us to stop parsing |
| 8112 | * right here |
| 8113 | */ |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 8114 | xmlHaltParser(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8115 | return; |
| 8116 | } |
| 8117 | } |
| 8118 | } |
| 8119 | } |
| 8120 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
| 8123 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8124 | * xmlLoadEntityContent: |
| 8125 | * @ctxt: an XML parser context |
| 8126 | * @entity: an unloaded system entity |
| 8127 | * |
| 8128 | * Load the original content of the given system entity from the |
| 8129 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 8130 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 8131 | * |
| 8132 | * Returns 0 in case of success and -1 in case of failure |
| 8133 | */ |
| 8134 | static int |
| 8135 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 8136 | xmlParserInputPtr input; |
| 8137 | xmlBufferPtr buf; |
| 8138 | int l, c; |
| 8139 | int count = 0; |
| 8140 | |
| 8141 | if ((ctxt == NULL) || (entity == NULL) || |
| 8142 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8143 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 8144 | (entity->content != NULL)) { |
| 8145 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8146 | "xmlLoadEntityContent parameter error"); |
| 8147 | return(-1); |
| 8148 | } |
| 8149 | |
| 8150 | if (xmlParserDebugEntities) |
| 8151 | xmlGenericError(xmlGenericErrorContext, |
| 8152 | "Reading %s entity content input\n", entity->name); |
| 8153 | |
| 8154 | buf = xmlBufferCreate(); |
| 8155 | if (buf == NULL) { |
| 8156 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8157 | "xmlLoadEntityContent parameter error"); |
| 8158 | return(-1); |
| 8159 | } |
| 8160 | |
| 8161 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8162 | if (input == NULL) { |
| 8163 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8164 | "xmlLoadEntityContent input error"); |
| 8165 | xmlBufferFree(buf); |
| 8166 | return(-1); |
| 8167 | } |
| 8168 | |
| 8169 | /* |
| 8170 | * Push the entity as the current input, read char by char |
| 8171 | * saving to the buffer until the end of the entity or an error |
| 8172 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 8173 | if (xmlPushInput(ctxt, input) < 0) { |
| 8174 | xmlBufferFree(buf); |
| 8175 | return(-1); |
| 8176 | } |
| 8177 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8178 | GROW; |
| 8179 | c = CUR_CHAR(l); |
| 8180 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 8181 | (IS_CHAR(c))) { |
| 8182 | xmlBufferAdd(buf, ctxt->input->cur, l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8183 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8184 | count = 0; |
| 8185 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8186 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8187 | xmlBufferFree(buf); |
| 8188 | return(-1); |
| 8189 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8190 | } |
| 8191 | NEXTL(l); |
| 8192 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8193 | if (c == 0) { |
| 8194 | count = 0; |
| 8195 | GROW; |
| 8196 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8197 | xmlBufferFree(buf); |
| 8198 | return(-1); |
| 8199 | } |
| 8200 | c = CUR_CHAR(l); |
| 8201 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8202 | } |
| 8203 | |
| 8204 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 8205 | xmlPopInput(ctxt); |
| 8206 | } else if (!IS_CHAR(c)) { |
| 8207 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 8208 | "xmlLoadEntityContent: invalid char value %d\n", |
| 8209 | c); |
| 8210 | xmlBufferFree(buf); |
| 8211 | return(-1); |
| 8212 | } |
| 8213 | entity->content = buf->content; |
| 8214 | buf->content = NULL; |
| 8215 | xmlBufferFree(buf); |
| 8216 | |
| 8217 | return(0); |
| 8218 | } |
| 8219 | |
| 8220 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8221 | * xmlParseStringPEReference: |
| 8222 | * @ctxt: an XML parser context |
| 8223 | * @str: a pointer to an index in the string |
| 8224 | * |
| 8225 | * parse PEReference declarations |
| 8226 | * |
| 8227 | * [69] PEReference ::= '%' Name ';' |
| 8228 | * |
| 8229 | * [ WFC: No Recursion ] |
| 8230 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8231 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8232 | * |
| 8233 | * [ WFC: Entity Declared ] |
| 8234 | * In a document without any DTD, a document with only an internal DTD |
| 8235 | * subset which contains no parameter entity references, or a document |
| 8236 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 8237 | * entity must precede any reference to it... |
| 8238 | * |
| 8239 | * [ VC: Entity Declared ] |
| 8240 | * In a document with an external subset or external parameter entities |
| 8241 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 8242 | * must precede any reference to it... |
| 8243 | * |
| 8244 | * [ WFC: In DTD ] |
| 8245 | * Parameter-entity references may only appear in the DTD. |
| 8246 | * NOTE: misleading but this is handled. |
| 8247 | * |
| 8248 | * Returns the string of the entity content. |
| 8249 | * str is updated to the current value of the index |
| 8250 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 8251 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8252 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 8253 | const xmlChar *ptr; |
| 8254 | xmlChar cur; |
| 8255 | xmlChar *name; |
| 8256 | xmlEntityPtr entity = NULL; |
| 8257 | |
| 8258 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 8259 | ptr = *str; |
| 8260 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8261 | if (cur != '%') |
| 8262 | return(NULL); |
| 8263 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8264 | name = xmlParseStringName(ctxt, &ptr); |
| 8265 | if (name == NULL) { |
| 8266 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8267 | "xmlParseStringPEReference: no name\n"); |
| 8268 | *str = ptr; |
| 8269 | return(NULL); |
| 8270 | } |
| 8271 | cur = *ptr; |
| 8272 | if (cur != ';') { |
| 8273 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 8274 | xmlFree(name); |
| 8275 | *str = ptr; |
| 8276 | return(NULL); |
| 8277 | } |
| 8278 | ptr++; |
| 8279 | |
| 8280 | /* |
| 8281 | * Increate the number of entity references parsed |
| 8282 | */ |
| 8283 | ctxt->nbentities++; |
| 8284 | |
| 8285 | /* |
| 8286 | * Request the entity from SAX |
| 8287 | */ |
| 8288 | if ((ctxt->sax != NULL) && |
| 8289 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8290 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 8291 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8292 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 8293 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8294 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8295 | if (entity == NULL) { |
| 8296 | /* |
| 8297 | * [ WFC: Entity Declared ] |
| 8298 | * In a document without any DTD, a document with only an |
| 8299 | * internal DTD subset which contains no parameter entity |
| 8300 | * references, or a document with "standalone='yes'", ... |
| 8301 | * ... The declaration of a parameter entity must precede |
| 8302 | * any reference to it... |
| 8303 | */ |
| 8304 | if ((ctxt->standalone == 1) || |
| 8305 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 8306 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 8307 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8308 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8309 | /* |
| 8310 | * [ VC: Entity Declared ] |
| 8311 | * In a document with an external subset or external |
| 8312 | * parameter entities with "standalone='no'", ... |
| 8313 | * ... The declaration of a parameter entity must |
| 8314 | * precede any reference to it... |
| 8315 | */ |
| 8316 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8317 | "PEReference: %%%s; not found\n", |
| 8318 | name, NULL); |
| 8319 | ctxt->valid = 0; |
| 8320 | } |
Daniel Veillard | be2a7ed | 2014-10-16 13:59:47 +0800 | [diff] [blame] | 8321 | xmlParserEntityCheck(ctxt, 0, NULL, 0); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8322 | } else { |
| 8323 | /* |
| 8324 | * Internal checking in case the entity quest barfed |
| 8325 | */ |
| 8326 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8327 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8328 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8329 | "%%%s; is not a parameter entity\n", |
| 8330 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8331 | } |
| 8332 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8333 | ctxt->hasPErefs = 1; |
| 8334 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8335 | *str = ptr; |
| 8336 | return(entity); |
| 8337 | } |
| 8338 | |
| 8339 | /** |
| 8340 | * xmlParseDocTypeDecl: |
| 8341 | * @ctxt: an XML parser context |
| 8342 | * |
| 8343 | * parse a DOCTYPE declaration |
| 8344 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8345 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8346 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8347 | * |
| 8348 | * [ VC: Root Element Type ] |
| 8349 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8350 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8351 | */ |
| 8352 | |
| 8353 | void |
| 8354 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8355 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8356 | xmlChar *ExternalID = NULL; |
| 8357 | xmlChar *URI = NULL; |
| 8358 | |
| 8359 | /* |
| 8360 | * We know that '<!DOCTYPE' has been detected. |
| 8361 | */ |
| 8362 | SKIP(9); |
| 8363 | |
| 8364 | SKIP_BLANKS; |
| 8365 | |
| 8366 | /* |
| 8367 | * Parse the DOCTYPE name. |
| 8368 | */ |
| 8369 | name = xmlParseName(ctxt); |
| 8370 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8371 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8372 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8373 | } |
| 8374 | ctxt->intSubName = name; |
| 8375 | |
| 8376 | SKIP_BLANKS; |
| 8377 | |
| 8378 | /* |
| 8379 | * Check for SystemID and ExternalID |
| 8380 | */ |
| 8381 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 8382 | |
| 8383 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 8384 | ctxt->hasExternalSubset = 1; |
| 8385 | } |
| 8386 | ctxt->extSubURI = URI; |
| 8387 | ctxt->extSubSystem = ExternalID; |
| 8388 | |
| 8389 | SKIP_BLANKS; |
| 8390 | |
| 8391 | /* |
| 8392 | * Create and update the internal subset. |
| 8393 | */ |
| 8394 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 8395 | (!ctxt->disableSAX)) |
| 8396 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8397 | if (ctxt->instate == XML_PARSER_EOF) |
| 8398 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8399 | |
| 8400 | /* |
| 8401 | * Is there any internal subset declarations ? |
| 8402 | * they are handled separately in xmlParseInternalSubset() |
| 8403 | */ |
| 8404 | if (RAW == '[') |
| 8405 | return; |
| 8406 | |
| 8407 | /* |
| 8408 | * We should be at the end of the DOCTYPE declaration. |
| 8409 | */ |
| 8410 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8411 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8412 | } |
| 8413 | NEXT; |
| 8414 | } |
| 8415 | |
| 8416 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8417 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8418 | * @ctxt: an XML parser context |
| 8419 | * |
| 8420 | * parse the internal subset declaration |
| 8421 | * |
| 8422 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8423 | */ |
| 8424 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8425 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8426 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 8427 | /* |
| 8428 | * Is there any DTD definition ? |
| 8429 | */ |
| 8430 | if (RAW == '[') { |
| 8431 | ctxt->instate = XML_PARSER_DTD; |
| 8432 | NEXT; |
| 8433 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8434 | * Parse the succession of Markup declarations and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8435 | * PEReferences. |
| 8436 | * Subsequence (markupdecl | PEReference | S)* |
| 8437 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8438 | while ((RAW != ']') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8439 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8440 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8441 | |
| 8442 | SKIP_BLANKS; |
| 8443 | xmlParseMarkupDecl(ctxt); |
| 8444 | xmlParsePEReference(ctxt); |
| 8445 | |
| 8446 | /* |
| 8447 | * Pop-up of finished entities. |
| 8448 | */ |
| 8449 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8450 | xmlPopInput(ctxt); |
| 8451 | |
| 8452 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8453 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8454 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8455 | break; |
| 8456 | } |
| 8457 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8458 | if (RAW == ']') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8459 | NEXT; |
| 8460 | SKIP_BLANKS; |
| 8461 | } |
| 8462 | } |
| 8463 | |
| 8464 | /* |
| 8465 | * We should be at the end of the DOCTYPE declaration. |
| 8466 | */ |
| 8467 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8468 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | } |
| 8470 | NEXT; |
| 8471 | } |
| 8472 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8473 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8474 | /** |
| 8475 | * xmlParseAttribute: |
| 8476 | * @ctxt: an XML parser context |
| 8477 | * @value: a xmlChar ** used to store the value of the attribute |
| 8478 | * |
| 8479 | * parse an attribute |
| 8480 | * |
| 8481 | * [41] Attribute ::= Name Eq AttValue |
| 8482 | * |
| 8483 | * [ WFC: No External Entity References ] |
| 8484 | * Attribute values cannot contain direct or indirect entity references |
| 8485 | * to external entities. |
| 8486 | * |
| 8487 | * [ WFC: No < in Attribute Values ] |
| 8488 | * The replacement text of any entity referred to directly or indirectly in |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8489 | * an attribute value (other than "<") must not contain a <. |
| 8490 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8491 | * [ VC: Attribute Value Type ] |
| 8492 | * The attribute must have been declared; the value must be of the type |
| 8493 | * declared for it. |
| 8494 | * |
| 8495 | * [25] Eq ::= S? '=' S? |
| 8496 | * |
| 8497 | * With namespace: |
| 8498 | * |
| 8499 | * [NS 11] Attribute ::= QName Eq AttValue |
| 8500 | * |
| 8501 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 8502 | * definition. |
| 8503 | * |
| 8504 | * Returns the attribute name, and the value in *value. |
| 8505 | */ |
| 8506 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8507 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8508 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8509 | const xmlChar *name; |
| 8510 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8511 | |
| 8512 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 8513 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8514 | name = xmlParseName(ctxt); |
| 8515 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8516 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8517 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8518 | return(NULL); |
| 8519 | } |
| 8520 | |
| 8521 | /* |
| 8522 | * read the value |
| 8523 | */ |
| 8524 | SKIP_BLANKS; |
| 8525 | if (RAW == '=') { |
| 8526 | NEXT; |
| 8527 | SKIP_BLANKS; |
| 8528 | val = xmlParseAttValue(ctxt); |
| 8529 | ctxt->instate = XML_PARSER_CONTENT; |
| 8530 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8531 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8532 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8533 | return(NULL); |
| 8534 | } |
| 8535 | |
| 8536 | /* |
| 8537 | * Check that xml:lang conforms to the specification |
| 8538 | * No more registered as an error, just generate a warning now |
| 8539 | * since this was deprecated in XML second edition |
| 8540 | */ |
| 8541 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 8542 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8543 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8544 | "Malformed value for xml:lang : %s\n", |
| 8545 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8546 | } |
| 8547 | } |
| 8548 | |
| 8549 | /* |
| 8550 | * Check that xml:space conforms to the specification |
| 8551 | */ |
| 8552 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 8553 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 8554 | *(ctxt->space) = 0; |
| 8555 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 8556 | *(ctxt->space) = 1; |
| 8557 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8558 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 8559 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8560 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8561 | } |
| 8562 | } |
| 8563 | |
| 8564 | *value = val; |
| 8565 | return(name); |
| 8566 | } |
| 8567 | |
| 8568 | /** |
| 8569 | * xmlParseStartTag: |
| 8570 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8571 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8572 | * parse a start of tag either for rule element or |
| 8573 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8574 | * |
| 8575 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8576 | * |
| 8577 | * [ WFC: Unique Att Spec ] |
| 8578 | * No attribute name may appear more than once in the same start-tag or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8579 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | * |
| 8581 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8582 | * |
| 8583 | * [ WFC: Unique Att Spec ] |
| 8584 | * No attribute name may appear more than once in the same start-tag or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8585 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8586 | * |
| 8587 | * With namespace: |
| 8588 | * |
| 8589 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8590 | * |
| 8591 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8592 | * |
| 8593 | * Returns the element name parsed |
| 8594 | */ |
| 8595 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8596 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8597 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8598 | const xmlChar *name; |
| 8599 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8600 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8601 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8603 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8604 | int i; |
| 8605 | |
| 8606 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8607 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8608 | |
| 8609 | name = xmlParseName(ctxt); |
| 8610 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8611 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8612 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8613 | return(NULL); |
| 8614 | } |
| 8615 | |
| 8616 | /* |
| 8617 | * Now parse the attributes, it ends up with the ending |
| 8618 | * |
| 8619 | * (S Attribute)* S? |
| 8620 | */ |
| 8621 | SKIP_BLANKS; |
| 8622 | GROW; |
| 8623 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8624 | while (((RAW != '>') && |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8625 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8626 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8627 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8628 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8629 | |
| 8630 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 8631 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8632 | /* |
| 8633 | * [ WFC: Unique Att Spec ] |
| 8634 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8635 | * start-tag or empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8636 | */ |
| 8637 | for (i = 0; i < nbatts;i += 2) { |
| 8638 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8639 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8640 | xmlFree(attvalue); |
| 8641 | goto failed; |
| 8642 | } |
| 8643 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8644 | /* |
| 8645 | * Add the pair to atts |
| 8646 | */ |
| 8647 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8648 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8649 | atts = (const xmlChar **) |
| 8650 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8651 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8652 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8653 | if (attvalue != NULL) |
| 8654 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8655 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8656 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8657 | ctxt->atts = atts; |
| 8658 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8659 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8660 | const xmlChar **n; |
| 8661 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8662 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8663 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8664 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8665 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8666 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8667 | if (attvalue != NULL) |
| 8668 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8669 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8670 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8671 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8672 | ctxt->atts = atts; |
| 8673 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8674 | } |
| 8675 | atts[nbatts++] = attname; |
| 8676 | atts[nbatts++] = attvalue; |
| 8677 | atts[nbatts] = NULL; |
| 8678 | atts[nbatts + 1] = NULL; |
| 8679 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8680 | if (attvalue != NULL) |
| 8681 | xmlFree(attvalue); |
| 8682 | } |
| 8683 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 8684 | failed: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8685 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8686 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8687 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8688 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8689 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8690 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8691 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8692 | } |
| 8693 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8694 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8695 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8696 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8697 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8698 | break; |
| 8699 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8700 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8701 | GROW; |
| 8702 | } |
| 8703 | |
| 8704 | /* |
| 8705 | * SAX: Start of Element ! |
| 8706 | */ |
| 8707 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8708 | (!ctxt->disableSAX)) { |
| 8709 | if (nbatts > 0) |
| 8710 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8711 | else |
| 8712 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8714 | |
| 8715 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8716 | /* Free only the content strings */ |
| 8717 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8718 | if (atts[i] != NULL) |
| 8719 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8720 | } |
| 8721 | return(name); |
| 8722 | } |
| 8723 | |
| 8724 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8725 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8726 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8727 | * @line: line of the start tag |
| 8728 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8729 | * |
| 8730 | * parse an end of tag |
| 8731 | * |
| 8732 | * [42] ETag ::= '</' Name S? '>' |
| 8733 | * |
| 8734 | * With namespace |
| 8735 | * |
| 8736 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8737 | */ |
| 8738 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8739 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8740 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8741 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8742 | |
| 8743 | GROW; |
| 8744 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8745 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8746 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8747 | return; |
| 8748 | } |
| 8749 | SKIP(2); |
| 8750 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8751 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8752 | |
| 8753 | /* |
| 8754 | * We should definitely be at the ending "S? '>'" part |
| 8755 | */ |
| 8756 | GROW; |
| 8757 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8758 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8759 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8760 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8761 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8762 | |
| 8763 | /* |
| 8764 | * [ WFC: Element Type Match ] |
| 8765 | * The Name in an element's end-tag must match the element type in the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8766 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8767 | * |
| 8768 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8769 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8770 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8771 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8772 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8773 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8774 | } |
| 8775 | |
| 8776 | /* |
| 8777 | * SAX: End of Tag |
| 8778 | */ |
| 8779 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8780 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8781 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8782 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8783 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8784 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8785 | return; |
| 8786 | } |
| 8787 | |
| 8788 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8789 | * xmlParseEndTag: |
| 8790 | * @ctxt: an XML parser context |
| 8791 | * |
| 8792 | * parse an end of tag |
| 8793 | * |
| 8794 | * [42] ETag ::= '</' Name S? '>' |
| 8795 | * |
| 8796 | * With namespace |
| 8797 | * |
| 8798 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8799 | */ |
| 8800 | |
| 8801 | void |
| 8802 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8803 | xmlParseEndTag1(ctxt, 0); |
| 8804 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8805 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8806 | |
| 8807 | /************************************************************************ |
| 8808 | * * |
| 8809 | * SAX 2 specific operations * |
| 8810 | * * |
| 8811 | ************************************************************************/ |
| 8812 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8813 | /* |
| 8814 | * xmlGetNamespace: |
| 8815 | * @ctxt: an XML parser context |
| 8816 | * @prefix: the prefix to lookup |
| 8817 | * |
| 8818 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8819 | * The prefix must come from the @ctxt->dict dictionnary |
| 8820 | * |
| 8821 | * Returns the namespace name or NULL if not bound |
| 8822 | */ |
| 8823 | static const xmlChar * |
| 8824 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8825 | int i; |
| 8826 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8827 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8828 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8829 | if (ctxt->nsTab[i] == prefix) { |
| 8830 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8831 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8832 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8833 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8834 | return(NULL); |
| 8835 | } |
| 8836 | |
| 8837 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8838 | * xmlParseQName: |
| 8839 | * @ctxt: an XML parser context |
| 8840 | * @prefix: pointer to store the prefix part |
| 8841 | * |
| 8842 | * parse an XML Namespace QName |
| 8843 | * |
| 8844 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8845 | * [7] Prefix ::= NCName |
| 8846 | * [8] LocalPart ::= NCName |
| 8847 | * |
| 8848 | * Returns the Name parsed or NULL |
| 8849 | */ |
| 8850 | |
| 8851 | static const xmlChar * |
| 8852 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8853 | const xmlChar *l, *p; |
| 8854 | |
| 8855 | GROW; |
| 8856 | |
| 8857 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8858 | if (l == NULL) { |
| 8859 | if (CUR == ':') { |
| 8860 | l = xmlParseName(ctxt); |
| 8861 | if (l != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8862 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8863 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8864 | *prefix = NULL; |
| 8865 | return(l); |
| 8866 | } |
| 8867 | } |
| 8868 | return(NULL); |
| 8869 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8870 | if (CUR == ':') { |
| 8871 | NEXT; |
| 8872 | p = l; |
| 8873 | l = xmlParseNCName(ctxt); |
| 8874 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8875 | xmlChar *tmp; |
| 8876 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8877 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8878 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8879 | l = xmlParseNmtoken(ctxt); |
| 8880 | if (l == NULL) |
| 8881 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8882 | else { |
| 8883 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8884 | xmlFree((char *)l); |
| 8885 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8886 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8887 | if (tmp != NULL) xmlFree(tmp); |
| 8888 | *prefix = NULL; |
| 8889 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8890 | } |
| 8891 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8892 | xmlChar *tmp; |
| 8893 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8894 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8895 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8896 | NEXT; |
| 8897 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8898 | if (tmp != NULL) { |
| 8899 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8900 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8901 | if (tmp != NULL) xmlFree(tmp); |
| 8902 | *prefix = p; |
| 8903 | return(l); |
| 8904 | } |
| 8905 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8906 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8907 | if (tmp != NULL) xmlFree(tmp); |
| 8908 | *prefix = p; |
| 8909 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8910 | } |
| 8911 | *prefix = p; |
| 8912 | } else |
| 8913 | *prefix = NULL; |
| 8914 | return(l); |
| 8915 | } |
| 8916 | |
| 8917 | /** |
| 8918 | * xmlParseQNameAndCompare: |
| 8919 | * @ctxt: an XML parser context |
| 8920 | * @name: the localname |
| 8921 | * @prefix: the prefix, if any. |
| 8922 | * |
| 8923 | * parse an XML name and compares for match |
| 8924 | * (specialized for endtag parsing) |
| 8925 | * |
| 8926 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8927 | * and the name for mismatch |
| 8928 | */ |
| 8929 | |
| 8930 | static const xmlChar * |
| 8931 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8932 | xmlChar const *prefix) { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8933 | const xmlChar *cmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8934 | const xmlChar *in; |
| 8935 | const xmlChar *ret; |
| 8936 | const xmlChar *prefix2; |
| 8937 | |
| 8938 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8939 | |
| 8940 | GROW; |
| 8941 | in = ctxt->input->cur; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8942 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8943 | cmp = prefix; |
| 8944 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8945 | ++in; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8946 | ++cmp; |
| 8947 | } |
| 8948 | if ((*cmp == 0) && (*in == ':')) { |
| 8949 | in++; |
| 8950 | cmp = name; |
| 8951 | while (*in != 0 && *in == *cmp) { |
| 8952 | ++in; |
| 8953 | ++cmp; |
| 8954 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8955 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8956 | /* success */ |
| 8957 | ctxt->input->cur = in; |
| 8958 | return((const xmlChar*) 1); |
| 8959 | } |
| 8960 | } |
| 8961 | /* |
| 8962 | * all strings coms from the dictionary, equality can be done directly |
| 8963 | */ |
| 8964 | ret = xmlParseQName (ctxt, &prefix2); |
| 8965 | if ((ret == name) && (prefix == prefix2)) |
| 8966 | return((const xmlChar*) 1); |
| 8967 | return ret; |
| 8968 | } |
| 8969 | |
| 8970 | /** |
| 8971 | * xmlParseAttValueInternal: |
| 8972 | * @ctxt: an XML parser context |
| 8973 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8974 | * @alloc: whether the attribute was reallocated as a new string |
| 8975 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8976 | * |
| 8977 | * parse a value for an attribute. |
| 8978 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8979 | * directly from the data buffer. |
| 8980 | * |
| 8981 | * 3.3.3 Attribute-Value Normalization: |
| 8982 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8983 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8984 | * - a character reference is processed by appending the referenced |
| 8985 | * character to the attribute value |
| 8986 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8987 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8988 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8989 | * appending #x20 to the normalized value, except that only a single |
| 8990 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8991 | * parsed entity or the literal entity value of an internal parsed entity |
| 8992 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8993 | * If the declared value is not CDATA, then the XML processor must further |
| 8994 | * process the normalized attribute value by discarding any leading and |
| 8995 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8996 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8997 | * All attributes for which no declaration has been read should be treated |
| 8998 | * by a non-validating parser as if declared CDATA. |
| 8999 | * |
| 9000 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 9001 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 9002 | */ |
| 9003 | |
| 9004 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9005 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 9006 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9007 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9008 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9009 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9010 | xmlChar *ret = NULL; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9011 | int line, col; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9012 | |
| 9013 | GROW; |
| 9014 | in = (xmlChar *) CUR_PTR; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9015 | line = ctxt->input->line; |
| 9016 | col = ctxt->input->col; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9017 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9018 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9019 | return (NULL); |
| 9020 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9021 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9022 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9023 | /* |
| 9024 | * try to handle in this routine the most common case where no |
| 9025 | * allocation of a new string is required and where content is |
| 9026 | * pure ASCII. |
| 9027 | */ |
| 9028 | limit = *in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9029 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9030 | end = ctxt->input->end; |
| 9031 | start = in; |
| 9032 | if (in >= end) { |
| 9033 | const xmlChar *oldbase = ctxt->input->base; |
| 9034 | GROW; |
| 9035 | if (oldbase != ctxt->input->base) { |
| 9036 | long delta = ctxt->input->base - oldbase; |
| 9037 | start = start + delta; |
| 9038 | in = in + delta; |
| 9039 | } |
| 9040 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9041 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9042 | if (normalize) { |
| 9043 | /* |
| 9044 | * Skip any leading spaces |
| 9045 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9046 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9047 | ((*in == 0x20) || (*in == 0x9) || |
| 9048 | (*in == 0xA) || (*in == 0xD))) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9049 | if (*in == 0xA) { |
| 9050 | line++; col = 1; |
| 9051 | } else { |
| 9052 | col++; |
| 9053 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9054 | in++; |
| 9055 | start = in; |
| 9056 | if (in >= end) { |
| 9057 | const xmlChar *oldbase = ctxt->input->base; |
| 9058 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9059 | if (ctxt->instate == XML_PARSER_EOF) |
| 9060 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9061 | if (oldbase != ctxt->input->base) { |
| 9062 | long delta = ctxt->input->base - oldbase; |
| 9063 | start = start + delta; |
| 9064 | in = in + delta; |
| 9065 | } |
| 9066 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9067 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9068 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9069 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9070 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9071 | return(NULL); |
| 9072 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9073 | } |
| 9074 | } |
| 9075 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 9076 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9077 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9078 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 9079 | if (in >= end) { |
| 9080 | const xmlChar *oldbase = ctxt->input->base; |
| 9081 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9082 | if (ctxt->instate == XML_PARSER_EOF) |
| 9083 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9084 | if (oldbase != ctxt->input->base) { |
| 9085 | long delta = ctxt->input->base - oldbase; |
| 9086 | start = start + delta; |
| 9087 | in = in + delta; |
| 9088 | } |
| 9089 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9090 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9091 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9092 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9093 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9094 | return(NULL); |
| 9095 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9096 | } |
| 9097 | } |
| 9098 | last = in; |
| 9099 | /* |
| 9100 | * skip the trailing blanks |
| 9101 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 9102 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9103 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9104 | ((*in == 0x20) || (*in == 0x9) || |
| 9105 | (*in == 0xA) || (*in == 0xD))) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9106 | if (*in == 0xA) { |
| 9107 | line++, col = 1; |
| 9108 | } else { |
| 9109 | col++; |
| 9110 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9111 | in++; |
| 9112 | if (in >= end) { |
| 9113 | const xmlChar *oldbase = ctxt->input->base; |
| 9114 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9115 | if (ctxt->instate == XML_PARSER_EOF) |
| 9116 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9117 | if (oldbase != ctxt->input->base) { |
| 9118 | long delta = ctxt->input->base - oldbase; |
| 9119 | start = start + delta; |
| 9120 | in = in + delta; |
| 9121 | last = last + delta; |
| 9122 | } |
| 9123 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9124 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9125 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9126 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9127 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9128 | return(NULL); |
| 9129 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9130 | } |
| 9131 | } |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9132 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9133 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9134 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9135 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9136 | return(NULL); |
| 9137 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9138 | if (*in != limit) goto need_complex; |
| 9139 | } else { |
| 9140 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 9141 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 9142 | in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9143 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9144 | if (in >= end) { |
| 9145 | const xmlChar *oldbase = ctxt->input->base; |
| 9146 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9147 | if (ctxt->instate == XML_PARSER_EOF) |
| 9148 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9149 | if (oldbase != ctxt->input->base) { |
| 9150 | long delta = ctxt->input->base - oldbase; |
| 9151 | start = start + delta; |
| 9152 | in = in + delta; |
| 9153 | } |
| 9154 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9155 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9156 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9157 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9158 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9159 | return(NULL); |
| 9160 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9161 | } |
| 9162 | } |
| 9163 | last = in; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9164 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9165 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9166 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9167 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9168 | return(NULL); |
| 9169 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9170 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9171 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9172 | in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9173 | col++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9174 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9175 | *len = last - start; |
| 9176 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9177 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9178 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9179 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9180 | } |
| 9181 | CUR_PTR = in; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9182 | ctxt->input->line = line; |
| 9183 | ctxt->input->col = col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9184 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9185 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9186 | need_complex: |
| 9187 | if (alloc) *alloc = 1; |
| 9188 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9189 | } |
| 9190 | |
| 9191 | /** |
| 9192 | * xmlParseAttribute2: |
| 9193 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9194 | * @pref: the element prefix |
| 9195 | * @elem: the element name |
| 9196 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9197 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9198 | * @len: an int * to save the length of the attribute |
| 9199 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9200 | * |
| 9201 | * parse an attribute in the new SAX2 framework. |
| 9202 | * |
| 9203 | * Returns the attribute name, and the value in *value, . |
| 9204 | */ |
| 9205 | |
| 9206 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9207 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9208 | const xmlChar * pref, const xmlChar * elem, |
| 9209 | const xmlChar ** prefix, xmlChar ** value, |
| 9210 | int *len, int *alloc) |
| 9211 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9212 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 9213 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9214 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9215 | |
| 9216 | *value = NULL; |
| 9217 | GROW; |
| 9218 | name = xmlParseQName(ctxt, prefix); |
| 9219 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9220 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9221 | "error parsing attribute name\n"); |
| 9222 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9223 | } |
| 9224 | |
| 9225 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9226 | * get the type if needed |
| 9227 | */ |
| 9228 | if (ctxt->attsSpecial != NULL) { |
| 9229 | int type; |
| 9230 | |
| 9231 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9232 | pref, elem, *prefix, name); |
| 9233 | if (type != 0) |
| 9234 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9235 | } |
| 9236 | |
| 9237 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9238 | * read the value |
| 9239 | */ |
| 9240 | SKIP_BLANKS; |
| 9241 | if (RAW == '=') { |
| 9242 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9243 | SKIP_BLANKS; |
| 9244 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 9245 | if (normalize) { |
| 9246 | /* |
| 9247 | * Sometimes a second normalisation pass for spaces is needed |
| 9248 | * but that only happens if charrefs or entities refernces |
| 9249 | * have been used in the attribute value, i.e. the attribute |
| 9250 | * value have been extracted in an allocated string already. |
| 9251 | */ |
| 9252 | if (*alloc) { |
| 9253 | const xmlChar *val2; |
| 9254 | |
| 9255 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9256 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9257 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 9258 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9259 | } |
| 9260 | } |
| 9261 | } |
| 9262 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9263 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9264 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 9265 | "Specification mandate value for attribute %s\n", |
| 9266 | name); |
| 9267 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9268 | } |
| 9269 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9270 | if (*prefix == ctxt->str_xml) { |
| 9271 | /* |
| 9272 | * Check that xml:lang conforms to the specification |
| 9273 | * No more registered as an error, just generate a warning now |
| 9274 | * since this was deprecated in XML second edition |
| 9275 | */ |
| 9276 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 9277 | internal_val = xmlStrndup(val, *len); |
| 9278 | if (!xmlCheckLanguageID(internal_val)) { |
| 9279 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 9280 | "Malformed value for xml:lang : %s\n", |
| 9281 | internal_val, NULL); |
| 9282 | } |
| 9283 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9284 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9285 | /* |
| 9286 | * Check that xml:space conforms to the specification |
| 9287 | */ |
| 9288 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 9289 | internal_val = xmlStrndup(val, *len); |
| 9290 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 9291 | *(ctxt->space) = 0; |
| 9292 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 9293 | *(ctxt->space) = 1; |
| 9294 | else { |
| 9295 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 9296 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 9297 | internal_val, NULL); |
| 9298 | } |
| 9299 | } |
| 9300 | if (internal_val) { |
| 9301 | xmlFree(internal_val); |
| 9302 | } |
| 9303 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9304 | |
| 9305 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9306 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9307 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9308 | /** |
| 9309 | * xmlParseStartTag2: |
| 9310 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9311 | * |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9312 | * parse a start of tag either for rule element or |
| 9313 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 9314 | * This routine is called when running SAX2 parsing |
| 9315 | * |
| 9316 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 9317 | * |
| 9318 | * [ WFC: Unique Att Spec ] |
| 9319 | * No attribute name may appear more than once in the same start-tag or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9320 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9321 | * |
| 9322 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 9323 | * |
| 9324 | * [ WFC: Unique Att Spec ] |
| 9325 | * No attribute name may appear more than once in the same start-tag or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9326 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9327 | * |
| 9328 | * With namespace: |
| 9329 | * |
| 9330 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 9331 | * |
| 9332 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 9333 | * |
| 9334 | * Returns the element name parsed |
| 9335 | */ |
| 9336 | |
| 9337 | static const xmlChar * |
| 9338 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9339 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9340 | const xmlChar *localname; |
| 9341 | const xmlChar *prefix; |
| 9342 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9343 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9344 | const xmlChar *nsname; |
| 9345 | xmlChar *attvalue; |
| 9346 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9347 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9348 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9349 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9350 | const xmlChar *base; |
| 9351 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9352 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9353 | |
| 9354 | if (RAW != '<') return(NULL); |
| 9355 | NEXT1; |
| 9356 | |
| 9357 | /* |
| 9358 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 9359 | * point since the attribute values may be stored as pointers to |
| 9360 | * the buffer and calling SHRINK would destroy them ! |
| 9361 | * The Shrinking is only possible once the full set of attribute |
| 9362 | * callbacks have been done. |
| 9363 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9364 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9365 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9366 | base = ctxt->input->base; |
| 9367 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9368 | oldline = ctxt->input->line; |
| 9369 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9370 | nbatts = 0; |
| 9371 | nratts = 0; |
| 9372 | nbdef = 0; |
| 9373 | nbNs = 0; |
| 9374 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9375 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 9376 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9377 | |
| 9378 | localname = xmlParseQName(ctxt, &prefix); |
| 9379 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9380 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9381 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9382 | return(NULL); |
| 9383 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9384 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9385 | |
| 9386 | /* |
| 9387 | * Now parse the attributes, it ends up with the ending |
| 9388 | * |
| 9389 | * (S Attribute)* S? |
| 9390 | */ |
| 9391 | SKIP_BLANKS; |
| 9392 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9393 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9394 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9395 | while (((RAW != '>') && |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9396 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9397 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9398 | const xmlChar *q = CUR_PTR; |
| 9399 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9400 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9401 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9402 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 9403 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9404 | if (ctxt->input->base != base) { |
| 9405 | if ((attvalue != NULL) && (alloc != 0)) |
| 9406 | xmlFree(attvalue); |
| 9407 | attvalue = NULL; |
| 9408 | goto base_changed; |
| 9409 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9410 | if ((attname != NULL) && (attvalue != NULL)) { |
| 9411 | if (len < 0) len = xmlStrlen(attvalue); |
| 9412 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9413 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9414 | xmlURIPtr uri; |
| 9415 | |
Daniel Veillard | c836ba6 | 2014-07-14 16:39:50 +0800 | [diff] [blame] | 9416 | if (URL == NULL) { |
| 9417 | xmlErrMemory(ctxt, "dictionary allocation failure"); |
| 9418 | if ((attvalue != NULL) && (alloc != 0)) |
| 9419 | xmlFree(attvalue); |
| 9420 | return(NULL); |
| 9421 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9422 | if (*URL != 0) { |
| 9423 | uri = xmlParseURI((const char *) URL); |
| 9424 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9425 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9426 | "xmlns: '%s' is not a valid URI\n", |
| 9427 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9428 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 9429 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9430 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9431 | "xmlns: URI %s is not absolute\n", |
| 9432 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9433 | } |
| 9434 | xmlFreeURI(uri); |
| 9435 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9436 | if (URL == ctxt->str_xml_ns) { |
| 9437 | if (attname != ctxt->str_xml) { |
| 9438 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9439 | "xml namespace URI cannot be the default namespace\n", |
| 9440 | NULL, NULL, NULL); |
| 9441 | } |
| 9442 | goto skip_default_ns; |
| 9443 | } |
| 9444 | if ((len == 29) && |
| 9445 | (xmlStrEqual(URL, |
| 9446 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9447 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9448 | "reuse of the xmlns namespace name is forbidden\n", |
| 9449 | NULL, NULL, NULL); |
| 9450 | goto skip_default_ns; |
| 9451 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9452 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9453 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9454 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9455 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9456 | for (j = 1;j <= nbNs;j++) |
| 9457 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9458 | break; |
| 9459 | if (j <= nbNs) |
| 9460 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 9461 | else |
| 9462 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9463 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9464 | if (alloc != 0) xmlFree(attvalue); |
Dennis Filder | 7e9bbdf | 2014-10-06 20:34:14 +0800 | [diff] [blame] | 9465 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9466 | break; |
| 9467 | if (!IS_BLANK_CH(RAW)) { |
| 9468 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9469 | "attributes construct error\n"); |
| 9470 | break; |
| 9471 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9472 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9473 | continue; |
| 9474 | } |
| 9475 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9476 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9477 | xmlURIPtr uri; |
| 9478 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9479 | if (attname == ctxt->str_xml) { |
| 9480 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9481 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9482 | "xml namespace prefix mapped to wrong URI\n", |
| 9483 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9484 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9485 | /* |
| 9486 | * Do not keep a namespace definition node |
| 9487 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9488 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9489 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9490 | if (URL == ctxt->str_xml_ns) { |
| 9491 | if (attname != ctxt->str_xml) { |
| 9492 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9493 | "xml namespace URI mapped to wrong prefix\n", |
| 9494 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9495 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9496 | goto skip_ns; |
| 9497 | } |
| 9498 | if (attname == ctxt->str_xmlns) { |
| 9499 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9500 | "redefinition of the xmlns prefix is forbidden\n", |
| 9501 | NULL, NULL, NULL); |
| 9502 | goto skip_ns; |
| 9503 | } |
| 9504 | if ((len == 29) && |
| 9505 | (xmlStrEqual(URL, |
| 9506 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9507 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9508 | "reuse of the xmlns namespace name is forbidden\n", |
| 9509 | NULL, NULL, NULL); |
| 9510 | goto skip_ns; |
| 9511 | } |
| 9512 | if ((URL == NULL) || (URL[0] == 0)) { |
| 9513 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9514 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 9515 | attname, NULL, NULL); |
| 9516 | goto skip_ns; |
| 9517 | } else { |
| 9518 | uri = xmlParseURI((const char *) URL); |
| 9519 | if (uri == NULL) { |
| 9520 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9521 | "xmlns:%s: '%s' is not a valid URI\n", |
| 9522 | attname, URL, NULL); |
| 9523 | } else { |
| 9524 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 9525 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9526 | "xmlns:%s: URI %s is not absolute\n", |
| 9527 | attname, URL, NULL); |
| 9528 | } |
| 9529 | xmlFreeURI(uri); |
| 9530 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9533 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9534 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9535 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9536 | for (j = 1;j <= nbNs;j++) |
| 9537 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9538 | break; |
| 9539 | if (j <= nbNs) |
| 9540 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 9541 | else |
| 9542 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9543 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9544 | if (alloc != 0) xmlFree(attvalue); |
Dennis Filder | 7e9bbdf | 2014-10-06 20:34:14 +0800 | [diff] [blame] | 9545 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9546 | break; |
| 9547 | if (!IS_BLANK_CH(RAW)) { |
| 9548 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9549 | "attributes construct error\n"); |
| 9550 | break; |
| 9551 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9552 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 9553 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9554 | continue; |
| 9555 | } |
| 9556 | |
| 9557 | /* |
| 9558 | * Add the pair to atts |
| 9559 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9560 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9561 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9562 | if (attvalue[len] == 0) |
| 9563 | xmlFree(attvalue); |
| 9564 | goto failed; |
| 9565 | } |
| 9566 | maxatts = ctxt->maxatts; |
| 9567 | atts = ctxt->atts; |
| 9568 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9569 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9570 | atts[nbatts++] = attname; |
| 9571 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9572 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9573 | atts[nbatts++] = attvalue; |
| 9574 | attvalue += len; |
| 9575 | atts[nbatts++] = attvalue; |
| 9576 | /* |
| 9577 | * tag if some deallocation is needed |
| 9578 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9579 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9580 | } else { |
| 9581 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 9582 | xmlFree(attvalue); |
| 9583 | } |
| 9584 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9585 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9586 | |
| 9587 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9588 | if (ctxt->instate == XML_PARSER_EOF) |
| 9589 | break; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9590 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9591 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9592 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9593 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9594 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9595 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9596 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9597 | } |
| 9598 | SKIP_BLANKS; |
| 9599 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 9600 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9601 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9602 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9603 | break; |
| 9604 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9605 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9606 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9609 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9610 | * The attributes defaulting |
| 9611 | */ |
| 9612 | if (ctxt->attsDefault != NULL) { |
| 9613 | xmlDefAttrsPtr defaults; |
| 9614 | |
| 9615 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 9616 | if (defaults != NULL) { |
| 9617 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9618 | attname = defaults->values[5 * i]; |
| 9619 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9620 | |
| 9621 | /* |
| 9622 | * special work for namespaces defaulted defs |
| 9623 | */ |
| 9624 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 9625 | /* |
| 9626 | * check that it's not a defined namespace |
| 9627 | */ |
| 9628 | for (j = 1;j <= nbNs;j++) |
| 9629 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9630 | break; |
| 9631 | if (j <= nbNs) continue; |
| 9632 | |
| 9633 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9634 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9635 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9636 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9637 | nbNs++; |
| 9638 | } |
| 9639 | } else if (aprefix == ctxt->str_xmlns) { |
| 9640 | /* |
| 9641 | * check that it's not a defined namespace |
| 9642 | */ |
| 9643 | for (j = 1;j <= nbNs;j++) |
| 9644 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9645 | break; |
| 9646 | if (j <= nbNs) continue; |
| 9647 | |
| 9648 | nsname = xmlGetNamespace(ctxt, attname); |
| 9649 | if (nsname != defaults->values[2]) { |
| 9650 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9651 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9652 | nbNs++; |
| 9653 | } |
| 9654 | } else { |
| 9655 | /* |
| 9656 | * check that it's not a defined attribute |
| 9657 | */ |
| 9658 | for (j = 0;j < nbatts;j+=5) { |
| 9659 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 9660 | break; |
| 9661 | } |
| 9662 | if (j < nbatts) continue; |
| 9663 | |
| 9664 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9665 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 9666 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9667 | } |
| 9668 | maxatts = ctxt->maxatts; |
| 9669 | atts = ctxt->atts; |
| 9670 | } |
| 9671 | atts[nbatts++] = attname; |
| 9672 | atts[nbatts++] = aprefix; |
| 9673 | if (aprefix == NULL) |
| 9674 | atts[nbatts++] = NULL; |
| 9675 | else |
| 9676 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9677 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 9678 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 9679 | if ((ctxt->standalone == 1) && |
| 9680 | (defaults->values[5 * i + 4] != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9681 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9682 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 9683 | attname, localname); |
| 9684 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9685 | nbdef++; |
| 9686 | } |
| 9687 | } |
| 9688 | } |
| 9689 | } |
| 9690 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9691 | /* |
| 9692 | * The attributes checkings |
| 9693 | */ |
| 9694 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 9695 | /* |
| 9696 | * The default namespace does not apply to attribute names. |
| 9697 | */ |
| 9698 | if (atts[i + 1] != NULL) { |
| 9699 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 9700 | if (nsname == NULL) { |
| 9701 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9702 | "Namespace prefix %s for %s on %s is not defined\n", |
| 9703 | atts[i + 1], atts[i], localname); |
| 9704 | } |
| 9705 | atts[i + 2] = nsname; |
| 9706 | } else |
| 9707 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9708 | /* |
| 9709 | * [ WFC: Unique Att Spec ] |
| 9710 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9711 | * start-tag or empty-element tag. |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9712 | * As extended by the Namespace in XML REC. |
| 9713 | */ |
| 9714 | for (j = 0; j < i;j += 5) { |
| 9715 | if (atts[i] == atts[j]) { |
| 9716 | if (atts[i+1] == atts[j+1]) { |
| 9717 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 9718 | break; |
| 9719 | } |
| 9720 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 9721 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 9722 | "Namespaced Attribute %s in '%s' redefined\n", |
| 9723 | atts[i], nsname, NULL); |
| 9724 | break; |
| 9725 | } |
| 9726 | } |
| 9727 | } |
| 9728 | } |
| 9729 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9730 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9731 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9732 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9733 | "Namespace prefix %s on %s is not defined\n", |
| 9734 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9735 | } |
| 9736 | *pref = prefix; |
| 9737 | *URI = nsname; |
| 9738 | |
| 9739 | /* |
| 9740 | * SAX: Start of Element ! |
| 9741 | */ |
| 9742 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9743 | (!ctxt->disableSAX)) { |
| 9744 | if (nbNs > 0) |
| 9745 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9746 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9747 | nbatts / 5, nbdef, atts); |
| 9748 | else |
| 9749 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9750 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9751 | } |
| 9752 | |
| 9753 | /* |
| 9754 | * Free up attribute allocated strings if needed |
| 9755 | */ |
| 9756 | if (attval != 0) { |
| 9757 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9758 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9759 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9760 | } |
| 9761 | |
| 9762 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9763 | |
| 9764 | base_changed: |
| 9765 | /* |
| 9766 | * the attribute strings are valid iif the base didn't changed |
| 9767 | */ |
| 9768 | if (attval != 0) { |
| 9769 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9770 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9771 | xmlFree((xmlChar *) atts[i]); |
| 9772 | } |
| 9773 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9774 | ctxt->input->line = oldline; |
| 9775 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9776 | if (ctxt->wellFormed == 1) { |
| 9777 | goto reparse; |
| 9778 | } |
| 9779 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9780 | } |
| 9781 | |
| 9782 | /** |
| 9783 | * xmlParseEndTag2: |
| 9784 | * @ctxt: an XML parser context |
| 9785 | * @line: line of the start tag |
| 9786 | * @nsNr: number of namespaces on the start tag |
| 9787 | * |
| 9788 | * parse an end of tag |
| 9789 | * |
| 9790 | * [42] ETag ::= '</' Name S? '>' |
| 9791 | * |
| 9792 | * With namespace |
| 9793 | * |
| 9794 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9795 | */ |
| 9796 | |
| 9797 | static void |
| 9798 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9799 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9800 | const xmlChar *name; |
| 9801 | |
| 9802 | GROW; |
| 9803 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9804 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9805 | return; |
| 9806 | } |
| 9807 | SKIP(2); |
| 9808 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9809 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9810 | if (ctxt->input->cur[tlen] == '>') { |
| 9811 | ctxt->input->cur += tlen + 1; |
Juergen Keil | 24fb4c3 | 2014-10-06 18:19:12 +0800 | [diff] [blame] | 9812 | ctxt->input->col += tlen + 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9813 | goto done; |
| 9814 | } |
| 9815 | ctxt->input->cur += tlen; |
Juergen Keil | 24fb4c3 | 2014-10-06 18:19:12 +0800 | [diff] [blame] | 9816 | ctxt->input->col += tlen; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9817 | name = (xmlChar*)1; |
| 9818 | } else { |
| 9819 | if (prefix == NULL) |
| 9820 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9821 | else |
| 9822 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9823 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9824 | |
| 9825 | /* |
| 9826 | * We should definitely be at the ending "S? '>'" part |
| 9827 | */ |
| 9828 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9829 | if (ctxt->instate == XML_PARSER_EOF) |
| 9830 | return; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9831 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9832 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9833 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9834 | } else |
| 9835 | NEXT1; |
| 9836 | |
| 9837 | /* |
| 9838 | * [ WFC: Element Type Match ] |
| 9839 | * The Name in an element's end-tag must match the element type in the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9840 | * start-tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9841 | * |
| 9842 | */ |
| 9843 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9844 | if (name == NULL) name = BAD_CAST "unparseable"; |
Daniel Veillard | 852505b | 2009-08-23 15:44:48 +0200 | [diff] [blame] | 9845 | if ((line == 0) && (ctxt->node != NULL)) |
| 9846 | line = ctxt->node->line; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9847 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9848 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9849 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9850 | } |
| 9851 | |
| 9852 | /* |
| 9853 | * SAX: End of Tag |
| 9854 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9855 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9856 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9857 | (!ctxt->disableSAX)) |
| 9858 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9859 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9860 | spacePop(ctxt); |
| 9861 | if (nsNr != 0) |
| 9862 | nsPop(ctxt, nsNr); |
| 9863 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9864 | } |
| 9865 | |
| 9866 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9867 | * xmlParseCDSect: |
| 9868 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9869 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9870 | * Parse escaped pure raw content. |
| 9871 | * |
| 9872 | * [18] CDSect ::= CDStart CData CDEnd |
| 9873 | * |
| 9874 | * [19] CDStart ::= '<![CDATA[' |
| 9875 | * |
| 9876 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9877 | * |
| 9878 | * [21] CDEnd ::= ']]>' |
| 9879 | */ |
| 9880 | void |
| 9881 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9882 | xmlChar *buf = NULL; |
| 9883 | int len = 0; |
| 9884 | int size = XML_PARSER_BUFFER_SIZE; |
| 9885 | int r, rl; |
| 9886 | int s, sl; |
| 9887 | int cur, l; |
| 9888 | int count = 0; |
| 9889 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9890 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9891 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9892 | SKIP(9); |
| 9893 | } else |
| 9894 | return; |
| 9895 | |
| 9896 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9897 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9898 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9899 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9900 | ctxt->instate = XML_PARSER_CONTENT; |
| 9901 | return; |
| 9902 | } |
| 9903 | NEXTL(rl); |
| 9904 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9905 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9906 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9907 | ctxt->instate = XML_PARSER_CONTENT; |
| 9908 | return; |
| 9909 | } |
| 9910 | NEXTL(sl); |
| 9911 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9912 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9913 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9914 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9915 | return; |
| 9916 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9917 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9918 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9919 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9920 | xmlChar *tmp; |
| 9921 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9922 | if ((size > XML_MAX_TEXT_LENGTH) && |
| 9923 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9924 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
| 9925 | "CData section too big found", NULL); |
| 9926 | xmlFree (buf); |
| 9927 | return; |
| 9928 | } |
| 9929 | tmp = (xmlChar *) xmlRealloc(buf, size * 2 * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9930 | if (tmp == NULL) { |
| 9931 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9932 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9933 | return; |
| 9934 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9935 | buf = tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9936 | size *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9937 | } |
| 9938 | COPY_BUF(rl,buf,len,r); |
| 9939 | r = s; |
| 9940 | rl = sl; |
| 9941 | s = cur; |
| 9942 | sl = l; |
| 9943 | count++; |
| 9944 | if (count > 50) { |
| 9945 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9946 | if (ctxt->instate == XML_PARSER_EOF) { |
| 9947 | xmlFree(buf); |
| 9948 | return; |
| 9949 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9950 | count = 0; |
| 9951 | } |
| 9952 | NEXTL(l); |
| 9953 | cur = CUR_CHAR(l); |
| 9954 | } |
| 9955 | buf[len] = 0; |
| 9956 | ctxt->instate = XML_PARSER_CONTENT; |
| 9957 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9958 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9959 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9960 | xmlFree(buf); |
| 9961 | return; |
| 9962 | } |
| 9963 | NEXTL(l); |
| 9964 | |
| 9965 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9966 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9967 | */ |
| 9968 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9969 | if (ctxt->sax->cdataBlock != NULL) |
| 9970 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9971 | else if (ctxt->sax->characters != NULL) |
| 9972 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9973 | } |
| 9974 | xmlFree(buf); |
| 9975 | } |
| 9976 | |
| 9977 | /** |
| 9978 | * xmlParseContent: |
| 9979 | * @ctxt: an XML parser context |
| 9980 | * |
| 9981 | * Parse a content: |
| 9982 | * |
| 9983 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9984 | */ |
| 9985 | |
| 9986 | void |
| 9987 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9988 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9989 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9990 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9991 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9992 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9993 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9994 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9995 | |
| 9996 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9997 | * First case : a Processing Instruction. |
| 9998 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9999 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10000 | xmlParsePI(ctxt); |
| 10001 | } |
| 10002 | |
| 10003 | /* |
| 10004 | * Second case : a CDSection |
| 10005 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 10006 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10007 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10008 | xmlParseCDSect(ctxt); |
| 10009 | } |
| 10010 | |
| 10011 | /* |
| 10012 | * Third case : a comment |
| 10013 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10014 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10015 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 10016 | xmlParseComment(ctxt); |
| 10017 | ctxt->instate = XML_PARSER_CONTENT; |
| 10018 | } |
| 10019 | |
| 10020 | /* |
| 10021 | * Fourth case : a sub-element. |
| 10022 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10023 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10024 | xmlParseElement(ctxt); |
| 10025 | } |
| 10026 | |
| 10027 | /* |
| 10028 | * Fifth case : a reference. If if has not been resolved, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10029 | * parsing returns it's Name, create the node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10030 | */ |
| 10031 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10032 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10033 | xmlParseReference(ctxt); |
| 10034 | } |
| 10035 | |
| 10036 | /* |
| 10037 | * Last case, text. Note that References are handled directly. |
| 10038 | */ |
| 10039 | else { |
| 10040 | xmlParseCharData(ctxt, 0); |
| 10041 | } |
| 10042 | |
| 10043 | GROW; |
| 10044 | /* |
| 10045 | * Pop-up of finished entities. |
| 10046 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10047 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10048 | xmlPopInput(ctxt); |
| 10049 | SHRINK; |
| 10050 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 10051 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10052 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 10053 | "detected an error in element content\n"); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 10054 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10055 | break; |
| 10056 | } |
| 10057 | } |
| 10058 | } |
| 10059 | |
| 10060 | /** |
| 10061 | * xmlParseElement: |
| 10062 | * @ctxt: an XML parser context |
| 10063 | * |
| 10064 | * parse an XML element, this is highly recursive |
| 10065 | * |
| 10066 | * [39] element ::= EmptyElemTag | STag content ETag |
| 10067 | * |
| 10068 | * [ WFC: Element Type Match ] |
| 10069 | * The Name in an element's end-tag must match the element type in the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10070 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10071 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10072 | */ |
| 10073 | |
| 10074 | void |
| 10075 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 10076 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 10077 | const xmlChar *prefix = NULL; |
| 10078 | const xmlChar *URI = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10079 | xmlParserNodeInfo node_info; |
Daniel Veillard | ed35d3d | 2012-05-11 10:52:27 +0800 | [diff] [blame] | 10080 | int line, tlen = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10081 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10082 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10083 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 10084 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 10085 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 10086 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 10087 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 10088 | xmlParserMaxDepth); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 10089 | xmlHaltParser(ctxt); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 10090 | return; |
| 10091 | } |
| 10092 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10093 | /* Capture start position */ |
| 10094 | if (ctxt->record_info) { |
| 10095 | node_info.begin_pos = ctxt->input->consumed + |
| 10096 | (CUR_PTR - ctxt->input->base); |
| 10097 | node_info.begin_line = ctxt->input->line; |
| 10098 | } |
| 10099 | |
| 10100 | if (ctxt->spaceNr == 0) |
| 10101 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 10102 | else if (*ctxt->space == -2) |
| 10103 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10104 | else |
| 10105 | spacePush(ctxt, *ctxt->space); |
| 10106 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 10107 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10108 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10109 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10110 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10111 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10112 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10113 | else |
| 10114 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10115 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 10116 | if (ctxt->instate == XML_PARSER_EOF) |
| 10117 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10118 | if (name == NULL) { |
| 10119 | spacePop(ctxt); |
| 10120 | return; |
| 10121 | } |
| 10122 | namePush(ctxt, name); |
| 10123 | ret = ctxt->node; |
| 10124 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10125 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10126 | /* |
| 10127 | * [ VC: Root Element Type ] |
| 10128 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10129 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10130 | */ |
| 10131 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 10132 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 10133 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10134 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10135 | |
| 10136 | /* |
| 10137 | * Check for an Empty Element. |
| 10138 | */ |
| 10139 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 10140 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10141 | if (ctxt->sax2) { |
| 10142 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 10143 | (!ctxt->disableSAX)) |
| 10144 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10145 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10146 | } else { |
| 10147 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 10148 | (!ctxt->disableSAX)) |
| 10149 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10150 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10151 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10152 | namePop(ctxt); |
| 10153 | spacePop(ctxt); |
| 10154 | if (nsNr != ctxt->nsNr) |
| 10155 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10156 | if ( ret != NULL && ctxt->record_info ) { |
| 10157 | node_info.end_pos = ctxt->input->consumed + |
| 10158 | (CUR_PTR - ctxt->input->base); |
| 10159 | node_info.end_line = ctxt->input->line; |
| 10160 | node_info.node = ret; |
| 10161 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10162 | } |
| 10163 | return; |
| 10164 | } |
| 10165 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10166 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10167 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10168 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 10169 | "Couldn't find end of Start Tag %s line %d\n", |
| 10170 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10171 | |
| 10172 | /* |
| 10173 | * end of parsing of this node. |
| 10174 | */ |
| 10175 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10176 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10177 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10178 | if (nsNr != ctxt->nsNr) |
| 10179 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10180 | |
| 10181 | /* |
| 10182 | * Capture end position and add node |
| 10183 | */ |
| 10184 | if ( ret != NULL && ctxt->record_info ) { |
| 10185 | node_info.end_pos = ctxt->input->consumed + |
| 10186 | (CUR_PTR - ctxt->input->base); |
| 10187 | node_info.end_line = ctxt->input->line; |
| 10188 | node_info.node = ret; |
| 10189 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10190 | } |
| 10191 | return; |
| 10192 | } |
| 10193 | |
| 10194 | /* |
| 10195 | * Parse the content of the element: |
| 10196 | */ |
| 10197 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10198 | if (ctxt->instate == XML_PARSER_EOF) |
| 10199 | return; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10200 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10201 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 10202 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10203 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10204 | |
| 10205 | /* |
| 10206 | * end of parsing of this node. |
| 10207 | */ |
| 10208 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10209 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10210 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10211 | if (nsNr != ctxt->nsNr) |
| 10212 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10213 | return; |
| 10214 | } |
| 10215 | |
| 10216 | /* |
| 10217 | * parse the end of tag: '</' should be here. |
| 10218 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10219 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10220 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10221 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10222 | } |
| 10223 | #ifdef LIBXML_SAX1_ENABLED |
| 10224 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10225 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10226 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10227 | |
| 10228 | /* |
| 10229 | * Capture end position and add node |
| 10230 | */ |
| 10231 | if ( ret != NULL && ctxt->record_info ) { |
| 10232 | node_info.end_pos = ctxt->input->consumed + |
| 10233 | (CUR_PTR - ctxt->input->base); |
| 10234 | node_info.end_line = ctxt->input->line; |
| 10235 | node_info.node = ret; |
| 10236 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10237 | } |
| 10238 | } |
| 10239 | |
| 10240 | /** |
| 10241 | * xmlParseVersionNum: |
| 10242 | * @ctxt: an XML parser context |
| 10243 | * |
| 10244 | * parse the XML version value. |
| 10245 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10246 | * [26] VersionNum ::= '1.' [0-9]+ |
| 10247 | * |
| 10248 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10249 | * |
| 10250 | * Returns the string giving the XML version number, or NULL |
| 10251 | */ |
| 10252 | xmlChar * |
| 10253 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 10254 | xmlChar *buf = NULL; |
| 10255 | int len = 0; |
| 10256 | int size = 10; |
| 10257 | xmlChar cur; |
| 10258 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10259 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10260 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10261 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10262 | return(NULL); |
| 10263 | } |
| 10264 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10265 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10266 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10267 | return(NULL); |
| 10268 | } |
| 10269 | buf[len++] = cur; |
| 10270 | NEXT; |
| 10271 | cur=CUR; |
| 10272 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10273 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10274 | return(NULL); |
| 10275 | } |
| 10276 | buf[len++] = cur; |
| 10277 | NEXT; |
| 10278 | cur=CUR; |
| 10279 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10280 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10281 | xmlChar *tmp; |
| 10282 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10283 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10284 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10285 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10286 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10287 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10288 | return(NULL); |
| 10289 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10290 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10291 | } |
| 10292 | buf[len++] = cur; |
| 10293 | NEXT; |
| 10294 | cur=CUR; |
| 10295 | } |
| 10296 | buf[len] = 0; |
| 10297 | return(buf); |
| 10298 | } |
| 10299 | |
| 10300 | /** |
| 10301 | * xmlParseVersionInfo: |
| 10302 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10303 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10304 | * parse the XML version. |
| 10305 | * |
| 10306 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10307 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10308 | * [25] Eq ::= S? '=' S? |
| 10309 | * |
| 10310 | * Returns the version string, e.g. "1.0" |
| 10311 | */ |
| 10312 | |
| 10313 | xmlChar * |
| 10314 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 10315 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10316 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10317 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10318 | SKIP(7); |
| 10319 | SKIP_BLANKS; |
| 10320 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10321 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10322 | return(NULL); |
| 10323 | } |
| 10324 | NEXT; |
| 10325 | SKIP_BLANKS; |
| 10326 | if (RAW == '"') { |
| 10327 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10328 | version = xmlParseVersionNum(ctxt); |
| 10329 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10330 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10331 | } else |
| 10332 | NEXT; |
| 10333 | } else if (RAW == '\''){ |
| 10334 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10335 | version = xmlParseVersionNum(ctxt); |
| 10336 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10337 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10338 | } else |
| 10339 | NEXT; |
| 10340 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10341 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10342 | } |
| 10343 | } |
| 10344 | return(version); |
| 10345 | } |
| 10346 | |
| 10347 | /** |
| 10348 | * xmlParseEncName: |
| 10349 | * @ctxt: an XML parser context |
| 10350 | * |
| 10351 | * parse the XML encoding name |
| 10352 | * |
| 10353 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 10354 | * |
| 10355 | * Returns the encoding name value or NULL |
| 10356 | */ |
| 10357 | xmlChar * |
| 10358 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 10359 | xmlChar *buf = NULL; |
| 10360 | int len = 0; |
| 10361 | int size = 10; |
| 10362 | xmlChar cur; |
| 10363 | |
| 10364 | cur = CUR; |
| 10365 | if (((cur >= 'a') && (cur <= 'z')) || |
| 10366 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10367 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10368 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10369 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10370 | return(NULL); |
| 10371 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10372 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10373 | buf[len++] = cur; |
| 10374 | NEXT; |
| 10375 | cur = CUR; |
| 10376 | while (((cur >= 'a') && (cur <= 'z')) || |
| 10377 | ((cur >= 'A') && (cur <= 'Z')) || |
| 10378 | ((cur >= '0') && (cur <= '9')) || |
| 10379 | (cur == '.') || (cur == '_') || |
| 10380 | (cur == '-')) { |
| 10381 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10382 | xmlChar *tmp; |
| 10383 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10384 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10385 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10386 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10387 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10388 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10389 | return(NULL); |
| 10390 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10391 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10392 | } |
| 10393 | buf[len++] = cur; |
| 10394 | NEXT; |
| 10395 | cur = CUR; |
| 10396 | if (cur == 0) { |
| 10397 | SHRINK; |
| 10398 | GROW; |
| 10399 | cur = CUR; |
| 10400 | } |
| 10401 | } |
| 10402 | buf[len] = 0; |
| 10403 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10404 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10405 | } |
| 10406 | return(buf); |
| 10407 | } |
| 10408 | |
| 10409 | /** |
| 10410 | * xmlParseEncodingDecl: |
| 10411 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10412 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10413 | * parse the XML encoding declaration |
| 10414 | * |
| 10415 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 10416 | * |
| 10417 | * this setups the conversion filters. |
| 10418 | * |
| 10419 | * Returns the encoding value or NULL |
| 10420 | */ |
| 10421 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10422 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10423 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 10424 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10425 | |
| 10426 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10427 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10428 | SKIP(8); |
| 10429 | SKIP_BLANKS; |
| 10430 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10431 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10432 | return(NULL); |
| 10433 | } |
| 10434 | NEXT; |
| 10435 | SKIP_BLANKS; |
| 10436 | if (RAW == '"') { |
| 10437 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10438 | encoding = xmlParseEncName(ctxt); |
| 10439 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10440 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Daniel Veillard | 9aa3758 | 2015-06-29 09:08:25 +0800 | [diff] [blame] | 10441 | xmlFree((xmlChar *) encoding); |
| 10442 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10443 | } else |
| 10444 | NEXT; |
| 10445 | } else if (RAW == '\''){ |
| 10446 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10447 | encoding = xmlParseEncName(ctxt); |
| 10448 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10449 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Daniel Veillard | 9aa3758 | 2015-06-29 09:08:25 +0800 | [diff] [blame] | 10450 | xmlFree((xmlChar *) encoding); |
| 10451 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10452 | } else |
| 10453 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 10454 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10455 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 10457 | |
| 10458 | /* |
| 10459 | * Non standard parsing, allowing the user to ignore encoding |
| 10460 | */ |
Bart De Schuymer | 500c54e | 2014-10-16 12:17:20 +0800 | [diff] [blame] | 10461 | if (ctxt->options & XML_PARSE_IGNORE_ENC) { |
| 10462 | xmlFree((xmlChar *) encoding); |
| 10463 | return(NULL); |
| 10464 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 10465 | |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10466 | /* |
| 10467 | * UTF-16 encoding stwich has already taken place at this stage, |
| 10468 | * more over the little-endian/big-endian selection is already done |
| 10469 | */ |
| 10470 | if ((encoding != NULL) && |
| 10471 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 10472 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10473 | /* |
| 10474 | * If no encoding was passed to the parser, that we are |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10475 | * using UTF-16 and no decoder is present i.e. the |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10476 | * document is apparently UTF-8 compatible, then raise an |
| 10477 | * encoding mismatch fatal error |
| 10478 | */ |
| 10479 | if ((ctxt->encoding == NULL) && |
| 10480 | (ctxt->input->buf != NULL) && |
| 10481 | (ctxt->input->buf->encoder == NULL)) { |
| 10482 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 10483 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 10484 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10485 | if (ctxt->encoding != NULL) |
| 10486 | xmlFree((xmlChar *) ctxt->encoding); |
| 10487 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10488 | } |
| 10489 | /* |
| 10490 | * UTF-8 encoding is handled natively |
| 10491 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10492 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10493 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 10494 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10495 | if (ctxt->encoding != NULL) |
| 10496 | xmlFree((xmlChar *) ctxt->encoding); |
| 10497 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10498 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10499 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10500 | xmlCharEncodingHandlerPtr handler; |
| 10501 | |
| 10502 | if (ctxt->input->encoding != NULL) |
| 10503 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 10504 | ctxt->input->encoding = encoding; |
| 10505 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10506 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 10507 | if (handler != NULL) { |
Daniel Veillard | 709a952 | 2015-06-29 16:10:26 +0800 | [diff] [blame] | 10508 | if (xmlSwitchToEncoding(ctxt, handler) < 0) { |
| 10509 | /* failed to convert */ |
| 10510 | ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING; |
| 10511 | return(NULL); |
| 10512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10513 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10514 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10515 | "Unsupported encoding %s\n", encoding); |
| 10516 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10517 | } |
| 10518 | } |
| 10519 | } |
| 10520 | return(encoding); |
| 10521 | } |
| 10522 | |
| 10523 | /** |
| 10524 | * xmlParseSDDecl: |
| 10525 | * @ctxt: an XML parser context |
| 10526 | * |
| 10527 | * parse the XML standalone declaration |
| 10528 | * |
| 10529 | * [32] SDDecl ::= S 'standalone' Eq |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10530 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10531 | * |
| 10532 | * [ VC: Standalone Document Declaration ] |
| 10533 | * TODO The standalone document declaration must have the value "no" |
| 10534 | * if any external markup declarations contain declarations of: |
| 10535 | * - attributes with default values, if elements to which these |
| 10536 | * attributes apply appear in the document without specifications |
| 10537 | * of values for these attributes, or |
| 10538 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 10539 | * to those entities appear in the document, or |
| 10540 | * - attributes with values subject to normalization, where the |
| 10541 | * attribute appears in the document with a value which will change |
| 10542 | * as a result of normalization, or |
| 10543 | * - element types with element content, if white space occurs directly |
| 10544 | * within any instance of those types. |
| 10545 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10546 | * Returns: |
| 10547 | * 1 if standalone="yes" |
| 10548 | * 0 if standalone="no" |
| 10549 | * -2 if standalone attribute is missing or invalid |
| 10550 | * (A standalone value of -2 means that the XML declaration was found, |
| 10551 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10552 | */ |
| 10553 | |
| 10554 | int |
| 10555 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10556 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10557 | |
| 10558 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10559 | if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10560 | SKIP(10); |
| 10561 | SKIP_BLANKS; |
| 10562 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10563 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10564 | return(standalone); |
| 10565 | } |
| 10566 | NEXT; |
| 10567 | SKIP_BLANKS; |
| 10568 | if (RAW == '\''){ |
| 10569 | NEXT; |
| 10570 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10571 | standalone = 0; |
| 10572 | SKIP(2); |
| 10573 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10574 | (NXT(2) == 's')) { |
| 10575 | standalone = 1; |
| 10576 | SKIP(3); |
| 10577 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10578 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10579 | } |
| 10580 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10581 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10582 | } else |
| 10583 | NEXT; |
| 10584 | } else if (RAW == '"'){ |
| 10585 | NEXT; |
| 10586 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10587 | standalone = 0; |
| 10588 | SKIP(2); |
| 10589 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10590 | (NXT(2) == 's')) { |
| 10591 | standalone = 1; |
| 10592 | SKIP(3); |
| 10593 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10594 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10595 | } |
| 10596 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10597 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10598 | } else |
| 10599 | NEXT; |
| 10600 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10601 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10602 | } |
| 10603 | } |
| 10604 | return(standalone); |
| 10605 | } |
| 10606 | |
| 10607 | /** |
| 10608 | * xmlParseXMLDecl: |
| 10609 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10610 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10611 | * parse an XML declaration header |
| 10612 | * |
| 10613 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 10614 | */ |
| 10615 | |
| 10616 | void |
| 10617 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 10618 | xmlChar *version; |
| 10619 | |
| 10620 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 10621 | * This value for standalone indicates that the document has an |
| 10622 | * XML declaration but it does not have a standalone attribute. |
| 10623 | * It will be overwritten later if a standalone attribute is found. |
| 10624 | */ |
| 10625 | ctxt->input->standalone = -2; |
| 10626 | |
| 10627 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10628 | * We know that '<?xml' is here. |
| 10629 | */ |
| 10630 | SKIP(5); |
| 10631 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10632 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10633 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 10634 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10635 | } |
| 10636 | SKIP_BLANKS; |
| 10637 | |
| 10638 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10639 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10640 | */ |
| 10641 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10642 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10643 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10644 | } else { |
| 10645 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 10646 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10647 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10648 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10649 | if (ctxt->options & XML_PARSE_OLD10) { |
| 10650 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10651 | "Unsupported version '%s'\n", |
| 10652 | version); |
| 10653 | } else { |
| 10654 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 10655 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 10656 | "Unsupported version '%s'\n", |
| 10657 | version, NULL); |
| 10658 | } else { |
| 10659 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10660 | "Unsupported version '%s'\n", |
| 10661 | version); |
| 10662 | } |
| 10663 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10664 | } |
| 10665 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 10666 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10667 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 10668 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10669 | |
| 10670 | /* |
| 10671 | * We may have the encoding declaration |
| 10672 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10673 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10674 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10675 | SKIP(2); |
| 10676 | return; |
| 10677 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10678 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10679 | } |
| 10680 | xmlParseEncodingDecl(ctxt); |
Daniel Veillard | afd27c2 | 2015-11-09 18:07:18 +0800 | [diff] [blame] | 10681 | if ((ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) || |
| 10682 | (ctxt->instate == XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10683 | /* |
| 10684 | * The XML REC instructs us to stop parsing right here |
| 10685 | */ |
| 10686 | return; |
| 10687 | } |
| 10688 | |
| 10689 | /* |
| 10690 | * We may have the standalone status. |
| 10691 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10692 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10693 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10694 | SKIP(2); |
| 10695 | return; |
| 10696 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10697 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10698 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 10699 | |
| 10700 | /* |
| 10701 | * We can grow the input buffer freely at that point |
| 10702 | */ |
| 10703 | GROW; |
| 10704 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10705 | SKIP_BLANKS; |
| 10706 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 10707 | |
| 10708 | SKIP_BLANKS; |
| 10709 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10710 | SKIP(2); |
| 10711 | } else if (RAW == '>') { |
| 10712 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10713 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10714 | NEXT; |
| 10715 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10716 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10717 | MOVETO_ENDTAG(CUR_PTR); |
| 10718 | NEXT; |
| 10719 | } |
| 10720 | } |
| 10721 | |
| 10722 | /** |
| 10723 | * xmlParseMisc: |
| 10724 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10725 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10726 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10727 | * |
| 10728 | * [27] Misc ::= Comment | PI | S |
| 10729 | */ |
| 10730 | |
| 10731 | void |
| 10732 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 10733 | while ((ctxt->instate != XML_PARSER_EOF) && |
| 10734 | (((RAW == '<') && (NXT(1) == '?')) || |
| 10735 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
| 10736 | IS_BLANK_CH(CUR))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10737 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10738 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10739 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10740 | NEXT; |
| 10741 | } else |
| 10742 | xmlParseComment(ctxt); |
| 10743 | } |
| 10744 | } |
| 10745 | |
| 10746 | /** |
| 10747 | * xmlParseDocument: |
| 10748 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10749 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10750 | * parse an XML document (and build a tree if using the standard SAX |
| 10751 | * interface). |
| 10752 | * |
| 10753 | * [1] document ::= prolog element Misc* |
| 10754 | * |
| 10755 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 10756 | * |
| 10757 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10758 | * as a result of the parsing. |
| 10759 | */ |
| 10760 | |
| 10761 | int |
| 10762 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 10763 | xmlChar start[4]; |
| 10764 | xmlCharEncoding enc; |
| 10765 | |
| 10766 | xmlInitParser(); |
| 10767 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10768 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10769 | return(-1); |
| 10770 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10771 | GROW; |
| 10772 | |
| 10773 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10774 | * SAX: detecting the level. |
| 10775 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10776 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10777 | |
| 10778 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10779 | * SAX: beginning of the document processing. |
| 10780 | */ |
| 10781 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10782 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10783 | if (ctxt->instate == XML_PARSER_EOF) |
| 10784 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10785 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 10786 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10787 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10788 | /* |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10789 | * Get the 4 first bytes and decode the charset |
| 10790 | * if enc != XML_CHAR_ENCODING_NONE |
| 10791 | * plug some encoding conversion routines. |
| 10792 | */ |
| 10793 | start[0] = RAW; |
| 10794 | start[1] = NXT(1); |
| 10795 | start[2] = NXT(2); |
| 10796 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10797 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10798 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10799 | xmlSwitchEncoding(ctxt, enc); |
| 10800 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10801 | } |
| 10802 | |
| 10803 | |
| 10804 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10805 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | afd27c2 | 2015-11-09 18:07:18 +0800 | [diff] [blame] | 10806 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10807 | } |
| 10808 | |
| 10809 | /* |
| 10810 | * Check for the XMLDecl in the Prolog. |
Daniel Veillard | 7e385bd | 2009-08-26 11:38:49 +0200 | [diff] [blame] | 10811 | * do not GROW here to avoid the detected encoder to decode more |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10812 | * than just the first line, unless the amount of data is really |
| 10813 | * too small to hold "<?xml version="1.0" encoding="foo" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10814 | */ |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10815 | if ((ctxt->input->end - ctxt->input->cur) < 35) { |
| 10816 | GROW; |
| 10817 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10818 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10819 | |
| 10820 | /* |
| 10821 | * Note that we will switch encoding on the fly. |
| 10822 | */ |
| 10823 | xmlParseXMLDecl(ctxt); |
Daniel Veillard | afd27c2 | 2015-11-09 18:07:18 +0800 | [diff] [blame] | 10824 | if ((ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) || |
| 10825 | (ctxt->instate == XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10826 | /* |
| 10827 | * The XML REC instructs us to stop parsing right here |
| 10828 | */ |
| 10829 | return(-1); |
| 10830 | } |
| 10831 | ctxt->standalone = ctxt->input->standalone; |
| 10832 | SKIP_BLANKS; |
| 10833 | } else { |
| 10834 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10835 | } |
| 10836 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10837 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10838 | if (ctxt->instate == XML_PARSER_EOF) |
| 10839 | return(-1); |
Daniel Veillard | 63588f4 | 2013-05-10 14:01:46 +0800 | [diff] [blame] | 10840 | if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) && |
| 10841 | (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) { |
| 10842 | ctxt->myDoc->compression = ctxt->input->buf->compressed; |
| 10843 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10844 | |
| 10845 | /* |
| 10846 | * The Misc part of the Prolog |
| 10847 | */ |
| 10848 | GROW; |
| 10849 | xmlParseMisc(ctxt); |
| 10850 | |
| 10851 | /* |
| 10852 | * Then possibly doc type declaration(s) and more Misc |
| 10853 | * (doctypedecl Misc*)? |
| 10854 | */ |
| 10855 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10856 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10857 | |
| 10858 | ctxt->inSubset = 1; |
| 10859 | xmlParseDocTypeDecl(ctxt); |
| 10860 | if (RAW == '[') { |
| 10861 | ctxt->instate = XML_PARSER_DTD; |
| 10862 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10863 | if (ctxt->instate == XML_PARSER_EOF) |
| 10864 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10865 | } |
| 10866 | |
| 10867 | /* |
| 10868 | * Create and update the external subset. |
| 10869 | */ |
| 10870 | ctxt->inSubset = 2; |
| 10871 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10872 | (!ctxt->disableSAX)) |
| 10873 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10874 | ctxt->extSubSystem, ctxt->extSubURI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10875 | if (ctxt->instate == XML_PARSER_EOF) |
| 10876 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10877 | ctxt->inSubset = 0; |
| 10878 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10879 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10880 | |
| 10881 | ctxt->instate = XML_PARSER_PROLOG; |
| 10882 | xmlParseMisc(ctxt); |
| 10883 | } |
| 10884 | |
| 10885 | /* |
| 10886 | * Time to start parsing the tree itself |
| 10887 | */ |
| 10888 | GROW; |
| 10889 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10890 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10891 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10892 | } else { |
| 10893 | ctxt->instate = XML_PARSER_CONTENT; |
| 10894 | xmlParseElement(ctxt); |
| 10895 | ctxt->instate = XML_PARSER_EPILOG; |
| 10896 | |
| 10897 | |
| 10898 | /* |
| 10899 | * The Misc part at the end |
| 10900 | */ |
| 10901 | xmlParseMisc(ctxt); |
| 10902 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10903 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10904 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10905 | } |
| 10906 | ctxt->instate = XML_PARSER_EOF; |
| 10907 | } |
| 10908 | |
| 10909 | /* |
| 10910 | * SAX: end of the document processing. |
| 10911 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10912 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10913 | ctxt->sax->endDocument(ctxt->userData); |
| 10914 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10915 | /* |
| 10916 | * Remove locally kept entity definitions if the tree was not built |
| 10917 | */ |
| 10918 | if ((ctxt->myDoc != NULL) && |
| 10919 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10920 | xmlFreeDoc(ctxt->myDoc); |
| 10921 | ctxt->myDoc = NULL; |
| 10922 | } |
| 10923 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10924 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10925 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10926 | if (ctxt->valid) |
| 10927 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10928 | if (ctxt->nsWellFormed) |
| 10929 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10930 | if (ctxt->options & XML_PARSE_OLD10) |
| 10931 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10932 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10933 | if (! ctxt->wellFormed) { |
| 10934 | ctxt->valid = 0; |
| 10935 | return(-1); |
| 10936 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10937 | return(0); |
| 10938 | } |
| 10939 | |
| 10940 | /** |
| 10941 | * xmlParseExtParsedEnt: |
| 10942 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10943 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10944 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10945 | * An external general parsed entity is well-formed if it matches the |
| 10946 | * production labeled extParsedEnt. |
| 10947 | * |
| 10948 | * [78] extParsedEnt ::= TextDecl? content |
| 10949 | * |
| 10950 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10951 | * as a result of the parsing. |
| 10952 | */ |
| 10953 | |
| 10954 | int |
| 10955 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10956 | xmlChar start[4]; |
| 10957 | xmlCharEncoding enc; |
| 10958 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10959 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10960 | return(-1); |
| 10961 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10962 | xmlDefaultSAXHandlerInit(); |
| 10963 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10964 | xmlDetectSAX2(ctxt); |
| 10965 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10966 | GROW; |
| 10967 | |
| 10968 | /* |
| 10969 | * SAX: beginning of the document processing. |
| 10970 | */ |
| 10971 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10972 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10973 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10974 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10975 | * Get the 4 first bytes and decode the charset |
| 10976 | * if enc != XML_CHAR_ENCODING_NONE |
| 10977 | * plug some encoding conversion routines. |
| 10978 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10979 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10980 | start[0] = RAW; |
| 10981 | start[1] = NXT(1); |
| 10982 | start[2] = NXT(2); |
| 10983 | start[3] = NXT(3); |
| 10984 | enc = xmlDetectCharEncoding(start, 4); |
| 10985 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10986 | xmlSwitchEncoding(ctxt, enc); |
| 10987 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10988 | } |
| 10989 | |
| 10990 | |
| 10991 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10992 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10993 | } |
| 10994 | |
| 10995 | /* |
| 10996 | * Check for the XMLDecl in the Prolog. |
| 10997 | */ |
| 10998 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10999 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11000 | |
| 11001 | /* |
| 11002 | * Note that we will switch encoding on the fly. |
| 11003 | */ |
| 11004 | xmlParseXMLDecl(ctxt); |
| 11005 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 11006 | /* |
| 11007 | * The XML REC instructs us to stop parsing right here |
| 11008 | */ |
| 11009 | return(-1); |
| 11010 | } |
| 11011 | SKIP_BLANKS; |
| 11012 | } else { |
| 11013 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 11014 | } |
| 11015 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 11016 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11017 | if (ctxt->instate == XML_PARSER_EOF) |
| 11018 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11019 | |
| 11020 | /* |
| 11021 | * Doing validity checking on chunk doesn't make sense |
| 11022 | */ |
| 11023 | ctxt->instate = XML_PARSER_CONTENT; |
| 11024 | ctxt->validate = 0; |
| 11025 | ctxt->loadsubset = 0; |
| 11026 | ctxt->depth = 0; |
| 11027 | |
| 11028 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11029 | if (ctxt->instate == XML_PARSER_EOF) |
| 11030 | return(-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11031 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11032 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11033 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11034 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11035 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11036 | } |
| 11037 | |
| 11038 | /* |
| 11039 | * SAX: end of the document processing. |
| 11040 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11041 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11042 | ctxt->sax->endDocument(ctxt->userData); |
| 11043 | |
| 11044 | if (! ctxt->wellFormed) return(-1); |
| 11045 | return(0); |
| 11046 | } |
| 11047 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 11048 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11049 | /************************************************************************ |
| 11050 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11051 | * Progressive parsing interfaces * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11052 | * * |
| 11053 | ************************************************************************/ |
| 11054 | |
| 11055 | /** |
| 11056 | * xmlParseLookupSequence: |
| 11057 | * @ctxt: an XML parser context |
| 11058 | * @first: the first char to lookup |
| 11059 | * @next: the next char to lookup or zero |
| 11060 | * @third: the next char to lookup or zero |
| 11061 | * |
| 11062 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 11063 | * (first) is available in the input stream. |
| 11064 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 11065 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 11066 | * parser, do not use liberally. |
| 11067 | * |
| 11068 | * Returns the index to the current parsing point if the full sequence |
| 11069 | * is available, -1 otherwise. |
| 11070 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11071 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11072 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 11073 | xmlChar next, xmlChar third) { |
| 11074 | int base, len; |
| 11075 | xmlParserInputPtr in; |
| 11076 | const xmlChar *buf; |
| 11077 | |
| 11078 | in = ctxt->input; |
| 11079 | if (in == NULL) return(-1); |
| 11080 | base = in->cur - in->base; |
| 11081 | if (base < 0) return(-1); |
| 11082 | if (ctxt->checkIndex > base) |
| 11083 | base = ctxt->checkIndex; |
| 11084 | if (in->buf == NULL) { |
| 11085 | buf = in->base; |
| 11086 | len = in->length; |
| 11087 | } else { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11088 | buf = xmlBufContent(in->buf->buffer); |
| 11089 | len = xmlBufUse(in->buf->buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11090 | } |
| 11091 | /* take into account the sequence length */ |
| 11092 | if (third) len -= 2; |
| 11093 | else if (next) len --; |
| 11094 | for (;base < len;base++) { |
| 11095 | if (buf[base] == first) { |
| 11096 | if (third != 0) { |
| 11097 | if ((buf[base + 1] != next) || |
| 11098 | (buf[base + 2] != third)) continue; |
| 11099 | } else if (next != 0) { |
| 11100 | if (buf[base + 1] != next) continue; |
| 11101 | } |
| 11102 | ctxt->checkIndex = 0; |
| 11103 | #ifdef DEBUG_PUSH |
| 11104 | if (next == 0) |
| 11105 | xmlGenericError(xmlGenericErrorContext, |
| 11106 | "PP: lookup '%c' found at %d\n", |
| 11107 | first, base); |
| 11108 | else if (third == 0) |
| 11109 | xmlGenericError(xmlGenericErrorContext, |
| 11110 | "PP: lookup '%c%c' found at %d\n", |
| 11111 | first, next, base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11112 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11113 | xmlGenericError(xmlGenericErrorContext, |
| 11114 | "PP: lookup '%c%c%c' found at %d\n", |
| 11115 | first, next, third, base); |
| 11116 | #endif |
| 11117 | return(base - (in->cur - in->base)); |
| 11118 | } |
| 11119 | } |
| 11120 | ctxt->checkIndex = base; |
| 11121 | #ifdef DEBUG_PUSH |
| 11122 | if (next == 0) |
| 11123 | xmlGenericError(xmlGenericErrorContext, |
| 11124 | "PP: lookup '%c' failed\n", first); |
| 11125 | else if (third == 0) |
| 11126 | xmlGenericError(xmlGenericErrorContext, |
| 11127 | "PP: lookup '%c%c' failed\n", first, next); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11128 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11129 | xmlGenericError(xmlGenericErrorContext, |
| 11130 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 11131 | #endif |
| 11132 | return(-1); |
| 11133 | } |
| 11134 | |
| 11135 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11136 | * xmlParseGetLasts: |
| 11137 | * @ctxt: an XML parser context |
| 11138 | * @lastlt: pointer to store the last '<' from the input |
| 11139 | * @lastgt: pointer to store the last '>' from the input |
| 11140 | * |
| 11141 | * Lookup the last < and > in the current chunk |
| 11142 | */ |
| 11143 | static void |
| 11144 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 11145 | const xmlChar **lastgt) { |
| 11146 | const xmlChar *tmp; |
| 11147 | |
| 11148 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 11149 | xmlGenericError(xmlGenericErrorContext, |
| 11150 | "Internal error: xmlParseGetLasts\n"); |
| 11151 | return; |
| 11152 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11153 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11154 | tmp = ctxt->input->end; |
| 11155 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11156 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11157 | if (tmp < ctxt->input->base) { |
| 11158 | *lastlt = NULL; |
| 11159 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11160 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11161 | *lastlt = tmp; |
| 11162 | tmp++; |
| 11163 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 11164 | if (*tmp == '\'') { |
| 11165 | tmp++; |
| 11166 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 11167 | if (tmp < ctxt->input->end) tmp++; |
| 11168 | } else if (*tmp == '"') { |
| 11169 | tmp++; |
| 11170 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 11171 | if (tmp < ctxt->input->end) tmp++; |
| 11172 | } else |
| 11173 | tmp++; |
| 11174 | } |
| 11175 | if (tmp < ctxt->input->end) |
| 11176 | *lastgt = tmp; |
| 11177 | else { |
| 11178 | tmp = *lastlt; |
| 11179 | tmp--; |
| 11180 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 11181 | if (tmp >= ctxt->input->base) |
| 11182 | *lastgt = tmp; |
| 11183 | else |
| 11184 | *lastgt = NULL; |
| 11185 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11186 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11187 | } else { |
| 11188 | *lastlt = NULL; |
| 11189 | *lastgt = NULL; |
| 11190 | } |
| 11191 | } |
| 11192 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11193 | * xmlCheckCdataPush: |
| 11194 | * @cur: pointer to the bock of characters |
| 11195 | * @len: length of the block in bytes |
| 11196 | * |
| 11197 | * Check that the block of characters is okay as SCdata content [20] |
| 11198 | * |
| 11199 | * Returns the number of bytes to pass if okay, a negative index where an |
| 11200 | * UTF-8 error occured otherwise |
| 11201 | */ |
| 11202 | static int |
| 11203 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 11204 | int ix; |
| 11205 | unsigned char c; |
| 11206 | int codepoint; |
| 11207 | |
| 11208 | if ((utf == NULL) || (len <= 0)) |
| 11209 | return(0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11210 | |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11211 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 11212 | c = utf[ix]; |
| 11213 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 11214 | if (c >= 0x20) |
| 11215 | ix++; |
| 11216 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 11217 | ix++; |
| 11218 | else |
| 11219 | return(-ix); |
| 11220 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
Daniel Veillard | 4a5d80a | 2015-09-18 15:06:46 +0800 | [diff] [blame] | 11221 | if (ix + 2 > len) return(-ix); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11222 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 11223 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11224 | codepoint = (utf[ix] & 0x1f) << 6; |
| 11225 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11226 | if (!xmlIsCharQ(codepoint)) |
| 11227 | return(-ix); |
| 11228 | ix += 2; |
| 11229 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
Daniel Veillard | 4a5d80a | 2015-09-18 15:06:46 +0800 | [diff] [blame] | 11230 | if (ix + 3 > len) return(-ix); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11231 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11232 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 11233 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11234 | codepoint = (utf[ix] & 0xf) << 12; |
| 11235 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 11236 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11237 | if (!xmlIsCharQ(codepoint)) |
| 11238 | return(-ix); |
| 11239 | ix += 3; |
| 11240 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
Daniel Veillard | 4a5d80a | 2015-09-18 15:06:46 +0800 | [diff] [blame] | 11241 | if (ix + 4 > len) return(-ix); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11242 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11243 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 11244 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 11245 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11246 | codepoint = (utf[ix] & 0x7) << 18; |
| 11247 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 11248 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 11249 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11250 | if (!xmlIsCharQ(codepoint)) |
| 11251 | return(-ix); |
| 11252 | ix += 4; |
| 11253 | } else /* unknown encoding */ |
| 11254 | return(-ix); |
| 11255 | } |
| 11256 | return(ix); |
| 11257 | } |
| 11258 | |
| 11259 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11260 | * xmlParseTryOrFinish: |
| 11261 | * @ctxt: an XML parser context |
| 11262 | * @terminate: last chunk indicator |
| 11263 | * |
| 11264 | * Try to progress on parsing |
| 11265 | * |
| 11266 | * Returns zero if no parsing was possible |
| 11267 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11268 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11269 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 11270 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11271 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11272 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11273 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11274 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11275 | if (ctxt->input == NULL) |
| 11276 | return(0); |
| 11277 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11278 | #ifdef DEBUG_PUSH |
| 11279 | switch (ctxt->instate) { |
| 11280 | case XML_PARSER_EOF: |
| 11281 | xmlGenericError(xmlGenericErrorContext, |
| 11282 | "PP: try EOF\n"); break; |
| 11283 | case XML_PARSER_START: |
| 11284 | xmlGenericError(xmlGenericErrorContext, |
| 11285 | "PP: try START\n"); break; |
| 11286 | case XML_PARSER_MISC: |
| 11287 | xmlGenericError(xmlGenericErrorContext, |
| 11288 | "PP: try MISC\n");break; |
| 11289 | case XML_PARSER_COMMENT: |
| 11290 | xmlGenericError(xmlGenericErrorContext, |
| 11291 | "PP: try COMMENT\n");break; |
| 11292 | case XML_PARSER_PROLOG: |
| 11293 | xmlGenericError(xmlGenericErrorContext, |
| 11294 | "PP: try PROLOG\n");break; |
| 11295 | case XML_PARSER_START_TAG: |
| 11296 | xmlGenericError(xmlGenericErrorContext, |
| 11297 | "PP: try START_TAG\n");break; |
| 11298 | case XML_PARSER_CONTENT: |
| 11299 | xmlGenericError(xmlGenericErrorContext, |
| 11300 | "PP: try CONTENT\n");break; |
| 11301 | case XML_PARSER_CDATA_SECTION: |
| 11302 | xmlGenericError(xmlGenericErrorContext, |
| 11303 | "PP: try CDATA_SECTION\n");break; |
| 11304 | case XML_PARSER_END_TAG: |
| 11305 | xmlGenericError(xmlGenericErrorContext, |
| 11306 | "PP: try END_TAG\n");break; |
| 11307 | case XML_PARSER_ENTITY_DECL: |
| 11308 | xmlGenericError(xmlGenericErrorContext, |
| 11309 | "PP: try ENTITY_DECL\n");break; |
| 11310 | case XML_PARSER_ENTITY_VALUE: |
| 11311 | xmlGenericError(xmlGenericErrorContext, |
| 11312 | "PP: try ENTITY_VALUE\n");break; |
| 11313 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11314 | xmlGenericError(xmlGenericErrorContext, |
| 11315 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 11316 | case XML_PARSER_DTD: |
| 11317 | xmlGenericError(xmlGenericErrorContext, |
| 11318 | "PP: try DTD\n");break; |
| 11319 | case XML_PARSER_EPILOG: |
| 11320 | xmlGenericError(xmlGenericErrorContext, |
| 11321 | "PP: try EPILOG\n");break; |
| 11322 | case XML_PARSER_PI: |
| 11323 | xmlGenericError(xmlGenericErrorContext, |
| 11324 | "PP: try PI\n");break; |
| 11325 | case XML_PARSER_IGNORE: |
| 11326 | xmlGenericError(xmlGenericErrorContext, |
| 11327 | "PP: try IGNORE\n");break; |
| 11328 | } |
| 11329 | #endif |
| 11330 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11331 | if ((ctxt->input != NULL) && |
| 11332 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11333 | xmlSHRINK(ctxt); |
| 11334 | ctxt->checkIndex = 0; |
| 11335 | } |
| 11336 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 11337 | |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11338 | while (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11339 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11340 | return(0); |
| 11341 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11342 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11343 | /* |
| 11344 | * Pop-up of finished entities. |
| 11345 | */ |
| 11346 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11347 | xmlPopInput(ctxt); |
| 11348 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11349 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11350 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11351 | avail = ctxt->input->length - |
| 11352 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11353 | else { |
| 11354 | /* |
| 11355 | * If we are operating on converted input, try to flush |
| 11356 | * remainng chars to avoid them stalling in the non-converted |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11357 | * buffer. But do not do this in document start where |
| 11358 | * encoding="..." may not have been read and we work on a |
| 11359 | * guessed encoding. |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11360 | */ |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11361 | if ((ctxt->instate != XML_PARSER_START) && |
| 11362 | (ctxt->input->buf->raw != NULL) && |
| 11363 | (xmlBufIsEmpty(ctxt->input->buf->raw) == 0)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11364 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 11365 | ctxt->input); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11366 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11367 | |
| 11368 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11369 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 11370 | base, current); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11371 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11372 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11373 | (ctxt->input->cur - ctxt->input->base); |
| 11374 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11375 | if (avail < 1) |
| 11376 | goto done; |
| 11377 | switch (ctxt->instate) { |
| 11378 | case XML_PARSER_EOF: |
| 11379 | /* |
| 11380 | * Document parsing is done ! |
| 11381 | */ |
| 11382 | goto done; |
| 11383 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11384 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 11385 | xmlChar start[4]; |
| 11386 | xmlCharEncoding enc; |
| 11387 | |
| 11388 | /* |
| 11389 | * Very first chars read from the document flow. |
| 11390 | */ |
| 11391 | if (avail < 4) |
| 11392 | goto done; |
| 11393 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11394 | /* |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11395 | * Get the 4 first bytes and decode the charset |
| 11396 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11397 | * plug some encoding conversion routines, |
| 11398 | * else xmlSwitchEncoding will set to (default) |
| 11399 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11400 | */ |
| 11401 | start[0] = RAW; |
| 11402 | start[1] = NXT(1); |
| 11403 | start[2] = NXT(2); |
| 11404 | start[3] = NXT(3); |
| 11405 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11406 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11407 | break; |
| 11408 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11409 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 11410 | if (avail < 2) |
| 11411 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11412 | cur = ctxt->input->cur[0]; |
| 11413 | next = ctxt->input->cur[1]; |
| 11414 | if (cur == 0) { |
| 11415 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11416 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11417 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11418 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 11419 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11420 | #ifdef DEBUG_PUSH |
| 11421 | xmlGenericError(xmlGenericErrorContext, |
| 11422 | "PP: entering EOF\n"); |
| 11423 | #endif |
| 11424 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11425 | ctxt->sax->endDocument(ctxt->userData); |
| 11426 | goto done; |
| 11427 | } |
| 11428 | if ((cur == '<') && (next == '?')) { |
| 11429 | /* PI or XML decl */ |
| 11430 | if (avail < 5) return(ret); |
| 11431 | if ((!terminate) && |
| 11432 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11433 | return(ret); |
| 11434 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11435 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11436 | &xmlDefaultSAXLocator); |
| 11437 | if ((ctxt->input->cur[2] == 'x') && |
| 11438 | (ctxt->input->cur[3] == 'm') && |
| 11439 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11440 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11441 | ret += 5; |
| 11442 | #ifdef DEBUG_PUSH |
| 11443 | xmlGenericError(xmlGenericErrorContext, |
| 11444 | "PP: Parsing XML Decl\n"); |
| 11445 | #endif |
| 11446 | xmlParseXMLDecl(ctxt); |
| 11447 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 11448 | /* |
| 11449 | * The XML REC instructs us to stop parsing right |
| 11450 | * here |
| 11451 | */ |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 11452 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11453 | return(0); |
| 11454 | } |
| 11455 | ctxt->standalone = ctxt->input->standalone; |
| 11456 | if ((ctxt->encoding == NULL) && |
| 11457 | (ctxt->input->encoding != NULL)) |
| 11458 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 11459 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11460 | (!ctxt->disableSAX)) |
| 11461 | ctxt->sax->startDocument(ctxt->userData); |
| 11462 | ctxt->instate = XML_PARSER_MISC; |
| 11463 | #ifdef DEBUG_PUSH |
| 11464 | xmlGenericError(xmlGenericErrorContext, |
| 11465 | "PP: entering MISC\n"); |
| 11466 | #endif |
| 11467 | } else { |
| 11468 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 11469 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11470 | (!ctxt->disableSAX)) |
| 11471 | ctxt->sax->startDocument(ctxt->userData); |
| 11472 | ctxt->instate = XML_PARSER_MISC; |
| 11473 | #ifdef DEBUG_PUSH |
| 11474 | xmlGenericError(xmlGenericErrorContext, |
| 11475 | "PP: entering MISC\n"); |
| 11476 | #endif |
| 11477 | } |
| 11478 | } else { |
| 11479 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11480 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11481 | &xmlDefaultSAXLocator); |
| 11482 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11483 | if (ctxt->version == NULL) { |
| 11484 | xmlErrMemory(ctxt, NULL); |
| 11485 | break; |
| 11486 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11487 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11488 | (!ctxt->disableSAX)) |
| 11489 | ctxt->sax->startDocument(ctxt->userData); |
| 11490 | ctxt->instate = XML_PARSER_MISC; |
| 11491 | #ifdef DEBUG_PUSH |
| 11492 | xmlGenericError(xmlGenericErrorContext, |
| 11493 | "PP: entering MISC\n"); |
| 11494 | #endif |
| 11495 | } |
| 11496 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11497 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11498 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 11499 | const xmlChar *prefix = NULL; |
| 11500 | const xmlChar *URI = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11501 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11502 | |
| 11503 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11504 | goto done; |
| 11505 | cur = ctxt->input->cur[0]; |
| 11506 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11507 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 11508 | xmlHaltParser(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11509 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11510 | ctxt->sax->endDocument(ctxt->userData); |
| 11511 | goto done; |
| 11512 | } |
| 11513 | if (!terminate) { |
| 11514 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 11515 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11516 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11517 | goto done; |
| 11518 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11519 | goto done; |
| 11520 | } |
| 11521 | } |
| 11522 | if (ctxt->spaceNr == 0) |
| 11523 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 11524 | else if (*ctxt->space == -2) |
| 11525 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11526 | else |
| 11527 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11528 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11529 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11530 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11531 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11532 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11533 | else |
| 11534 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11535 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11536 | if (ctxt->instate == XML_PARSER_EOF) |
| 11537 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11538 | if (name == NULL) { |
| 11539 | spacePop(ctxt); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 11540 | xmlHaltParser(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11541 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11542 | ctxt->sax->endDocument(ctxt->userData); |
| 11543 | goto done; |
| 11544 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11545 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11546 | /* |
| 11547 | * [ VC: Root Element Type ] |
| 11548 | * The Name in the document type declaration must match |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11549 | * the element type of the root element. |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11550 | */ |
| 11551 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 11552 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 11553 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11554 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11555 | |
| 11556 | /* |
| 11557 | * Check for an Empty Element. |
| 11558 | */ |
| 11559 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 11560 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11561 | |
| 11562 | if (ctxt->sax2) { |
| 11563 | if ((ctxt->sax != NULL) && |
| 11564 | (ctxt->sax->endElementNs != NULL) && |
| 11565 | (!ctxt->disableSAX)) |
| 11566 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 11567 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 11568 | if (ctxt->nsNr - nsNr > 0) |
| 11569 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11570 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11571 | } else { |
| 11572 | if ((ctxt->sax != NULL) && |
| 11573 | (ctxt->sax->endElement != NULL) && |
| 11574 | (!ctxt->disableSAX)) |
| 11575 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11576 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11577 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11578 | if (ctxt->instate == XML_PARSER_EOF) |
| 11579 | goto done; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11580 | spacePop(ctxt); |
| 11581 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11582 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11583 | } else { |
| 11584 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11585 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11586 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11587 | break; |
| 11588 | } |
| 11589 | if (RAW == '>') { |
| 11590 | NEXT; |
| 11591 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 11592 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11593 | "Couldn't find end of Start Tag %s\n", |
| 11594 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11595 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11596 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11597 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11598 | if (ctxt->sax2) |
| 11599 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11600 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11601 | else |
| 11602 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11603 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11604 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11605 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11606 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11607 | break; |
| 11608 | } |
| 11609 | case XML_PARSER_CONTENT: { |
| 11610 | const xmlChar *test; |
| 11611 | unsigned int cons; |
| 11612 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11613 | goto done; |
| 11614 | cur = ctxt->input->cur[0]; |
| 11615 | next = ctxt->input->cur[1]; |
| 11616 | |
| 11617 | test = CUR_PTR; |
| 11618 | cons = ctxt->input->consumed; |
| 11619 | if ((cur == '<') && (next == '/')) { |
| 11620 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11621 | break; |
| 11622 | } else if ((cur == '<') && (next == '?')) { |
| 11623 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11624 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11625 | ctxt->progressive = XML_PARSER_PI; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11626 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11627 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11628 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11629 | ctxt->instate = XML_PARSER_CONTENT; |
| 11630 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11631 | } else if ((cur == '<') && (next != '!')) { |
| 11632 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11633 | break; |
| 11634 | } else if ((cur == '<') && (next == '!') && |
| 11635 | (ctxt->input->cur[2] == '-') && |
| 11636 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 11637 | int term; |
| 11638 | |
| 11639 | if (avail < 4) |
| 11640 | goto done; |
| 11641 | ctxt->input->cur += 4; |
| 11642 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 11643 | ctxt->input->cur -= 4; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11644 | if ((!terminate) && (term < 0)) { |
| 11645 | ctxt->progressive = XML_PARSER_COMMENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11646 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11647 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11648 | xmlParseComment(ctxt); |
| 11649 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11650 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11651 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 11652 | (ctxt->input->cur[2] == '[') && |
| 11653 | (ctxt->input->cur[3] == 'C') && |
| 11654 | (ctxt->input->cur[4] == 'D') && |
| 11655 | (ctxt->input->cur[5] == 'A') && |
| 11656 | (ctxt->input->cur[6] == 'T') && |
| 11657 | (ctxt->input->cur[7] == 'A') && |
| 11658 | (ctxt->input->cur[8] == '[')) { |
| 11659 | SKIP(9); |
| 11660 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11661 | break; |
| 11662 | } else if ((cur == '<') && (next == '!') && |
| 11663 | (avail < 9)) { |
| 11664 | goto done; |
| 11665 | } else if (cur == '&') { |
| 11666 | if ((!terminate) && |
| 11667 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 11668 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11669 | xmlParseReference(ctxt); |
| 11670 | } else { |
| 11671 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 11672 | /* |
| 11673 | * Goal of the following test is: |
| 11674 | * - minimize calls to the SAX 'character' callback |
| 11675 | * when they are mergeable |
| 11676 | * - handle an problem for isBlank when we only parse |
| 11677 | * a sequence of blank chars and the next one is |
| 11678 | * not available to check against '<' presence. |
| 11679 | * - tries to homogenize the differences in SAX |
| 11680 | * callbacks between the push and pull versions |
| 11681 | * of the parser. |
| 11682 | */ |
| 11683 | if ((ctxt->inputNr == 1) && |
| 11684 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 11685 | if (!terminate) { |
| 11686 | if (ctxt->progressive) { |
| 11687 | if ((lastlt == NULL) || |
| 11688 | (ctxt->input->cur > lastlt)) |
| 11689 | goto done; |
| 11690 | } else if (xmlParseLookupSequence(ctxt, |
| 11691 | '<', 0, 0) < 0) { |
| 11692 | goto done; |
| 11693 | } |
| 11694 | } |
| 11695 | } |
| 11696 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11697 | xmlParseCharData(ctxt, 0); |
| 11698 | } |
| 11699 | /* |
| 11700 | * Pop-up of finished entities. |
| 11701 | */ |
| 11702 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11703 | xmlPopInput(ctxt); |
| 11704 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11705 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 11706 | "detected an error in element content\n"); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 11707 | xmlHaltParser(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11708 | break; |
| 11709 | } |
| 11710 | break; |
| 11711 | } |
| 11712 | case XML_PARSER_END_TAG: |
| 11713 | if (avail < 2) |
| 11714 | goto done; |
| 11715 | if (!terminate) { |
| 11716 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11717 | /* > can be found unescaped in attribute values */ |
| 11718 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11719 | goto done; |
| 11720 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11721 | goto done; |
| 11722 | } |
| 11723 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11724 | if (ctxt->sax2) { |
| 11725 | xmlParseEndTag2(ctxt, |
| 11726 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 11727 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11728 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11729 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11730 | } |
| 11731 | #ifdef LIBXML_SAX1_ENABLED |
| 11732 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11733 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11734 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11735 | if (ctxt->instate == XML_PARSER_EOF) { |
| 11736 | /* Nothing */ |
| 11737 | } else if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11738 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11739 | } else { |
| 11740 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11741 | } |
| 11742 | break; |
| 11743 | case XML_PARSER_CDATA_SECTION: { |
| 11744 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11745 | * The Push mode need to have the SAX callback for |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11746 | * cdataBlock merge back contiguous callbacks. |
| 11747 | */ |
| 11748 | int base; |
| 11749 | |
| 11750 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 11751 | if (base < 0) { |
| 11752 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11753 | int tmp; |
| 11754 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11755 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11756 | XML_PARSER_BIG_BUFFER_SIZE); |
| 11757 | if (tmp < 0) { |
| 11758 | tmp = -tmp; |
| 11759 | ctxt->input->cur += tmp; |
| 11760 | goto encoding_error; |
| 11761 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11762 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 11763 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11764 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11765 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11766 | else if (ctxt->sax->characters != NULL) |
| 11767 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11768 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11769 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11770 | if (ctxt->instate == XML_PARSER_EOF) |
| 11771 | goto done; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11772 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11773 | ctxt->checkIndex = 0; |
| 11774 | } |
| 11775 | goto done; |
| 11776 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11777 | int tmp; |
| 11778 | |
| 11779 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 11780 | if ((tmp < 0) || (tmp != base)) { |
| 11781 | tmp = -tmp; |
| 11782 | ctxt->input->cur += tmp; |
| 11783 | goto encoding_error; |
| 11784 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 11785 | if ((ctxt->sax != NULL) && (base == 0) && |
| 11786 | (ctxt->sax->cdataBlock != NULL) && |
| 11787 | (!ctxt->disableSAX)) { |
| 11788 | /* |
| 11789 | * Special case to provide identical behaviour |
| 11790 | * between pull and push parsers on enpty CDATA |
| 11791 | * sections |
| 11792 | */ |
| 11793 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 11794 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 11795 | "<![CDATA[", 9))) |
| 11796 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11797 | BAD_CAST "", 0); |
| 11798 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11799 | (!ctxt->disableSAX)) { |
| 11800 | if (ctxt->sax->cdataBlock != NULL) |
| 11801 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11802 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11803 | else if (ctxt->sax->characters != NULL) |
| 11804 | ctxt->sax->characters(ctxt->userData, |
| 11805 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11806 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11807 | if (ctxt->instate == XML_PARSER_EOF) |
| 11808 | goto done; |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 11809 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11810 | ctxt->checkIndex = 0; |
| 11811 | ctxt->instate = XML_PARSER_CONTENT; |
| 11812 | #ifdef DEBUG_PUSH |
| 11813 | xmlGenericError(xmlGenericErrorContext, |
| 11814 | "PP: entering CONTENT\n"); |
| 11815 | #endif |
| 11816 | } |
| 11817 | break; |
| 11818 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11819 | case XML_PARSER_MISC: |
| 11820 | SKIP_BLANKS; |
| 11821 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11822 | avail = ctxt->input->length - |
| 11823 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11824 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11825 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11826 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11827 | if (avail < 2) |
| 11828 | goto done; |
| 11829 | cur = ctxt->input->cur[0]; |
| 11830 | next = ctxt->input->cur[1]; |
| 11831 | if ((cur == '<') && (next == '?')) { |
| 11832 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11833 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11834 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11835 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11836 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11837 | #ifdef DEBUG_PUSH |
| 11838 | xmlGenericError(xmlGenericErrorContext, |
| 11839 | "PP: Parsing PI\n"); |
| 11840 | #endif |
| 11841 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11842 | if (ctxt->instate == XML_PARSER_EOF) |
| 11843 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11844 | ctxt->instate = XML_PARSER_MISC; |
| 11845 | ctxt->progressive = 1; |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11846 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11847 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11848 | (ctxt->input->cur[2] == '-') && |
| 11849 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11850 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11851 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11852 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11853 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11854 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11855 | #ifdef DEBUG_PUSH |
| 11856 | xmlGenericError(xmlGenericErrorContext, |
| 11857 | "PP: Parsing Comment\n"); |
| 11858 | #endif |
| 11859 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11860 | if (ctxt->instate == XML_PARSER_EOF) |
| 11861 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11862 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11863 | ctxt->progressive = 1; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11864 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11865 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11866 | (ctxt->input->cur[2] == 'D') && |
| 11867 | (ctxt->input->cur[3] == 'O') && |
| 11868 | (ctxt->input->cur[4] == 'C') && |
| 11869 | (ctxt->input->cur[5] == 'T') && |
| 11870 | (ctxt->input->cur[6] == 'Y') && |
| 11871 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11872 | (ctxt->input->cur[8] == 'E')) { |
| 11873 | if ((!terminate) && |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11874 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) { |
| 11875 | ctxt->progressive = XML_PARSER_DTD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11876 | goto done; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11877 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11878 | #ifdef DEBUG_PUSH |
| 11879 | xmlGenericError(xmlGenericErrorContext, |
| 11880 | "PP: Parsing internal subset\n"); |
| 11881 | #endif |
| 11882 | ctxt->inSubset = 1; |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 11883 | ctxt->progressive = 0; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11884 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11885 | xmlParseDocTypeDecl(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11886 | if (ctxt->instate == XML_PARSER_EOF) |
| 11887 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11888 | if (RAW == '[') { |
| 11889 | ctxt->instate = XML_PARSER_DTD; |
| 11890 | #ifdef DEBUG_PUSH |
| 11891 | xmlGenericError(xmlGenericErrorContext, |
| 11892 | "PP: entering DTD\n"); |
| 11893 | #endif |
| 11894 | } else { |
| 11895 | /* |
| 11896 | * Create and update the external subset. |
| 11897 | */ |
| 11898 | ctxt->inSubset = 2; |
| 11899 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11900 | (ctxt->sax->externalSubset != NULL)) |
| 11901 | ctxt->sax->externalSubset(ctxt->userData, |
| 11902 | ctxt->intSubName, ctxt->extSubSystem, |
| 11903 | ctxt->extSubURI); |
| 11904 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11905 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11906 | ctxt->instate = XML_PARSER_PROLOG; |
| 11907 | #ifdef DEBUG_PUSH |
| 11908 | xmlGenericError(xmlGenericErrorContext, |
| 11909 | "PP: entering PROLOG\n"); |
| 11910 | #endif |
| 11911 | } |
| 11912 | } else if ((cur == '<') && (next == '!') && |
| 11913 | (avail < 9)) { |
| 11914 | goto done; |
| 11915 | } else { |
| 11916 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11917 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11918 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11919 | #ifdef DEBUG_PUSH |
| 11920 | xmlGenericError(xmlGenericErrorContext, |
| 11921 | "PP: entering START_TAG\n"); |
| 11922 | #endif |
| 11923 | } |
| 11924 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11925 | case XML_PARSER_PROLOG: |
| 11926 | SKIP_BLANKS; |
| 11927 | if (ctxt->input->buf == NULL) |
| 11928 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11929 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11930 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11931 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11932 | if (avail < 2) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11933 | goto done; |
| 11934 | cur = ctxt->input->cur[0]; |
| 11935 | next = ctxt->input->cur[1]; |
| 11936 | if ((cur == '<') && (next == '?')) { |
| 11937 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11938 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11939 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11940 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11941 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11942 | #ifdef DEBUG_PUSH |
| 11943 | xmlGenericError(xmlGenericErrorContext, |
| 11944 | "PP: Parsing PI\n"); |
| 11945 | #endif |
| 11946 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11947 | if (ctxt->instate == XML_PARSER_EOF) |
| 11948 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11949 | ctxt->instate = XML_PARSER_PROLOG; |
| 11950 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11951 | } else if ((cur == '<') && (next == '!') && |
| 11952 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11953 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11954 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11955 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11956 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11957 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11958 | #ifdef DEBUG_PUSH |
| 11959 | xmlGenericError(xmlGenericErrorContext, |
| 11960 | "PP: Parsing Comment\n"); |
| 11961 | #endif |
| 11962 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11963 | if (ctxt->instate == XML_PARSER_EOF) |
| 11964 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11965 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11966 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11967 | } else if ((cur == '<') && (next == '!') && |
| 11968 | (avail < 4)) { |
| 11969 | goto done; |
| 11970 | } else { |
| 11971 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11972 | if (ctxt->progressive == 0) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11973 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11974 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11975 | #ifdef DEBUG_PUSH |
| 11976 | xmlGenericError(xmlGenericErrorContext, |
| 11977 | "PP: entering START_TAG\n"); |
| 11978 | #endif |
| 11979 | } |
| 11980 | break; |
| 11981 | case XML_PARSER_EPILOG: |
| 11982 | SKIP_BLANKS; |
| 11983 | if (ctxt->input->buf == NULL) |
| 11984 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11985 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11986 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11987 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11988 | if (avail < 2) |
| 11989 | goto done; |
| 11990 | cur = ctxt->input->cur[0]; |
| 11991 | next = ctxt->input->cur[1]; |
| 11992 | if ((cur == '<') && (next == '?')) { |
| 11993 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11994 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11995 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11996 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11997 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11998 | #ifdef DEBUG_PUSH |
| 11999 | xmlGenericError(xmlGenericErrorContext, |
| 12000 | "PP: Parsing PI\n"); |
| 12001 | #endif |
| 12002 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12003 | if (ctxt->instate == XML_PARSER_EOF) |
| 12004 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12005 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 12006 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12007 | } else if ((cur == '<') && (next == '!') && |
| 12008 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 12009 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12010 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 12011 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12012 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12013 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12014 | #ifdef DEBUG_PUSH |
| 12015 | xmlGenericError(xmlGenericErrorContext, |
| 12016 | "PP: Parsing Comment\n"); |
| 12017 | #endif |
| 12018 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12019 | if (ctxt->instate == XML_PARSER_EOF) |
| 12020 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12021 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12022 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12023 | } else if ((cur == '<') && (next == '!') && |
| 12024 | (avail < 4)) { |
| 12025 | goto done; |
| 12026 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12027 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 12028 | xmlHaltParser(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12029 | #ifdef DEBUG_PUSH |
| 12030 | xmlGenericError(xmlGenericErrorContext, |
| 12031 | "PP: entering EOF\n"); |
| 12032 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 12033 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12034 | ctxt->sax->endDocument(ctxt->userData); |
| 12035 | goto done; |
| 12036 | } |
| 12037 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12038 | case XML_PARSER_DTD: { |
| 12039 | /* |
| 12040 | * Sorry but progressive parsing of the internal subset |
| 12041 | * is not expected to be supported. We first check that |
| 12042 | * the full content of the internal subset is available and |
| 12043 | * the parsing is launched only at that point. |
| 12044 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 12045 | * section and not in a ']]>' sequence which are conditional |
| 12046 | * sections (whoever argued to keep that crap in XML deserve |
| 12047 | * a place in hell !). |
| 12048 | */ |
| 12049 | int base, i; |
| 12050 | xmlChar *buf; |
| 12051 | xmlChar quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12052 | size_t use; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12053 | |
| 12054 | base = ctxt->input->cur - ctxt->input->base; |
| 12055 | if (base < 0) return(0); |
| 12056 | if (ctxt->checkIndex > base) |
| 12057 | base = ctxt->checkIndex; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12058 | buf = xmlBufContent(ctxt->input->buf->buffer); |
| 12059 | use = xmlBufUse(ctxt->input->buf->buffer); |
| 12060 | for (;(unsigned int) base < use; base++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12061 | if (quote != 0) { |
| 12062 | if (buf[base] == quote) |
| 12063 | quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12064 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12065 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 12066 | if ((quote == 0) && (buf[base] == '<')) { |
| 12067 | int found = 0; |
| 12068 | /* special handling of comments */ |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12069 | if (((unsigned int) base + 4 < use) && |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 12070 | (buf[base + 1] == '!') && |
| 12071 | (buf[base + 2] == '-') && |
| 12072 | (buf[base + 3] == '-')) { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12073 | for (;(unsigned int) base + 3 < use; base++) { |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 12074 | if ((buf[base] == '-') && |
| 12075 | (buf[base + 1] == '-') && |
| 12076 | (buf[base + 2] == '>')) { |
| 12077 | found = 1; |
| 12078 | base += 2; |
| 12079 | break; |
| 12080 | } |
| 12081 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12082 | if (!found) { |
| 12083 | #if 0 |
| 12084 | fprintf(stderr, "unfinished comment\n"); |
| 12085 | #endif |
| 12086 | break; /* for */ |
| 12087 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 12088 | continue; |
| 12089 | } |
| 12090 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12091 | if (buf[base] == '"') { |
| 12092 | quote = '"'; |
| 12093 | continue; |
| 12094 | } |
| 12095 | if (buf[base] == '\'') { |
| 12096 | quote = '\''; |
| 12097 | continue; |
| 12098 | } |
| 12099 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12100 | #if 0 |
| 12101 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 12102 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 12103 | #endif |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12104 | if ((unsigned int) base +1 >= use) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12105 | break; |
| 12106 | if (buf[base + 1] == ']') { |
| 12107 | /* conditional crap, skip both ']' ! */ |
| 12108 | base++; |
| 12109 | continue; |
| 12110 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12111 | for (i = 1; (unsigned int) base + i < use; i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12112 | if (buf[base + i] == '>') { |
| 12113 | #if 0 |
| 12114 | fprintf(stderr, "found\n"); |
| 12115 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12116 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12117 | } |
| 12118 | if (!IS_BLANK_CH(buf[base + i])) { |
| 12119 | #if 0 |
| 12120 | fprintf(stderr, "not found\n"); |
| 12121 | #endif |
| 12122 | goto not_end_of_int_subset; |
| 12123 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12124 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12125 | #if 0 |
| 12126 | fprintf(stderr, "end of stream\n"); |
| 12127 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12128 | break; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12129 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12130 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12131 | not_end_of_int_subset: |
| 12132 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12133 | } |
| 12134 | /* |
| 12135 | * We didn't found the end of the Internal subset |
| 12136 | */ |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12137 | if (quote == 0) |
| 12138 | ctxt->checkIndex = base; |
| 12139 | else |
| 12140 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12141 | #ifdef DEBUG_PUSH |
| 12142 | if (next == 0) |
| 12143 | xmlGenericError(xmlGenericErrorContext, |
| 12144 | "PP: lookup of int subset end filed\n"); |
| 12145 | #endif |
| 12146 | goto done; |
| 12147 | |
| 12148 | found_end_int_subset: |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12149 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12150 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12151 | if (ctxt->instate == XML_PARSER_EOF) |
| 12152 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12153 | ctxt->inSubset = 2; |
| 12154 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 12155 | (ctxt->sax->externalSubset != NULL)) |
| 12156 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 12157 | ctxt->extSubSystem, ctxt->extSubURI); |
| 12158 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 12159 | xmlCleanSpecialAttr(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12160 | if (ctxt->instate == XML_PARSER_EOF) |
| 12161 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12162 | ctxt->instate = XML_PARSER_PROLOG; |
| 12163 | ctxt->checkIndex = 0; |
| 12164 | #ifdef DEBUG_PUSH |
| 12165 | xmlGenericError(xmlGenericErrorContext, |
| 12166 | "PP: entering PROLOG\n"); |
| 12167 | #endif |
| 12168 | break; |
| 12169 | } |
| 12170 | case XML_PARSER_COMMENT: |
| 12171 | xmlGenericError(xmlGenericErrorContext, |
| 12172 | "PP: internal error, state == COMMENT\n"); |
| 12173 | ctxt->instate = XML_PARSER_CONTENT; |
| 12174 | #ifdef DEBUG_PUSH |
| 12175 | xmlGenericError(xmlGenericErrorContext, |
| 12176 | "PP: entering CONTENT\n"); |
| 12177 | #endif |
| 12178 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 12179 | case XML_PARSER_IGNORE: |
| 12180 | xmlGenericError(xmlGenericErrorContext, |
| 12181 | "PP: internal error, state == IGNORE"); |
| 12182 | ctxt->instate = XML_PARSER_DTD; |
| 12183 | #ifdef DEBUG_PUSH |
| 12184 | xmlGenericError(xmlGenericErrorContext, |
| 12185 | "PP: entering DTD\n"); |
| 12186 | #endif |
| 12187 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12188 | case XML_PARSER_PI: |
| 12189 | xmlGenericError(xmlGenericErrorContext, |
| 12190 | "PP: internal error, state == PI\n"); |
| 12191 | ctxt->instate = XML_PARSER_CONTENT; |
| 12192 | #ifdef DEBUG_PUSH |
| 12193 | xmlGenericError(xmlGenericErrorContext, |
| 12194 | "PP: entering CONTENT\n"); |
| 12195 | #endif |
| 12196 | break; |
| 12197 | case XML_PARSER_ENTITY_DECL: |
| 12198 | xmlGenericError(xmlGenericErrorContext, |
| 12199 | "PP: internal error, state == ENTITY_DECL\n"); |
| 12200 | ctxt->instate = XML_PARSER_DTD; |
| 12201 | #ifdef DEBUG_PUSH |
| 12202 | xmlGenericError(xmlGenericErrorContext, |
| 12203 | "PP: entering DTD\n"); |
| 12204 | #endif |
| 12205 | break; |
| 12206 | case XML_PARSER_ENTITY_VALUE: |
| 12207 | xmlGenericError(xmlGenericErrorContext, |
| 12208 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 12209 | ctxt->instate = XML_PARSER_CONTENT; |
| 12210 | #ifdef DEBUG_PUSH |
| 12211 | xmlGenericError(xmlGenericErrorContext, |
| 12212 | "PP: entering DTD\n"); |
| 12213 | #endif |
| 12214 | break; |
| 12215 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 12216 | xmlGenericError(xmlGenericErrorContext, |
| 12217 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 12218 | ctxt->instate = XML_PARSER_START_TAG; |
| 12219 | #ifdef DEBUG_PUSH |
| 12220 | xmlGenericError(xmlGenericErrorContext, |
| 12221 | "PP: entering START_TAG\n"); |
| 12222 | #endif |
| 12223 | break; |
| 12224 | case XML_PARSER_SYSTEM_LITERAL: |
| 12225 | xmlGenericError(xmlGenericErrorContext, |
| 12226 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 12227 | ctxt->instate = XML_PARSER_START_TAG; |
| 12228 | #ifdef DEBUG_PUSH |
| 12229 | xmlGenericError(xmlGenericErrorContext, |
| 12230 | "PP: entering START_TAG\n"); |
| 12231 | #endif |
| 12232 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 12233 | case XML_PARSER_PUBLIC_LITERAL: |
| 12234 | xmlGenericError(xmlGenericErrorContext, |
| 12235 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 12236 | ctxt->instate = XML_PARSER_START_TAG; |
| 12237 | #ifdef DEBUG_PUSH |
| 12238 | xmlGenericError(xmlGenericErrorContext, |
| 12239 | "PP: entering START_TAG\n"); |
| 12240 | #endif |
| 12241 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12242 | } |
| 12243 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12244 | done: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12245 | #ifdef DEBUG_PUSH |
| 12246 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 12247 | #endif |
| 12248 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 12249 | encoding_error: |
| 12250 | { |
| 12251 | char buffer[150]; |
| 12252 | |
| 12253 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 12254 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 12255 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 12256 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 12257 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 12258 | BAD_CAST buffer, NULL); |
| 12259 | } |
| 12260 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12261 | } |
| 12262 | |
| 12263 | /** |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12264 | * xmlParseCheckTransition: |
| 12265 | * @ctxt: an XML parser context |
| 12266 | * @chunk: a char array |
| 12267 | * @size: the size in byte of the chunk |
| 12268 | * |
| 12269 | * Check depending on the current parser state if the chunk given must be |
| 12270 | * processed immediately or one need more data to advance on parsing. |
| 12271 | * |
| 12272 | * Returns -1 in case of error, 0 if the push is not needed and 1 if needed |
| 12273 | */ |
| 12274 | static int |
| 12275 | xmlParseCheckTransition(xmlParserCtxtPtr ctxt, const char *chunk, int size) { |
| 12276 | if ((ctxt == NULL) || (chunk == NULL) || (size < 0)) |
| 12277 | return(-1); |
| 12278 | if (ctxt->instate == XML_PARSER_START_TAG) { |
| 12279 | if (memchr(chunk, '>', size) != NULL) |
| 12280 | return(1); |
| 12281 | return(0); |
| 12282 | } |
| 12283 | if (ctxt->progressive == XML_PARSER_COMMENT) { |
| 12284 | if (memchr(chunk, '>', size) != NULL) |
| 12285 | return(1); |
| 12286 | return(0); |
| 12287 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12288 | if (ctxt->instate == XML_PARSER_CDATA_SECTION) { |
| 12289 | if (memchr(chunk, '>', size) != NULL) |
| 12290 | return(1); |
| 12291 | return(0); |
| 12292 | } |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 12293 | if (ctxt->progressive == XML_PARSER_PI) { |
| 12294 | if (memchr(chunk, '>', size) != NULL) |
| 12295 | return(1); |
| 12296 | return(0); |
| 12297 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12298 | if (ctxt->instate == XML_PARSER_END_TAG) { |
| 12299 | if (memchr(chunk, '>', size) != NULL) |
| 12300 | return(1); |
| 12301 | return(0); |
| 12302 | } |
| 12303 | if ((ctxt->progressive == XML_PARSER_DTD) || |
| 12304 | (ctxt->instate == XML_PARSER_DTD)) { |
Dan Winship | cf8f042 | 2012-12-21 11:13:31 +0800 | [diff] [blame] | 12305 | if (memchr(chunk, '>', size) != NULL) |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12306 | return(1); |
| 12307 | return(0); |
| 12308 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12309 | return(1); |
| 12310 | } |
| 12311 | |
| 12312 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12313 | * xmlParseChunk: |
| 12314 | * @ctxt: an XML parser context |
| 12315 | * @chunk: an char array |
| 12316 | * @size: the size in byte of the chunk |
| 12317 | * @terminate: last chunk indicator |
| 12318 | * |
| 12319 | * Parse a Chunk of memory |
| 12320 | * |
| 12321 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 12322 | */ |
| 12323 | int |
| 12324 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 12325 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12326 | int end_in_lf = 0; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12327 | int remain = 0; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12328 | size_t old_avail = 0; |
| 12329 | size_t avail = 0; |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12330 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12331 | if (ctxt == NULL) |
| 12332 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 12333 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12334 | return(ctxt->errNo); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 12335 | if (ctxt->instate == XML_PARSER_EOF) |
| 12336 | return(-1); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 12337 | if (ctxt->instate == XML_PARSER_START) |
| 12338 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12339 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 12340 | (chunk[size - 1] == '\r')) { |
| 12341 | end_in_lf = 1; |
| 12342 | size--; |
| 12343 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12344 | |
| 12345 | xmldecl_done: |
| 12346 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12347 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12348 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12349 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12350 | size_t cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12351 | int res; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12352 | |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12353 | old_avail = xmlBufUse(ctxt->input->buf->buffer); |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12354 | /* |
| 12355 | * Specific handling if we autodetected an encoding, we should not |
| 12356 | * push more than the first line ... which depend on the encoding |
| 12357 | * And only push the rest once the final encoding was detected |
| 12358 | */ |
| 12359 | if ((ctxt->instate == XML_PARSER_START) && (ctxt->input != NULL) && |
| 12360 | (ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL)) { |
Nikolay Sivov | 7304683 | 2010-01-19 15:38:05 +0100 | [diff] [blame] | 12361 | unsigned int len = 45; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12362 | |
| 12363 | if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12364 | BAD_CAST "UTF-16")) || |
| 12365 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12366 | BAD_CAST "UTF16"))) |
| 12367 | len = 90; |
| 12368 | else if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12369 | BAD_CAST "UCS-4")) || |
| 12370 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12371 | BAD_CAST "UCS4"))) |
| 12372 | len = 180; |
| 12373 | |
| 12374 | if (ctxt->input->buf->rawconsumed < len) |
| 12375 | len -= ctxt->input->buf->rawconsumed; |
| 12376 | |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12377 | /* |
| 12378 | * Change size for reading the initial declaration only |
| 12379 | * if size is greater than len. Otherwise, memmove in xmlBufferAdd |
| 12380 | * will blindly copy extra bytes from memory. |
| 12381 | */ |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 12382 | if ((unsigned int) size > len) { |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12383 | remain = size - len; |
| 12384 | size = len; |
| 12385 | } else { |
| 12386 | remain = 0; |
| 12387 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12388 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12389 | res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12390 | if (res < 0) { |
| 12391 | ctxt->errNo = XML_PARSER_EOF; |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 12392 | xmlHaltParser(ctxt); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12393 | return (XML_PARSER_EOF); |
| 12394 | } |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12395 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12396 | #ifdef DEBUG_PUSH |
| 12397 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12398 | #endif |
| 12399 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12400 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 12401 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 12402 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 12403 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 12404 | (in->raw != NULL)) { |
| 12405 | int nbchars; |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12406 | size_t base = xmlBufGetInputBase(in->buffer, ctxt->input); |
| 12407 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12408 | |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 12409 | nbchars = xmlCharEncInput(in, terminate); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12410 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12411 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12412 | xmlGenericError(xmlGenericErrorContext, |
| 12413 | "xmlParseChunk: encoder error\n"); |
| 12414 | return(XML_ERR_INVALID_ENCODING); |
| 12415 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12416 | xmlBufSetInputBaseCur(in->buffer, ctxt->input, base, current); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12417 | } |
| 12418 | } |
| 12419 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12420 | if (remain != 0) { |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12421 | xmlParseTryOrFinish(ctxt, 0); |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12422 | } else { |
| 12423 | if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) |
| 12424 | avail = xmlBufUse(ctxt->input->buf->buffer); |
| 12425 | /* |
| 12426 | * Depending on the current state it may not be such |
| 12427 | * a good idea to try parsing if there is nothing in the chunk |
| 12428 | * which would be worth doing a parser state transition and we |
| 12429 | * need to wait for more data |
| 12430 | */ |
| 12431 | if ((terminate) || (avail > XML_MAX_TEXT_LENGTH) || |
| 12432 | (old_avail == 0) || (avail == 0) || |
| 12433 | (xmlParseCheckTransition(ctxt, |
| 12434 | (const char *)&ctxt->input->base[old_avail], |
| 12435 | avail - old_avail))) |
| 12436 | xmlParseTryOrFinish(ctxt, terminate); |
| 12437 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12438 | if (ctxt->instate == XML_PARSER_EOF) |
| 12439 | return(ctxt->errNo); |
| 12440 | |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12441 | if ((ctxt->input != NULL) && |
| 12442 | (((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || |
| 12443 | ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && |
| 12444 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 12445 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
Daniel Veillard | e3b1597 | 2015-11-20 14:59:30 +0800 | [diff] [blame] | 12446 | xmlHaltParser(ctxt); |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12447 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12448 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 12449 | return(ctxt->errNo); |
| 12450 | |
| 12451 | if (remain != 0) { |
| 12452 | chunk += size; |
| 12453 | size = remain; |
| 12454 | remain = 0; |
| 12455 | goto xmldecl_done; |
| 12456 | } |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12457 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 12458 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12459 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 12460 | ctxt->input); |
| 12461 | size_t current = ctxt->input->cur - ctxt->input->base; |
| 12462 | |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12463 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12464 | |
| 12465 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 12466 | base, current); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12467 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12468 | if (terminate) { |
| 12469 | /* |
| 12470 | * Check for termination |
| 12471 | */ |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12472 | int cur_avail = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12473 | |
| 12474 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12475 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12476 | cur_avail = ctxt->input->length - |
| 12477 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12478 | else |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12479 | cur_avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 12480 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12481 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12482 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12483 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 12484 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12485 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12486 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12487 | if ((ctxt->instate == XML_PARSER_EPILOG) && (cur_avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12488 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12489 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12490 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 12491 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12492 | ctxt->sax->endDocument(ctxt->userData); |
| 12493 | } |
| 12494 | ctxt->instate = XML_PARSER_EOF; |
| 12495 | } |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 12496 | if (ctxt->wellFormed == 0) |
| 12497 | return((xmlParserErrors) ctxt->errNo); |
| 12498 | else |
| 12499 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12500 | } |
| 12501 | |
| 12502 | /************************************************************************ |
| 12503 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12504 | * I/O front end functions to the parser * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12505 | * * |
| 12506 | ************************************************************************/ |
| 12507 | |
| 12508 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12509 | * xmlCreatePushParserCtxt: |
| 12510 | * @sax: a SAX handler |
| 12511 | * @user_data: The user data returned on SAX callbacks |
| 12512 | * @chunk: a pointer to an array of chars |
| 12513 | * @size: number of chars in the array |
| 12514 | * @filename: an optional file name or URI |
| 12515 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12516 | * Create a parser context for using the XML parser in push mode. |
| 12517 | * If @buffer and @size are non-NULL, the data is used to detect |
| 12518 | * the encoding. The remaining characters will be parsed so they |
| 12519 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12520 | * To allow content encoding detection, @size should be >= 4 |
| 12521 | * The value of @filename is used for fetching external entities |
| 12522 | * and error/warning reports. |
| 12523 | * |
| 12524 | * Returns the new parser context or NULL |
| 12525 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12526 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12527 | xmlParserCtxtPtr |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12528 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12529 | const char *chunk, int size, const char *filename) { |
| 12530 | xmlParserCtxtPtr ctxt; |
| 12531 | xmlParserInputPtr inputStream; |
| 12532 | xmlParserInputBufferPtr buf; |
| 12533 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12534 | |
| 12535 | /* |
| 12536 | * plug some encoding conversion routines |
| 12537 | */ |
| 12538 | if ((chunk != NULL) && (size >= 4)) |
| 12539 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12540 | |
| 12541 | buf = xmlAllocParserInputBuffer(enc); |
| 12542 | if (buf == NULL) return(NULL); |
| 12543 | |
| 12544 | ctxt = xmlNewParserCtxt(); |
| 12545 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12546 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12547 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12548 | return(NULL); |
| 12549 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12550 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12551 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 12552 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12553 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12554 | xmlFreeParserInputBuffer(buf); |
| 12555 | xmlFreeParserCtxt(ctxt); |
| 12556 | return(NULL); |
| 12557 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12558 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12559 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12560 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12561 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12562 | xmlFree(ctxt->sax); |
| 12563 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12564 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12565 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12566 | xmlFreeParserInputBuffer(buf); |
| 12567 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12568 | return(NULL); |
| 12569 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12570 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12571 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12572 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12573 | else |
| 12574 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12575 | if (user_data != NULL) |
| 12576 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12577 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12578 | if (filename == NULL) { |
| 12579 | ctxt->directory = NULL; |
| 12580 | } else { |
| 12581 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12582 | } |
| 12583 | |
| 12584 | inputStream = xmlNewInputStream(ctxt); |
| 12585 | if (inputStream == NULL) { |
| 12586 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12587 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12588 | return(NULL); |
| 12589 | } |
| 12590 | |
| 12591 | if (filename == NULL) |
| 12592 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12593 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 12594 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 12595 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12596 | if (inputStream->filename == NULL) { |
| 12597 | xmlFreeParserCtxt(ctxt); |
| 12598 | xmlFreeParserInputBuffer(buf); |
| 12599 | return(NULL); |
| 12600 | } |
| 12601 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12602 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 12603 | xmlBufResetInput(inputStream->buf->buffer, inputStream); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12604 | inputPush(ctxt, inputStream); |
| 12605 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 12606 | /* |
| 12607 | * If the caller didn't provide an initial 'chunk' for determining |
| 12608 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 12609 | * that it can be automatically determined later |
| 12610 | */ |
| 12611 | if ((size == 0) || (chunk == NULL)) { |
| 12612 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 12613 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12614 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12615 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12616 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12617 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12618 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12619 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12620 | #ifdef DEBUG_PUSH |
| 12621 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12622 | #endif |
| 12623 | } |
| 12624 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 12625 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12626 | xmlSwitchEncoding(ctxt, enc); |
| 12627 | } |
| 12628 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12629 | return(ctxt); |
| 12630 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 12631 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12632 | |
| 12633 | /** |
Daniel Veillard | 28cd9cb | 2015-11-20 14:55:30 +0800 | [diff] [blame] | 12634 | * xmlHaltParser: |
| 12635 | * @ctxt: an XML parser context |
| 12636 | * |
| 12637 | * Blocks further parser processing don't override error |
| 12638 | * for internal use |
| 12639 | */ |
| 12640 | static void |
| 12641 | xmlHaltParser(xmlParserCtxtPtr ctxt) { |
| 12642 | if (ctxt == NULL) |
| 12643 | return; |
| 12644 | ctxt->instate = XML_PARSER_EOF; |
| 12645 | ctxt->disableSAX = 1; |
| 12646 | if (ctxt->input != NULL) { |
| 12647 | /* |
| 12648 | * in case there was a specific allocation deallocate before |
| 12649 | * overriding base |
| 12650 | */ |
| 12651 | if (ctxt->input->free != NULL) { |
| 12652 | ctxt->input->free((xmlChar *) ctxt->input->base); |
| 12653 | ctxt->input->free = NULL; |
| 12654 | } |
| 12655 | ctxt->input->cur = BAD_CAST""; |
| 12656 | ctxt->input->base = ctxt->input->cur; |
| 12657 | } |
| 12658 | } |
| 12659 | |
| 12660 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12661 | * xmlStopParser: |
| 12662 | * @ctxt: an XML parser context |
| 12663 | * |
| 12664 | * Blocks further parser processing |
| 12665 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12666 | void |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12667 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 12668 | if (ctxt == NULL) |
| 12669 | return; |
Daniel Veillard | 28cd9cb | 2015-11-20 14:55:30 +0800 | [diff] [blame] | 12670 | xmlHaltParser(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12671 | ctxt->errNo = XML_ERR_USER_STOP; |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12672 | } |
| 12673 | |
| 12674 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12675 | * xmlCreateIOParserCtxt: |
| 12676 | * @sax: a SAX handler |
| 12677 | * @user_data: The user data returned on SAX callbacks |
| 12678 | * @ioread: an I/O read function |
| 12679 | * @ioclose: an I/O close function |
| 12680 | * @ioctx: an I/O handler |
| 12681 | * @enc: the charset encoding if known |
| 12682 | * |
| 12683 | * Create a parser context for using the XML parser with an existing |
| 12684 | * I/O stream |
| 12685 | * |
| 12686 | * Returns the new parser context or NULL |
| 12687 | */ |
| 12688 | xmlParserCtxtPtr |
| 12689 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 12690 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 12691 | void *ioctx, xmlCharEncoding enc) { |
| 12692 | xmlParserCtxtPtr ctxt; |
| 12693 | xmlParserInputPtr inputStream; |
| 12694 | xmlParserInputBufferPtr buf; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12695 | |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 12696 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12697 | |
| 12698 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12699 | if (buf == NULL) { |
| 12700 | if (ioclose != NULL) |
| 12701 | ioclose(ioctx); |
| 12702 | return (NULL); |
| 12703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12704 | |
| 12705 | ctxt = xmlNewParserCtxt(); |
| 12706 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12707 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12708 | return(NULL); |
| 12709 | } |
| 12710 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12711 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12712 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12713 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12714 | xmlFree(ctxt->sax); |
| 12715 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12716 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12717 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12718 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12719 | return(NULL); |
| 12720 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12721 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12722 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12723 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12724 | else |
| 12725 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12726 | if (user_data != NULL) |
| 12727 | ctxt->userData = user_data; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12729 | |
| 12730 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 12731 | if (inputStream == NULL) { |
| 12732 | xmlFreeParserCtxt(ctxt); |
| 12733 | return(NULL); |
| 12734 | } |
| 12735 | inputPush(ctxt, inputStream); |
| 12736 | |
| 12737 | return(ctxt); |
| 12738 | } |
| 12739 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12740 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12741 | /************************************************************************ |
| 12742 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12743 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12744 | * * |
| 12745 | ************************************************************************/ |
| 12746 | |
| 12747 | /** |
| 12748 | * xmlIOParseDTD: |
| 12749 | * @sax: the SAX handler block or NULL |
| 12750 | * @input: an Input Buffer |
| 12751 | * @enc: the charset encoding if known |
| 12752 | * |
| 12753 | * Load and parse a DTD |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12754 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12755 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12756 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12757 | */ |
| 12758 | |
| 12759 | xmlDtdPtr |
| 12760 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 12761 | xmlCharEncoding enc) { |
| 12762 | xmlDtdPtr ret = NULL; |
| 12763 | xmlParserCtxtPtr ctxt; |
| 12764 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12765 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12766 | |
| 12767 | if (input == NULL) |
| 12768 | return(NULL); |
| 12769 | |
| 12770 | ctxt = xmlNewParserCtxt(); |
| 12771 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12772 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12773 | return(NULL); |
| 12774 | } |
| 12775 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12776 | /* We are loading a DTD */ |
| 12777 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12778 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12779 | /* |
| 12780 | * Set-up the SAX context |
| 12781 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12782 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12783 | if (ctxt->sax != NULL) |
| 12784 | xmlFree(ctxt->sax); |
| 12785 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 12786 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12787 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12788 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12789 | |
| 12790 | /* |
| 12791 | * generate a parser input from the I/O handler |
| 12792 | */ |
| 12793 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12794 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12795 | if (pinput == NULL) { |
| 12796 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12797 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12798 | xmlFreeParserCtxt(ctxt); |
| 12799 | return(NULL); |
| 12800 | } |
| 12801 | |
| 12802 | /* |
| 12803 | * plug some encoding conversion routines here. |
| 12804 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12805 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 12806 | if (sax != NULL) ctxt->sax = NULL; |
| 12807 | xmlFreeParserCtxt(ctxt); |
| 12808 | return(NULL); |
| 12809 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12810 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12811 | xmlSwitchEncoding(ctxt, enc); |
| 12812 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12813 | |
| 12814 | pinput->filename = NULL; |
| 12815 | pinput->line = 1; |
| 12816 | pinput->col = 1; |
| 12817 | pinput->base = ctxt->input->cur; |
| 12818 | pinput->cur = ctxt->input->cur; |
| 12819 | pinput->free = NULL; |
| 12820 | |
| 12821 | /* |
| 12822 | * let's parse that entity knowing it's an external subset. |
| 12823 | */ |
| 12824 | ctxt->inSubset = 2; |
| 12825 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12826 | if (ctxt->myDoc == NULL) { |
| 12827 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12828 | return(NULL); |
| 12829 | } |
| 12830 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12831 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12832 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12833 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12834 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 12835 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12836 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12837 | * Get the 4 first bytes and decode the charset |
| 12838 | * if enc != XML_CHAR_ENCODING_NONE |
| 12839 | * plug some encoding conversion routines. |
| 12840 | */ |
| 12841 | start[0] = RAW; |
| 12842 | start[1] = NXT(1); |
| 12843 | start[2] = NXT(2); |
| 12844 | start[3] = NXT(3); |
| 12845 | enc = xmlDetectCharEncoding(start, 4); |
| 12846 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12847 | xmlSwitchEncoding(ctxt, enc); |
| 12848 | } |
| 12849 | } |
| 12850 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12851 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 12852 | |
| 12853 | if (ctxt->myDoc != NULL) { |
| 12854 | if (ctxt->wellFormed) { |
| 12855 | ret = ctxt->myDoc->extSubset; |
| 12856 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 12857 | if (ret != NULL) { |
| 12858 | xmlNodePtr tmp; |
| 12859 | |
| 12860 | ret->doc = NULL; |
| 12861 | tmp = ret->children; |
| 12862 | while (tmp != NULL) { |
| 12863 | tmp->doc = NULL; |
| 12864 | tmp = tmp->next; |
| 12865 | } |
| 12866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12867 | } else { |
| 12868 | ret = NULL; |
| 12869 | } |
| 12870 | xmlFreeDoc(ctxt->myDoc); |
| 12871 | ctxt->myDoc = NULL; |
| 12872 | } |
| 12873 | if (sax != NULL) ctxt->sax = NULL; |
| 12874 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12875 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12876 | return(ret); |
| 12877 | } |
| 12878 | |
| 12879 | /** |
| 12880 | * xmlSAXParseDTD: |
| 12881 | * @sax: the SAX handler block |
| 12882 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12883 | * @SystemID: a NAME* containing the URL to the DTD |
| 12884 | * |
| 12885 | * Load and parse an external subset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12886 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12887 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12888 | */ |
| 12889 | |
| 12890 | xmlDtdPtr |
| 12891 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 12892 | const xmlChar *SystemID) { |
| 12893 | xmlDtdPtr ret = NULL; |
| 12894 | xmlParserCtxtPtr ctxt; |
| 12895 | xmlParserInputPtr input = NULL; |
| 12896 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12897 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12898 | |
| 12899 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 12900 | |
| 12901 | ctxt = xmlNewParserCtxt(); |
| 12902 | if (ctxt == NULL) { |
| 12903 | return(NULL); |
| 12904 | } |
| 12905 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12906 | /* We are loading a DTD */ |
| 12907 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12908 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12909 | /* |
| 12910 | * Set-up the SAX context |
| 12911 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12912 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12913 | if (ctxt->sax != NULL) |
| 12914 | xmlFree(ctxt->sax); |
| 12915 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 12916 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12917 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12918 | |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12919 | /* |
| 12920 | * Canonicalise the system ID |
| 12921 | */ |
| 12922 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 12923 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12924 | xmlFreeParserCtxt(ctxt); |
| 12925 | return(NULL); |
| 12926 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12927 | |
| 12928 | /* |
| 12929 | * Ask the Entity resolver to load the damn thing |
| 12930 | */ |
| 12931 | |
| 12932 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 12933 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 12934 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12935 | if (input == NULL) { |
| 12936 | if (sax != NULL) ctxt->sax = NULL; |
| 12937 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12938 | if (systemIdCanonic != NULL) |
| 12939 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12940 | return(NULL); |
| 12941 | } |
| 12942 | |
| 12943 | /* |
| 12944 | * plug some encoding conversion routines here. |
| 12945 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12946 | if (xmlPushInput(ctxt, input) < 0) { |
| 12947 | if (sax != NULL) ctxt->sax = NULL; |
| 12948 | xmlFreeParserCtxt(ctxt); |
| 12949 | if (systemIdCanonic != NULL) |
| 12950 | xmlFree(systemIdCanonic); |
| 12951 | return(NULL); |
| 12952 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12953 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12954 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 12955 | xmlSwitchEncoding(ctxt, enc); |
| 12956 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12957 | |
| 12958 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12959 | input->filename = (char *) systemIdCanonic; |
| 12960 | else |
| 12961 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12962 | input->line = 1; |
| 12963 | input->col = 1; |
| 12964 | input->base = ctxt->input->cur; |
| 12965 | input->cur = ctxt->input->cur; |
| 12966 | input->free = NULL; |
| 12967 | |
| 12968 | /* |
| 12969 | * let's parse that entity knowing it's an external subset. |
| 12970 | */ |
| 12971 | ctxt->inSubset = 2; |
| 12972 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12973 | if (ctxt->myDoc == NULL) { |
| 12974 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12975 | if (sax != NULL) ctxt->sax = NULL; |
| 12976 | xmlFreeParserCtxt(ctxt); |
| 12977 | return(NULL); |
| 12978 | } |
| 12979 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12980 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12981 | ExternalID, SystemID); |
| 12982 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 12983 | |
| 12984 | if (ctxt->myDoc != NULL) { |
| 12985 | if (ctxt->wellFormed) { |
| 12986 | ret = ctxt->myDoc->extSubset; |
| 12987 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 12988 | if (ret != NULL) { |
| 12989 | xmlNodePtr tmp; |
| 12990 | |
| 12991 | ret->doc = NULL; |
| 12992 | tmp = ret->children; |
| 12993 | while (tmp != NULL) { |
| 12994 | tmp->doc = NULL; |
| 12995 | tmp = tmp->next; |
| 12996 | } |
| 12997 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12998 | } else { |
| 12999 | ret = NULL; |
| 13000 | } |
| 13001 | xmlFreeDoc(ctxt->myDoc); |
| 13002 | ctxt->myDoc = NULL; |
| 13003 | } |
| 13004 | if (sax != NULL) ctxt->sax = NULL; |
| 13005 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13006 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13007 | return(ret); |
| 13008 | } |
| 13009 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13010 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13011 | /** |
| 13012 | * xmlParseDTD: |
| 13013 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 13014 | * @SystemID: a NAME* containing the URL to the DTD |
| 13015 | * |
| 13016 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13017 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13018 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 13019 | */ |
| 13020 | |
| 13021 | xmlDtdPtr |
| 13022 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 13023 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 13024 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13025 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13026 | |
| 13027 | /************************************************************************ |
| 13028 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13029 | * Front ends when parsing an Entity * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13030 | * * |
| 13031 | ************************************************************************/ |
| 13032 | |
| 13033 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13034 | * xmlParseCtxtExternalEntity: |
| 13035 | * @ctx: the existing parsing context |
| 13036 | * @URL: the URL for the entity to load |
| 13037 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13038 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13039 | * |
| 13040 | * Parse an external general entity within an existing parsing context |
| 13041 | * An external general parsed entity is well-formed if it matches the |
| 13042 | * production labeled extParsedEnt. |
| 13043 | * |
| 13044 | * [78] extParsedEnt ::= TextDecl? content |
| 13045 | * |
| 13046 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13047 | * the parser error code otherwise |
| 13048 | */ |
| 13049 | |
| 13050 | int |
| 13051 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13052 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13053 | xmlParserCtxtPtr ctxt; |
| 13054 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13055 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13056 | xmlSAXHandlerPtr oldsax = NULL; |
| 13057 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13058 | xmlChar start[4]; |
| 13059 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13060 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 13061 | if (ctx == NULL) return(-1); |
| 13062 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13063 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 13064 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13065 | return(XML_ERR_ENTITY_LOOP); |
| 13066 | } |
| 13067 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13068 | if (lst != NULL) |
| 13069 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13070 | if ((URL == NULL) && (ID == NULL)) |
| 13071 | return(-1); |
| 13072 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 13073 | return(-1); |
| 13074 | |
Rob Richards | 798743a | 2009-06-19 13:54:25 -0400 | [diff] [blame] | 13075 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, ctx); |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 13076 | if (ctxt == NULL) { |
| 13077 | return(-1); |
| 13078 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13079 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13080 | oldsax = ctxt->sax; |
| 13081 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13082 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13083 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13084 | if (newDoc == NULL) { |
| 13085 | xmlFreeParserCtxt(ctxt); |
| 13086 | return(-1); |
| 13087 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13088 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13089 | if (ctx->myDoc->dict) { |
| 13090 | newDoc->dict = ctx->myDoc->dict; |
| 13091 | xmlDictReference(newDoc->dict); |
| 13092 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13093 | if (ctx->myDoc != NULL) { |
| 13094 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 13095 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 13096 | } |
| 13097 | if (ctx->myDoc->URL != NULL) { |
| 13098 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 13099 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13100 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13101 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13102 | ctxt->sax = oldsax; |
| 13103 | xmlFreeParserCtxt(ctxt); |
| 13104 | newDoc->intSubset = NULL; |
| 13105 | newDoc->extSubset = NULL; |
| 13106 | xmlFreeDoc(newDoc); |
| 13107 | return(-1); |
| 13108 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13109 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13110 | nodePush(ctxt, newDoc->children); |
| 13111 | if (ctx->myDoc == NULL) { |
| 13112 | ctxt->myDoc = newDoc; |
| 13113 | } else { |
| 13114 | ctxt->myDoc = ctx->myDoc; |
| 13115 | newDoc->children->doc = ctx->myDoc; |
| 13116 | } |
| 13117 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13118 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13119 | * Get the 4 first bytes and decode the charset |
| 13120 | * if enc != XML_CHAR_ENCODING_NONE |
| 13121 | * plug some encoding conversion routines. |
| 13122 | */ |
| 13123 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13124 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13125 | start[0] = RAW; |
| 13126 | start[1] = NXT(1); |
| 13127 | start[2] = NXT(2); |
| 13128 | start[3] = NXT(3); |
| 13129 | enc = xmlDetectCharEncoding(start, 4); |
| 13130 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13131 | xmlSwitchEncoding(ctxt, enc); |
| 13132 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13133 | } |
| 13134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13135 | /* |
| 13136 | * Parse a possible text declaration first |
| 13137 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13138 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13139 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13140 | /* |
| 13141 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 13142 | */ |
| 13143 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 13144 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13145 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13146 | "Version mismatch between document and entity\n"); |
| 13147 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13148 | } |
| 13149 | |
| 13150 | /* |
Daniel Veillard | 4aa68ab | 2012-04-02 17:50:54 +0800 | [diff] [blame] | 13151 | * If the user provided its own SAX callbacks then reuse the |
| 13152 | * useData callback field, otherwise the expected setup in a |
| 13153 | * DOM builder is to have userData == ctxt |
| 13154 | */ |
| 13155 | if (ctx->userData == ctx) |
| 13156 | ctxt->userData = ctxt; |
| 13157 | else |
| 13158 | ctxt->userData = ctx->userData; |
| 13159 | |
| 13160 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13161 | * Doing validity checking on chunk doesn't make sense |
| 13162 | */ |
| 13163 | ctxt->instate = XML_PARSER_CONTENT; |
| 13164 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13165 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13166 | ctxt->loadsubset = ctx->loadsubset; |
| 13167 | ctxt->depth = ctx->depth + 1; |
| 13168 | ctxt->replaceEntities = ctx->replaceEntities; |
| 13169 | if (ctxt->validate) { |
| 13170 | ctxt->vctxt.error = ctx->vctxt.error; |
| 13171 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13172 | } else { |
| 13173 | ctxt->vctxt.error = NULL; |
| 13174 | ctxt->vctxt.warning = NULL; |
| 13175 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 13176 | ctxt->vctxt.nodeTab = NULL; |
| 13177 | ctxt->vctxt.nodeNr = 0; |
| 13178 | ctxt->vctxt.nodeMax = 0; |
| 13179 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13180 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13181 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13182 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13183 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13184 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13185 | ctxt->dictNames = ctx->dictNames; |
| 13186 | ctxt->attsDefault = ctx->attsDefault; |
| 13187 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 13188 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13189 | |
| 13190 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13191 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13192 | ctx->validate = ctxt->validate; |
| 13193 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13194 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13195 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13196 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13197 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13198 | } |
| 13199 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13200 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13201 | } |
| 13202 | |
| 13203 | if (!ctxt->wellFormed) { |
| 13204 | if (ctxt->errNo == 0) |
| 13205 | ret = 1; |
| 13206 | else |
| 13207 | ret = ctxt->errNo; |
| 13208 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13209 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13210 | xmlNodePtr cur; |
| 13211 | |
| 13212 | /* |
| 13213 | * Return the newly created nodeset after unlinking it from |
| 13214 | * they pseudo parent. |
| 13215 | */ |
| 13216 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13217 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13218 | while (cur != NULL) { |
| 13219 | cur->parent = NULL; |
| 13220 | cur = cur->next; |
| 13221 | } |
| 13222 | newDoc->children->children = NULL; |
| 13223 | } |
| 13224 | ret = 0; |
| 13225 | } |
| 13226 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13227 | ctxt->dict = NULL; |
| 13228 | ctxt->attsDefault = NULL; |
| 13229 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13230 | xmlFreeParserCtxt(ctxt); |
| 13231 | newDoc->intSubset = NULL; |
| 13232 | newDoc->extSubset = NULL; |
| 13233 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13234 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13235 | return(ret); |
| 13236 | } |
| 13237 | |
| 13238 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13239 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13240 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13241 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13242 | * @sax: the SAX handler bloc (possibly NULL) |
| 13243 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13244 | * @depth: Used for loop detection, use 0 |
| 13245 | * @URL: the URL for the entity to load |
| 13246 | * @ID: the System ID for the entity to load |
| 13247 | * @list: the return value for the set of parsed nodes |
| 13248 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13249 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13250 | * |
| 13251 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13252 | * the parser error code otherwise |
| 13253 | */ |
| 13254 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13255 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13256 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 13257 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13258 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13259 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13260 | xmlParserCtxtPtr ctxt; |
| 13261 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13262 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13263 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13264 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13265 | xmlChar start[4]; |
| 13266 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13267 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13268 | if (((depth > 40) && |
| 13269 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13270 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13271 | return(XML_ERR_ENTITY_LOOP); |
| 13272 | } |
| 13273 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13274 | if (list != NULL) |
| 13275 | *list = NULL; |
| 13276 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13277 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13278 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13279 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13280 | |
| 13281 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13282 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 13283 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13284 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13285 | if (oldctxt != NULL) { |
| 13286 | ctxt->_private = oldctxt->_private; |
| 13287 | ctxt->loadsubset = oldctxt->loadsubset; |
| 13288 | ctxt->validate = oldctxt->validate; |
| 13289 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13290 | ctxt->record_info = oldctxt->record_info; |
| 13291 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 13292 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 13293 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13294 | } else { |
| 13295 | /* |
| 13296 | * Doing validity checking on chunk without context |
| 13297 | * doesn't make sense |
| 13298 | */ |
| 13299 | ctxt->_private = NULL; |
| 13300 | ctxt->validate = 0; |
| 13301 | ctxt->external = 2; |
| 13302 | ctxt->loadsubset = 0; |
| 13303 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13304 | if (sax != NULL) { |
| 13305 | oldsax = ctxt->sax; |
| 13306 | ctxt->sax = sax; |
| 13307 | if (user_data != NULL) |
| 13308 | ctxt->userData = user_data; |
| 13309 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13310 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13311 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13312 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13313 | ctxt->node_seq.maximum = 0; |
| 13314 | ctxt->node_seq.length = 0; |
| 13315 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13316 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13317 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13318 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13319 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13320 | newDoc->intSubset = doc->intSubset; |
| 13321 | newDoc->extSubset = doc->extSubset; |
| 13322 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13323 | xmlDictReference(newDoc->dict); |
| 13324 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13325 | if (doc->URL != NULL) { |
| 13326 | newDoc->URL = xmlStrdup(doc->URL); |
| 13327 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13328 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13329 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13330 | if (sax != NULL) |
| 13331 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13332 | ctxt->node_seq.maximum = 0; |
| 13333 | ctxt->node_seq.length = 0; |
| 13334 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13335 | xmlFreeParserCtxt(ctxt); |
| 13336 | newDoc->intSubset = NULL; |
| 13337 | newDoc->extSubset = NULL; |
| 13338 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13339 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13340 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13341 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13342 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13343 | ctxt->myDoc = doc; |
| 13344 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13345 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13346 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13347 | * Get the 4 first bytes and decode the charset |
| 13348 | * if enc != XML_CHAR_ENCODING_NONE |
| 13349 | * plug some encoding conversion routines. |
| 13350 | */ |
| 13351 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13352 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13353 | start[0] = RAW; |
| 13354 | start[1] = NXT(1); |
| 13355 | start[2] = NXT(2); |
| 13356 | start[3] = NXT(3); |
| 13357 | enc = xmlDetectCharEncoding(start, 4); |
| 13358 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13359 | xmlSwitchEncoding(ctxt, enc); |
| 13360 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13361 | } |
| 13362 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13363 | /* |
| 13364 | * Parse a possible text declaration first |
| 13365 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13366 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13367 | xmlParseTextDecl(ctxt); |
| 13368 | } |
| 13369 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13370 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13371 | ctxt->depth = depth; |
| 13372 | |
| 13373 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13374 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13375 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13376 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13377 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13378 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13379 | } |
| 13380 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13381 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13382 | } |
| 13383 | |
| 13384 | if (!ctxt->wellFormed) { |
| 13385 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13386 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13387 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13388 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13389 | } else { |
| 13390 | if (list != NULL) { |
| 13391 | xmlNodePtr cur; |
| 13392 | |
| 13393 | /* |
| 13394 | * Return the newly created nodeset after unlinking it from |
| 13395 | * they pseudo parent. |
| 13396 | */ |
| 13397 | cur = newDoc->children->children; |
| 13398 | *list = cur; |
| 13399 | while (cur != NULL) { |
| 13400 | cur->parent = NULL; |
| 13401 | cur = cur->next; |
| 13402 | } |
| 13403 | newDoc->children->children = NULL; |
| 13404 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13405 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13406 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13407 | |
| 13408 | /* |
| 13409 | * Record in the parent context the number of entities replacement |
| 13410 | * done when parsing that reference. |
| 13411 | */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 13412 | if (oldctxt != NULL) |
| 13413 | oldctxt->nbentities += ctxt->nbentities; |
| 13414 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13415 | /* |
| 13416 | * Also record the size of the entity parsed |
| 13417 | */ |
Gaurav Gupta | cf77e60 | 2015-09-30 14:46:29 +0200 | [diff] [blame] | 13418 | if (ctxt->input != NULL && oldctxt != NULL) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13419 | oldctxt->sizeentities += ctxt->input->consumed; |
| 13420 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 13421 | } |
| 13422 | /* |
| 13423 | * And record the last error if any |
| 13424 | */ |
| 13425 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13426 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13427 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13428 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13429 | ctxt->sax = oldsax; |
Gaurav Gupta | cf77e60 | 2015-09-30 14:46:29 +0200 | [diff] [blame] | 13430 | if (oldctxt != NULL) { |
| 13431 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 13432 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 13433 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
| 13434 | } |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13435 | ctxt->node_seq.maximum = 0; |
| 13436 | ctxt->node_seq.length = 0; |
| 13437 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13438 | xmlFreeParserCtxt(ctxt); |
| 13439 | newDoc->intSubset = NULL; |
| 13440 | newDoc->extSubset = NULL; |
| 13441 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13442 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13443 | return(ret); |
| 13444 | } |
| 13445 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13446 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13447 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13448 | * xmlParseExternalEntity: |
| 13449 | * @doc: the document the chunk pertains to |
| 13450 | * @sax: the SAX handler bloc (possibly NULL) |
| 13451 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13452 | * @depth: Used for loop detection, use 0 |
| 13453 | * @URL: the URL for the entity to load |
| 13454 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13455 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13456 | * |
| 13457 | * Parse an external general entity |
| 13458 | * An external general parsed entity is well-formed if it matches the |
| 13459 | * production labeled extParsedEnt. |
| 13460 | * |
| 13461 | * [78] extParsedEnt ::= TextDecl? content |
| 13462 | * |
| 13463 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13464 | * the parser error code otherwise |
| 13465 | */ |
| 13466 | |
| 13467 | int |
| 13468 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13469 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13470 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13471 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13472 | } |
| 13473 | |
| 13474 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 13475 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13476 | * @doc: the document the chunk pertains to |
| 13477 | * @sax: the SAX handler bloc (possibly NULL) |
| 13478 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13479 | * @depth: Used for loop detection, use 0 |
| 13480 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13481 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13482 | * |
| 13483 | * Parse a well-balanced chunk of an XML document |
| 13484 | * called by the parser |
| 13485 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13486 | * the content production in the XML grammar: |
| 13487 | * |
| 13488 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13489 | * |
| 13490 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13491 | * the parser error code otherwise |
| 13492 | */ |
| 13493 | |
| 13494 | int |
| 13495 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13496 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13497 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 13498 | depth, string, lst, 0 ); |
| 13499 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13500 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13501 | |
| 13502 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13503 | * xmlParseBalancedChunkMemoryInternal: |
| 13504 | * @oldctxt: the existing parsing context |
| 13505 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13506 | * @user_data: the user data field for the parser context |
| 13507 | * @lst: the return value for the set of parsed nodes |
| 13508 | * |
| 13509 | * |
| 13510 | * Parse a well-balanced chunk of an XML document |
| 13511 | * called by the parser |
| 13512 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13513 | * the content production in the XML grammar: |
| 13514 | * |
| 13515 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13516 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13517 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13518 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13519 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13520 | * In case recover is set to 1, the nodelist will not be empty even if |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13521 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13522 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13523 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13524 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 13525 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 13526 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13527 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13528 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13529 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13530 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13531 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13532 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13533 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13534 | #ifdef SAX2 |
| 13535 | int i; |
| 13536 | #endif |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13537 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13538 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13539 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13540 | return(XML_ERR_ENTITY_LOOP); |
| 13541 | } |
| 13542 | |
| 13543 | |
| 13544 | if (lst != NULL) |
| 13545 | *lst = NULL; |
| 13546 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13547 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13548 | |
| 13549 | size = xmlStrlen(string); |
| 13550 | |
| 13551 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13552 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13553 | if (user_data != NULL) |
| 13554 | ctxt->userData = user_data; |
| 13555 | else |
| 13556 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13557 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13558 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13559 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13560 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13561 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13562 | |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13563 | #ifdef SAX2 |
| 13564 | /* propagate namespaces down the entity */ |
| 13565 | for (i = 0;i < oldctxt->nsNr;i += 2) { |
| 13566 | nsPush(ctxt, oldctxt->nsTab[i], oldctxt->nsTab[i+1]); |
| 13567 | } |
| 13568 | #endif |
| 13569 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13570 | oldsax = ctxt->sax; |
| 13571 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13572 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13573 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 13574 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13575 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 13576 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13577 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13578 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13579 | if (newDoc == NULL) { |
| 13580 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13581 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13582 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13583 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13584 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13585 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13586 | newDoc->dict = ctxt->dict; |
| 13587 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13588 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13589 | } else { |
| 13590 | ctxt->myDoc = oldctxt->myDoc; |
| 13591 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13592 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13593 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13594 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13595 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13596 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13597 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13598 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13599 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13600 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13601 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13602 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13603 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13604 | ctxt->myDoc->children = NULL; |
| 13605 | ctxt->myDoc->last = NULL; |
| 13606 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13607 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13608 | ctxt->instate = XML_PARSER_CONTENT; |
| 13609 | ctxt->depth = oldctxt->depth + 1; |
| 13610 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13611 | ctxt->validate = 0; |
| 13612 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 13613 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 13614 | /* |
| 13615 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13616 | */ |
| 13617 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13618 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13619 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13620 | ctxt->attsDefault = oldctxt->attsDefault; |
| 13621 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13622 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13623 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13624 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13625 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13626 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13627 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13628 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13629 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13630 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13631 | } |
| 13632 | |
| 13633 | if (!ctxt->wellFormed) { |
| 13634 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13635 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13636 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13637 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13638 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13639 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13640 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13641 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13642 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13643 | xmlNodePtr cur; |
| 13644 | |
| 13645 | /* |
| 13646 | * Return the newly created nodeset after unlinking it from |
| 13647 | * they pseudo parent. |
| 13648 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13649 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13650 | *lst = cur; |
| 13651 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13652 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 13653 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 13654 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 13655 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 13656 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 13657 | oldctxt->myDoc, cur); |
| 13658 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13659 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13660 | cur->parent = NULL; |
| 13661 | cur = cur->next; |
| 13662 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13663 | ctxt->myDoc->children->children = NULL; |
| 13664 | } |
| 13665 | if (ctxt->myDoc != NULL) { |
| 13666 | xmlFreeNode(ctxt->myDoc->children); |
| 13667 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13668 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13669 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13670 | |
| 13671 | /* |
| 13672 | * Record in the parent context the number of entities replacement |
| 13673 | * done when parsing that reference. |
| 13674 | */ |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 13675 | if (oldctxt != NULL) |
| 13676 | oldctxt->nbentities += ctxt->nbentities; |
| 13677 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13678 | /* |
| 13679 | * Also record the last error if any |
| 13680 | */ |
| 13681 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13682 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13683 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13684 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13685 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13686 | ctxt->attsDefault = NULL; |
| 13687 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13688 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13689 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13690 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13691 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13692 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13693 | return(ret); |
| 13694 | } |
| 13695 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13696 | /** |
| 13697 | * xmlParseInNodeContext: |
| 13698 | * @node: the context node |
| 13699 | * @data: the input string |
| 13700 | * @datalen: the input string length in bytes |
| 13701 | * @options: a combination of xmlParserOption |
| 13702 | * @lst: the return value for the set of parsed nodes |
| 13703 | * |
| 13704 | * Parse a well-balanced chunk of an XML document |
| 13705 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 13706 | * |
| 13707 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 13708 | * the content production in the XML grammar: |
| 13709 | * |
| 13710 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13711 | * |
| 13712 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13713 | * error code otherwise |
| 13714 | */ |
| 13715 | xmlParserErrors |
| 13716 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 13717 | int options, xmlNodePtr *lst) { |
| 13718 | #ifdef SAX2 |
| 13719 | xmlParserCtxtPtr ctxt; |
| 13720 | xmlDocPtr doc = NULL; |
| 13721 | xmlNodePtr fake, cur; |
| 13722 | int nsnr = 0; |
| 13723 | |
| 13724 | xmlParserErrors ret = XML_ERR_OK; |
| 13725 | |
| 13726 | /* |
| 13727 | * check all input parameters, grab the document |
| 13728 | */ |
| 13729 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 13730 | return(XML_ERR_INTERNAL_ERROR); |
| 13731 | switch (node->type) { |
| 13732 | case XML_ELEMENT_NODE: |
| 13733 | case XML_ATTRIBUTE_NODE: |
| 13734 | case XML_TEXT_NODE: |
| 13735 | case XML_CDATA_SECTION_NODE: |
| 13736 | case XML_ENTITY_REF_NODE: |
| 13737 | case XML_PI_NODE: |
| 13738 | case XML_COMMENT_NODE: |
| 13739 | case XML_DOCUMENT_NODE: |
| 13740 | case XML_HTML_DOCUMENT_NODE: |
| 13741 | break; |
| 13742 | default: |
| 13743 | return(XML_ERR_INTERNAL_ERROR); |
| 13744 | |
| 13745 | } |
| 13746 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 13747 | (node->type != XML_DOCUMENT_NODE) && |
| 13748 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 13749 | node = node->parent; |
| 13750 | if (node == NULL) |
| 13751 | return(XML_ERR_INTERNAL_ERROR); |
| 13752 | if (node->type == XML_ELEMENT_NODE) |
| 13753 | doc = node->doc; |
| 13754 | else |
| 13755 | doc = (xmlDocPtr) node; |
| 13756 | if (doc == NULL) |
| 13757 | return(XML_ERR_INTERNAL_ERROR); |
| 13758 | |
| 13759 | /* |
| 13760 | * allocate a context and set-up everything not related to the |
| 13761 | * node position in the tree |
| 13762 | */ |
| 13763 | if (doc->type == XML_DOCUMENT_NODE) |
| 13764 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 13765 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13766 | else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13767 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13768 | /* |
| 13769 | * When parsing in context, it makes no sense to add implied |
| 13770 | * elements like html/body/etc... |
| 13771 | */ |
| 13772 | options |= HTML_PARSE_NOIMPLIED; |
| 13773 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13774 | #endif |
| 13775 | else |
| 13776 | return(XML_ERR_INTERNAL_ERROR); |
| 13777 | |
| 13778 | if (ctxt == NULL) |
| 13779 | return(XML_ERR_NO_MEMORY); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13780 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13781 | /* |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13782 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 13783 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 13784 | * we must wait until the last moment to free the original one. |
| 13785 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13786 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13787 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13788 | xmlDictFree(ctxt->dict); |
| 13789 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13790 | } else |
| 13791 | options |= XML_PARSE_NODICT; |
| 13792 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13793 | if (doc->encoding != NULL) { |
| 13794 | xmlCharEncodingHandlerPtr hdlr; |
| 13795 | |
| 13796 | if (ctxt->encoding != NULL) |
| 13797 | xmlFree((xmlChar *) ctxt->encoding); |
| 13798 | ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); |
| 13799 | |
Nikolay Sivov | d4a5d98 | 2013-04-30 17:45:36 +0400 | [diff] [blame] | 13800 | hdlr = xmlFindCharEncodingHandler((const char *) doc->encoding); |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13801 | if (hdlr != NULL) { |
| 13802 | xmlSwitchToEncoding(ctxt, hdlr); |
| 13803 | } else { |
| 13804 | return(XML_ERR_UNSUPPORTED_ENCODING); |
| 13805 | } |
| 13806 | } |
| 13807 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13808 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13809 | xmlDetectSAX2(ctxt); |
| 13810 | ctxt->myDoc = doc; |
Daniel Veillard | 6faa126 | 2014-03-21 17:05:51 +0800 | [diff] [blame] | 13811 | /* parsing in context, i.e. as within existing content */ |
| 13812 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13813 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13814 | fake = xmlNewComment(NULL); |
| 13815 | if (fake == NULL) { |
| 13816 | xmlFreeParserCtxt(ctxt); |
| 13817 | return(XML_ERR_NO_MEMORY); |
| 13818 | } |
| 13819 | xmlAddChild(node, fake); |
| 13820 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13821 | if (node->type == XML_ELEMENT_NODE) { |
| 13822 | nodePush(ctxt, node); |
| 13823 | /* |
| 13824 | * initialize the SAX2 namespaces stack |
| 13825 | */ |
| 13826 | cur = node; |
| 13827 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 13828 | xmlNsPtr ns = cur->nsDef; |
| 13829 | const xmlChar *iprefix, *ihref; |
| 13830 | |
| 13831 | while (ns != NULL) { |
| 13832 | if (ctxt->dict) { |
| 13833 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 13834 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 13835 | } else { |
| 13836 | iprefix = ns->prefix; |
| 13837 | ihref = ns->href; |
| 13838 | } |
| 13839 | |
| 13840 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 13841 | nsPush(ctxt, iprefix, ihref); |
| 13842 | nsnr++; |
| 13843 | } |
| 13844 | ns = ns->next; |
| 13845 | } |
| 13846 | cur = cur->parent; |
| 13847 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13848 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13849 | |
| 13850 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 13851 | /* |
| 13852 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13853 | */ |
| 13854 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13855 | } |
| 13856 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 13857 | #ifdef LIBXML_HTML_ENABLED |
| 13858 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 13859 | __htmlParseContent(ctxt); |
| 13860 | else |
| 13861 | #endif |
| 13862 | xmlParseContent(ctxt); |
| 13863 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13864 | nsPop(ctxt, nsnr); |
| 13865 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 13866 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13867 | } else if (RAW != 0) { |
| 13868 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 13869 | } |
| 13870 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 13871 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13872 | ctxt->wellFormed = 0; |
| 13873 | } |
| 13874 | |
| 13875 | if (!ctxt->wellFormed) { |
| 13876 | if (ctxt->errNo == 0) |
| 13877 | ret = XML_ERR_INTERNAL_ERROR; |
| 13878 | else |
| 13879 | ret = (xmlParserErrors)ctxt->errNo; |
| 13880 | } else { |
| 13881 | ret = XML_ERR_OK; |
| 13882 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13883 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13884 | /* |
| 13885 | * Return the newly created nodeset after unlinking it from |
| 13886 | * the pseudo sibling. |
| 13887 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13888 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13889 | cur = fake->next; |
| 13890 | fake->next = NULL; |
| 13891 | node->last = fake; |
| 13892 | |
| 13893 | if (cur != NULL) { |
| 13894 | cur->prev = NULL; |
| 13895 | } |
| 13896 | |
| 13897 | *lst = cur; |
| 13898 | |
| 13899 | while (cur != NULL) { |
| 13900 | cur->parent = NULL; |
| 13901 | cur = cur->next; |
| 13902 | } |
| 13903 | |
| 13904 | xmlUnlinkNode(fake); |
| 13905 | xmlFreeNode(fake); |
| 13906 | |
| 13907 | |
| 13908 | if (ret != XML_ERR_OK) { |
| 13909 | xmlFreeNodeList(*lst); |
| 13910 | *lst = NULL; |
| 13911 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13912 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 13913 | if (doc->dict != NULL) |
| 13914 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13915 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13916 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13917 | return(ret); |
| 13918 | #else /* !SAX2 */ |
| 13919 | return(XML_ERR_INTERNAL_ERROR); |
| 13920 | #endif |
| 13921 | } |
| 13922 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13923 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13924 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13925 | * xmlParseBalancedChunkMemoryRecover: |
| 13926 | * @doc: the document the chunk pertains to |
| 13927 | * @sax: the SAX handler bloc (possibly NULL) |
| 13928 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13929 | * @depth: Used for loop detection, use 0 |
| 13930 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13931 | * @lst: the return value for the set of parsed nodes |
| 13932 | * @recover: return nodes even if the data is broken (use 0) |
| 13933 | * |
| 13934 | * |
| 13935 | * Parse a well-balanced chunk of an XML document |
| 13936 | * called by the parser |
| 13937 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13938 | * the content production in the XML grammar: |
| 13939 | * |
| 13940 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13941 | * |
| 13942 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13943 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13944 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13945 | * In case recover is set to 1, the nodelist will not be empty even if |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13946 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 13947 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13948 | */ |
| 13949 | int |
| 13950 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13951 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13952 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13953 | xmlParserCtxtPtr ctxt; |
| 13954 | xmlDocPtr newDoc; |
| 13955 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13956 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13957 | int size; |
| 13958 | int ret = 0; |
| 13959 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13960 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13961 | return(XML_ERR_ENTITY_LOOP); |
| 13962 | } |
| 13963 | |
| 13964 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13965 | if (lst != NULL) |
| 13966 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13967 | if (string == NULL) |
| 13968 | return(-1); |
| 13969 | |
| 13970 | size = xmlStrlen(string); |
| 13971 | |
| 13972 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 13973 | if (ctxt == NULL) return(-1); |
| 13974 | ctxt->userData = ctxt; |
| 13975 | if (sax != NULL) { |
| 13976 | oldsax = ctxt->sax; |
| 13977 | ctxt->sax = sax; |
| 13978 | if (user_data != NULL) |
| 13979 | ctxt->userData = user_data; |
| 13980 | } |
| 13981 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13982 | if (newDoc == NULL) { |
| 13983 | xmlFreeParserCtxt(ctxt); |
| 13984 | return(-1); |
| 13985 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13986 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13987 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 13988 | xmlDictFree(ctxt->dict); |
| 13989 | ctxt->dict = doc->dict; |
| 13990 | xmlDictReference(ctxt->dict); |
| 13991 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13992 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13993 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 13994 | ctxt->dictNames = 1; |
| 13995 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13996 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13997 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13998 | if (doc != NULL) { |
| 13999 | newDoc->intSubset = doc->intSubset; |
| 14000 | newDoc->extSubset = doc->extSubset; |
| 14001 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 14002 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 14003 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14004 | if (sax != NULL) |
| 14005 | ctxt->sax = oldsax; |
| 14006 | xmlFreeParserCtxt(ctxt); |
| 14007 | newDoc->intSubset = NULL; |
| 14008 | newDoc->extSubset = NULL; |
| 14009 | xmlFreeDoc(newDoc); |
| 14010 | return(-1); |
| 14011 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 14012 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 14013 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14014 | if (doc == NULL) { |
| 14015 | ctxt->myDoc = newDoc; |
| 14016 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 14017 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14018 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 14019 | /* Ensure that doc has XML spec namespace */ |
| 14020 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 14021 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14022 | } |
| 14023 | ctxt->instate = XML_PARSER_CONTENT; |
| 14024 | ctxt->depth = depth; |
| 14025 | |
| 14026 | /* |
| 14027 | * Doing validity checking on chunk doesn't make sense |
| 14028 | */ |
| 14029 | ctxt->validate = 0; |
| 14030 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14031 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14032 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 14033 | if ( doc != NULL ){ |
| 14034 | content = doc->children; |
| 14035 | doc->children = NULL; |
| 14036 | xmlParseContent(ctxt); |
| 14037 | doc->children = content; |
| 14038 | } |
| 14039 | else { |
| 14040 | xmlParseContent(ctxt); |
| 14041 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14042 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 14043 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14044 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 14045 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14046 | } |
| 14047 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 14048 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14049 | } |
| 14050 | |
| 14051 | if (!ctxt->wellFormed) { |
| 14052 | if (ctxt->errNo == 0) |
| 14053 | ret = 1; |
| 14054 | else |
| 14055 | ret = ctxt->errNo; |
| 14056 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 14057 | ret = 0; |
| 14058 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14059 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 14060 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 14061 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14062 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 14063 | /* |
| 14064 | * Return the newly created nodeset after unlinking it from |
| 14065 | * they pseudo parent. |
| 14066 | */ |
| 14067 | cur = newDoc->children->children; |
| 14068 | *lst = cur; |
| 14069 | while (cur != NULL) { |
| 14070 | xmlSetTreeDoc(cur, doc); |
| 14071 | cur->parent = NULL; |
| 14072 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14073 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 14074 | newDoc->children->children = NULL; |
| 14075 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14076 | |
| 14077 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14078 | ctxt->sax = oldsax; |
| 14079 | xmlFreeParserCtxt(ctxt); |
| 14080 | newDoc->intSubset = NULL; |
| 14081 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 14082 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14083 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14084 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14085 | return(ret); |
| 14086 | } |
| 14087 | |
| 14088 | /** |
| 14089 | * xmlSAXParseEntity: |
| 14090 | * @sax: the SAX handler block |
| 14091 | * @filename: the filename |
| 14092 | * |
| 14093 | * parse an XML external entity out of context and build a tree. |
| 14094 | * It use the given SAX function block to handle the parsing callback. |
| 14095 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14096 | * |
| 14097 | * [78] extParsedEnt ::= TextDecl? content |
| 14098 | * |
| 14099 | * This correspond to a "Well Balanced" chunk |
| 14100 | * |
| 14101 | * Returns the resulting document tree |
| 14102 | */ |
| 14103 | |
| 14104 | xmlDocPtr |
| 14105 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 14106 | xmlDocPtr ret; |
| 14107 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14108 | |
| 14109 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14110 | if (ctxt == NULL) { |
| 14111 | return(NULL); |
| 14112 | } |
| 14113 | if (sax != NULL) { |
| 14114 | if (ctxt->sax != NULL) |
| 14115 | xmlFree(ctxt->sax); |
| 14116 | ctxt->sax = sax; |
| 14117 | ctxt->userData = NULL; |
| 14118 | } |
| 14119 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14120 | xmlParseExtParsedEnt(ctxt); |
| 14121 | |
| 14122 | if (ctxt->wellFormed) |
| 14123 | ret = ctxt->myDoc; |
| 14124 | else { |
| 14125 | ret = NULL; |
| 14126 | xmlFreeDoc(ctxt->myDoc); |
| 14127 | ctxt->myDoc = NULL; |
| 14128 | } |
| 14129 | if (sax != NULL) |
| 14130 | ctxt->sax = NULL; |
| 14131 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14132 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14133 | return(ret); |
| 14134 | } |
| 14135 | |
| 14136 | /** |
| 14137 | * xmlParseEntity: |
| 14138 | * @filename: the filename |
| 14139 | * |
| 14140 | * parse an XML external entity out of context and build a tree. |
| 14141 | * |
| 14142 | * [78] extParsedEnt ::= TextDecl? content |
| 14143 | * |
| 14144 | * This correspond to a "Well Balanced" chunk |
| 14145 | * |
| 14146 | * Returns the resulting document tree |
| 14147 | */ |
| 14148 | |
| 14149 | xmlDocPtr |
| 14150 | xmlParseEntity(const char *filename) { |
| 14151 | return(xmlSAXParseEntity(NULL, filename)); |
| 14152 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14153 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14154 | |
| 14155 | /** |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14156 | * xmlCreateEntityParserCtxtInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14157 | * @URL: the entity URL |
| 14158 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14159 | * @base: a possible base for the target URI |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14160 | * @pctx: parser context used to set options on new context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14161 | * |
| 14162 | * Create a parser context for an external entity |
| 14163 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14164 | * by default if found at compile-time. |
| 14165 | * |
| 14166 | * Returns the new parser context or NULL |
| 14167 | */ |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14168 | static xmlParserCtxtPtr |
| 14169 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 14170 | const xmlChar *base, xmlParserCtxtPtr pctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14171 | xmlParserCtxtPtr ctxt; |
| 14172 | xmlParserInputPtr inputStream; |
| 14173 | char *directory = NULL; |
| 14174 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14175 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14176 | ctxt = xmlNewParserCtxt(); |
| 14177 | if (ctxt == NULL) { |
| 14178 | return(NULL); |
| 14179 | } |
| 14180 | |
Daniel Veillard | 48247b4 | 2009-07-10 16:12:46 +0200 | [diff] [blame] | 14181 | if (pctx != NULL) { |
| 14182 | ctxt->options = pctx->options; |
| 14183 | ctxt->_private = pctx->_private; |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14184 | } |
| 14185 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14186 | uri = xmlBuildURI(URL, base); |
| 14187 | |
| 14188 | if (uri == NULL) { |
| 14189 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 14190 | if (inputStream == NULL) { |
| 14191 | xmlFreeParserCtxt(ctxt); |
| 14192 | return(NULL); |
| 14193 | } |
| 14194 | |
| 14195 | inputPush(ctxt, inputStream); |
| 14196 | |
| 14197 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14198 | directory = xmlParserGetDirectory((char *)URL); |
| 14199 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14200 | ctxt->directory = directory; |
| 14201 | } else { |
| 14202 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 14203 | if (inputStream == NULL) { |
| 14204 | xmlFree(uri); |
| 14205 | xmlFreeParserCtxt(ctxt); |
| 14206 | return(NULL); |
| 14207 | } |
| 14208 | |
| 14209 | inputPush(ctxt, inputStream); |
| 14210 | |
| 14211 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14212 | directory = xmlParserGetDirectory((char *)uri); |
| 14213 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14214 | ctxt->directory = directory; |
| 14215 | xmlFree(uri); |
| 14216 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14217 | return(ctxt); |
| 14218 | } |
| 14219 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14220 | /** |
| 14221 | * xmlCreateEntityParserCtxt: |
| 14222 | * @URL: the entity URL |
| 14223 | * @ID: the entity PUBLIC ID |
| 14224 | * @base: a possible base for the target URI |
| 14225 | * |
| 14226 | * Create a parser context for an external entity |
| 14227 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14228 | * by default if found at compile-time. |
| 14229 | * |
| 14230 | * Returns the new parser context or NULL |
| 14231 | */ |
| 14232 | xmlParserCtxtPtr |
| 14233 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 14234 | const xmlChar *base) { |
| 14235 | return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); |
| 14236 | |
| 14237 | } |
| 14238 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14239 | /************************************************************************ |
| 14240 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14241 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14242 | * * |
| 14243 | ************************************************************************/ |
| 14244 | |
| 14245 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14246 | * xmlCreateURLParserCtxt: |
| 14247 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14248 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14249 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14250 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14251 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14252 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14253 | * |
| 14254 | * Returns the new parser context or NULL |
| 14255 | */ |
| 14256 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14257 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14258 | { |
| 14259 | xmlParserCtxtPtr ctxt; |
| 14260 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14261 | char *directory = NULL; |
| 14262 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14263 | ctxt = xmlNewParserCtxt(); |
| 14264 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14265 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14266 | return(NULL); |
| 14267 | } |
| 14268 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14269 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14270 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14271 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14272 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 14273 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14274 | if (inputStream == NULL) { |
| 14275 | xmlFreeParserCtxt(ctxt); |
| 14276 | return(NULL); |
| 14277 | } |
| 14278 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14279 | inputPush(ctxt, inputStream); |
| 14280 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 14281 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14282 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14283 | ctxt->directory = directory; |
| 14284 | |
| 14285 | return(ctxt); |
| 14286 | } |
| 14287 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14288 | /** |
| 14289 | * xmlCreateFileParserCtxt: |
| 14290 | * @filename: the filename |
| 14291 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14292 | * Create a parser context for a file content. |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14293 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14294 | * by default if found at compile-time. |
| 14295 | * |
| 14296 | * Returns the new parser context or NULL |
| 14297 | */ |
| 14298 | xmlParserCtxtPtr |
| 14299 | xmlCreateFileParserCtxt(const char *filename) |
| 14300 | { |
| 14301 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 14302 | } |
| 14303 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14304 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14305 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14306 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14307 | * @sax: the SAX handler block |
| 14308 | * @filename: the filename |
| 14309 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14310 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14311 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14312 | * |
| 14313 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14314 | * compressed document is provided by default if found at compile-time. |
| 14315 | * It use the given SAX function block to handle the parsing callback. |
| 14316 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14317 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 14318 | * User data (void *) is stored within the parser context in the |
| 14319 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14320 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14321 | * Returns the resulting document tree |
| 14322 | */ |
| 14323 | |
| 14324 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14325 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 14326 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14327 | xmlDocPtr ret; |
| 14328 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14329 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 14330 | xmlInitParser(); |
| 14331 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14332 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14333 | if (ctxt == NULL) { |
| 14334 | return(NULL); |
| 14335 | } |
| 14336 | if (sax != NULL) { |
| 14337 | if (ctxt->sax != NULL) |
| 14338 | xmlFree(ctxt->sax); |
| 14339 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14340 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14341 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14342 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 14343 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14344 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14345 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 14346 | if (ctxt->directory == NULL) |
| 14347 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14348 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 14349 | ctxt->recovery = recovery; |
| 14350 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14351 | xmlParseDocument(ctxt); |
| 14352 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14353 | if ((ctxt->wellFormed) || recovery) { |
| 14354 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 14355 | if (ret != NULL) { |
| 14356 | if (ctxt->input->buf->compressed > 0) |
| 14357 | ret->compression = 9; |
| 14358 | else |
| 14359 | ret->compression = ctxt->input->buf->compressed; |
| 14360 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14361 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14362 | else { |
| 14363 | ret = NULL; |
| 14364 | xmlFreeDoc(ctxt->myDoc); |
| 14365 | ctxt->myDoc = NULL; |
| 14366 | } |
| 14367 | if (sax != NULL) |
| 14368 | ctxt->sax = NULL; |
| 14369 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14370 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14371 | return(ret); |
| 14372 | } |
| 14373 | |
| 14374 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14375 | * xmlSAXParseFile: |
| 14376 | * @sax: the SAX handler block |
| 14377 | * @filename: the filename |
| 14378 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14379 | * documents |
| 14380 | * |
| 14381 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14382 | * compressed document is provided by default if found at compile-time. |
| 14383 | * It use the given SAX function block to handle the parsing callback. |
| 14384 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14385 | * |
| 14386 | * Returns the resulting document tree |
| 14387 | */ |
| 14388 | |
| 14389 | xmlDocPtr |
| 14390 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 14391 | int recovery) { |
| 14392 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 14393 | } |
| 14394 | |
| 14395 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14396 | * xmlRecoverDoc: |
| 14397 | * @cur: a pointer to an array of xmlChar |
| 14398 | * |
| 14399 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14400 | * In the case the document is not Well Formed, a attempt to build a |
| 14401 | * tree is tried anyway |
| 14402 | * |
| 14403 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14404 | */ |
| 14405 | |
| 14406 | xmlDocPtr |
Daniel Veillard | f39eafa | 2009-08-20 19:15:08 +0200 | [diff] [blame] | 14407 | xmlRecoverDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14408 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 14409 | } |
| 14410 | |
| 14411 | /** |
| 14412 | * xmlParseFile: |
| 14413 | * @filename: the filename |
| 14414 | * |
| 14415 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14416 | * compressed document is provided by default if found at compile-time. |
| 14417 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 14418 | * Returns the resulting document tree if the file was wellformed, |
| 14419 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14420 | */ |
| 14421 | |
| 14422 | xmlDocPtr |
| 14423 | xmlParseFile(const char *filename) { |
| 14424 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 14425 | } |
| 14426 | |
| 14427 | /** |
| 14428 | * xmlRecoverFile: |
| 14429 | * @filename: the filename |
| 14430 | * |
| 14431 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14432 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14433 | * In the case the document is not Well Formed, it attempts to build |
| 14434 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14435 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14436 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14437 | */ |
| 14438 | |
| 14439 | xmlDocPtr |
| 14440 | xmlRecoverFile(const char *filename) { |
| 14441 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 14442 | } |
| 14443 | |
| 14444 | |
| 14445 | /** |
| 14446 | * xmlSetupParserForBuffer: |
| 14447 | * @ctxt: an XML parser context |
| 14448 | * @buffer: a xmlChar * buffer |
| 14449 | * @filename: a file name |
| 14450 | * |
| 14451 | * Setup the parser context to parse a new buffer; Clears any prior |
| 14452 | * contents from the parser context. The buffer parameter must not be |
| 14453 | * NULL, but the filename parameter can be |
| 14454 | */ |
| 14455 | void |
| 14456 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 14457 | const char* filename) |
| 14458 | { |
| 14459 | xmlParserInputPtr input; |
| 14460 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14461 | if ((ctxt == NULL) || (buffer == NULL)) |
| 14462 | return; |
| 14463 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14464 | input = xmlNewInputStream(ctxt); |
| 14465 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 14466 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14467 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14468 | return; |
| 14469 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14470 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14471 | xmlClearParserCtxt(ctxt); |
| 14472 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 14473 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14474 | input->base = buffer; |
| 14475 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 14476 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14477 | inputPush(ctxt, input); |
| 14478 | } |
| 14479 | |
| 14480 | /** |
| 14481 | * xmlSAXUserParseFile: |
| 14482 | * @sax: a SAX handler |
| 14483 | * @user_data: The user data returned on SAX callbacks |
| 14484 | * @filename: a file name |
| 14485 | * |
| 14486 | * parse an XML file and call the given SAX handler routines. |
| 14487 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14488 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14489 | * Returns 0 in case of success or a error number otherwise |
| 14490 | */ |
| 14491 | int |
| 14492 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 14493 | const char *filename) { |
| 14494 | int ret = 0; |
| 14495 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14496 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14497 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14498 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 14499 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14500 | xmlFree(ctxt->sax); |
| 14501 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14502 | xmlDetectSAX2(ctxt); |
| 14503 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14504 | if (user_data != NULL) |
| 14505 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14506 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14507 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14508 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14509 | if (ctxt->wellFormed) |
| 14510 | ret = 0; |
| 14511 | else { |
| 14512 | if (ctxt->errNo != 0) |
| 14513 | ret = ctxt->errNo; |
| 14514 | else |
| 14515 | ret = -1; |
| 14516 | } |
| 14517 | if (sax != NULL) |
| 14518 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14519 | if (ctxt->myDoc != NULL) { |
| 14520 | xmlFreeDoc(ctxt->myDoc); |
| 14521 | ctxt->myDoc = NULL; |
| 14522 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14523 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14524 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14525 | return ret; |
| 14526 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14527 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14528 | |
| 14529 | /************************************************************************ |
| 14530 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14531 | * Front ends when parsing from memory * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14532 | * * |
| 14533 | ************************************************************************/ |
| 14534 | |
| 14535 | /** |
| 14536 | * xmlCreateMemoryParserCtxt: |
| 14537 | * @buffer: a pointer to a char array |
| 14538 | * @size: the size of the array |
| 14539 | * |
| 14540 | * Create a parser context for an XML in-memory document. |
| 14541 | * |
| 14542 | * Returns the new parser context or NULL |
| 14543 | */ |
| 14544 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14545 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14546 | xmlParserCtxtPtr ctxt; |
| 14547 | xmlParserInputPtr input; |
| 14548 | xmlParserInputBufferPtr buf; |
| 14549 | |
| 14550 | if (buffer == NULL) |
| 14551 | return(NULL); |
| 14552 | if (size <= 0) |
| 14553 | return(NULL); |
| 14554 | |
| 14555 | ctxt = xmlNewParserCtxt(); |
| 14556 | if (ctxt == NULL) |
| 14557 | return(NULL); |
| 14558 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 14559 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14560 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14561 | if (buf == NULL) { |
| 14562 | xmlFreeParserCtxt(ctxt); |
| 14563 | return(NULL); |
| 14564 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14565 | |
| 14566 | input = xmlNewInputStream(ctxt); |
| 14567 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14568 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14569 | xmlFreeParserCtxt(ctxt); |
| 14570 | return(NULL); |
| 14571 | } |
| 14572 | |
| 14573 | input->filename = NULL; |
| 14574 | input->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14575 | xmlBufResetInput(input->buf->buffer, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14576 | |
| 14577 | inputPush(ctxt, input); |
| 14578 | return(ctxt); |
| 14579 | } |
| 14580 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14581 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14582 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14583 | * xmlSAXParseMemoryWithData: |
| 14584 | * @sax: the SAX handler block |
| 14585 | * @buffer: an pointer to a char array |
| 14586 | * @size: the size of the array |
| 14587 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14588 | * documents |
| 14589 | * @data: the userdata |
| 14590 | * |
| 14591 | * parse an XML in-memory block and use the given SAX function block |
| 14592 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14593 | * DOM tree building routines. |
| 14594 | * |
| 14595 | * User data (void *) is stored within the parser context in the |
| 14596 | * context's _private member, so it is available nearly everywhere in libxml |
| 14597 | * |
| 14598 | * Returns the resulting document tree |
| 14599 | */ |
| 14600 | |
| 14601 | xmlDocPtr |
| 14602 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 14603 | int size, int recovery, void *data) { |
| 14604 | xmlDocPtr ret; |
| 14605 | xmlParserCtxtPtr ctxt; |
| 14606 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14607 | xmlInitParser(); |
| 14608 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14609 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14610 | if (ctxt == NULL) return(NULL); |
| 14611 | if (sax != NULL) { |
| 14612 | if (ctxt->sax != NULL) |
| 14613 | xmlFree(ctxt->sax); |
| 14614 | ctxt->sax = sax; |
| 14615 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14616 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14617 | if (data!=NULL) { |
| 14618 | ctxt->_private=data; |
| 14619 | } |
| 14620 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 14621 | ctxt->recovery = recovery; |
| 14622 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14623 | xmlParseDocument(ctxt); |
| 14624 | |
| 14625 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14626 | else { |
| 14627 | ret = NULL; |
| 14628 | xmlFreeDoc(ctxt->myDoc); |
| 14629 | ctxt->myDoc = NULL; |
| 14630 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14631 | if (sax != NULL) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14632 | ctxt->sax = NULL; |
| 14633 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14634 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14635 | return(ret); |
| 14636 | } |
| 14637 | |
| 14638 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14639 | * xmlSAXParseMemory: |
| 14640 | * @sax: the SAX handler block |
| 14641 | * @buffer: an pointer to a char array |
| 14642 | * @size: the size of the array |
| 14643 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 14644 | * documents |
| 14645 | * |
| 14646 | * parse an XML in-memory block and use the given SAX function block |
| 14647 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14648 | * DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14649 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14650 | * Returns the resulting document tree |
| 14651 | */ |
| 14652 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14653 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 14654 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14655 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14656 | } |
| 14657 | |
| 14658 | /** |
| 14659 | * xmlParseMemory: |
| 14660 | * @buffer: an pointer to a char array |
| 14661 | * @size: the size of the array |
| 14662 | * |
| 14663 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14664 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14665 | * Returns the resulting document tree |
| 14666 | */ |
| 14667 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14668 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14669 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 14670 | } |
| 14671 | |
| 14672 | /** |
| 14673 | * xmlRecoverMemory: |
| 14674 | * @buffer: an pointer to a char array |
| 14675 | * @size: the size of the array |
| 14676 | * |
| 14677 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14678 | * In the case the document is not Well Formed, an attempt to |
| 14679 | * build a tree is tried anyway |
| 14680 | * |
| 14681 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14682 | */ |
| 14683 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14684 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14685 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 14686 | } |
| 14687 | |
| 14688 | /** |
| 14689 | * xmlSAXUserParseMemory: |
| 14690 | * @sax: a SAX handler |
| 14691 | * @user_data: The user data returned on SAX callbacks |
| 14692 | * @buffer: an in-memory XML document input |
| 14693 | * @size: the length of the XML document in bytes |
| 14694 | * |
| 14695 | * A better SAX parsing routine. |
| 14696 | * parse an XML in-memory buffer and call the given SAX handler routines. |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14697 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14698 | * Returns 0 in case of success or a error number otherwise |
| 14699 | */ |
| 14700 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14701 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14702 | int ret = 0; |
| 14703 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14704 | |
| 14705 | xmlInitParser(); |
| 14706 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14707 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14708 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14709 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 14710 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 14711 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14712 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14713 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 14714 | if (user_data != NULL) |
| 14715 | ctxt->userData = user_data; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14716 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14717 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14718 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14719 | if (ctxt->wellFormed) |
| 14720 | ret = 0; |
| 14721 | else { |
| 14722 | if (ctxt->errNo != 0) |
| 14723 | ret = ctxt->errNo; |
| 14724 | else |
| 14725 | ret = -1; |
| 14726 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14727 | if (sax != NULL) |
| 14728 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14729 | if (ctxt->myDoc != NULL) { |
| 14730 | xmlFreeDoc(ctxt->myDoc); |
| 14731 | ctxt->myDoc = NULL; |
| 14732 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14733 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14734 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14735 | return ret; |
| 14736 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14737 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14738 | |
| 14739 | /** |
| 14740 | * xmlCreateDocParserCtxt: |
| 14741 | * @cur: a pointer to an array of xmlChar |
| 14742 | * |
| 14743 | * Creates a parser context for an XML in-memory document. |
| 14744 | * |
| 14745 | * Returns the new parser context or NULL |
| 14746 | */ |
| 14747 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14748 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14749 | int len; |
| 14750 | |
| 14751 | if (cur == NULL) |
| 14752 | return(NULL); |
| 14753 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14754 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14755 | } |
| 14756 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14757 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14758 | /** |
| 14759 | * xmlSAXParseDoc: |
| 14760 | * @sax: the SAX handler block |
| 14761 | * @cur: a pointer to an array of xmlChar |
| 14762 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14763 | * documents |
| 14764 | * |
| 14765 | * parse an XML in-memory document and build a tree. |
| 14766 | * It use the given SAX function block to handle the parsing callback. |
| 14767 | * If sax is NULL, fallback to the default DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14768 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14769 | * Returns the resulting document tree |
| 14770 | */ |
| 14771 | |
| 14772 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14773 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14774 | xmlDocPtr ret; |
| 14775 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14776 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14777 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14778 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14779 | |
| 14780 | |
| 14781 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14782 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14783 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14784 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14785 | ctxt->sax = sax; |
| 14786 | ctxt->userData = NULL; |
| 14787 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14788 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14789 | |
| 14790 | xmlParseDocument(ctxt); |
| 14791 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14792 | else { |
| 14793 | ret = NULL; |
| 14794 | xmlFreeDoc(ctxt->myDoc); |
| 14795 | ctxt->myDoc = NULL; |
| 14796 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14797 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14798 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14799 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14800 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14801 | return(ret); |
| 14802 | } |
| 14803 | |
| 14804 | /** |
| 14805 | * xmlParseDoc: |
| 14806 | * @cur: a pointer to an array of xmlChar |
| 14807 | * |
| 14808 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14809 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14810 | * Returns the resulting document tree |
| 14811 | */ |
| 14812 | |
| 14813 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14814 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14815 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 14816 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14817 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14818 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14819 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14820 | /************************************************************************ |
| 14821 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14822 | * Specific function to keep track of entities references * |
| 14823 | * and used by the XSLT debugger * |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14824 | * * |
| 14825 | ************************************************************************/ |
| 14826 | |
| 14827 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 14828 | |
| 14829 | /** |
| 14830 | * xmlAddEntityReference: |
| 14831 | * @ent : A valid entity |
| 14832 | * @firstNode : A valid first node for children of entity |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14833 | * @lastNode : A valid last node of children entity |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14834 | * |
| 14835 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 14836 | */ |
| 14837 | static void |
| 14838 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 14839 | xmlNodePtr lastNode) |
| 14840 | { |
| 14841 | if (xmlEntityRefFunc != NULL) { |
| 14842 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 14843 | } |
| 14844 | } |
| 14845 | |
| 14846 | |
| 14847 | /** |
| 14848 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 14849 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14850 | * |
| 14851 | * Set the function to call call back when a xml reference has been made |
| 14852 | */ |
| 14853 | void |
| 14854 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 14855 | { |
| 14856 | xmlEntityRefFunc = func; |
| 14857 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14858 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14859 | |
| 14860 | /************************************************************************ |
| 14861 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14862 | * Miscellaneous * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14863 | * * |
| 14864 | ************************************************************************/ |
| 14865 | |
| 14866 | #ifdef LIBXML_XPATH_ENABLED |
| 14867 | #include <libxml/xpath.h> |
| 14868 | #endif |
| 14869 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 14870 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14871 | static int xmlParserInitialized = 0; |
| 14872 | |
| 14873 | /** |
| 14874 | * xmlInitParser: |
| 14875 | * |
| 14876 | * Initialization function for the XML parser. |
| 14877 | * This is not reentrant. Call once before processing in case of |
| 14878 | * use in multithreaded programs. |
| 14879 | */ |
| 14880 | |
| 14881 | void |
| 14882 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 14883 | if (xmlParserInitialized != 0) |
| 14884 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14885 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14886 | #ifdef LIBXML_THREAD_ENABLED |
| 14887 | __xmlGlobalInitMutexLock(); |
| 14888 | if (xmlParserInitialized == 0) { |
| 14889 | #endif |
Daniel Veillard | 7dd7080 | 2009-06-04 11:08:39 +0200 | [diff] [blame] | 14890 | xmlInitThreads(); |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 14891 | xmlInitGlobals(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14892 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 14893 | (xmlGenericError == NULL)) |
| 14894 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14895 | xmlInitMemory(); |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 14896 | xmlInitializeDict(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14897 | xmlInitCharEncodingHandlers(); |
| 14898 | xmlDefaultSAXHandlerInit(); |
| 14899 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14900 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14901 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14902 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14903 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14904 | htmlInitAutoClose(); |
| 14905 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14906 | #endif |
| 14907 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14908 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14909 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14910 | xmlParserInitialized = 1; |
| 14911 | #ifdef LIBXML_THREAD_ENABLED |
| 14912 | } |
| 14913 | __xmlGlobalInitMutexUnlock(); |
| 14914 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14915 | } |
| 14916 | |
| 14917 | /** |
| 14918 | * xmlCleanupParser: |
| 14919 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 14920 | * This function name is somewhat misleading. It does not clean up |
| 14921 | * parser state, it cleans up memory allocated by the library itself. |
| 14922 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 14923 | * related global memory allocated for the library processing. |
| 14924 | * It doesn't deallocate any document related memory. One should |
| 14925 | * call xmlCleanupParser() only when the process has finished using |
| 14926 | * the library and all XML/HTML documents built with it. |
| 14927 | * See also xmlInitParser() which has the opposite function of preparing |
| 14928 | * the library for operations. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 14929 | * |
| 14930 | * WARNING: if your application is multithreaded or has plugin support |
| 14931 | * calling this may crash the application if another thread or |
| 14932 | * a plugin is still using libxml2. It's sometimes very hard to |
| 14933 | * guess if libxml2 is in use in the application, some libraries |
| 14934 | * or plugins may use it without notice. In case of doubt abstain |
| 14935 | * from calling this function or do it just before calling exit() |
| 14936 | * to avoid leak reports from valgrind ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14937 | */ |
| 14938 | |
| 14939 | void |
| 14940 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 14941 | if (!xmlParserInitialized) |
| 14942 | return; |
| 14943 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14944 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 14945 | #ifdef LIBXML_CATALOG_ENABLED |
| 14946 | xmlCatalogCleanup(); |
| 14947 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 14948 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 14949 | xmlCleanupInputCallbacks(); |
| 14950 | #ifdef LIBXML_OUTPUT_ENABLED |
| 14951 | xmlCleanupOutputCallbacks(); |
| 14952 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14953 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14954 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 14955 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14956 | #endif |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 14957 | xmlResetLastError(); |
Alexander Pastukhov | 704d8c5 | 2013-04-23 13:02:11 +0800 | [diff] [blame] | 14958 | xmlCleanupGlobals(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 14959 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14960 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 14961 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14962 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14963 | |
| 14964 | /************************************************************************ |
| 14965 | * * |
| 14966 | * New set (2.6.0) of simpler and more flexible APIs * |
| 14967 | * * |
| 14968 | ************************************************************************/ |
| 14969 | |
| 14970 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14971 | * DICT_FREE: |
| 14972 | * @str: a string |
| 14973 | * |
| 14974 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 14975 | * current scope |
| 14976 | */ |
| 14977 | #define DICT_FREE(str) \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14978 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14979 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 14980 | xmlFree((char *)(str)); |
| 14981 | |
| 14982 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14983 | * xmlCtxtReset: |
| 14984 | * @ctxt: an XML parser context |
| 14985 | * |
| 14986 | * Reset a parser context |
| 14987 | */ |
| 14988 | void |
| 14989 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 14990 | { |
| 14991 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14992 | xmlDictPtr dict; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14993 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14994 | if (ctxt == NULL) |
| 14995 | return; |
| 14996 | |
| 14997 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14998 | |
| 14999 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 15000 | xmlFreeInputStream(input); |
| 15001 | } |
| 15002 | ctxt->inputNr = 0; |
| 15003 | ctxt->input = NULL; |
| 15004 | |
| 15005 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 15006 | if (ctxt->spaceTab != NULL) { |
| 15007 | ctxt->spaceTab[0] = -1; |
| 15008 | ctxt->space = &ctxt->spaceTab[0]; |
| 15009 | } else { |
| 15010 | ctxt->space = NULL; |
| 15011 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15012 | |
| 15013 | |
| 15014 | ctxt->nodeNr = 0; |
| 15015 | ctxt->node = NULL; |
| 15016 | |
| 15017 | ctxt->nameNr = 0; |
| 15018 | ctxt->name = NULL; |
| 15019 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15020 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15021 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15022 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15023 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15024 | DICT_FREE(ctxt->directory); |
| 15025 | ctxt->directory = NULL; |
| 15026 | DICT_FREE(ctxt->extSubURI); |
| 15027 | ctxt->extSubURI = NULL; |
| 15028 | DICT_FREE(ctxt->extSubSystem); |
| 15029 | ctxt->extSubSystem = NULL; |
| 15030 | if (ctxt->myDoc != NULL) |
| 15031 | xmlFreeDoc(ctxt->myDoc); |
| 15032 | ctxt->myDoc = NULL; |
| 15033 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15034 | ctxt->standalone = -1; |
| 15035 | ctxt->hasExternalSubset = 0; |
| 15036 | ctxt->hasPErefs = 0; |
| 15037 | ctxt->html = 0; |
| 15038 | ctxt->external = 0; |
| 15039 | ctxt->instate = XML_PARSER_START; |
| 15040 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15041 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15042 | ctxt->wellFormed = 1; |
| 15043 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 15044 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15045 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 15046 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15047 | ctxt->vctxt.userData = ctxt; |
| 15048 | ctxt->vctxt.error = xmlParserValidityError; |
| 15049 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 15050 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15051 | ctxt->record_info = 0; |
| 15052 | ctxt->nbChars = 0; |
| 15053 | ctxt->checkIndex = 0; |
| 15054 | ctxt->inSubset = 0; |
| 15055 | ctxt->errNo = XML_ERR_OK; |
| 15056 | ctxt->depth = 0; |
| 15057 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 15058 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 15059 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 15060 | ctxt->sizeentities = 0; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 15061 | ctxt->sizeentcopy = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15062 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 15063 | |
| 15064 | if (ctxt->attsDefault != NULL) { |
| 15065 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 15066 | ctxt->attsDefault = NULL; |
| 15067 | } |
| 15068 | if (ctxt->attsSpecial != NULL) { |
| 15069 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 15070 | ctxt->attsSpecial = NULL; |
| 15071 | } |
| 15072 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 15073 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15074 | if (ctxt->catalogs != NULL) |
| 15075 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 15076 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 15077 | if (ctxt->lastError.code != XML_ERR_OK) |
| 15078 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15079 | } |
| 15080 | |
| 15081 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15082 | * xmlCtxtResetPush: |
| 15083 | * @ctxt: an XML parser context |
| 15084 | * @chunk: a pointer to an array of chars |
| 15085 | * @size: number of chars in the array |
| 15086 | * @filename: an optional file name or URI |
| 15087 | * @encoding: the document encoding, or NULL |
| 15088 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 15089 | * Reset a push parser context |
| 15090 | * |
| 15091 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15092 | */ |
| 15093 | int |
| 15094 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 15095 | int size, const char *filename, const char *encoding) |
| 15096 | { |
| 15097 | xmlParserInputPtr inputStream; |
| 15098 | xmlParserInputBufferPtr buf; |
| 15099 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 15100 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 15101 | if (ctxt == NULL) |
| 15102 | return(1); |
| 15103 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15104 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 15105 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 15106 | |
| 15107 | buf = xmlAllocParserInputBuffer(enc); |
| 15108 | if (buf == NULL) |
| 15109 | return(1); |
| 15110 | |
| 15111 | if (ctxt == NULL) { |
| 15112 | xmlFreeParserInputBuffer(buf); |
| 15113 | return(1); |
| 15114 | } |
| 15115 | |
| 15116 | xmlCtxtReset(ctxt); |
| 15117 | |
| 15118 | if (ctxt->pushTab == NULL) { |
| 15119 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 15120 | sizeof(xmlChar *)); |
| 15121 | if (ctxt->pushTab == NULL) { |
| 15122 | xmlErrMemory(ctxt, NULL); |
| 15123 | xmlFreeParserInputBuffer(buf); |
| 15124 | return(1); |
| 15125 | } |
| 15126 | } |
| 15127 | |
| 15128 | if (filename == NULL) { |
| 15129 | ctxt->directory = NULL; |
| 15130 | } else { |
| 15131 | ctxt->directory = xmlParserGetDirectory(filename); |
| 15132 | } |
| 15133 | |
| 15134 | inputStream = xmlNewInputStream(ctxt); |
| 15135 | if (inputStream == NULL) { |
| 15136 | xmlFreeParserInputBuffer(buf); |
| 15137 | return(1); |
| 15138 | } |
| 15139 | |
| 15140 | if (filename == NULL) |
| 15141 | inputStream->filename = NULL; |
| 15142 | else |
| 15143 | inputStream->filename = (char *) |
| 15144 | xmlCanonicPath((const xmlChar *) filename); |
| 15145 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 15146 | xmlBufResetInput(buf->buffer, inputStream); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15147 | |
| 15148 | inputPush(ctxt, inputStream); |
| 15149 | |
| 15150 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 15151 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15152 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 15153 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15154 | |
| 15155 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 15156 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15157 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15158 | #ifdef DEBUG_PUSH |
| 15159 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 15160 | #endif |
| 15161 | } |
| 15162 | |
| 15163 | if (encoding != NULL) { |
| 15164 | xmlCharEncodingHandlerPtr hdlr; |
| 15165 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15166 | if (ctxt->encoding != NULL) |
| 15167 | xmlFree((xmlChar *) ctxt->encoding); |
| 15168 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15169 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15170 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15171 | if (hdlr != NULL) { |
| 15172 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15173 | } else { |
| 15174 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 15175 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 15176 | } |
| 15177 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 15178 | xmlSwitchEncoding(ctxt, enc); |
| 15179 | } |
| 15180 | |
| 15181 | return(0); |
| 15182 | } |
| 15183 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15184 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15185 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15186 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15187 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15188 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15189 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15190 | * |
| 15191 | * Applies the options to the parser context |
| 15192 | * |
| 15193 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15194 | * in case of error. |
| 15195 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15196 | static int |
| 15197 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15198 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 15199 | if (ctxt == NULL) |
| 15200 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15201 | if (encoding != NULL) { |
| 15202 | if (ctxt->encoding != NULL) |
| 15203 | xmlFree((xmlChar *) ctxt->encoding); |
| 15204 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15205 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15206 | if (options & XML_PARSE_RECOVER) { |
| 15207 | ctxt->recovery = 1; |
| 15208 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15209 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15210 | } else |
| 15211 | ctxt->recovery = 0; |
| 15212 | if (options & XML_PARSE_DTDLOAD) { |
| 15213 | ctxt->loadsubset = XML_DETECT_IDS; |
| 15214 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15215 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15216 | } else |
| 15217 | ctxt->loadsubset = 0; |
| 15218 | if (options & XML_PARSE_DTDATTR) { |
| 15219 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 15220 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15221 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15222 | } |
| 15223 | if (options & XML_PARSE_NOENT) { |
| 15224 | ctxt->replaceEntities = 1; |
| 15225 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 15226 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15227 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15228 | } else |
| 15229 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15230 | if (options & XML_PARSE_PEDANTIC) { |
| 15231 | ctxt->pedantic = 1; |
| 15232 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15233 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15234 | } else |
| 15235 | ctxt->pedantic = 0; |
| 15236 | if (options & XML_PARSE_NOBLANKS) { |
| 15237 | ctxt->keepBlanks = 0; |
| 15238 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 15239 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15240 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15241 | } else |
| 15242 | ctxt->keepBlanks = 1; |
| 15243 | if (options & XML_PARSE_DTDVALID) { |
| 15244 | ctxt->validate = 1; |
| 15245 | if (options & XML_PARSE_NOWARNING) |
| 15246 | ctxt->vctxt.warning = NULL; |
| 15247 | if (options & XML_PARSE_NOERROR) |
| 15248 | ctxt->vctxt.error = NULL; |
| 15249 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15250 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15251 | } else |
| 15252 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 15253 | if (options & XML_PARSE_NOWARNING) { |
| 15254 | ctxt->sax->warning = NULL; |
| 15255 | options -= XML_PARSE_NOWARNING; |
| 15256 | } |
| 15257 | if (options & XML_PARSE_NOERROR) { |
| 15258 | ctxt->sax->error = NULL; |
| 15259 | ctxt->sax->fatalError = NULL; |
| 15260 | options -= XML_PARSE_NOERROR; |
| 15261 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15262 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15263 | if (options & XML_PARSE_SAX1) { |
| 15264 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 15265 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 15266 | ctxt->sax->startElementNs = NULL; |
| 15267 | ctxt->sax->endElementNs = NULL; |
| 15268 | ctxt->sax->initialized = 1; |
| 15269 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15270 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15271 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15272 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15273 | if (options & XML_PARSE_NODICT) { |
| 15274 | ctxt->dictNames = 0; |
| 15275 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15276 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15277 | } else { |
| 15278 | ctxt->dictNames = 1; |
| 15279 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15280 | if (options & XML_PARSE_NOCDATA) { |
| 15281 | ctxt->sax->cdataBlock = NULL; |
| 15282 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15283 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15284 | } |
| 15285 | if (options & XML_PARSE_NSCLEAN) { |
| 15286 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 15287 | options -= XML_PARSE_NSCLEAN; |
| 15288 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15289 | if (options & XML_PARSE_NONET) { |
| 15290 | ctxt->options |= XML_PARSE_NONET; |
| 15291 | options -= XML_PARSE_NONET; |
| 15292 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 15293 | if (options & XML_PARSE_COMPACT) { |
| 15294 | ctxt->options |= XML_PARSE_COMPACT; |
| 15295 | options -= XML_PARSE_COMPACT; |
| 15296 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 15297 | if (options & XML_PARSE_OLD10) { |
| 15298 | ctxt->options |= XML_PARSE_OLD10; |
| 15299 | options -= XML_PARSE_OLD10; |
| 15300 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15301 | if (options & XML_PARSE_NOBASEFIX) { |
| 15302 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 15303 | options -= XML_PARSE_NOBASEFIX; |
| 15304 | } |
| 15305 | if (options & XML_PARSE_HUGE) { |
| 15306 | ctxt->options |= XML_PARSE_HUGE; |
| 15307 | options -= XML_PARSE_HUGE; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 15308 | if (ctxt->dict != NULL) |
| 15309 | xmlDictSetLimit(ctxt->dict, 0); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15310 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 15311 | if (options & XML_PARSE_OLDSAX) { |
| 15312 | ctxt->options |= XML_PARSE_OLDSAX; |
| 15313 | options -= XML_PARSE_OLDSAX; |
| 15314 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 15315 | if (options & XML_PARSE_IGNORE_ENC) { |
| 15316 | ctxt->options |= XML_PARSE_IGNORE_ENC; |
| 15317 | options -= XML_PARSE_IGNORE_ENC; |
| 15318 | } |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 15319 | if (options & XML_PARSE_BIG_LINES) { |
| 15320 | ctxt->options |= XML_PARSE_BIG_LINES; |
| 15321 | options -= XML_PARSE_BIG_LINES; |
| 15322 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 15323 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15324 | return (options); |
| 15325 | } |
| 15326 | |
| 15327 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15328 | * xmlCtxtUseOptions: |
| 15329 | * @ctxt: an XML parser context |
| 15330 | * @options: a combination of xmlParserOption |
| 15331 | * |
| 15332 | * Applies the options to the parser context |
| 15333 | * |
| 15334 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15335 | * in case of error. |
| 15336 | */ |
| 15337 | int |
| 15338 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 15339 | { |
| 15340 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 15341 | } |
| 15342 | |
| 15343 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15344 | * xmlDoRead: |
| 15345 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15346 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15347 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15348 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15349 | * @reuse: keep the context for reuse |
| 15350 | * |
| 15351 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15352 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15353 | * Returns the resulting document tree or NULL |
| 15354 | */ |
| 15355 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15356 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 15357 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15358 | { |
| 15359 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15360 | |
| 15361 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15362 | if (encoding != NULL) { |
| 15363 | xmlCharEncodingHandlerPtr hdlr; |
| 15364 | |
| 15365 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15366 | if (hdlr != NULL) |
| 15367 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15368 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15369 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 15370 | (ctxt->input->filename == NULL)) |
| 15371 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15372 | xmlParseDocument(ctxt); |
| 15373 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 15374 | ret = ctxt->myDoc; |
| 15375 | else { |
| 15376 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15377 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15378 | xmlFreeDoc(ctxt->myDoc); |
| 15379 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15380 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15381 | ctxt->myDoc = NULL; |
| 15382 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15383 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15384 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15385 | |
| 15386 | return (ret); |
| 15387 | } |
| 15388 | |
| 15389 | /** |
| 15390 | * xmlReadDoc: |
| 15391 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15392 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15393 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15394 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15395 | * |
| 15396 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15397 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15398 | * Returns the resulting document tree |
| 15399 | */ |
| 15400 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15401 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15402 | { |
| 15403 | xmlParserCtxtPtr ctxt; |
| 15404 | |
| 15405 | if (cur == NULL) |
| 15406 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15407 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15408 | |
| 15409 | ctxt = xmlCreateDocParserCtxt(cur); |
| 15410 | if (ctxt == NULL) |
| 15411 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15412 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15413 | } |
| 15414 | |
| 15415 | /** |
| 15416 | * xmlReadFile: |
| 15417 | * @filename: a file or URL |
| 15418 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15419 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15420 | * |
| 15421 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15422 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15423 | * Returns the resulting document tree |
| 15424 | */ |
| 15425 | xmlDocPtr |
| 15426 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 15427 | { |
| 15428 | xmlParserCtxtPtr ctxt; |
| 15429 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15430 | xmlInitParser(); |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15431 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15432 | if (ctxt == NULL) |
| 15433 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15434 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15435 | } |
| 15436 | |
| 15437 | /** |
| 15438 | * xmlReadMemory: |
| 15439 | * @buffer: a pointer to a char array |
| 15440 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15441 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15442 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15443 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15444 | * |
| 15445 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15446 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15447 | * Returns the resulting document tree |
| 15448 | */ |
| 15449 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15450 | 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] | 15451 | { |
| 15452 | xmlParserCtxtPtr ctxt; |
| 15453 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15454 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15455 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 15456 | if (ctxt == NULL) |
| 15457 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15458 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15459 | } |
| 15460 | |
| 15461 | /** |
| 15462 | * xmlReadFd: |
| 15463 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15464 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15465 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15466 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15467 | * |
| 15468 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15469 | * NOTE that the file descriptor will not be closed when the |
| 15470 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15471 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15472 | * Returns the resulting document tree |
| 15473 | */ |
| 15474 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15475 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15476 | { |
| 15477 | xmlParserCtxtPtr ctxt; |
| 15478 | xmlParserInputBufferPtr input; |
| 15479 | xmlParserInputPtr stream; |
| 15480 | |
| 15481 | if (fd < 0) |
| 15482 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15483 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15484 | |
| 15485 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15486 | if (input == NULL) |
| 15487 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15488 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15489 | ctxt = xmlNewParserCtxt(); |
| 15490 | if (ctxt == NULL) { |
| 15491 | xmlFreeParserInputBuffer(input); |
| 15492 | return (NULL); |
| 15493 | } |
| 15494 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15495 | if (stream == NULL) { |
| 15496 | xmlFreeParserInputBuffer(input); |
| 15497 | xmlFreeParserCtxt(ctxt); |
| 15498 | return (NULL); |
| 15499 | } |
| 15500 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15501 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15502 | } |
| 15503 | |
| 15504 | /** |
| 15505 | * xmlReadIO: |
| 15506 | * @ioread: an I/O read function |
| 15507 | * @ioclose: an I/O close function |
| 15508 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15509 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15510 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15511 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15512 | * |
| 15513 | * parse an XML document from I/O functions and source and build a tree. |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15514 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15515 | * Returns the resulting document tree |
| 15516 | */ |
| 15517 | xmlDocPtr |
| 15518 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15519 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15520 | { |
| 15521 | xmlParserCtxtPtr ctxt; |
| 15522 | xmlParserInputBufferPtr input; |
| 15523 | xmlParserInputPtr stream; |
| 15524 | |
| 15525 | if (ioread == NULL) |
| 15526 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15527 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15528 | |
| 15529 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15530 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15531 | if (input == NULL) { |
| 15532 | if (ioclose != NULL) |
| 15533 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15534 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15535 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15536 | ctxt = xmlNewParserCtxt(); |
| 15537 | if (ctxt == NULL) { |
| 15538 | xmlFreeParserInputBuffer(input); |
| 15539 | return (NULL); |
| 15540 | } |
| 15541 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15542 | if (stream == NULL) { |
| 15543 | xmlFreeParserInputBuffer(input); |
| 15544 | xmlFreeParserCtxt(ctxt); |
| 15545 | return (NULL); |
| 15546 | } |
| 15547 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15548 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15549 | } |
| 15550 | |
| 15551 | /** |
| 15552 | * xmlCtxtReadDoc: |
| 15553 | * @ctxt: an XML parser context |
| 15554 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15555 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15556 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15557 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15558 | * |
| 15559 | * parse an XML in-memory document and build a tree. |
| 15560 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15561 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15562 | * Returns the resulting document tree |
| 15563 | */ |
| 15564 | xmlDocPtr |
| 15565 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15566 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15567 | { |
| 15568 | xmlParserInputPtr stream; |
| 15569 | |
| 15570 | if (cur == NULL) |
| 15571 | return (NULL); |
| 15572 | if (ctxt == NULL) |
| 15573 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15574 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15575 | |
| 15576 | xmlCtxtReset(ctxt); |
| 15577 | |
| 15578 | stream = xmlNewStringInputStream(ctxt, cur); |
| 15579 | if (stream == NULL) { |
| 15580 | return (NULL); |
| 15581 | } |
| 15582 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15583 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15584 | } |
| 15585 | |
| 15586 | /** |
| 15587 | * xmlCtxtReadFile: |
| 15588 | * @ctxt: an XML parser context |
| 15589 | * @filename: a file or URL |
| 15590 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15591 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15592 | * |
| 15593 | * parse an XML file from the filesystem or the network. |
| 15594 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15595 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15596 | * Returns the resulting document tree |
| 15597 | */ |
| 15598 | xmlDocPtr |
| 15599 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 15600 | const char *encoding, int options) |
| 15601 | { |
| 15602 | xmlParserInputPtr stream; |
| 15603 | |
| 15604 | if (filename == NULL) |
| 15605 | return (NULL); |
| 15606 | if (ctxt == NULL) |
| 15607 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15608 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15609 | |
| 15610 | xmlCtxtReset(ctxt); |
| 15611 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 15612 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15613 | if (stream == NULL) { |
| 15614 | return (NULL); |
| 15615 | } |
| 15616 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15617 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15618 | } |
| 15619 | |
| 15620 | /** |
| 15621 | * xmlCtxtReadMemory: |
| 15622 | * @ctxt: an XML parser context |
| 15623 | * @buffer: a pointer to a char array |
| 15624 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15625 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15626 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15627 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15628 | * |
| 15629 | * parse an XML in-memory document and build a tree. |
| 15630 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15631 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15632 | * Returns the resulting document tree |
| 15633 | */ |
| 15634 | xmlDocPtr |
| 15635 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15636 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15637 | { |
| 15638 | xmlParserInputBufferPtr input; |
| 15639 | xmlParserInputPtr stream; |
| 15640 | |
| 15641 | if (ctxt == NULL) |
| 15642 | return (NULL); |
| 15643 | if (buffer == NULL) |
| 15644 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15645 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15646 | |
| 15647 | xmlCtxtReset(ctxt); |
| 15648 | |
| 15649 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 15650 | if (input == NULL) { |
| 15651 | return(NULL); |
| 15652 | } |
| 15653 | |
| 15654 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15655 | if (stream == NULL) { |
| 15656 | xmlFreeParserInputBuffer(input); |
| 15657 | return(NULL); |
| 15658 | } |
| 15659 | |
| 15660 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15661 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15662 | } |
| 15663 | |
| 15664 | /** |
| 15665 | * xmlCtxtReadFd: |
| 15666 | * @ctxt: an XML parser context |
| 15667 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15668 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15669 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15670 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15671 | * |
| 15672 | * parse an XML from a file descriptor and build a tree. |
| 15673 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15674 | * NOTE that the file descriptor will not be closed when the |
| 15675 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15676 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15677 | * Returns the resulting document tree |
| 15678 | */ |
| 15679 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15680 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 15681 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15682 | { |
| 15683 | xmlParserInputBufferPtr input; |
| 15684 | xmlParserInputPtr stream; |
| 15685 | |
| 15686 | if (fd < 0) |
| 15687 | return (NULL); |
| 15688 | if (ctxt == NULL) |
| 15689 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15690 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15691 | |
| 15692 | xmlCtxtReset(ctxt); |
| 15693 | |
| 15694 | |
| 15695 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15696 | if (input == NULL) |
| 15697 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15698 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15699 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15700 | if (stream == NULL) { |
| 15701 | xmlFreeParserInputBuffer(input); |
| 15702 | return (NULL); |
| 15703 | } |
| 15704 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15705 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15706 | } |
| 15707 | |
| 15708 | /** |
| 15709 | * xmlCtxtReadIO: |
| 15710 | * @ctxt: an XML parser context |
| 15711 | * @ioread: an I/O read function |
| 15712 | * @ioclose: an I/O close function |
| 15713 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15714 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15715 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15716 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15717 | * |
| 15718 | * parse an XML document from I/O functions and source and build a tree. |
| 15719 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15720 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15721 | * Returns the resulting document tree |
| 15722 | */ |
| 15723 | xmlDocPtr |
| 15724 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 15725 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15726 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15727 | const char *encoding, int options) |
| 15728 | { |
| 15729 | xmlParserInputBufferPtr input; |
| 15730 | xmlParserInputPtr stream; |
| 15731 | |
| 15732 | if (ioread == NULL) |
| 15733 | return (NULL); |
| 15734 | if (ctxt == NULL) |
| 15735 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15736 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15737 | |
| 15738 | xmlCtxtReset(ctxt); |
| 15739 | |
| 15740 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15741 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15742 | if (input == NULL) { |
| 15743 | if (ioclose != NULL) |
| 15744 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15745 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15746 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15747 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15748 | if (stream == NULL) { |
| 15749 | xmlFreeParserInputBuffer(input); |
| 15750 | return (NULL); |
| 15751 | } |
| 15752 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15753 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15754 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15755 | |
| 15756 | #define bottom_parser |
| 15757 | #include "elfgcchack.h" |