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 | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 97 | /************************************************************************ |
| 98 | * * |
| 99 | * Arbitrary limits set in the parser. See XML_PARSE_HUGE * |
| 100 | * * |
| 101 | ************************************************************************/ |
| 102 | |
| 103 | #define XML_PARSER_BIG_ENTITY 1000 |
| 104 | #define XML_PARSER_LOT_ENTITY 5000 |
| 105 | |
| 106 | /* |
| 107 | * XML_PARSER_NON_LINEAR is the threshold where the ratio of parsed entity |
| 108 | * replacement over the size in byte of the input indicates that you have |
| 109 | * and eponential behaviour. A value of 10 correspond to at least 3 entity |
| 110 | * replacement per byte of input. |
| 111 | */ |
| 112 | #define XML_PARSER_NON_LINEAR 10 |
| 113 | |
| 114 | /* |
| 115 | * xmlParserEntityCheck |
| 116 | * |
| 117 | * Function to check non-linear entity expansion behaviour |
| 118 | * This is here to detect and stop exponential linear entity expansion |
| 119 | * This is not a limitation of the parser but a safety |
| 120 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 121 | * parser option. |
| 122 | */ |
| 123 | static int |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 124 | xmlParserEntityCheck(xmlParserCtxtPtr ctxt, size_t size, |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 125 | xmlEntityPtr ent, size_t replacement) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 126 | { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 127 | size_t consumed = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 128 | |
| 129 | if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE)) |
| 130 | return (0); |
| 131 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 132 | return (1); |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 133 | if (replacement != 0) { |
| 134 | if (replacement < XML_MAX_TEXT_LENGTH) |
| 135 | return(0); |
| 136 | |
| 137 | /* |
| 138 | * If the volume of entity copy reaches 10 times the |
| 139 | * amount of parsed data and over the large text threshold |
| 140 | * then that's very likely to be an abuse. |
| 141 | */ |
| 142 | if (ctxt->input != NULL) { |
| 143 | consumed = ctxt->input->consumed + |
| 144 | (ctxt->input->cur - ctxt->input->base); |
| 145 | } |
| 146 | consumed += ctxt->sizeentities; |
| 147 | |
| 148 | if (replacement < XML_PARSER_NON_LINEAR * consumed) |
| 149 | return(0); |
| 150 | } else if (size != 0) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Do the check based on the replacement size of the entity |
| 153 | */ |
| 154 | if (size < XML_PARSER_BIG_ENTITY) |
| 155 | return(0); |
| 156 | |
| 157 | /* |
| 158 | * A limit on the amount of text data reasonably used |
| 159 | */ |
| 160 | if (ctxt->input != NULL) { |
| 161 | consumed = ctxt->input->consumed + |
| 162 | (ctxt->input->cur - ctxt->input->base); |
| 163 | } |
| 164 | consumed += ctxt->sizeentities; |
| 165 | |
| 166 | if ((size < XML_PARSER_NON_LINEAR * consumed) && |
| 167 | (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed)) |
| 168 | return (0); |
| 169 | } else if (ent != NULL) { |
| 170 | /* |
| 171 | * use the number of parsed entities in the replacement |
| 172 | */ |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 173 | size = ent->checked / 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * The amount of data parsed counting entities size only once |
| 177 | */ |
| 178 | if (ctxt->input != NULL) { |
| 179 | consumed = ctxt->input->consumed + |
| 180 | (ctxt->input->cur - ctxt->input->base); |
| 181 | } |
| 182 | consumed += ctxt->sizeentities; |
| 183 | |
| 184 | /* |
| 185 | * Check the density of entities for the amount of data |
| 186 | * knowing an entity reference will take at least 3 bytes |
| 187 | */ |
| 188 | if (size * 3 < consumed * XML_PARSER_NON_LINEAR) |
| 189 | return (0); |
| 190 | } else { |
| 191 | /* |
| 192 | * strange we got no data for checking just return |
| 193 | */ |
| 194 | return (0); |
| 195 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 196 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 197 | return (1); |
| 198 | } |
| 199 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 200 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 201 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 202 | * |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 203 | * arbitrary depth limit for the XML documents that we allow to |
| 204 | * process. This is not a limitation of the parser but a safety |
| 205 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 206 | * parser option. |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 207 | */ |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 208 | unsigned int xmlParserMaxDepth = 256; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 210 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 211 | |
| 212 | #define SAX2 1 |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 213 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 214 | #define XML_PARSER_BUFFER_SIZE 100 |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 215 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 216 | |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 217 | /** |
| 218 | * XML_PARSER_CHUNK_SIZE |
| 219 | * |
| 220 | * When calling GROW that's the minimal amount of data |
| 221 | * the parser expected to have received. It is not a hard |
| 222 | * limit but an optimization when reading strings like Names |
| 223 | * It is not strictly needed as long as inputs available characters |
| 224 | * are followed by 0, which should be provided by the I/O level |
| 225 | */ |
| 226 | #define XML_PARSER_CHUNK_SIZE 100 |
| 227 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 228 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 229 | * List of XML prefixed PI allowed by W3C specs |
| 230 | */ |
| 231 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 232 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 233 | "xml-stylesheet", |
Daniel Veillard | 4c4653e | 2011-06-05 11:29:29 +0800 | [diff] [blame] | 234 | "xml-model", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 235 | NULL |
| 236 | }; |
| 237 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 238 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 239 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 240 | static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 241 | const xmlChar **str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 242 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 243 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 244 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 245 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 246 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 247 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 248 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 249 | static int |
| 250 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, |
| 251 | const char *encoding); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 252 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 253 | static void |
| 254 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 255 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 256 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 257 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 258 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 259 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 260 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 261 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 262 | static int |
| 263 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity); |
| 264 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 265 | /************************************************************************ |
| 266 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 267 | * Some factorized error routines * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 268 | * * |
| 269 | ************************************************************************/ |
| 270 | |
| 271 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 272 | * xmlErrAttributeDup: |
| 273 | * @ctxt: an XML parser context |
| 274 | * @prefix: the attribute prefix |
| 275 | * @localname: the attribute localname |
| 276 | * |
| 277 | * Handle a redefinition of attribute error |
| 278 | */ |
| 279 | static void |
| 280 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 281 | const xmlChar * localname) |
| 282 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 283 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 284 | (ctxt->instate == XML_PARSER_EOF)) |
| 285 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 286 | if (ctxt != NULL) |
| 287 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 288 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 289 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 290 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 291 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 292 | (const char *) localname, NULL, NULL, 0, 0, |
| 293 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 294 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 295 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 296 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 297 | (const char *) prefix, (const char *) localname, |
| 298 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 299 | localname); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 300 | if (ctxt != NULL) { |
| 301 | ctxt->wellFormed = 0; |
| 302 | if (ctxt->recovery == 0) |
| 303 | ctxt->disableSAX = 1; |
| 304 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * xmlFatalErr: |
| 309 | * @ctxt: an XML parser context |
| 310 | * @error: the error number |
| 311 | * @extra: extra information string |
| 312 | * |
| 313 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 314 | */ |
| 315 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 316 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 317 | { |
| 318 | const char *errmsg; |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 319 | char errstr[129] = ""; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 320 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 321 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 322 | (ctxt->instate == XML_PARSER_EOF)) |
| 323 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 324 | switch (error) { |
| 325 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 326 | errmsg = "CharRef: invalid hexadecimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 327 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 328 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 329 | errmsg = "CharRef: invalid decimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 330 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 331 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 332 | errmsg = "CharRef: invalid value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 333 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 334 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 335 | errmsg = "internal error"; |
| 336 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 337 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 338 | errmsg = "PEReference at end of document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 339 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 340 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 341 | errmsg = "PEReference in prolog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 342 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 343 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 344 | errmsg = "PEReference in epilog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 345 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 346 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 347 | errmsg = "PEReference: no name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 348 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 349 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 350 | errmsg = "PEReference: expecting ';'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 351 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 352 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 353 | errmsg = "Detected an entity reference loop"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 354 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 355 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 356 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 357 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 358 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 359 | errmsg = "PEReferences forbidden in internal subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 360 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 361 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 362 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 363 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 364 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 365 | errmsg = "AttValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 366 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 367 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 368 | errmsg = "Unescaped '<' not allowed in attributes values"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 369 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 370 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 371 | errmsg = "SystemLiteral \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 372 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 373 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 374 | errmsg = "Unfinished System or Public ID \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 375 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 376 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 377 | errmsg = "Sequence ']]>' not allowed in content"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 378 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 379 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 380 | errmsg = "SYSTEM or PUBLIC, the URI is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 381 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 382 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 383 | errmsg = "PUBLIC, the Public Identifier is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 384 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 385 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 386 | errmsg = "Comment must not contain '--' (double-hyphen)"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 387 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 388 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 389 | errmsg = "xmlParsePI : no target name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 390 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 391 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 392 | errmsg = "Invalid PI name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 393 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 394 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 395 | errmsg = "NOTATION: Name expected here"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 396 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 397 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 398 | errmsg = "'>' required to close NOTATION declaration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 399 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 400 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 401 | errmsg = "Entity value required"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 402 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 403 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 404 | errmsg = "Fragment not allowed"; |
| 405 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 406 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 407 | errmsg = "'(' required to start ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 408 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 409 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 410 | errmsg = "NmToken expected in ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 411 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 412 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 413 | errmsg = "')' required to finish ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 414 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 415 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 416 | errmsg = "MixedContentDecl : '|' or ')*' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 417 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 418 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 419 | errmsg = "MixedContentDecl : '#PCDATA' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 420 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 421 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 422 | errmsg = "ContentDecl : Name or '(' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 423 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 424 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 425 | errmsg = "ContentDecl : ',' '|' or ')' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 426 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 427 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 428 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 429 | "PEReference: forbidden within markup decl in internal subset"; |
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_GT_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 432 | errmsg = "expected '>'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 433 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 434 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 435 | errmsg = "XML conditional section '[' expected"; |
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_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 438 | errmsg = "Content error in the external subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 439 | break; |
| 440 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 441 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 442 | "conditional section INCLUDE or IGNORE keyword expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 443 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 444 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 445 | errmsg = "XML conditional section not closed"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 446 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 447 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 448 | errmsg = "Text declaration '<?xml' required"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 449 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 450 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 451 | errmsg = "parsing XML declaration: '?>' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 452 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 453 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 454 | errmsg = "external parsed entities cannot be standalone"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 455 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 456 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 457 | errmsg = "EntityRef: expecting ';'"; |
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_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 460 | errmsg = "DOCTYPE improperly terminated"; |
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_LTSLASH_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 463 | errmsg = "EndTag: '</' not found"; |
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_EQUAL_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 466 | errmsg = "expected '='"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 467 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 468 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 469 | errmsg = "String not closed expecting \" or '"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 470 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 471 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 472 | errmsg = "String not started expecting ' or \""; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 473 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 474 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 475 | errmsg = "Invalid XML encoding name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 476 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 477 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 478 | errmsg = "standalone accepts only 'yes' or 'no'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 479 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 480 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 481 | errmsg = "Document is empty"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 482 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 483 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 484 | errmsg = "Extra content at the end of the document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 485 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 486 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 487 | errmsg = "chunk is not well balanced"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 488 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 489 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 490 | errmsg = "extra content at the end of well balanced chunk"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 491 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 492 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 493 | errmsg = "Malformed declaration expecting version"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 494 | break; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 495 | case XML_ERR_NAME_TOO_LONG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 496 | errmsg = "Name too long use XML_PARSE_HUGE option"; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 497 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 498 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 499 | case: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 500 | errmsg = ""; |
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 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 503 | default: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 504 | errmsg = "Unregistered error message"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 505 | } |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 506 | if (info == NULL) |
| 507 | snprintf(errstr, 128, "%s\n", errmsg); |
| 508 | else |
| 509 | snprintf(errstr, 128, "%s: %%s\n", errmsg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 510 | if (ctxt != NULL) |
| 511 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 512 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 513 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, &errstr[0], |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 514 | info); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 515 | if (ctxt != NULL) { |
| 516 | ctxt->wellFormed = 0; |
| 517 | if (ctxt->recovery == 0) |
| 518 | ctxt->disableSAX = 1; |
| 519 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 522 | /** |
| 523 | * xmlFatalErrMsg: |
| 524 | * @ctxt: an XML parser context |
| 525 | * @error: the error number |
| 526 | * @msg: the error message |
| 527 | * |
| 528 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 529 | */ |
| 530 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 531 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 532 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 533 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 534 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 535 | (ctxt->instate == XML_PARSER_EOF)) |
| 536 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 537 | if (ctxt != NULL) |
| 538 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 539 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 540 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 541 | if (ctxt != NULL) { |
| 542 | ctxt->wellFormed = 0; |
| 543 | if (ctxt->recovery == 0) |
| 544 | ctxt->disableSAX = 1; |
| 545 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 549 | * xmlWarningMsg: |
| 550 | * @ctxt: an XML parser context |
| 551 | * @error: the error number |
| 552 | * @msg: the error message |
| 553 | * @str1: extra data |
| 554 | * @str2: extra data |
| 555 | * |
| 556 | * Handle a warning. |
| 557 | */ |
| 558 | static void |
| 559 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 560 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 561 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 562 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 563 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 564 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 565 | (ctxt->instate == XML_PARSER_EOF)) |
| 566 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 567 | if ((ctxt != NULL) && (ctxt->sax != NULL) && |
| 568 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 569 | schannel = ctxt->sax->serror; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 570 | if (ctxt != NULL) { |
| 571 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 572 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 573 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 574 | ctxt, NULL, XML_FROM_PARSER, error, |
| 575 | XML_ERR_WARNING, NULL, 0, |
| 576 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 577 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 578 | } else { |
| 579 | __xmlRaiseError(schannel, NULL, NULL, |
| 580 | ctxt, NULL, XML_FROM_PARSER, error, |
| 581 | XML_ERR_WARNING, NULL, 0, |
| 582 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 583 | msg, (const char *) str1, (const char *) str2); |
| 584 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
| 588 | * xmlValidityError: |
| 589 | * @ctxt: an XML parser context |
| 590 | * @error: the error number |
| 591 | * @msg: the error message |
| 592 | * @str1: extra data |
| 593 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 594 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 595 | */ |
| 596 | static void |
| 597 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 598 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 599 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 600 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 601 | |
| 602 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 603 | (ctxt->instate == XML_PARSER_EOF)) |
| 604 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 605 | if (ctxt != NULL) { |
| 606 | ctxt->errNo = error; |
| 607 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 608 | schannel = ctxt->sax->serror; |
| 609 | } |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 610 | if (ctxt != NULL) { |
| 611 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 612 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 613 | ctxt, NULL, XML_FROM_DTD, error, |
| 614 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 615 | (const char *) str2, NULL, 0, 0, |
| 616 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 617 | ctxt->valid = 0; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 618 | } else { |
| 619 | __xmlRaiseError(schannel, NULL, NULL, |
| 620 | ctxt, NULL, XML_FROM_DTD, error, |
| 621 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 622 | (const char *) str2, NULL, 0, 0, |
| 623 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 624 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 628 | * xmlFatalErrMsgInt: |
| 629 | * @ctxt: an XML parser context |
| 630 | * @error: the error number |
| 631 | * @msg: the error message |
| 632 | * @val: an integer value |
| 633 | * |
| 634 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 635 | */ |
| 636 | static void |
| 637 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 638 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 639 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 640 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 641 | (ctxt->instate == XML_PARSER_EOF)) |
| 642 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 643 | if (ctxt != NULL) |
| 644 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 645 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 646 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 647 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 648 | if (ctxt != NULL) { |
| 649 | ctxt->wellFormed = 0; |
| 650 | if (ctxt->recovery == 0) |
| 651 | ctxt->disableSAX = 1; |
| 652 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 656 | * xmlFatalErrMsgStrIntStr: |
| 657 | * @ctxt: an XML parser context |
| 658 | * @error: the error number |
| 659 | * @msg: the error message |
| 660 | * @str1: an string info |
| 661 | * @val: an integer value |
| 662 | * @str2: an string info |
| 663 | * |
| 664 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 665 | */ |
| 666 | static void |
| 667 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 668 | const char *msg, const xmlChar *str1, int val, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 669 | const xmlChar *str2) |
| 670 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 671 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 672 | (ctxt->instate == XML_PARSER_EOF)) |
| 673 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 674 | if (ctxt != NULL) |
| 675 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 676 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 677 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 678 | NULL, 0, (const char *) str1, (const char *) str2, |
| 679 | NULL, val, 0, msg, str1, val, str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 680 | if (ctxt != NULL) { |
| 681 | ctxt->wellFormed = 0; |
| 682 | if (ctxt->recovery == 0) |
| 683 | ctxt->disableSAX = 1; |
| 684 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 688 | * xmlFatalErrMsgStr: |
| 689 | * @ctxt: an XML parser context |
| 690 | * @error: the error number |
| 691 | * @msg: the error message |
| 692 | * @val: a string value |
| 693 | * |
| 694 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 695 | */ |
| 696 | static void |
| 697 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 698 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 699 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 700 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 701 | (ctxt->instate == XML_PARSER_EOF)) |
| 702 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 703 | if (ctxt != NULL) |
| 704 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 705 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 706 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 707 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 708 | val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 709 | if (ctxt != NULL) { |
| 710 | ctxt->wellFormed = 0; |
| 711 | if (ctxt->recovery == 0) |
| 712 | ctxt->disableSAX = 1; |
| 713 | } |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 717 | * xmlErrMsgStr: |
| 718 | * @ctxt: an XML parser context |
| 719 | * @error: the error number |
| 720 | * @msg: the error message |
| 721 | * @val: a string value |
| 722 | * |
| 723 | * Handle a non fatal parser error |
| 724 | */ |
| 725 | static void |
| 726 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 727 | const char *msg, const xmlChar * val) |
| 728 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 729 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 730 | (ctxt->instate == XML_PARSER_EOF)) |
| 731 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 732 | if (ctxt != NULL) |
| 733 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 734 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 735 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 736 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 737 | val); |
| 738 | } |
| 739 | |
| 740 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 741 | * xmlNsErr: |
| 742 | * @ctxt: an XML parser context |
| 743 | * @error: the error number |
| 744 | * @msg: the message |
| 745 | * @info1: extra information string |
| 746 | * @info2: extra information string |
| 747 | * |
| 748 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 749 | */ |
| 750 | static void |
| 751 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 752 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 753 | const xmlChar * info1, const xmlChar * info2, |
| 754 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 755 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 756 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 757 | (ctxt->instate == XML_PARSER_EOF)) |
| 758 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 759 | if (ctxt != NULL) |
| 760 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 761 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 762 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 763 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 764 | info1, info2, info3); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 765 | if (ctxt != NULL) |
| 766 | ctxt->nsWellFormed = 0; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 769 | /** |
| 770 | * xmlNsWarn |
| 771 | * @ctxt: an XML parser context |
| 772 | * @error: the error number |
| 773 | * @msg: the message |
| 774 | * @info1: extra information string |
| 775 | * @info2: extra information string |
| 776 | * |
Daniel Veillard | 288bb62 | 2012-05-07 15:01:29 +0800 | [diff] [blame] | 777 | * Handle a namespace warning error |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 778 | */ |
| 779 | static void |
| 780 | xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 781 | const char *msg, |
| 782 | const xmlChar * info1, const xmlChar * info2, |
| 783 | const xmlChar * info3) |
| 784 | { |
| 785 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 786 | (ctxt->instate == XML_PARSER_EOF)) |
| 787 | return; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 788 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
| 789 | XML_ERR_WARNING, NULL, 0, (const char *) info1, |
| 790 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 791 | info1, info2, info3); |
| 792 | } |
| 793 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 794 | /************************************************************************ |
| 795 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 796 | * Library wide options * |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 797 | * * |
| 798 | ************************************************************************/ |
| 799 | |
| 800 | /** |
| 801 | * xmlHasFeature: |
| 802 | * @feature: the feature to be examined |
| 803 | * |
| 804 | * Examines if the library has been compiled with a given feature. |
| 805 | * |
| 806 | * Returns a non-zero value if the feature exist, otherwise zero. |
| 807 | * Returns zero (0) if the feature does not exist or an unknown |
| 808 | * unknown feature is requested, non-zero otherwise. |
| 809 | */ |
| 810 | int |
| 811 | xmlHasFeature(xmlFeature feature) |
| 812 | { |
| 813 | switch (feature) { |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 814 | case XML_WITH_THREAD: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 815 | #ifdef LIBXML_THREAD_ENABLED |
| 816 | return(1); |
| 817 | #else |
| 818 | return(0); |
| 819 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 820 | case XML_WITH_TREE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 821 | #ifdef LIBXML_TREE_ENABLED |
| 822 | return(1); |
| 823 | #else |
| 824 | return(0); |
| 825 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 826 | case XML_WITH_OUTPUT: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 827 | #ifdef LIBXML_OUTPUT_ENABLED |
| 828 | return(1); |
| 829 | #else |
| 830 | return(0); |
| 831 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 832 | case XML_WITH_PUSH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 833 | #ifdef LIBXML_PUSH_ENABLED |
| 834 | return(1); |
| 835 | #else |
| 836 | return(0); |
| 837 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 838 | case XML_WITH_READER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 839 | #ifdef LIBXML_READER_ENABLED |
| 840 | return(1); |
| 841 | #else |
| 842 | return(0); |
| 843 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 844 | case XML_WITH_PATTERN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 845 | #ifdef LIBXML_PATTERN_ENABLED |
| 846 | return(1); |
| 847 | #else |
| 848 | return(0); |
| 849 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 850 | case XML_WITH_WRITER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 851 | #ifdef LIBXML_WRITER_ENABLED |
| 852 | return(1); |
| 853 | #else |
| 854 | return(0); |
| 855 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 856 | case XML_WITH_SAX1: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 857 | #ifdef LIBXML_SAX1_ENABLED |
| 858 | return(1); |
| 859 | #else |
| 860 | return(0); |
| 861 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 862 | case XML_WITH_FTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 863 | #ifdef LIBXML_FTP_ENABLED |
| 864 | return(1); |
| 865 | #else |
| 866 | return(0); |
| 867 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 868 | case XML_WITH_HTTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 869 | #ifdef LIBXML_HTTP_ENABLED |
| 870 | return(1); |
| 871 | #else |
| 872 | return(0); |
| 873 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 874 | case XML_WITH_VALID: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 875 | #ifdef LIBXML_VALID_ENABLED |
| 876 | return(1); |
| 877 | #else |
| 878 | return(0); |
| 879 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 880 | case XML_WITH_HTML: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 881 | #ifdef LIBXML_HTML_ENABLED |
| 882 | return(1); |
| 883 | #else |
| 884 | return(0); |
| 885 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 886 | case XML_WITH_LEGACY: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 887 | #ifdef LIBXML_LEGACY_ENABLED |
| 888 | return(1); |
| 889 | #else |
| 890 | return(0); |
| 891 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 892 | case XML_WITH_C14N: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 893 | #ifdef LIBXML_C14N_ENABLED |
| 894 | return(1); |
| 895 | #else |
| 896 | return(0); |
| 897 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 898 | case XML_WITH_CATALOG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 899 | #ifdef LIBXML_CATALOG_ENABLED |
| 900 | return(1); |
| 901 | #else |
| 902 | return(0); |
| 903 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 904 | case XML_WITH_XPATH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 905 | #ifdef LIBXML_XPATH_ENABLED |
| 906 | return(1); |
| 907 | #else |
| 908 | return(0); |
| 909 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 910 | case XML_WITH_XPTR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 911 | #ifdef LIBXML_XPTR_ENABLED |
| 912 | return(1); |
| 913 | #else |
| 914 | return(0); |
| 915 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 916 | case XML_WITH_XINCLUDE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 917 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 918 | return(1); |
| 919 | #else |
| 920 | return(0); |
| 921 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 922 | case XML_WITH_ICONV: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 923 | #ifdef LIBXML_ICONV_ENABLED |
| 924 | return(1); |
| 925 | #else |
| 926 | return(0); |
| 927 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 928 | case XML_WITH_ISO8859X: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 929 | #ifdef LIBXML_ISO8859X_ENABLED |
| 930 | return(1); |
| 931 | #else |
| 932 | return(0); |
| 933 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 934 | case XML_WITH_UNICODE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 935 | #ifdef LIBXML_UNICODE_ENABLED |
| 936 | return(1); |
| 937 | #else |
| 938 | return(0); |
| 939 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 940 | case XML_WITH_REGEXP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 941 | #ifdef LIBXML_REGEXP_ENABLED |
| 942 | return(1); |
| 943 | #else |
| 944 | return(0); |
| 945 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 946 | case XML_WITH_AUTOMATA: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 947 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 948 | return(1); |
| 949 | #else |
| 950 | return(0); |
| 951 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 952 | case XML_WITH_EXPR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 953 | #ifdef LIBXML_EXPR_ENABLED |
| 954 | return(1); |
| 955 | #else |
| 956 | return(0); |
| 957 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 958 | case XML_WITH_SCHEMAS: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 959 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 960 | return(1); |
| 961 | #else |
| 962 | return(0); |
| 963 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 964 | case XML_WITH_SCHEMATRON: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 965 | #ifdef LIBXML_SCHEMATRON_ENABLED |
| 966 | return(1); |
| 967 | #else |
| 968 | return(0); |
| 969 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 970 | case XML_WITH_MODULES: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 971 | #ifdef LIBXML_MODULES_ENABLED |
| 972 | return(1); |
| 973 | #else |
| 974 | return(0); |
| 975 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 976 | case XML_WITH_DEBUG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 977 | #ifdef LIBXML_DEBUG_ENABLED |
| 978 | return(1); |
| 979 | #else |
| 980 | return(0); |
| 981 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 982 | case XML_WITH_DEBUG_MEM: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 983 | #ifdef DEBUG_MEMORY_LOCATION |
| 984 | return(1); |
| 985 | #else |
| 986 | return(0); |
| 987 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 988 | case XML_WITH_DEBUG_RUN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 989 | #ifdef LIBXML_DEBUG_RUNTIME |
| 990 | return(1); |
| 991 | #else |
| 992 | return(0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 993 | #endif |
Daniel Veillard | 75acfee | 2006-07-13 06:29:56 +0000 | [diff] [blame] | 994 | case XML_WITH_ZLIB: |
| 995 | #ifdef LIBXML_ZLIB_ENABLED |
| 996 | return(1); |
| 997 | #else |
| 998 | return(0); |
| 999 | #endif |
Anders F Bjorklund | eae5261 | 2011-09-18 16:59:13 +0200 | [diff] [blame] | 1000 | case XML_WITH_LZMA: |
| 1001 | #ifdef LIBXML_LZMA_ENABLED |
| 1002 | return(1); |
| 1003 | #else |
| 1004 | return(0); |
| 1005 | #endif |
Giuseppe Iuculano | 48f7dcb | 2010-11-04 17:42:42 +0100 | [diff] [blame] | 1006 | case XML_WITH_ICU: |
| 1007 | #ifdef LIBXML_ICU_ENABLED |
| 1008 | return(1); |
| 1009 | #else |
| 1010 | return(0); |
| 1011 | #endif |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 1012 | default: |
| 1013 | break; |
| 1014 | } |
| 1015 | return(0); |
| 1016 | } |
| 1017 | |
| 1018 | /************************************************************************ |
| 1019 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1020 | * SAX2 defaulted attributes handling * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1021 | * * |
| 1022 | ************************************************************************/ |
| 1023 | |
| 1024 | /** |
| 1025 | * xmlDetectSAX2: |
| 1026 | * @ctxt: an XML parser context |
| 1027 | * |
| 1028 | * Do the SAX2 detection and specific intialization |
| 1029 | */ |
| 1030 | static void |
| 1031 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 1032 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1033 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1034 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 1035 | ((ctxt->sax->startElementNs != NULL) || |
| 1036 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1037 | #else |
| 1038 | ctxt->sax2 = 1; |
| 1039 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1040 | |
| 1041 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 1042 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 1043 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1044 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 1045 | (ctxt->str_xml_ns == NULL)) { |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 1046 | xmlErrMemory(ctxt, NULL); |
| 1047 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1050 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 1051 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 1052 | struct _xmlDefAttrs { |
| 1053 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 1054 | int maxAttrs; /* the size of the array */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1055 | const xmlChar *values[5]; /* array of localname/prefix/values/external */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1056 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1057 | |
| 1058 | /** |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1059 | * xmlAttrNormalizeSpace: |
| 1060 | * @src: the source string |
| 1061 | * @dst: the target string |
| 1062 | * |
| 1063 | * Normalize the space in non CDATA attribute values: |
| 1064 | * If the attribute type is not CDATA, then the XML processor MUST further |
| 1065 | * process the normalized attribute value by discarding any leading and |
| 1066 | * trailing space (#x20) characters, and by replacing sequences of space |
| 1067 | * (#x20) characters by a single space (#x20) character. |
| 1068 | * Note that the size of dst need to be at least src, and if one doesn't need |
| 1069 | * to preserve dst (and it doesn't come from a dictionary or read-only) then |
| 1070 | * passing src as dst is just fine. |
| 1071 | * |
| 1072 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1073 | * is needed. |
| 1074 | */ |
| 1075 | static xmlChar * |
| 1076 | xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst) |
| 1077 | { |
| 1078 | if ((src == NULL) || (dst == NULL)) |
| 1079 | return(NULL); |
| 1080 | |
| 1081 | while (*src == 0x20) src++; |
| 1082 | while (*src != 0) { |
| 1083 | if (*src == 0x20) { |
| 1084 | while (*src == 0x20) src++; |
| 1085 | if (*src != 0) |
| 1086 | *dst++ = 0x20; |
| 1087 | } else { |
| 1088 | *dst++ = *src++; |
| 1089 | } |
| 1090 | } |
| 1091 | *dst = 0; |
| 1092 | if (dst == src) |
| 1093 | return(NULL); |
| 1094 | return(dst); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * xmlAttrNormalizeSpace2: |
| 1099 | * @src: the source string |
| 1100 | * |
| 1101 | * Normalize the space in non CDATA attribute values, a slightly more complex |
| 1102 | * front end to avoid allocation problems when running on attribute values |
| 1103 | * coming from the input. |
| 1104 | * |
| 1105 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1106 | * is needed. |
| 1107 | */ |
| 1108 | static const xmlChar * |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1109 | xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len) |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1110 | { |
| 1111 | int i; |
| 1112 | int remove_head = 0; |
| 1113 | int need_realloc = 0; |
| 1114 | const xmlChar *cur; |
| 1115 | |
| 1116 | if ((ctxt == NULL) || (src == NULL) || (len == NULL)) |
| 1117 | return(NULL); |
| 1118 | i = *len; |
| 1119 | if (i <= 0) |
| 1120 | return(NULL); |
| 1121 | |
| 1122 | cur = src; |
| 1123 | while (*cur == 0x20) { |
| 1124 | cur++; |
| 1125 | remove_head++; |
| 1126 | } |
| 1127 | while (*cur != 0) { |
| 1128 | if (*cur == 0x20) { |
| 1129 | cur++; |
| 1130 | if ((*cur == 0x20) || (*cur == 0)) { |
| 1131 | need_realloc = 1; |
| 1132 | break; |
| 1133 | } |
| 1134 | } else |
| 1135 | cur++; |
| 1136 | } |
| 1137 | if (need_realloc) { |
| 1138 | xmlChar *ret; |
| 1139 | |
| 1140 | ret = xmlStrndup(src + remove_head, i - remove_head + 1); |
| 1141 | if (ret == NULL) { |
| 1142 | xmlErrMemory(ctxt, NULL); |
| 1143 | return(NULL); |
| 1144 | } |
| 1145 | xmlAttrNormalizeSpace(ret, ret); |
| 1146 | *len = (int) strlen((const char *)ret); |
| 1147 | return(ret); |
| 1148 | } else if (remove_head) { |
| 1149 | *len -= remove_head; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1150 | memmove(src, src + remove_head, 1 + *len); |
| 1151 | return(src); |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1152 | } |
| 1153 | return(NULL); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1157 | * xmlAddDefAttrs: |
| 1158 | * @ctxt: an XML parser context |
| 1159 | * @fullname: the element fullname |
| 1160 | * @fullattr: the attribute fullname |
| 1161 | * @value: the attribute value |
| 1162 | * |
| 1163 | * Add a defaulted attribute for an element |
| 1164 | */ |
| 1165 | static void |
| 1166 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 1167 | const xmlChar *fullname, |
| 1168 | const xmlChar *fullattr, |
| 1169 | const xmlChar *value) { |
| 1170 | xmlDefAttrsPtr defaults; |
| 1171 | int len; |
| 1172 | const xmlChar *name; |
| 1173 | const xmlChar *prefix; |
| 1174 | |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 1175 | /* |
| 1176 | * Allows to detect attribute redefinitions |
| 1177 | */ |
| 1178 | if (ctxt->attsSpecial != NULL) { |
| 1179 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1180 | return; |
| 1181 | } |
| 1182 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1183 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1184 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1185 | if (ctxt->attsDefault == NULL) |
| 1186 | goto mem_error; |
| 1187 | } |
| 1188 | |
| 1189 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1190 | * split the element name into prefix:localname , the string found |
| 1191 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1192 | */ |
| 1193 | name = xmlSplitQName3(fullname, &len); |
| 1194 | if (name == NULL) { |
| 1195 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 1196 | prefix = NULL; |
| 1197 | } else { |
| 1198 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1199 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * make sure there is some storage |
| 1204 | */ |
| 1205 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 1206 | if (defaults == NULL) { |
| 1207 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1208 | (4 * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1209 | if (defaults == NULL) |
| 1210 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1211 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1212 | defaults->maxAttrs = 4; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1213 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1214 | defaults, NULL) < 0) { |
| 1215 | xmlFree(defaults); |
| 1216 | goto mem_error; |
| 1217 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1218 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1219 | xmlDefAttrsPtr temp; |
| 1220 | |
| 1221 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1222 | (2 * defaults->maxAttrs * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1223 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1224 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1225 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1226 | defaults->maxAttrs *= 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1227 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1228 | defaults, NULL) < 0) { |
| 1229 | xmlFree(defaults); |
| 1230 | goto mem_error; |
| 1231 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /* |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 1235 | * Split the element name into prefix:localname , the string found |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1236 | * are within the DTD and hen not associated to namespace names. |
| 1237 | */ |
| 1238 | name = xmlSplitQName3(fullattr, &len); |
| 1239 | if (name == NULL) { |
| 1240 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 1241 | prefix = NULL; |
| 1242 | } else { |
| 1243 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1244 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 1245 | } |
| 1246 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1247 | defaults->values[5 * defaults->nbAttrs] = name; |
| 1248 | defaults->values[5 * defaults->nbAttrs + 1] = prefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1249 | /* intern the string and precompute the end */ |
| 1250 | len = xmlStrlen(value); |
| 1251 | value = xmlDictLookup(ctxt->dict, value, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1252 | defaults->values[5 * defaults->nbAttrs + 2] = value; |
| 1253 | defaults->values[5 * defaults->nbAttrs + 3] = value + len; |
| 1254 | if (ctxt->external) |
| 1255 | defaults->values[5 * defaults->nbAttrs + 4] = BAD_CAST "external"; |
| 1256 | else |
| 1257 | defaults->values[5 * defaults->nbAttrs + 4] = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1258 | defaults->nbAttrs++; |
| 1259 | |
| 1260 | return; |
| 1261 | |
| 1262 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1263 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1264 | return; |
| 1265 | } |
| 1266 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1267 | /** |
| 1268 | * xmlAddSpecialAttr: |
| 1269 | * @ctxt: an XML parser context |
| 1270 | * @fullname: the element fullname |
| 1271 | * @fullattr: the attribute fullname |
| 1272 | * @type: the attribute type |
| 1273 | * |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1274 | * Register this attribute type |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1275 | */ |
| 1276 | static void |
| 1277 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 1278 | const xmlChar *fullname, |
| 1279 | const xmlChar *fullattr, |
| 1280 | int type) |
| 1281 | { |
| 1282 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1283 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1284 | if (ctxt->attsSpecial == NULL) |
| 1285 | goto mem_error; |
| 1286 | } |
| 1287 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1288 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1289 | return; |
| 1290 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1291 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 1292 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1293 | return; |
| 1294 | |
| 1295 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1296 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1300 | /** |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1301 | * xmlCleanSpecialAttrCallback: |
| 1302 | * |
| 1303 | * Removes CDATA attributes from the special attribute table |
| 1304 | */ |
| 1305 | static void |
| 1306 | xmlCleanSpecialAttrCallback(void *payload, void *data, |
| 1307 | const xmlChar *fullname, const xmlChar *fullattr, |
| 1308 | const xmlChar *unused ATTRIBUTE_UNUSED) { |
| 1309 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data; |
| 1310 | |
Daniel Veillard | b3edafd | 2008-01-11 08:00:57 +0000 | [diff] [blame] | 1311 | if (((long) payload) == XML_ATTRIBUTE_CDATA) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1312 | xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * xmlCleanSpecialAttr: |
| 1318 | * @ctxt: an XML parser context |
| 1319 | * |
| 1320 | * Trim the list of attributes defined to remove all those of type |
| 1321 | * CDATA as they are not special. This call should be done when finishing |
| 1322 | * to parse the DTD and before starting to parse the document root. |
| 1323 | */ |
| 1324 | static void |
| 1325 | xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt) |
| 1326 | { |
| 1327 | if (ctxt->attsSpecial == NULL) |
| 1328 | return; |
| 1329 | |
| 1330 | xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt); |
| 1331 | |
| 1332 | if (xmlHashSize(ctxt->attsSpecial) == 0) { |
| 1333 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 1334 | ctxt->attsSpecial = NULL; |
| 1335 | } |
| 1336 | return; |
| 1337 | } |
| 1338 | |
| 1339 | /** |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1340 | * xmlCheckLanguageID: |
| 1341 | * @lang: pointer to the string value |
| 1342 | * |
| 1343 | * Checks that the value conforms to the LanguageID production: |
| 1344 | * |
| 1345 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 1346 | * the XML Second edition. |
| 1347 | * |
| 1348 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 1349 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 1350 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 1351 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 1352 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 1353 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 1354 | * |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1355 | * The current REC reference the sucessors of RFC 1766, currently 5646 |
| 1356 | * |
| 1357 | * http://www.rfc-editor.org/rfc/rfc5646.txt |
| 1358 | * langtag = language |
| 1359 | * ["-" script] |
| 1360 | * ["-" region] |
| 1361 | * *("-" variant) |
| 1362 | * *("-" extension) |
| 1363 | * ["-" privateuse] |
| 1364 | * language = 2*3ALPHA ; shortest ISO 639 code |
| 1365 | * ["-" extlang] ; sometimes followed by |
| 1366 | * ; extended language subtags |
| 1367 | * / 4ALPHA ; or reserved for future use |
| 1368 | * / 5*8ALPHA ; or registered language subtag |
| 1369 | * |
| 1370 | * extlang = 3ALPHA ; selected ISO 639 codes |
| 1371 | * *2("-" 3ALPHA) ; permanently reserved |
| 1372 | * |
| 1373 | * script = 4ALPHA ; ISO 15924 code |
| 1374 | * |
| 1375 | * region = 2ALPHA ; ISO 3166-1 code |
| 1376 | * / 3DIGIT ; UN M.49 code |
| 1377 | * |
| 1378 | * variant = 5*8alphanum ; registered variants |
| 1379 | * / (DIGIT 3alphanum) |
| 1380 | * |
| 1381 | * extension = singleton 1*("-" (2*8alphanum)) |
| 1382 | * |
| 1383 | * ; Single alphanumerics |
| 1384 | * ; "x" reserved for private use |
| 1385 | * singleton = DIGIT ; 0 - 9 |
| 1386 | * / %x41-57 ; A - W |
| 1387 | * / %x59-5A ; Y - Z |
| 1388 | * / %x61-77 ; a - w |
| 1389 | * / %x79-7A ; y - z |
| 1390 | * |
| 1391 | * it sounds right to still allow Irregular i-xxx IANA and user codes too |
| 1392 | * The parser below doesn't try to cope with extension or privateuse |
| 1393 | * that could be added but that's not interoperable anyway |
| 1394 | * |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1395 | * Returns 1 if correct 0 otherwise |
| 1396 | **/ |
| 1397 | int |
| 1398 | xmlCheckLanguageID(const xmlChar * lang) |
| 1399 | { |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1400 | const xmlChar *cur = lang, *nxt; |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (cur == NULL) |
| 1403 | return (0); |
| 1404 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1405 | ((cur[0] == 'I') && (cur[1] == '-')) || |
| 1406 | ((cur[0] == 'x') && (cur[1] == '-')) || |
| 1407 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1408 | /* |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1409 | * Still allow IANA code and user code which were coming |
| 1410 | * from the previous version of the XML-1.0 specification |
| 1411 | * it's deprecated but we should not fail |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1412 | */ |
| 1413 | cur += 2; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1414 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1415 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1416 | cur++; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1417 | return(cur[0] == 0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1418 | } |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1419 | nxt = cur; |
| 1420 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1421 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1422 | nxt++; |
| 1423 | if (nxt - cur >= 4) { |
| 1424 | /* |
| 1425 | * Reserved |
| 1426 | */ |
| 1427 | if ((nxt - cur > 8) || (nxt[0] != 0)) |
| 1428 | return(0); |
| 1429 | return(1); |
| 1430 | } |
| 1431 | if (nxt - cur < 2) |
| 1432 | return(0); |
| 1433 | /* we got an ISO 639 code */ |
| 1434 | if (nxt[0] == 0) |
| 1435 | return(1); |
| 1436 | if (nxt[0] != '-') |
| 1437 | return(0); |
| 1438 | |
| 1439 | nxt++; |
| 1440 | cur = nxt; |
| 1441 | /* now we can have extlang or script or region or variant */ |
| 1442 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1443 | goto region_m49; |
| 1444 | |
| 1445 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1446 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1447 | nxt++; |
| 1448 | if (nxt - cur == 4) |
| 1449 | goto script; |
| 1450 | if (nxt - cur == 2) |
| 1451 | goto region; |
| 1452 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1453 | goto variant; |
| 1454 | if (nxt - cur != 3) |
| 1455 | return(0); |
| 1456 | /* we parsed an extlang */ |
| 1457 | if (nxt[0] == 0) |
| 1458 | return(1); |
| 1459 | if (nxt[0] != '-') |
| 1460 | return(0); |
| 1461 | |
| 1462 | nxt++; |
| 1463 | cur = nxt; |
| 1464 | /* now we can have script or region or variant */ |
| 1465 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1466 | goto region_m49; |
| 1467 | |
| 1468 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1469 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1470 | nxt++; |
| 1471 | if (nxt - cur == 2) |
| 1472 | goto region; |
| 1473 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1474 | goto variant; |
| 1475 | if (nxt - cur != 4) |
| 1476 | return(0); |
| 1477 | /* we parsed a script */ |
| 1478 | script: |
| 1479 | if (nxt[0] == 0) |
| 1480 | return(1); |
| 1481 | if (nxt[0] != '-') |
| 1482 | return(0); |
| 1483 | |
| 1484 | nxt++; |
| 1485 | cur = nxt; |
| 1486 | /* now we can have region or variant */ |
| 1487 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1488 | goto region_m49; |
| 1489 | |
| 1490 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1491 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1492 | nxt++; |
| 1493 | |
| 1494 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1495 | goto variant; |
| 1496 | if (nxt - cur != 2) |
| 1497 | return(0); |
| 1498 | /* we parsed a region */ |
| 1499 | region: |
| 1500 | if (nxt[0] == 0) |
| 1501 | return(1); |
| 1502 | if (nxt[0] != '-') |
| 1503 | return(0); |
| 1504 | |
| 1505 | nxt++; |
| 1506 | cur = nxt; |
| 1507 | /* now we can just have a variant */ |
| 1508 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1509 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1510 | nxt++; |
| 1511 | |
| 1512 | if ((nxt - cur < 5) || (nxt - cur > 8)) |
| 1513 | return(0); |
| 1514 | |
| 1515 | /* we parsed a variant */ |
| 1516 | variant: |
| 1517 | if (nxt[0] == 0) |
| 1518 | return(1); |
| 1519 | if (nxt[0] != '-') |
| 1520 | return(0); |
| 1521 | /* extensions and private use subtags not checked */ |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1522 | return (1); |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1523 | |
| 1524 | region_m49: |
| 1525 | if (((nxt[1] >= '0') && (nxt[1] <= '9')) && |
| 1526 | ((nxt[2] >= '0') && (nxt[2] <= '9'))) { |
| 1527 | nxt += 3; |
| 1528 | goto region; |
| 1529 | } |
| 1530 | return(0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1533 | /************************************************************************ |
| 1534 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 1535 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | * * |
| 1537 | ************************************************************************/ |
| 1538 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 1539 | static xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 1540 | const xmlChar ** str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1541 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1542 | #ifdef SAX2 |
| 1543 | /** |
| 1544 | * nsPush: |
| 1545 | * @ctxt: an XML parser context |
| 1546 | * @prefix: the namespace prefix or NULL |
| 1547 | * @URL: the namespace name |
| 1548 | * |
| 1549 | * Pushes a new parser namespace on top of the ns stack |
| 1550 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 1551 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 1552 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1553 | */ |
| 1554 | static int |
| 1555 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 1556 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1557 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 1558 | int i; |
Daniel Veillard | 711b15d | 2012-10-25 19:23:26 +0800 | [diff] [blame] | 1559 | for (i = ctxt->nsNr - 2;i >= 0;i -= 2) { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1560 | if (ctxt->nsTab[i] == prefix) { |
| 1561 | /* in scope */ |
| 1562 | if (ctxt->nsTab[i + 1] == URL) |
| 1563 | return(-2); |
| 1564 | /* out of scope keep it */ |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1569 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 1570 | ctxt->nsMax = 10; |
| 1571 | ctxt->nsNr = 0; |
| 1572 | ctxt->nsTab = (const xmlChar **) |
| 1573 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 1574 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1575 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1576 | ctxt->nsMax = 0; |
| 1577 | return (-1); |
| 1578 | } |
| 1579 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1580 | const xmlChar ** tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1581 | ctxt->nsMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1582 | tmp = (const xmlChar **) xmlRealloc((char *) ctxt->nsTab, |
| 1583 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 1584 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1585 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1586 | ctxt->nsMax /= 2; |
| 1587 | return (-1); |
| 1588 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1589 | ctxt->nsTab = tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1590 | } |
| 1591 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 1592 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 1593 | return (ctxt->nsNr); |
| 1594 | } |
| 1595 | /** |
| 1596 | * nsPop: |
| 1597 | * @ctxt: an XML parser context |
| 1598 | * @nr: the number to pop |
| 1599 | * |
| 1600 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 1601 | * |
| 1602 | * Returns the number of namespaces removed |
| 1603 | */ |
| 1604 | static int |
| 1605 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 1606 | { |
| 1607 | int i; |
| 1608 | |
| 1609 | if (ctxt->nsTab == NULL) return(0); |
| 1610 | if (ctxt->nsNr < nr) { |
| 1611 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 1612 | nr = ctxt->nsNr; |
| 1613 | } |
| 1614 | if (ctxt->nsNr <= 0) |
| 1615 | return (0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1616 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1617 | for (i = 0;i < nr;i++) { |
| 1618 | ctxt->nsNr--; |
| 1619 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 1620 | } |
| 1621 | return(nr); |
| 1622 | } |
| 1623 | #endif |
| 1624 | |
| 1625 | static int |
| 1626 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 1627 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1628 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1629 | int maxatts; |
| 1630 | |
| 1631 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1632 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1633 | atts = (const xmlChar **) |
| 1634 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1635 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1636 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1637 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 1638 | if (attallocs == NULL) goto mem_error; |
| 1639 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1640 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1641 | } else if (nr + 5 > ctxt->maxatts) { |
| 1642 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1643 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 1644 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1645 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1646 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1647 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 1648 | (maxatts / 5) * sizeof(int)); |
| 1649 | if (attallocs == NULL) goto mem_error; |
| 1650 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1651 | ctxt->maxatts = maxatts; |
| 1652 | } |
| 1653 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1654 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1655 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1656 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1659 | /** |
| 1660 | * inputPush: |
| 1661 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1662 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1663 | * |
| 1664 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1665 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1666 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1667 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1668 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1669 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 1670 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1671 | if ((ctxt == NULL) || (value == NULL)) |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1672 | return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1673 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 1674 | ctxt->inputMax *= 2; |
| 1675 | ctxt->inputTab = |
| 1676 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 1677 | ctxt->inputMax * |
| 1678 | sizeof(ctxt->inputTab[0])); |
| 1679 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1680 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1681 | xmlFreeInputStream(value); |
| 1682 | ctxt->inputMax /= 2; |
| 1683 | value = NULL; |
| 1684 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1685 | } |
| 1686 | } |
| 1687 | ctxt->inputTab[ctxt->inputNr] = value; |
| 1688 | ctxt->input = value; |
| 1689 | return (ctxt->inputNr++); |
| 1690 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1691 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1692 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1693 | * @ctxt: an XML parser context |
| 1694 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1695 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1696 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1697 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1698 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1699 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1700 | inputPop(xmlParserCtxtPtr ctxt) |
| 1701 | { |
| 1702 | xmlParserInputPtr ret; |
| 1703 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1704 | if (ctxt == NULL) |
| 1705 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1706 | if (ctxt->inputNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1707 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1708 | ctxt->inputNr--; |
| 1709 | if (ctxt->inputNr > 0) |
| 1710 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 1711 | else |
| 1712 | ctxt->input = NULL; |
| 1713 | ret = ctxt->inputTab[ctxt->inputNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1714 | ctxt->inputTab[ctxt->inputNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1715 | return (ret); |
| 1716 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1717 | /** |
| 1718 | * nodePush: |
| 1719 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1720 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1721 | * |
| 1722 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1723 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1724 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1725 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1726 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1727 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1728 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1729 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1730 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1731 | xmlNodePtr *tmp; |
| 1732 | |
| 1733 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1734 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1735 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1736 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1737 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1738 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1739 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1740 | ctxt->nodeTab = tmp; |
| 1741 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1742 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1743 | if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) && |
| 1744 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1745 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1746 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1747 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1748 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1749 | return(-1); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1750 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1751 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1752 | ctxt->node = value; |
| 1753 | return (ctxt->nodeNr++); |
| 1754 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1755 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1756 | /** |
| 1757 | * nodePop: |
| 1758 | * @ctxt: an XML parser context |
| 1759 | * |
| 1760 | * Pops the top element node from the node stack |
| 1761 | * |
| 1762 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1763 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1764 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1765 | nodePop(xmlParserCtxtPtr ctxt) |
| 1766 | { |
| 1767 | xmlNodePtr ret; |
| 1768 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1769 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1770 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1771 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1772 | ctxt->nodeNr--; |
| 1773 | if (ctxt->nodeNr > 0) |
| 1774 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1775 | else |
| 1776 | ctxt->node = NULL; |
| 1777 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1778 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1779 | return (ret); |
| 1780 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1781 | |
| 1782 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1783 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1784 | * nameNsPush: |
| 1785 | * @ctxt: an XML parser context |
| 1786 | * @value: the element name |
| 1787 | * @prefix: the element prefix |
| 1788 | * @URI: the element namespace name |
| 1789 | * |
| 1790 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1791 | * |
| 1792 | * Returns -1 in case of error, the index in the stack otherwise |
| 1793 | */ |
| 1794 | static int |
| 1795 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1796 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1797 | { |
| 1798 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1799 | const xmlChar * *tmp; |
| 1800 | void **tmp2; |
| 1801 | ctxt->nameMax *= 2; |
| 1802 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1803 | ctxt->nameMax * |
| 1804 | sizeof(ctxt->nameTab[0])); |
| 1805 | if (tmp == NULL) { |
| 1806 | ctxt->nameMax /= 2; |
| 1807 | goto mem_error; |
| 1808 | } |
| 1809 | ctxt->nameTab = tmp; |
| 1810 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1811 | ctxt->nameMax * 3 * |
| 1812 | sizeof(ctxt->pushTab[0])); |
| 1813 | if (tmp2 == NULL) { |
| 1814 | ctxt->nameMax /= 2; |
| 1815 | goto mem_error; |
| 1816 | } |
| 1817 | ctxt->pushTab = tmp2; |
| 1818 | } |
| 1819 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1820 | ctxt->name = value; |
| 1821 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1822 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1823 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1824 | return (ctxt->nameNr++); |
| 1825 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1826 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1827 | return (-1); |
| 1828 | } |
| 1829 | /** |
| 1830 | * nameNsPop: |
| 1831 | * @ctxt: an XML parser context |
| 1832 | * |
| 1833 | * Pops the top element/prefix/URI name from the name stack |
| 1834 | * |
| 1835 | * Returns the name just removed |
| 1836 | */ |
| 1837 | static const xmlChar * |
| 1838 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1839 | { |
| 1840 | const xmlChar *ret; |
| 1841 | |
| 1842 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1843 | return (NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1844 | ctxt->nameNr--; |
| 1845 | if (ctxt->nameNr > 0) |
| 1846 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1847 | else |
| 1848 | ctxt->name = NULL; |
| 1849 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1850 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1851 | return (ret); |
| 1852 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1853 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1854 | |
| 1855 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1856 | * namePush: |
| 1857 | * @ctxt: an XML parser context |
| 1858 | * @value: the element name |
| 1859 | * |
| 1860 | * Pushes a new element name on top of the name stack |
| 1861 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1862 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1863 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1864 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1865 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1866 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1867 | if (ctxt == NULL) return (-1); |
| 1868 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1869 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1870 | const xmlChar * *tmp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1871 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1872 | ctxt->nameMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1873 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1874 | if (tmp == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1875 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1876 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1877 | ctxt->nameTab = tmp; |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1878 | ctxt->nameMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1879 | } |
| 1880 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1881 | ctxt->name = value; |
| 1882 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1883 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1884 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1885 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1886 | } |
| 1887 | /** |
| 1888 | * namePop: |
| 1889 | * @ctxt: an XML parser context |
| 1890 | * |
| 1891 | * Pops the top element name from the name stack |
| 1892 | * |
| 1893 | * Returns the name just removed |
| 1894 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1895 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1896 | namePop(xmlParserCtxtPtr ctxt) |
| 1897 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1898 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1899 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1900 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1901 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1902 | ctxt->nameNr--; |
| 1903 | if (ctxt->nameNr > 0) |
| 1904 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1905 | else |
| 1906 | ctxt->name = NULL; |
| 1907 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1908 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1909 | return (ret); |
| 1910 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1911 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1912 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1913 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1914 | int *tmp; |
| 1915 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1916 | ctxt->spaceMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1917 | tmp = (int *) xmlRealloc(ctxt->spaceTab, |
| 1918 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1919 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1920 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1921 | ctxt->spaceMax /=2; |
| 1922 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1923 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1924 | ctxt->spaceTab = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1925 | } |
| 1926 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1927 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1928 | return(ctxt->spaceNr++); |
| 1929 | } |
| 1930 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1931 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1932 | int ret; |
| 1933 | if (ctxt->spaceNr <= 0) return(0); |
| 1934 | ctxt->spaceNr--; |
| 1935 | if (ctxt->spaceNr > 0) |
| 1936 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1937 | else |
Daniel Veillard | dbcbbd2 | 2006-06-18 19:55:20 +0000 | [diff] [blame] | 1938 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1940 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1941 | return(ret); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * Macros for accessing the content. Those should be used only by the parser, |
| 1946 | * and not exported. |
| 1947 | * |
| 1948 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1949 | * use them |
| 1950 | * |
| 1951 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1952 | * To be used with extreme caution since operations consuming |
| 1953 | * characters may move the input buffer to a different location ! |
| 1954 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1955 | * This should be used internally by the parser |
| 1956 | * only to compare to ASCII values otherwise it would break when |
| 1957 | * running with UTF-8 encoding. |
| 1958 | * RAW same as CUR but in the input buffer, bypass any token |
| 1959 | * extraction that may have been done |
| 1960 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1961 | * to compare on ASCII based substring. |
| 1962 | * 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] | 1963 | * strings without newlines within the parser. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1964 | * 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] | 1965 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1967 | * |
| 1968 | * NEXT Skip to the next character, this does the proper decoding |
| 1969 | * 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] | 1970 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1971 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1972 | * to the number of xmlChars used for the encoding [0-5]. |
| 1973 | * CUR_SCHAR same but operate on a string instead of the context |
| 1974 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1975 | * the index |
| 1976 | * GROW, SHRINK handling of input buffers |
| 1977 | */ |
| 1978 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1979 | #define RAW (*ctxt->input->cur) |
| 1980 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1981 | #define NXT(val) ctxt->input->cur[(val)] |
| 1982 | #define CUR_PTR ctxt->input->cur |
| 1983 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1984 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1985 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1986 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1987 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1988 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1989 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1990 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1991 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1992 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1993 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1994 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1995 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1996 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1997 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1998 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1999 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 2000 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 2001 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2002 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2003 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2004 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2005 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2006 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 2007 | xmlPopInput(ctxt); \ |
| 2008 | } while (0) |
| 2009 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2010 | #define SKIPL(val) do { \ |
| 2011 | int skipl; \ |
| 2012 | for(skipl=0; skipl<val; skipl++) { \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2013 | if (*(ctxt->input->cur) == '\n') { \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2014 | ctxt->input->line++; ctxt->input->col = 1; \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2015 | } else ctxt->input->col++; \ |
| 2016 | ctxt->nbChars++; \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2017 | ctxt->input->cur++; \ |
| 2018 | } \ |
| 2019 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 2020 | if ((*ctxt->input->cur == 0) && \ |
| 2021 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 2022 | xmlPopInput(ctxt); \ |
| 2023 | } while (0) |
| 2024 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2025 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 2026 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 2027 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2028 | xmlSHRINK (ctxt); |
| 2029 | |
| 2030 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 2031 | xmlParserInputShrink(ctxt->input); |
| 2032 | if ((*ctxt->input->cur == 0) && |
| 2033 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2034 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2035 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2036 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2037 | #define GROW if ((ctxt->progressive == 0) && \ |
| 2038 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2039 | xmlGROW (ctxt); |
| 2040 | |
| 2041 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
Longstreth Jon | 190a0b8 | 2014-02-06 10:58:17 +0100 | [diff] [blame] | 2042 | unsigned long curEnd = ctxt->input->end - ctxt->input->cur; |
| 2043 | unsigned long curBase = ctxt->input->cur - ctxt->input->base; |
| 2044 | |
| 2045 | if (((curEnd > (unsigned long) XML_MAX_LOOKUP_LIMIT) || |
| 2046 | (curBase > (unsigned long) XML_MAX_LOOKUP_LIMIT)) && |
Patrick Gansterer | 9c8eaab | 2013-01-04 12:41:53 +0100 | [diff] [blame] | 2047 | ((ctxt->input->buf) && (ctxt->input->buf->readcallback != (xmlInputReadCallback) xmlNop)) && |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2048 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 2049 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
Daniel Veillard | 0df83ca | 2012-07-30 15:41:10 +0800 | [diff] [blame] | 2050 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2051 | } |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2052 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Daniel Veillard | 59df783 | 2010-02-02 10:24:01 +0100 | [diff] [blame] | 2053 | if ((ctxt->input->cur != NULL) && (*ctxt->input->cur == 0) && |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2054 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2055 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2056 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2057 | |
| 2058 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 2059 | |
| 2060 | #define NEXT xmlNextChar(ctxt) |
| 2061 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2062 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2063 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2064 | ctxt->input->cur++; \ |
| 2065 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2066 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2067 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 2068 | } |
| 2069 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2070 | #define NEXTL(l) do { \ |
| 2071 | if (*(ctxt->input->cur) == '\n') { \ |
| 2072 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 2073 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2074 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2075 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2076 | } while (0) |
| 2077 | |
| 2078 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 2079 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 2080 | |
| 2081 | #define COPY_BUF(l,b,i,v) \ |
| 2082 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2083 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2084 | |
| 2085 | /** |
| 2086 | * xmlSkipBlankChars: |
| 2087 | * @ctxt: the XML parser context |
| 2088 | * |
| 2089 | * skip all blanks character found at that point in the input streams. |
| 2090 | * It pops up finished entities in the process if allowable at that point. |
| 2091 | * |
| 2092 | * Returns the number of space chars skipped |
| 2093 | */ |
| 2094 | |
| 2095 | int |
| 2096 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2097 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2098 | |
| 2099 | /* |
| 2100 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 2101 | * the ASCII range. |
| 2102 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2103 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 2104 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2105 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2106 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2107 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2108 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2109 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2110 | if (*cur == '\n') { |
| 2111 | ctxt->input->line++; ctxt->input->col = 1; |
Juergen Keil | 5d4310a | 2014-08-07 16:28:09 +0800 | [diff] [blame] | 2112 | } else { |
| 2113 | ctxt->input->col++; |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2114 | } |
| 2115 | cur++; |
| 2116 | res++; |
| 2117 | if (*cur == 0) { |
| 2118 | ctxt->input->cur = cur; |
| 2119 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 2120 | cur = ctxt->input->cur; |
| 2121 | } |
| 2122 | } |
| 2123 | ctxt->input->cur = cur; |
| 2124 | } else { |
| 2125 | int cur; |
| 2126 | do { |
| 2127 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 2128 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2129 | NEXT; |
| 2130 | cur = CUR; |
| 2131 | res++; |
| 2132 | } |
| 2133 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 2134 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 2135 | xmlPopInput(ctxt); |
| 2136 | cur = CUR; |
| 2137 | } |
| 2138 | /* |
| 2139 | * Need to handle support of entities branching here |
| 2140 | */ |
| 2141 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 2142 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 2143 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2144 | return(res); |
| 2145 | } |
| 2146 | |
| 2147 | /************************************************************************ |
| 2148 | * * |
| 2149 | * Commodity functions to handle entities * |
| 2150 | * * |
| 2151 | ************************************************************************/ |
| 2152 | |
| 2153 | /** |
| 2154 | * xmlPopInput: |
| 2155 | * @ctxt: an XML parser context |
| 2156 | * |
| 2157 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 2158 | * pop it and return the next char. |
| 2159 | * |
| 2160 | * Returns the current xmlChar in the parser context |
| 2161 | */ |
| 2162 | xmlChar |
| 2163 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2164 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2165 | if (xmlParserDebugEntities) |
| 2166 | xmlGenericError(xmlGenericErrorContext, |
| 2167 | "Popping input %d\n", ctxt->inputNr); |
| 2168 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2169 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2170 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2171 | return(xmlPopInput(ctxt)); |
| 2172 | return(CUR); |
| 2173 | } |
| 2174 | |
| 2175 | /** |
| 2176 | * xmlPushInput: |
| 2177 | * @ctxt: an XML parser context |
| 2178 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 2179 | * |
| 2180 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 2181 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2182 | * 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] | 2183 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2184 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2185 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2186 | int ret; |
| 2187 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2188 | |
| 2189 | if (xmlParserDebugEntities) { |
| 2190 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 2191 | xmlGenericError(xmlGenericErrorContext, |
| 2192 | "%s(%d): ", ctxt->input->filename, |
| 2193 | ctxt->input->line); |
| 2194 | xmlGenericError(xmlGenericErrorContext, |
| 2195 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 2196 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2197 | ret = inputPush(ctxt, input); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2198 | if (ctxt->instate == XML_PARSER_EOF) |
| 2199 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2201 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | /** |
| 2205 | * xmlParseCharRef: |
| 2206 | * @ctxt: an XML parser context |
| 2207 | * |
| 2208 | * parse Reference declarations |
| 2209 | * |
| 2210 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2211 | * '&#x' [0-9a-fA-F]+ ';' |
| 2212 | * |
| 2213 | * [ WFC: Legal Character ] |
| 2214 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2215 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2216 | * |
| 2217 | * Returns the value parsed (as an int), 0 in case of error |
| 2218 | */ |
| 2219 | int |
| 2220 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2221 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2222 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2223 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2224 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2225 | /* |
| 2226 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2227 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2228 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2229 | (NXT(2) == 'x')) { |
| 2230 | SKIP(3); |
| 2231 | GROW; |
| 2232 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2233 | if (count++ > 20) { |
| 2234 | count = 0; |
| 2235 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2236 | if (ctxt->instate == XML_PARSER_EOF) |
| 2237 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2238 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2239 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2240 | val = val * 16 + (CUR - '0'); |
| 2241 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2242 | val = val * 16 + (CUR - 'a') + 10; |
| 2243 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2244 | val = val * 16 + (CUR - 'A') + 10; |
| 2245 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2246 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2247 | val = 0; |
| 2248 | break; |
| 2249 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2250 | if (val > 0x10FFFF) |
| 2251 | outofrange = val; |
| 2252 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2253 | NEXT; |
| 2254 | count++; |
| 2255 | } |
| 2256 | if (RAW == ';') { |
| 2257 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2258 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2259 | ctxt->nbChars ++; |
| 2260 | ctxt->input->cur++; |
| 2261 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2262 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2263 | SKIP(2); |
| 2264 | GROW; |
| 2265 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2266 | if (count++ > 20) { |
| 2267 | count = 0; |
| 2268 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2269 | if (ctxt->instate == XML_PARSER_EOF) |
| 2270 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2271 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2272 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2273 | val = val * 10 + (CUR - '0'); |
| 2274 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2275 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | val = 0; |
| 2277 | break; |
| 2278 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2279 | if (val > 0x10FFFF) |
| 2280 | outofrange = val; |
| 2281 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2282 | NEXT; |
| 2283 | count++; |
| 2284 | } |
| 2285 | if (RAW == ';') { |
| 2286 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2287 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2288 | ctxt->nbChars ++; |
| 2289 | ctxt->input->cur++; |
| 2290 | } |
| 2291 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2292 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * [ WFC: Legal Character ] |
| 2297 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2298 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2299 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2300 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2301 | return(val); |
| 2302 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2303 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2304 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2305 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2306 | } |
| 2307 | return(0); |
| 2308 | } |
| 2309 | |
| 2310 | /** |
| 2311 | * xmlParseStringCharRef: |
| 2312 | * @ctxt: an XML parser context |
| 2313 | * @str: a pointer to an index in the string |
| 2314 | * |
| 2315 | * parse Reference declarations, variant parsing from a string rather |
| 2316 | * than an an input flow. |
| 2317 | * |
| 2318 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2319 | * '&#x' [0-9a-fA-F]+ ';' |
| 2320 | * |
| 2321 | * [ WFC: Legal Character ] |
| 2322 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2323 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2324 | * |
| 2325 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2326 | * updated to the current value of the index |
| 2327 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2328 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2329 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2330 | const xmlChar *ptr; |
| 2331 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2332 | unsigned int val = 0; |
| 2333 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2334 | |
| 2335 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2336 | ptr = *str; |
| 2337 | cur = *ptr; |
| 2338 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2339 | ptr += 3; |
| 2340 | cur = *ptr; |
| 2341 | while (cur != ';') { /* Non input consuming loop */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2342 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | val = val * 16 + (cur - '0'); |
| 2344 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2345 | val = val * 16 + (cur - 'a') + 10; |
| 2346 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2347 | val = val * 16 + (cur - 'A') + 10; |
| 2348 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2349 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2350 | val = 0; |
| 2351 | break; |
| 2352 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2353 | if (val > 0x10FFFF) |
| 2354 | outofrange = val; |
| 2355 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2356 | ptr++; |
| 2357 | cur = *ptr; |
| 2358 | } |
| 2359 | if (cur == ';') |
| 2360 | ptr++; |
| 2361 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2362 | ptr += 2; |
| 2363 | cur = *ptr; |
| 2364 | while (cur != ';') { /* Non input consuming loops */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2365 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2366 | val = val * 10 + (cur - '0'); |
| 2367 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2368 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | val = 0; |
| 2370 | break; |
| 2371 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2372 | if (val > 0x10FFFF) |
| 2373 | outofrange = val; |
| 2374 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2375 | ptr++; |
| 2376 | cur = *ptr; |
| 2377 | } |
| 2378 | if (cur == ';') |
| 2379 | ptr++; |
| 2380 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2381 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2382 | return(0); |
| 2383 | } |
| 2384 | *str = ptr; |
| 2385 | |
| 2386 | /* |
| 2387 | * [ WFC: Legal Character ] |
| 2388 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2389 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2390 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2391 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2392 | return(val); |
| 2393 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2394 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2395 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2396 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2397 | } |
| 2398 | return(0); |
| 2399 | } |
| 2400 | |
| 2401 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2402 | * xmlNewBlanksWrapperInputStream: |
| 2403 | * @ctxt: an XML parser context |
| 2404 | * @entity: an Entity pointer |
| 2405 | * |
| 2406 | * Create a new input stream for wrapping |
| 2407 | * blanks around a PEReference |
| 2408 | * |
| 2409 | * Returns the new input stream or NULL |
| 2410 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2411 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2412 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2413 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2414 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2415 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2416 | xmlParserInputPtr input; |
| 2417 | xmlChar *buffer; |
| 2418 | size_t length; |
| 2419 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2420 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2421 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2422 | return(NULL); |
| 2423 | } |
| 2424 | if (xmlParserDebugEntities) |
| 2425 | xmlGenericError(xmlGenericErrorContext, |
| 2426 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2427 | input = xmlNewInputStream(ctxt); |
| 2428 | if (input == NULL) { |
| 2429 | return(NULL); |
| 2430 | } |
| 2431 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2432 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2433 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2434 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2435 | xmlFree(input); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2436 | return(NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2437 | } |
| 2438 | buffer [0] = ' '; |
| 2439 | buffer [1] = '%'; |
| 2440 | buffer [length-3] = ';'; |
| 2441 | buffer [length-2] = ' '; |
| 2442 | buffer [length-1] = 0; |
| 2443 | memcpy(buffer + 2, entity->name, length - 5); |
| 2444 | input->free = deallocblankswrapper; |
| 2445 | input->base = buffer; |
| 2446 | input->cur = buffer; |
| 2447 | input->length = length; |
| 2448 | input->end = &buffer[length]; |
| 2449 | return(input); |
| 2450 | } |
| 2451 | |
| 2452 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2453 | * xmlParserHandlePEReference: |
| 2454 | * @ctxt: the parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2455 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2456 | * [69] PEReference ::= '%' Name ';' |
| 2457 | * |
| 2458 | * [ WFC: No Recursion ] |
| 2459 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2460 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2461 | * |
| 2462 | * [ WFC: Entity Declared ] |
| 2463 | * In a document without any DTD, a document with only an internal DTD |
| 2464 | * subset which contains no parameter entity references, or a document |
| 2465 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2466 | * entity must precede any reference to it... |
| 2467 | * |
| 2468 | * [ VC: Entity Declared ] |
| 2469 | * In a document with an external subset or external parameter entities |
| 2470 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2471 | * must precede any reference to it... |
| 2472 | * |
| 2473 | * [ WFC: In DTD ] |
| 2474 | * Parameter-entity references may only appear in the DTD. |
| 2475 | * NOTE: misleading but this is handled. |
| 2476 | * |
| 2477 | * A PEReference may have been detected in the current input stream |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2478 | * the handling is done accordingly to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2479 | * http://www.w3.org/TR/REC-xml#entproc |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2480 | * i.e. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2481 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2482 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2483 | */ |
| 2484 | void |
| 2485 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2486 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2487 | xmlEntityPtr entity = NULL; |
| 2488 | xmlParserInputPtr input; |
| 2489 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2490 | if (RAW != '%') return; |
| 2491 | switch(ctxt->instate) { |
| 2492 | case XML_PARSER_CDATA_SECTION: |
| 2493 | return; |
| 2494 | case XML_PARSER_COMMENT: |
| 2495 | return; |
| 2496 | case XML_PARSER_START_TAG: |
| 2497 | return; |
| 2498 | case XML_PARSER_END_TAG: |
| 2499 | return; |
| 2500 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2501 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2502 | return; |
| 2503 | case XML_PARSER_PROLOG: |
| 2504 | case XML_PARSER_START: |
| 2505 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2506 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2507 | return; |
| 2508 | case XML_PARSER_ENTITY_DECL: |
| 2509 | case XML_PARSER_CONTENT: |
| 2510 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2511 | case XML_PARSER_PI: |
| 2512 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2513 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2514 | /* we just ignore it there */ |
| 2515 | return; |
| 2516 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2517 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2518 | return; |
| 2519 | case XML_PARSER_ENTITY_VALUE: |
| 2520 | /* |
| 2521 | * NOTE: in the case of entity values, we don't do the |
| 2522 | * substitution here since we need the literal |
| 2523 | * entity value to be able to save the internal |
| 2524 | * subset of the document. |
| 2525 | * This will be handled by xmlStringDecodeEntities |
| 2526 | */ |
| 2527 | return; |
| 2528 | case XML_PARSER_DTD: |
| 2529 | /* |
| 2530 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2531 | * In the internal DTD subset, parameter-entity references |
| 2532 | * can occur only where markup declarations can occur, not |
| 2533 | * within markup declarations. |
| 2534 | * In that case this is handled in xmlParseMarkupDecl |
| 2535 | */ |
| 2536 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2537 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2538 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2539 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2540 | break; |
| 2541 | case XML_PARSER_IGNORE: |
| 2542 | return; |
| 2543 | } |
| 2544 | |
| 2545 | NEXT; |
| 2546 | name = xmlParseName(ctxt); |
| 2547 | if (xmlParserDebugEntities) |
| 2548 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2549 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2550 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2551 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2552 | } else { |
| 2553 | if (RAW == ';') { |
| 2554 | NEXT; |
| 2555 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2556 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 2557 | if (ctxt->instate == XML_PARSER_EOF) |
| 2558 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2559 | if (entity == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2560 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2561 | /* |
| 2562 | * [ WFC: Entity Declared ] |
| 2563 | * In a document without any DTD, a document with only an |
| 2564 | * internal DTD subset which contains no parameter entity |
| 2565 | * references, or a document with "standalone='yes'", ... |
| 2566 | * ... The declaration of a parameter entity must precede |
| 2567 | * any reference to it... |
| 2568 | */ |
| 2569 | if ((ctxt->standalone == 1) || |
| 2570 | ((ctxt->hasExternalSubset == 0) && |
| 2571 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2572 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2573 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | } else { |
| 2575 | /* |
| 2576 | * [ VC: Entity Declared ] |
| 2577 | * In a document with an external subset or external |
| 2578 | * parameter entities with "standalone='no'", ... |
| 2579 | * ... The declaration of a parameter entity must precede |
| 2580 | * any reference to it... |
| 2581 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2582 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2583 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2584 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2585 | name, NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2586 | } else |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2587 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2588 | "PEReference: %%%s; not found\n", |
| 2589 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2590 | ctxt->valid = 0; |
| 2591 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2592 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2593 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2594 | if (xmlPushInput(ctxt, input) < 0) |
| 2595 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2596 | } else { |
| 2597 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2598 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2599 | xmlChar start[4]; |
| 2600 | xmlCharEncoding enc; |
| 2601 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2602 | /* |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2603 | * Note: external parameter entities will not be loaded, it |
| 2604 | * is not required for a non-validating parser, unless the |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2605 | * option of validating, or substituting entities were |
| 2606 | * given. Doing so is far more secure as the parser will |
| 2607 | * only process data coming from the document entity by |
| 2608 | * default. |
| 2609 | */ |
| 2610 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 2611 | ((ctxt->options & XML_PARSE_NOENT) == 0) && |
| 2612 | ((ctxt->options & XML_PARSE_DTDVALID) == 0) && |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2613 | ((ctxt->options & XML_PARSE_DTDLOAD) == 0) && |
| 2614 | ((ctxt->options & XML_PARSE_DTDATTR) == 0) && |
| 2615 | (ctxt->replaceEntities == 0) && |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2616 | (ctxt->validate == 0)) |
| 2617 | return; |
| 2618 | |
| 2619 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2620 | * handle the extra spaces added before and after |
| 2621 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2622 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2623 | */ |
| 2624 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2625 | if (xmlPushInput(ctxt, input) < 0) |
| 2626 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2627 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2628 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2629 | * Get the 4 first bytes and decode the charset |
| 2630 | * if enc != XML_CHAR_ENCODING_NONE |
| 2631 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2632 | * Note that, since we may have some non-UTF8 |
| 2633 | * encoding (like UTF16, bug 135229), the 'length' |
| 2634 | * is not known, but we can calculate based upon |
| 2635 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2636 | */ |
| 2637 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2638 | if (ctxt->instate == XML_PARSER_EOF) |
| 2639 | return; |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2640 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2641 | start[0] = RAW; |
| 2642 | start[1] = NXT(1); |
| 2643 | start[2] = NXT(2); |
| 2644 | start[3] = NXT(3); |
| 2645 | enc = xmlDetectCharEncoding(start, 4); |
| 2646 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2647 | xmlSwitchEncoding(ctxt, enc); |
| 2648 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2651 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2652 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2653 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2654 | xmlParseTextDecl(ctxt); |
| 2655 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2656 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2657 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2658 | "PEReference: %s is not a parameter entity\n", |
| 2659 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2663 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2664 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | /* |
| 2669 | * Macro used to grow the current buffer. |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2670 | * buffer##_size is expected to be a size_t |
| 2671 | * mem_error: is expected to handle memory allocation failures |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2672 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2673 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2674 | xmlChar *tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2675 | size_t new_size = buffer##_size * 2 + n; \ |
| 2676 | if (new_size < buffer##_size) goto mem_error; \ |
| 2677 | tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2678 | if (tmp == NULL) goto mem_error; \ |
| 2679 | buffer = tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2680 | buffer##_size = new_size; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2684 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2685 | * @ctxt: the parser context |
| 2686 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2687 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2688 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2689 | * @end: an end marker xmlChar, 0 if none |
| 2690 | * @end2: an end marker xmlChar, 0 if none |
| 2691 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2692 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2693 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2694 | * |
| 2695 | * [67] Reference ::= EntityRef | CharRef |
| 2696 | * |
| 2697 | * [69] PEReference ::= '%' Name ';' |
| 2698 | * |
| 2699 | * Returns A newly allocated string with the substitution done. The caller |
| 2700 | * must deallocate it ! |
| 2701 | */ |
| 2702 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2703 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2704 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | xmlChar *buffer = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2706 | size_t buffer_size = 0; |
| 2707 | size_t nbchars = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2708 | |
| 2709 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2710 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2711 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | xmlEntityPtr ent; |
| 2713 | int c,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2715 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2717 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2718 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2719 | if (((ctxt->depth > 40) && |
| 2720 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2721 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2722 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2723 | return(NULL); |
| 2724 | } |
| 2725 | |
| 2726 | /* |
| 2727 | * allocate a translation buffer. |
| 2728 | */ |
| 2729 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2730 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2731 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2732 | |
| 2733 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2734 | * 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] | 2735 | * we are operating on already parsed values. |
| 2736 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2737 | if (str < last) |
| 2738 | c = CUR_SCHAR(str, l); |
| 2739 | else |
| 2740 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2742 | (c != end2) && (c != end3)) { |
| 2743 | |
| 2744 | if (c == 0) break; |
| 2745 | if ((c == '&') && (str[1] == '#')) { |
| 2746 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2747 | if (val != 0) { |
| 2748 | COPY_BUF(0,buffer,nbchars,val); |
| 2749 | } |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2750 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2751 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2752 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2754 | if (xmlParserDebugEntities) |
| 2755 | xmlGenericError(xmlGenericErrorContext, |
| 2756 | "String decoding Entity Reference: %.30s\n", |
| 2757 | str); |
| 2758 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2759 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2760 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2761 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2762 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2763 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2764 | if ((ent != NULL) && |
| 2765 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2766 | if (ent->content != NULL) { |
| 2767 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2768 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2769 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2770 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2771 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2772 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2773 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2776 | ctxt->depth++; |
| 2777 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2778 | 0, 0, 0); |
| 2779 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2780 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2781 | if (rep != NULL) { |
| 2782 | current = rep; |
| 2783 | while (*current != 0) { /* non input consuming loop */ |
| 2784 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2785 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2786 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2787 | goto int_error; |
| 2788 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2789 | } |
| 2790 | } |
| 2791 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2792 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2793 | } |
| 2794 | } else if (ent != NULL) { |
| 2795 | int i = xmlStrlen(ent->name); |
| 2796 | const xmlChar *cur = ent->name; |
| 2797 | |
| 2798 | buffer[nbchars++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2799 | if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 5bd3c06 | 2011-12-16 18:53:35 +0800 | [diff] [blame] | 2800 | growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2801 | } |
| 2802 | for (;i > 0;i--) |
| 2803 | buffer[nbchars++] = *cur++; |
| 2804 | buffer[nbchars++] = ';'; |
| 2805 | } |
| 2806 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2807 | if (xmlParserDebugEntities) |
| 2808 | xmlGenericError(xmlGenericErrorContext, |
| 2809 | "String decoding PE Reference: %.30s\n", str); |
| 2810 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2811 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2812 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2813 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2814 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2815 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2816 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2817 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2818 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2819 | ctxt->depth++; |
| 2820 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2821 | 0, 0, 0); |
| 2822 | ctxt->depth--; |
| 2823 | if (rep != NULL) { |
| 2824 | current = rep; |
| 2825 | while (*current != 0) { /* non input consuming loop */ |
| 2826 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2827 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2828 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2829 | goto int_error; |
| 2830 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2834 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | } else { |
| 2838 | COPY_BUF(l,buffer,nbchars,c); |
| 2839 | str += l; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2840 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
| 2841 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2844 | if (str < last) |
| 2845 | c = CUR_SCHAR(str, l); |
| 2846 | else |
| 2847 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2848 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2849 | buffer[nbchars] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2850 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2851 | |
| 2852 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2853 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2854 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2855 | if (rep != NULL) |
| 2856 | xmlFree(rep); |
| 2857 | if (buffer != NULL) |
| 2858 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2859 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2862 | /** |
| 2863 | * xmlStringDecodeEntities: |
| 2864 | * @ctxt: the parser context |
| 2865 | * @str: the input string |
| 2866 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2867 | * @end: an end marker xmlChar, 0 if none |
| 2868 | * @end2: an end marker xmlChar, 0 if none |
| 2869 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2870 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2871 | * Takes a entity string content and process to do the adequate substitutions. |
| 2872 | * |
| 2873 | * [67] Reference ::= EntityRef | CharRef |
| 2874 | * |
| 2875 | * [69] PEReference ::= '%' Name ';' |
| 2876 | * |
| 2877 | * Returns A newly allocated string with the substitution done. The caller |
| 2878 | * must deallocate it ! |
| 2879 | */ |
| 2880 | xmlChar * |
| 2881 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2882 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2883 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2884 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2885 | end, end2, end3)); |
| 2886 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2887 | |
| 2888 | /************************************************************************ |
| 2889 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2890 | * Commodity functions, cleanup needed ? * |
| 2891 | * * |
| 2892 | ************************************************************************/ |
| 2893 | |
| 2894 | /** |
| 2895 | * areBlanks: |
| 2896 | * @ctxt: an XML parser context |
| 2897 | * @str: a xmlChar * |
| 2898 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2899 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2900 | * |
| 2901 | * Is this a sequence of blank chars that one can ignore ? |
| 2902 | * |
| 2903 | * Returns 1 if ignorable 0 otherwise. |
| 2904 | */ |
| 2905 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2906 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2907 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2908 | int i, ret; |
| 2909 | xmlNodePtr lastChild; |
| 2910 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2911 | /* |
| 2912 | * Don't spend time trying to differentiate them, the same callback is |
| 2913 | * used ! |
| 2914 | */ |
| 2915 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2916 | return(0); |
| 2917 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2918 | /* |
| 2919 | * Check for xml:space value. |
| 2920 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2921 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2922 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2923 | return(0); |
| 2924 | |
| 2925 | /* |
| 2926 | * Check that the string is made of blanks |
| 2927 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2928 | if (blank_chars == 0) { |
| 2929 | for (i = 0;i < len;i++) |
| 2930 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2931 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2932 | |
| 2933 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2934 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2935 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2936 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2937 | if (ctxt->myDoc != NULL) { |
| 2938 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2939 | if (ret == 0) return(1); |
| 2940 | if (ret == 1) return(0); |
| 2941 | } |
| 2942 | |
| 2943 | /* |
| 2944 | * Otherwise, heuristic :-\ |
| 2945 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2946 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2947 | if ((ctxt->node->children == NULL) && |
| 2948 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2949 | |
| 2950 | lastChild = xmlGetLastChild(ctxt->node); |
| 2951 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2952 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2953 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2954 | } else if (xmlNodeIsText(lastChild)) |
| 2955 | return(0); |
| 2956 | else if ((ctxt->node->children != NULL) && |
| 2957 | (xmlNodeIsText(ctxt->node->children))) |
| 2958 | return(0); |
| 2959 | return(1); |
| 2960 | } |
| 2961 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2962 | /************************************************************************ |
| 2963 | * * |
| 2964 | * Extra stuff for namespace support * |
| 2965 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2966 | * * |
| 2967 | ************************************************************************/ |
| 2968 | |
| 2969 | /** |
| 2970 | * xmlSplitQName: |
| 2971 | * @ctxt: an XML parser context |
| 2972 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2973 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2974 | * |
| 2975 | * parse an UTF8 encoded XML qualified name string |
| 2976 | * |
| 2977 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2978 | * |
| 2979 | * [NS 6] Prefix ::= NCName |
| 2980 | * |
| 2981 | * [NS 7] LocalPart ::= NCName |
| 2982 | * |
| 2983 | * Returns the local part, and prefix is updated |
| 2984 | * to get the Prefix if any. |
| 2985 | */ |
| 2986 | |
| 2987 | xmlChar * |
| 2988 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2989 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2990 | xmlChar *buffer = NULL; |
| 2991 | int len = 0; |
| 2992 | int max = XML_MAX_NAMELEN; |
| 2993 | xmlChar *ret = NULL; |
| 2994 | const xmlChar *cur = name; |
| 2995 | int c; |
| 2996 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2997 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2998 | *prefix = NULL; |
| 2999 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3000 | if (cur == NULL) return(NULL); |
| 3001 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3002 | #ifndef XML_XML_NAMESPACE |
| 3003 | /* xml: prefix is not really a namespace */ |
| 3004 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 3005 | (cur[2] == 'l') && (cur[3] == ':')) |
| 3006 | return(xmlStrdup(name)); |
| 3007 | #endif |
| 3008 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3009 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3010 | if (cur[0] == ':') |
| 3011 | return(xmlStrdup(name)); |
| 3012 | |
| 3013 | c = *cur++; |
| 3014 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 3015 | buf[len++] = c; |
| 3016 | c = *cur++; |
| 3017 | } |
| 3018 | if (len >= max) { |
| 3019 | /* |
| 3020 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3021 | * for the processing speed. |
| 3022 | */ |
| 3023 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3024 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3025 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3027 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3028 | return(NULL); |
| 3029 | } |
| 3030 | memcpy(buffer, buf, len); |
| 3031 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 3032 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3033 | xmlChar *tmp; |
| 3034 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3035 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3036 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3038 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3039 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3040 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3041 | return(NULL); |
| 3042 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3043 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | } |
| 3045 | buffer[len++] = c; |
| 3046 | c = *cur++; |
| 3047 | } |
| 3048 | buffer[len] = 0; |
| 3049 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3050 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3051 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3052 | if (buffer != NULL) |
| 3053 | xmlFree(buffer); |
| 3054 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3055 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3056 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3057 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | if (buffer == NULL) |
| 3059 | ret = xmlStrndup(buf, len); |
| 3060 | else { |
| 3061 | ret = buffer; |
| 3062 | buffer = NULL; |
| 3063 | max = XML_MAX_NAMELEN; |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3068 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3069 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3070 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 3071 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3072 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3073 | len = 0; |
| 3074 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3075 | /* |
| 3076 | * Check that the first character is proper to start |
| 3077 | * a new name |
| 3078 | */ |
| 3079 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 3080 | ((c >= 0x41) && (c <= 0x5A)) || |
| 3081 | (c == '_') || (c == ':'))) { |
| 3082 | int l; |
| 3083 | int first = CUR_SCHAR(cur, l); |
| 3084 | |
| 3085 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3086 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3087 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3088 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3089 | } |
| 3090 | } |
| 3091 | cur++; |
| 3092 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3093 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 3094 | buf[len++] = c; |
| 3095 | c = *cur++; |
| 3096 | } |
| 3097 | if (len >= max) { |
| 3098 | /* |
| 3099 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3100 | * for the processing speed. |
| 3101 | */ |
| 3102 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3103 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3104 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3105 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3106 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3107 | return(NULL); |
| 3108 | } |
| 3109 | memcpy(buffer, buf, len); |
| 3110 | while (c != 0) { /* tested bigname2.xml */ |
| 3111 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3112 | xmlChar *tmp; |
| 3113 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3114 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3115 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3116 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3117 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3118 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3119 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3120 | return(NULL); |
| 3121 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3122 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3123 | } |
| 3124 | buffer[len++] = c; |
| 3125 | c = *cur++; |
| 3126 | } |
| 3127 | buffer[len] = 0; |
| 3128 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3129 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3130 | if (buffer == NULL) |
| 3131 | ret = xmlStrndup(buf, len); |
| 3132 | else { |
| 3133 | ret = buffer; |
| 3134 | } |
| 3135 | } |
| 3136 | |
| 3137 | return(ret); |
| 3138 | } |
| 3139 | |
| 3140 | /************************************************************************ |
| 3141 | * * |
| 3142 | * The parser itself * |
| 3143 | * Relates to http://www.w3.org/TR/REC-xml * |
| 3144 | * * |
| 3145 | ************************************************************************/ |
| 3146 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3147 | /************************************************************************ |
| 3148 | * * |
| 3149 | * Routines to parse Name, NCName and NmToken * |
| 3150 | * * |
| 3151 | ************************************************************************/ |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3152 | #ifdef DEBUG |
| 3153 | static unsigned long nbParseName = 0; |
| 3154 | static unsigned long nbParseNmToken = 0; |
| 3155 | static unsigned long nbParseNCName = 0; |
| 3156 | static unsigned long nbParseNCNameComplex = 0; |
| 3157 | static unsigned long nbParseNameComplex = 0; |
| 3158 | static unsigned long nbParseStringName = 0; |
| 3159 | #endif |
| 3160 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3161 | /* |
| 3162 | * The two following functions are related to the change of accepted |
| 3163 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 3164 | * They correspond to the modified production [4] and the new production [4a] |
| 3165 | * changes in that revision. Also note that the macros used for the |
| 3166 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 3167 | * anymore. |
| 3168 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 3169 | * new XML_PARSE_OLD10 option is given to the parser. |
| 3170 | */ |
| 3171 | static int |
| 3172 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 3173 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3174 | /* |
| 3175 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3176 | * Update 5 of XML-1.0 |
| 3177 | */ |
| 3178 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3179 | (((c >= 'a') && (c <= 'z')) || |
| 3180 | ((c >= 'A') && (c <= 'Z')) || |
| 3181 | (c == '_') || (c == ':') || |
| 3182 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3183 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3184 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3185 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3186 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3187 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3188 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3189 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3190 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3191 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3192 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3193 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3194 | return(1); |
| 3195 | } else { |
| 3196 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3197 | return(1); |
| 3198 | } |
| 3199 | return(0); |
| 3200 | } |
| 3201 | |
| 3202 | static int |
| 3203 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 3204 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3205 | /* |
| 3206 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3207 | * Update 5 of XML-1.0 |
| 3208 | */ |
| 3209 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3210 | (((c >= 'a') && (c <= 'z')) || |
| 3211 | ((c >= 'A') && (c <= 'Z')) || |
| 3212 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3213 | (c == '_') || (c == ':') || |
| 3214 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3215 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3216 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3217 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3218 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3219 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3220 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3221 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3222 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3223 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3224 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3225 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3226 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3227 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3228 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3229 | return(1); |
| 3230 | } else { |
| 3231 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3232 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3233 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3234 | (IS_COMBINING(c)) || |
| 3235 | (IS_EXTENDER(c))) |
| 3236 | return(1); |
| 3237 | } |
| 3238 | return(0); |
| 3239 | } |
| 3240 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3241 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3242 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3243 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3244 | static const xmlChar * |
| 3245 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3246 | int len = 0, l; |
| 3247 | int c; |
| 3248 | int count = 0; |
| 3249 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3250 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3251 | nbParseNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3252 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3253 | |
| 3254 | /* |
| 3255 | * Handler for more complex cases |
| 3256 | */ |
| 3257 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3258 | if (ctxt->instate == XML_PARSER_EOF) |
| 3259 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3260 | c = CUR_CHAR(l); |
| 3261 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3262 | /* |
| 3263 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3264 | * Update 5 of XML-1.0 |
| 3265 | */ |
| 3266 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3267 | (!(((c >= 'a') && (c <= 'z')) || |
| 3268 | ((c >= 'A') && (c <= 'Z')) || |
| 3269 | (c == '_') || (c == ':') || |
| 3270 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3271 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3272 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3273 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3274 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3275 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3276 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3277 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3278 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3279 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3280 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3281 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3282 | return(NULL); |
| 3283 | } |
| 3284 | len += l; |
| 3285 | NEXTL(l); |
| 3286 | c = CUR_CHAR(l); |
| 3287 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3288 | (((c >= 'a') && (c <= 'z')) || |
| 3289 | ((c >= 'A') && (c <= 'Z')) || |
| 3290 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3291 | (c == '_') || (c == ':') || |
| 3292 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3293 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3294 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3295 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3296 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3297 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3298 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3299 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3300 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3301 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3302 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3303 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3304 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3305 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3306 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3307 | )) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3308 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3309 | count = 0; |
| 3310 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3311 | if (ctxt->instate == XML_PARSER_EOF) |
| 3312 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3313 | } |
| 3314 | len += l; |
| 3315 | NEXTL(l); |
| 3316 | c = CUR_CHAR(l); |
| 3317 | } |
| 3318 | } else { |
| 3319 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3320 | (!IS_LETTER(c) && (c != '_') && |
| 3321 | (c != ':'))) { |
| 3322 | return(NULL); |
| 3323 | } |
| 3324 | len += l; |
| 3325 | NEXTL(l); |
| 3326 | c = CUR_CHAR(l); |
| 3327 | |
| 3328 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3329 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3330 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3331 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3332 | (IS_COMBINING(c)) || |
| 3333 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3334 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3335 | count = 0; |
| 3336 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3337 | if (ctxt->instate == XML_PARSER_EOF) |
| 3338 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3339 | } |
| 3340 | len += l; |
| 3341 | NEXTL(l); |
| 3342 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3343 | if (c == 0) { |
| 3344 | count = 0; |
| 3345 | GROW; |
| 3346 | if (ctxt->instate == XML_PARSER_EOF) |
| 3347 | return(NULL); |
| 3348 | c = CUR_CHAR(l); |
| 3349 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3350 | } |
| 3351 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3352 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3353 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3354 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3355 | return(NULL); |
| 3356 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3357 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3358 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3359 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3360 | } |
| 3361 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3362 | /** |
| 3363 | * xmlParseName: |
| 3364 | * @ctxt: an XML parser context |
| 3365 | * |
| 3366 | * parse an XML name. |
| 3367 | * |
| 3368 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3369 | * CombiningChar | Extender |
| 3370 | * |
| 3371 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3372 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3373 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3374 | * |
| 3375 | * Returns the Name parsed or NULL |
| 3376 | */ |
| 3377 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3378 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3379 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3380 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3381 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | int count = 0; |
| 3383 | |
| 3384 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3385 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3386 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3387 | nbParseName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3388 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3389 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3390 | /* |
| 3391 | * Accelerator for simple ASCII names |
| 3392 | */ |
| 3393 | in = ctxt->input->cur; |
| 3394 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3395 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3396 | (*in == '_') || (*in == ':')) { |
| 3397 | in++; |
| 3398 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3399 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3400 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3401 | (*in == '_') || (*in == '-') || |
| 3402 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3403 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3404 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3405 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3406 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3407 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3408 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3409 | return(NULL); |
| 3410 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3411 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3412 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3413 | ctxt->nbChars += count; |
| 3414 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3415 | if (ret == NULL) |
| 3416 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3417 | return(ret); |
| 3418 | } |
| 3419 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3420 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3421 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3422 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3423 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3424 | static const xmlChar * |
| 3425 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3426 | int len = 0, l; |
| 3427 | int c; |
| 3428 | int count = 0; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3429 | 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] | 3430 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3431 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3432 | nbParseNCNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3433 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3434 | |
| 3435 | /* |
| 3436 | * Handler for more complex cases |
| 3437 | */ |
| 3438 | GROW; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3439 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3440 | c = CUR_CHAR(l); |
| 3441 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3442 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3443 | return(NULL); |
| 3444 | } |
| 3445 | |
| 3446 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3447 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3448 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3449 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3450 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3451 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3452 | return(NULL); |
| 3453 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3454 | count = 0; |
| 3455 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3456 | if (ctxt->instate == XML_PARSER_EOF) |
| 3457 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3458 | } |
| 3459 | len += l; |
| 3460 | NEXTL(l); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3461 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3462 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3463 | if (c == 0) { |
| 3464 | count = 0; |
| 3465 | GROW; |
| 3466 | if (ctxt->instate == XML_PARSER_EOF) |
| 3467 | return(NULL); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3468 | end = ctxt->input->cur; |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3469 | c = CUR_CHAR(l); |
| 3470 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3471 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3472 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3473 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3474 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3475 | return(NULL); |
| 3476 | } |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3477 | return(xmlDictLookup(ctxt->dict, end - len, len)); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | /** |
| 3481 | * xmlParseNCName: |
| 3482 | * @ctxt: an XML parser context |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3483 | * @len: length of the string parsed |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3484 | * |
| 3485 | * parse an XML name. |
| 3486 | * |
| 3487 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3488 | * CombiningChar | Extender |
| 3489 | * |
| 3490 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3491 | * |
| 3492 | * Returns the Name parsed or NULL |
| 3493 | */ |
| 3494 | |
| 3495 | static const xmlChar * |
| 3496 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 3497 | const xmlChar *in; |
| 3498 | const xmlChar *ret; |
| 3499 | int count = 0; |
| 3500 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3501 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3502 | nbParseNCName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3503 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3504 | |
| 3505 | /* |
| 3506 | * Accelerator for simple ASCII names |
| 3507 | */ |
| 3508 | in = ctxt->input->cur; |
| 3509 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3510 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3511 | (*in == '_')) { |
| 3512 | in++; |
| 3513 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3514 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3515 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3516 | (*in == '_') || (*in == '-') || |
| 3517 | (*in == '.')) |
| 3518 | in++; |
| 3519 | if ((*in > 0) && (*in < 0x80)) { |
| 3520 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3521 | if ((count > 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 | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3526 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3527 | ctxt->input->cur = in; |
| 3528 | ctxt->nbChars += count; |
| 3529 | ctxt->input->col += count; |
| 3530 | if (ret == NULL) { |
| 3531 | xmlErrMemory(ctxt, NULL); |
| 3532 | } |
| 3533 | return(ret); |
| 3534 | } |
| 3535 | } |
| 3536 | return(xmlParseNCNameComplex(ctxt)); |
| 3537 | } |
| 3538 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3539 | /** |
| 3540 | * xmlParseNameAndCompare: |
| 3541 | * @ctxt: an XML parser context |
| 3542 | * |
| 3543 | * parse an XML name and compares for match |
| 3544 | * (specialized for endtag parsing) |
| 3545 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3546 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3547 | * and the name for mismatch |
| 3548 | */ |
| 3549 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3550 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3551 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3552 | register const xmlChar *cmp = other; |
| 3553 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3554 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3555 | |
| 3556 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3557 | if (ctxt->instate == XML_PARSER_EOF) |
| 3558 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3559 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3560 | in = ctxt->input->cur; |
| 3561 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3562 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3563 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3564 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3565 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3566 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3567 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3568 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3569 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3570 | } |
| 3571 | /* failure (or end of input buffer), check with full function */ |
| 3572 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3573 | /* strings coming from the dictionnary direct compare possible */ |
| 3574 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3575 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3576 | } |
| 3577 | return ret; |
| 3578 | } |
| 3579 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3580 | /** |
| 3581 | * xmlParseStringName: |
| 3582 | * @ctxt: an XML parser context |
| 3583 | * @str: a pointer to the string pointer (IN/OUT) |
| 3584 | * |
| 3585 | * parse an XML name. |
| 3586 | * |
| 3587 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3588 | * CombiningChar | Extender |
| 3589 | * |
| 3590 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3591 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3592 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3593 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3594 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3595 | * is updated to the current location in the string. |
| 3596 | */ |
| 3597 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3598 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3599 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3600 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3601 | const xmlChar *cur = *str; |
| 3602 | int len = 0, l; |
| 3603 | int c; |
| 3604 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3605 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3606 | nbParseStringName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3607 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3608 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3609 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3610 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3611 | return(NULL); |
| 3612 | } |
| 3613 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3614 | COPY_BUF(l,buf,len,c); |
| 3615 | cur += l; |
| 3616 | c = CUR_SCHAR(cur, l); |
| 3617 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3618 | COPY_BUF(l,buf,len,c); |
| 3619 | cur += l; |
| 3620 | c = CUR_SCHAR(cur, l); |
| 3621 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3622 | /* |
| 3623 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3624 | * for the processing speed. |
| 3625 | */ |
| 3626 | xmlChar *buffer; |
| 3627 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3628 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3629 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3630 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3631 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3632 | return(NULL); |
| 3633 | } |
| 3634 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3635 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3636 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3637 | xmlChar *tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3638 | |
| 3639 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3640 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3641 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3642 | xmlFree(buffer); |
| 3643 | return(NULL); |
| 3644 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3645 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3646 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3647 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3648 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3649 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3650 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3651 | return(NULL); |
| 3652 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3653 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3654 | } |
| 3655 | COPY_BUF(l,buffer,len,c); |
| 3656 | cur += l; |
| 3657 | c = CUR_SCHAR(cur, l); |
| 3658 | } |
| 3659 | buffer[len] = 0; |
| 3660 | *str = cur; |
| 3661 | return(buffer); |
| 3662 | } |
| 3663 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3664 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3665 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3666 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3667 | return(NULL); |
| 3668 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3669 | *str = cur; |
| 3670 | return(xmlStrndup(buf, len)); |
| 3671 | } |
| 3672 | |
| 3673 | /** |
| 3674 | * xmlParseNmtoken: |
| 3675 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3676 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3677 | * parse an XML Nmtoken. |
| 3678 | * |
| 3679 | * [7] Nmtoken ::= (NameChar)+ |
| 3680 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3681 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3682 | * |
| 3683 | * Returns the Nmtoken parsed or NULL |
| 3684 | */ |
| 3685 | |
| 3686 | xmlChar * |
| 3687 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3688 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3689 | int len = 0, l; |
| 3690 | int c; |
| 3691 | int count = 0; |
| 3692 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3693 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3694 | nbParseNmToken++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3695 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3696 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3697 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3698 | if (ctxt->instate == XML_PARSER_EOF) |
| 3699 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3700 | c = CUR_CHAR(l); |
| 3701 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3702 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3703 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3704 | count = 0; |
| 3705 | GROW; |
| 3706 | } |
| 3707 | COPY_BUF(l,buf,len,c); |
| 3708 | NEXTL(l); |
| 3709 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3710 | if (c == 0) { |
| 3711 | count = 0; |
| 3712 | GROW; |
| 3713 | if (ctxt->instate == XML_PARSER_EOF) |
| 3714 | return(NULL); |
| 3715 | c = CUR_CHAR(l); |
| 3716 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3717 | if (len >= XML_MAX_NAMELEN) { |
| 3718 | /* |
| 3719 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3720 | * for the processing speed. |
| 3721 | */ |
| 3722 | xmlChar *buffer; |
| 3723 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3724 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3725 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3726 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3727 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3728 | return(NULL); |
| 3729 | } |
| 3730 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3731 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3732 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3733 | count = 0; |
| 3734 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3735 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3736 | xmlFree(buffer); |
| 3737 | return(NULL); |
| 3738 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3739 | } |
| 3740 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3741 | xmlChar *tmp; |
| 3742 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3743 | if ((max > XML_MAX_NAME_LENGTH) && |
| 3744 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3745 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3746 | xmlFree(buffer); |
| 3747 | return(NULL); |
| 3748 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3749 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3750 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3751 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3752 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3753 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3754 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3755 | return(NULL); |
| 3756 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3757 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3758 | } |
| 3759 | COPY_BUF(l,buffer,len,c); |
| 3760 | NEXTL(l); |
| 3761 | c = CUR_CHAR(l); |
| 3762 | } |
| 3763 | buffer[len] = 0; |
| 3764 | return(buffer); |
| 3765 | } |
| 3766 | } |
| 3767 | if (len == 0) |
| 3768 | return(NULL); |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3769 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3770 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3771 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3772 | return(NULL); |
| 3773 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3774 | return(xmlStrndup(buf, len)); |
| 3775 | } |
| 3776 | |
| 3777 | /** |
| 3778 | * xmlParseEntityValue: |
| 3779 | * @ctxt: an XML parser context |
| 3780 | * @orig: if non-NULL store a copy of the original entity value |
| 3781 | * |
| 3782 | * parse a value for ENTITY declarations |
| 3783 | * |
| 3784 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3785 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3786 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3787 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3788 | */ |
| 3789 | |
| 3790 | xmlChar * |
| 3791 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3792 | xmlChar *buf = NULL; |
| 3793 | int len = 0; |
| 3794 | int size = XML_PARSER_BUFFER_SIZE; |
| 3795 | int c, l; |
| 3796 | xmlChar stop; |
| 3797 | xmlChar *ret = NULL; |
| 3798 | const xmlChar *cur = NULL; |
| 3799 | xmlParserInputPtr input; |
| 3800 | |
| 3801 | if (RAW == '"') stop = '"'; |
| 3802 | else if (RAW == '\'') stop = '\''; |
| 3803 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3804 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3805 | return(NULL); |
| 3806 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3807 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3808 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3809 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3810 | return(NULL); |
| 3811 | } |
| 3812 | |
| 3813 | /* |
| 3814 | * The content of the entity definition is copied in a buffer. |
| 3815 | */ |
| 3816 | |
| 3817 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3818 | input = ctxt->input; |
| 3819 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3820 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3821 | xmlFree(buf); |
| 3822 | return(NULL); |
| 3823 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3824 | NEXT; |
| 3825 | c = CUR_CHAR(l); |
| 3826 | /* |
| 3827 | * NOTE: 4.4.5 Included in Literal |
| 3828 | * When a parameter entity reference appears in a literal entity |
| 3829 | * value, ... a single or double quote character in the replacement |
| 3830 | * text is always treated as a normal data character and will not |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3831 | * terminate the literal. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3832 | * In practice it means we stop the loop only when back at parsing |
| 3833 | * the initial entity and the quote is found |
| 3834 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3835 | while (((IS_CHAR(c)) && ((c != stop) || /* checked */ |
| 3836 | (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3837 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3838 | xmlChar *tmp; |
| 3839 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3840 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3841 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3842 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3843 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3844 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3845 | return(NULL); |
| 3846 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3847 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3848 | } |
| 3849 | COPY_BUF(l,buf,len,c); |
| 3850 | NEXTL(l); |
| 3851 | /* |
| 3852 | * Pop-up of finished entities. |
| 3853 | */ |
| 3854 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3855 | xmlPopInput(ctxt); |
| 3856 | |
| 3857 | GROW; |
| 3858 | c = CUR_CHAR(l); |
| 3859 | if (c == 0) { |
| 3860 | GROW; |
| 3861 | c = CUR_CHAR(l); |
| 3862 | } |
| 3863 | } |
| 3864 | buf[len] = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3865 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3866 | xmlFree(buf); |
| 3867 | return(NULL); |
| 3868 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3869 | |
| 3870 | /* |
| 3871 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3872 | * reference constructs. Note Charref will be handled in |
| 3873 | * xmlStringDecodeEntities() |
| 3874 | */ |
| 3875 | cur = buf; |
| 3876 | while (*cur != 0) { /* non input consuming */ |
| 3877 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3878 | xmlChar *name; |
| 3879 | xmlChar tmp = *cur; |
| 3880 | |
| 3881 | cur++; |
| 3882 | name = xmlParseStringName(ctxt, &cur); |
| 3883 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3884 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3885 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3886 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3887 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3888 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3889 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3890 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | } |
| 3892 | if (name != NULL) |
| 3893 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3894 | if (*cur == 0) |
| 3895 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3896 | } |
| 3897 | cur++; |
| 3898 | } |
| 3899 | |
| 3900 | /* |
| 3901 | * Then PEReference entities are substituted. |
| 3902 | */ |
| 3903 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3904 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3905 | xmlFree(buf); |
| 3906 | } else { |
| 3907 | NEXT; |
| 3908 | /* |
| 3909 | * NOTE: 4.4.7 Bypassed |
| 3910 | * When a general entity reference appears in the EntityValue in |
| 3911 | * an entity declaration, it is bypassed and left as is. |
| 3912 | * so XML_SUBSTITUTE_REF is not set here. |
| 3913 | */ |
| 3914 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3915 | 0, 0, 0); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3916 | if (orig != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3917 | *orig = buf; |
| 3918 | else |
| 3919 | xmlFree(buf); |
| 3920 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3921 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3922 | return(ret); |
| 3923 | } |
| 3924 | |
| 3925 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3926 | * xmlParseAttValueComplex: |
| 3927 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3928 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3929 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3930 | * |
| 3931 | * parse a value for an attribute, this is the fallback function |
| 3932 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3933 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3934 | * |
| 3935 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3936 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3937 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3938 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3939 | xmlChar limit = 0; |
| 3940 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3941 | xmlChar *rep = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3942 | size_t len = 0; |
| 3943 | size_t buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3944 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3945 | xmlChar *current = NULL; |
| 3946 | xmlEntityPtr ent; |
| 3947 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3948 | if (NXT(0) == '"') { |
| 3949 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3950 | limit = '"'; |
| 3951 | NEXT; |
| 3952 | } else if (NXT(0) == '\'') { |
| 3953 | limit = '\''; |
| 3954 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3955 | NEXT; |
| 3956 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3957 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3958 | return(NULL); |
| 3959 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3960 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3961 | /* |
| 3962 | * allocate a translation buffer. |
| 3963 | */ |
| 3964 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3965 | buf = (xmlChar *) xmlMallocAtomic(buf_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3966 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3967 | |
| 3968 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3969 | * 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] | 3970 | */ |
| 3971 | c = CUR_CHAR(l); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3972 | while (((NXT(0) != limit) && /* checked */ |
| 3973 | (IS_CHAR(c)) && (c != '<')) && |
| 3974 | (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3975 | /* |
| 3976 | * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE |
| 3977 | * special option is given |
| 3978 | */ |
| 3979 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 3980 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3981 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3982 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3983 | goto mem_error; |
| 3984 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3985 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3986 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3987 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3988 | if (NXT(1) == '#') { |
| 3989 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3990 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3991 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3992 | if (ctxt->replaceEntities) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3993 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3994 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3995 | } |
| 3996 | buf[len++] = '&'; |
| 3997 | } else { |
| 3998 | /* |
| 3999 | * The reparsing will be done in xmlStringGetNodeList() |
| 4000 | * called by the attribute() function in SAX.c |
| 4001 | */ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4002 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4003 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 4004 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4005 | buf[len++] = '&'; |
| 4006 | buf[len++] = '#'; |
| 4007 | buf[len++] = '3'; |
| 4008 | buf[len++] = '8'; |
| 4009 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4010 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 4011 | } else if (val != 0) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4012 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4013 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4014 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4015 | len += xmlCopyChar(0, &buf[len], val); |
| 4016 | } |
| 4017 | } else { |
| 4018 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 4019 | ctxt->nbentities++; |
| 4020 | if (ent != NULL) |
| 4021 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4022 | if ((ent != NULL) && |
| 4023 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4024 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4025 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4026 | } |
| 4027 | if ((ctxt->replaceEntities == 0) && |
| 4028 | (ent->content[0] == '&')) { |
| 4029 | buf[len++] = '&'; |
| 4030 | buf[len++] = '#'; |
| 4031 | buf[len++] = '3'; |
| 4032 | buf[len++] = '8'; |
| 4033 | buf[len++] = ';'; |
| 4034 | } else { |
| 4035 | buf[len++] = ent->content[0]; |
| 4036 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4037 | } else if ((ent != NULL) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4038 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4039 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 4040 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4041 | XML_SUBSTITUTE_REF, |
| 4042 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4043 | if (rep != NULL) { |
| 4044 | current = rep; |
| 4045 | while (*current != 0) { /* non input consuming */ |
Daniel Veillard | 283d502 | 2009-08-25 17:18:39 +0200 | [diff] [blame] | 4046 | if ((*current == 0xD) || (*current == 0xA) || |
| 4047 | (*current == 0x9)) { |
| 4048 | buf[len++] = 0x20; |
| 4049 | current++; |
| 4050 | } else |
| 4051 | buf[len++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4052 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4053 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4054 | } |
| 4055 | } |
| 4056 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4057 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4058 | } |
| 4059 | } else { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4060 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4061 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4062 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4063 | if (ent->content != NULL) |
| 4064 | buf[len++] = ent->content[0]; |
| 4065 | } |
| 4066 | } else if (ent != NULL) { |
| 4067 | int i = xmlStrlen(ent->name); |
| 4068 | const xmlChar *cur = ent->name; |
| 4069 | |
| 4070 | /* |
| 4071 | * This may look absurd but is needed to detect |
| 4072 | * entities problems |
| 4073 | */ |
| 4074 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4075 | (ent->content != NULL) && (ent->checked == 0)) { |
| 4076 | unsigned long oldnbent = ctxt->nbentities; |
| 4077 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4078 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4079 | XML_SUBSTITUTE_REF, 0, 0, 0); |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4080 | |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4081 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4082 | if (rep != NULL) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4083 | if (xmlStrchr(rep, '<')) |
| 4084 | ent->checked |= 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4085 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4086 | rep = NULL; |
| 4087 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | /* |
| 4091 | * Just output the reference |
| 4092 | */ |
| 4093 | buf[len++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4094 | while (len + i + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4095 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4096 | } |
| 4097 | for (;i > 0;i--) |
| 4098 | buf[len++] = *cur++; |
| 4099 | buf[len++] = ';'; |
| 4100 | } |
| 4101 | } |
| 4102 | } else { |
| 4103 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4104 | if ((len != 0) || (!normalize)) { |
| 4105 | if ((!normalize) || (!in_space)) { |
| 4106 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4107 | while (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4108 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4109 | } |
| 4110 | } |
| 4111 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4112 | } |
| 4113 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4114 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4115 | COPY_BUF(l,buf,len,c); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4116 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4117 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4118 | } |
| 4119 | } |
| 4120 | NEXTL(l); |
| 4121 | } |
| 4122 | GROW; |
| 4123 | c = CUR_CHAR(l); |
| 4124 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4125 | if (ctxt->instate == XML_PARSER_EOF) |
| 4126 | goto error; |
| 4127 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4128 | if ((in_space) && (normalize)) { |
Daniel Veillard | 6a36fbe | 2012-10-29 10:39:55 +0800 | [diff] [blame] | 4129 | while ((len > 0) && (buf[len - 1] == 0x20)) len--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4130 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4131 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4133 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4134 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 4135 | if ((c != 0) && (!IS_CHAR(c))) { |
| 4136 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 4137 | "invalid character in attribute value\n"); |
| 4138 | } else { |
| 4139 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 4140 | "AttValue: ' expected\n"); |
| 4141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4142 | } else |
| 4143 | NEXT; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4144 | |
| 4145 | /* |
| 4146 | * There we potentially risk an overflow, don't allow attribute value of |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4147 | * length more than INT_MAX it is a very reasonnable assumption ! |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4148 | */ |
| 4149 | if (len >= INT_MAX) { |
| 4150 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4151 | "AttValue length too long\n"); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4152 | goto mem_error; |
| 4153 | } |
| 4154 | |
| 4155 | if (attlen != NULL) *attlen = (int) len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4156 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4157 | |
| 4158 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4159 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4160 | error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4161 | if (buf != NULL) |
| 4162 | xmlFree(buf); |
| 4163 | if (rep != NULL) |
| 4164 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4165 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4169 | * xmlParseAttValue: |
| 4170 | * @ctxt: an XML parser context |
| 4171 | * |
| 4172 | * parse a value for an attribute |
| 4173 | * Note: the parser won't do substitution of entities here, this |
| 4174 | * will be handled later in xmlStringGetNodeList |
| 4175 | * |
| 4176 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 4177 | * "'" ([^<&'] | Reference)* "'" |
| 4178 | * |
| 4179 | * 3.3.3 Attribute-Value Normalization: |
| 4180 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4181 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4182 | * - a character reference is processed by appending the referenced |
| 4183 | * character to the attribute value |
| 4184 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4185 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4186 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 4187 | * appending #x20 to the normalized value, except that only a single |
| 4188 | * #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] | 4189 | * parsed entity or the literal entity value of an internal parsed entity |
| 4190 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4191 | * If the declared value is not CDATA, then the XML processor must further |
| 4192 | * process the normalized attribute value by discarding any leading and |
| 4193 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4194 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4195 | * All attributes for which no declaration has been read should be treated |
| 4196 | * by a non-validating parser as if declared CDATA. |
| 4197 | * |
| 4198 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 4199 | */ |
| 4200 | |
| 4201 | |
| 4202 | xmlChar * |
| 4203 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 4204 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4205 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | * xmlParseSystemLiteral: |
| 4210 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4211 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4212 | * parse an XML Literal |
| 4213 | * |
| 4214 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 4215 | * |
| 4216 | * Returns the SystemLiteral parsed or NULL |
| 4217 | */ |
| 4218 | |
| 4219 | xmlChar * |
| 4220 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 4221 | xmlChar *buf = NULL; |
| 4222 | int len = 0; |
| 4223 | int size = XML_PARSER_BUFFER_SIZE; |
| 4224 | int cur, l; |
| 4225 | xmlChar stop; |
| 4226 | int state = ctxt->instate; |
| 4227 | int count = 0; |
| 4228 | |
| 4229 | SHRINK; |
| 4230 | if (RAW == '"') { |
| 4231 | NEXT; |
| 4232 | stop = '"'; |
| 4233 | } else if (RAW == '\'') { |
| 4234 | NEXT; |
| 4235 | stop = '\''; |
| 4236 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4237 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4238 | return(NULL); |
| 4239 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4240 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4241 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4242 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4243 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4244 | return(NULL); |
| 4245 | } |
| 4246 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 4247 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4248 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4249 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4250 | xmlChar *tmp; |
| 4251 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4252 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4253 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4254 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral"); |
| 4255 | xmlFree(buf); |
| 4256 | ctxt->instate = (xmlParserInputState) state; |
| 4257 | return(NULL); |
| 4258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4259 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4260 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4261 | if (tmp == NULL) { |
| 4262 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4263 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4264 | ctxt->instate = (xmlParserInputState) state; |
| 4265 | return(NULL); |
| 4266 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4267 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4268 | } |
| 4269 | count++; |
| 4270 | if (count > 50) { |
| 4271 | GROW; |
| 4272 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4273 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4274 | xmlFree(buf); |
| 4275 | return(NULL); |
| 4276 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4277 | } |
| 4278 | COPY_BUF(l,buf,len,cur); |
| 4279 | NEXTL(l); |
| 4280 | cur = CUR_CHAR(l); |
| 4281 | if (cur == 0) { |
| 4282 | GROW; |
| 4283 | SHRINK; |
| 4284 | cur = CUR_CHAR(l); |
| 4285 | } |
| 4286 | } |
| 4287 | buf[len] = 0; |
| 4288 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4289 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4290 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4291 | } else { |
| 4292 | NEXT; |
| 4293 | } |
| 4294 | return(buf); |
| 4295 | } |
| 4296 | |
| 4297 | /** |
| 4298 | * xmlParsePubidLiteral: |
| 4299 | * @ctxt: an XML parser context |
| 4300 | * |
| 4301 | * parse an XML public literal |
| 4302 | * |
| 4303 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 4304 | * |
| 4305 | * Returns the PubidLiteral parsed or NULL. |
| 4306 | */ |
| 4307 | |
| 4308 | xmlChar * |
| 4309 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 4310 | xmlChar *buf = NULL; |
| 4311 | int len = 0; |
| 4312 | int size = XML_PARSER_BUFFER_SIZE; |
| 4313 | xmlChar cur; |
| 4314 | xmlChar stop; |
| 4315 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4316 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4317 | |
| 4318 | SHRINK; |
| 4319 | if (RAW == '"') { |
| 4320 | NEXT; |
| 4321 | stop = '"'; |
| 4322 | } else if (RAW == '\'') { |
| 4323 | NEXT; |
| 4324 | stop = '\''; |
| 4325 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4326 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4327 | return(NULL); |
| 4328 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4329 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4331 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4332 | return(NULL); |
| 4333 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4334 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4335 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4336 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4337 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4338 | xmlChar *tmp; |
| 4339 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4340 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4341 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4342 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID"); |
| 4343 | xmlFree(buf); |
| 4344 | return(NULL); |
| 4345 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4346 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4347 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4348 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4349 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4350 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4351 | return(NULL); |
| 4352 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4353 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4354 | } |
| 4355 | buf[len++] = cur; |
| 4356 | count++; |
| 4357 | if (count > 50) { |
| 4358 | GROW; |
| 4359 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4360 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4361 | xmlFree(buf); |
| 4362 | return(NULL); |
| 4363 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4364 | } |
| 4365 | NEXT; |
| 4366 | cur = CUR; |
| 4367 | if (cur == 0) { |
| 4368 | GROW; |
| 4369 | SHRINK; |
| 4370 | cur = CUR; |
| 4371 | } |
| 4372 | } |
| 4373 | buf[len] = 0; |
| 4374 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4375 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | } else { |
| 4377 | NEXT; |
| 4378 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4379 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4380 | return(buf); |
| 4381 | } |
| 4382 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4383 | static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4384 | |
| 4385 | /* |
| 4386 | * used for the test in the inner loop of the char data testing |
| 4387 | */ |
| 4388 | static const unsigned char test_char_data[256] = { |
| 4389 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4390 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 4391 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4392 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4393 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 4394 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 4395 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 4396 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 4397 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 4398 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 4399 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 4400 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 4401 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4402 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4403 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4404 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4405 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4406 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4407 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4408 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4409 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4410 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4411 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4412 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4413 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4414 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4415 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4416 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4417 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4418 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4419 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4420 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4421 | }; |
| 4422 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4423 | /** |
| 4424 | * xmlParseCharData: |
| 4425 | * @ctxt: an XML parser context |
| 4426 | * @cdata: int indicating whether we are within a CDATA section |
| 4427 | * |
| 4428 | * parse a CharData section. |
| 4429 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4430 | * |
| 4431 | * The right angle bracket (>) may be represented using the string ">", |
| 4432 | * and must, for compatibility, be escaped using ">" or a character |
| 4433 | * reference when it appears in the string "]]>" in content, when that |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4434 | * string is not marking the end of a CDATA section. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4435 | * |
| 4436 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4437 | */ |
| 4438 | |
| 4439 | void |
| 4440 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4441 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4442 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4443 | int line = ctxt->input->line; |
| 4444 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4445 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4446 | |
| 4447 | SHRINK; |
| 4448 | GROW; |
| 4449 | /* |
| 4450 | * Accelerated common case where input don't need to be |
| 4451 | * modified before passing it to the handler. |
| 4452 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4453 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4454 | in = ctxt->input->cur; |
| 4455 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4456 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4457 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4458 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4459 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4460 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4461 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4462 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4463 | goto get_more_space; |
| 4464 | } |
| 4465 | if (*in == '<') { |
| 4466 | nbchar = in - ctxt->input->cur; |
| 4467 | if (nbchar > 0) { |
| 4468 | const xmlChar *tmp = ctxt->input->cur; |
| 4469 | ctxt->input->cur = in; |
| 4470 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4471 | if ((ctxt->sax != NULL) && |
| 4472 | (ctxt->sax->ignorableWhitespace != |
| 4473 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4474 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4475 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4476 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4477 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4478 | } else { |
| 4479 | if (ctxt->sax->characters != NULL) |
| 4480 | ctxt->sax->characters(ctxt->userData, |
| 4481 | tmp, nbchar); |
| 4482 | if (*ctxt->space == -1) |
| 4483 | *ctxt->space = -2; |
| 4484 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4485 | } else if ((ctxt->sax != NULL) && |
| 4486 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4487 | ctxt->sax->characters(ctxt->userData, |
| 4488 | tmp, nbchar); |
| 4489 | } |
| 4490 | } |
| 4491 | return; |
| 4492 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4493 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4494 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4495 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4496 | while (test_char_data[*in]) { |
| 4497 | in++; |
| 4498 | ccol++; |
| 4499 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4500 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4501 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4502 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4503 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4504 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4505 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4506 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4507 | } |
| 4508 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4509 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4510 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4511 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4512 | return; |
| 4513 | } |
| 4514 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4515 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4516 | goto get_more; |
| 4517 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4518 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4519 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4520 | if ((ctxt->sax != NULL) && |
| 4521 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4522 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4523 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4524 | const xmlChar *tmp = ctxt->input->cur; |
| 4525 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4526 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4527 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
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 | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4538 | line = ctxt->input->line; |
| 4539 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4540 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4541 | if (ctxt->sax->characters != NULL) |
| 4542 | ctxt->sax->characters(ctxt->userData, |
| 4543 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4544 | line = ctxt->input->line; |
| 4545 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4546 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4547 | /* something really bad happened in the SAX callback */ |
| 4548 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4549 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4550 | } |
| 4551 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4552 | if (*in == 0xD) { |
| 4553 | in++; |
| 4554 | if (*in == 0xA) { |
| 4555 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4556 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4557 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4558 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4559 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4560 | in--; |
| 4561 | } |
| 4562 | if (*in == '<') { |
| 4563 | return; |
| 4564 | } |
| 4565 | if (*in == '&') { |
| 4566 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4567 | } |
| 4568 | SHRINK; |
| 4569 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4570 | if (ctxt->instate == XML_PARSER_EOF) |
| 4571 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4572 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4573 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4574 | nbchar = 0; |
| 4575 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4576 | ctxt->input->line = line; |
| 4577 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4578 | xmlParseCharDataComplex(ctxt, cdata); |
| 4579 | } |
| 4580 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4581 | /** |
| 4582 | * xmlParseCharDataComplex: |
| 4583 | * @ctxt: an XML parser context |
| 4584 | * @cdata: int indicating whether we are within a CDATA section |
| 4585 | * |
| 4586 | * parse a CharData section.this is the fallback function |
| 4587 | * of xmlParseCharData() when the parsing requires handling |
| 4588 | * of non-ASCII characters. |
| 4589 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4590 | static void |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4591 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4592 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4593 | int nbchar = 0; |
| 4594 | int cur, l; |
| 4595 | int count = 0; |
| 4596 | |
| 4597 | SHRINK; |
| 4598 | GROW; |
| 4599 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4600 | while ((cur != '<') && /* checked */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4601 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4602 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4603 | if ((cur == ']') && (NXT(1) == ']') && |
| 4604 | (NXT(2) == '>')) { |
| 4605 | if (cdata) break; |
| 4606 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4607 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4608 | } |
| 4609 | } |
| 4610 | COPY_BUF(l,buf,nbchar,cur); |
| 4611 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4612 | buf[nbchar] = 0; |
| 4613 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4614 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4615 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4616 | */ |
| 4617 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4618 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4619 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4620 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4621 | buf, nbchar); |
| 4622 | } else { |
| 4623 | if (ctxt->sax->characters != NULL) |
| 4624 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4625 | if ((ctxt->sax->characters != |
| 4626 | ctxt->sax->ignorableWhitespace) && |
| 4627 | (*ctxt->space == -1)) |
| 4628 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4629 | } |
| 4630 | } |
| 4631 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4632 | /* something really bad happened in the SAX callback */ |
| 4633 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4634 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4635 | } |
| 4636 | count++; |
| 4637 | if (count > 50) { |
| 4638 | GROW; |
| 4639 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4640 | if (ctxt->instate == XML_PARSER_EOF) |
| 4641 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4642 | } |
| 4643 | NEXTL(l); |
| 4644 | cur = CUR_CHAR(l); |
| 4645 | } |
| 4646 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4647 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4648 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4649 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4650 | */ |
| 4651 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4652 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4653 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4654 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4655 | } else { |
| 4656 | if (ctxt->sax->characters != NULL) |
| 4657 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4658 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4659 | (*ctxt->space == -1)) |
| 4660 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4661 | } |
| 4662 | } |
| 4663 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4664 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4665 | /* Generate the error and skip the offending character */ |
| 4666 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4667 | "PCDATA invalid Char value %d\n", |
| 4668 | cur); |
| 4669 | NEXTL(l); |
| 4670 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | /** |
| 4674 | * xmlParseExternalID: |
| 4675 | * @ctxt: an XML parser context |
| 4676 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4677 | * @strict: indicate whether we should restrict parsing to only |
| 4678 | * production [75], see NOTE below |
| 4679 | * |
| 4680 | * Parse an External ID or a Public ID |
| 4681 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4682 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4683 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4684 | * |
| 4685 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4686 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4687 | * |
| 4688 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4689 | * |
| 4690 | * Returns the function returns SystemLiteral and in the second |
| 4691 | * case publicID receives PubidLiteral, is strict is off |
| 4692 | * it is possible to return NULL and have publicID set. |
| 4693 | */ |
| 4694 | |
| 4695 | xmlChar * |
| 4696 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4697 | xmlChar *URI = NULL; |
| 4698 | |
| 4699 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4700 | |
| 4701 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4702 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4703 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4704 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4705 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4706 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4707 | } |
| 4708 | SKIP_BLANKS; |
| 4709 | URI = xmlParseSystemLiteral(ctxt); |
| 4710 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4711 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4712 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4713 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4714 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4715 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4716 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4717 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4718 | } |
| 4719 | SKIP_BLANKS; |
| 4720 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4721 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4722 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4723 | } |
| 4724 | if (strict) { |
| 4725 | /* |
| 4726 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4727 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4728 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4729 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4731 | } |
| 4732 | } else { |
| 4733 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4734 | * We handle [83] so we return immediately, if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4735 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4736 | * point of view that's a nice mess. |
| 4737 | */ |
| 4738 | const xmlChar *ptr; |
| 4739 | GROW; |
| 4740 | |
| 4741 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4742 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4743 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4744 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4745 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4746 | } |
| 4747 | SKIP_BLANKS; |
| 4748 | URI = xmlParseSystemLiteral(ctxt); |
| 4749 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4750 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | return(URI); |
| 4754 | } |
| 4755 | |
| 4756 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4757 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4758 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4759 | * @buf: the already parsed part of the buffer |
| 4760 | * @len: number of bytes filles in the buffer |
| 4761 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | * |
| 4763 | * Skip an XML (SGML) comment <!-- .... --> |
| 4764 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4765 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4766 | * 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] | 4767 | * |
| 4768 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4769 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4770 | static void |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4771 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, |
| 4772 | size_t len, size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4773 | int q, ql; |
| 4774 | int r, rl; |
| 4775 | int cur, l; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4776 | size_t count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4777 | int inputid; |
| 4778 | |
| 4779 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4780 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4781 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4782 | len = 0; |
| 4783 | size = XML_PARSER_BUFFER_SIZE; |
| 4784 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4785 | if (buf == NULL) { |
| 4786 | xmlErrMemory(ctxt, NULL); |
| 4787 | return; |
| 4788 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4789 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4790 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4791 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4792 | if (q == 0) |
| 4793 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4794 | if (!IS_CHAR(q)) { |
| 4795 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4796 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4797 | q); |
| 4798 | xmlFree (buf); |
| 4799 | return; |
| 4800 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4801 | NEXTL(ql); |
| 4802 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4803 | if (r == 0) |
| 4804 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4805 | if (!IS_CHAR(r)) { |
| 4806 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4807 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4808 | q); |
| 4809 | xmlFree (buf); |
| 4810 | return; |
| 4811 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4812 | NEXTL(rl); |
| 4813 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4814 | if (cur == 0) |
| 4815 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4816 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4817 | ((cur != '>') || |
| 4818 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4819 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4820 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4821 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4822 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4823 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4824 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4825 | "Comment too big found", NULL); |
| 4826 | xmlFree (buf); |
| 4827 | return; |
| 4828 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4829 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4830 | xmlChar *new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4831 | size_t new_size; |
| 4832 | |
| 4833 | new_size = size * 2; |
| 4834 | new_buf = (xmlChar *) xmlRealloc(buf, new_size); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4835 | if (new_buf == NULL) { |
| 4836 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4837 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4838 | return; |
| 4839 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4840 | buf = new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4841 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4842 | } |
| 4843 | COPY_BUF(ql,buf,len,q); |
| 4844 | q = r; |
| 4845 | ql = rl; |
| 4846 | r = cur; |
| 4847 | rl = l; |
| 4848 | |
| 4849 | count++; |
| 4850 | if (count > 50) { |
| 4851 | GROW; |
| 4852 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4853 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4854 | xmlFree(buf); |
| 4855 | return; |
| 4856 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4857 | } |
| 4858 | NEXTL(l); |
| 4859 | cur = CUR_CHAR(l); |
| 4860 | if (cur == 0) { |
| 4861 | SHRINK; |
| 4862 | GROW; |
| 4863 | cur = CUR_CHAR(l); |
| 4864 | } |
| 4865 | } |
| 4866 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4867 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4868 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4869 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4870 | } else if (!IS_CHAR(cur)) { |
| 4871 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4872 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4873 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4874 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4875 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4876 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4877 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4878 | } |
| 4879 | NEXT; |
| 4880 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4881 | (!ctxt->disableSAX)) |
| 4882 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4883 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4884 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4885 | return; |
| 4886 | not_terminated: |
| 4887 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4888 | "Comment not terminated\n", NULL); |
| 4889 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4890 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4891 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4892 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4893 | /** |
| 4894 | * xmlParseComment: |
| 4895 | * @ctxt: an XML parser context |
| 4896 | * |
| 4897 | * Skip an XML (SGML) comment <!-- .... --> |
| 4898 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4899 | * must not occur within comments. " |
| 4900 | * |
| 4901 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4902 | */ |
| 4903 | void |
| 4904 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4905 | xmlChar *buf = NULL; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4906 | size_t size = XML_PARSER_BUFFER_SIZE; |
| 4907 | size_t len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4908 | xmlParserInputState state; |
| 4909 | const xmlChar *in; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4910 | size_t nbchar = 0; |
| 4911 | int ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4912 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4913 | |
| 4914 | /* |
| 4915 | * Check that there is a comment right here. |
| 4916 | */ |
| 4917 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4918 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4919 | state = ctxt->instate; |
| 4920 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4921 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4922 | SKIP(4); |
| 4923 | SHRINK; |
| 4924 | GROW; |
| 4925 | |
| 4926 | /* |
| 4927 | * Accelerated common case where input don't need to be |
| 4928 | * modified before passing it to the handler. |
| 4929 | */ |
| 4930 | in = ctxt->input->cur; |
| 4931 | do { |
| 4932 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4933 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4934 | ctxt->input->line++; ctxt->input->col = 1; |
| 4935 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4936 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4937 | } |
| 4938 | get_more: |
| 4939 | ccol = ctxt->input->col; |
| 4940 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4941 | ((*in >= 0x20) && (*in < '-')) || |
| 4942 | (*in == 0x09)) { |
| 4943 | in++; |
| 4944 | ccol++; |
| 4945 | } |
| 4946 | ctxt->input->col = ccol; |
| 4947 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4948 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4949 | ctxt->input->line++; ctxt->input->col = 1; |
| 4950 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4951 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4952 | goto get_more; |
| 4953 | } |
| 4954 | nbchar = in - ctxt->input->cur; |
| 4955 | /* |
| 4956 | * save current set of data |
| 4957 | */ |
| 4958 | if (nbchar > 0) { |
| 4959 | if ((ctxt->sax != NULL) && |
| 4960 | (ctxt->sax->comment != NULL)) { |
| 4961 | if (buf == NULL) { |
| 4962 | if ((*in == '-') && (in[1] == '-')) |
| 4963 | size = nbchar + 1; |
| 4964 | else |
| 4965 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4966 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4967 | if (buf == NULL) { |
| 4968 | xmlErrMemory(ctxt, NULL); |
| 4969 | ctxt->instate = state; |
| 4970 | return; |
| 4971 | } |
| 4972 | len = 0; |
| 4973 | } else if (len + nbchar + 1 >= size) { |
| 4974 | xmlChar *new_buf; |
| 4975 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4976 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4977 | size * sizeof(xmlChar)); |
| 4978 | if (new_buf == NULL) { |
| 4979 | xmlFree (buf); |
| 4980 | xmlErrMemory(ctxt, NULL); |
| 4981 | ctxt->instate = state; |
| 4982 | return; |
| 4983 | } |
| 4984 | buf = new_buf; |
| 4985 | } |
| 4986 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4987 | len += nbchar; |
| 4988 | buf[len] = 0; |
| 4989 | } |
| 4990 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4991 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4992 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4993 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4994 | "Comment too big found", NULL); |
| 4995 | xmlFree (buf); |
| 4996 | return; |
| 4997 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4998 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4999 | if (*in == 0xA) { |
| 5000 | in++; |
| 5001 | ctxt->input->line++; ctxt->input->col = 1; |
| 5002 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5003 | if (*in == 0xD) { |
| 5004 | in++; |
| 5005 | if (*in == 0xA) { |
| 5006 | ctxt->input->cur = in; |
| 5007 | in++; |
| 5008 | ctxt->input->line++; ctxt->input->col = 1; |
| 5009 | continue; /* while */ |
| 5010 | } |
| 5011 | in--; |
| 5012 | } |
| 5013 | SHRINK; |
| 5014 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5015 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5016 | xmlFree(buf); |
| 5017 | return; |
| 5018 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5019 | in = ctxt->input->cur; |
| 5020 | if (*in == '-') { |
| 5021 | if (in[1] == '-') { |
| 5022 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 5023 | if (ctxt->input->id != inputid) { |
| 5024 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5025 | "comment doesn't start and stop in the same entity\n"); |
| 5026 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5027 | SKIP(3); |
| 5028 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 5029 | (!ctxt->disableSAX)) { |
| 5030 | if (buf != NULL) |
| 5031 | ctxt->sax->comment(ctxt->userData, buf); |
| 5032 | else |
| 5033 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 5034 | } |
| 5035 | if (buf != NULL) |
| 5036 | xmlFree(buf); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5037 | if (ctxt->instate != XML_PARSER_EOF) |
| 5038 | ctxt->instate = state; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5039 | return; |
| 5040 | } |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5041 | if (buf != NULL) { |
| 5042 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5043 | "Double hyphen within comment: " |
| 5044 | "<!--%.50s\n", |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5045 | buf); |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5046 | } else |
| 5047 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5048 | "Double hyphen within comment\n", NULL); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5049 | in++; |
| 5050 | ctxt->input->col++; |
| 5051 | } |
| 5052 | in++; |
| 5053 | ctxt->input->col++; |
| 5054 | goto get_more; |
| 5055 | } |
| 5056 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 5057 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 5058 | ctxt->instate = state; |
| 5059 | return; |
| 5060 | } |
| 5061 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5062 | |
| 5063 | /** |
| 5064 | * xmlParsePITarget: |
| 5065 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5066 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5067 | * parse the name of a PI |
| 5068 | * |
| 5069 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 5070 | * |
| 5071 | * Returns the PITarget name or NULL |
| 5072 | */ |
| 5073 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5074 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5075 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5076 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5077 | |
| 5078 | name = xmlParseName(ctxt); |
| 5079 | if ((name != NULL) && |
| 5080 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 5081 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 5082 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 5083 | int i; |
| 5084 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 5085 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5086 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5087 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5088 | return(name); |
| 5089 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5090 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5091 | return(name); |
| 5092 | } |
| 5093 | for (i = 0;;i++) { |
| 5094 | if (xmlW3CPIs[i] == NULL) break; |
| 5095 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 5096 | return(name); |
| 5097 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5098 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 5099 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 5100 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5101 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5102 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5103 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5104 | "colons are forbidden from PI names '%s'\n", name, NULL, NULL); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5105 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5106 | return(name); |
| 5107 | } |
| 5108 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5109 | #ifdef LIBXML_CATALOG_ENABLED |
| 5110 | /** |
| 5111 | * xmlParseCatalogPI: |
| 5112 | * @ctxt: an XML parser context |
| 5113 | * @catalog: the PI value string |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5114 | * |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5115 | * parse an XML Catalog Processing Instruction. |
| 5116 | * |
| 5117 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 5118 | * |
| 5119 | * Occurs only if allowed by the user and if happening in the Misc |
| 5120 | * part of the document before any doctype informations |
| 5121 | * This will add the given catalog to the parsing context in order |
| 5122 | * to be used if there is a resolution need further down in the document |
| 5123 | */ |
| 5124 | |
| 5125 | static void |
| 5126 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 5127 | xmlChar *URL = NULL; |
| 5128 | const xmlChar *tmp, *base; |
| 5129 | xmlChar marker; |
| 5130 | |
| 5131 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5132 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5133 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 5134 | goto error; |
| 5135 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5136 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5137 | if (*tmp != '=') { |
| 5138 | return; |
| 5139 | } |
| 5140 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5141 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5142 | marker = *tmp; |
| 5143 | if ((marker != '\'') && (marker != '"')) |
| 5144 | goto error; |
| 5145 | tmp++; |
| 5146 | base = tmp; |
| 5147 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 5148 | if (*tmp == 0) |
| 5149 | goto error; |
| 5150 | URL = xmlStrndup(base, tmp - base); |
| 5151 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5152 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5153 | if (*tmp != 0) |
| 5154 | goto error; |
| 5155 | |
| 5156 | if (URL != NULL) { |
| 5157 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 5158 | xmlFree(URL); |
| 5159 | } |
| 5160 | return; |
| 5161 | |
| 5162 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5163 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 5164 | "Catalog PI syntax error: %s\n", |
| 5165 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5166 | if (URL != NULL) |
| 5167 | xmlFree(URL); |
| 5168 | } |
| 5169 | #endif |
| 5170 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5171 | /** |
| 5172 | * xmlParsePI: |
| 5173 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5174 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5175 | * parse an XML Processing Instruction. |
| 5176 | * |
| 5177 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 5178 | * |
| 5179 | * The processing is transfered to SAX once parsed. |
| 5180 | */ |
| 5181 | |
| 5182 | void |
| 5183 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 5184 | xmlChar *buf = NULL; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5185 | size_t len = 0; |
| 5186 | size_t size = XML_PARSER_BUFFER_SIZE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5187 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5188 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5189 | xmlParserInputState state; |
| 5190 | int count = 0; |
| 5191 | |
| 5192 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 5193 | xmlParserInputPtr input = ctxt->input; |
| 5194 | state = ctxt->instate; |
| 5195 | ctxt->instate = XML_PARSER_PI; |
| 5196 | /* |
| 5197 | * this is a Processing Instruction. |
| 5198 | */ |
| 5199 | SKIP(2); |
| 5200 | SHRINK; |
| 5201 | |
| 5202 | /* |
| 5203 | * Parse the target name and check for special support like |
| 5204 | * namespace. |
| 5205 | */ |
| 5206 | target = xmlParsePITarget(ctxt); |
| 5207 | if (target != NULL) { |
| 5208 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5209 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5210 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5211 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5212 | } |
| 5213 | SKIP(2); |
| 5214 | |
| 5215 | /* |
| 5216 | * SAX: PI detected. |
| 5217 | */ |
| 5218 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5219 | (ctxt->sax->processingInstruction != NULL)) |
| 5220 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5221 | target, NULL); |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5222 | if (ctxt->instate != XML_PARSER_EOF) |
| 5223 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5224 | return; |
| 5225 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5226 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5227 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5228 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5229 | ctxt->instate = state; |
| 5230 | return; |
| 5231 | } |
| 5232 | cur = CUR; |
| 5233 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5234 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5235 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5236 | } |
| 5237 | SKIP_BLANKS; |
| 5238 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 5239 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5240 | ((cur != '?') || (NXT(1) != '>'))) { |
| 5241 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5242 | xmlChar *tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5243 | size_t new_size = size * 2; |
| 5244 | tmp = (xmlChar *) xmlRealloc(buf, new_size); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5245 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5246 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5247 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5248 | ctxt->instate = state; |
| 5249 | return; |
| 5250 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5251 | buf = tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5252 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5253 | } |
| 5254 | count++; |
| 5255 | if (count > 50) { |
| 5256 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5257 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5258 | xmlFree(buf); |
| 5259 | return; |
| 5260 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5261 | count = 0; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5262 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5263 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5264 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5265 | "PI %s too big found", target); |
| 5266 | xmlFree(buf); |
| 5267 | ctxt->instate = state; |
| 5268 | return; |
| 5269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5270 | } |
| 5271 | COPY_BUF(l,buf,len,cur); |
| 5272 | NEXTL(l); |
| 5273 | cur = CUR_CHAR(l); |
| 5274 | if (cur == 0) { |
| 5275 | SHRINK; |
| 5276 | GROW; |
| 5277 | cur = CUR_CHAR(l); |
| 5278 | } |
| 5279 | } |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5280 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5281 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5282 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5283 | "PI %s too big found", target); |
| 5284 | xmlFree(buf); |
| 5285 | ctxt->instate = state; |
| 5286 | return; |
| 5287 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5288 | buf[len] = 0; |
| 5289 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5290 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5291 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5292 | } else { |
| 5293 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5294 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5295 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5296 | } |
| 5297 | SKIP(2); |
| 5298 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5299 | #ifdef LIBXML_CATALOG_ENABLED |
| 5300 | if (((state == XML_PARSER_MISC) || |
| 5301 | (state == XML_PARSER_START)) && |
| 5302 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 5303 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 5304 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 5305 | (allow == XML_CATA_ALLOW_ALL)) |
| 5306 | xmlParseCatalogPI(ctxt, buf); |
| 5307 | } |
| 5308 | #endif |
| 5309 | |
| 5310 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5311 | /* |
| 5312 | * SAX: PI detected. |
| 5313 | */ |
| 5314 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5315 | (ctxt->sax->processingInstruction != NULL)) |
| 5316 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5317 | target, buf); |
| 5318 | } |
| 5319 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5320 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5321 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5322 | } |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5323 | if (ctxt->instate != XML_PARSER_EOF) |
| 5324 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5325 | } |
| 5326 | } |
| 5327 | |
| 5328 | /** |
| 5329 | * xmlParseNotationDecl: |
| 5330 | * @ctxt: an XML parser context |
| 5331 | * |
| 5332 | * parse a notation declaration |
| 5333 | * |
| 5334 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 5335 | * |
| 5336 | * Hence there is actually 3 choices: |
| 5337 | * 'PUBLIC' S PubidLiteral |
| 5338 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 5339 | * and 'SYSTEM' S SystemLiteral |
| 5340 | * |
| 5341 | * See the NOTE on xmlParseExternalID(). |
| 5342 | */ |
| 5343 | |
| 5344 | void |
| 5345 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5346 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5347 | xmlChar *Pubid; |
| 5348 | xmlChar *Systemid; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5349 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5350 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5351 | xmlParserInputPtr input = ctxt->input; |
| 5352 | SHRINK; |
| 5353 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5354 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5355 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5356 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5357 | return; |
| 5358 | } |
| 5359 | SKIP_BLANKS; |
| 5360 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5361 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5362 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5363 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5364 | return; |
| 5365 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5366 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5367 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5368 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5369 | return; |
| 5370 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5371 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5372 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5373 | "colons are forbidden from notation names '%s'\n", |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5374 | name, NULL, NULL); |
| 5375 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5376 | SKIP_BLANKS; |
| 5377 | |
| 5378 | /* |
| 5379 | * Parse the IDs. |
| 5380 | */ |
| 5381 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 5382 | SKIP_BLANKS; |
| 5383 | |
| 5384 | if (RAW == '>') { |
| 5385 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5386 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5387 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5388 | } |
| 5389 | NEXT; |
| 5390 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5391 | (ctxt->sax->notationDecl != NULL)) |
| 5392 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 5393 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5394 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5395 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5396 | if (Systemid != NULL) xmlFree(Systemid); |
| 5397 | if (Pubid != NULL) xmlFree(Pubid); |
| 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | /** |
| 5402 | * xmlParseEntityDecl: |
| 5403 | * @ctxt: an XML parser context |
| 5404 | * |
| 5405 | * parse <!ENTITY declarations |
| 5406 | * |
| 5407 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 5408 | * |
| 5409 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 5410 | * |
| 5411 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 5412 | * |
| 5413 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 5414 | * |
| 5415 | * [74] PEDef ::= EntityValue | ExternalID |
| 5416 | * |
| 5417 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 5418 | * |
| 5419 | * [ VC: Notation Declared ] |
| 5420 | * The Name must match the declared name of a notation. |
| 5421 | */ |
| 5422 | |
| 5423 | void |
| 5424 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5425 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5426 | xmlChar *value = NULL; |
| 5427 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5428 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5429 | int isParameter = 0; |
| 5430 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5431 | int skipped; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5432 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5433 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5434 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5435 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5436 | SHRINK; |
| 5437 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5438 | skipped = SKIP_BLANKS; |
| 5439 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5440 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5441 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5442 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5443 | |
| 5444 | if (RAW == '%') { |
| 5445 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5446 | skipped = SKIP_BLANKS; |
| 5447 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5448 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5449 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5450 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | isParameter = 1; |
| 5452 | } |
| 5453 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5454 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5455 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5456 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5457 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5458 | return; |
| 5459 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5460 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5461 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 2f9b126 | 2014-07-26 20:29:36 +0800 | [diff] [blame] | 5462 | "colons are forbidden from entities names '%s'\n", |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5463 | name, NULL, NULL); |
| 5464 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5465 | skipped = SKIP_BLANKS; |
| 5466 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5467 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5468 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5469 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5470 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5471 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5472 | /* |
| 5473 | * handle the various case of definitions... |
| 5474 | */ |
| 5475 | if (isParameter) { |
| 5476 | if ((RAW == '"') || (RAW == '\'')) { |
| 5477 | value = xmlParseEntityValue(ctxt, &orig); |
| 5478 | if (value) { |
| 5479 | if ((ctxt->sax != NULL) && |
| 5480 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5481 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5482 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5483 | NULL, NULL, value); |
| 5484 | } |
| 5485 | } else { |
| 5486 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5487 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5488 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5489 | } |
| 5490 | if (URI) { |
| 5491 | xmlURIPtr uri; |
| 5492 | |
| 5493 | uri = xmlParseURI((const char *) URI); |
| 5494 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5495 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5496 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5497 | /* |
| 5498 | * This really ought to be a well formedness error |
| 5499 | * but the XML Core WG decided otherwise c.f. issue |
| 5500 | * E26 of the XML erratas. |
| 5501 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5502 | } else { |
| 5503 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5504 | /* |
| 5505 | * Okay this is foolish to block those but not |
| 5506 | * invalid URIs. |
| 5507 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5508 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5509 | } else { |
| 5510 | if ((ctxt->sax != NULL) && |
| 5511 | (!ctxt->disableSAX) && |
| 5512 | (ctxt->sax->entityDecl != NULL)) |
| 5513 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5514 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5515 | literal, URI, NULL); |
| 5516 | } |
| 5517 | xmlFreeURI(uri); |
| 5518 | } |
| 5519 | } |
| 5520 | } |
| 5521 | } else { |
| 5522 | if ((RAW == '"') || (RAW == '\'')) { |
| 5523 | value = xmlParseEntityValue(ctxt, &orig); |
| 5524 | if ((ctxt->sax != NULL) && |
| 5525 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5526 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5527 | XML_INTERNAL_GENERAL_ENTITY, |
| 5528 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5529 | /* |
| 5530 | * For expat compatibility in SAX mode. |
| 5531 | */ |
| 5532 | if ((ctxt->myDoc == NULL) || |
| 5533 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5534 | if (ctxt->myDoc == NULL) { |
| 5535 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5536 | if (ctxt->myDoc == NULL) { |
| 5537 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5538 | return; |
| 5539 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5540 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5541 | } |
| 5542 | if (ctxt->myDoc->intSubset == NULL) |
| 5543 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5544 | BAD_CAST "fake", NULL, NULL); |
| 5545 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5546 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5547 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5548 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5549 | } else { |
| 5550 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5551 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5552 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5553 | } |
| 5554 | if (URI) { |
| 5555 | xmlURIPtr uri; |
| 5556 | |
| 5557 | uri = xmlParseURI((const char *)URI); |
| 5558 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5559 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5560 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5561 | /* |
| 5562 | * This really ought to be a well formedness error |
| 5563 | * but the XML Core WG decided otherwise c.f. issue |
| 5564 | * E26 of the XML erratas. |
| 5565 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5566 | } else { |
| 5567 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5568 | /* |
| 5569 | * Okay this is foolish to block those but not |
| 5570 | * invalid URIs. |
| 5571 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5572 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5573 | } |
| 5574 | xmlFreeURI(uri); |
| 5575 | } |
| 5576 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5577 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5578 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5579 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5580 | } |
| 5581 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5582 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5583 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5584 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5585 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5586 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5587 | } |
| 5588 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5589 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5590 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5591 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5592 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5593 | literal, URI, ndata); |
| 5594 | } else { |
| 5595 | if ((ctxt->sax != NULL) && |
| 5596 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5597 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5598 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5599 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5600 | /* |
| 5601 | * For expat compatibility in SAX mode. |
| 5602 | * assuming the entity repalcement was asked for |
| 5603 | */ |
| 5604 | if ((ctxt->replaceEntities != 0) && |
| 5605 | ((ctxt->myDoc == NULL) || |
| 5606 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5607 | if (ctxt->myDoc == NULL) { |
| 5608 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5609 | if (ctxt->myDoc == NULL) { |
| 5610 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5611 | return; |
| 5612 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5613 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | if (ctxt->myDoc->intSubset == NULL) |
| 5617 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5618 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5619 | xmlSAX2EntityDecl(ctxt, name, |
| 5620 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5621 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5622 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5623 | } |
| 5624 | } |
| 5625 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5626 | if (ctxt->instate == XML_PARSER_EOF) |
| 5627 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5628 | SKIP_BLANKS; |
| 5629 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5630 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5631 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5632 | } else { |
| 5633 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5634 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5635 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5636 | } |
| 5637 | NEXT; |
| 5638 | } |
| 5639 | if (orig != NULL) { |
| 5640 | /* |
| 5641 | * Ugly mechanism to save the raw entity value. |
| 5642 | */ |
| 5643 | xmlEntityPtr cur = NULL; |
| 5644 | |
| 5645 | if (isParameter) { |
| 5646 | if ((ctxt->sax != NULL) && |
| 5647 | (ctxt->sax->getParameterEntity != NULL)) |
| 5648 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5649 | } else { |
| 5650 | if ((ctxt->sax != NULL) && |
| 5651 | (ctxt->sax->getEntity != NULL)) |
| 5652 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5653 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5654 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5655 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5656 | } |
| 5657 | if (cur != NULL) { |
| 5658 | if (cur->orig != NULL) |
| 5659 | xmlFree(orig); |
| 5660 | else |
| 5661 | cur->orig = orig; |
| 5662 | } else |
| 5663 | xmlFree(orig); |
| 5664 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5665 | if (value != NULL) xmlFree(value); |
| 5666 | if (URI != NULL) xmlFree(URI); |
| 5667 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5668 | } |
| 5669 | } |
| 5670 | |
| 5671 | /** |
| 5672 | * xmlParseDefaultDecl: |
| 5673 | * @ctxt: an XML parser context |
| 5674 | * @value: Receive a possible fixed default value for the attribute |
| 5675 | * |
| 5676 | * Parse an attribute default declaration |
| 5677 | * |
| 5678 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5679 | * |
| 5680 | * [ VC: Required Attribute ] |
| 5681 | * if the default declaration is the keyword #REQUIRED, then the |
| 5682 | * attribute must be specified for all elements of the type in the |
| 5683 | * attribute-list declaration. |
| 5684 | * |
| 5685 | * [ VC: Attribute Default Legal ] |
| 5686 | * The declared default value must meet the lexical constraints of |
| 5687 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5688 | * |
| 5689 | * [ VC: Fixed Attribute Default ] |
| 5690 | * if an attribute has a default value declared with the #FIXED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5691 | * keyword, instances of that attribute must match the default value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5692 | * |
| 5693 | * [ WFC: No < in Attribute Values ] |
| 5694 | * handled in xmlParseAttValue() |
| 5695 | * |
| 5696 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5697 | * or XML_ATTRIBUTE_FIXED. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5698 | */ |
| 5699 | |
| 5700 | int |
| 5701 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5702 | int val; |
| 5703 | xmlChar *ret; |
| 5704 | |
| 5705 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5706 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5707 | SKIP(9); |
| 5708 | return(XML_ATTRIBUTE_REQUIRED); |
| 5709 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5710 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5711 | SKIP(8); |
| 5712 | return(XML_ATTRIBUTE_IMPLIED); |
| 5713 | } |
| 5714 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5715 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5716 | SKIP(6); |
| 5717 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5718 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5719 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5720 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5721 | } |
| 5722 | SKIP_BLANKS; |
| 5723 | } |
| 5724 | ret = xmlParseAttValue(ctxt); |
| 5725 | ctxt->instate = XML_PARSER_DTD; |
| 5726 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5727 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5728 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5729 | } else |
| 5730 | *value = ret; |
| 5731 | return(val); |
| 5732 | } |
| 5733 | |
| 5734 | /** |
| 5735 | * xmlParseNotationType: |
| 5736 | * @ctxt: an XML parser context |
| 5737 | * |
| 5738 | * parse an Notation attribute type. |
| 5739 | * |
| 5740 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5741 | * |
| 5742 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5743 | * |
| 5744 | * [ VC: Notation Attributes ] |
| 5745 | * Values of this type must match one of the notation names included |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5746 | * in the declaration; all notation names in the declaration must be declared. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5747 | * |
| 5748 | * Returns: the notation attribute tree built while parsing |
| 5749 | */ |
| 5750 | |
| 5751 | xmlEnumerationPtr |
| 5752 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5753 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5754 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5755 | |
| 5756 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5757 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | return(NULL); |
| 5759 | } |
| 5760 | SHRINK; |
| 5761 | do { |
| 5762 | NEXT; |
| 5763 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5764 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5765 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5766 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5767 | "Name expected in NOTATION declaration\n"); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5768 | xmlFreeEnumeration(ret); |
| 5769 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5770 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5771 | tmp = ret; |
| 5772 | while (tmp != NULL) { |
| 5773 | if (xmlStrEqual(name, tmp->name)) { |
| 5774 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5775 | "standalone: attribute notation value token %s duplicated\n", |
| 5776 | name, NULL); |
| 5777 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5778 | xmlFree((xmlChar *) name); |
| 5779 | break; |
| 5780 | } |
| 5781 | tmp = tmp->next; |
| 5782 | } |
| 5783 | if (tmp == NULL) { |
| 5784 | cur = xmlCreateEnumeration(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5785 | if (cur == NULL) { |
| 5786 | xmlFreeEnumeration(ret); |
| 5787 | return(NULL); |
| 5788 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5789 | if (last == NULL) ret = last = cur; |
| 5790 | else { |
| 5791 | last->next = cur; |
| 5792 | last = cur; |
| 5793 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5794 | } |
| 5795 | SKIP_BLANKS; |
| 5796 | } while (RAW == '|'); |
| 5797 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5798 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5799 | xmlFreeEnumeration(ret); |
| 5800 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5801 | } |
| 5802 | NEXT; |
| 5803 | return(ret); |
| 5804 | } |
| 5805 | |
| 5806 | /** |
| 5807 | * xmlParseEnumerationType: |
| 5808 | * @ctxt: an XML parser context |
| 5809 | * |
| 5810 | * parse an Enumeration attribute type. |
| 5811 | * |
| 5812 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5813 | * |
| 5814 | * [ VC: Enumeration ] |
| 5815 | * Values of this type must match one of the Nmtoken tokens in |
| 5816 | * the declaration |
| 5817 | * |
| 5818 | * Returns: the enumeration attribute tree built while parsing |
| 5819 | */ |
| 5820 | |
| 5821 | xmlEnumerationPtr |
| 5822 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5823 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5824 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5825 | |
| 5826 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5827 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5828 | return(NULL); |
| 5829 | } |
| 5830 | SHRINK; |
| 5831 | do { |
| 5832 | NEXT; |
| 5833 | SKIP_BLANKS; |
| 5834 | name = xmlParseNmtoken(ctxt); |
| 5835 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5836 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5837 | return(ret); |
| 5838 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5839 | tmp = ret; |
| 5840 | while (tmp != NULL) { |
| 5841 | if (xmlStrEqual(name, tmp->name)) { |
| 5842 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5843 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5844 | name, NULL); |
| 5845 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5846 | xmlFree(name); |
| 5847 | break; |
| 5848 | } |
| 5849 | tmp = tmp->next; |
| 5850 | } |
| 5851 | if (tmp == NULL) { |
| 5852 | cur = xmlCreateEnumeration(name); |
| 5853 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5854 | xmlFree(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5855 | if (cur == NULL) { |
| 5856 | xmlFreeEnumeration(ret); |
| 5857 | return(NULL); |
| 5858 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5859 | if (last == NULL) ret = last = cur; |
| 5860 | else { |
| 5861 | last->next = cur; |
| 5862 | last = cur; |
| 5863 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5864 | } |
| 5865 | SKIP_BLANKS; |
| 5866 | } while (RAW == '|'); |
| 5867 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5868 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5869 | return(ret); |
| 5870 | } |
| 5871 | NEXT; |
| 5872 | return(ret); |
| 5873 | } |
| 5874 | |
| 5875 | /** |
| 5876 | * xmlParseEnumeratedType: |
| 5877 | * @ctxt: an XML parser context |
| 5878 | * @tree: the enumeration tree built while parsing |
| 5879 | * |
| 5880 | * parse an Enumerated attribute type. |
| 5881 | * |
| 5882 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5883 | * |
| 5884 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5885 | * |
| 5886 | * |
| 5887 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5888 | */ |
| 5889 | |
| 5890 | int |
| 5891 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5892 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5893 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5894 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5895 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5896 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5897 | return(0); |
| 5898 | } |
| 5899 | SKIP_BLANKS; |
| 5900 | *tree = xmlParseNotationType(ctxt); |
| 5901 | if (*tree == NULL) return(0); |
| 5902 | return(XML_ATTRIBUTE_NOTATION); |
| 5903 | } |
| 5904 | *tree = xmlParseEnumerationType(ctxt); |
| 5905 | if (*tree == NULL) return(0); |
| 5906 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5907 | } |
| 5908 | |
| 5909 | /** |
| 5910 | * xmlParseAttributeType: |
| 5911 | * @ctxt: an XML parser context |
| 5912 | * @tree: the enumeration tree built while parsing |
| 5913 | * |
| 5914 | * parse the Attribute list def for an element |
| 5915 | * |
| 5916 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5917 | * |
| 5918 | * [55] StringType ::= 'CDATA' |
| 5919 | * |
| 5920 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5921 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5922 | * |
| 5923 | * Validity constraints for attribute values syntax are checked in |
| 5924 | * xmlValidateAttributeValue() |
| 5925 | * |
| 5926 | * [ VC: ID ] |
| 5927 | * Values of type ID must match the Name production. A name must not |
| 5928 | * appear more than once in an XML document as a value of this type; |
| 5929 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5930 | * |
| 5931 | * [ VC: One ID per Element Type ] |
| 5932 | * No element type may have more than one ID attribute specified. |
| 5933 | * |
| 5934 | * [ VC: ID Attribute Default ] |
| 5935 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5936 | * |
| 5937 | * [ VC: IDREF ] |
| 5938 | * Values of type IDREF must match the Name production, and values |
| 5939 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5940 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5941 | * values must match the value of some ID attribute. |
| 5942 | * |
| 5943 | * [ VC: Entity Name ] |
| 5944 | * Values of type ENTITY must match the Name production, values |
| 5945 | * of type ENTITIES must match Names; each Entity Name must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5946 | * name of an unparsed entity declared in the DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5947 | * |
| 5948 | * [ VC: Name Token ] |
| 5949 | * Values of type NMTOKEN must match the Nmtoken production; values |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5950 | * of type NMTOKENS must match Nmtokens. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5951 | * |
| 5952 | * Returns the attribute type |
| 5953 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5954 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5955 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5956 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5957 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5958 | SKIP(5); |
| 5959 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5960 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5961 | SKIP(6); |
| 5962 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5963 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5964 | SKIP(5); |
| 5965 | return(XML_ATTRIBUTE_IDREF); |
| 5966 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5967 | SKIP(2); |
| 5968 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5969 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5970 | SKIP(6); |
| 5971 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5972 | } 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] | 5973 | SKIP(8); |
| 5974 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5975 | } 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] | 5976 | SKIP(8); |
| 5977 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5978 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5979 | SKIP(7); |
| 5980 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5981 | } |
| 5982 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5983 | } |
| 5984 | |
| 5985 | /** |
| 5986 | * xmlParseAttributeListDecl: |
| 5987 | * @ctxt: an XML parser context |
| 5988 | * |
| 5989 | * : parse the Attribute list def for an element |
| 5990 | * |
| 5991 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5992 | * |
| 5993 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5994 | * |
| 5995 | */ |
| 5996 | void |
| 5997 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5998 | const xmlChar *elemName; |
| 5999 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6000 | xmlEnumerationPtr tree; |
| 6001 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6002 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6003 | xmlParserInputPtr input = ctxt->input; |
| 6004 | |
| 6005 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6006 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6007 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6008 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6009 | } |
| 6010 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6011 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6012 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6013 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6014 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6015 | return; |
| 6016 | } |
| 6017 | SKIP_BLANKS; |
| 6018 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6019 | while ((RAW != '>') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6020 | const xmlChar *check = CUR_PTR; |
| 6021 | int type; |
| 6022 | int def; |
| 6023 | xmlChar *defaultValue = NULL; |
| 6024 | |
| 6025 | GROW; |
| 6026 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6027 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6028 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6029 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6030 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6031 | break; |
| 6032 | } |
| 6033 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6034 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6035 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6037 | break; |
| 6038 | } |
| 6039 | SKIP_BLANKS; |
| 6040 | |
| 6041 | type = xmlParseAttributeType(ctxt, &tree); |
| 6042 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6043 | break; |
| 6044 | } |
| 6045 | |
| 6046 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6047 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6048 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6049 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6050 | if (tree != NULL) |
| 6051 | xmlFreeEnumeration(tree); |
| 6052 | break; |
| 6053 | } |
| 6054 | SKIP_BLANKS; |
| 6055 | |
| 6056 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 6057 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6058 | if (defaultValue != NULL) |
| 6059 | xmlFree(defaultValue); |
| 6060 | if (tree != NULL) |
| 6061 | xmlFreeEnumeration(tree); |
| 6062 | break; |
| 6063 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 6064 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 6065 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6066 | |
| 6067 | GROW; |
| 6068 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6069 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6070 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6071 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6072 | if (defaultValue != NULL) |
| 6073 | xmlFree(defaultValue); |
| 6074 | if (tree != NULL) |
| 6075 | xmlFreeEnumeration(tree); |
| 6076 | break; |
| 6077 | } |
| 6078 | SKIP_BLANKS; |
| 6079 | } |
| 6080 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6081 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6082 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6083 | if (defaultValue != NULL) |
| 6084 | xmlFree(defaultValue); |
| 6085 | if (tree != NULL) |
| 6086 | xmlFreeEnumeration(tree); |
| 6087 | break; |
| 6088 | } |
| 6089 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 6090 | (ctxt->sax->attributeDecl != NULL)) |
| 6091 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 6092 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6093 | else if (tree != NULL) |
| 6094 | xmlFreeEnumeration(tree); |
| 6095 | |
| 6096 | if ((ctxt->sax2) && (defaultValue != NULL) && |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6097 | (def != XML_ATTRIBUTE_IMPLIED) && |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6098 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 6099 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 6100 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 6101 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6102 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 6103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6104 | if (defaultValue != NULL) |
| 6105 | xmlFree(defaultValue); |
| 6106 | GROW; |
| 6107 | } |
| 6108 | if (RAW == '>') { |
| 6109 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6110 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6111 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 6112 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6113 | } |
| 6114 | NEXT; |
| 6115 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6116 | } |
| 6117 | } |
| 6118 | |
| 6119 | /** |
| 6120 | * xmlParseElementMixedContentDecl: |
| 6121 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6122 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6123 | * |
| 6124 | * parse the declaration for a Mixed Element content |
| 6125 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6126 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6127 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 6128 | * '(' S? '#PCDATA' S? ')' |
| 6129 | * |
| 6130 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 6131 | * |
| 6132 | * [ VC: No Duplicate Types ] |
| 6133 | * The same name must not appear more than once in a single |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6134 | * mixed-content declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6135 | * |
| 6136 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 6137 | */ |
| 6138 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6139 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6140 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6141 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6142 | |
| 6143 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6144 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6145 | SKIP(7); |
| 6146 | SKIP_BLANKS; |
| 6147 | SHRINK; |
| 6148 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6149 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6150 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6151 | "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] | 6152 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6153 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6154 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6155 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6156 | if (ret == NULL) |
| 6157 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | if (RAW == '*') { |
| 6159 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6160 | NEXT; |
| 6161 | } |
| 6162 | return(ret); |
| 6163 | } |
| 6164 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6165 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6166 | if (ret == NULL) return(NULL); |
| 6167 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6168 | while ((RAW == '|') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6169 | NEXT; |
| 6170 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6171 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6172 | if (ret == NULL) return(NULL); |
| 6173 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6174 | if (cur != NULL) |
| 6175 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6176 | cur = ret; |
| 6177 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6178 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6179 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6180 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6181 | if (n->c1 != NULL) |
| 6182 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6183 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6184 | if (n != NULL) |
| 6185 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6186 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | } |
| 6188 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6189 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6190 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6191 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6192 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6193 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | return(NULL); |
| 6195 | } |
| 6196 | SKIP_BLANKS; |
| 6197 | GROW; |
| 6198 | } |
| 6199 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 6200 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6201 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6202 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6203 | if (cur->c2 != NULL) |
| 6204 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6205 | } |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6206 | if (ret != NULL) |
| 6207 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6208 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6209 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6210 | "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] | 6211 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6212 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6213 | SKIP(2); |
| 6214 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6215 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6216 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6217 | return(NULL); |
| 6218 | } |
| 6219 | |
| 6220 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6221 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6222 | } |
| 6223 | return(ret); |
| 6224 | } |
| 6225 | |
| 6226 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6227 | * xmlParseElementChildrenContentDeclPriv: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6229 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6230 | * @depth: the level of recursion |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6231 | * |
| 6232 | * parse the declaration for a Mixed Element content |
| 6233 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6234 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6235 | * |
| 6236 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6237 | * |
| 6238 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6239 | * |
| 6240 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6241 | * |
| 6242 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6243 | * |
| 6244 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6245 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6246 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6247 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6248 | * construct is contained in the replacement text for a parameter |
| 6249 | * entity, both must be contained in the same replacement text. For |
| 6250 | * interoperability, if a parameter-entity reference appears in a |
| 6251 | * choice, seq, or Mixed construct, its replacement text should not |
| 6252 | * be empty, and neither the first nor last non-blank character of |
| 6253 | * the replacement text should be a connector (| or ,). |
| 6254 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6255 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6256 | * hierarchy. |
| 6257 | */ |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6258 | static xmlElementContentPtr |
| 6259 | xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk, |
| 6260 | int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6262 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | xmlChar type = 0; |
| 6264 | |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6265 | if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 6266 | (depth > 2048)) { |
| 6267 | xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, |
| 6268 | "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n", |
| 6269 | depth); |
| 6270 | return(NULL); |
| 6271 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6272 | SKIP_BLANKS; |
| 6273 | GROW; |
| 6274 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6275 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6276 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6277 | /* Recurse on first child */ |
| 6278 | NEXT; |
| 6279 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6280 | cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6281 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | SKIP_BLANKS; |
| 6283 | GROW; |
| 6284 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6285 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6286 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6287 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6288 | return(NULL); |
| 6289 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6290 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6291 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6292 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6293 | return(NULL); |
| 6294 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6295 | GROW; |
| 6296 | if (RAW == '?') { |
| 6297 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6298 | NEXT; |
| 6299 | } else if (RAW == '*') { |
| 6300 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6301 | NEXT; |
| 6302 | } else if (RAW == '+') { |
| 6303 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6304 | NEXT; |
| 6305 | } else { |
| 6306 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6307 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6308 | GROW; |
| 6309 | } |
| 6310 | SKIP_BLANKS; |
| 6311 | SHRINK; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6312 | while ((RAW != ')') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6313 | /* |
| 6314 | * Each loop we parse one separator and one element. |
| 6315 | */ |
| 6316 | if (RAW == ',') { |
| 6317 | if (type == 0) type = CUR; |
| 6318 | |
| 6319 | /* |
| 6320 | * Detect "Name | Name , Name" error |
| 6321 | */ |
| 6322 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6323 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6324 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6325 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6326 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6327 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6328 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6329 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6330 | return(NULL); |
| 6331 | } |
| 6332 | NEXT; |
| 6333 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6334 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6335 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6336 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6337 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 6338 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6339 | return(NULL); |
| 6340 | } |
| 6341 | if (last == NULL) { |
| 6342 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6343 | if (ret != NULL) |
| 6344 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6345 | ret = cur = op; |
| 6346 | } else { |
| 6347 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6348 | if (op != NULL) |
| 6349 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6351 | if (last != NULL) |
| 6352 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6353 | cur =op; |
| 6354 | last = NULL; |
| 6355 | } |
| 6356 | } else if (RAW == '|') { |
| 6357 | if (type == 0) type = CUR; |
| 6358 | |
| 6359 | /* |
| 6360 | * Detect "Name , Name | Name" error |
| 6361 | */ |
| 6362 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6363 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6364 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6365 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6366 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6367 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6368 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6369 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6370 | return(NULL); |
| 6371 | } |
| 6372 | NEXT; |
| 6373 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6374 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6375 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6376 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6377 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6378 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6379 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6380 | return(NULL); |
| 6381 | } |
| 6382 | if (last == NULL) { |
| 6383 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6384 | if (ret != NULL) |
| 6385 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6386 | ret = cur = op; |
| 6387 | } else { |
| 6388 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6389 | if (op != NULL) |
| 6390 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6391 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6392 | if (last != NULL) |
| 6393 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6394 | cur =op; |
| 6395 | last = NULL; |
| 6396 | } |
| 6397 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6398 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 6399 | if ((last != NULL) && (last != ret)) |
| 6400 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6402 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | return(NULL); |
| 6404 | } |
| 6405 | GROW; |
| 6406 | SKIP_BLANKS; |
| 6407 | GROW; |
| 6408 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6409 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6410 | /* Recurse on second child */ |
| 6411 | NEXT; |
| 6412 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6413 | last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6414 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6415 | SKIP_BLANKS; |
| 6416 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6417 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6418 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6419 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6420 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6421 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6422 | return(NULL); |
| 6423 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6424 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6425 | if (last == NULL) { |
| 6426 | if (ret != NULL) |
| 6427 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 6428 | return(NULL); |
| 6429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6430 | if (RAW == '?') { |
| 6431 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6432 | NEXT; |
| 6433 | } else if (RAW == '*') { |
| 6434 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6435 | NEXT; |
| 6436 | } else if (RAW == '+') { |
| 6437 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6438 | NEXT; |
| 6439 | } else { |
| 6440 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6441 | } |
| 6442 | } |
| 6443 | SKIP_BLANKS; |
| 6444 | GROW; |
| 6445 | } |
| 6446 | if ((cur != NULL) && (last != NULL)) { |
| 6447 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6448 | if (last != NULL) |
| 6449 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6450 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6451 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6452 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6453 | "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] | 6454 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6455 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6456 | NEXT; |
| 6457 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6458 | if (ret != NULL) { |
| 6459 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 6460 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6461 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6462 | else |
| 6463 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6464 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6465 | NEXT; |
| 6466 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6467 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6468 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6469 | cur = ret; |
| 6470 | /* |
| 6471 | * Some normalization: |
| 6472 | * (a | b* | c?)* == (a | b | c)* |
| 6473 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6474 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6475 | if ((cur->c1 != NULL) && |
| 6476 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6477 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6478 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6479 | if ((cur->c2 != NULL) && |
| 6480 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6481 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6482 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6483 | cur = cur->c2; |
| 6484 | } |
| 6485 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6486 | NEXT; |
| 6487 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6488 | if (ret != NULL) { |
| 6489 | int found = 0; |
| 6490 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6491 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6492 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6493 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6494 | else |
| 6495 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6496 | /* |
| 6497 | * Some normalization: |
| 6498 | * (a | b*)+ == (a | b)* |
| 6499 | * (a | b?)+ == (a | b)* |
| 6500 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6501 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6502 | if ((cur->c1 != NULL) && |
| 6503 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6504 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6505 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6506 | found = 1; |
| 6507 | } |
| 6508 | if ((cur->c2 != NULL) && |
| 6509 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6510 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6511 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6512 | found = 1; |
| 6513 | } |
| 6514 | cur = cur->c2; |
| 6515 | } |
| 6516 | if (found) |
| 6517 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6518 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6519 | NEXT; |
| 6520 | } |
| 6521 | return(ret); |
| 6522 | } |
| 6523 | |
| 6524 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6525 | * xmlParseElementChildrenContentDecl: |
| 6526 | * @ctxt: an XML parser context |
| 6527 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6528 | * |
| 6529 | * parse the declaration for a Mixed Element content |
| 6530 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 6531 | * |
| 6532 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6533 | * |
| 6534 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6535 | * |
| 6536 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6537 | * |
| 6538 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6539 | * |
| 6540 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6541 | * TODO Parameter-entity replacement text must be properly nested |
| 6542 | * with parenthesized groups. That is to say, if either of the |
| 6543 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6544 | * construct is contained in the replacement text for a parameter |
| 6545 | * entity, both must be contained in the same replacement text. For |
| 6546 | * interoperability, if a parameter-entity reference appears in a |
| 6547 | * choice, seq, or Mixed construct, its replacement text should not |
| 6548 | * be empty, and neither the first nor last non-blank character of |
| 6549 | * the replacement text should be a connector (| or ,). |
| 6550 | * |
| 6551 | * Returns the tree of xmlElementContentPtr describing the element |
| 6552 | * hierarchy. |
| 6553 | */ |
| 6554 | xmlElementContentPtr |
| 6555 | xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
| 6556 | /* stub left for API/ABI compat */ |
| 6557 | return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1)); |
| 6558 | } |
| 6559 | |
| 6560 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6561 | * xmlParseElementContentDecl: |
| 6562 | * @ctxt: an XML parser context |
| 6563 | * @name: the name of the element being defined. |
| 6564 | * @result: the Element Content pointer will be stored here if any |
| 6565 | * |
| 6566 | * parse the declaration for an Element content either Mixed or Children, |
| 6567 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6568 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6569 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6570 | * |
| 6571 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6572 | */ |
| 6573 | |
| 6574 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6575 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6576 | xmlElementContentPtr *result) { |
| 6577 | |
| 6578 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6579 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6580 | int res; |
| 6581 | |
| 6582 | *result = NULL; |
| 6583 | |
| 6584 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6585 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6586 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6587 | return(-1); |
| 6588 | } |
| 6589 | NEXT; |
| 6590 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6591 | if (ctxt->instate == XML_PARSER_EOF) |
| 6592 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6593 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6594 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6595 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6596 | res = XML_ELEMENT_TYPE_MIXED; |
| 6597 | } else { |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6598 | tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6599 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6601 | SKIP_BLANKS; |
| 6602 | *result = tree; |
| 6603 | return(res); |
| 6604 | } |
| 6605 | |
| 6606 | /** |
| 6607 | * xmlParseElementDecl: |
| 6608 | * @ctxt: an XML parser context |
| 6609 | * |
| 6610 | * parse an Element declaration. |
| 6611 | * |
| 6612 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6613 | * |
| 6614 | * [ VC: Unique Element Type Declaration ] |
| 6615 | * No element type may be declared more than once |
| 6616 | * |
| 6617 | * Returns the type of the element, or -1 in case of error |
| 6618 | */ |
| 6619 | int |
| 6620 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6621 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6622 | int ret = -1; |
| 6623 | xmlElementContentPtr content = NULL; |
| 6624 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6625 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6626 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6627 | xmlParserInputPtr input = ctxt->input; |
| 6628 | |
| 6629 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6630 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6631 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6632 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6633 | } |
| 6634 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6635 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6636 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6637 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6638 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6639 | return(-1); |
| 6640 | } |
| 6641 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6642 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6643 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6644 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6645 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | } |
| 6647 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6648 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6649 | SKIP(5); |
| 6650 | /* |
| 6651 | * Element must always be empty. |
| 6652 | */ |
| 6653 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6654 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6655 | (NXT(2) == 'Y')) { |
| 6656 | SKIP(3); |
| 6657 | /* |
| 6658 | * Element is a generic container. |
| 6659 | */ |
| 6660 | ret = XML_ELEMENT_TYPE_ANY; |
| 6661 | } else if (RAW == '(') { |
| 6662 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6663 | } else { |
| 6664 | /* |
| 6665 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6666 | */ |
| 6667 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6668 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6669 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6670 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6672 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6673 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6674 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6675 | return(-1); |
| 6676 | } |
| 6677 | |
| 6678 | SKIP_BLANKS; |
| 6679 | /* |
| 6680 | * Pop-up of finished entities. |
| 6681 | */ |
| 6682 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6683 | xmlPopInput(ctxt); |
| 6684 | SKIP_BLANKS; |
| 6685 | |
| 6686 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6687 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6688 | if (content != NULL) { |
| 6689 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6690 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6691 | } else { |
| 6692 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6693 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6694 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6695 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6696 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | NEXT; |
| 6698 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6699 | (ctxt->sax->elementDecl != NULL)) { |
| 6700 | if (content != NULL) |
| 6701 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6702 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6703 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6704 | if ((content != NULL) && (content->parent == NULL)) { |
| 6705 | /* |
| 6706 | * this is a trick: if xmlAddElementDecl is called, |
| 6707 | * instead of copying the full tree it is plugged directly |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6708 | * if called from the parser. Avoid duplicating the |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6709 | * interfaces or change the API/ABI |
| 6710 | */ |
| 6711 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6712 | } |
| 6713 | } else if (content != NULL) { |
| 6714 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6715 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6716 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6717 | } |
| 6718 | return(ret); |
| 6719 | } |
| 6720 | |
| 6721 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6722 | * xmlParseConditionalSections |
| 6723 | * @ctxt: an XML parser context |
| 6724 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6725 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6726 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6727 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6728 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6729 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6730 | */ |
| 6731 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6732 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6733 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6734 | int id = ctxt->input->id; |
| 6735 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6736 | SKIP(3); |
| 6737 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6738 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6739 | SKIP(7); |
| 6740 | SKIP_BLANKS; |
| 6741 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6742 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6743 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6744 | if (ctxt->input->id != id) { |
| 6745 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6746 | "All markup of the conditional section is not in the same entity\n", |
| 6747 | NULL, NULL); |
| 6748 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6749 | NEXT; |
| 6750 | } |
| 6751 | if (xmlParserDebugEntities) { |
| 6752 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6753 | xmlGenericError(xmlGenericErrorContext, |
| 6754 | "%s(%d): ", ctxt->input->filename, |
| 6755 | ctxt->input->line); |
| 6756 | xmlGenericError(xmlGenericErrorContext, |
| 6757 | "Entering INCLUDE Conditional Section\n"); |
| 6758 | } |
| 6759 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6760 | while (((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6761 | (NXT(2) != '>'))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6762 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6763 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6764 | |
| 6765 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6766 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6767 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6768 | NEXT; |
| 6769 | } else if (RAW == '%') { |
| 6770 | xmlParsePEReference(ctxt); |
| 6771 | } else |
| 6772 | xmlParseMarkupDecl(ctxt); |
| 6773 | |
| 6774 | /* |
| 6775 | * Pop-up of finished entities. |
| 6776 | */ |
| 6777 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6778 | xmlPopInput(ctxt); |
| 6779 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6780 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6781 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6782 | break; |
| 6783 | } |
| 6784 | } |
| 6785 | if (xmlParserDebugEntities) { |
| 6786 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6787 | xmlGenericError(xmlGenericErrorContext, |
| 6788 | "%s(%d): ", ctxt->input->filename, |
| 6789 | ctxt->input->line); |
| 6790 | xmlGenericError(xmlGenericErrorContext, |
| 6791 | "Leaving INCLUDE Conditional Section\n"); |
| 6792 | } |
| 6793 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6794 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6795 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6796 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6797 | int depth = 0; |
| 6798 | |
| 6799 | SKIP(6); |
| 6800 | SKIP_BLANKS; |
| 6801 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6802 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6803 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6804 | if (ctxt->input->id != id) { |
| 6805 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6806 | "All markup of the conditional section is not in the same entity\n", |
| 6807 | NULL, NULL); |
| 6808 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6809 | NEXT; |
| 6810 | } |
| 6811 | if (xmlParserDebugEntities) { |
| 6812 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6813 | xmlGenericError(xmlGenericErrorContext, |
| 6814 | "%s(%d): ", ctxt->input->filename, |
| 6815 | ctxt->input->line); |
| 6816 | xmlGenericError(xmlGenericErrorContext, |
| 6817 | "Entering IGNORE Conditional Section\n"); |
| 6818 | } |
| 6819 | |
| 6820 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6821 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6822 | * But disable SAX event generating DTD building in the meantime |
| 6823 | */ |
| 6824 | state = ctxt->disableSAX; |
| 6825 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6826 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6827 | ctxt->instate = XML_PARSER_IGNORE; |
| 6828 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6829 | while (((depth >= 0) && (RAW != 0)) && |
| 6830 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6832 | depth++; |
| 6833 | SKIP(3); |
| 6834 | continue; |
| 6835 | } |
| 6836 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6837 | if (--depth >= 0) SKIP(3); |
| 6838 | continue; |
| 6839 | } |
| 6840 | NEXT; |
| 6841 | continue; |
| 6842 | } |
| 6843 | |
| 6844 | ctxt->disableSAX = state; |
| 6845 | ctxt->instate = instate; |
| 6846 | |
| 6847 | if (xmlParserDebugEntities) { |
| 6848 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6849 | xmlGenericError(xmlGenericErrorContext, |
| 6850 | "%s(%d): ", ctxt->input->filename, |
| 6851 | ctxt->input->line); |
| 6852 | xmlGenericError(xmlGenericErrorContext, |
| 6853 | "Leaving IGNORE Conditional Section\n"); |
| 6854 | } |
| 6855 | |
| 6856 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6857 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6858 | } |
| 6859 | |
| 6860 | if (RAW == 0) |
| 6861 | SHRINK; |
| 6862 | |
| 6863 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6864 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6865 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6866 | if (ctxt->input->id != id) { |
| 6867 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6868 | "All markup of the conditional section is not in the same entity\n", |
| 6869 | NULL, NULL); |
| 6870 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6871 | SKIP(3); |
| 6872 | } |
| 6873 | } |
| 6874 | |
| 6875 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6876 | * xmlParseMarkupDecl: |
| 6877 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6878 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6879 | * parse Markup declarations |
| 6880 | * |
| 6881 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6882 | * NotationDecl | PI | Comment |
| 6883 | * |
| 6884 | * [ VC: Proper Declaration/PE Nesting ] |
| 6885 | * Parameter-entity replacement text must be properly nested with |
| 6886 | * markup declarations. That is to say, if either the first character |
| 6887 | * or the last character of a markup declaration (markupdecl above) is |
| 6888 | * contained in the replacement text for a parameter-entity reference, |
| 6889 | * both must be contained in the same replacement text. |
| 6890 | * |
| 6891 | * [ WFC: PEs in Internal Subset ] |
| 6892 | * In the internal DTD subset, parameter-entity references can occur |
| 6893 | * only where markup declarations can occur, not within markup declarations. |
| 6894 | * (This does not apply to references that occur in external parameter |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6895 | * entities or to the external subset.) |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6896 | */ |
| 6897 | void |
| 6898 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6899 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6900 | if (CUR == '<') { |
| 6901 | if (NXT(1) == '!') { |
| 6902 | switch (NXT(2)) { |
| 6903 | case 'E': |
| 6904 | if (NXT(3) == 'L') |
| 6905 | xmlParseElementDecl(ctxt); |
| 6906 | else if (NXT(3) == 'N') |
| 6907 | xmlParseEntityDecl(ctxt); |
| 6908 | break; |
| 6909 | case 'A': |
| 6910 | xmlParseAttributeListDecl(ctxt); |
| 6911 | break; |
| 6912 | case 'N': |
| 6913 | xmlParseNotationDecl(ctxt); |
| 6914 | break; |
| 6915 | case '-': |
| 6916 | xmlParseComment(ctxt); |
| 6917 | break; |
| 6918 | default: |
| 6919 | /* there is an error but it will be detected later */ |
| 6920 | break; |
| 6921 | } |
| 6922 | } else if (NXT(1) == '?') { |
| 6923 | xmlParsePI(ctxt); |
| 6924 | } |
| 6925 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6926 | /* |
| 6927 | * This is only for internal subset. On external entities, |
| 6928 | * the replacement is done before parsing stage |
| 6929 | */ |
| 6930 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6931 | xmlParsePEReference(ctxt); |
| 6932 | |
| 6933 | /* |
| 6934 | * Conditional sections are allowed from entities included |
| 6935 | * by PE References in the internal subset. |
| 6936 | */ |
| 6937 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6938 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6939 | xmlParseConditionalSections(ctxt); |
| 6940 | } |
| 6941 | } |
| 6942 | |
| 6943 | ctxt->instate = XML_PARSER_DTD; |
| 6944 | } |
| 6945 | |
| 6946 | /** |
| 6947 | * xmlParseTextDecl: |
| 6948 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6949 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6950 | * parse an XML declaration header for external entities |
| 6951 | * |
| 6952 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6953 | */ |
| 6954 | |
| 6955 | void |
| 6956 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6957 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6958 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6959 | |
| 6960 | /* |
| 6961 | * We know that '<?xml' is here. |
| 6962 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6963 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6964 | SKIP(5); |
| 6965 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6966 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6967 | return; |
| 6968 | } |
| 6969 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6970 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6971 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6972 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6973 | } |
| 6974 | SKIP_BLANKS; |
| 6975 | |
| 6976 | /* |
| 6977 | * We may have the VersionInfo here. |
| 6978 | */ |
| 6979 | version = xmlParseVersionInfo(ctxt); |
| 6980 | if (version == NULL) |
| 6981 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6982 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6983 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6984 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6985 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6986 | } |
| 6987 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6988 | ctxt->input->version = version; |
| 6989 | |
| 6990 | /* |
| 6991 | * We must have the encoding declaration |
| 6992 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6993 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6994 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6995 | /* |
| 6996 | * The XML REC instructs us to stop parsing right here |
| 6997 | */ |
| 6998 | return; |
| 6999 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 7000 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 7001 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 7002 | "Missing encoding in text declaration\n"); |
| 7003 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7004 | |
| 7005 | SKIP_BLANKS; |
| 7006 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 7007 | SKIP(2); |
| 7008 | } else if (RAW == '>') { |
| 7009 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7010 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7011 | NEXT; |
| 7012 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7013 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7014 | MOVETO_ENDTAG(CUR_PTR); |
| 7015 | NEXT; |
| 7016 | } |
| 7017 | } |
| 7018 | |
| 7019 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7020 | * xmlParseExternalSubset: |
| 7021 | * @ctxt: an XML parser context |
| 7022 | * @ExternalID: the external identifier |
| 7023 | * @SystemID: the system identifier (or URL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7024 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7025 | * parse Markup declarations from an external subset |
| 7026 | * |
| 7027 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 7028 | * |
| 7029 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 7030 | */ |
| 7031 | void |
| 7032 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 7033 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 7034 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7035 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7036 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 7037 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7038 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 7039 | xmlChar start[4]; |
| 7040 | xmlCharEncoding enc; |
| 7041 | |
| 7042 | start[0] = RAW; |
| 7043 | start[1] = NXT(1); |
| 7044 | start[2] = NXT(2); |
| 7045 | start[3] = NXT(3); |
| 7046 | enc = xmlDetectCharEncoding(start, 4); |
| 7047 | if (enc != XML_CHAR_ENCODING_NONE) |
| 7048 | xmlSwitchEncoding(ctxt, enc); |
| 7049 | } |
| 7050 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7051 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7052 | xmlParseTextDecl(ctxt); |
| 7053 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 7054 | /* |
| 7055 | * The XML REC instructs us to stop parsing right here |
| 7056 | */ |
| 7057 | ctxt->instate = XML_PARSER_EOF; |
| 7058 | return; |
| 7059 | } |
| 7060 | } |
| 7061 | if (ctxt->myDoc == NULL) { |
| 7062 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 7063 | if (ctxt->myDoc == NULL) { |
| 7064 | xmlErrMemory(ctxt, "New Doc failed"); |
| 7065 | return; |
| 7066 | } |
| 7067 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7068 | } |
| 7069 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 7070 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 7071 | |
| 7072 | ctxt->instate = XML_PARSER_DTD; |
| 7073 | ctxt->external = 1; |
| 7074 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 7075 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7076 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7077 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7078 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7079 | |
| 7080 | GROW; |
| 7081 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 7082 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7083 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7084 | NEXT; |
| 7085 | } else if (RAW == '%') { |
| 7086 | xmlParsePEReference(ctxt); |
| 7087 | } else |
| 7088 | xmlParseMarkupDecl(ctxt); |
| 7089 | |
| 7090 | /* |
| 7091 | * Pop-up of finished entities. |
| 7092 | */ |
| 7093 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7094 | xmlPopInput(ctxt); |
| 7095 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7096 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7097 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7098 | break; |
| 7099 | } |
| 7100 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7101 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7102 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7103 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7104 | } |
| 7105 | |
| 7106 | } |
| 7107 | |
| 7108 | /** |
| 7109 | * xmlParseReference: |
| 7110 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7111 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7112 | * parse and handle entity references in content, depending on the SAX |
| 7113 | * interface, this may end-up in a call to character() if this is a |
| 7114 | * CharRef, a predefined entity, if there is no reference() callback. |
| 7115 | * or if the parser was asked to switch to that mode. |
| 7116 | * |
| 7117 | * [67] Reference ::= EntityRef | CharRef |
| 7118 | */ |
| 7119 | void |
| 7120 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 7121 | xmlEntityPtr ent; |
| 7122 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7123 | int was_checked; |
| 7124 | xmlNodePtr list = NULL; |
| 7125 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7126 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7127 | |
| 7128 | if (RAW != '&') |
| 7129 | return; |
| 7130 | |
| 7131 | /* |
| 7132 | * Simple case of a CharRef |
| 7133 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7134 | if (NXT(1) == '#') { |
| 7135 | int i = 0; |
| 7136 | xmlChar out[10]; |
| 7137 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7138 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7139 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 7140 | if (value == 0) |
| 7141 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7142 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 7143 | /* |
| 7144 | * So we are using non-UTF-8 buffers |
| 7145 | * Check that the char fit on 8bits, if not |
| 7146 | * generate a CharRef. |
| 7147 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7148 | if (value <= 0xFF) { |
| 7149 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7150 | out[1] = 0; |
| 7151 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7152 | (!ctxt->disableSAX)) |
| 7153 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 7154 | } else { |
| 7155 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7156 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7157 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7158 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7159 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7160 | (!ctxt->disableSAX)) |
| 7161 | ctxt->sax->reference(ctxt->userData, out); |
| 7162 | } |
| 7163 | } else { |
| 7164 | /* |
| 7165 | * Just encode the value in UTF-8 |
| 7166 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7167 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7168 | out[i] = 0; |
| 7169 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7170 | (!ctxt->disableSAX)) |
| 7171 | ctxt->sax->characters(ctxt->userData, out, i); |
| 7172 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7173 | return; |
| 7174 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7175 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7176 | /* |
| 7177 | * We are seeing an entity reference |
| 7178 | */ |
| 7179 | ent = xmlParseEntityRef(ctxt); |
| 7180 | if (ent == NULL) return; |
| 7181 | if (!ctxt->wellFormed) |
| 7182 | return; |
| 7183 | was_checked = ent->checked; |
| 7184 | |
| 7185 | /* special case of predefined entities */ |
| 7186 | if ((ent->name == NULL) || |
| 7187 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 7188 | val = ent->content; |
| 7189 | if (val == NULL) return; |
| 7190 | /* |
| 7191 | * inline the entity. |
| 7192 | */ |
| 7193 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7194 | (!ctxt->disableSAX)) |
| 7195 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 7196 | return; |
| 7197 | } |
| 7198 | |
| 7199 | /* |
| 7200 | * The first reference to the entity trigger a parsing phase |
| 7201 | * where the ent->children is filled with the result from |
| 7202 | * the parsing. |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7203 | * Note: external parsed entities will not be loaded, it is not |
| 7204 | * required for a non-validating parser, unless the parsing option |
| 7205 | * of validating, or substituting entities were given. Doing so is |
| 7206 | * far more secure as the parser will only process data coming from |
| 7207 | * the document entity by default. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7208 | */ |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7209 | if ((ent->checked == 0) && |
| 7210 | ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
| 7211 | (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7212 | unsigned long oldnbent = ctxt->nbentities; |
| 7213 | |
| 7214 | /* |
| 7215 | * This is a bit hackish but this seems the best |
| 7216 | * way to make sure both SAX and DOM entity support |
| 7217 | * behaves okay. |
| 7218 | */ |
| 7219 | void *user_data; |
| 7220 | if (ctxt->userData == ctxt) |
| 7221 | user_data = NULL; |
| 7222 | else |
| 7223 | user_data = ctxt->userData; |
| 7224 | |
| 7225 | /* |
| 7226 | * Check that this entity is well formed |
| 7227 | * 4.3.2: An internal general parsed entity is well-formed |
| 7228 | * if its replacement text matches the production labeled |
| 7229 | * content. |
| 7230 | */ |
| 7231 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7232 | ctxt->depth++; |
| 7233 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 7234 | user_data, &list); |
| 7235 | ctxt->depth--; |
| 7236 | |
| 7237 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7238 | ctxt->depth++; |
| 7239 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 7240 | user_data, ctxt->depth, ent->URI, |
| 7241 | ent->ExternalID, &list); |
| 7242 | ctxt->depth--; |
| 7243 | } else { |
| 7244 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7245 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7246 | "invalid entity type found\n", NULL); |
| 7247 | } |
| 7248 | |
| 7249 | /* |
| 7250 | * Store the number of entities needing parsing for this entity |
| 7251 | * content and do checkings |
| 7252 | */ |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7253 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
| 7254 | if ((ent->content != NULL) && (xmlStrchr(ent->content, '<'))) |
| 7255 | ent->checked |= 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7256 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7257 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7258 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7259 | return; |
| 7260 | } |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7261 | if (xmlParserEntityCheck(ctxt, 0, ent, 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7262 | xmlFreeNodeList(list); |
| 7263 | return; |
| 7264 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7266 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 7267 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 7268 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 7269 | (ent->children == NULL)) { |
| 7270 | ent->children = list; |
| 7271 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7272 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7273 | * Prune it directly in the generated document |
| 7274 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7275 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7276 | if (((list->type == XML_TEXT_NODE) && |
| 7277 | (list->next == NULL)) || |
| 7278 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7279 | list->parent = (xmlNodePtr) ent; |
| 7280 | list = NULL; |
| 7281 | ent->owner = 1; |
| 7282 | } else { |
| 7283 | ent->owner = 0; |
| 7284 | while (list != NULL) { |
| 7285 | list->parent = (xmlNodePtr) ctxt->node; |
| 7286 | list->doc = ctxt->myDoc; |
| 7287 | if (list->next == NULL) |
| 7288 | ent->last = list; |
| 7289 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7290 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7291 | list = ent->children; |
| 7292 | #ifdef LIBXML_LEGACY_ENABLED |
| 7293 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7294 | xmlAddEntityReference(ent, list, NULL); |
| 7295 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7296 | } |
| 7297 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7298 | ent->owner = 1; |
| 7299 | while (list != NULL) { |
| 7300 | list->parent = (xmlNodePtr) ent; |
Rob Richards | c794eb5 | 2011-02-18 12:17:17 -0500 | [diff] [blame] | 7301 | xmlSetTreeDoc(list, ent->doc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7302 | if (list->next == NULL) |
| 7303 | ent->last = list; |
| 7304 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7305 | } |
| 7306 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7307 | } else { |
| 7308 | xmlFreeNodeList(list); |
| 7309 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7310 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7311 | } else if ((ret != XML_ERR_OK) && |
| 7312 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 7313 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7314 | "Entity '%s' failed to parse\n", ent->name); |
| 7315 | } else if (list != NULL) { |
| 7316 | xmlFreeNodeList(list); |
| 7317 | list = NULL; |
| 7318 | } |
| 7319 | if (ent->checked == 0) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7320 | ent->checked = 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7321 | } else if (ent->checked != 1) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7322 | ctxt->nbentities += ent->checked / 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7323 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7324 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7325 | /* |
| 7326 | * Now that the entity content has been gathered |
| 7327 | * provide it to the application, this can take different forms based |
| 7328 | * on the parsing modes. |
| 7329 | */ |
| 7330 | if (ent->children == NULL) { |
| 7331 | /* |
| 7332 | * Probably running in SAX mode and the callbacks don't |
| 7333 | * build the entity content. So unless we already went |
| 7334 | * though parsing for first checking go though the entity |
| 7335 | * content to generate callbacks associated to the entity |
| 7336 | */ |
| 7337 | if (was_checked != 0) { |
| 7338 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7339 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7340 | * This is a bit hackish but this seems the best |
| 7341 | * way to make sure both SAX and DOM entity support |
| 7342 | * behaves okay. |
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 | if (ctxt->userData == ctxt) |
| 7345 | user_data = NULL; |
| 7346 | else |
| 7347 | user_data = ctxt->userData; |
| 7348 | |
| 7349 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7350 | ctxt->depth++; |
| 7351 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 7352 | ent->content, user_data, NULL); |
| 7353 | ctxt->depth--; |
| 7354 | } else if (ent->etype == |
| 7355 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7356 | ctxt->depth++; |
| 7357 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 7358 | ctxt->sax, user_data, ctxt->depth, |
| 7359 | ent->URI, ent->ExternalID, NULL); |
| 7360 | ctxt->depth--; |
| 7361 | } else { |
| 7362 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7363 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7364 | "invalid entity type found\n", NULL); |
| 7365 | } |
| 7366 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 7367 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 7368 | return; |
| 7369 | } |
| 7370 | } |
| 7371 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7372 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7373 | /* |
| 7374 | * Entity reference callback comes second, it's somewhat |
| 7375 | * superfluous but a compatibility to historical behaviour |
| 7376 | */ |
| 7377 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7378 | } |
| 7379 | return; |
| 7380 | } |
| 7381 | |
| 7382 | /* |
| 7383 | * If we didn't get any children for the entity being built |
| 7384 | */ |
| 7385 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7386 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7387 | /* |
| 7388 | * Create a node. |
| 7389 | */ |
| 7390 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7391 | return; |
| 7392 | } |
| 7393 | |
| 7394 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 7395 | /* |
| 7396 | * There is a problem on the handling of _private for entities |
| 7397 | * (bug 155816): Should we copy the content of the field from |
| 7398 | * the entity (possibly overwriting some value set by the user |
| 7399 | * when a copy is created), should we leave it alone, or should |
| 7400 | * we try to take care of different situations? The problem |
| 7401 | * is exacerbated by the usage of this field by the xmlReader. |
| 7402 | * To fix this bug, we look at _private on the created node |
| 7403 | * and, if it's NULL, we copy in whatever was in the entity. |
| 7404 | * If it's not NULL we leave it alone. This is somewhat of a |
| 7405 | * hack - maybe we should have further tests to determine |
| 7406 | * what to do. |
| 7407 | */ |
| 7408 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 7409 | /* |
| 7410 | * Seems we are generating the DOM content, do |
| 7411 | * a simple tree copy for all references except the first |
| 7412 | * In the first occurrence list contains the replacement. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7413 | */ |
| 7414 | if (((list == NULL) && (ent->owner == 0)) || |
| 7415 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7416 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 7417 | |
| 7418 | /* |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7419 | * We are copying here, make sure there is no abuse |
| 7420 | */ |
| 7421 | ctxt->sizeentcopy += ent->length; |
| 7422 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7423 | return; |
| 7424 | |
| 7425 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7426 | * when operating on a reader, the entities definitions |
| 7427 | * are always owning the entities subtree. |
| 7428 | if (ctxt->parseMode == XML_PARSE_READER) |
| 7429 | ent->owner = 1; |
| 7430 | */ |
| 7431 | |
| 7432 | cur = ent->children; |
| 7433 | while (cur != NULL) { |
| 7434 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7435 | if (nw != NULL) { |
| 7436 | if (nw->_private == NULL) |
| 7437 | nw->_private = cur->_private; |
| 7438 | if (firstChild == NULL){ |
| 7439 | firstChild = nw; |
| 7440 | } |
| 7441 | nw = xmlAddChild(ctxt->node, nw); |
| 7442 | } |
| 7443 | if (cur == ent->last) { |
| 7444 | /* |
| 7445 | * needed to detect some strange empty |
| 7446 | * node cases in the reader tests |
| 7447 | */ |
| 7448 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 7449 | (nw != NULL) && |
| 7450 | (nw->type == XML_ELEMENT_NODE) && |
| 7451 | (nw->children == NULL)) |
| 7452 | nw->extra = 1; |
| 7453 | |
| 7454 | break; |
| 7455 | } |
| 7456 | cur = cur->next; |
| 7457 | } |
| 7458 | #ifdef LIBXML_LEGACY_ENABLED |
| 7459 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7460 | xmlAddEntityReference(ent, firstChild, nw); |
| 7461 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 28f5e1a | 2012-09-04 11:18:39 +0800 | [diff] [blame] | 7462 | } else if ((list == NULL) || (ctxt->inputNr > 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7463 | xmlNodePtr nw = NULL, cur, next, last, |
| 7464 | firstChild = NULL; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7465 | |
| 7466 | /* |
| 7467 | * We are copying here, make sure there is no abuse |
| 7468 | */ |
| 7469 | ctxt->sizeentcopy += ent->length; |
| 7470 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7471 | return; |
| 7472 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7473 | /* |
| 7474 | * Copy the entity child list and make it the new |
| 7475 | * entity child list. The goal is to make sure any |
| 7476 | * ID or REF referenced will be the one from the |
| 7477 | * document content and not the entity copy. |
| 7478 | */ |
| 7479 | cur = ent->children; |
| 7480 | ent->children = NULL; |
| 7481 | last = ent->last; |
| 7482 | ent->last = NULL; |
| 7483 | while (cur != NULL) { |
| 7484 | next = cur->next; |
| 7485 | cur->next = NULL; |
| 7486 | cur->parent = NULL; |
| 7487 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7488 | if (nw != NULL) { |
| 7489 | if (nw->_private == NULL) |
| 7490 | nw->_private = cur->_private; |
| 7491 | if (firstChild == NULL){ |
| 7492 | firstChild = cur; |
| 7493 | } |
| 7494 | xmlAddChild((xmlNodePtr) ent, nw); |
| 7495 | xmlAddChild(ctxt->node, cur); |
| 7496 | } |
| 7497 | if (cur == last) |
| 7498 | break; |
| 7499 | cur = next; |
| 7500 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 7501 | if (ent->owner == 0) |
| 7502 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7503 | #ifdef LIBXML_LEGACY_ENABLED |
| 7504 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7505 | xmlAddEntityReference(ent, firstChild, nw); |
| 7506 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7507 | } else { |
| 7508 | const xmlChar *nbktext; |
| 7509 | |
| 7510 | /* |
| 7511 | * the name change is to avoid coalescing of the |
| 7512 | * node with a possible previous text one which |
| 7513 | * would make ent->children a dangling pointer |
| 7514 | */ |
| 7515 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 7516 | -1); |
| 7517 | if (ent->children->type == XML_TEXT_NODE) |
| 7518 | ent->children->name = nbktext; |
| 7519 | if ((ent->last != ent->children) && |
| 7520 | (ent->last->type == XML_TEXT_NODE)) |
| 7521 | ent->last->name = nbktext; |
| 7522 | xmlAddChildList(ctxt->node, ent->children); |
| 7523 | } |
| 7524 | |
| 7525 | /* |
| 7526 | * This is to avoid a nasty side effect, see |
| 7527 | * characters() in SAX.c |
| 7528 | */ |
| 7529 | ctxt->nodemem = 0; |
| 7530 | ctxt->nodelen = 0; |
| 7531 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7532 | } |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | /** |
| 7537 | * xmlParseEntityRef: |
| 7538 | * @ctxt: an XML parser context |
| 7539 | * |
| 7540 | * parse ENTITY references declarations |
| 7541 | * |
| 7542 | * [68] EntityRef ::= '&' Name ';' |
| 7543 | * |
| 7544 | * [ WFC: Entity Declared ] |
| 7545 | * In a document without any DTD, a document with only an internal DTD |
| 7546 | * subset which contains no parameter entity references, or a document |
| 7547 | * with "standalone='yes'", the Name given in the entity reference |
| 7548 | * must match that in an entity declaration, except that well-formed |
| 7549 | * documents need not declare any of the following entities: amp, lt, |
| 7550 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7551 | * any reference to it. Similarly, the declaration of a general entity |
| 7552 | * must precede any reference to it which appears in a default value in an |
| 7553 | * attribute-list declaration. Note that if entities are declared in the |
| 7554 | * external subset or in external parameter entities, a non-validating |
| 7555 | * processor is not obligated to read and process their declarations; |
| 7556 | * for such documents, the rule that an entity must be declared is a |
| 7557 | * well-formedness constraint only if standalone='yes'. |
| 7558 | * |
| 7559 | * [ WFC: Parsed Entity ] |
| 7560 | * An entity reference must not contain the name of an unparsed entity |
| 7561 | * |
| 7562 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7563 | */ |
| 7564 | xmlEntityPtr |
| 7565 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7566 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7567 | xmlEntityPtr ent = NULL; |
| 7568 | |
| 7569 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7570 | if (ctxt->instate == XML_PARSER_EOF) |
| 7571 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7572 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7573 | if (RAW != '&') |
| 7574 | return(NULL); |
| 7575 | NEXT; |
| 7576 | name = xmlParseName(ctxt); |
| 7577 | if (name == NULL) { |
| 7578 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7579 | "xmlParseEntityRef: no name\n"); |
| 7580 | return(NULL); |
| 7581 | } |
| 7582 | if (RAW != ';') { |
| 7583 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7584 | return(NULL); |
| 7585 | } |
| 7586 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7587 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7588 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7589 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7590 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7591 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7592 | ent = xmlGetPredefinedEntity(name); |
| 7593 | if (ent != NULL) |
| 7594 | return(ent); |
| 7595 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7596 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7597 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7598 | * Increase the number of entity references parsed |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7599 | */ |
| 7600 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7601 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7602 | /* |
| 7603 | * Ask first SAX for entity resolution, otherwise try the |
| 7604 | * entities which may have stored in the parser context. |
| 7605 | */ |
| 7606 | if (ctxt->sax != NULL) { |
| 7607 | if (ctxt->sax->getEntity != NULL) |
| 7608 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7609 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7610 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7611 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7612 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7613 | (ctxt->userData==ctxt)) { |
| 7614 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7615 | } |
| 7616 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7617 | if (ctxt->instate == XML_PARSER_EOF) |
| 7618 | return(NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7619 | /* |
| 7620 | * [ WFC: Entity Declared ] |
| 7621 | * In a document without any DTD, a document with only an |
| 7622 | * internal DTD subset which contains no parameter entity |
| 7623 | * references, or a document with "standalone='yes'", the |
| 7624 | * Name given in the entity reference must match that in an |
| 7625 | * entity declaration, except that well-formed documents |
| 7626 | * need not declare any of the following entities: amp, lt, |
| 7627 | * gt, apos, quot. |
| 7628 | * The declaration of a parameter entity must precede any |
| 7629 | * reference to it. |
| 7630 | * Similarly, the declaration of a general entity must |
| 7631 | * precede any reference to it which appears in a default |
| 7632 | * value in an attribute-list declaration. Note that if |
| 7633 | * entities are declared in the external subset or in |
| 7634 | * external parameter entities, a non-validating processor |
| 7635 | * is not obligated to read and process their declarations; |
| 7636 | * for such documents, the rule that an entity must be |
| 7637 | * declared is a well-formedness constraint only if |
| 7638 | * standalone='yes'. |
| 7639 | */ |
| 7640 | if (ent == NULL) { |
| 7641 | if ((ctxt->standalone == 1) || |
| 7642 | ((ctxt->hasExternalSubset == 0) && |
| 7643 | (ctxt->hasPErefs == 0))) { |
| 7644 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7645 | "Entity '%s' not defined\n", name); |
| 7646 | } else { |
| 7647 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7648 | "Entity '%s' not defined\n", name); |
| 7649 | if ((ctxt->inSubset == 0) && |
| 7650 | (ctxt->sax != NULL) && |
| 7651 | (ctxt->sax->reference != NULL)) { |
| 7652 | ctxt->sax->reference(ctxt->userData, name); |
| 7653 | } |
| 7654 | } |
| 7655 | ctxt->valid = 0; |
| 7656 | } |
| 7657 | |
| 7658 | /* |
| 7659 | * [ WFC: Parsed Entity ] |
| 7660 | * An entity reference must not contain the name of an |
| 7661 | * unparsed entity |
| 7662 | */ |
| 7663 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7664 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7665 | "Entity reference to unparsed entity %s\n", name); |
| 7666 | } |
| 7667 | |
| 7668 | /* |
| 7669 | * [ WFC: No External Entity References ] |
| 7670 | * Attribute values cannot contain direct or indirect |
| 7671 | * entity references to external entities. |
| 7672 | */ |
| 7673 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7674 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7675 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7676 | "Attribute references external entity '%s'\n", name); |
| 7677 | } |
| 7678 | /* |
| 7679 | * [ WFC: No < in Attribute Values ] |
| 7680 | * The replacement text of any entity referred to directly or |
| 7681 | * indirectly in an attribute value (other than "<") must |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7682 | * not contain a <. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7683 | */ |
| 7684 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7685 | (ent != NULL) && |
| 7686 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 7cf5738 | 2014-10-08 16:09:56 +0800 | [diff] [blame] | 7687 | if (((ent->checked & 1) || (ent->checked == 0)) && |
| 7688 | (ent->content != NULL) && (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7689 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7690 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7691 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7692 | } |
| 7693 | |
| 7694 | /* |
| 7695 | * Internal check, no parameter entities here ... |
| 7696 | */ |
| 7697 | else { |
| 7698 | switch (ent->etype) { |
| 7699 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7700 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7701 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7702 | "Attempt to reference the parameter entity '%s'\n", |
| 7703 | name); |
| 7704 | break; |
| 7705 | default: |
| 7706 | break; |
| 7707 | } |
| 7708 | } |
| 7709 | |
| 7710 | /* |
| 7711 | * [ WFC: No Recursion ] |
| 7712 | * A parsed entity must not contain a recursive reference |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7713 | * to itself, either directly or indirectly. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7714 | * Done somewhere else |
| 7715 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7716 | return(ent); |
| 7717 | } |
| 7718 | |
| 7719 | /** |
| 7720 | * xmlParseStringEntityRef: |
| 7721 | * @ctxt: an XML parser context |
| 7722 | * @str: a pointer to an index in the string |
| 7723 | * |
| 7724 | * parse ENTITY references declarations, but this version parses it from |
| 7725 | * a string value. |
| 7726 | * |
| 7727 | * [68] EntityRef ::= '&' Name ';' |
| 7728 | * |
| 7729 | * [ WFC: Entity Declared ] |
| 7730 | * In a document without any DTD, a document with only an internal DTD |
| 7731 | * subset which contains no parameter entity references, or a document |
| 7732 | * with "standalone='yes'", the Name given in the entity reference |
| 7733 | * must match that in an entity declaration, except that well-formed |
| 7734 | * documents need not declare any of the following entities: amp, lt, |
| 7735 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7736 | * any reference to it. Similarly, the declaration of a general entity |
| 7737 | * must precede any reference to it which appears in a default value in an |
| 7738 | * attribute-list declaration. Note that if entities are declared in the |
| 7739 | * external subset or in external parameter entities, a non-validating |
| 7740 | * processor is not obligated to read and process their declarations; |
| 7741 | * for such documents, the rule that an entity must be declared is a |
| 7742 | * well-formedness constraint only if standalone='yes'. |
| 7743 | * |
| 7744 | * [ WFC: Parsed Entity ] |
| 7745 | * An entity reference must not contain the name of an unparsed entity |
| 7746 | * |
| 7747 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7748 | * is updated to the current location in the string. |
| 7749 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7750 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7751 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7752 | xmlChar *name; |
| 7753 | const xmlChar *ptr; |
| 7754 | xmlChar cur; |
| 7755 | xmlEntityPtr ent = NULL; |
| 7756 | |
| 7757 | if ((str == NULL) || (*str == NULL)) |
| 7758 | return(NULL); |
| 7759 | ptr = *str; |
| 7760 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7761 | if (cur != '&') |
| 7762 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7763 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7764 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7765 | name = xmlParseStringName(ctxt, &ptr); |
| 7766 | if (name == NULL) { |
| 7767 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7768 | "xmlParseStringEntityRef: no name\n"); |
| 7769 | *str = ptr; |
| 7770 | return(NULL); |
| 7771 | } |
| 7772 | if (*ptr != ';') { |
| 7773 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7774 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7775 | *str = ptr; |
| 7776 | return(NULL); |
| 7777 | } |
| 7778 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7779 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7780 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7781 | /* |
Jan Pokorný | 9a85d40 | 2013-11-29 23:26:25 +0100 | [diff] [blame] | 7782 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7783 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7784 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7785 | ent = xmlGetPredefinedEntity(name); |
| 7786 | if (ent != NULL) { |
| 7787 | xmlFree(name); |
| 7788 | *str = ptr; |
| 7789 | return(ent); |
| 7790 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7791 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7792 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7793 | /* |
| 7794 | * Increate the number of entity references parsed |
| 7795 | */ |
| 7796 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7797 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7798 | /* |
| 7799 | * Ask first SAX for entity resolution, otherwise try the |
| 7800 | * entities which may have stored in the parser context. |
| 7801 | */ |
| 7802 | if (ctxt->sax != NULL) { |
| 7803 | if (ctxt->sax->getEntity != NULL) |
| 7804 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7805 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7806 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7807 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7808 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7809 | } |
| 7810 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7811 | if (ctxt->instate == XML_PARSER_EOF) { |
| 7812 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 7813 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7814 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7815 | |
| 7816 | /* |
| 7817 | * [ WFC: Entity Declared ] |
| 7818 | * In a document without any DTD, a document with only an |
| 7819 | * internal DTD subset which contains no parameter entity |
| 7820 | * references, or a document with "standalone='yes'", the |
| 7821 | * Name given in the entity reference must match that in an |
| 7822 | * entity declaration, except that well-formed documents |
| 7823 | * need not declare any of the following entities: amp, lt, |
| 7824 | * gt, apos, quot. |
| 7825 | * The declaration of a parameter entity must precede any |
| 7826 | * reference to it. |
| 7827 | * Similarly, the declaration of a general entity must |
| 7828 | * precede any reference to it which appears in a default |
| 7829 | * value in an attribute-list declaration. Note that if |
| 7830 | * entities are declared in the external subset or in |
| 7831 | * external parameter entities, a non-validating processor |
| 7832 | * is not obligated to read and process their declarations; |
| 7833 | * for such documents, the rule that an entity must be |
| 7834 | * declared is a well-formedness constraint only if |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7835 | * standalone='yes'. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7836 | */ |
| 7837 | if (ent == NULL) { |
| 7838 | if ((ctxt->standalone == 1) || |
| 7839 | ((ctxt->hasExternalSubset == 0) && |
| 7840 | (ctxt->hasPErefs == 0))) { |
| 7841 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7842 | "Entity '%s' not defined\n", name); |
| 7843 | } else { |
| 7844 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7845 | "Entity '%s' not defined\n", |
| 7846 | name); |
| 7847 | } |
| 7848 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7849 | } |
| 7850 | |
| 7851 | /* |
| 7852 | * [ WFC: Parsed Entity ] |
| 7853 | * An entity reference must not contain the name of an |
| 7854 | * unparsed entity |
| 7855 | */ |
| 7856 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7857 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7858 | "Entity reference to unparsed entity %s\n", name); |
| 7859 | } |
| 7860 | |
| 7861 | /* |
| 7862 | * [ WFC: No External Entity References ] |
| 7863 | * Attribute values cannot contain direct or indirect |
| 7864 | * entity references to external entities. |
| 7865 | */ |
| 7866 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7867 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7868 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7869 | "Attribute references external entity '%s'\n", name); |
| 7870 | } |
| 7871 | /* |
| 7872 | * [ WFC: No < in Attribute Values ] |
| 7873 | * The replacement text of any entity referred to directly or |
| 7874 | * indirectly in an attribute value (other than "<") must |
| 7875 | * not contain a <. |
| 7876 | */ |
| 7877 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7878 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7879 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7880 | (xmlStrchr(ent->content, '<'))) { |
| 7881 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7882 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7883 | name); |
| 7884 | } |
| 7885 | |
| 7886 | /* |
| 7887 | * Internal check, no parameter entities here ... |
| 7888 | */ |
| 7889 | else { |
| 7890 | switch (ent->etype) { |
| 7891 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7892 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7893 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7894 | "Attempt to reference the parameter entity '%s'\n", |
| 7895 | name); |
| 7896 | break; |
| 7897 | default: |
| 7898 | break; |
| 7899 | } |
| 7900 | } |
| 7901 | |
| 7902 | /* |
| 7903 | * [ WFC: No Recursion ] |
| 7904 | * A parsed entity must not contain a recursive reference |
| 7905 | * to itself, either directly or indirectly. |
| 7906 | * Done somewhere else |
| 7907 | */ |
| 7908 | |
| 7909 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7910 | *str = ptr; |
| 7911 | return(ent); |
| 7912 | } |
| 7913 | |
| 7914 | /** |
| 7915 | * xmlParsePEReference: |
| 7916 | * @ctxt: an XML parser context |
| 7917 | * |
| 7918 | * parse PEReference declarations |
| 7919 | * The entity content is handled directly by pushing it's content as |
| 7920 | * a new input stream. |
| 7921 | * |
| 7922 | * [69] PEReference ::= '%' Name ';' |
| 7923 | * |
| 7924 | * [ WFC: No Recursion ] |
| 7925 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7926 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7927 | * |
| 7928 | * [ WFC: Entity Declared ] |
| 7929 | * In a document without any DTD, a document with only an internal DTD |
| 7930 | * subset which contains no parameter entity references, or a document |
| 7931 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7932 | * entity must precede any reference to it... |
| 7933 | * |
| 7934 | * [ VC: Entity Declared ] |
| 7935 | * In a document with an external subset or external parameter entities |
| 7936 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7937 | * must precede any reference to it... |
| 7938 | * |
| 7939 | * [ WFC: In DTD ] |
| 7940 | * Parameter-entity references may only appear in the DTD. |
| 7941 | * NOTE: misleading but this is handled. |
| 7942 | */ |
| 7943 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7944 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7945 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7946 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7947 | xmlEntityPtr entity = NULL; |
| 7948 | xmlParserInputPtr input; |
| 7949 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7950 | if (RAW != '%') |
| 7951 | return; |
| 7952 | NEXT; |
| 7953 | name = xmlParseName(ctxt); |
| 7954 | if (name == NULL) { |
| 7955 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7956 | "xmlParsePEReference: no name\n"); |
| 7957 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7958 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7959 | if (RAW != ';') { |
| 7960 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7961 | return; |
| 7962 | } |
| 7963 | |
| 7964 | NEXT; |
| 7965 | |
| 7966 | /* |
| 7967 | * Increate the number of entity references parsed |
| 7968 | */ |
| 7969 | ctxt->nbentities++; |
| 7970 | |
| 7971 | /* |
| 7972 | * Request the entity from SAX |
| 7973 | */ |
| 7974 | if ((ctxt->sax != NULL) && |
| 7975 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7976 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 7977 | if (ctxt->instate == XML_PARSER_EOF) |
| 7978 | return; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7979 | if (entity == NULL) { |
| 7980 | /* |
| 7981 | * [ WFC: Entity Declared ] |
| 7982 | * In a document without any DTD, a document with only an |
| 7983 | * internal DTD subset which contains no parameter entity |
| 7984 | * references, or a document with "standalone='yes'", ... |
| 7985 | * ... The declaration of a parameter entity must precede |
| 7986 | * any reference to it... |
| 7987 | */ |
| 7988 | if ((ctxt->standalone == 1) || |
| 7989 | ((ctxt->hasExternalSubset == 0) && |
| 7990 | (ctxt->hasPErefs == 0))) { |
| 7991 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7992 | "PEReference: %%%s; not found\n", |
| 7993 | name); |
| 7994 | } else { |
| 7995 | /* |
| 7996 | * [ VC: Entity Declared ] |
| 7997 | * In a document with an external subset or external |
| 7998 | * parameter entities with "standalone='no'", ... |
| 7999 | * ... The declaration of a parameter entity must |
| 8000 | * precede any reference to it... |
| 8001 | */ |
| 8002 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8003 | "PEReference: %%%s; not found\n", |
| 8004 | name, NULL); |
| 8005 | ctxt->valid = 0; |
| 8006 | } |
| 8007 | } else { |
| 8008 | /* |
| 8009 | * Internal checking in case the entity quest barfed |
| 8010 | */ |
| 8011 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8012 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8013 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8014 | "Internal: %%%s; is not a parameter entity\n", |
| 8015 | name, NULL); |
| 8016 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 8017 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 8018 | if (xmlPushInput(ctxt, input) < 0) |
| 8019 | return; |
| 8020 | } else { |
| 8021 | /* |
| 8022 | * TODO !!! |
| 8023 | * handle the extra spaces added before and after |
| 8024 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 8025 | */ |
| 8026 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8027 | if (xmlPushInput(ctxt, input) < 0) |
| 8028 | return; |
| 8029 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8030 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 8031 | (IS_BLANK_CH(NXT(5)))) { |
| 8032 | xmlParseTextDecl(ctxt); |
| 8033 | if (ctxt->errNo == |
| 8034 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 8035 | /* |
| 8036 | * The XML REC instructs us to stop parsing |
| 8037 | * right here |
| 8038 | */ |
| 8039 | ctxt->instate = XML_PARSER_EOF; |
| 8040 | return; |
| 8041 | } |
| 8042 | } |
| 8043 | } |
| 8044 | } |
| 8045 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8046 | } |
| 8047 | |
| 8048 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8049 | * xmlLoadEntityContent: |
| 8050 | * @ctxt: an XML parser context |
| 8051 | * @entity: an unloaded system entity |
| 8052 | * |
| 8053 | * Load the original content of the given system entity from the |
| 8054 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 8055 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 8056 | * |
| 8057 | * Returns 0 in case of success and -1 in case of failure |
| 8058 | */ |
| 8059 | static int |
| 8060 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 8061 | xmlParserInputPtr input; |
| 8062 | xmlBufferPtr buf; |
| 8063 | int l, c; |
| 8064 | int count = 0; |
| 8065 | |
| 8066 | if ((ctxt == NULL) || (entity == NULL) || |
| 8067 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8068 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 8069 | (entity->content != NULL)) { |
| 8070 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8071 | "xmlLoadEntityContent parameter error"); |
| 8072 | return(-1); |
| 8073 | } |
| 8074 | |
| 8075 | if (xmlParserDebugEntities) |
| 8076 | xmlGenericError(xmlGenericErrorContext, |
| 8077 | "Reading %s entity content input\n", entity->name); |
| 8078 | |
| 8079 | buf = xmlBufferCreate(); |
| 8080 | if (buf == NULL) { |
| 8081 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8082 | "xmlLoadEntityContent parameter error"); |
| 8083 | return(-1); |
| 8084 | } |
| 8085 | |
| 8086 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8087 | if (input == NULL) { |
| 8088 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8089 | "xmlLoadEntityContent input error"); |
| 8090 | xmlBufferFree(buf); |
| 8091 | return(-1); |
| 8092 | } |
| 8093 | |
| 8094 | /* |
| 8095 | * Push the entity as the current input, read char by char |
| 8096 | * saving to the buffer until the end of the entity or an error |
| 8097 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 8098 | if (xmlPushInput(ctxt, input) < 0) { |
| 8099 | xmlBufferFree(buf); |
| 8100 | return(-1); |
| 8101 | } |
| 8102 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8103 | GROW; |
| 8104 | c = CUR_CHAR(l); |
| 8105 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 8106 | (IS_CHAR(c))) { |
| 8107 | xmlBufferAdd(buf, ctxt->input->cur, l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8108 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8109 | count = 0; |
| 8110 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8111 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8112 | xmlBufferFree(buf); |
| 8113 | return(-1); |
| 8114 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8115 | } |
| 8116 | NEXTL(l); |
| 8117 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8118 | if (c == 0) { |
| 8119 | count = 0; |
| 8120 | GROW; |
| 8121 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8122 | xmlBufferFree(buf); |
| 8123 | return(-1); |
| 8124 | } |
| 8125 | c = CUR_CHAR(l); |
| 8126 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
| 8129 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 8130 | xmlPopInput(ctxt); |
| 8131 | } else if (!IS_CHAR(c)) { |
| 8132 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 8133 | "xmlLoadEntityContent: invalid char value %d\n", |
| 8134 | c); |
| 8135 | xmlBufferFree(buf); |
| 8136 | return(-1); |
| 8137 | } |
| 8138 | entity->content = buf->content; |
| 8139 | buf->content = NULL; |
| 8140 | xmlBufferFree(buf); |
| 8141 | |
| 8142 | return(0); |
| 8143 | } |
| 8144 | |
| 8145 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8146 | * xmlParseStringPEReference: |
| 8147 | * @ctxt: an XML parser context |
| 8148 | * @str: a pointer to an index in the string |
| 8149 | * |
| 8150 | * parse PEReference declarations |
| 8151 | * |
| 8152 | * [69] PEReference ::= '%' Name ';' |
| 8153 | * |
| 8154 | * [ WFC: No Recursion ] |
| 8155 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8156 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8157 | * |
| 8158 | * [ WFC: Entity Declared ] |
| 8159 | * In a document without any DTD, a document with only an internal DTD |
| 8160 | * subset which contains no parameter entity references, or a document |
| 8161 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 8162 | * entity must precede any reference to it... |
| 8163 | * |
| 8164 | * [ VC: Entity Declared ] |
| 8165 | * In a document with an external subset or external parameter entities |
| 8166 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 8167 | * must precede any reference to it... |
| 8168 | * |
| 8169 | * [ WFC: In DTD ] |
| 8170 | * Parameter-entity references may only appear in the DTD. |
| 8171 | * NOTE: misleading but this is handled. |
| 8172 | * |
| 8173 | * Returns the string of the entity content. |
| 8174 | * str is updated to the current value of the index |
| 8175 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 8176 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8177 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 8178 | const xmlChar *ptr; |
| 8179 | xmlChar cur; |
| 8180 | xmlChar *name; |
| 8181 | xmlEntityPtr entity = NULL; |
| 8182 | |
| 8183 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 8184 | ptr = *str; |
| 8185 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8186 | if (cur != '%') |
| 8187 | return(NULL); |
| 8188 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8189 | name = xmlParseStringName(ctxt, &ptr); |
| 8190 | if (name == NULL) { |
| 8191 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8192 | "xmlParseStringPEReference: no name\n"); |
| 8193 | *str = ptr; |
| 8194 | return(NULL); |
| 8195 | } |
| 8196 | cur = *ptr; |
| 8197 | if (cur != ';') { |
| 8198 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 8199 | xmlFree(name); |
| 8200 | *str = ptr; |
| 8201 | return(NULL); |
| 8202 | } |
| 8203 | ptr++; |
| 8204 | |
| 8205 | /* |
| 8206 | * Increate the number of entity references parsed |
| 8207 | */ |
| 8208 | ctxt->nbentities++; |
| 8209 | |
| 8210 | /* |
| 8211 | * Request the entity from SAX |
| 8212 | */ |
| 8213 | if ((ctxt->sax != NULL) && |
| 8214 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8215 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 8216 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8217 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 8218 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8219 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8220 | if (entity == NULL) { |
| 8221 | /* |
| 8222 | * [ WFC: Entity Declared ] |
| 8223 | * In a document without any DTD, a document with only an |
| 8224 | * internal DTD subset which contains no parameter entity |
| 8225 | * references, or a document with "standalone='yes'", ... |
| 8226 | * ... The declaration of a parameter entity must precede |
| 8227 | * any reference to it... |
| 8228 | */ |
| 8229 | if ((ctxt->standalone == 1) || |
| 8230 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 8231 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 8232 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8233 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8234 | /* |
| 8235 | * [ VC: Entity Declared ] |
| 8236 | * In a document with an external subset or external |
| 8237 | * parameter entities with "standalone='no'", ... |
| 8238 | * ... The declaration of a parameter entity must |
| 8239 | * precede any reference to it... |
| 8240 | */ |
| 8241 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8242 | "PEReference: %%%s; not found\n", |
| 8243 | name, NULL); |
| 8244 | ctxt->valid = 0; |
| 8245 | } |
| 8246 | } else { |
| 8247 | /* |
| 8248 | * Internal checking in case the entity quest barfed |
| 8249 | */ |
| 8250 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8251 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8252 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8253 | "%%%s; is not a parameter entity\n", |
| 8254 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8255 | } |
| 8256 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8257 | ctxt->hasPErefs = 1; |
| 8258 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8259 | *str = ptr; |
| 8260 | return(entity); |
| 8261 | } |
| 8262 | |
| 8263 | /** |
| 8264 | * xmlParseDocTypeDecl: |
| 8265 | * @ctxt: an XML parser context |
| 8266 | * |
| 8267 | * parse a DOCTYPE declaration |
| 8268 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8269 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8270 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8271 | * |
| 8272 | * [ VC: Root Element Type ] |
| 8273 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8274 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8275 | */ |
| 8276 | |
| 8277 | void |
| 8278 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8279 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8280 | xmlChar *ExternalID = NULL; |
| 8281 | xmlChar *URI = NULL; |
| 8282 | |
| 8283 | /* |
| 8284 | * We know that '<!DOCTYPE' has been detected. |
| 8285 | */ |
| 8286 | SKIP(9); |
| 8287 | |
| 8288 | SKIP_BLANKS; |
| 8289 | |
| 8290 | /* |
| 8291 | * Parse the DOCTYPE name. |
| 8292 | */ |
| 8293 | name = xmlParseName(ctxt); |
| 8294 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8295 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8296 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8297 | } |
| 8298 | ctxt->intSubName = name; |
| 8299 | |
| 8300 | SKIP_BLANKS; |
| 8301 | |
| 8302 | /* |
| 8303 | * Check for SystemID and ExternalID |
| 8304 | */ |
| 8305 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 8306 | |
| 8307 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 8308 | ctxt->hasExternalSubset = 1; |
| 8309 | } |
| 8310 | ctxt->extSubURI = URI; |
| 8311 | ctxt->extSubSystem = ExternalID; |
| 8312 | |
| 8313 | SKIP_BLANKS; |
| 8314 | |
| 8315 | /* |
| 8316 | * Create and update the internal subset. |
| 8317 | */ |
| 8318 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 8319 | (!ctxt->disableSAX)) |
| 8320 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8321 | if (ctxt->instate == XML_PARSER_EOF) |
| 8322 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8323 | |
| 8324 | /* |
| 8325 | * Is there any internal subset declarations ? |
| 8326 | * they are handled separately in xmlParseInternalSubset() |
| 8327 | */ |
| 8328 | if (RAW == '[') |
| 8329 | return; |
| 8330 | |
| 8331 | /* |
| 8332 | * We should be at the end of the DOCTYPE declaration. |
| 8333 | */ |
| 8334 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8335 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8336 | } |
| 8337 | NEXT; |
| 8338 | } |
| 8339 | |
| 8340 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8341 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8342 | * @ctxt: an XML parser context |
| 8343 | * |
| 8344 | * parse the internal subset declaration |
| 8345 | * |
| 8346 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8347 | */ |
| 8348 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8349 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8350 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 8351 | /* |
| 8352 | * Is there any DTD definition ? |
| 8353 | */ |
| 8354 | if (RAW == '[') { |
| 8355 | ctxt->instate = XML_PARSER_DTD; |
| 8356 | NEXT; |
| 8357 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8358 | * Parse the succession of Markup declarations and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | * PEReferences. |
| 8360 | * Subsequence (markupdecl | PEReference | S)* |
| 8361 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8362 | while ((RAW != ']') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8363 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8364 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8365 | |
| 8366 | SKIP_BLANKS; |
| 8367 | xmlParseMarkupDecl(ctxt); |
| 8368 | xmlParsePEReference(ctxt); |
| 8369 | |
| 8370 | /* |
| 8371 | * Pop-up of finished entities. |
| 8372 | */ |
| 8373 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8374 | xmlPopInput(ctxt); |
| 8375 | |
| 8376 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8377 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8378 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8379 | break; |
| 8380 | } |
| 8381 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8382 | if (RAW == ']') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8383 | NEXT; |
| 8384 | SKIP_BLANKS; |
| 8385 | } |
| 8386 | } |
| 8387 | |
| 8388 | /* |
| 8389 | * We should be at the end of the DOCTYPE declaration. |
| 8390 | */ |
| 8391 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8392 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8393 | } |
| 8394 | NEXT; |
| 8395 | } |
| 8396 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8397 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8398 | /** |
| 8399 | * xmlParseAttribute: |
| 8400 | * @ctxt: an XML parser context |
| 8401 | * @value: a xmlChar ** used to store the value of the attribute |
| 8402 | * |
| 8403 | * parse an attribute |
| 8404 | * |
| 8405 | * [41] Attribute ::= Name Eq AttValue |
| 8406 | * |
| 8407 | * [ WFC: No External Entity References ] |
| 8408 | * Attribute values cannot contain direct or indirect entity references |
| 8409 | * to external entities. |
| 8410 | * |
| 8411 | * [ WFC: No < in Attribute Values ] |
| 8412 | * The replacement text of any entity referred to directly or indirectly in |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8413 | * an attribute value (other than "<") must not contain a <. |
| 8414 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8415 | * [ VC: Attribute Value Type ] |
| 8416 | * The attribute must have been declared; the value must be of the type |
| 8417 | * declared for it. |
| 8418 | * |
| 8419 | * [25] Eq ::= S? '=' S? |
| 8420 | * |
| 8421 | * With namespace: |
| 8422 | * |
| 8423 | * [NS 11] Attribute ::= QName Eq AttValue |
| 8424 | * |
| 8425 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 8426 | * definition. |
| 8427 | * |
| 8428 | * Returns the attribute name, and the value in *value. |
| 8429 | */ |
| 8430 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8431 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8432 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8433 | const xmlChar *name; |
| 8434 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8435 | |
| 8436 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 8437 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8438 | name = xmlParseName(ctxt); |
| 8439 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8440 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8441 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8442 | return(NULL); |
| 8443 | } |
| 8444 | |
| 8445 | /* |
| 8446 | * read the value |
| 8447 | */ |
| 8448 | SKIP_BLANKS; |
| 8449 | if (RAW == '=') { |
| 8450 | NEXT; |
| 8451 | SKIP_BLANKS; |
| 8452 | val = xmlParseAttValue(ctxt); |
| 8453 | ctxt->instate = XML_PARSER_CONTENT; |
| 8454 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8455 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8456 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8457 | return(NULL); |
| 8458 | } |
| 8459 | |
| 8460 | /* |
| 8461 | * Check that xml:lang conforms to the specification |
| 8462 | * No more registered as an error, just generate a warning now |
| 8463 | * since this was deprecated in XML second edition |
| 8464 | */ |
| 8465 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 8466 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8467 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8468 | "Malformed value for xml:lang : %s\n", |
| 8469 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8470 | } |
| 8471 | } |
| 8472 | |
| 8473 | /* |
| 8474 | * Check that xml:space conforms to the specification |
| 8475 | */ |
| 8476 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 8477 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 8478 | *(ctxt->space) = 0; |
| 8479 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 8480 | *(ctxt->space) = 1; |
| 8481 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8482 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 8483 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8484 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8485 | } |
| 8486 | } |
| 8487 | |
| 8488 | *value = val; |
| 8489 | return(name); |
| 8490 | } |
| 8491 | |
| 8492 | /** |
| 8493 | * xmlParseStartTag: |
| 8494 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8495 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8496 | * parse a start of tag either for rule element or |
| 8497 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8498 | * |
| 8499 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8500 | * |
| 8501 | * [ WFC: Unique Att Spec ] |
| 8502 | * 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] | 8503 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8504 | * |
| 8505 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8506 | * |
| 8507 | * [ WFC: Unique Att Spec ] |
| 8508 | * 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] | 8509 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8510 | * |
| 8511 | * With namespace: |
| 8512 | * |
| 8513 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8514 | * |
| 8515 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8516 | * |
| 8517 | * Returns the element name parsed |
| 8518 | */ |
| 8519 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8520 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8521 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8522 | const xmlChar *name; |
| 8523 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8524 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8525 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8526 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8527 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8528 | int i; |
| 8529 | |
| 8530 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8531 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8532 | |
| 8533 | name = xmlParseName(ctxt); |
| 8534 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8535 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8536 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8537 | return(NULL); |
| 8538 | } |
| 8539 | |
| 8540 | /* |
| 8541 | * Now parse the attributes, it ends up with the ending |
| 8542 | * |
| 8543 | * (S Attribute)* S? |
| 8544 | */ |
| 8545 | SKIP_BLANKS; |
| 8546 | GROW; |
| 8547 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8548 | while (((RAW != '>') && |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8549 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8550 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8551 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8552 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8553 | |
| 8554 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 8555 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8556 | /* |
| 8557 | * [ WFC: Unique Att Spec ] |
| 8558 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8559 | * start-tag or empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8560 | */ |
| 8561 | for (i = 0; i < nbatts;i += 2) { |
| 8562 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8563 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8564 | xmlFree(attvalue); |
| 8565 | goto failed; |
| 8566 | } |
| 8567 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8568 | /* |
| 8569 | * Add the pair to atts |
| 8570 | */ |
| 8571 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8572 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8573 | atts = (const xmlChar **) |
| 8574 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8575 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8576 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8577 | if (attvalue != NULL) |
| 8578 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8579 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8581 | ctxt->atts = atts; |
| 8582 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8583 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8584 | const xmlChar **n; |
| 8585 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8586 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8587 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8588 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8589 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8590 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8591 | if (attvalue != NULL) |
| 8592 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8593 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8594 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8595 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8596 | ctxt->atts = atts; |
| 8597 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8598 | } |
| 8599 | atts[nbatts++] = attname; |
| 8600 | atts[nbatts++] = attvalue; |
| 8601 | atts[nbatts] = NULL; |
| 8602 | atts[nbatts + 1] = NULL; |
| 8603 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8604 | if (attvalue != NULL) |
| 8605 | xmlFree(attvalue); |
| 8606 | } |
| 8607 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 8608 | failed: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8609 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8610 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8611 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8612 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8613 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8614 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8615 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8616 | } |
| 8617 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8618 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8619 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8620 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8621 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8622 | break; |
| 8623 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8624 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8625 | GROW; |
| 8626 | } |
| 8627 | |
| 8628 | /* |
| 8629 | * SAX: Start of Element ! |
| 8630 | */ |
| 8631 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8632 | (!ctxt->disableSAX)) { |
| 8633 | if (nbatts > 0) |
| 8634 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8635 | else |
| 8636 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8637 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8638 | |
| 8639 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8640 | /* Free only the content strings */ |
| 8641 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8642 | if (atts[i] != NULL) |
| 8643 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8644 | } |
| 8645 | return(name); |
| 8646 | } |
| 8647 | |
| 8648 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8649 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8650 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8651 | * @line: line of the start tag |
| 8652 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8653 | * |
| 8654 | * parse an end of tag |
| 8655 | * |
| 8656 | * [42] ETag ::= '</' Name S? '>' |
| 8657 | * |
| 8658 | * With namespace |
| 8659 | * |
| 8660 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8661 | */ |
| 8662 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8663 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8664 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8665 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8666 | |
| 8667 | GROW; |
| 8668 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8669 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8670 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8671 | return; |
| 8672 | } |
| 8673 | SKIP(2); |
| 8674 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8675 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8676 | |
| 8677 | /* |
| 8678 | * We should definitely be at the ending "S? '>'" part |
| 8679 | */ |
| 8680 | GROW; |
| 8681 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8682 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8683 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8684 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8685 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8686 | |
| 8687 | /* |
| 8688 | * [ WFC: Element Type Match ] |
| 8689 | * 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] | 8690 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8691 | * |
| 8692 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8693 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8694 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8695 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8696 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8697 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8698 | } |
| 8699 | |
| 8700 | /* |
| 8701 | * SAX: End of Tag |
| 8702 | */ |
| 8703 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8704 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8705 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8706 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8707 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8708 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8709 | return; |
| 8710 | } |
| 8711 | |
| 8712 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8713 | * xmlParseEndTag: |
| 8714 | * @ctxt: an XML parser context |
| 8715 | * |
| 8716 | * parse an end of tag |
| 8717 | * |
| 8718 | * [42] ETag ::= '</' Name S? '>' |
| 8719 | * |
| 8720 | * With namespace |
| 8721 | * |
| 8722 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8723 | */ |
| 8724 | |
| 8725 | void |
| 8726 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8727 | xmlParseEndTag1(ctxt, 0); |
| 8728 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8729 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8730 | |
| 8731 | /************************************************************************ |
| 8732 | * * |
| 8733 | * SAX 2 specific operations * |
| 8734 | * * |
| 8735 | ************************************************************************/ |
| 8736 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8737 | /* |
| 8738 | * xmlGetNamespace: |
| 8739 | * @ctxt: an XML parser context |
| 8740 | * @prefix: the prefix to lookup |
| 8741 | * |
| 8742 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8743 | * The prefix must come from the @ctxt->dict dictionnary |
| 8744 | * |
| 8745 | * Returns the namespace name or NULL if not bound |
| 8746 | */ |
| 8747 | static const xmlChar * |
| 8748 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8749 | int i; |
| 8750 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8751 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8752 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8753 | if (ctxt->nsTab[i] == prefix) { |
| 8754 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8755 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8756 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8757 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8758 | return(NULL); |
| 8759 | } |
| 8760 | |
| 8761 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8762 | * xmlParseQName: |
| 8763 | * @ctxt: an XML parser context |
| 8764 | * @prefix: pointer to store the prefix part |
| 8765 | * |
| 8766 | * parse an XML Namespace QName |
| 8767 | * |
| 8768 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8769 | * [7] Prefix ::= NCName |
| 8770 | * [8] LocalPart ::= NCName |
| 8771 | * |
| 8772 | * Returns the Name parsed or NULL |
| 8773 | */ |
| 8774 | |
| 8775 | static const xmlChar * |
| 8776 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8777 | const xmlChar *l, *p; |
| 8778 | |
| 8779 | GROW; |
| 8780 | |
| 8781 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8782 | if (l == NULL) { |
| 8783 | if (CUR == ':') { |
| 8784 | l = xmlParseName(ctxt); |
| 8785 | if (l != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8786 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8787 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8788 | *prefix = NULL; |
| 8789 | return(l); |
| 8790 | } |
| 8791 | } |
| 8792 | return(NULL); |
| 8793 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8794 | if (CUR == ':') { |
| 8795 | NEXT; |
| 8796 | p = l; |
| 8797 | l = xmlParseNCName(ctxt); |
| 8798 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8799 | xmlChar *tmp; |
| 8800 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8801 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8802 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8803 | l = xmlParseNmtoken(ctxt); |
| 8804 | if (l == NULL) |
| 8805 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8806 | else { |
| 8807 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8808 | xmlFree((char *)l); |
| 8809 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8810 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8811 | if (tmp != NULL) xmlFree(tmp); |
| 8812 | *prefix = NULL; |
| 8813 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8814 | } |
| 8815 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8816 | xmlChar *tmp; |
| 8817 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8818 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8819 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8820 | NEXT; |
| 8821 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8822 | if (tmp != NULL) { |
| 8823 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8824 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8825 | if (tmp != NULL) xmlFree(tmp); |
| 8826 | *prefix = p; |
| 8827 | return(l); |
| 8828 | } |
| 8829 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8830 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8831 | if (tmp != NULL) xmlFree(tmp); |
| 8832 | *prefix = p; |
| 8833 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8834 | } |
| 8835 | *prefix = p; |
| 8836 | } else |
| 8837 | *prefix = NULL; |
| 8838 | return(l); |
| 8839 | } |
| 8840 | |
| 8841 | /** |
| 8842 | * xmlParseQNameAndCompare: |
| 8843 | * @ctxt: an XML parser context |
| 8844 | * @name: the localname |
| 8845 | * @prefix: the prefix, if any. |
| 8846 | * |
| 8847 | * parse an XML name and compares for match |
| 8848 | * (specialized for endtag parsing) |
| 8849 | * |
| 8850 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8851 | * and the name for mismatch |
| 8852 | */ |
| 8853 | |
| 8854 | static const xmlChar * |
| 8855 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8856 | xmlChar const *prefix) { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8857 | const xmlChar *cmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8858 | const xmlChar *in; |
| 8859 | const xmlChar *ret; |
| 8860 | const xmlChar *prefix2; |
| 8861 | |
| 8862 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8863 | |
| 8864 | GROW; |
| 8865 | in = ctxt->input->cur; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8866 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8867 | cmp = prefix; |
| 8868 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8869 | ++in; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8870 | ++cmp; |
| 8871 | } |
| 8872 | if ((*cmp == 0) && (*in == ':')) { |
| 8873 | in++; |
| 8874 | cmp = name; |
| 8875 | while (*in != 0 && *in == *cmp) { |
| 8876 | ++in; |
| 8877 | ++cmp; |
| 8878 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8879 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8880 | /* success */ |
| 8881 | ctxt->input->cur = in; |
| 8882 | return((const xmlChar*) 1); |
| 8883 | } |
| 8884 | } |
| 8885 | /* |
| 8886 | * all strings coms from the dictionary, equality can be done directly |
| 8887 | */ |
| 8888 | ret = xmlParseQName (ctxt, &prefix2); |
| 8889 | if ((ret == name) && (prefix == prefix2)) |
| 8890 | return((const xmlChar*) 1); |
| 8891 | return ret; |
| 8892 | } |
| 8893 | |
| 8894 | /** |
| 8895 | * xmlParseAttValueInternal: |
| 8896 | * @ctxt: an XML parser context |
| 8897 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8898 | * @alloc: whether the attribute was reallocated as a new string |
| 8899 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8900 | * |
| 8901 | * parse a value for an attribute. |
| 8902 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8903 | * directly from the data buffer. |
| 8904 | * |
| 8905 | * 3.3.3 Attribute-Value Normalization: |
| 8906 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8907 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8908 | * - a character reference is processed by appending the referenced |
| 8909 | * character to the attribute value |
| 8910 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8911 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8912 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8913 | * appending #x20 to the normalized value, except that only a single |
| 8914 | * #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] | 8915 | * parsed entity or the literal entity value of an internal parsed entity |
| 8916 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8917 | * If the declared value is not CDATA, then the XML processor must further |
| 8918 | * process the normalized attribute value by discarding any leading and |
| 8919 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8920 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8921 | * All attributes for which no declaration has been read should be treated |
| 8922 | * by a non-validating parser as if declared CDATA. |
| 8923 | * |
| 8924 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8925 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8926 | */ |
| 8927 | |
| 8928 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8929 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8930 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8931 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8932 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8933 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8934 | xmlChar *ret = NULL; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 8935 | int line, col; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8936 | |
| 8937 | GROW; |
| 8938 | in = (xmlChar *) CUR_PTR; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 8939 | line = ctxt->input->line; |
| 8940 | col = ctxt->input->col; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8941 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8942 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8943 | return (NULL); |
| 8944 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8945 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8946 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8947 | /* |
| 8948 | * try to handle in this routine the most common case where no |
| 8949 | * allocation of a new string is required and where content is |
| 8950 | * pure ASCII. |
| 8951 | */ |
| 8952 | limit = *in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 8953 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8954 | end = ctxt->input->end; |
| 8955 | start = in; |
| 8956 | if (in >= end) { |
| 8957 | const xmlChar *oldbase = ctxt->input->base; |
| 8958 | GROW; |
| 8959 | if (oldbase != ctxt->input->base) { |
| 8960 | long delta = ctxt->input->base - oldbase; |
| 8961 | start = start + delta; |
| 8962 | in = in + delta; |
| 8963 | } |
| 8964 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8965 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8966 | if (normalize) { |
| 8967 | /* |
| 8968 | * Skip any leading spaces |
| 8969 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8970 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8971 | ((*in == 0x20) || (*in == 0x9) || |
| 8972 | (*in == 0xA) || (*in == 0xD))) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 8973 | if (*in == 0xA) { |
| 8974 | line++; col = 1; |
| 8975 | } else { |
| 8976 | col++; |
| 8977 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8978 | in++; |
| 8979 | start = in; |
| 8980 | if (in >= end) { |
| 8981 | const xmlChar *oldbase = ctxt->input->base; |
| 8982 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8983 | if (ctxt->instate == XML_PARSER_EOF) |
| 8984 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8985 | if (oldbase != ctxt->input->base) { |
| 8986 | long delta = ctxt->input->base - oldbase; |
| 8987 | start = start + delta; |
| 8988 | in = in + delta; |
| 8989 | } |
| 8990 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8991 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8992 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8993 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8994 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8995 | return(NULL); |
| 8996 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8997 | } |
| 8998 | } |
| 8999 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 9000 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9001 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9002 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 9003 | if (in >= end) { |
| 9004 | const xmlChar *oldbase = ctxt->input->base; |
| 9005 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9006 | if (ctxt->instate == XML_PARSER_EOF) |
| 9007 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9008 | if (oldbase != ctxt->input->base) { |
| 9009 | long delta = ctxt->input->base - oldbase; |
| 9010 | start = start + delta; |
| 9011 | in = in + delta; |
| 9012 | } |
| 9013 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9014 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9015 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9016 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9017 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9018 | return(NULL); |
| 9019 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9020 | } |
| 9021 | } |
| 9022 | last = in; |
| 9023 | /* |
| 9024 | * skip the trailing blanks |
| 9025 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 9026 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9027 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9028 | ((*in == 0x20) || (*in == 0x9) || |
| 9029 | (*in == 0xA) || (*in == 0xD))) { |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9030 | if (*in == 0xA) { |
| 9031 | line++, col = 1; |
| 9032 | } else { |
| 9033 | col++; |
| 9034 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9035 | in++; |
| 9036 | if (in >= end) { |
| 9037 | const xmlChar *oldbase = ctxt->input->base; |
| 9038 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9039 | if (ctxt->instate == XML_PARSER_EOF) |
| 9040 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9041 | if (oldbase != ctxt->input->base) { |
| 9042 | long delta = ctxt->input->base - oldbase; |
| 9043 | start = start + delta; |
| 9044 | in = in + delta; |
| 9045 | last = last + delta; |
| 9046 | } |
| 9047 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9048 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9049 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9050 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9051 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9052 | return(NULL); |
| 9053 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9054 | } |
| 9055 | } |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9056 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9057 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9058 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9059 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9060 | return(NULL); |
| 9061 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9062 | if (*in != limit) goto need_complex; |
| 9063 | } else { |
| 9064 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 9065 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 9066 | in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9067 | col++; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9068 | if (in >= end) { |
| 9069 | const xmlChar *oldbase = ctxt->input->base; |
| 9070 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9071 | if (ctxt->instate == XML_PARSER_EOF) |
| 9072 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9073 | if (oldbase != ctxt->input->base) { |
| 9074 | long delta = ctxt->input->base - oldbase; |
| 9075 | start = start + delta; |
| 9076 | in = in + delta; |
| 9077 | } |
| 9078 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9079 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9080 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9081 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9082 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9083 | return(NULL); |
| 9084 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9085 | } |
| 9086 | } |
| 9087 | last = in; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9088 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9089 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9090 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9091 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9092 | return(NULL); |
| 9093 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9094 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9095 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9096 | in++; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9097 | col++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9098 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9099 | *len = last - start; |
| 9100 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9101 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9102 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9103 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9104 | } |
| 9105 | CUR_PTR = in; |
Juergen Keil | 33f658c | 2014-08-07 17:30:36 +0800 | [diff] [blame] | 9106 | ctxt->input->line = line; |
| 9107 | ctxt->input->col = col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9108 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9109 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9110 | need_complex: |
| 9111 | if (alloc) *alloc = 1; |
| 9112 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9113 | } |
| 9114 | |
| 9115 | /** |
| 9116 | * xmlParseAttribute2: |
| 9117 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9118 | * @pref: the element prefix |
| 9119 | * @elem: the element name |
| 9120 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9121 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9122 | * @len: an int * to save the length of the attribute |
| 9123 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9124 | * |
| 9125 | * parse an attribute in the new SAX2 framework. |
| 9126 | * |
| 9127 | * Returns the attribute name, and the value in *value, . |
| 9128 | */ |
| 9129 | |
| 9130 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9131 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9132 | const xmlChar * pref, const xmlChar * elem, |
| 9133 | const xmlChar ** prefix, xmlChar ** value, |
| 9134 | int *len, int *alloc) |
| 9135 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9136 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 9137 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9138 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9139 | |
| 9140 | *value = NULL; |
| 9141 | GROW; |
| 9142 | name = xmlParseQName(ctxt, prefix); |
| 9143 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9144 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9145 | "error parsing attribute name\n"); |
| 9146 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9147 | } |
| 9148 | |
| 9149 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9150 | * get the type if needed |
| 9151 | */ |
| 9152 | if (ctxt->attsSpecial != NULL) { |
| 9153 | int type; |
| 9154 | |
| 9155 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9156 | pref, elem, *prefix, name); |
| 9157 | if (type != 0) |
| 9158 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9159 | } |
| 9160 | |
| 9161 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9162 | * read the value |
| 9163 | */ |
| 9164 | SKIP_BLANKS; |
| 9165 | if (RAW == '=') { |
| 9166 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9167 | SKIP_BLANKS; |
| 9168 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 9169 | if (normalize) { |
| 9170 | /* |
| 9171 | * Sometimes a second normalisation pass for spaces is needed |
| 9172 | * but that only happens if charrefs or entities refernces |
| 9173 | * have been used in the attribute value, i.e. the attribute |
| 9174 | * value have been extracted in an allocated string already. |
| 9175 | */ |
| 9176 | if (*alloc) { |
| 9177 | const xmlChar *val2; |
| 9178 | |
| 9179 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9180 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9181 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 9182 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9183 | } |
| 9184 | } |
| 9185 | } |
| 9186 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9187 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9188 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 9189 | "Specification mandate value for attribute %s\n", |
| 9190 | name); |
| 9191 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9192 | } |
| 9193 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9194 | if (*prefix == ctxt->str_xml) { |
| 9195 | /* |
| 9196 | * Check that xml:lang conforms to the specification |
| 9197 | * No more registered as an error, just generate a warning now |
| 9198 | * since this was deprecated in XML second edition |
| 9199 | */ |
| 9200 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 9201 | internal_val = xmlStrndup(val, *len); |
| 9202 | if (!xmlCheckLanguageID(internal_val)) { |
| 9203 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 9204 | "Malformed value for xml:lang : %s\n", |
| 9205 | internal_val, NULL); |
| 9206 | } |
| 9207 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9208 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9209 | /* |
| 9210 | * Check that xml:space conforms to the specification |
| 9211 | */ |
| 9212 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 9213 | internal_val = xmlStrndup(val, *len); |
| 9214 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 9215 | *(ctxt->space) = 0; |
| 9216 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 9217 | *(ctxt->space) = 1; |
| 9218 | else { |
| 9219 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 9220 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 9221 | internal_val, NULL); |
| 9222 | } |
| 9223 | } |
| 9224 | if (internal_val) { |
| 9225 | xmlFree(internal_val); |
| 9226 | } |
| 9227 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9228 | |
| 9229 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9230 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9231 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9232 | /** |
| 9233 | * xmlParseStartTag2: |
| 9234 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9235 | * |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9236 | * parse a start of tag either for rule element or |
| 9237 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 9238 | * This routine is called when running SAX2 parsing |
| 9239 | * |
| 9240 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 9241 | * |
| 9242 | * [ WFC: Unique Att Spec ] |
| 9243 | * 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] | 9244 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9245 | * |
| 9246 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 9247 | * |
| 9248 | * [ WFC: Unique Att Spec ] |
| 9249 | * 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] | 9250 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9251 | * |
| 9252 | * With namespace: |
| 9253 | * |
| 9254 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 9255 | * |
| 9256 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 9257 | * |
| 9258 | * Returns the element name parsed |
| 9259 | */ |
| 9260 | |
| 9261 | static const xmlChar * |
| 9262 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9263 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9264 | const xmlChar *localname; |
| 9265 | const xmlChar *prefix; |
| 9266 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9267 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9268 | const xmlChar *nsname; |
| 9269 | xmlChar *attvalue; |
| 9270 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9271 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9272 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9273 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9274 | const xmlChar *base; |
| 9275 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9276 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9277 | |
| 9278 | if (RAW != '<') return(NULL); |
| 9279 | NEXT1; |
| 9280 | |
| 9281 | /* |
| 9282 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 9283 | * point since the attribute values may be stored as pointers to |
| 9284 | * the buffer and calling SHRINK would destroy them ! |
| 9285 | * The Shrinking is only possible once the full set of attribute |
| 9286 | * callbacks have been done. |
| 9287 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9288 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9289 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9290 | base = ctxt->input->base; |
| 9291 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9292 | oldline = ctxt->input->line; |
| 9293 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9294 | nbatts = 0; |
| 9295 | nratts = 0; |
| 9296 | nbdef = 0; |
| 9297 | nbNs = 0; |
| 9298 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9299 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 9300 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9301 | |
| 9302 | localname = xmlParseQName(ctxt, &prefix); |
| 9303 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9304 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9305 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9306 | return(NULL); |
| 9307 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9308 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9309 | |
| 9310 | /* |
| 9311 | * Now parse the attributes, it ends up with the ending |
| 9312 | * |
| 9313 | * (S Attribute)* S? |
| 9314 | */ |
| 9315 | SKIP_BLANKS; |
| 9316 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9317 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9318 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9319 | while (((RAW != '>') && |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9320 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9321 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9322 | const xmlChar *q = CUR_PTR; |
| 9323 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9324 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9325 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9326 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 9327 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9328 | if (ctxt->input->base != base) { |
| 9329 | if ((attvalue != NULL) && (alloc != 0)) |
| 9330 | xmlFree(attvalue); |
| 9331 | attvalue = NULL; |
| 9332 | goto base_changed; |
| 9333 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9334 | if ((attname != NULL) && (attvalue != NULL)) { |
| 9335 | if (len < 0) len = xmlStrlen(attvalue); |
| 9336 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9337 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9338 | xmlURIPtr uri; |
| 9339 | |
Daniel Veillard | c836ba6 | 2014-07-14 16:39:50 +0800 | [diff] [blame] | 9340 | if (URL == NULL) { |
| 9341 | xmlErrMemory(ctxt, "dictionary allocation failure"); |
| 9342 | if ((attvalue != NULL) && (alloc != 0)) |
| 9343 | xmlFree(attvalue); |
| 9344 | return(NULL); |
| 9345 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9346 | if (*URL != 0) { |
| 9347 | uri = xmlParseURI((const char *) URL); |
| 9348 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9349 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9350 | "xmlns: '%s' is not a valid URI\n", |
| 9351 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9352 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 9353 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9354 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9355 | "xmlns: URI %s is not absolute\n", |
| 9356 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9357 | } |
| 9358 | xmlFreeURI(uri); |
| 9359 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9360 | if (URL == ctxt->str_xml_ns) { |
| 9361 | if (attname != ctxt->str_xml) { |
| 9362 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9363 | "xml namespace URI cannot be the default namespace\n", |
| 9364 | NULL, NULL, NULL); |
| 9365 | } |
| 9366 | goto skip_default_ns; |
| 9367 | } |
| 9368 | if ((len == 29) && |
| 9369 | (xmlStrEqual(URL, |
| 9370 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9371 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9372 | "reuse of the xmlns namespace name is forbidden\n", |
| 9373 | NULL, NULL, NULL); |
| 9374 | goto skip_default_ns; |
| 9375 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9376 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9377 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9378 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9379 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9380 | for (j = 1;j <= nbNs;j++) |
| 9381 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9382 | break; |
| 9383 | if (j <= nbNs) |
| 9384 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 9385 | else |
| 9386 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9387 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9388 | if (alloc != 0) xmlFree(attvalue); |
Dennis Filder | 7e9bbdf | 2014-10-06 20:34:14 +0800 | [diff] [blame] | 9389 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9390 | break; |
| 9391 | if (!IS_BLANK_CH(RAW)) { |
| 9392 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9393 | "attributes construct error\n"); |
| 9394 | break; |
| 9395 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9396 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9397 | continue; |
| 9398 | } |
| 9399 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9400 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9401 | xmlURIPtr uri; |
| 9402 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9403 | if (attname == ctxt->str_xml) { |
| 9404 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9405 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9406 | "xml namespace prefix mapped to wrong URI\n", |
| 9407 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9408 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9409 | /* |
| 9410 | * Do not keep a namespace definition node |
| 9411 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9412 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9413 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9414 | if (URL == ctxt->str_xml_ns) { |
| 9415 | if (attname != ctxt->str_xml) { |
| 9416 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9417 | "xml namespace URI mapped to wrong prefix\n", |
| 9418 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9419 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9420 | goto skip_ns; |
| 9421 | } |
| 9422 | if (attname == ctxt->str_xmlns) { |
| 9423 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9424 | "redefinition of the xmlns prefix is forbidden\n", |
| 9425 | NULL, NULL, NULL); |
| 9426 | goto skip_ns; |
| 9427 | } |
| 9428 | if ((len == 29) && |
| 9429 | (xmlStrEqual(URL, |
| 9430 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9431 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9432 | "reuse of the xmlns namespace name is forbidden\n", |
| 9433 | NULL, NULL, NULL); |
| 9434 | goto skip_ns; |
| 9435 | } |
| 9436 | if ((URL == NULL) || (URL[0] == 0)) { |
| 9437 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9438 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 9439 | attname, NULL, NULL); |
| 9440 | goto skip_ns; |
| 9441 | } else { |
| 9442 | uri = xmlParseURI((const char *) URL); |
| 9443 | if (uri == NULL) { |
| 9444 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9445 | "xmlns:%s: '%s' is not a valid URI\n", |
| 9446 | attname, URL, NULL); |
| 9447 | } else { |
| 9448 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 9449 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9450 | "xmlns:%s: URI %s is not absolute\n", |
| 9451 | attname, URL, NULL); |
| 9452 | } |
| 9453 | xmlFreeURI(uri); |
| 9454 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9455 | } |
| 9456 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9457 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9458 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9459 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9460 | for (j = 1;j <= nbNs;j++) |
| 9461 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9462 | break; |
| 9463 | if (j <= nbNs) |
| 9464 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 9465 | else |
| 9466 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9467 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9468 | if (alloc != 0) xmlFree(attvalue); |
Dennis Filder | 7e9bbdf | 2014-10-06 20:34:14 +0800 | [diff] [blame] | 9469 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9470 | break; |
| 9471 | if (!IS_BLANK_CH(RAW)) { |
| 9472 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9473 | "attributes construct error\n"); |
| 9474 | break; |
| 9475 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9476 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 9477 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9478 | continue; |
| 9479 | } |
| 9480 | |
| 9481 | /* |
| 9482 | * Add the pair to atts |
| 9483 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9484 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9485 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9486 | if (attvalue[len] == 0) |
| 9487 | xmlFree(attvalue); |
| 9488 | goto failed; |
| 9489 | } |
| 9490 | maxatts = ctxt->maxatts; |
| 9491 | atts = ctxt->atts; |
| 9492 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9493 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9494 | atts[nbatts++] = attname; |
| 9495 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9496 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9497 | atts[nbatts++] = attvalue; |
| 9498 | attvalue += len; |
| 9499 | atts[nbatts++] = attvalue; |
| 9500 | /* |
| 9501 | * tag if some deallocation is needed |
| 9502 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9503 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9504 | } else { |
| 9505 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 9506 | xmlFree(attvalue); |
| 9507 | } |
| 9508 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9509 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9510 | |
| 9511 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9512 | if (ctxt->instate == XML_PARSER_EOF) |
| 9513 | break; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9514 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9515 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9516 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9517 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9518 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9519 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9520 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9521 | } |
| 9522 | SKIP_BLANKS; |
| 9523 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 9524 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9525 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9526 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9527 | break; |
| 9528 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9529 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9530 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9531 | } |
| 9532 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9533 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9534 | * The attributes defaulting |
| 9535 | */ |
| 9536 | if (ctxt->attsDefault != NULL) { |
| 9537 | xmlDefAttrsPtr defaults; |
| 9538 | |
| 9539 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 9540 | if (defaults != NULL) { |
| 9541 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9542 | attname = defaults->values[5 * i]; |
| 9543 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9544 | |
| 9545 | /* |
| 9546 | * special work for namespaces defaulted defs |
| 9547 | */ |
| 9548 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 9549 | /* |
| 9550 | * check that it's not a defined namespace |
| 9551 | */ |
| 9552 | for (j = 1;j <= nbNs;j++) |
| 9553 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9554 | break; |
| 9555 | if (j <= nbNs) continue; |
| 9556 | |
| 9557 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9558 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9559 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9560 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9561 | nbNs++; |
| 9562 | } |
| 9563 | } else if (aprefix == ctxt->str_xmlns) { |
| 9564 | /* |
| 9565 | * check that it's not a defined namespace |
| 9566 | */ |
| 9567 | for (j = 1;j <= nbNs;j++) |
| 9568 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9569 | break; |
| 9570 | if (j <= nbNs) continue; |
| 9571 | |
| 9572 | nsname = xmlGetNamespace(ctxt, attname); |
| 9573 | if (nsname != defaults->values[2]) { |
| 9574 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9575 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9576 | nbNs++; |
| 9577 | } |
| 9578 | } else { |
| 9579 | /* |
| 9580 | * check that it's not a defined attribute |
| 9581 | */ |
| 9582 | for (j = 0;j < nbatts;j+=5) { |
| 9583 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 9584 | break; |
| 9585 | } |
| 9586 | if (j < nbatts) continue; |
| 9587 | |
| 9588 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9589 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 9590 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9591 | } |
| 9592 | maxatts = ctxt->maxatts; |
| 9593 | atts = ctxt->atts; |
| 9594 | } |
| 9595 | atts[nbatts++] = attname; |
| 9596 | atts[nbatts++] = aprefix; |
| 9597 | if (aprefix == NULL) |
| 9598 | atts[nbatts++] = NULL; |
| 9599 | else |
| 9600 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9601 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 9602 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 9603 | if ((ctxt->standalone == 1) && |
| 9604 | (defaults->values[5 * i + 4] != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9605 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9606 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 9607 | attname, localname); |
| 9608 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9609 | nbdef++; |
| 9610 | } |
| 9611 | } |
| 9612 | } |
| 9613 | } |
| 9614 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9615 | /* |
| 9616 | * The attributes checkings |
| 9617 | */ |
| 9618 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 9619 | /* |
| 9620 | * The default namespace does not apply to attribute names. |
| 9621 | */ |
| 9622 | if (atts[i + 1] != NULL) { |
| 9623 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 9624 | if (nsname == NULL) { |
| 9625 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9626 | "Namespace prefix %s for %s on %s is not defined\n", |
| 9627 | atts[i + 1], atts[i], localname); |
| 9628 | } |
| 9629 | atts[i + 2] = nsname; |
| 9630 | } else |
| 9631 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9632 | /* |
| 9633 | * [ WFC: Unique Att Spec ] |
| 9634 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9635 | * start-tag or empty-element tag. |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9636 | * As extended by the Namespace in XML REC. |
| 9637 | */ |
| 9638 | for (j = 0; j < i;j += 5) { |
| 9639 | if (atts[i] == atts[j]) { |
| 9640 | if (atts[i+1] == atts[j+1]) { |
| 9641 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 9642 | break; |
| 9643 | } |
| 9644 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 9645 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 9646 | "Namespaced Attribute %s in '%s' redefined\n", |
| 9647 | atts[i], nsname, NULL); |
| 9648 | break; |
| 9649 | } |
| 9650 | } |
| 9651 | } |
| 9652 | } |
| 9653 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9654 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9655 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9656 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9657 | "Namespace prefix %s on %s is not defined\n", |
| 9658 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9659 | } |
| 9660 | *pref = prefix; |
| 9661 | *URI = nsname; |
| 9662 | |
| 9663 | /* |
| 9664 | * SAX: Start of Element ! |
| 9665 | */ |
| 9666 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9667 | (!ctxt->disableSAX)) { |
| 9668 | if (nbNs > 0) |
| 9669 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9670 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9671 | nbatts / 5, nbdef, atts); |
| 9672 | else |
| 9673 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9674 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9675 | } |
| 9676 | |
| 9677 | /* |
| 9678 | * Free up attribute allocated strings if needed |
| 9679 | */ |
| 9680 | if (attval != 0) { |
| 9681 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9682 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9683 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9684 | } |
| 9685 | |
| 9686 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9687 | |
| 9688 | base_changed: |
| 9689 | /* |
| 9690 | * the attribute strings are valid iif the base didn't changed |
| 9691 | */ |
| 9692 | if (attval != 0) { |
| 9693 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9694 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9695 | xmlFree((xmlChar *) atts[i]); |
| 9696 | } |
| 9697 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9698 | ctxt->input->line = oldline; |
| 9699 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9700 | if (ctxt->wellFormed == 1) { |
| 9701 | goto reparse; |
| 9702 | } |
| 9703 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9704 | } |
| 9705 | |
| 9706 | /** |
| 9707 | * xmlParseEndTag2: |
| 9708 | * @ctxt: an XML parser context |
| 9709 | * @line: line of the start tag |
| 9710 | * @nsNr: number of namespaces on the start tag |
| 9711 | * |
| 9712 | * parse an end of tag |
| 9713 | * |
| 9714 | * [42] ETag ::= '</' Name S? '>' |
| 9715 | * |
| 9716 | * With namespace |
| 9717 | * |
| 9718 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9719 | */ |
| 9720 | |
| 9721 | static void |
| 9722 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9723 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9724 | const xmlChar *name; |
| 9725 | |
| 9726 | GROW; |
| 9727 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9728 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9729 | return; |
| 9730 | } |
| 9731 | SKIP(2); |
| 9732 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9733 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9734 | if (ctxt->input->cur[tlen] == '>') { |
| 9735 | ctxt->input->cur += tlen + 1; |
Juergen Keil | 24fb4c3 | 2014-10-06 18:19:12 +0800 | [diff] [blame] | 9736 | ctxt->input->col += tlen + 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9737 | goto done; |
| 9738 | } |
| 9739 | ctxt->input->cur += tlen; |
Juergen Keil | 24fb4c3 | 2014-10-06 18:19:12 +0800 | [diff] [blame] | 9740 | ctxt->input->col += tlen; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9741 | name = (xmlChar*)1; |
| 9742 | } else { |
| 9743 | if (prefix == NULL) |
| 9744 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9745 | else |
| 9746 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9747 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9748 | |
| 9749 | /* |
| 9750 | * We should definitely be at the ending "S? '>'" part |
| 9751 | */ |
| 9752 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9753 | if (ctxt->instate == XML_PARSER_EOF) |
| 9754 | return; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9755 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9756 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9757 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9758 | } else |
| 9759 | NEXT1; |
| 9760 | |
| 9761 | /* |
| 9762 | * [ WFC: Element Type Match ] |
| 9763 | * 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] | 9764 | * start-tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9765 | * |
| 9766 | */ |
| 9767 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9768 | if (name == NULL) name = BAD_CAST "unparseable"; |
Daniel Veillard | 852505b | 2009-08-23 15:44:48 +0200 | [diff] [blame] | 9769 | if ((line == 0) && (ctxt->node != NULL)) |
| 9770 | line = ctxt->node->line; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9771 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9772 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9773 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9774 | } |
| 9775 | |
| 9776 | /* |
| 9777 | * SAX: End of Tag |
| 9778 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9779 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9780 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9781 | (!ctxt->disableSAX)) |
| 9782 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9783 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9784 | spacePop(ctxt); |
| 9785 | if (nsNr != 0) |
| 9786 | nsPop(ctxt, nsNr); |
| 9787 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9788 | } |
| 9789 | |
| 9790 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9791 | * xmlParseCDSect: |
| 9792 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9793 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9794 | * Parse escaped pure raw content. |
| 9795 | * |
| 9796 | * [18] CDSect ::= CDStart CData CDEnd |
| 9797 | * |
| 9798 | * [19] CDStart ::= '<![CDATA[' |
| 9799 | * |
| 9800 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9801 | * |
| 9802 | * [21] CDEnd ::= ']]>' |
| 9803 | */ |
| 9804 | void |
| 9805 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9806 | xmlChar *buf = NULL; |
| 9807 | int len = 0; |
| 9808 | int size = XML_PARSER_BUFFER_SIZE; |
| 9809 | int r, rl; |
| 9810 | int s, sl; |
| 9811 | int cur, l; |
| 9812 | int count = 0; |
| 9813 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9814 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9815 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9816 | SKIP(9); |
| 9817 | } else |
| 9818 | return; |
| 9819 | |
| 9820 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9821 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9822 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9823 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9824 | ctxt->instate = XML_PARSER_CONTENT; |
| 9825 | return; |
| 9826 | } |
| 9827 | NEXTL(rl); |
| 9828 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9829 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9830 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9831 | ctxt->instate = XML_PARSER_CONTENT; |
| 9832 | return; |
| 9833 | } |
| 9834 | NEXTL(sl); |
| 9835 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9836 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9837 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9838 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9839 | return; |
| 9840 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9841 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9842 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9843 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9844 | xmlChar *tmp; |
| 9845 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9846 | if ((size > XML_MAX_TEXT_LENGTH) && |
| 9847 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9848 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
| 9849 | "CData section too big found", NULL); |
| 9850 | xmlFree (buf); |
| 9851 | return; |
| 9852 | } |
| 9853 | tmp = (xmlChar *) xmlRealloc(buf, size * 2 * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9854 | if (tmp == NULL) { |
| 9855 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9856 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9857 | return; |
| 9858 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9859 | buf = tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9860 | size *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9861 | } |
| 9862 | COPY_BUF(rl,buf,len,r); |
| 9863 | r = s; |
| 9864 | rl = sl; |
| 9865 | s = cur; |
| 9866 | sl = l; |
| 9867 | count++; |
| 9868 | if (count > 50) { |
| 9869 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9870 | if (ctxt->instate == XML_PARSER_EOF) { |
| 9871 | xmlFree(buf); |
| 9872 | return; |
| 9873 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9874 | count = 0; |
| 9875 | } |
| 9876 | NEXTL(l); |
| 9877 | cur = CUR_CHAR(l); |
| 9878 | } |
| 9879 | buf[len] = 0; |
| 9880 | ctxt->instate = XML_PARSER_CONTENT; |
| 9881 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9882 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9883 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9884 | xmlFree(buf); |
| 9885 | return; |
| 9886 | } |
| 9887 | NEXTL(l); |
| 9888 | |
| 9889 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9890 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9891 | */ |
| 9892 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9893 | if (ctxt->sax->cdataBlock != NULL) |
| 9894 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9895 | else if (ctxt->sax->characters != NULL) |
| 9896 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9897 | } |
| 9898 | xmlFree(buf); |
| 9899 | } |
| 9900 | |
| 9901 | /** |
| 9902 | * xmlParseContent: |
| 9903 | * @ctxt: an XML parser context |
| 9904 | * |
| 9905 | * Parse a content: |
| 9906 | * |
| 9907 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9908 | */ |
| 9909 | |
| 9910 | void |
| 9911 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9912 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9913 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9914 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9915 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9916 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9917 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9918 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9919 | |
| 9920 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9921 | * First case : a Processing Instruction. |
| 9922 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9923 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9924 | xmlParsePI(ctxt); |
| 9925 | } |
| 9926 | |
| 9927 | /* |
| 9928 | * Second case : a CDSection |
| 9929 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9930 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9931 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9932 | xmlParseCDSect(ctxt); |
| 9933 | } |
| 9934 | |
| 9935 | /* |
| 9936 | * Third case : a comment |
| 9937 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9938 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9939 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9940 | xmlParseComment(ctxt); |
| 9941 | ctxt->instate = XML_PARSER_CONTENT; |
| 9942 | } |
| 9943 | |
| 9944 | /* |
| 9945 | * Fourth case : a sub-element. |
| 9946 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9947 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9948 | xmlParseElement(ctxt); |
| 9949 | } |
| 9950 | |
| 9951 | /* |
| 9952 | * Fifth case : a reference. If if has not been resolved, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9953 | * parsing returns it's Name, create the node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9954 | */ |
| 9955 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9956 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9957 | xmlParseReference(ctxt); |
| 9958 | } |
| 9959 | |
| 9960 | /* |
| 9961 | * Last case, text. Note that References are handled directly. |
| 9962 | */ |
| 9963 | else { |
| 9964 | xmlParseCharData(ctxt, 0); |
| 9965 | } |
| 9966 | |
| 9967 | GROW; |
| 9968 | /* |
| 9969 | * Pop-up of finished entities. |
| 9970 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9971 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9972 | xmlPopInput(ctxt); |
| 9973 | SHRINK; |
| 9974 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9975 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9976 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9977 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9978 | ctxt->instate = XML_PARSER_EOF; |
| 9979 | break; |
| 9980 | } |
| 9981 | } |
| 9982 | } |
| 9983 | |
| 9984 | /** |
| 9985 | * xmlParseElement: |
| 9986 | * @ctxt: an XML parser context |
| 9987 | * |
| 9988 | * parse an XML element, this is highly recursive |
| 9989 | * |
| 9990 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9991 | * |
| 9992 | * [ WFC: Element Type Match ] |
| 9993 | * 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] | 9994 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9995 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9996 | */ |
| 9997 | |
| 9998 | void |
| 9999 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 10000 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 10001 | const xmlChar *prefix = NULL; |
| 10002 | const xmlChar *URI = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10003 | xmlParserNodeInfo node_info; |
Daniel Veillard | ed35d3d | 2012-05-11 10:52:27 +0800 | [diff] [blame] | 10004 | int line, tlen = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10005 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10006 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10007 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 10008 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 10009 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 10010 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 10011 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 10012 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 10013 | ctxt->instate = XML_PARSER_EOF; |
| 10014 | return; |
| 10015 | } |
| 10016 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10017 | /* Capture start position */ |
| 10018 | if (ctxt->record_info) { |
| 10019 | node_info.begin_pos = ctxt->input->consumed + |
| 10020 | (CUR_PTR - ctxt->input->base); |
| 10021 | node_info.begin_line = ctxt->input->line; |
| 10022 | } |
| 10023 | |
| 10024 | if (ctxt->spaceNr == 0) |
| 10025 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 10026 | else if (*ctxt->space == -2) |
| 10027 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10028 | else |
| 10029 | spacePush(ctxt, *ctxt->space); |
| 10030 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 10031 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10032 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10033 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10034 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10035 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10036 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10037 | else |
| 10038 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10039 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 10040 | if (ctxt->instate == XML_PARSER_EOF) |
| 10041 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10042 | if (name == NULL) { |
| 10043 | spacePop(ctxt); |
| 10044 | return; |
| 10045 | } |
| 10046 | namePush(ctxt, name); |
| 10047 | ret = ctxt->node; |
| 10048 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10049 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10050 | /* |
| 10051 | * [ VC: Root Element Type ] |
| 10052 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10053 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10054 | */ |
| 10055 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 10056 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 10057 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10058 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10059 | |
| 10060 | /* |
| 10061 | * Check for an Empty Element. |
| 10062 | */ |
| 10063 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 10064 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10065 | if (ctxt->sax2) { |
| 10066 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 10067 | (!ctxt->disableSAX)) |
| 10068 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10069 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10070 | } else { |
| 10071 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 10072 | (!ctxt->disableSAX)) |
| 10073 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10074 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10075 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10076 | namePop(ctxt); |
| 10077 | spacePop(ctxt); |
| 10078 | if (nsNr != ctxt->nsNr) |
| 10079 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10080 | if ( ret != NULL && ctxt->record_info ) { |
| 10081 | node_info.end_pos = ctxt->input->consumed + |
| 10082 | (CUR_PTR - ctxt->input->base); |
| 10083 | node_info.end_line = ctxt->input->line; |
| 10084 | node_info.node = ret; |
| 10085 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10086 | } |
| 10087 | return; |
| 10088 | } |
| 10089 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10090 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10091 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10092 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 10093 | "Couldn't find end of Start Tag %s line %d\n", |
| 10094 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10095 | |
| 10096 | /* |
| 10097 | * end of parsing of this node. |
| 10098 | */ |
| 10099 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10100 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10101 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10102 | if (nsNr != ctxt->nsNr) |
| 10103 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10104 | |
| 10105 | /* |
| 10106 | * Capture end position and add node |
| 10107 | */ |
| 10108 | if ( ret != NULL && ctxt->record_info ) { |
| 10109 | node_info.end_pos = ctxt->input->consumed + |
| 10110 | (CUR_PTR - ctxt->input->base); |
| 10111 | node_info.end_line = ctxt->input->line; |
| 10112 | node_info.node = ret; |
| 10113 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10114 | } |
| 10115 | return; |
| 10116 | } |
| 10117 | |
| 10118 | /* |
| 10119 | * Parse the content of the element: |
| 10120 | */ |
| 10121 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10122 | if (ctxt->instate == XML_PARSER_EOF) |
| 10123 | return; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10124 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10125 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 10126 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10127 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10128 | |
| 10129 | /* |
| 10130 | * end of parsing of this node. |
| 10131 | */ |
| 10132 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10133 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10134 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10135 | if (nsNr != ctxt->nsNr) |
| 10136 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10137 | return; |
| 10138 | } |
| 10139 | |
| 10140 | /* |
| 10141 | * parse the end of tag: '</' should be here. |
| 10142 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10143 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10144 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10145 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10146 | } |
| 10147 | #ifdef LIBXML_SAX1_ENABLED |
| 10148 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10149 | xmlParseEndTag1(ctxt, line); |
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 | |
| 10152 | /* |
| 10153 | * Capture end position and add node |
| 10154 | */ |
| 10155 | if ( ret != NULL && ctxt->record_info ) { |
| 10156 | node_info.end_pos = ctxt->input->consumed + |
| 10157 | (CUR_PTR - ctxt->input->base); |
| 10158 | node_info.end_line = ctxt->input->line; |
| 10159 | node_info.node = ret; |
| 10160 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10161 | } |
| 10162 | } |
| 10163 | |
| 10164 | /** |
| 10165 | * xmlParseVersionNum: |
| 10166 | * @ctxt: an XML parser context |
| 10167 | * |
| 10168 | * parse the XML version value. |
| 10169 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10170 | * [26] VersionNum ::= '1.' [0-9]+ |
| 10171 | * |
| 10172 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10173 | * |
| 10174 | * Returns the string giving the XML version number, or NULL |
| 10175 | */ |
| 10176 | xmlChar * |
| 10177 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 10178 | xmlChar *buf = NULL; |
| 10179 | int len = 0; |
| 10180 | int size = 10; |
| 10181 | xmlChar cur; |
| 10182 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10183 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10184 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10185 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10186 | return(NULL); |
| 10187 | } |
| 10188 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10189 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10190 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10191 | return(NULL); |
| 10192 | } |
| 10193 | buf[len++] = cur; |
| 10194 | NEXT; |
| 10195 | cur=CUR; |
| 10196 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10197 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10198 | return(NULL); |
| 10199 | } |
| 10200 | buf[len++] = cur; |
| 10201 | NEXT; |
| 10202 | cur=CUR; |
| 10203 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10204 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10205 | xmlChar *tmp; |
| 10206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10207 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10208 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10209 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10210 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10211 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10212 | return(NULL); |
| 10213 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10214 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10215 | } |
| 10216 | buf[len++] = cur; |
| 10217 | NEXT; |
| 10218 | cur=CUR; |
| 10219 | } |
| 10220 | buf[len] = 0; |
| 10221 | return(buf); |
| 10222 | } |
| 10223 | |
| 10224 | /** |
| 10225 | * xmlParseVersionInfo: |
| 10226 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10227 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10228 | * parse the XML version. |
| 10229 | * |
| 10230 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10231 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10232 | * [25] Eq ::= S? '=' S? |
| 10233 | * |
| 10234 | * Returns the version string, e.g. "1.0" |
| 10235 | */ |
| 10236 | |
| 10237 | xmlChar * |
| 10238 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 10239 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10240 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10241 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10242 | SKIP(7); |
| 10243 | SKIP_BLANKS; |
| 10244 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10245 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10246 | return(NULL); |
| 10247 | } |
| 10248 | NEXT; |
| 10249 | SKIP_BLANKS; |
| 10250 | if (RAW == '"') { |
| 10251 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10252 | version = xmlParseVersionNum(ctxt); |
| 10253 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10254 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10255 | } else |
| 10256 | NEXT; |
| 10257 | } else if (RAW == '\''){ |
| 10258 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10259 | version = xmlParseVersionNum(ctxt); |
| 10260 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10261 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10262 | } else |
| 10263 | NEXT; |
| 10264 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10265 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10266 | } |
| 10267 | } |
| 10268 | return(version); |
| 10269 | } |
| 10270 | |
| 10271 | /** |
| 10272 | * xmlParseEncName: |
| 10273 | * @ctxt: an XML parser context |
| 10274 | * |
| 10275 | * parse the XML encoding name |
| 10276 | * |
| 10277 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 10278 | * |
| 10279 | * Returns the encoding name value or NULL |
| 10280 | */ |
| 10281 | xmlChar * |
| 10282 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 10283 | xmlChar *buf = NULL; |
| 10284 | int len = 0; |
| 10285 | int size = 10; |
| 10286 | xmlChar cur; |
| 10287 | |
| 10288 | cur = CUR; |
| 10289 | if (((cur >= 'a') && (cur <= 'z')) || |
| 10290 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10291 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10292 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10293 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10294 | return(NULL); |
| 10295 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10296 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10297 | buf[len++] = cur; |
| 10298 | NEXT; |
| 10299 | cur = CUR; |
| 10300 | while (((cur >= 'a') && (cur <= 'z')) || |
| 10301 | ((cur >= 'A') && (cur <= 'Z')) || |
| 10302 | ((cur >= '0') && (cur <= '9')) || |
| 10303 | (cur == '.') || (cur == '_') || |
| 10304 | (cur == '-')) { |
| 10305 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10306 | xmlChar *tmp; |
| 10307 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10308 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10309 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10310 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10311 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10312 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10313 | return(NULL); |
| 10314 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10315 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10316 | } |
| 10317 | buf[len++] = cur; |
| 10318 | NEXT; |
| 10319 | cur = CUR; |
| 10320 | if (cur == 0) { |
| 10321 | SHRINK; |
| 10322 | GROW; |
| 10323 | cur = CUR; |
| 10324 | } |
| 10325 | } |
| 10326 | buf[len] = 0; |
| 10327 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10328 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10329 | } |
| 10330 | return(buf); |
| 10331 | } |
| 10332 | |
| 10333 | /** |
| 10334 | * xmlParseEncodingDecl: |
| 10335 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10336 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10337 | * parse the XML encoding declaration |
| 10338 | * |
| 10339 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 10340 | * |
| 10341 | * this setups the conversion filters. |
| 10342 | * |
| 10343 | * Returns the encoding value or NULL |
| 10344 | */ |
| 10345 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10346 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10347 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 10348 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10349 | |
| 10350 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10351 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10352 | SKIP(8); |
| 10353 | SKIP_BLANKS; |
| 10354 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10355 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10356 | return(NULL); |
| 10357 | } |
| 10358 | NEXT; |
| 10359 | SKIP_BLANKS; |
| 10360 | if (RAW == '"') { |
| 10361 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10362 | encoding = xmlParseEncName(ctxt); |
| 10363 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10364 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10365 | } else |
| 10366 | NEXT; |
| 10367 | } else if (RAW == '\''){ |
| 10368 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10369 | encoding = xmlParseEncName(ctxt); |
| 10370 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10371 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10372 | } else |
| 10373 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 10374 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10375 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10376 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 10377 | |
| 10378 | /* |
| 10379 | * Non standard parsing, allowing the user to ignore encoding |
| 10380 | */ |
| 10381 | if (ctxt->options & XML_PARSE_IGNORE_ENC) |
| 10382 | return(encoding); |
| 10383 | |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10384 | /* |
| 10385 | * UTF-16 encoding stwich has already taken place at this stage, |
| 10386 | * more over the little-endian/big-endian selection is already done |
| 10387 | */ |
| 10388 | if ((encoding != NULL) && |
| 10389 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 10390 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10391 | /* |
| 10392 | * If no encoding was passed to the parser, that we are |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10393 | * using UTF-16 and no decoder is present i.e. the |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10394 | * document is apparently UTF-8 compatible, then raise an |
| 10395 | * encoding mismatch fatal error |
| 10396 | */ |
| 10397 | if ((ctxt->encoding == NULL) && |
| 10398 | (ctxt->input->buf != NULL) && |
| 10399 | (ctxt->input->buf->encoder == NULL)) { |
| 10400 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 10401 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 10402 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10403 | if (ctxt->encoding != NULL) |
| 10404 | xmlFree((xmlChar *) ctxt->encoding); |
| 10405 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10406 | } |
| 10407 | /* |
| 10408 | * UTF-8 encoding is handled natively |
| 10409 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10410 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10411 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 10412 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10413 | if (ctxt->encoding != NULL) |
| 10414 | xmlFree((xmlChar *) ctxt->encoding); |
| 10415 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10416 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10417 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10418 | xmlCharEncodingHandlerPtr handler; |
| 10419 | |
| 10420 | if (ctxt->input->encoding != NULL) |
| 10421 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 10422 | ctxt->input->encoding = encoding; |
| 10423 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10424 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 10425 | if (handler != NULL) { |
| 10426 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10427 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10428 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10429 | "Unsupported encoding %s\n", encoding); |
| 10430 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10431 | } |
| 10432 | } |
| 10433 | } |
| 10434 | return(encoding); |
| 10435 | } |
| 10436 | |
| 10437 | /** |
| 10438 | * xmlParseSDDecl: |
| 10439 | * @ctxt: an XML parser context |
| 10440 | * |
| 10441 | * parse the XML standalone declaration |
| 10442 | * |
| 10443 | * [32] SDDecl ::= S 'standalone' Eq |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10444 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10445 | * |
| 10446 | * [ VC: Standalone Document Declaration ] |
| 10447 | * TODO The standalone document declaration must have the value "no" |
| 10448 | * if any external markup declarations contain declarations of: |
| 10449 | * - attributes with default values, if elements to which these |
| 10450 | * attributes apply appear in the document without specifications |
| 10451 | * of values for these attributes, or |
| 10452 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 10453 | * to those entities appear in the document, or |
| 10454 | * - attributes with values subject to normalization, where the |
| 10455 | * attribute appears in the document with a value which will change |
| 10456 | * as a result of normalization, or |
| 10457 | * - element types with element content, if white space occurs directly |
| 10458 | * within any instance of those types. |
| 10459 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10460 | * Returns: |
| 10461 | * 1 if standalone="yes" |
| 10462 | * 0 if standalone="no" |
| 10463 | * -2 if standalone attribute is missing or invalid |
| 10464 | * (A standalone value of -2 means that the XML declaration was found, |
| 10465 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10466 | */ |
| 10467 | |
| 10468 | int |
| 10469 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10470 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10471 | |
| 10472 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10473 | 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] | 10474 | SKIP(10); |
| 10475 | SKIP_BLANKS; |
| 10476 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10477 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10478 | return(standalone); |
| 10479 | } |
| 10480 | NEXT; |
| 10481 | SKIP_BLANKS; |
| 10482 | if (RAW == '\''){ |
| 10483 | NEXT; |
| 10484 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10485 | standalone = 0; |
| 10486 | SKIP(2); |
| 10487 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10488 | (NXT(2) == 's')) { |
| 10489 | standalone = 1; |
| 10490 | SKIP(3); |
| 10491 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10492 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10493 | } |
| 10494 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10495 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10496 | } else |
| 10497 | NEXT; |
| 10498 | } else if (RAW == '"'){ |
| 10499 | NEXT; |
| 10500 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10501 | standalone = 0; |
| 10502 | SKIP(2); |
| 10503 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10504 | (NXT(2) == 's')) { |
| 10505 | standalone = 1; |
| 10506 | SKIP(3); |
| 10507 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10508 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10509 | } |
| 10510 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10511 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10512 | } else |
| 10513 | NEXT; |
| 10514 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10515 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10516 | } |
| 10517 | } |
| 10518 | return(standalone); |
| 10519 | } |
| 10520 | |
| 10521 | /** |
| 10522 | * xmlParseXMLDecl: |
| 10523 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10524 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10525 | * parse an XML declaration header |
| 10526 | * |
| 10527 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 10528 | */ |
| 10529 | |
| 10530 | void |
| 10531 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 10532 | xmlChar *version; |
| 10533 | |
| 10534 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 10535 | * This value for standalone indicates that the document has an |
| 10536 | * XML declaration but it does not have a standalone attribute. |
| 10537 | * It will be overwritten later if a standalone attribute is found. |
| 10538 | */ |
| 10539 | ctxt->input->standalone = -2; |
| 10540 | |
| 10541 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10542 | * We know that '<?xml' is here. |
| 10543 | */ |
| 10544 | SKIP(5); |
| 10545 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10546 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10547 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 10548 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10549 | } |
| 10550 | SKIP_BLANKS; |
| 10551 | |
| 10552 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10553 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10554 | */ |
| 10555 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10556 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10557 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10558 | } else { |
| 10559 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 10560 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10561 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10562 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10563 | if (ctxt->options & XML_PARSE_OLD10) { |
| 10564 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10565 | "Unsupported version '%s'\n", |
| 10566 | version); |
| 10567 | } else { |
| 10568 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 10569 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 10570 | "Unsupported version '%s'\n", |
| 10571 | version, NULL); |
| 10572 | } else { |
| 10573 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10574 | "Unsupported version '%s'\n", |
| 10575 | version); |
| 10576 | } |
| 10577 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10578 | } |
| 10579 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 10580 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10581 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 10582 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10583 | |
| 10584 | /* |
| 10585 | * We may have the encoding declaration |
| 10586 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10587 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10588 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10589 | SKIP(2); |
| 10590 | return; |
| 10591 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10592 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10593 | } |
| 10594 | xmlParseEncodingDecl(ctxt); |
| 10595 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10596 | /* |
| 10597 | * The XML REC instructs us to stop parsing right here |
| 10598 | */ |
| 10599 | return; |
| 10600 | } |
| 10601 | |
| 10602 | /* |
| 10603 | * We may have the standalone status. |
| 10604 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10605 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10606 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10607 | SKIP(2); |
| 10608 | return; |
| 10609 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10610 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10611 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 10612 | |
| 10613 | /* |
| 10614 | * We can grow the input buffer freely at that point |
| 10615 | */ |
| 10616 | GROW; |
| 10617 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10618 | SKIP_BLANKS; |
| 10619 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 10620 | |
| 10621 | SKIP_BLANKS; |
| 10622 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10623 | SKIP(2); |
| 10624 | } else if (RAW == '>') { |
| 10625 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10626 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10627 | NEXT; |
| 10628 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10629 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10630 | MOVETO_ENDTAG(CUR_PTR); |
| 10631 | NEXT; |
| 10632 | } |
| 10633 | } |
| 10634 | |
| 10635 | /** |
| 10636 | * xmlParseMisc: |
| 10637 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10638 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10639 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10640 | * |
| 10641 | * [27] Misc ::= Comment | PI | S |
| 10642 | */ |
| 10643 | |
| 10644 | void |
| 10645 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 10646 | while ((ctxt->instate != XML_PARSER_EOF) && |
| 10647 | (((RAW == '<') && (NXT(1) == '?')) || |
| 10648 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
| 10649 | IS_BLANK_CH(CUR))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10650 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10651 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10652 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10653 | NEXT; |
| 10654 | } else |
| 10655 | xmlParseComment(ctxt); |
| 10656 | } |
| 10657 | } |
| 10658 | |
| 10659 | /** |
| 10660 | * xmlParseDocument: |
| 10661 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10662 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10663 | * parse an XML document (and build a tree if using the standard SAX |
| 10664 | * interface). |
| 10665 | * |
| 10666 | * [1] document ::= prolog element Misc* |
| 10667 | * |
| 10668 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 10669 | * |
| 10670 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10671 | * as a result of the parsing. |
| 10672 | */ |
| 10673 | |
| 10674 | int |
| 10675 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 10676 | xmlChar start[4]; |
| 10677 | xmlCharEncoding enc; |
| 10678 | |
| 10679 | xmlInitParser(); |
| 10680 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10681 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10682 | return(-1); |
| 10683 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10684 | GROW; |
| 10685 | |
| 10686 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10687 | * SAX: detecting the level. |
| 10688 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10689 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10690 | |
| 10691 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10692 | * SAX: beginning of the document processing. |
| 10693 | */ |
| 10694 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10695 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10696 | if (ctxt->instate == XML_PARSER_EOF) |
| 10697 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10698 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 10699 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10700 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10701 | /* |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10702 | * Get the 4 first bytes and decode the charset |
| 10703 | * if enc != XML_CHAR_ENCODING_NONE |
| 10704 | * plug some encoding conversion routines. |
| 10705 | */ |
| 10706 | start[0] = RAW; |
| 10707 | start[1] = NXT(1); |
| 10708 | start[2] = NXT(2); |
| 10709 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10710 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10711 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10712 | xmlSwitchEncoding(ctxt, enc); |
| 10713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10714 | } |
| 10715 | |
| 10716 | |
| 10717 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10718 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10719 | } |
| 10720 | |
| 10721 | /* |
| 10722 | * Check for the XMLDecl in the Prolog. |
Daniel Veillard | 7e385bd | 2009-08-26 11:38:49 +0200 | [diff] [blame] | 10723 | * do not GROW here to avoid the detected encoder to decode more |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10724 | * than just the first line, unless the amount of data is really |
| 10725 | * too small to hold "<?xml version="1.0" encoding="foo" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10726 | */ |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10727 | if ((ctxt->input->end - ctxt->input->cur) < 35) { |
| 10728 | GROW; |
| 10729 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10730 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10731 | |
| 10732 | /* |
| 10733 | * Note that we will switch encoding on the fly. |
| 10734 | */ |
| 10735 | xmlParseXMLDecl(ctxt); |
| 10736 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10737 | /* |
| 10738 | * The XML REC instructs us to stop parsing right here |
| 10739 | */ |
| 10740 | return(-1); |
| 10741 | } |
| 10742 | ctxt->standalone = ctxt->input->standalone; |
| 10743 | SKIP_BLANKS; |
| 10744 | } else { |
| 10745 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10746 | } |
| 10747 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10748 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10749 | if (ctxt->instate == XML_PARSER_EOF) |
| 10750 | return(-1); |
Daniel Veillard | 63588f4 | 2013-05-10 14:01:46 +0800 | [diff] [blame] | 10751 | if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) && |
| 10752 | (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) { |
| 10753 | ctxt->myDoc->compression = ctxt->input->buf->compressed; |
| 10754 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10755 | |
| 10756 | /* |
| 10757 | * The Misc part of the Prolog |
| 10758 | */ |
| 10759 | GROW; |
| 10760 | xmlParseMisc(ctxt); |
| 10761 | |
| 10762 | /* |
| 10763 | * Then possibly doc type declaration(s) and more Misc |
| 10764 | * (doctypedecl Misc*)? |
| 10765 | */ |
| 10766 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10767 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10768 | |
| 10769 | ctxt->inSubset = 1; |
| 10770 | xmlParseDocTypeDecl(ctxt); |
| 10771 | if (RAW == '[') { |
| 10772 | ctxt->instate = XML_PARSER_DTD; |
| 10773 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10774 | if (ctxt->instate == XML_PARSER_EOF) |
| 10775 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10776 | } |
| 10777 | |
| 10778 | /* |
| 10779 | * Create and update the external subset. |
| 10780 | */ |
| 10781 | ctxt->inSubset = 2; |
| 10782 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10783 | (!ctxt->disableSAX)) |
| 10784 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10785 | ctxt->extSubSystem, ctxt->extSubURI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10786 | if (ctxt->instate == XML_PARSER_EOF) |
| 10787 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10788 | ctxt->inSubset = 0; |
| 10789 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10790 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10791 | |
| 10792 | ctxt->instate = XML_PARSER_PROLOG; |
| 10793 | xmlParseMisc(ctxt); |
| 10794 | } |
| 10795 | |
| 10796 | /* |
| 10797 | * Time to start parsing the tree itself |
| 10798 | */ |
| 10799 | GROW; |
| 10800 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10801 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10802 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10803 | } else { |
| 10804 | ctxt->instate = XML_PARSER_CONTENT; |
| 10805 | xmlParseElement(ctxt); |
| 10806 | ctxt->instate = XML_PARSER_EPILOG; |
| 10807 | |
| 10808 | |
| 10809 | /* |
| 10810 | * The Misc part at the end |
| 10811 | */ |
| 10812 | xmlParseMisc(ctxt); |
| 10813 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10814 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10815 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10816 | } |
| 10817 | ctxt->instate = XML_PARSER_EOF; |
| 10818 | } |
| 10819 | |
| 10820 | /* |
| 10821 | * SAX: end of the document processing. |
| 10822 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10823 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10824 | ctxt->sax->endDocument(ctxt->userData); |
| 10825 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10826 | /* |
| 10827 | * Remove locally kept entity definitions if the tree was not built |
| 10828 | */ |
| 10829 | if ((ctxt->myDoc != NULL) && |
| 10830 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10831 | xmlFreeDoc(ctxt->myDoc); |
| 10832 | ctxt->myDoc = NULL; |
| 10833 | } |
| 10834 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10835 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10836 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10837 | if (ctxt->valid) |
| 10838 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10839 | if (ctxt->nsWellFormed) |
| 10840 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10841 | if (ctxt->options & XML_PARSE_OLD10) |
| 10842 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10843 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10844 | if (! ctxt->wellFormed) { |
| 10845 | ctxt->valid = 0; |
| 10846 | return(-1); |
| 10847 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10848 | return(0); |
| 10849 | } |
| 10850 | |
| 10851 | /** |
| 10852 | * xmlParseExtParsedEnt: |
| 10853 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10854 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10855 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10856 | * An external general parsed entity is well-formed if it matches the |
| 10857 | * production labeled extParsedEnt. |
| 10858 | * |
| 10859 | * [78] extParsedEnt ::= TextDecl? content |
| 10860 | * |
| 10861 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10862 | * as a result of the parsing. |
| 10863 | */ |
| 10864 | |
| 10865 | int |
| 10866 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10867 | xmlChar start[4]; |
| 10868 | xmlCharEncoding enc; |
| 10869 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10870 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10871 | return(-1); |
| 10872 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10873 | xmlDefaultSAXHandlerInit(); |
| 10874 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10875 | xmlDetectSAX2(ctxt); |
| 10876 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10877 | GROW; |
| 10878 | |
| 10879 | /* |
| 10880 | * SAX: beginning of the document processing. |
| 10881 | */ |
| 10882 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10883 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10884 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10885 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10886 | * Get the 4 first bytes and decode the charset |
| 10887 | * if enc != XML_CHAR_ENCODING_NONE |
| 10888 | * plug some encoding conversion routines. |
| 10889 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10890 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10891 | start[0] = RAW; |
| 10892 | start[1] = NXT(1); |
| 10893 | start[2] = NXT(2); |
| 10894 | start[3] = NXT(3); |
| 10895 | enc = xmlDetectCharEncoding(start, 4); |
| 10896 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10897 | xmlSwitchEncoding(ctxt, enc); |
| 10898 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
| 10901 | |
| 10902 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10903 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10904 | } |
| 10905 | |
| 10906 | /* |
| 10907 | * Check for the XMLDecl in the Prolog. |
| 10908 | */ |
| 10909 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10910 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10911 | |
| 10912 | /* |
| 10913 | * Note that we will switch encoding on the fly. |
| 10914 | */ |
| 10915 | xmlParseXMLDecl(ctxt); |
| 10916 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10917 | /* |
| 10918 | * The XML REC instructs us to stop parsing right here |
| 10919 | */ |
| 10920 | return(-1); |
| 10921 | } |
| 10922 | SKIP_BLANKS; |
| 10923 | } else { |
| 10924 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10925 | } |
| 10926 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10927 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10928 | if (ctxt->instate == XML_PARSER_EOF) |
| 10929 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10930 | |
| 10931 | /* |
| 10932 | * Doing validity checking on chunk doesn't make sense |
| 10933 | */ |
| 10934 | ctxt->instate = XML_PARSER_CONTENT; |
| 10935 | ctxt->validate = 0; |
| 10936 | ctxt->loadsubset = 0; |
| 10937 | ctxt->depth = 0; |
| 10938 | |
| 10939 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10940 | if (ctxt->instate == XML_PARSER_EOF) |
| 10941 | return(-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10942 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10943 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10944 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10945 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10946 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10947 | } |
| 10948 | |
| 10949 | /* |
| 10950 | * SAX: end of the document processing. |
| 10951 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10952 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10953 | ctxt->sax->endDocument(ctxt->userData); |
| 10954 | |
| 10955 | if (! ctxt->wellFormed) return(-1); |
| 10956 | return(0); |
| 10957 | } |
| 10958 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10959 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10960 | /************************************************************************ |
| 10961 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10962 | * Progressive parsing interfaces * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10963 | * * |
| 10964 | ************************************************************************/ |
| 10965 | |
| 10966 | /** |
| 10967 | * xmlParseLookupSequence: |
| 10968 | * @ctxt: an XML parser context |
| 10969 | * @first: the first char to lookup |
| 10970 | * @next: the next char to lookup or zero |
| 10971 | * @third: the next char to lookup or zero |
| 10972 | * |
| 10973 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10974 | * (first) is available in the input stream. |
| 10975 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10976 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10977 | * parser, do not use liberally. |
| 10978 | * |
| 10979 | * Returns the index to the current parsing point if the full sequence |
| 10980 | * is available, -1 otherwise. |
| 10981 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10982 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10983 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10984 | xmlChar next, xmlChar third) { |
| 10985 | int base, len; |
| 10986 | xmlParserInputPtr in; |
| 10987 | const xmlChar *buf; |
| 10988 | |
| 10989 | in = ctxt->input; |
| 10990 | if (in == NULL) return(-1); |
| 10991 | base = in->cur - in->base; |
| 10992 | if (base < 0) return(-1); |
| 10993 | if (ctxt->checkIndex > base) |
| 10994 | base = ctxt->checkIndex; |
| 10995 | if (in->buf == NULL) { |
| 10996 | buf = in->base; |
| 10997 | len = in->length; |
| 10998 | } else { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 10999 | buf = xmlBufContent(in->buf->buffer); |
| 11000 | len = xmlBufUse(in->buf->buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11001 | } |
| 11002 | /* take into account the sequence length */ |
| 11003 | if (third) len -= 2; |
| 11004 | else if (next) len --; |
| 11005 | for (;base < len;base++) { |
| 11006 | if (buf[base] == first) { |
| 11007 | if (third != 0) { |
| 11008 | if ((buf[base + 1] != next) || |
| 11009 | (buf[base + 2] != third)) continue; |
| 11010 | } else if (next != 0) { |
| 11011 | if (buf[base + 1] != next) continue; |
| 11012 | } |
| 11013 | ctxt->checkIndex = 0; |
| 11014 | #ifdef DEBUG_PUSH |
| 11015 | if (next == 0) |
| 11016 | xmlGenericError(xmlGenericErrorContext, |
| 11017 | "PP: lookup '%c' found at %d\n", |
| 11018 | first, base); |
| 11019 | else if (third == 0) |
| 11020 | xmlGenericError(xmlGenericErrorContext, |
| 11021 | "PP: lookup '%c%c' found at %d\n", |
| 11022 | first, next, base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11023 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11024 | xmlGenericError(xmlGenericErrorContext, |
| 11025 | "PP: lookup '%c%c%c' found at %d\n", |
| 11026 | first, next, third, base); |
| 11027 | #endif |
| 11028 | return(base - (in->cur - in->base)); |
| 11029 | } |
| 11030 | } |
| 11031 | ctxt->checkIndex = base; |
| 11032 | #ifdef DEBUG_PUSH |
| 11033 | if (next == 0) |
| 11034 | xmlGenericError(xmlGenericErrorContext, |
| 11035 | "PP: lookup '%c' failed\n", first); |
| 11036 | else if (third == 0) |
| 11037 | xmlGenericError(xmlGenericErrorContext, |
| 11038 | "PP: lookup '%c%c' failed\n", first, next); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11039 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11040 | xmlGenericError(xmlGenericErrorContext, |
| 11041 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 11042 | #endif |
| 11043 | return(-1); |
| 11044 | } |
| 11045 | |
| 11046 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11047 | * xmlParseGetLasts: |
| 11048 | * @ctxt: an XML parser context |
| 11049 | * @lastlt: pointer to store the last '<' from the input |
| 11050 | * @lastgt: pointer to store the last '>' from the input |
| 11051 | * |
| 11052 | * Lookup the last < and > in the current chunk |
| 11053 | */ |
| 11054 | static void |
| 11055 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 11056 | const xmlChar **lastgt) { |
| 11057 | const xmlChar *tmp; |
| 11058 | |
| 11059 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 11060 | xmlGenericError(xmlGenericErrorContext, |
| 11061 | "Internal error: xmlParseGetLasts\n"); |
| 11062 | return; |
| 11063 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11064 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11065 | tmp = ctxt->input->end; |
| 11066 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11067 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11068 | if (tmp < ctxt->input->base) { |
| 11069 | *lastlt = NULL; |
| 11070 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11071 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11072 | *lastlt = tmp; |
| 11073 | tmp++; |
| 11074 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 11075 | if (*tmp == '\'') { |
| 11076 | tmp++; |
| 11077 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 11078 | if (tmp < ctxt->input->end) tmp++; |
| 11079 | } else if (*tmp == '"') { |
| 11080 | tmp++; |
| 11081 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 11082 | if (tmp < ctxt->input->end) tmp++; |
| 11083 | } else |
| 11084 | tmp++; |
| 11085 | } |
| 11086 | if (tmp < ctxt->input->end) |
| 11087 | *lastgt = tmp; |
| 11088 | else { |
| 11089 | tmp = *lastlt; |
| 11090 | tmp--; |
| 11091 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 11092 | if (tmp >= ctxt->input->base) |
| 11093 | *lastgt = tmp; |
| 11094 | else |
| 11095 | *lastgt = NULL; |
| 11096 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11097 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11098 | } else { |
| 11099 | *lastlt = NULL; |
| 11100 | *lastgt = NULL; |
| 11101 | } |
| 11102 | } |
| 11103 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11104 | * xmlCheckCdataPush: |
| 11105 | * @cur: pointer to the bock of characters |
| 11106 | * @len: length of the block in bytes |
| 11107 | * |
| 11108 | * Check that the block of characters is okay as SCdata content [20] |
| 11109 | * |
| 11110 | * Returns the number of bytes to pass if okay, a negative index where an |
| 11111 | * UTF-8 error occured otherwise |
| 11112 | */ |
| 11113 | static int |
| 11114 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 11115 | int ix; |
| 11116 | unsigned char c; |
| 11117 | int codepoint; |
| 11118 | |
| 11119 | if ((utf == NULL) || (len <= 0)) |
| 11120 | return(0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11121 | |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11122 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 11123 | c = utf[ix]; |
| 11124 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 11125 | if (c >= 0x20) |
| 11126 | ix++; |
| 11127 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 11128 | ix++; |
| 11129 | else |
| 11130 | return(-ix); |
| 11131 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 11132 | if (ix + 2 > len) return(ix); |
| 11133 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 11134 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11135 | codepoint = (utf[ix] & 0x1f) << 6; |
| 11136 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11137 | if (!xmlIsCharQ(codepoint)) |
| 11138 | return(-ix); |
| 11139 | ix += 2; |
| 11140 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 11141 | if (ix + 3 > len) return(ix); |
| 11142 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11143 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 11144 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11145 | codepoint = (utf[ix] & 0xf) << 12; |
| 11146 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 11147 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11148 | if (!xmlIsCharQ(codepoint)) |
| 11149 | return(-ix); |
| 11150 | ix += 3; |
| 11151 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 11152 | if (ix + 4 > len) return(ix); |
| 11153 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11154 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 11155 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 11156 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11157 | codepoint = (utf[ix] & 0x7) << 18; |
| 11158 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 11159 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 11160 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11161 | if (!xmlIsCharQ(codepoint)) |
| 11162 | return(-ix); |
| 11163 | ix += 4; |
| 11164 | } else /* unknown encoding */ |
| 11165 | return(-ix); |
| 11166 | } |
| 11167 | return(ix); |
| 11168 | } |
| 11169 | |
| 11170 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11171 | * xmlParseTryOrFinish: |
| 11172 | * @ctxt: an XML parser context |
| 11173 | * @terminate: last chunk indicator |
| 11174 | * |
| 11175 | * Try to progress on parsing |
| 11176 | * |
| 11177 | * Returns zero if no parsing was possible |
| 11178 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11179 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11180 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 11181 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11182 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11183 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11184 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11185 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11186 | if (ctxt->input == NULL) |
| 11187 | return(0); |
| 11188 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11189 | #ifdef DEBUG_PUSH |
| 11190 | switch (ctxt->instate) { |
| 11191 | case XML_PARSER_EOF: |
| 11192 | xmlGenericError(xmlGenericErrorContext, |
| 11193 | "PP: try EOF\n"); break; |
| 11194 | case XML_PARSER_START: |
| 11195 | xmlGenericError(xmlGenericErrorContext, |
| 11196 | "PP: try START\n"); break; |
| 11197 | case XML_PARSER_MISC: |
| 11198 | xmlGenericError(xmlGenericErrorContext, |
| 11199 | "PP: try MISC\n");break; |
| 11200 | case XML_PARSER_COMMENT: |
| 11201 | xmlGenericError(xmlGenericErrorContext, |
| 11202 | "PP: try COMMENT\n");break; |
| 11203 | case XML_PARSER_PROLOG: |
| 11204 | xmlGenericError(xmlGenericErrorContext, |
| 11205 | "PP: try PROLOG\n");break; |
| 11206 | case XML_PARSER_START_TAG: |
| 11207 | xmlGenericError(xmlGenericErrorContext, |
| 11208 | "PP: try START_TAG\n");break; |
| 11209 | case XML_PARSER_CONTENT: |
| 11210 | xmlGenericError(xmlGenericErrorContext, |
| 11211 | "PP: try CONTENT\n");break; |
| 11212 | case XML_PARSER_CDATA_SECTION: |
| 11213 | xmlGenericError(xmlGenericErrorContext, |
| 11214 | "PP: try CDATA_SECTION\n");break; |
| 11215 | case XML_PARSER_END_TAG: |
| 11216 | xmlGenericError(xmlGenericErrorContext, |
| 11217 | "PP: try END_TAG\n");break; |
| 11218 | case XML_PARSER_ENTITY_DECL: |
| 11219 | xmlGenericError(xmlGenericErrorContext, |
| 11220 | "PP: try ENTITY_DECL\n");break; |
| 11221 | case XML_PARSER_ENTITY_VALUE: |
| 11222 | xmlGenericError(xmlGenericErrorContext, |
| 11223 | "PP: try ENTITY_VALUE\n");break; |
| 11224 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11225 | xmlGenericError(xmlGenericErrorContext, |
| 11226 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 11227 | case XML_PARSER_DTD: |
| 11228 | xmlGenericError(xmlGenericErrorContext, |
| 11229 | "PP: try DTD\n");break; |
| 11230 | case XML_PARSER_EPILOG: |
| 11231 | xmlGenericError(xmlGenericErrorContext, |
| 11232 | "PP: try EPILOG\n");break; |
| 11233 | case XML_PARSER_PI: |
| 11234 | xmlGenericError(xmlGenericErrorContext, |
| 11235 | "PP: try PI\n");break; |
| 11236 | case XML_PARSER_IGNORE: |
| 11237 | xmlGenericError(xmlGenericErrorContext, |
| 11238 | "PP: try IGNORE\n");break; |
| 11239 | } |
| 11240 | #endif |
| 11241 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11242 | if ((ctxt->input != NULL) && |
| 11243 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11244 | xmlSHRINK(ctxt); |
| 11245 | ctxt->checkIndex = 0; |
| 11246 | } |
| 11247 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 11248 | |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11249 | while (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11250 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11251 | return(0); |
| 11252 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11253 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11254 | /* |
| 11255 | * Pop-up of finished entities. |
| 11256 | */ |
| 11257 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11258 | xmlPopInput(ctxt); |
| 11259 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11260 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11261 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11262 | avail = ctxt->input->length - |
| 11263 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11264 | else { |
| 11265 | /* |
| 11266 | * If we are operating on converted input, try to flush |
| 11267 | * remainng chars to avoid them stalling in the non-converted |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11268 | * buffer. But do not do this in document start where |
| 11269 | * encoding="..." may not have been read and we work on a |
| 11270 | * guessed encoding. |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11271 | */ |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11272 | if ((ctxt->instate != XML_PARSER_START) && |
| 11273 | (ctxt->input->buf->raw != NULL) && |
| 11274 | (xmlBufIsEmpty(ctxt->input->buf->raw) == 0)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11275 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 11276 | ctxt->input); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11277 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11278 | |
| 11279 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11280 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 11281 | base, current); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11282 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11283 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11284 | (ctxt->input->cur - ctxt->input->base); |
| 11285 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11286 | if (avail < 1) |
| 11287 | goto done; |
| 11288 | switch (ctxt->instate) { |
| 11289 | case XML_PARSER_EOF: |
| 11290 | /* |
| 11291 | * Document parsing is done ! |
| 11292 | */ |
| 11293 | goto done; |
| 11294 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11295 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 11296 | xmlChar start[4]; |
| 11297 | xmlCharEncoding enc; |
| 11298 | |
| 11299 | /* |
| 11300 | * Very first chars read from the document flow. |
| 11301 | */ |
| 11302 | if (avail < 4) |
| 11303 | goto done; |
| 11304 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11305 | /* |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11306 | * Get the 4 first bytes and decode the charset |
| 11307 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11308 | * plug some encoding conversion routines, |
| 11309 | * else xmlSwitchEncoding will set to (default) |
| 11310 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11311 | */ |
| 11312 | start[0] = RAW; |
| 11313 | start[1] = NXT(1); |
| 11314 | start[2] = NXT(2); |
| 11315 | start[3] = NXT(3); |
| 11316 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11317 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11318 | break; |
| 11319 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11320 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 11321 | if (avail < 2) |
| 11322 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11323 | cur = ctxt->input->cur[0]; |
| 11324 | next = ctxt->input->cur[1]; |
| 11325 | if (cur == 0) { |
| 11326 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11327 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11328 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11329 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11330 | ctxt->instate = XML_PARSER_EOF; |
| 11331 | #ifdef DEBUG_PUSH |
| 11332 | xmlGenericError(xmlGenericErrorContext, |
| 11333 | "PP: entering EOF\n"); |
| 11334 | #endif |
| 11335 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11336 | ctxt->sax->endDocument(ctxt->userData); |
| 11337 | goto done; |
| 11338 | } |
| 11339 | if ((cur == '<') && (next == '?')) { |
| 11340 | /* PI or XML decl */ |
| 11341 | if (avail < 5) return(ret); |
| 11342 | if ((!terminate) && |
| 11343 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11344 | return(ret); |
| 11345 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11346 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11347 | &xmlDefaultSAXLocator); |
| 11348 | if ((ctxt->input->cur[2] == 'x') && |
| 11349 | (ctxt->input->cur[3] == 'm') && |
| 11350 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11351 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11352 | ret += 5; |
| 11353 | #ifdef DEBUG_PUSH |
| 11354 | xmlGenericError(xmlGenericErrorContext, |
| 11355 | "PP: Parsing XML Decl\n"); |
| 11356 | #endif |
| 11357 | xmlParseXMLDecl(ctxt); |
| 11358 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 11359 | /* |
| 11360 | * The XML REC instructs us to stop parsing right |
| 11361 | * here |
| 11362 | */ |
| 11363 | ctxt->instate = XML_PARSER_EOF; |
| 11364 | return(0); |
| 11365 | } |
| 11366 | ctxt->standalone = ctxt->input->standalone; |
| 11367 | if ((ctxt->encoding == NULL) && |
| 11368 | (ctxt->input->encoding != NULL)) |
| 11369 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 11370 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11371 | (!ctxt->disableSAX)) |
| 11372 | ctxt->sax->startDocument(ctxt->userData); |
| 11373 | ctxt->instate = XML_PARSER_MISC; |
| 11374 | #ifdef DEBUG_PUSH |
| 11375 | xmlGenericError(xmlGenericErrorContext, |
| 11376 | "PP: entering MISC\n"); |
| 11377 | #endif |
| 11378 | } else { |
| 11379 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 11380 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11381 | (!ctxt->disableSAX)) |
| 11382 | ctxt->sax->startDocument(ctxt->userData); |
| 11383 | ctxt->instate = XML_PARSER_MISC; |
| 11384 | #ifdef DEBUG_PUSH |
| 11385 | xmlGenericError(xmlGenericErrorContext, |
| 11386 | "PP: entering MISC\n"); |
| 11387 | #endif |
| 11388 | } |
| 11389 | } else { |
| 11390 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11391 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11392 | &xmlDefaultSAXLocator); |
| 11393 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11394 | if (ctxt->version == NULL) { |
| 11395 | xmlErrMemory(ctxt, NULL); |
| 11396 | break; |
| 11397 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11398 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11399 | (!ctxt->disableSAX)) |
| 11400 | ctxt->sax->startDocument(ctxt->userData); |
| 11401 | ctxt->instate = XML_PARSER_MISC; |
| 11402 | #ifdef DEBUG_PUSH |
| 11403 | xmlGenericError(xmlGenericErrorContext, |
| 11404 | "PP: entering MISC\n"); |
| 11405 | #endif |
| 11406 | } |
| 11407 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11408 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11409 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 11410 | const xmlChar *prefix = NULL; |
| 11411 | const xmlChar *URI = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11412 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11413 | |
| 11414 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11415 | goto done; |
| 11416 | cur = ctxt->input->cur[0]; |
| 11417 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11418 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11419 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11420 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11421 | ctxt->sax->endDocument(ctxt->userData); |
| 11422 | goto done; |
| 11423 | } |
| 11424 | if (!terminate) { |
| 11425 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 11426 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11427 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11428 | goto done; |
| 11429 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11430 | goto done; |
| 11431 | } |
| 11432 | } |
| 11433 | if (ctxt->spaceNr == 0) |
| 11434 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 11435 | else if (*ctxt->space == -2) |
| 11436 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11437 | else |
| 11438 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11439 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11440 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11441 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11442 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11443 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11444 | else |
| 11445 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11446 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11447 | if (ctxt->instate == XML_PARSER_EOF) |
| 11448 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11449 | if (name == NULL) { |
| 11450 | spacePop(ctxt); |
| 11451 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11452 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11453 | ctxt->sax->endDocument(ctxt->userData); |
| 11454 | goto done; |
| 11455 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11456 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11457 | /* |
| 11458 | * [ VC: Root Element Type ] |
| 11459 | * The Name in the document type declaration must match |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11460 | * the element type of the root element. |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11461 | */ |
| 11462 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 11463 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 11464 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11465 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11466 | |
| 11467 | /* |
| 11468 | * Check for an Empty Element. |
| 11469 | */ |
| 11470 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 11471 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11472 | |
| 11473 | if (ctxt->sax2) { |
| 11474 | if ((ctxt->sax != NULL) && |
| 11475 | (ctxt->sax->endElementNs != NULL) && |
| 11476 | (!ctxt->disableSAX)) |
| 11477 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 11478 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 11479 | if (ctxt->nsNr - nsNr > 0) |
| 11480 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11481 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11482 | } else { |
| 11483 | if ((ctxt->sax != NULL) && |
| 11484 | (ctxt->sax->endElement != NULL) && |
| 11485 | (!ctxt->disableSAX)) |
| 11486 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11487 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11488 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11489 | if (ctxt->instate == XML_PARSER_EOF) |
| 11490 | goto done; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11491 | spacePop(ctxt); |
| 11492 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11493 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11494 | } else { |
| 11495 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11496 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11497 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11498 | break; |
| 11499 | } |
| 11500 | if (RAW == '>') { |
| 11501 | NEXT; |
| 11502 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 11503 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11504 | "Couldn't find end of Start Tag %s\n", |
| 11505 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11506 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11507 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11508 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11509 | if (ctxt->sax2) |
| 11510 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11511 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11512 | else |
| 11513 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11514 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11515 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11516 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11517 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11518 | break; |
| 11519 | } |
| 11520 | case XML_PARSER_CONTENT: { |
| 11521 | const xmlChar *test; |
| 11522 | unsigned int cons; |
| 11523 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11524 | goto done; |
| 11525 | cur = ctxt->input->cur[0]; |
| 11526 | next = ctxt->input->cur[1]; |
| 11527 | |
| 11528 | test = CUR_PTR; |
| 11529 | cons = ctxt->input->consumed; |
| 11530 | if ((cur == '<') && (next == '/')) { |
| 11531 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11532 | break; |
| 11533 | } else if ((cur == '<') && (next == '?')) { |
| 11534 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11535 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11536 | ctxt->progressive = XML_PARSER_PI; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11537 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11538 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11539 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11540 | ctxt->instate = XML_PARSER_CONTENT; |
| 11541 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11542 | } else if ((cur == '<') && (next != '!')) { |
| 11543 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11544 | break; |
| 11545 | } else if ((cur == '<') && (next == '!') && |
| 11546 | (ctxt->input->cur[2] == '-') && |
| 11547 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 11548 | int term; |
| 11549 | |
| 11550 | if (avail < 4) |
| 11551 | goto done; |
| 11552 | ctxt->input->cur += 4; |
| 11553 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 11554 | ctxt->input->cur -= 4; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11555 | if ((!terminate) && (term < 0)) { |
| 11556 | ctxt->progressive = XML_PARSER_COMMENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11557 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11558 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11559 | xmlParseComment(ctxt); |
| 11560 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11561 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11562 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 11563 | (ctxt->input->cur[2] == '[') && |
| 11564 | (ctxt->input->cur[3] == 'C') && |
| 11565 | (ctxt->input->cur[4] == 'D') && |
| 11566 | (ctxt->input->cur[5] == 'A') && |
| 11567 | (ctxt->input->cur[6] == 'T') && |
| 11568 | (ctxt->input->cur[7] == 'A') && |
| 11569 | (ctxt->input->cur[8] == '[')) { |
| 11570 | SKIP(9); |
| 11571 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11572 | break; |
| 11573 | } else if ((cur == '<') && (next == '!') && |
| 11574 | (avail < 9)) { |
| 11575 | goto done; |
| 11576 | } else if (cur == '&') { |
| 11577 | if ((!terminate) && |
| 11578 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 11579 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11580 | xmlParseReference(ctxt); |
| 11581 | } else { |
| 11582 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 11583 | /* |
| 11584 | * Goal of the following test is: |
| 11585 | * - minimize calls to the SAX 'character' callback |
| 11586 | * when they are mergeable |
| 11587 | * - handle an problem for isBlank when we only parse |
| 11588 | * a sequence of blank chars and the next one is |
| 11589 | * not available to check against '<' presence. |
| 11590 | * - tries to homogenize the differences in SAX |
| 11591 | * callbacks between the push and pull versions |
| 11592 | * of the parser. |
| 11593 | */ |
| 11594 | if ((ctxt->inputNr == 1) && |
| 11595 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 11596 | if (!terminate) { |
| 11597 | if (ctxt->progressive) { |
| 11598 | if ((lastlt == NULL) || |
| 11599 | (ctxt->input->cur > lastlt)) |
| 11600 | goto done; |
| 11601 | } else if (xmlParseLookupSequence(ctxt, |
| 11602 | '<', 0, 0) < 0) { |
| 11603 | goto done; |
| 11604 | } |
| 11605 | } |
| 11606 | } |
| 11607 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11608 | xmlParseCharData(ctxt, 0); |
| 11609 | } |
| 11610 | /* |
| 11611 | * Pop-up of finished entities. |
| 11612 | */ |
| 11613 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11614 | xmlPopInput(ctxt); |
| 11615 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11616 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 11617 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11618 | ctxt->instate = XML_PARSER_EOF; |
| 11619 | break; |
| 11620 | } |
| 11621 | break; |
| 11622 | } |
| 11623 | case XML_PARSER_END_TAG: |
| 11624 | if (avail < 2) |
| 11625 | goto done; |
| 11626 | if (!terminate) { |
| 11627 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11628 | /* > can be found unescaped in attribute values */ |
| 11629 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11630 | goto done; |
| 11631 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11632 | goto done; |
| 11633 | } |
| 11634 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11635 | if (ctxt->sax2) { |
| 11636 | xmlParseEndTag2(ctxt, |
| 11637 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 11638 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11639 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11640 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11641 | } |
| 11642 | #ifdef LIBXML_SAX1_ENABLED |
| 11643 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11644 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11645 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11646 | if (ctxt->instate == XML_PARSER_EOF) { |
| 11647 | /* Nothing */ |
| 11648 | } else if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11649 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11650 | } else { |
| 11651 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11652 | } |
| 11653 | break; |
| 11654 | case XML_PARSER_CDATA_SECTION: { |
| 11655 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11656 | * The Push mode need to have the SAX callback for |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11657 | * cdataBlock merge back contiguous callbacks. |
| 11658 | */ |
| 11659 | int base; |
| 11660 | |
| 11661 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 11662 | if (base < 0) { |
| 11663 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11664 | int tmp; |
| 11665 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11666 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11667 | XML_PARSER_BIG_BUFFER_SIZE); |
| 11668 | if (tmp < 0) { |
| 11669 | tmp = -tmp; |
| 11670 | ctxt->input->cur += tmp; |
| 11671 | goto encoding_error; |
| 11672 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11673 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 11674 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11675 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11676 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11677 | else if (ctxt->sax->characters != NULL) |
| 11678 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11679 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11680 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11681 | if (ctxt->instate == XML_PARSER_EOF) |
| 11682 | goto done; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11683 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11684 | ctxt->checkIndex = 0; |
| 11685 | } |
| 11686 | goto done; |
| 11687 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11688 | int tmp; |
| 11689 | |
| 11690 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 11691 | if ((tmp < 0) || (tmp != base)) { |
| 11692 | tmp = -tmp; |
| 11693 | ctxt->input->cur += tmp; |
| 11694 | goto encoding_error; |
| 11695 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 11696 | if ((ctxt->sax != NULL) && (base == 0) && |
| 11697 | (ctxt->sax->cdataBlock != NULL) && |
| 11698 | (!ctxt->disableSAX)) { |
| 11699 | /* |
| 11700 | * Special case to provide identical behaviour |
| 11701 | * between pull and push parsers on enpty CDATA |
| 11702 | * sections |
| 11703 | */ |
| 11704 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 11705 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 11706 | "<![CDATA[", 9))) |
| 11707 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11708 | BAD_CAST "", 0); |
| 11709 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11710 | (!ctxt->disableSAX)) { |
| 11711 | if (ctxt->sax->cdataBlock != NULL) |
| 11712 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11713 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11714 | else if (ctxt->sax->characters != NULL) |
| 11715 | ctxt->sax->characters(ctxt->userData, |
| 11716 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11717 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11718 | if (ctxt->instate == XML_PARSER_EOF) |
| 11719 | goto done; |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 11720 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11721 | ctxt->checkIndex = 0; |
| 11722 | ctxt->instate = XML_PARSER_CONTENT; |
| 11723 | #ifdef DEBUG_PUSH |
| 11724 | xmlGenericError(xmlGenericErrorContext, |
| 11725 | "PP: entering CONTENT\n"); |
| 11726 | #endif |
| 11727 | } |
| 11728 | break; |
| 11729 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11730 | case XML_PARSER_MISC: |
| 11731 | SKIP_BLANKS; |
| 11732 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11733 | avail = ctxt->input->length - |
| 11734 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11735 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11736 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11737 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11738 | if (avail < 2) |
| 11739 | goto done; |
| 11740 | cur = ctxt->input->cur[0]; |
| 11741 | next = ctxt->input->cur[1]; |
| 11742 | if ((cur == '<') && (next == '?')) { |
| 11743 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11744 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11745 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11746 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11747 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11748 | #ifdef DEBUG_PUSH |
| 11749 | xmlGenericError(xmlGenericErrorContext, |
| 11750 | "PP: Parsing PI\n"); |
| 11751 | #endif |
| 11752 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11753 | if (ctxt->instate == XML_PARSER_EOF) |
| 11754 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11755 | ctxt->instate = XML_PARSER_MISC; |
| 11756 | ctxt->progressive = 1; |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11757 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11758 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11759 | (ctxt->input->cur[2] == '-') && |
| 11760 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11761 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11762 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11763 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11764 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11765 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11766 | #ifdef DEBUG_PUSH |
| 11767 | xmlGenericError(xmlGenericErrorContext, |
| 11768 | "PP: Parsing Comment\n"); |
| 11769 | #endif |
| 11770 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11771 | if (ctxt->instate == XML_PARSER_EOF) |
| 11772 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11773 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11774 | ctxt->progressive = 1; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11775 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11776 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11777 | (ctxt->input->cur[2] == 'D') && |
| 11778 | (ctxt->input->cur[3] == 'O') && |
| 11779 | (ctxt->input->cur[4] == 'C') && |
| 11780 | (ctxt->input->cur[5] == 'T') && |
| 11781 | (ctxt->input->cur[6] == 'Y') && |
| 11782 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11783 | (ctxt->input->cur[8] == 'E')) { |
| 11784 | if ((!terminate) && |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11785 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) { |
| 11786 | ctxt->progressive = XML_PARSER_DTD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11787 | goto done; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11788 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11789 | #ifdef DEBUG_PUSH |
| 11790 | xmlGenericError(xmlGenericErrorContext, |
| 11791 | "PP: Parsing internal subset\n"); |
| 11792 | #endif |
| 11793 | ctxt->inSubset = 1; |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 11794 | ctxt->progressive = 0; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11795 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11796 | xmlParseDocTypeDecl(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11797 | if (ctxt->instate == XML_PARSER_EOF) |
| 11798 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11799 | if (RAW == '[') { |
| 11800 | ctxt->instate = XML_PARSER_DTD; |
| 11801 | #ifdef DEBUG_PUSH |
| 11802 | xmlGenericError(xmlGenericErrorContext, |
| 11803 | "PP: entering DTD\n"); |
| 11804 | #endif |
| 11805 | } else { |
| 11806 | /* |
| 11807 | * Create and update the external subset. |
| 11808 | */ |
| 11809 | ctxt->inSubset = 2; |
| 11810 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11811 | (ctxt->sax->externalSubset != NULL)) |
| 11812 | ctxt->sax->externalSubset(ctxt->userData, |
| 11813 | ctxt->intSubName, ctxt->extSubSystem, |
| 11814 | ctxt->extSubURI); |
| 11815 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11816 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11817 | ctxt->instate = XML_PARSER_PROLOG; |
| 11818 | #ifdef DEBUG_PUSH |
| 11819 | xmlGenericError(xmlGenericErrorContext, |
| 11820 | "PP: entering PROLOG\n"); |
| 11821 | #endif |
| 11822 | } |
| 11823 | } else if ((cur == '<') && (next == '!') && |
| 11824 | (avail < 9)) { |
| 11825 | goto done; |
| 11826 | } else { |
| 11827 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11828 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11829 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11830 | #ifdef DEBUG_PUSH |
| 11831 | xmlGenericError(xmlGenericErrorContext, |
| 11832 | "PP: entering START_TAG\n"); |
| 11833 | #endif |
| 11834 | } |
| 11835 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11836 | case XML_PARSER_PROLOG: |
| 11837 | SKIP_BLANKS; |
| 11838 | if (ctxt->input->buf == NULL) |
| 11839 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11840 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11841 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11842 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11843 | if (avail < 2) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11844 | goto done; |
| 11845 | cur = ctxt->input->cur[0]; |
| 11846 | next = ctxt->input->cur[1]; |
| 11847 | if ((cur == '<') && (next == '?')) { |
| 11848 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11849 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11850 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11851 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11852 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11853 | #ifdef DEBUG_PUSH |
| 11854 | xmlGenericError(xmlGenericErrorContext, |
| 11855 | "PP: Parsing PI\n"); |
| 11856 | #endif |
| 11857 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11858 | if (ctxt->instate == XML_PARSER_EOF) |
| 11859 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11860 | ctxt->instate = XML_PARSER_PROLOG; |
| 11861 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11862 | } else if ((cur == '<') && (next == '!') && |
| 11863 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11864 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11865 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11866 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11867 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11868 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11869 | #ifdef DEBUG_PUSH |
| 11870 | xmlGenericError(xmlGenericErrorContext, |
| 11871 | "PP: Parsing Comment\n"); |
| 11872 | #endif |
| 11873 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11874 | if (ctxt->instate == XML_PARSER_EOF) |
| 11875 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11876 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11877 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11878 | } else if ((cur == '<') && (next == '!') && |
| 11879 | (avail < 4)) { |
| 11880 | goto done; |
| 11881 | } else { |
| 11882 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11883 | if (ctxt->progressive == 0) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11884 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11885 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11886 | #ifdef DEBUG_PUSH |
| 11887 | xmlGenericError(xmlGenericErrorContext, |
| 11888 | "PP: entering START_TAG\n"); |
| 11889 | #endif |
| 11890 | } |
| 11891 | break; |
| 11892 | case XML_PARSER_EPILOG: |
| 11893 | SKIP_BLANKS; |
| 11894 | if (ctxt->input->buf == NULL) |
| 11895 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11896 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11897 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11898 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11899 | if (avail < 2) |
| 11900 | goto done; |
| 11901 | cur = ctxt->input->cur[0]; |
| 11902 | next = ctxt->input->cur[1]; |
| 11903 | if ((cur == '<') && (next == '?')) { |
| 11904 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11905 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11906 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11907 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11908 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11909 | #ifdef DEBUG_PUSH |
| 11910 | xmlGenericError(xmlGenericErrorContext, |
| 11911 | "PP: Parsing PI\n"); |
| 11912 | #endif |
| 11913 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11914 | if (ctxt->instate == XML_PARSER_EOF) |
| 11915 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11916 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11917 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11918 | } else if ((cur == '<') && (next == '!') && |
| 11919 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11920 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11921 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11922 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11923 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11924 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11925 | #ifdef DEBUG_PUSH |
| 11926 | xmlGenericError(xmlGenericErrorContext, |
| 11927 | "PP: Parsing Comment\n"); |
| 11928 | #endif |
| 11929 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11930 | if (ctxt->instate == XML_PARSER_EOF) |
| 11931 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11932 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11933 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11934 | } else if ((cur == '<') && (next == '!') && |
| 11935 | (avail < 4)) { |
| 11936 | goto done; |
| 11937 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11938 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11939 | ctxt->instate = XML_PARSER_EOF; |
| 11940 | #ifdef DEBUG_PUSH |
| 11941 | xmlGenericError(xmlGenericErrorContext, |
| 11942 | "PP: entering EOF\n"); |
| 11943 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11944 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11945 | ctxt->sax->endDocument(ctxt->userData); |
| 11946 | goto done; |
| 11947 | } |
| 11948 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11949 | case XML_PARSER_DTD: { |
| 11950 | /* |
| 11951 | * Sorry but progressive parsing of the internal subset |
| 11952 | * is not expected to be supported. We first check that |
| 11953 | * the full content of the internal subset is available and |
| 11954 | * the parsing is launched only at that point. |
| 11955 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11956 | * section and not in a ']]>' sequence which are conditional |
| 11957 | * sections (whoever argued to keep that crap in XML deserve |
| 11958 | * a place in hell !). |
| 11959 | */ |
| 11960 | int base, i; |
| 11961 | xmlChar *buf; |
| 11962 | xmlChar quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11963 | size_t use; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11964 | |
| 11965 | base = ctxt->input->cur - ctxt->input->base; |
| 11966 | if (base < 0) return(0); |
| 11967 | if (ctxt->checkIndex > base) |
| 11968 | base = ctxt->checkIndex; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11969 | buf = xmlBufContent(ctxt->input->buf->buffer); |
| 11970 | use = xmlBufUse(ctxt->input->buf->buffer); |
| 11971 | for (;(unsigned int) base < use; base++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11972 | if (quote != 0) { |
| 11973 | if (buf[base] == quote) |
| 11974 | quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11975 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11976 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11977 | if ((quote == 0) && (buf[base] == '<')) { |
| 11978 | int found = 0; |
| 11979 | /* special handling of comments */ |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11980 | if (((unsigned int) base + 4 < use) && |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11981 | (buf[base + 1] == '!') && |
| 11982 | (buf[base + 2] == '-') && |
| 11983 | (buf[base + 3] == '-')) { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11984 | for (;(unsigned int) base + 3 < use; base++) { |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11985 | if ((buf[base] == '-') && |
| 11986 | (buf[base + 1] == '-') && |
| 11987 | (buf[base + 2] == '>')) { |
| 11988 | found = 1; |
| 11989 | base += 2; |
| 11990 | break; |
| 11991 | } |
| 11992 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11993 | if (!found) { |
| 11994 | #if 0 |
| 11995 | fprintf(stderr, "unfinished comment\n"); |
| 11996 | #endif |
| 11997 | break; /* for */ |
| 11998 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11999 | continue; |
| 12000 | } |
| 12001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12002 | if (buf[base] == '"') { |
| 12003 | quote = '"'; |
| 12004 | continue; |
| 12005 | } |
| 12006 | if (buf[base] == '\'') { |
| 12007 | quote = '\''; |
| 12008 | continue; |
| 12009 | } |
| 12010 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12011 | #if 0 |
| 12012 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 12013 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 12014 | #endif |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12015 | if ((unsigned int) base +1 >= use) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12016 | break; |
| 12017 | if (buf[base + 1] == ']') { |
| 12018 | /* conditional crap, skip both ']' ! */ |
| 12019 | base++; |
| 12020 | continue; |
| 12021 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12022 | for (i = 1; (unsigned int) base + i < use; i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12023 | if (buf[base + i] == '>') { |
| 12024 | #if 0 |
| 12025 | fprintf(stderr, "found\n"); |
| 12026 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12027 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12028 | } |
| 12029 | if (!IS_BLANK_CH(buf[base + i])) { |
| 12030 | #if 0 |
| 12031 | fprintf(stderr, "not found\n"); |
| 12032 | #endif |
| 12033 | goto not_end_of_int_subset; |
| 12034 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12035 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12036 | #if 0 |
| 12037 | fprintf(stderr, "end of stream\n"); |
| 12038 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12039 | break; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12040 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12041 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12042 | not_end_of_int_subset: |
| 12043 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12044 | } |
| 12045 | /* |
| 12046 | * We didn't found the end of the Internal subset |
| 12047 | */ |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12048 | if (quote == 0) |
| 12049 | ctxt->checkIndex = base; |
| 12050 | else |
| 12051 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12052 | #ifdef DEBUG_PUSH |
| 12053 | if (next == 0) |
| 12054 | xmlGenericError(xmlGenericErrorContext, |
| 12055 | "PP: lookup of int subset end filed\n"); |
| 12056 | #endif |
| 12057 | goto done; |
| 12058 | |
| 12059 | found_end_int_subset: |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12060 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12061 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12062 | if (ctxt->instate == XML_PARSER_EOF) |
| 12063 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12064 | ctxt->inSubset = 2; |
| 12065 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 12066 | (ctxt->sax->externalSubset != NULL)) |
| 12067 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 12068 | ctxt->extSubSystem, ctxt->extSubURI); |
| 12069 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 12070 | xmlCleanSpecialAttr(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12071 | if (ctxt->instate == XML_PARSER_EOF) |
| 12072 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12073 | ctxt->instate = XML_PARSER_PROLOG; |
| 12074 | ctxt->checkIndex = 0; |
| 12075 | #ifdef DEBUG_PUSH |
| 12076 | xmlGenericError(xmlGenericErrorContext, |
| 12077 | "PP: entering PROLOG\n"); |
| 12078 | #endif |
| 12079 | break; |
| 12080 | } |
| 12081 | case XML_PARSER_COMMENT: |
| 12082 | xmlGenericError(xmlGenericErrorContext, |
| 12083 | "PP: internal error, state == COMMENT\n"); |
| 12084 | ctxt->instate = XML_PARSER_CONTENT; |
| 12085 | #ifdef DEBUG_PUSH |
| 12086 | xmlGenericError(xmlGenericErrorContext, |
| 12087 | "PP: entering CONTENT\n"); |
| 12088 | #endif |
| 12089 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 12090 | case XML_PARSER_IGNORE: |
| 12091 | xmlGenericError(xmlGenericErrorContext, |
| 12092 | "PP: internal error, state == IGNORE"); |
| 12093 | ctxt->instate = XML_PARSER_DTD; |
| 12094 | #ifdef DEBUG_PUSH |
| 12095 | xmlGenericError(xmlGenericErrorContext, |
| 12096 | "PP: entering DTD\n"); |
| 12097 | #endif |
| 12098 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12099 | case XML_PARSER_PI: |
| 12100 | xmlGenericError(xmlGenericErrorContext, |
| 12101 | "PP: internal error, state == PI\n"); |
| 12102 | ctxt->instate = XML_PARSER_CONTENT; |
| 12103 | #ifdef DEBUG_PUSH |
| 12104 | xmlGenericError(xmlGenericErrorContext, |
| 12105 | "PP: entering CONTENT\n"); |
| 12106 | #endif |
| 12107 | break; |
| 12108 | case XML_PARSER_ENTITY_DECL: |
| 12109 | xmlGenericError(xmlGenericErrorContext, |
| 12110 | "PP: internal error, state == ENTITY_DECL\n"); |
| 12111 | ctxt->instate = XML_PARSER_DTD; |
| 12112 | #ifdef DEBUG_PUSH |
| 12113 | xmlGenericError(xmlGenericErrorContext, |
| 12114 | "PP: entering DTD\n"); |
| 12115 | #endif |
| 12116 | break; |
| 12117 | case XML_PARSER_ENTITY_VALUE: |
| 12118 | xmlGenericError(xmlGenericErrorContext, |
| 12119 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 12120 | ctxt->instate = XML_PARSER_CONTENT; |
| 12121 | #ifdef DEBUG_PUSH |
| 12122 | xmlGenericError(xmlGenericErrorContext, |
| 12123 | "PP: entering DTD\n"); |
| 12124 | #endif |
| 12125 | break; |
| 12126 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 12127 | xmlGenericError(xmlGenericErrorContext, |
| 12128 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 12129 | ctxt->instate = XML_PARSER_START_TAG; |
| 12130 | #ifdef DEBUG_PUSH |
| 12131 | xmlGenericError(xmlGenericErrorContext, |
| 12132 | "PP: entering START_TAG\n"); |
| 12133 | #endif |
| 12134 | break; |
| 12135 | case XML_PARSER_SYSTEM_LITERAL: |
| 12136 | xmlGenericError(xmlGenericErrorContext, |
| 12137 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 12138 | ctxt->instate = XML_PARSER_START_TAG; |
| 12139 | #ifdef DEBUG_PUSH |
| 12140 | xmlGenericError(xmlGenericErrorContext, |
| 12141 | "PP: entering START_TAG\n"); |
| 12142 | #endif |
| 12143 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 12144 | case XML_PARSER_PUBLIC_LITERAL: |
| 12145 | xmlGenericError(xmlGenericErrorContext, |
| 12146 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 12147 | ctxt->instate = XML_PARSER_START_TAG; |
| 12148 | #ifdef DEBUG_PUSH |
| 12149 | xmlGenericError(xmlGenericErrorContext, |
| 12150 | "PP: entering START_TAG\n"); |
| 12151 | #endif |
| 12152 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12153 | } |
| 12154 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12155 | done: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12156 | #ifdef DEBUG_PUSH |
| 12157 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 12158 | #endif |
| 12159 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 12160 | encoding_error: |
| 12161 | { |
| 12162 | char buffer[150]; |
| 12163 | |
| 12164 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 12165 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 12166 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 12167 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 12168 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 12169 | BAD_CAST buffer, NULL); |
| 12170 | } |
| 12171 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12172 | } |
| 12173 | |
| 12174 | /** |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12175 | * xmlParseCheckTransition: |
| 12176 | * @ctxt: an XML parser context |
| 12177 | * @chunk: a char array |
| 12178 | * @size: the size in byte of the chunk |
| 12179 | * |
| 12180 | * Check depending on the current parser state if the chunk given must be |
| 12181 | * processed immediately or one need more data to advance on parsing. |
| 12182 | * |
| 12183 | * Returns -1 in case of error, 0 if the push is not needed and 1 if needed |
| 12184 | */ |
| 12185 | static int |
| 12186 | xmlParseCheckTransition(xmlParserCtxtPtr ctxt, const char *chunk, int size) { |
| 12187 | if ((ctxt == NULL) || (chunk == NULL) || (size < 0)) |
| 12188 | return(-1); |
| 12189 | if (ctxt->instate == XML_PARSER_START_TAG) { |
| 12190 | if (memchr(chunk, '>', size) != NULL) |
| 12191 | return(1); |
| 12192 | return(0); |
| 12193 | } |
| 12194 | if (ctxt->progressive == XML_PARSER_COMMENT) { |
| 12195 | if (memchr(chunk, '>', size) != NULL) |
| 12196 | return(1); |
| 12197 | return(0); |
| 12198 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12199 | if (ctxt->instate == XML_PARSER_CDATA_SECTION) { |
| 12200 | if (memchr(chunk, '>', size) != NULL) |
| 12201 | return(1); |
| 12202 | return(0); |
| 12203 | } |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 12204 | if (ctxt->progressive == XML_PARSER_PI) { |
| 12205 | if (memchr(chunk, '>', size) != NULL) |
| 12206 | return(1); |
| 12207 | return(0); |
| 12208 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12209 | if (ctxt->instate == XML_PARSER_END_TAG) { |
| 12210 | if (memchr(chunk, '>', size) != NULL) |
| 12211 | return(1); |
| 12212 | return(0); |
| 12213 | } |
| 12214 | if ((ctxt->progressive == XML_PARSER_DTD) || |
| 12215 | (ctxt->instate == XML_PARSER_DTD)) { |
Dan Winship | cf8f042 | 2012-12-21 11:13:31 +0800 | [diff] [blame] | 12216 | if (memchr(chunk, '>', size) != NULL) |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12217 | return(1); |
| 12218 | return(0); |
| 12219 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12220 | return(1); |
| 12221 | } |
| 12222 | |
| 12223 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12224 | * xmlParseChunk: |
| 12225 | * @ctxt: an XML parser context |
| 12226 | * @chunk: an char array |
| 12227 | * @size: the size in byte of the chunk |
| 12228 | * @terminate: last chunk indicator |
| 12229 | * |
| 12230 | * Parse a Chunk of memory |
| 12231 | * |
| 12232 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 12233 | */ |
| 12234 | int |
| 12235 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 12236 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12237 | int end_in_lf = 0; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12238 | int remain = 0; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12239 | size_t old_avail = 0; |
| 12240 | size_t avail = 0; |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12241 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12242 | if (ctxt == NULL) |
| 12243 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 12244 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12245 | return(ctxt->errNo); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 12246 | if (ctxt->instate == XML_PARSER_EOF) |
| 12247 | return(-1); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 12248 | if (ctxt->instate == XML_PARSER_START) |
| 12249 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12250 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 12251 | (chunk[size - 1] == '\r')) { |
| 12252 | end_in_lf = 1; |
| 12253 | size--; |
| 12254 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12255 | |
| 12256 | xmldecl_done: |
| 12257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12258 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12259 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12260 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12261 | size_t cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12262 | int res; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12263 | |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12264 | old_avail = xmlBufUse(ctxt->input->buf->buffer); |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12265 | /* |
| 12266 | * Specific handling if we autodetected an encoding, we should not |
| 12267 | * push more than the first line ... which depend on the encoding |
| 12268 | * And only push the rest once the final encoding was detected |
| 12269 | */ |
| 12270 | if ((ctxt->instate == XML_PARSER_START) && (ctxt->input != NULL) && |
| 12271 | (ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL)) { |
Nikolay Sivov | 7304683 | 2010-01-19 15:38:05 +0100 | [diff] [blame] | 12272 | unsigned int len = 45; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12273 | |
| 12274 | if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12275 | BAD_CAST "UTF-16")) || |
| 12276 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12277 | BAD_CAST "UTF16"))) |
| 12278 | len = 90; |
| 12279 | else if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12280 | BAD_CAST "UCS-4")) || |
| 12281 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12282 | BAD_CAST "UCS4"))) |
| 12283 | len = 180; |
| 12284 | |
| 12285 | if (ctxt->input->buf->rawconsumed < len) |
| 12286 | len -= ctxt->input->buf->rawconsumed; |
| 12287 | |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12288 | /* |
| 12289 | * Change size for reading the initial declaration only |
| 12290 | * if size is greater than len. Otherwise, memmove in xmlBufferAdd |
| 12291 | * will blindly copy extra bytes from memory. |
| 12292 | */ |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 12293 | if ((unsigned int) size > len) { |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12294 | remain = size - len; |
| 12295 | size = len; |
| 12296 | } else { |
| 12297 | remain = 0; |
| 12298 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12299 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12300 | res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12301 | if (res < 0) { |
| 12302 | ctxt->errNo = XML_PARSER_EOF; |
| 12303 | ctxt->disableSAX = 1; |
| 12304 | return (XML_PARSER_EOF); |
| 12305 | } |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12306 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12307 | #ifdef DEBUG_PUSH |
| 12308 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12309 | #endif |
| 12310 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12311 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 12312 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 12313 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 12314 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 12315 | (in->raw != NULL)) { |
| 12316 | int nbchars; |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12317 | size_t base = xmlBufGetInputBase(in->buffer, ctxt->input); |
| 12318 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12319 | |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 12320 | nbchars = xmlCharEncInput(in, terminate); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12321 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12322 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12323 | xmlGenericError(xmlGenericErrorContext, |
| 12324 | "xmlParseChunk: encoder error\n"); |
| 12325 | return(XML_ERR_INVALID_ENCODING); |
| 12326 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12327 | xmlBufSetInputBaseCur(in->buffer, ctxt->input, base, current); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12328 | } |
| 12329 | } |
| 12330 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12331 | if (remain != 0) { |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12332 | xmlParseTryOrFinish(ctxt, 0); |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12333 | } else { |
| 12334 | if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) |
| 12335 | avail = xmlBufUse(ctxt->input->buf->buffer); |
| 12336 | /* |
| 12337 | * Depending on the current state it may not be such |
| 12338 | * a good idea to try parsing if there is nothing in the chunk |
| 12339 | * which would be worth doing a parser state transition and we |
| 12340 | * need to wait for more data |
| 12341 | */ |
| 12342 | if ((terminate) || (avail > XML_MAX_TEXT_LENGTH) || |
| 12343 | (old_avail == 0) || (avail == 0) || |
| 12344 | (xmlParseCheckTransition(ctxt, |
| 12345 | (const char *)&ctxt->input->base[old_avail], |
| 12346 | avail - old_avail))) |
| 12347 | xmlParseTryOrFinish(ctxt, terminate); |
| 12348 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12349 | if (ctxt->instate == XML_PARSER_EOF) |
| 12350 | return(ctxt->errNo); |
| 12351 | |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12352 | if ((ctxt->input != NULL) && |
| 12353 | (((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || |
| 12354 | ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && |
| 12355 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 12356 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
| 12357 | ctxt->instate = XML_PARSER_EOF; |
| 12358 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12359 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 12360 | return(ctxt->errNo); |
| 12361 | |
| 12362 | if (remain != 0) { |
| 12363 | chunk += size; |
| 12364 | size = remain; |
| 12365 | remain = 0; |
| 12366 | goto xmldecl_done; |
| 12367 | } |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12368 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 12369 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12370 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 12371 | ctxt->input); |
| 12372 | size_t current = ctxt->input->cur - ctxt->input->base; |
| 12373 | |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12374 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12375 | |
| 12376 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 12377 | base, current); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12378 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12379 | if (terminate) { |
| 12380 | /* |
| 12381 | * Check for termination |
| 12382 | */ |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12383 | int cur_avail = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12384 | |
| 12385 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12386 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12387 | cur_avail = ctxt->input->length - |
| 12388 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12389 | else |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12390 | cur_avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 12391 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12392 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12393 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12394 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 12395 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12396 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12397 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12398 | if ((ctxt->instate == XML_PARSER_EPILOG) && (cur_avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12399 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12400 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12401 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 12402 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12403 | ctxt->sax->endDocument(ctxt->userData); |
| 12404 | } |
| 12405 | ctxt->instate = XML_PARSER_EOF; |
| 12406 | } |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 12407 | if (ctxt->wellFormed == 0) |
| 12408 | return((xmlParserErrors) ctxt->errNo); |
| 12409 | else |
| 12410 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12411 | } |
| 12412 | |
| 12413 | /************************************************************************ |
| 12414 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12415 | * I/O front end functions to the parser * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12416 | * * |
| 12417 | ************************************************************************/ |
| 12418 | |
| 12419 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12420 | * xmlCreatePushParserCtxt: |
| 12421 | * @sax: a SAX handler |
| 12422 | * @user_data: The user data returned on SAX callbacks |
| 12423 | * @chunk: a pointer to an array of chars |
| 12424 | * @size: number of chars in the array |
| 12425 | * @filename: an optional file name or URI |
| 12426 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12427 | * Create a parser context for using the XML parser in push mode. |
| 12428 | * If @buffer and @size are non-NULL, the data is used to detect |
| 12429 | * the encoding. The remaining characters will be parsed so they |
| 12430 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12431 | * To allow content encoding detection, @size should be >= 4 |
| 12432 | * The value of @filename is used for fetching external entities |
| 12433 | * and error/warning reports. |
| 12434 | * |
| 12435 | * Returns the new parser context or NULL |
| 12436 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12437 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12438 | xmlParserCtxtPtr |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12439 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12440 | const char *chunk, int size, const char *filename) { |
| 12441 | xmlParserCtxtPtr ctxt; |
| 12442 | xmlParserInputPtr inputStream; |
| 12443 | xmlParserInputBufferPtr buf; |
| 12444 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12445 | |
| 12446 | /* |
| 12447 | * plug some encoding conversion routines |
| 12448 | */ |
| 12449 | if ((chunk != NULL) && (size >= 4)) |
| 12450 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12451 | |
| 12452 | buf = xmlAllocParserInputBuffer(enc); |
| 12453 | if (buf == NULL) return(NULL); |
| 12454 | |
| 12455 | ctxt = xmlNewParserCtxt(); |
| 12456 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12457 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12458 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12459 | return(NULL); |
| 12460 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12461 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12462 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 12463 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12464 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12465 | xmlFreeParserInputBuffer(buf); |
| 12466 | xmlFreeParserCtxt(ctxt); |
| 12467 | return(NULL); |
| 12468 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12469 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12470 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12471 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12472 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12473 | xmlFree(ctxt->sax); |
| 12474 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12475 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12476 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12477 | xmlFreeParserInputBuffer(buf); |
| 12478 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12479 | return(NULL); |
| 12480 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12481 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12482 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12483 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12484 | else |
| 12485 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12486 | if (user_data != NULL) |
| 12487 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12488 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12489 | if (filename == NULL) { |
| 12490 | ctxt->directory = NULL; |
| 12491 | } else { |
| 12492 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12493 | } |
| 12494 | |
| 12495 | inputStream = xmlNewInputStream(ctxt); |
| 12496 | if (inputStream == NULL) { |
| 12497 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12498 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12499 | return(NULL); |
| 12500 | } |
| 12501 | |
| 12502 | if (filename == NULL) |
| 12503 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12504 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 12505 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 12506 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12507 | if (inputStream->filename == NULL) { |
| 12508 | xmlFreeParserCtxt(ctxt); |
| 12509 | xmlFreeParserInputBuffer(buf); |
| 12510 | return(NULL); |
| 12511 | } |
| 12512 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12513 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 12514 | xmlBufResetInput(inputStream->buf->buffer, inputStream); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12515 | inputPush(ctxt, inputStream); |
| 12516 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 12517 | /* |
| 12518 | * If the caller didn't provide an initial 'chunk' for determining |
| 12519 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 12520 | * that it can be automatically determined later |
| 12521 | */ |
| 12522 | if ((size == 0) || (chunk == NULL)) { |
| 12523 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 12524 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12525 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12526 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12527 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12528 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12529 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12530 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12531 | #ifdef DEBUG_PUSH |
| 12532 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12533 | #endif |
| 12534 | } |
| 12535 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 12536 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12537 | xmlSwitchEncoding(ctxt, enc); |
| 12538 | } |
| 12539 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12540 | return(ctxt); |
| 12541 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 12542 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12543 | |
| 12544 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12545 | * xmlStopParser: |
| 12546 | * @ctxt: an XML parser context |
| 12547 | * |
| 12548 | * Blocks further parser processing |
| 12549 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12550 | void |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12551 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 12552 | if (ctxt == NULL) |
| 12553 | return; |
| 12554 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12555 | ctxt->errNo = XML_ERR_USER_STOP; |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12556 | ctxt->disableSAX = 1; |
| 12557 | if (ctxt->input != NULL) { |
| 12558 | ctxt->input->cur = BAD_CAST""; |
| 12559 | ctxt->input->base = ctxt->input->cur; |
| 12560 | } |
| 12561 | } |
| 12562 | |
| 12563 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12564 | * xmlCreateIOParserCtxt: |
| 12565 | * @sax: a SAX handler |
| 12566 | * @user_data: The user data returned on SAX callbacks |
| 12567 | * @ioread: an I/O read function |
| 12568 | * @ioclose: an I/O close function |
| 12569 | * @ioctx: an I/O handler |
| 12570 | * @enc: the charset encoding if known |
| 12571 | * |
| 12572 | * Create a parser context for using the XML parser with an existing |
| 12573 | * I/O stream |
| 12574 | * |
| 12575 | * Returns the new parser context or NULL |
| 12576 | */ |
| 12577 | xmlParserCtxtPtr |
| 12578 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 12579 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 12580 | void *ioctx, xmlCharEncoding enc) { |
| 12581 | xmlParserCtxtPtr ctxt; |
| 12582 | xmlParserInputPtr inputStream; |
| 12583 | xmlParserInputBufferPtr buf; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12584 | |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 12585 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12586 | |
| 12587 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12588 | if (buf == NULL) { |
| 12589 | if (ioclose != NULL) |
| 12590 | ioclose(ioctx); |
| 12591 | return (NULL); |
| 12592 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12593 | |
| 12594 | ctxt = xmlNewParserCtxt(); |
| 12595 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12596 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12597 | return(NULL); |
| 12598 | } |
| 12599 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12600 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12601 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12602 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12603 | xmlFree(ctxt->sax); |
| 12604 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12605 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12606 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12607 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12608 | return(NULL); |
| 12609 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12610 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12611 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12612 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12613 | else |
| 12614 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12615 | if (user_data != NULL) |
| 12616 | ctxt->userData = user_data; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12617 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12618 | |
| 12619 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 12620 | if (inputStream == NULL) { |
| 12621 | xmlFreeParserCtxt(ctxt); |
| 12622 | return(NULL); |
| 12623 | } |
| 12624 | inputPush(ctxt, inputStream); |
| 12625 | |
| 12626 | return(ctxt); |
| 12627 | } |
| 12628 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12629 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12630 | /************************************************************************ |
| 12631 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12632 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12633 | * * |
| 12634 | ************************************************************************/ |
| 12635 | |
| 12636 | /** |
| 12637 | * xmlIOParseDTD: |
| 12638 | * @sax: the SAX handler block or NULL |
| 12639 | * @input: an Input Buffer |
| 12640 | * @enc: the charset encoding if known |
| 12641 | * |
| 12642 | * Load and parse a DTD |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12643 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12644 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12645 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12646 | */ |
| 12647 | |
| 12648 | xmlDtdPtr |
| 12649 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 12650 | xmlCharEncoding enc) { |
| 12651 | xmlDtdPtr ret = NULL; |
| 12652 | xmlParserCtxtPtr ctxt; |
| 12653 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12654 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12655 | |
| 12656 | if (input == NULL) |
| 12657 | return(NULL); |
| 12658 | |
| 12659 | ctxt = xmlNewParserCtxt(); |
| 12660 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12661 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12662 | return(NULL); |
| 12663 | } |
| 12664 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12665 | /* We are loading a DTD */ |
| 12666 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12667 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12668 | /* |
| 12669 | * Set-up the SAX context |
| 12670 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12671 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12672 | if (ctxt->sax != NULL) |
| 12673 | xmlFree(ctxt->sax); |
| 12674 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 12675 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12676 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12677 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12678 | |
| 12679 | /* |
| 12680 | * generate a parser input from the I/O handler |
| 12681 | */ |
| 12682 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12683 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12684 | if (pinput == NULL) { |
| 12685 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12686 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12687 | xmlFreeParserCtxt(ctxt); |
| 12688 | return(NULL); |
| 12689 | } |
| 12690 | |
| 12691 | /* |
| 12692 | * plug some encoding conversion routines here. |
| 12693 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12694 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 12695 | if (sax != NULL) ctxt->sax = NULL; |
| 12696 | xmlFreeParserCtxt(ctxt); |
| 12697 | return(NULL); |
| 12698 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12699 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12700 | xmlSwitchEncoding(ctxt, enc); |
| 12701 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12702 | |
| 12703 | pinput->filename = NULL; |
| 12704 | pinput->line = 1; |
| 12705 | pinput->col = 1; |
| 12706 | pinput->base = ctxt->input->cur; |
| 12707 | pinput->cur = ctxt->input->cur; |
| 12708 | pinput->free = NULL; |
| 12709 | |
| 12710 | /* |
| 12711 | * let's parse that entity knowing it's an external subset. |
| 12712 | */ |
| 12713 | ctxt->inSubset = 2; |
| 12714 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12715 | if (ctxt->myDoc == NULL) { |
| 12716 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12717 | return(NULL); |
| 12718 | } |
| 12719 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12720 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12721 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12722 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12723 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 12724 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12725 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12726 | * Get the 4 first bytes and decode the charset |
| 12727 | * if enc != XML_CHAR_ENCODING_NONE |
| 12728 | * plug some encoding conversion routines. |
| 12729 | */ |
| 12730 | start[0] = RAW; |
| 12731 | start[1] = NXT(1); |
| 12732 | start[2] = NXT(2); |
| 12733 | start[3] = NXT(3); |
| 12734 | enc = xmlDetectCharEncoding(start, 4); |
| 12735 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12736 | xmlSwitchEncoding(ctxt, enc); |
| 12737 | } |
| 12738 | } |
| 12739 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12740 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 12741 | |
| 12742 | if (ctxt->myDoc != NULL) { |
| 12743 | if (ctxt->wellFormed) { |
| 12744 | ret = ctxt->myDoc->extSubset; |
| 12745 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 12746 | if (ret != NULL) { |
| 12747 | xmlNodePtr tmp; |
| 12748 | |
| 12749 | ret->doc = NULL; |
| 12750 | tmp = ret->children; |
| 12751 | while (tmp != NULL) { |
| 12752 | tmp->doc = NULL; |
| 12753 | tmp = tmp->next; |
| 12754 | } |
| 12755 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12756 | } else { |
| 12757 | ret = NULL; |
| 12758 | } |
| 12759 | xmlFreeDoc(ctxt->myDoc); |
| 12760 | ctxt->myDoc = NULL; |
| 12761 | } |
| 12762 | if (sax != NULL) ctxt->sax = NULL; |
| 12763 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12764 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12765 | return(ret); |
| 12766 | } |
| 12767 | |
| 12768 | /** |
| 12769 | * xmlSAXParseDTD: |
| 12770 | * @sax: the SAX handler block |
| 12771 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12772 | * @SystemID: a NAME* containing the URL to the DTD |
| 12773 | * |
| 12774 | * Load and parse an external subset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12775 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12776 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12777 | */ |
| 12778 | |
| 12779 | xmlDtdPtr |
| 12780 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 12781 | const xmlChar *SystemID) { |
| 12782 | xmlDtdPtr ret = NULL; |
| 12783 | xmlParserCtxtPtr ctxt; |
| 12784 | xmlParserInputPtr input = NULL; |
| 12785 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12786 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12787 | |
| 12788 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 12789 | |
| 12790 | ctxt = xmlNewParserCtxt(); |
| 12791 | if (ctxt == NULL) { |
| 12792 | return(NULL); |
| 12793 | } |
| 12794 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12795 | /* We are loading a DTD */ |
| 12796 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12797 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12798 | /* |
| 12799 | * Set-up the SAX context |
| 12800 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12801 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12802 | if (ctxt->sax != NULL) |
| 12803 | xmlFree(ctxt->sax); |
| 12804 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 12805 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12806 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12807 | |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12808 | /* |
| 12809 | * Canonicalise the system ID |
| 12810 | */ |
| 12811 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 12812 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12813 | xmlFreeParserCtxt(ctxt); |
| 12814 | return(NULL); |
| 12815 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12816 | |
| 12817 | /* |
| 12818 | * Ask the Entity resolver to load the damn thing |
| 12819 | */ |
| 12820 | |
| 12821 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 12822 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 12823 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12824 | if (input == NULL) { |
| 12825 | if (sax != NULL) ctxt->sax = NULL; |
| 12826 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12827 | if (systemIdCanonic != NULL) |
| 12828 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12829 | return(NULL); |
| 12830 | } |
| 12831 | |
| 12832 | /* |
| 12833 | * plug some encoding conversion routines here. |
| 12834 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12835 | if (xmlPushInput(ctxt, input) < 0) { |
| 12836 | if (sax != NULL) ctxt->sax = NULL; |
| 12837 | xmlFreeParserCtxt(ctxt); |
| 12838 | if (systemIdCanonic != NULL) |
| 12839 | xmlFree(systemIdCanonic); |
| 12840 | return(NULL); |
| 12841 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12842 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12843 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 12844 | xmlSwitchEncoding(ctxt, enc); |
| 12845 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12846 | |
| 12847 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12848 | input->filename = (char *) systemIdCanonic; |
| 12849 | else |
| 12850 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12851 | input->line = 1; |
| 12852 | input->col = 1; |
| 12853 | input->base = ctxt->input->cur; |
| 12854 | input->cur = ctxt->input->cur; |
| 12855 | input->free = NULL; |
| 12856 | |
| 12857 | /* |
| 12858 | * let's parse that entity knowing it's an external subset. |
| 12859 | */ |
| 12860 | ctxt->inSubset = 2; |
| 12861 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12862 | if (ctxt->myDoc == NULL) { |
| 12863 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12864 | if (sax != NULL) ctxt->sax = NULL; |
| 12865 | xmlFreeParserCtxt(ctxt); |
| 12866 | return(NULL); |
| 12867 | } |
| 12868 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12869 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12870 | ExternalID, SystemID); |
| 12871 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 12872 | |
| 12873 | if (ctxt->myDoc != NULL) { |
| 12874 | if (ctxt->wellFormed) { |
| 12875 | ret = ctxt->myDoc->extSubset; |
| 12876 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 12877 | if (ret != NULL) { |
| 12878 | xmlNodePtr tmp; |
| 12879 | |
| 12880 | ret->doc = NULL; |
| 12881 | tmp = ret->children; |
| 12882 | while (tmp != NULL) { |
| 12883 | tmp->doc = NULL; |
| 12884 | tmp = tmp->next; |
| 12885 | } |
| 12886 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12887 | } else { |
| 12888 | ret = NULL; |
| 12889 | } |
| 12890 | xmlFreeDoc(ctxt->myDoc); |
| 12891 | ctxt->myDoc = NULL; |
| 12892 | } |
| 12893 | if (sax != NULL) ctxt->sax = NULL; |
| 12894 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12895 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12896 | return(ret); |
| 12897 | } |
| 12898 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12899 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12900 | /** |
| 12901 | * xmlParseDTD: |
| 12902 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12903 | * @SystemID: a NAME* containing the URL to the DTD |
| 12904 | * |
| 12905 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12906 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12907 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12908 | */ |
| 12909 | |
| 12910 | xmlDtdPtr |
| 12911 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 12912 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 12913 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12914 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12915 | |
| 12916 | /************************************************************************ |
| 12917 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12918 | * Front ends when parsing an Entity * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12919 | * * |
| 12920 | ************************************************************************/ |
| 12921 | |
| 12922 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12923 | * xmlParseCtxtExternalEntity: |
| 12924 | * @ctx: the existing parsing context |
| 12925 | * @URL: the URL for the entity to load |
| 12926 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12927 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12928 | * |
| 12929 | * Parse an external general entity within an existing parsing context |
| 12930 | * An external general parsed entity is well-formed if it matches the |
| 12931 | * production labeled extParsedEnt. |
| 12932 | * |
| 12933 | * [78] extParsedEnt ::= TextDecl? content |
| 12934 | * |
| 12935 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12936 | * the parser error code otherwise |
| 12937 | */ |
| 12938 | |
| 12939 | int |
| 12940 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12941 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12942 | xmlParserCtxtPtr ctxt; |
| 12943 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12944 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12945 | xmlSAXHandlerPtr oldsax = NULL; |
| 12946 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12947 | xmlChar start[4]; |
| 12948 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12949 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12950 | if (ctx == NULL) return(-1); |
| 12951 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12952 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12953 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12954 | return(XML_ERR_ENTITY_LOOP); |
| 12955 | } |
| 12956 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12957 | if (lst != NULL) |
| 12958 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12959 | if ((URL == NULL) && (ID == NULL)) |
| 12960 | return(-1); |
| 12961 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12962 | return(-1); |
| 12963 | |
Rob Richards | 798743a | 2009-06-19 13:54:25 -0400 | [diff] [blame] | 12964 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, ctx); |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12965 | if (ctxt == NULL) { |
| 12966 | return(-1); |
| 12967 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12968 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12969 | oldsax = ctxt->sax; |
| 12970 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12971 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12972 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12973 | if (newDoc == NULL) { |
| 12974 | xmlFreeParserCtxt(ctxt); |
| 12975 | return(-1); |
| 12976 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12977 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12978 | if (ctx->myDoc->dict) { |
| 12979 | newDoc->dict = ctx->myDoc->dict; |
| 12980 | xmlDictReference(newDoc->dict); |
| 12981 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12982 | if (ctx->myDoc != NULL) { |
| 12983 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12984 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12985 | } |
| 12986 | if (ctx->myDoc->URL != NULL) { |
| 12987 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12988 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12989 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12990 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12991 | ctxt->sax = oldsax; |
| 12992 | xmlFreeParserCtxt(ctxt); |
| 12993 | newDoc->intSubset = NULL; |
| 12994 | newDoc->extSubset = NULL; |
| 12995 | xmlFreeDoc(newDoc); |
| 12996 | return(-1); |
| 12997 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12998 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12999 | nodePush(ctxt, newDoc->children); |
| 13000 | if (ctx->myDoc == NULL) { |
| 13001 | ctxt->myDoc = newDoc; |
| 13002 | } else { |
| 13003 | ctxt->myDoc = ctx->myDoc; |
| 13004 | newDoc->children->doc = ctx->myDoc; |
| 13005 | } |
| 13006 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13007 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13008 | * Get the 4 first bytes and decode the charset |
| 13009 | * if enc != XML_CHAR_ENCODING_NONE |
| 13010 | * plug some encoding conversion routines. |
| 13011 | */ |
| 13012 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13013 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13014 | start[0] = RAW; |
| 13015 | start[1] = NXT(1); |
| 13016 | start[2] = NXT(2); |
| 13017 | start[3] = NXT(3); |
| 13018 | enc = xmlDetectCharEncoding(start, 4); |
| 13019 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13020 | xmlSwitchEncoding(ctxt, enc); |
| 13021 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13022 | } |
| 13023 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13024 | /* |
| 13025 | * Parse a possible text declaration first |
| 13026 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13027 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13028 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13029 | /* |
| 13030 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 13031 | */ |
| 13032 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 13033 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13034 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13035 | "Version mismatch between document and entity\n"); |
| 13036 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13037 | } |
| 13038 | |
| 13039 | /* |
Daniel Veillard | 4aa68ab | 2012-04-02 17:50:54 +0800 | [diff] [blame] | 13040 | * If the user provided its own SAX callbacks then reuse the |
| 13041 | * useData callback field, otherwise the expected setup in a |
| 13042 | * DOM builder is to have userData == ctxt |
| 13043 | */ |
| 13044 | if (ctx->userData == ctx) |
| 13045 | ctxt->userData = ctxt; |
| 13046 | else |
| 13047 | ctxt->userData = ctx->userData; |
| 13048 | |
| 13049 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13050 | * Doing validity checking on chunk doesn't make sense |
| 13051 | */ |
| 13052 | ctxt->instate = XML_PARSER_CONTENT; |
| 13053 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13054 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13055 | ctxt->loadsubset = ctx->loadsubset; |
| 13056 | ctxt->depth = ctx->depth + 1; |
| 13057 | ctxt->replaceEntities = ctx->replaceEntities; |
| 13058 | if (ctxt->validate) { |
| 13059 | ctxt->vctxt.error = ctx->vctxt.error; |
| 13060 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13061 | } else { |
| 13062 | ctxt->vctxt.error = NULL; |
| 13063 | ctxt->vctxt.warning = NULL; |
| 13064 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 13065 | ctxt->vctxt.nodeTab = NULL; |
| 13066 | ctxt->vctxt.nodeNr = 0; |
| 13067 | ctxt->vctxt.nodeMax = 0; |
| 13068 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13069 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13070 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13071 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13072 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13073 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13074 | ctxt->dictNames = ctx->dictNames; |
| 13075 | ctxt->attsDefault = ctx->attsDefault; |
| 13076 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 13077 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13078 | |
| 13079 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13080 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13081 | ctx->validate = ctxt->validate; |
| 13082 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13083 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13084 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13085 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13086 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13087 | } |
| 13088 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13089 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13090 | } |
| 13091 | |
| 13092 | if (!ctxt->wellFormed) { |
| 13093 | if (ctxt->errNo == 0) |
| 13094 | ret = 1; |
| 13095 | else |
| 13096 | ret = ctxt->errNo; |
| 13097 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13098 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13099 | xmlNodePtr cur; |
| 13100 | |
| 13101 | /* |
| 13102 | * Return the newly created nodeset after unlinking it from |
| 13103 | * they pseudo parent. |
| 13104 | */ |
| 13105 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13106 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13107 | while (cur != NULL) { |
| 13108 | cur->parent = NULL; |
| 13109 | cur = cur->next; |
| 13110 | } |
| 13111 | newDoc->children->children = NULL; |
| 13112 | } |
| 13113 | ret = 0; |
| 13114 | } |
| 13115 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13116 | ctxt->dict = NULL; |
| 13117 | ctxt->attsDefault = NULL; |
| 13118 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13119 | xmlFreeParserCtxt(ctxt); |
| 13120 | newDoc->intSubset = NULL; |
| 13121 | newDoc->extSubset = NULL; |
| 13122 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13123 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13124 | return(ret); |
| 13125 | } |
| 13126 | |
| 13127 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13128 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13129 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13130 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13131 | * @sax: the SAX handler bloc (possibly NULL) |
| 13132 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13133 | * @depth: Used for loop detection, use 0 |
| 13134 | * @URL: the URL for the entity to load |
| 13135 | * @ID: the System ID for the entity to load |
| 13136 | * @list: the return value for the set of parsed nodes |
| 13137 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13138 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13139 | * |
| 13140 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13141 | * the parser error code otherwise |
| 13142 | */ |
| 13143 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13144 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13145 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 13146 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13147 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13148 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13149 | xmlParserCtxtPtr ctxt; |
| 13150 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13151 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13152 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13153 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13154 | xmlChar start[4]; |
| 13155 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13156 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13157 | if (((depth > 40) && |
| 13158 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13159 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13160 | return(XML_ERR_ENTITY_LOOP); |
| 13161 | } |
| 13162 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13163 | if (list != NULL) |
| 13164 | *list = NULL; |
| 13165 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13166 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13167 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13168 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13169 | |
| 13170 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13171 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 13172 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13173 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13174 | if (oldctxt != NULL) { |
| 13175 | ctxt->_private = oldctxt->_private; |
| 13176 | ctxt->loadsubset = oldctxt->loadsubset; |
| 13177 | ctxt->validate = oldctxt->validate; |
| 13178 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13179 | ctxt->record_info = oldctxt->record_info; |
| 13180 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 13181 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 13182 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13183 | } else { |
| 13184 | /* |
| 13185 | * Doing validity checking on chunk without context |
| 13186 | * doesn't make sense |
| 13187 | */ |
| 13188 | ctxt->_private = NULL; |
| 13189 | ctxt->validate = 0; |
| 13190 | ctxt->external = 2; |
| 13191 | ctxt->loadsubset = 0; |
| 13192 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13193 | if (sax != NULL) { |
| 13194 | oldsax = ctxt->sax; |
| 13195 | ctxt->sax = sax; |
| 13196 | if (user_data != NULL) |
| 13197 | ctxt->userData = user_data; |
| 13198 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13199 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13200 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13201 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13202 | ctxt->node_seq.maximum = 0; |
| 13203 | ctxt->node_seq.length = 0; |
| 13204 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13205 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13206 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13207 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13208 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13209 | newDoc->intSubset = doc->intSubset; |
| 13210 | newDoc->extSubset = doc->extSubset; |
| 13211 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13212 | xmlDictReference(newDoc->dict); |
| 13213 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13214 | if (doc->URL != NULL) { |
| 13215 | newDoc->URL = xmlStrdup(doc->URL); |
| 13216 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13217 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13218 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13219 | if (sax != NULL) |
| 13220 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13221 | ctxt->node_seq.maximum = 0; |
| 13222 | ctxt->node_seq.length = 0; |
| 13223 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13224 | xmlFreeParserCtxt(ctxt); |
| 13225 | newDoc->intSubset = NULL; |
| 13226 | newDoc->extSubset = NULL; |
| 13227 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13228 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13229 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13230 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13231 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13232 | ctxt->myDoc = doc; |
| 13233 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13234 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13235 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13236 | * Get the 4 first bytes and decode the charset |
| 13237 | * if enc != XML_CHAR_ENCODING_NONE |
| 13238 | * plug some encoding conversion routines. |
| 13239 | */ |
| 13240 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13241 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13242 | start[0] = RAW; |
| 13243 | start[1] = NXT(1); |
| 13244 | start[2] = NXT(2); |
| 13245 | start[3] = NXT(3); |
| 13246 | enc = xmlDetectCharEncoding(start, 4); |
| 13247 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13248 | xmlSwitchEncoding(ctxt, enc); |
| 13249 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13250 | } |
| 13251 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13252 | /* |
| 13253 | * Parse a possible text declaration first |
| 13254 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13255 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13256 | xmlParseTextDecl(ctxt); |
| 13257 | } |
| 13258 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13259 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13260 | ctxt->depth = depth; |
| 13261 | |
| 13262 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13263 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13264 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13265 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13266 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13267 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13268 | } |
| 13269 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13270 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13271 | } |
| 13272 | |
| 13273 | if (!ctxt->wellFormed) { |
| 13274 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13275 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13276 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13277 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13278 | } else { |
| 13279 | if (list != NULL) { |
| 13280 | xmlNodePtr cur; |
| 13281 | |
| 13282 | /* |
| 13283 | * Return the newly created nodeset after unlinking it from |
| 13284 | * they pseudo parent. |
| 13285 | */ |
| 13286 | cur = newDoc->children->children; |
| 13287 | *list = cur; |
| 13288 | while (cur != NULL) { |
| 13289 | cur->parent = NULL; |
| 13290 | cur = cur->next; |
| 13291 | } |
| 13292 | newDoc->children->children = NULL; |
| 13293 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13294 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13295 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13296 | |
| 13297 | /* |
| 13298 | * Record in the parent context the number of entities replacement |
| 13299 | * done when parsing that reference. |
| 13300 | */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 13301 | if (oldctxt != NULL) |
| 13302 | oldctxt->nbentities += ctxt->nbentities; |
| 13303 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13304 | /* |
| 13305 | * Also record the size of the entity parsed |
| 13306 | */ |
| 13307 | if (ctxt->input != NULL) { |
| 13308 | oldctxt->sizeentities += ctxt->input->consumed; |
| 13309 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 13310 | } |
| 13311 | /* |
| 13312 | * And record the last error if any |
| 13313 | */ |
| 13314 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13315 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13316 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13317 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13318 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 13319 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 13320 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 13321 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13322 | ctxt->node_seq.maximum = 0; |
| 13323 | ctxt->node_seq.length = 0; |
| 13324 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13325 | xmlFreeParserCtxt(ctxt); |
| 13326 | newDoc->intSubset = NULL; |
| 13327 | newDoc->extSubset = NULL; |
| 13328 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13329 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13330 | return(ret); |
| 13331 | } |
| 13332 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13333 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13334 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13335 | * xmlParseExternalEntity: |
| 13336 | * @doc: the document the chunk pertains to |
| 13337 | * @sax: the SAX handler bloc (possibly NULL) |
| 13338 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13339 | * @depth: Used for loop detection, use 0 |
| 13340 | * @URL: the URL for the entity to load |
| 13341 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13342 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13343 | * |
| 13344 | * Parse an external general entity |
| 13345 | * An external general parsed entity is well-formed if it matches the |
| 13346 | * production labeled extParsedEnt. |
| 13347 | * |
| 13348 | * [78] extParsedEnt ::= TextDecl? content |
| 13349 | * |
| 13350 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13351 | * the parser error code otherwise |
| 13352 | */ |
| 13353 | |
| 13354 | int |
| 13355 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13356 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13357 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13358 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13359 | } |
| 13360 | |
| 13361 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 13362 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13363 | * @doc: the document the chunk pertains to |
| 13364 | * @sax: the SAX handler bloc (possibly NULL) |
| 13365 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13366 | * @depth: Used for loop detection, use 0 |
| 13367 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13368 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13369 | * |
| 13370 | * Parse a well-balanced chunk of an XML document |
| 13371 | * called by the parser |
| 13372 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13373 | * the content production in the XML grammar: |
| 13374 | * |
| 13375 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13376 | * |
| 13377 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13378 | * the parser error code otherwise |
| 13379 | */ |
| 13380 | |
| 13381 | int |
| 13382 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13383 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13384 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 13385 | depth, string, lst, 0 ); |
| 13386 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13387 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13388 | |
| 13389 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13390 | * xmlParseBalancedChunkMemoryInternal: |
| 13391 | * @oldctxt: the existing parsing context |
| 13392 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13393 | * @user_data: the user data field for the parser context |
| 13394 | * @lst: the return value for the set of parsed nodes |
| 13395 | * |
| 13396 | * |
| 13397 | * Parse a well-balanced chunk of an XML document |
| 13398 | * called by the parser |
| 13399 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13400 | * the content production in the XML grammar: |
| 13401 | * |
| 13402 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13403 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13404 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13405 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13406 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13407 | * 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] | 13408 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13409 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13410 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13411 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 13412 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 13413 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13414 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13415 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13416 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13417 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13418 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13419 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13420 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13421 | #ifdef SAX2 |
| 13422 | int i; |
| 13423 | #endif |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13424 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13425 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13426 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13427 | return(XML_ERR_ENTITY_LOOP); |
| 13428 | } |
| 13429 | |
| 13430 | |
| 13431 | if (lst != NULL) |
| 13432 | *lst = NULL; |
| 13433 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13434 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13435 | |
| 13436 | size = xmlStrlen(string); |
| 13437 | |
| 13438 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13439 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13440 | if (user_data != NULL) |
| 13441 | ctxt->userData = user_data; |
| 13442 | else |
| 13443 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13444 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13445 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13446 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13447 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13448 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13449 | |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13450 | #ifdef SAX2 |
| 13451 | /* propagate namespaces down the entity */ |
| 13452 | for (i = 0;i < oldctxt->nsNr;i += 2) { |
| 13453 | nsPush(ctxt, oldctxt->nsTab[i], oldctxt->nsTab[i+1]); |
| 13454 | } |
| 13455 | #endif |
| 13456 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13457 | oldsax = ctxt->sax; |
| 13458 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13459 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13460 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 13461 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13462 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 13463 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13464 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13465 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13466 | if (newDoc == NULL) { |
| 13467 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13468 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13469 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13470 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13471 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13472 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13473 | newDoc->dict = ctxt->dict; |
| 13474 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13475 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13476 | } else { |
| 13477 | ctxt->myDoc = oldctxt->myDoc; |
| 13478 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13479 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13480 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13481 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13482 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13483 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13484 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13485 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13486 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13487 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13488 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13489 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13490 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13491 | ctxt->myDoc->children = NULL; |
| 13492 | ctxt->myDoc->last = NULL; |
| 13493 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13494 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13495 | ctxt->instate = XML_PARSER_CONTENT; |
| 13496 | ctxt->depth = oldctxt->depth + 1; |
| 13497 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13498 | ctxt->validate = 0; |
| 13499 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 13500 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 13501 | /* |
| 13502 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13503 | */ |
| 13504 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13505 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13506 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13507 | ctxt->attsDefault = oldctxt->attsDefault; |
| 13508 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13509 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13510 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13511 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13512 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13513 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13514 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13515 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13516 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13517 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13518 | } |
| 13519 | |
| 13520 | if (!ctxt->wellFormed) { |
| 13521 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13522 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13523 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13524 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13525 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13526 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13527 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13528 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13529 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13530 | xmlNodePtr cur; |
| 13531 | |
| 13532 | /* |
| 13533 | * Return the newly created nodeset after unlinking it from |
| 13534 | * they pseudo parent. |
| 13535 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13536 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13537 | *lst = cur; |
| 13538 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13539 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 13540 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 13541 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 13542 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 13543 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 13544 | oldctxt->myDoc, cur); |
| 13545 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13546 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13547 | cur->parent = NULL; |
| 13548 | cur = cur->next; |
| 13549 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13550 | ctxt->myDoc->children->children = NULL; |
| 13551 | } |
| 13552 | if (ctxt->myDoc != NULL) { |
| 13553 | xmlFreeNode(ctxt->myDoc->children); |
| 13554 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13555 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13556 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13557 | |
| 13558 | /* |
| 13559 | * Record in the parent context the number of entities replacement |
| 13560 | * done when parsing that reference. |
| 13561 | */ |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 13562 | if (oldctxt != NULL) |
| 13563 | oldctxt->nbentities += ctxt->nbentities; |
| 13564 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13565 | /* |
| 13566 | * Also record the last error if any |
| 13567 | */ |
| 13568 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13569 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13570 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13571 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13572 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13573 | ctxt->attsDefault = NULL; |
| 13574 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13575 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13576 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13577 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13578 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13579 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13580 | return(ret); |
| 13581 | } |
| 13582 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13583 | /** |
| 13584 | * xmlParseInNodeContext: |
| 13585 | * @node: the context node |
| 13586 | * @data: the input string |
| 13587 | * @datalen: the input string length in bytes |
| 13588 | * @options: a combination of xmlParserOption |
| 13589 | * @lst: the return value for the set of parsed nodes |
| 13590 | * |
| 13591 | * Parse a well-balanced chunk of an XML document |
| 13592 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 13593 | * |
| 13594 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 13595 | * the content production in the XML grammar: |
| 13596 | * |
| 13597 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13598 | * |
| 13599 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13600 | * error code otherwise |
| 13601 | */ |
| 13602 | xmlParserErrors |
| 13603 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 13604 | int options, xmlNodePtr *lst) { |
| 13605 | #ifdef SAX2 |
| 13606 | xmlParserCtxtPtr ctxt; |
| 13607 | xmlDocPtr doc = NULL; |
| 13608 | xmlNodePtr fake, cur; |
| 13609 | int nsnr = 0; |
| 13610 | |
| 13611 | xmlParserErrors ret = XML_ERR_OK; |
| 13612 | |
| 13613 | /* |
| 13614 | * check all input parameters, grab the document |
| 13615 | */ |
| 13616 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 13617 | return(XML_ERR_INTERNAL_ERROR); |
| 13618 | switch (node->type) { |
| 13619 | case XML_ELEMENT_NODE: |
| 13620 | case XML_ATTRIBUTE_NODE: |
| 13621 | case XML_TEXT_NODE: |
| 13622 | case XML_CDATA_SECTION_NODE: |
| 13623 | case XML_ENTITY_REF_NODE: |
| 13624 | case XML_PI_NODE: |
| 13625 | case XML_COMMENT_NODE: |
| 13626 | case XML_DOCUMENT_NODE: |
| 13627 | case XML_HTML_DOCUMENT_NODE: |
| 13628 | break; |
| 13629 | default: |
| 13630 | return(XML_ERR_INTERNAL_ERROR); |
| 13631 | |
| 13632 | } |
| 13633 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 13634 | (node->type != XML_DOCUMENT_NODE) && |
| 13635 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 13636 | node = node->parent; |
| 13637 | if (node == NULL) |
| 13638 | return(XML_ERR_INTERNAL_ERROR); |
| 13639 | if (node->type == XML_ELEMENT_NODE) |
| 13640 | doc = node->doc; |
| 13641 | else |
| 13642 | doc = (xmlDocPtr) node; |
| 13643 | if (doc == NULL) |
| 13644 | return(XML_ERR_INTERNAL_ERROR); |
| 13645 | |
| 13646 | /* |
| 13647 | * allocate a context and set-up everything not related to the |
| 13648 | * node position in the tree |
| 13649 | */ |
| 13650 | if (doc->type == XML_DOCUMENT_NODE) |
| 13651 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 13652 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13653 | else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13654 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13655 | /* |
| 13656 | * When parsing in context, it makes no sense to add implied |
| 13657 | * elements like html/body/etc... |
| 13658 | */ |
| 13659 | options |= HTML_PARSE_NOIMPLIED; |
| 13660 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13661 | #endif |
| 13662 | else |
| 13663 | return(XML_ERR_INTERNAL_ERROR); |
| 13664 | |
| 13665 | if (ctxt == NULL) |
| 13666 | return(XML_ERR_NO_MEMORY); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13667 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13668 | /* |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13669 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 13670 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 13671 | * we must wait until the last moment to free the original one. |
| 13672 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13673 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13674 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13675 | xmlDictFree(ctxt->dict); |
| 13676 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13677 | } else |
| 13678 | options |= XML_PARSE_NODICT; |
| 13679 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13680 | if (doc->encoding != NULL) { |
| 13681 | xmlCharEncodingHandlerPtr hdlr; |
| 13682 | |
| 13683 | if (ctxt->encoding != NULL) |
| 13684 | xmlFree((xmlChar *) ctxt->encoding); |
| 13685 | ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); |
| 13686 | |
Nikolay Sivov | d4a5d98 | 2013-04-30 17:45:36 +0400 | [diff] [blame] | 13687 | hdlr = xmlFindCharEncodingHandler((const char *) doc->encoding); |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13688 | if (hdlr != NULL) { |
| 13689 | xmlSwitchToEncoding(ctxt, hdlr); |
| 13690 | } else { |
| 13691 | return(XML_ERR_UNSUPPORTED_ENCODING); |
| 13692 | } |
| 13693 | } |
| 13694 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13695 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13696 | xmlDetectSAX2(ctxt); |
| 13697 | ctxt->myDoc = doc; |
Daniel Veillard | 6faa126 | 2014-03-21 17:05:51 +0800 | [diff] [blame] | 13698 | /* parsing in context, i.e. as within existing content */ |
| 13699 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13700 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13701 | fake = xmlNewComment(NULL); |
| 13702 | if (fake == NULL) { |
| 13703 | xmlFreeParserCtxt(ctxt); |
| 13704 | return(XML_ERR_NO_MEMORY); |
| 13705 | } |
| 13706 | xmlAddChild(node, fake); |
| 13707 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13708 | if (node->type == XML_ELEMENT_NODE) { |
| 13709 | nodePush(ctxt, node); |
| 13710 | /* |
| 13711 | * initialize the SAX2 namespaces stack |
| 13712 | */ |
| 13713 | cur = node; |
| 13714 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 13715 | xmlNsPtr ns = cur->nsDef; |
| 13716 | const xmlChar *iprefix, *ihref; |
| 13717 | |
| 13718 | while (ns != NULL) { |
| 13719 | if (ctxt->dict) { |
| 13720 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 13721 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 13722 | } else { |
| 13723 | iprefix = ns->prefix; |
| 13724 | ihref = ns->href; |
| 13725 | } |
| 13726 | |
| 13727 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 13728 | nsPush(ctxt, iprefix, ihref); |
| 13729 | nsnr++; |
| 13730 | } |
| 13731 | ns = ns->next; |
| 13732 | } |
| 13733 | cur = cur->parent; |
| 13734 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13735 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13736 | |
| 13737 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 13738 | /* |
| 13739 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13740 | */ |
| 13741 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13742 | } |
| 13743 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 13744 | #ifdef LIBXML_HTML_ENABLED |
| 13745 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 13746 | __htmlParseContent(ctxt); |
| 13747 | else |
| 13748 | #endif |
| 13749 | xmlParseContent(ctxt); |
| 13750 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13751 | nsPop(ctxt, nsnr); |
| 13752 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 13753 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13754 | } else if (RAW != 0) { |
| 13755 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 13756 | } |
| 13757 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 13758 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13759 | ctxt->wellFormed = 0; |
| 13760 | } |
| 13761 | |
| 13762 | if (!ctxt->wellFormed) { |
| 13763 | if (ctxt->errNo == 0) |
| 13764 | ret = XML_ERR_INTERNAL_ERROR; |
| 13765 | else |
| 13766 | ret = (xmlParserErrors)ctxt->errNo; |
| 13767 | } else { |
| 13768 | ret = XML_ERR_OK; |
| 13769 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13770 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13771 | /* |
| 13772 | * Return the newly created nodeset after unlinking it from |
| 13773 | * the pseudo sibling. |
| 13774 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13775 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13776 | cur = fake->next; |
| 13777 | fake->next = NULL; |
| 13778 | node->last = fake; |
| 13779 | |
| 13780 | if (cur != NULL) { |
| 13781 | cur->prev = NULL; |
| 13782 | } |
| 13783 | |
| 13784 | *lst = cur; |
| 13785 | |
| 13786 | while (cur != NULL) { |
| 13787 | cur->parent = NULL; |
| 13788 | cur = cur->next; |
| 13789 | } |
| 13790 | |
| 13791 | xmlUnlinkNode(fake); |
| 13792 | xmlFreeNode(fake); |
| 13793 | |
| 13794 | |
| 13795 | if (ret != XML_ERR_OK) { |
| 13796 | xmlFreeNodeList(*lst); |
| 13797 | *lst = NULL; |
| 13798 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13799 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 13800 | if (doc->dict != NULL) |
| 13801 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13802 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13803 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13804 | return(ret); |
| 13805 | #else /* !SAX2 */ |
| 13806 | return(XML_ERR_INTERNAL_ERROR); |
| 13807 | #endif |
| 13808 | } |
| 13809 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13810 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13811 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13812 | * xmlParseBalancedChunkMemoryRecover: |
| 13813 | * @doc: the document the chunk pertains to |
| 13814 | * @sax: the SAX handler bloc (possibly NULL) |
| 13815 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13816 | * @depth: Used for loop detection, use 0 |
| 13817 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13818 | * @lst: the return value for the set of parsed nodes |
| 13819 | * @recover: return nodes even if the data is broken (use 0) |
| 13820 | * |
| 13821 | * |
| 13822 | * Parse a well-balanced chunk of an XML document |
| 13823 | * called by the parser |
| 13824 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13825 | * the content production in the XML grammar: |
| 13826 | * |
| 13827 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13828 | * |
| 13829 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13830 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13831 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13832 | * 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] | 13833 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 13834 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13835 | */ |
| 13836 | int |
| 13837 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13838 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13839 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13840 | xmlParserCtxtPtr ctxt; |
| 13841 | xmlDocPtr newDoc; |
| 13842 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13843 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13844 | int size; |
| 13845 | int ret = 0; |
| 13846 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13847 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13848 | return(XML_ERR_ENTITY_LOOP); |
| 13849 | } |
| 13850 | |
| 13851 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13852 | if (lst != NULL) |
| 13853 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13854 | if (string == NULL) |
| 13855 | return(-1); |
| 13856 | |
| 13857 | size = xmlStrlen(string); |
| 13858 | |
| 13859 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 13860 | if (ctxt == NULL) return(-1); |
| 13861 | ctxt->userData = ctxt; |
| 13862 | if (sax != NULL) { |
| 13863 | oldsax = ctxt->sax; |
| 13864 | ctxt->sax = sax; |
| 13865 | if (user_data != NULL) |
| 13866 | ctxt->userData = user_data; |
| 13867 | } |
| 13868 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13869 | if (newDoc == NULL) { |
| 13870 | xmlFreeParserCtxt(ctxt); |
| 13871 | return(-1); |
| 13872 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13873 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13874 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 13875 | xmlDictFree(ctxt->dict); |
| 13876 | ctxt->dict = doc->dict; |
| 13877 | xmlDictReference(ctxt->dict); |
| 13878 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13879 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13880 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 13881 | ctxt->dictNames = 1; |
| 13882 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13883 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13884 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13885 | if (doc != NULL) { |
| 13886 | newDoc->intSubset = doc->intSubset; |
| 13887 | newDoc->extSubset = doc->extSubset; |
| 13888 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13889 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13890 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13891 | if (sax != NULL) |
| 13892 | ctxt->sax = oldsax; |
| 13893 | xmlFreeParserCtxt(ctxt); |
| 13894 | newDoc->intSubset = NULL; |
| 13895 | newDoc->extSubset = NULL; |
| 13896 | xmlFreeDoc(newDoc); |
| 13897 | return(-1); |
| 13898 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13899 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 13900 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13901 | if (doc == NULL) { |
| 13902 | ctxt->myDoc = newDoc; |
| 13903 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 13904 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13905 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13906 | /* Ensure that doc has XML spec namespace */ |
| 13907 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 13908 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13909 | } |
| 13910 | ctxt->instate = XML_PARSER_CONTENT; |
| 13911 | ctxt->depth = depth; |
| 13912 | |
| 13913 | /* |
| 13914 | * Doing validity checking on chunk doesn't make sense |
| 13915 | */ |
| 13916 | ctxt->validate = 0; |
| 13917 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13918 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13919 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 13920 | if ( doc != NULL ){ |
| 13921 | content = doc->children; |
| 13922 | doc->children = NULL; |
| 13923 | xmlParseContent(ctxt); |
| 13924 | doc->children = content; |
| 13925 | } |
| 13926 | else { |
| 13927 | xmlParseContent(ctxt); |
| 13928 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13929 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13930 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13931 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13932 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13933 | } |
| 13934 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13935 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13936 | } |
| 13937 | |
| 13938 | if (!ctxt->wellFormed) { |
| 13939 | if (ctxt->errNo == 0) |
| 13940 | ret = 1; |
| 13941 | else |
| 13942 | ret = ctxt->errNo; |
| 13943 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13944 | ret = 0; |
| 13945 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13946 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13947 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 13948 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13949 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13950 | /* |
| 13951 | * Return the newly created nodeset after unlinking it from |
| 13952 | * they pseudo parent. |
| 13953 | */ |
| 13954 | cur = newDoc->children->children; |
| 13955 | *lst = cur; |
| 13956 | while (cur != NULL) { |
| 13957 | xmlSetTreeDoc(cur, doc); |
| 13958 | cur->parent = NULL; |
| 13959 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13960 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13961 | newDoc->children->children = NULL; |
| 13962 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13963 | |
| 13964 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13965 | ctxt->sax = oldsax; |
| 13966 | xmlFreeParserCtxt(ctxt); |
| 13967 | newDoc->intSubset = NULL; |
| 13968 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13969 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13970 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13971 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13972 | return(ret); |
| 13973 | } |
| 13974 | |
| 13975 | /** |
| 13976 | * xmlSAXParseEntity: |
| 13977 | * @sax: the SAX handler block |
| 13978 | * @filename: the filename |
| 13979 | * |
| 13980 | * parse an XML external entity out of context and build a tree. |
| 13981 | * It use the given SAX function block to handle the parsing callback. |
| 13982 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13983 | * |
| 13984 | * [78] extParsedEnt ::= TextDecl? content |
| 13985 | * |
| 13986 | * This correspond to a "Well Balanced" chunk |
| 13987 | * |
| 13988 | * Returns the resulting document tree |
| 13989 | */ |
| 13990 | |
| 13991 | xmlDocPtr |
| 13992 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13993 | xmlDocPtr ret; |
| 13994 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13995 | |
| 13996 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13997 | if (ctxt == NULL) { |
| 13998 | return(NULL); |
| 13999 | } |
| 14000 | if (sax != NULL) { |
| 14001 | if (ctxt->sax != NULL) |
| 14002 | xmlFree(ctxt->sax); |
| 14003 | ctxt->sax = sax; |
| 14004 | ctxt->userData = NULL; |
| 14005 | } |
| 14006 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14007 | xmlParseExtParsedEnt(ctxt); |
| 14008 | |
| 14009 | if (ctxt->wellFormed) |
| 14010 | ret = ctxt->myDoc; |
| 14011 | else { |
| 14012 | ret = NULL; |
| 14013 | xmlFreeDoc(ctxt->myDoc); |
| 14014 | ctxt->myDoc = NULL; |
| 14015 | } |
| 14016 | if (sax != NULL) |
| 14017 | ctxt->sax = NULL; |
| 14018 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14019 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14020 | return(ret); |
| 14021 | } |
| 14022 | |
| 14023 | /** |
| 14024 | * xmlParseEntity: |
| 14025 | * @filename: the filename |
| 14026 | * |
| 14027 | * parse an XML external entity out of context and build a tree. |
| 14028 | * |
| 14029 | * [78] extParsedEnt ::= TextDecl? content |
| 14030 | * |
| 14031 | * This correspond to a "Well Balanced" chunk |
| 14032 | * |
| 14033 | * Returns the resulting document tree |
| 14034 | */ |
| 14035 | |
| 14036 | xmlDocPtr |
| 14037 | xmlParseEntity(const char *filename) { |
| 14038 | return(xmlSAXParseEntity(NULL, filename)); |
| 14039 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14040 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14041 | |
| 14042 | /** |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14043 | * xmlCreateEntityParserCtxtInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14044 | * @URL: the entity URL |
| 14045 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14046 | * @base: a possible base for the target URI |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14047 | * @pctx: parser context used to set options on new context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14048 | * |
| 14049 | * Create a parser context for an external entity |
| 14050 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14051 | * by default if found at compile-time. |
| 14052 | * |
| 14053 | * Returns the new parser context or NULL |
| 14054 | */ |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14055 | static xmlParserCtxtPtr |
| 14056 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 14057 | const xmlChar *base, xmlParserCtxtPtr pctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14058 | xmlParserCtxtPtr ctxt; |
| 14059 | xmlParserInputPtr inputStream; |
| 14060 | char *directory = NULL; |
| 14061 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14062 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14063 | ctxt = xmlNewParserCtxt(); |
| 14064 | if (ctxt == NULL) { |
| 14065 | return(NULL); |
| 14066 | } |
| 14067 | |
Daniel Veillard | 48247b4 | 2009-07-10 16:12:46 +0200 | [diff] [blame] | 14068 | if (pctx != NULL) { |
| 14069 | ctxt->options = pctx->options; |
| 14070 | ctxt->_private = pctx->_private; |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14071 | } |
| 14072 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14073 | uri = xmlBuildURI(URL, base); |
| 14074 | |
| 14075 | if (uri == NULL) { |
| 14076 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 14077 | if (inputStream == NULL) { |
| 14078 | xmlFreeParserCtxt(ctxt); |
| 14079 | return(NULL); |
| 14080 | } |
| 14081 | |
| 14082 | inputPush(ctxt, inputStream); |
| 14083 | |
| 14084 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14085 | directory = xmlParserGetDirectory((char *)URL); |
| 14086 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14087 | ctxt->directory = directory; |
| 14088 | } else { |
| 14089 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 14090 | if (inputStream == NULL) { |
| 14091 | xmlFree(uri); |
| 14092 | xmlFreeParserCtxt(ctxt); |
| 14093 | return(NULL); |
| 14094 | } |
| 14095 | |
| 14096 | inputPush(ctxt, inputStream); |
| 14097 | |
| 14098 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14099 | directory = xmlParserGetDirectory((char *)uri); |
| 14100 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14101 | ctxt->directory = directory; |
| 14102 | xmlFree(uri); |
| 14103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14104 | return(ctxt); |
| 14105 | } |
| 14106 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14107 | /** |
| 14108 | * xmlCreateEntityParserCtxt: |
| 14109 | * @URL: the entity URL |
| 14110 | * @ID: the entity PUBLIC ID |
| 14111 | * @base: a possible base for the target URI |
| 14112 | * |
| 14113 | * Create a parser context for an external entity |
| 14114 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14115 | * by default if found at compile-time. |
| 14116 | * |
| 14117 | * Returns the new parser context or NULL |
| 14118 | */ |
| 14119 | xmlParserCtxtPtr |
| 14120 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 14121 | const xmlChar *base) { |
| 14122 | return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); |
| 14123 | |
| 14124 | } |
| 14125 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14126 | /************************************************************************ |
| 14127 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14128 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14129 | * * |
| 14130 | ************************************************************************/ |
| 14131 | |
| 14132 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14133 | * xmlCreateURLParserCtxt: |
| 14134 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14135 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14136 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14137 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14138 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14139 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14140 | * |
| 14141 | * Returns the new parser context or NULL |
| 14142 | */ |
| 14143 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14144 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14145 | { |
| 14146 | xmlParserCtxtPtr ctxt; |
| 14147 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14148 | char *directory = NULL; |
| 14149 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14150 | ctxt = xmlNewParserCtxt(); |
| 14151 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14152 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14153 | return(NULL); |
| 14154 | } |
| 14155 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14156 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14157 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14158 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14159 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 14160 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14161 | if (inputStream == NULL) { |
| 14162 | xmlFreeParserCtxt(ctxt); |
| 14163 | return(NULL); |
| 14164 | } |
| 14165 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14166 | inputPush(ctxt, inputStream); |
| 14167 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 14168 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14169 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14170 | ctxt->directory = directory; |
| 14171 | |
| 14172 | return(ctxt); |
| 14173 | } |
| 14174 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14175 | /** |
| 14176 | * xmlCreateFileParserCtxt: |
| 14177 | * @filename: the filename |
| 14178 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14179 | * Create a parser context for a file content. |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14180 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14181 | * by default if found at compile-time. |
| 14182 | * |
| 14183 | * Returns the new parser context or NULL |
| 14184 | */ |
| 14185 | xmlParserCtxtPtr |
| 14186 | xmlCreateFileParserCtxt(const char *filename) |
| 14187 | { |
| 14188 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 14189 | } |
| 14190 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14191 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14192 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14193 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14194 | * @sax: the SAX handler block |
| 14195 | * @filename: the filename |
| 14196 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14197 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14198 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14199 | * |
| 14200 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14201 | * compressed document is provided by default if found at compile-time. |
| 14202 | * It use the given SAX function block to handle the parsing callback. |
| 14203 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14204 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 14205 | * User data (void *) is stored within the parser context in the |
| 14206 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14207 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14208 | * Returns the resulting document tree |
| 14209 | */ |
| 14210 | |
| 14211 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14212 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 14213 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14214 | xmlDocPtr ret; |
| 14215 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14216 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 14217 | xmlInitParser(); |
| 14218 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14219 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14220 | if (ctxt == NULL) { |
| 14221 | return(NULL); |
| 14222 | } |
| 14223 | if (sax != NULL) { |
| 14224 | if (ctxt->sax != NULL) |
| 14225 | xmlFree(ctxt->sax); |
| 14226 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14227 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14228 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14229 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 14230 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14231 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14232 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 14233 | if (ctxt->directory == NULL) |
| 14234 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14235 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 14236 | ctxt->recovery = recovery; |
| 14237 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14238 | xmlParseDocument(ctxt); |
| 14239 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14240 | if ((ctxt->wellFormed) || recovery) { |
| 14241 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 14242 | if (ret != NULL) { |
| 14243 | if (ctxt->input->buf->compressed > 0) |
| 14244 | ret->compression = 9; |
| 14245 | else |
| 14246 | ret->compression = ctxt->input->buf->compressed; |
| 14247 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14249 | else { |
| 14250 | ret = NULL; |
| 14251 | xmlFreeDoc(ctxt->myDoc); |
| 14252 | ctxt->myDoc = NULL; |
| 14253 | } |
| 14254 | if (sax != NULL) |
| 14255 | ctxt->sax = NULL; |
| 14256 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14258 | return(ret); |
| 14259 | } |
| 14260 | |
| 14261 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14262 | * xmlSAXParseFile: |
| 14263 | * @sax: the SAX handler block |
| 14264 | * @filename: the filename |
| 14265 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14266 | * documents |
| 14267 | * |
| 14268 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14269 | * compressed document is provided by default if found at compile-time. |
| 14270 | * It use the given SAX function block to handle the parsing callback. |
| 14271 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14272 | * |
| 14273 | * Returns the resulting document tree |
| 14274 | */ |
| 14275 | |
| 14276 | xmlDocPtr |
| 14277 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 14278 | int recovery) { |
| 14279 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 14280 | } |
| 14281 | |
| 14282 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14283 | * xmlRecoverDoc: |
| 14284 | * @cur: a pointer to an array of xmlChar |
| 14285 | * |
| 14286 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14287 | * In the case the document is not Well Formed, a attempt to build a |
| 14288 | * tree is tried anyway |
| 14289 | * |
| 14290 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14291 | */ |
| 14292 | |
| 14293 | xmlDocPtr |
Daniel Veillard | f39eafa | 2009-08-20 19:15:08 +0200 | [diff] [blame] | 14294 | xmlRecoverDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14295 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 14296 | } |
| 14297 | |
| 14298 | /** |
| 14299 | * xmlParseFile: |
| 14300 | * @filename: the filename |
| 14301 | * |
| 14302 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14303 | * compressed document is provided by default if found at compile-time. |
| 14304 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 14305 | * Returns the resulting document tree if the file was wellformed, |
| 14306 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14307 | */ |
| 14308 | |
| 14309 | xmlDocPtr |
| 14310 | xmlParseFile(const char *filename) { |
| 14311 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 14312 | } |
| 14313 | |
| 14314 | /** |
| 14315 | * xmlRecoverFile: |
| 14316 | * @filename: the filename |
| 14317 | * |
| 14318 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14319 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14320 | * In the case the document is not Well Formed, it attempts to build |
| 14321 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14322 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14323 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14324 | */ |
| 14325 | |
| 14326 | xmlDocPtr |
| 14327 | xmlRecoverFile(const char *filename) { |
| 14328 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 14329 | } |
| 14330 | |
| 14331 | |
| 14332 | /** |
| 14333 | * xmlSetupParserForBuffer: |
| 14334 | * @ctxt: an XML parser context |
| 14335 | * @buffer: a xmlChar * buffer |
| 14336 | * @filename: a file name |
| 14337 | * |
| 14338 | * Setup the parser context to parse a new buffer; Clears any prior |
| 14339 | * contents from the parser context. The buffer parameter must not be |
| 14340 | * NULL, but the filename parameter can be |
| 14341 | */ |
| 14342 | void |
| 14343 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 14344 | const char* filename) |
| 14345 | { |
| 14346 | xmlParserInputPtr input; |
| 14347 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14348 | if ((ctxt == NULL) || (buffer == NULL)) |
| 14349 | return; |
| 14350 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14351 | input = xmlNewInputStream(ctxt); |
| 14352 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 14353 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14354 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14355 | return; |
| 14356 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14357 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14358 | xmlClearParserCtxt(ctxt); |
| 14359 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 14360 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14361 | input->base = buffer; |
| 14362 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 14363 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14364 | inputPush(ctxt, input); |
| 14365 | } |
| 14366 | |
| 14367 | /** |
| 14368 | * xmlSAXUserParseFile: |
| 14369 | * @sax: a SAX handler |
| 14370 | * @user_data: The user data returned on SAX callbacks |
| 14371 | * @filename: a file name |
| 14372 | * |
| 14373 | * parse an XML file and call the given SAX handler routines. |
| 14374 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14375 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14376 | * Returns 0 in case of success or a error number otherwise |
| 14377 | */ |
| 14378 | int |
| 14379 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 14380 | const char *filename) { |
| 14381 | int ret = 0; |
| 14382 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14383 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14384 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14385 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 14386 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14387 | xmlFree(ctxt->sax); |
| 14388 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14389 | xmlDetectSAX2(ctxt); |
| 14390 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14391 | if (user_data != NULL) |
| 14392 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14393 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14394 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14395 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14396 | if (ctxt->wellFormed) |
| 14397 | ret = 0; |
| 14398 | else { |
| 14399 | if (ctxt->errNo != 0) |
| 14400 | ret = ctxt->errNo; |
| 14401 | else |
| 14402 | ret = -1; |
| 14403 | } |
| 14404 | if (sax != NULL) |
| 14405 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14406 | if (ctxt->myDoc != NULL) { |
| 14407 | xmlFreeDoc(ctxt->myDoc); |
| 14408 | ctxt->myDoc = NULL; |
| 14409 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14410 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14411 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14412 | return ret; |
| 14413 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14414 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14415 | |
| 14416 | /************************************************************************ |
| 14417 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14418 | * Front ends when parsing from memory * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14419 | * * |
| 14420 | ************************************************************************/ |
| 14421 | |
| 14422 | /** |
| 14423 | * xmlCreateMemoryParserCtxt: |
| 14424 | * @buffer: a pointer to a char array |
| 14425 | * @size: the size of the array |
| 14426 | * |
| 14427 | * Create a parser context for an XML in-memory document. |
| 14428 | * |
| 14429 | * Returns the new parser context or NULL |
| 14430 | */ |
| 14431 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14432 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14433 | xmlParserCtxtPtr ctxt; |
| 14434 | xmlParserInputPtr input; |
| 14435 | xmlParserInputBufferPtr buf; |
| 14436 | |
| 14437 | if (buffer == NULL) |
| 14438 | return(NULL); |
| 14439 | if (size <= 0) |
| 14440 | return(NULL); |
| 14441 | |
| 14442 | ctxt = xmlNewParserCtxt(); |
| 14443 | if (ctxt == NULL) |
| 14444 | return(NULL); |
| 14445 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 14446 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14447 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14448 | if (buf == NULL) { |
| 14449 | xmlFreeParserCtxt(ctxt); |
| 14450 | return(NULL); |
| 14451 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14452 | |
| 14453 | input = xmlNewInputStream(ctxt); |
| 14454 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14455 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14456 | xmlFreeParserCtxt(ctxt); |
| 14457 | return(NULL); |
| 14458 | } |
| 14459 | |
| 14460 | input->filename = NULL; |
| 14461 | input->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14462 | xmlBufResetInput(input->buf->buffer, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14463 | |
| 14464 | inputPush(ctxt, input); |
| 14465 | return(ctxt); |
| 14466 | } |
| 14467 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14468 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14469 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14470 | * xmlSAXParseMemoryWithData: |
| 14471 | * @sax: the SAX handler block |
| 14472 | * @buffer: an pointer to a char array |
| 14473 | * @size: the size of the array |
| 14474 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14475 | * documents |
| 14476 | * @data: the userdata |
| 14477 | * |
| 14478 | * parse an XML in-memory block and use the given SAX function block |
| 14479 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14480 | * DOM tree building routines. |
| 14481 | * |
| 14482 | * User data (void *) is stored within the parser context in the |
| 14483 | * context's _private member, so it is available nearly everywhere in libxml |
| 14484 | * |
| 14485 | * Returns the resulting document tree |
| 14486 | */ |
| 14487 | |
| 14488 | xmlDocPtr |
| 14489 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 14490 | int size, int recovery, void *data) { |
| 14491 | xmlDocPtr ret; |
| 14492 | xmlParserCtxtPtr ctxt; |
| 14493 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14494 | xmlInitParser(); |
| 14495 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14496 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14497 | if (ctxt == NULL) return(NULL); |
| 14498 | if (sax != NULL) { |
| 14499 | if (ctxt->sax != NULL) |
| 14500 | xmlFree(ctxt->sax); |
| 14501 | ctxt->sax = sax; |
| 14502 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14503 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14504 | if (data!=NULL) { |
| 14505 | ctxt->_private=data; |
| 14506 | } |
| 14507 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 14508 | ctxt->recovery = recovery; |
| 14509 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14510 | xmlParseDocument(ctxt); |
| 14511 | |
| 14512 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14513 | else { |
| 14514 | ret = NULL; |
| 14515 | xmlFreeDoc(ctxt->myDoc); |
| 14516 | ctxt->myDoc = NULL; |
| 14517 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14518 | if (sax != NULL) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14519 | ctxt->sax = NULL; |
| 14520 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14521 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14522 | return(ret); |
| 14523 | } |
| 14524 | |
| 14525 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14526 | * xmlSAXParseMemory: |
| 14527 | * @sax: the SAX handler block |
| 14528 | * @buffer: an pointer to a char array |
| 14529 | * @size: the size of the array |
| 14530 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 14531 | * documents |
| 14532 | * |
| 14533 | * parse an XML in-memory block and use the given SAX function block |
| 14534 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14535 | * DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14536 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14537 | * Returns the resulting document tree |
| 14538 | */ |
| 14539 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14540 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 14541 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14542 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14543 | } |
| 14544 | |
| 14545 | /** |
| 14546 | * xmlParseMemory: |
| 14547 | * @buffer: an pointer to a char array |
| 14548 | * @size: the size of the array |
| 14549 | * |
| 14550 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14551 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14552 | * Returns the resulting document tree |
| 14553 | */ |
| 14554 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14555 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14556 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 14557 | } |
| 14558 | |
| 14559 | /** |
| 14560 | * xmlRecoverMemory: |
| 14561 | * @buffer: an pointer to a char array |
| 14562 | * @size: the size of the array |
| 14563 | * |
| 14564 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14565 | * In the case the document is not Well Formed, an attempt to |
| 14566 | * build a tree is tried anyway |
| 14567 | * |
| 14568 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14569 | */ |
| 14570 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14571 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14572 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 14573 | } |
| 14574 | |
| 14575 | /** |
| 14576 | * xmlSAXUserParseMemory: |
| 14577 | * @sax: a SAX handler |
| 14578 | * @user_data: The user data returned on SAX callbacks |
| 14579 | * @buffer: an in-memory XML document input |
| 14580 | * @size: the length of the XML document in bytes |
| 14581 | * |
| 14582 | * A better SAX parsing routine. |
| 14583 | * 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] | 14584 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14585 | * Returns 0 in case of success or a error number otherwise |
| 14586 | */ |
| 14587 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14588 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14589 | int ret = 0; |
| 14590 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14591 | |
| 14592 | xmlInitParser(); |
| 14593 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14594 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14595 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14596 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 14597 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 14598 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14599 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14600 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 14601 | if (user_data != NULL) |
| 14602 | ctxt->userData = user_data; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14604 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14605 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14606 | if (ctxt->wellFormed) |
| 14607 | ret = 0; |
| 14608 | else { |
| 14609 | if (ctxt->errNo != 0) |
| 14610 | ret = ctxt->errNo; |
| 14611 | else |
| 14612 | ret = -1; |
| 14613 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14614 | if (sax != NULL) |
| 14615 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14616 | if (ctxt->myDoc != NULL) { |
| 14617 | xmlFreeDoc(ctxt->myDoc); |
| 14618 | ctxt->myDoc = NULL; |
| 14619 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14620 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14621 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14622 | return ret; |
| 14623 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14624 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14625 | |
| 14626 | /** |
| 14627 | * xmlCreateDocParserCtxt: |
| 14628 | * @cur: a pointer to an array of xmlChar |
| 14629 | * |
| 14630 | * Creates a parser context for an XML in-memory document. |
| 14631 | * |
| 14632 | * Returns the new parser context or NULL |
| 14633 | */ |
| 14634 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14635 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14636 | int len; |
| 14637 | |
| 14638 | if (cur == NULL) |
| 14639 | return(NULL); |
| 14640 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14641 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14642 | } |
| 14643 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14644 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14645 | /** |
| 14646 | * xmlSAXParseDoc: |
| 14647 | * @sax: the SAX handler block |
| 14648 | * @cur: a pointer to an array of xmlChar |
| 14649 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14650 | * documents |
| 14651 | * |
| 14652 | * parse an XML in-memory document and build a tree. |
| 14653 | * It use the given SAX function block to handle the parsing callback. |
| 14654 | * If sax is NULL, fallback to the default DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14655 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14656 | * Returns the resulting document tree |
| 14657 | */ |
| 14658 | |
| 14659 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14660 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14661 | xmlDocPtr ret; |
| 14662 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14663 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14664 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14665 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14666 | |
| 14667 | |
| 14668 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14669 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14670 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14671 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14672 | ctxt->sax = sax; |
| 14673 | ctxt->userData = NULL; |
| 14674 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14675 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14676 | |
| 14677 | xmlParseDocument(ctxt); |
| 14678 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14679 | else { |
| 14680 | ret = NULL; |
| 14681 | xmlFreeDoc(ctxt->myDoc); |
| 14682 | ctxt->myDoc = NULL; |
| 14683 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14684 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14685 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14686 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14687 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14688 | return(ret); |
| 14689 | } |
| 14690 | |
| 14691 | /** |
| 14692 | * xmlParseDoc: |
| 14693 | * @cur: a pointer to an array of xmlChar |
| 14694 | * |
| 14695 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14696 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14697 | * Returns the resulting document tree |
| 14698 | */ |
| 14699 | |
| 14700 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14701 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14702 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 14703 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14704 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14705 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14706 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14707 | /************************************************************************ |
| 14708 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14709 | * Specific function to keep track of entities references * |
| 14710 | * and used by the XSLT debugger * |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14711 | * * |
| 14712 | ************************************************************************/ |
| 14713 | |
| 14714 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 14715 | |
| 14716 | /** |
| 14717 | * xmlAddEntityReference: |
| 14718 | * @ent : A valid entity |
| 14719 | * @firstNode : A valid first node for children of entity |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14720 | * @lastNode : A valid last node of children entity |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14721 | * |
| 14722 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 14723 | */ |
| 14724 | static void |
| 14725 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 14726 | xmlNodePtr lastNode) |
| 14727 | { |
| 14728 | if (xmlEntityRefFunc != NULL) { |
| 14729 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 14730 | } |
| 14731 | } |
| 14732 | |
| 14733 | |
| 14734 | /** |
| 14735 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 14736 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14737 | * |
| 14738 | * Set the function to call call back when a xml reference has been made |
| 14739 | */ |
| 14740 | void |
| 14741 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 14742 | { |
| 14743 | xmlEntityRefFunc = func; |
| 14744 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14745 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14746 | |
| 14747 | /************************************************************************ |
| 14748 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14749 | * Miscellaneous * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14750 | * * |
| 14751 | ************************************************************************/ |
| 14752 | |
| 14753 | #ifdef LIBXML_XPATH_ENABLED |
| 14754 | #include <libxml/xpath.h> |
| 14755 | #endif |
| 14756 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 14757 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14758 | static int xmlParserInitialized = 0; |
| 14759 | |
| 14760 | /** |
| 14761 | * xmlInitParser: |
| 14762 | * |
| 14763 | * Initialization function for the XML parser. |
| 14764 | * This is not reentrant. Call once before processing in case of |
| 14765 | * use in multithreaded programs. |
| 14766 | */ |
| 14767 | |
| 14768 | void |
| 14769 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 14770 | if (xmlParserInitialized != 0) |
| 14771 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14772 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14773 | #ifdef LIBXML_THREAD_ENABLED |
| 14774 | __xmlGlobalInitMutexLock(); |
| 14775 | if (xmlParserInitialized == 0) { |
| 14776 | #endif |
Daniel Veillard | 7dd7080 | 2009-06-04 11:08:39 +0200 | [diff] [blame] | 14777 | xmlInitThreads(); |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 14778 | xmlInitGlobals(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14779 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 14780 | (xmlGenericError == NULL)) |
| 14781 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14782 | xmlInitMemory(); |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 14783 | xmlInitializeDict(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14784 | xmlInitCharEncodingHandlers(); |
| 14785 | xmlDefaultSAXHandlerInit(); |
| 14786 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14787 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14788 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14789 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14790 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14791 | htmlInitAutoClose(); |
| 14792 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14793 | #endif |
| 14794 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14795 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14796 | #endif |
Daniel Veillard | 054c716 | 2014-01-26 15:02:25 +0100 | [diff] [blame] | 14797 | #ifdef LIBXML_CATALOG_ENABLED |
| 14798 | xmlInitializeCatalog(); |
| 14799 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14800 | xmlParserInitialized = 1; |
| 14801 | #ifdef LIBXML_THREAD_ENABLED |
| 14802 | } |
| 14803 | __xmlGlobalInitMutexUnlock(); |
| 14804 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14805 | } |
| 14806 | |
| 14807 | /** |
| 14808 | * xmlCleanupParser: |
| 14809 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 14810 | * This function name is somewhat misleading. It does not clean up |
| 14811 | * parser state, it cleans up memory allocated by the library itself. |
| 14812 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 14813 | * related global memory allocated for the library processing. |
| 14814 | * It doesn't deallocate any document related memory. One should |
| 14815 | * call xmlCleanupParser() only when the process has finished using |
| 14816 | * the library and all XML/HTML documents built with it. |
| 14817 | * See also xmlInitParser() which has the opposite function of preparing |
| 14818 | * the library for operations. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 14819 | * |
| 14820 | * WARNING: if your application is multithreaded or has plugin support |
| 14821 | * calling this may crash the application if another thread or |
| 14822 | * a plugin is still using libxml2. It's sometimes very hard to |
| 14823 | * guess if libxml2 is in use in the application, some libraries |
| 14824 | * or plugins may use it without notice. In case of doubt abstain |
| 14825 | * from calling this function or do it just before calling exit() |
| 14826 | * to avoid leak reports from valgrind ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14827 | */ |
| 14828 | |
| 14829 | void |
| 14830 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 14831 | if (!xmlParserInitialized) |
| 14832 | return; |
| 14833 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14834 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 14835 | #ifdef LIBXML_CATALOG_ENABLED |
| 14836 | xmlCatalogCleanup(); |
| 14837 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 14838 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 14839 | xmlCleanupInputCallbacks(); |
| 14840 | #ifdef LIBXML_OUTPUT_ENABLED |
| 14841 | xmlCleanupOutputCallbacks(); |
| 14842 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14843 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14844 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 14845 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14846 | #endif |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 14847 | xmlResetLastError(); |
Alexander Pastukhov | 704d8c5 | 2013-04-23 13:02:11 +0800 | [diff] [blame] | 14848 | xmlCleanupGlobals(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 14849 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14850 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 14851 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14852 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14853 | |
| 14854 | /************************************************************************ |
| 14855 | * * |
| 14856 | * New set (2.6.0) of simpler and more flexible APIs * |
| 14857 | * * |
| 14858 | ************************************************************************/ |
| 14859 | |
| 14860 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14861 | * DICT_FREE: |
| 14862 | * @str: a string |
| 14863 | * |
| 14864 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 14865 | * current scope |
| 14866 | */ |
| 14867 | #define DICT_FREE(str) \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14868 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14869 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 14870 | xmlFree((char *)(str)); |
| 14871 | |
| 14872 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14873 | * xmlCtxtReset: |
| 14874 | * @ctxt: an XML parser context |
| 14875 | * |
| 14876 | * Reset a parser context |
| 14877 | */ |
| 14878 | void |
| 14879 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 14880 | { |
| 14881 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14882 | xmlDictPtr dict; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14883 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14884 | if (ctxt == NULL) |
| 14885 | return; |
| 14886 | |
| 14887 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14888 | |
| 14889 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 14890 | xmlFreeInputStream(input); |
| 14891 | } |
| 14892 | ctxt->inputNr = 0; |
| 14893 | ctxt->input = NULL; |
| 14894 | |
| 14895 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 14896 | if (ctxt->spaceTab != NULL) { |
| 14897 | ctxt->spaceTab[0] = -1; |
| 14898 | ctxt->space = &ctxt->spaceTab[0]; |
| 14899 | } else { |
| 14900 | ctxt->space = NULL; |
| 14901 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14902 | |
| 14903 | |
| 14904 | ctxt->nodeNr = 0; |
| 14905 | ctxt->node = NULL; |
| 14906 | |
| 14907 | ctxt->nameNr = 0; |
| 14908 | ctxt->name = NULL; |
| 14909 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14910 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14911 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14912 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14913 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14914 | DICT_FREE(ctxt->directory); |
| 14915 | ctxt->directory = NULL; |
| 14916 | DICT_FREE(ctxt->extSubURI); |
| 14917 | ctxt->extSubURI = NULL; |
| 14918 | DICT_FREE(ctxt->extSubSystem); |
| 14919 | ctxt->extSubSystem = NULL; |
| 14920 | if (ctxt->myDoc != NULL) |
| 14921 | xmlFreeDoc(ctxt->myDoc); |
| 14922 | ctxt->myDoc = NULL; |
| 14923 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14924 | ctxt->standalone = -1; |
| 14925 | ctxt->hasExternalSubset = 0; |
| 14926 | ctxt->hasPErefs = 0; |
| 14927 | ctxt->html = 0; |
| 14928 | ctxt->external = 0; |
| 14929 | ctxt->instate = XML_PARSER_START; |
| 14930 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14931 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14932 | ctxt->wellFormed = 1; |
| 14933 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 14934 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14935 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14936 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14937 | ctxt->vctxt.userData = ctxt; |
| 14938 | ctxt->vctxt.error = xmlParserValidityError; |
| 14939 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14940 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14941 | ctxt->record_info = 0; |
| 14942 | ctxt->nbChars = 0; |
| 14943 | ctxt->checkIndex = 0; |
| 14944 | ctxt->inSubset = 0; |
| 14945 | ctxt->errNo = XML_ERR_OK; |
| 14946 | ctxt->depth = 0; |
| 14947 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 14948 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 14949 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14950 | ctxt->sizeentities = 0; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 14951 | ctxt->sizeentcopy = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14952 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 14953 | |
| 14954 | if (ctxt->attsDefault != NULL) { |
| 14955 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 14956 | ctxt->attsDefault = NULL; |
| 14957 | } |
| 14958 | if (ctxt->attsSpecial != NULL) { |
| 14959 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 14960 | ctxt->attsSpecial = NULL; |
| 14961 | } |
| 14962 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14963 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14964 | if (ctxt->catalogs != NULL) |
| 14965 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14966 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 14967 | if (ctxt->lastError.code != XML_ERR_OK) |
| 14968 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14969 | } |
| 14970 | |
| 14971 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14972 | * xmlCtxtResetPush: |
| 14973 | * @ctxt: an XML parser context |
| 14974 | * @chunk: a pointer to an array of chars |
| 14975 | * @size: number of chars in the array |
| 14976 | * @filename: an optional file name or URI |
| 14977 | * @encoding: the document encoding, or NULL |
| 14978 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14979 | * Reset a push parser context |
| 14980 | * |
| 14981 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14982 | */ |
| 14983 | int |
| 14984 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 14985 | int size, const char *filename, const char *encoding) |
| 14986 | { |
| 14987 | xmlParserInputPtr inputStream; |
| 14988 | xmlParserInputBufferPtr buf; |
| 14989 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 14990 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14991 | if (ctxt == NULL) |
| 14992 | return(1); |
| 14993 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14994 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 14995 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 14996 | |
| 14997 | buf = xmlAllocParserInputBuffer(enc); |
| 14998 | if (buf == NULL) |
| 14999 | return(1); |
| 15000 | |
| 15001 | if (ctxt == NULL) { |
| 15002 | xmlFreeParserInputBuffer(buf); |
| 15003 | return(1); |
| 15004 | } |
| 15005 | |
| 15006 | xmlCtxtReset(ctxt); |
| 15007 | |
| 15008 | if (ctxt->pushTab == NULL) { |
| 15009 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 15010 | sizeof(xmlChar *)); |
| 15011 | if (ctxt->pushTab == NULL) { |
| 15012 | xmlErrMemory(ctxt, NULL); |
| 15013 | xmlFreeParserInputBuffer(buf); |
| 15014 | return(1); |
| 15015 | } |
| 15016 | } |
| 15017 | |
| 15018 | if (filename == NULL) { |
| 15019 | ctxt->directory = NULL; |
| 15020 | } else { |
| 15021 | ctxt->directory = xmlParserGetDirectory(filename); |
| 15022 | } |
| 15023 | |
| 15024 | inputStream = xmlNewInputStream(ctxt); |
| 15025 | if (inputStream == NULL) { |
| 15026 | xmlFreeParserInputBuffer(buf); |
| 15027 | return(1); |
| 15028 | } |
| 15029 | |
| 15030 | if (filename == NULL) |
| 15031 | inputStream->filename = NULL; |
| 15032 | else |
| 15033 | inputStream->filename = (char *) |
| 15034 | xmlCanonicPath((const xmlChar *) filename); |
| 15035 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 15036 | xmlBufResetInput(buf->buffer, inputStream); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15037 | |
| 15038 | inputPush(ctxt, inputStream); |
| 15039 | |
| 15040 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 15041 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15042 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 15043 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15044 | |
| 15045 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 15046 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15047 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15048 | #ifdef DEBUG_PUSH |
| 15049 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 15050 | #endif |
| 15051 | } |
| 15052 | |
| 15053 | if (encoding != NULL) { |
| 15054 | xmlCharEncodingHandlerPtr hdlr; |
| 15055 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15056 | if (ctxt->encoding != NULL) |
| 15057 | xmlFree((xmlChar *) ctxt->encoding); |
| 15058 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15059 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15060 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15061 | if (hdlr != NULL) { |
| 15062 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15063 | } else { |
| 15064 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 15065 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 15066 | } |
| 15067 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 15068 | xmlSwitchEncoding(ctxt, enc); |
| 15069 | } |
| 15070 | |
| 15071 | return(0); |
| 15072 | } |
| 15073 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15074 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15075 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15076 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15077 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15078 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15079 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15080 | * |
| 15081 | * Applies the options to the parser context |
| 15082 | * |
| 15083 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15084 | * in case of error. |
| 15085 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15086 | static int |
| 15087 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15088 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 15089 | if (ctxt == NULL) |
| 15090 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15091 | if (encoding != NULL) { |
| 15092 | if (ctxt->encoding != NULL) |
| 15093 | xmlFree((xmlChar *) ctxt->encoding); |
| 15094 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15095 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15096 | if (options & XML_PARSE_RECOVER) { |
| 15097 | ctxt->recovery = 1; |
| 15098 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15099 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15100 | } else |
| 15101 | ctxt->recovery = 0; |
| 15102 | if (options & XML_PARSE_DTDLOAD) { |
| 15103 | ctxt->loadsubset = XML_DETECT_IDS; |
| 15104 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15105 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15106 | } else |
| 15107 | ctxt->loadsubset = 0; |
| 15108 | if (options & XML_PARSE_DTDATTR) { |
| 15109 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 15110 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15111 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15112 | } |
| 15113 | if (options & XML_PARSE_NOENT) { |
| 15114 | ctxt->replaceEntities = 1; |
| 15115 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 15116 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15117 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15118 | } else |
| 15119 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15120 | if (options & XML_PARSE_PEDANTIC) { |
| 15121 | ctxt->pedantic = 1; |
| 15122 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15123 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15124 | } else |
| 15125 | ctxt->pedantic = 0; |
| 15126 | if (options & XML_PARSE_NOBLANKS) { |
| 15127 | ctxt->keepBlanks = 0; |
| 15128 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 15129 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15130 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15131 | } else |
| 15132 | ctxt->keepBlanks = 1; |
| 15133 | if (options & XML_PARSE_DTDVALID) { |
| 15134 | ctxt->validate = 1; |
| 15135 | if (options & XML_PARSE_NOWARNING) |
| 15136 | ctxt->vctxt.warning = NULL; |
| 15137 | if (options & XML_PARSE_NOERROR) |
| 15138 | ctxt->vctxt.error = NULL; |
| 15139 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15140 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15141 | } else |
| 15142 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 15143 | if (options & XML_PARSE_NOWARNING) { |
| 15144 | ctxt->sax->warning = NULL; |
| 15145 | options -= XML_PARSE_NOWARNING; |
| 15146 | } |
| 15147 | if (options & XML_PARSE_NOERROR) { |
| 15148 | ctxt->sax->error = NULL; |
| 15149 | ctxt->sax->fatalError = NULL; |
| 15150 | options -= XML_PARSE_NOERROR; |
| 15151 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15152 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15153 | if (options & XML_PARSE_SAX1) { |
| 15154 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 15155 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 15156 | ctxt->sax->startElementNs = NULL; |
| 15157 | ctxt->sax->endElementNs = NULL; |
| 15158 | ctxt->sax->initialized = 1; |
| 15159 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15160 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15161 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15162 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15163 | if (options & XML_PARSE_NODICT) { |
| 15164 | ctxt->dictNames = 0; |
| 15165 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15166 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15167 | } else { |
| 15168 | ctxt->dictNames = 1; |
| 15169 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15170 | if (options & XML_PARSE_NOCDATA) { |
| 15171 | ctxt->sax->cdataBlock = NULL; |
| 15172 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15173 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15174 | } |
| 15175 | if (options & XML_PARSE_NSCLEAN) { |
| 15176 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 15177 | options -= XML_PARSE_NSCLEAN; |
| 15178 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15179 | if (options & XML_PARSE_NONET) { |
| 15180 | ctxt->options |= XML_PARSE_NONET; |
| 15181 | options -= XML_PARSE_NONET; |
| 15182 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 15183 | if (options & XML_PARSE_COMPACT) { |
| 15184 | ctxt->options |= XML_PARSE_COMPACT; |
| 15185 | options -= XML_PARSE_COMPACT; |
| 15186 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 15187 | if (options & XML_PARSE_OLD10) { |
| 15188 | ctxt->options |= XML_PARSE_OLD10; |
| 15189 | options -= XML_PARSE_OLD10; |
| 15190 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15191 | if (options & XML_PARSE_NOBASEFIX) { |
| 15192 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 15193 | options -= XML_PARSE_NOBASEFIX; |
| 15194 | } |
| 15195 | if (options & XML_PARSE_HUGE) { |
| 15196 | ctxt->options |= XML_PARSE_HUGE; |
| 15197 | options -= XML_PARSE_HUGE; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 15198 | if (ctxt->dict != NULL) |
| 15199 | xmlDictSetLimit(ctxt->dict, 0); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15200 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 15201 | if (options & XML_PARSE_OLDSAX) { |
| 15202 | ctxt->options |= XML_PARSE_OLDSAX; |
| 15203 | options -= XML_PARSE_OLDSAX; |
| 15204 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 15205 | if (options & XML_PARSE_IGNORE_ENC) { |
| 15206 | ctxt->options |= XML_PARSE_IGNORE_ENC; |
| 15207 | options -= XML_PARSE_IGNORE_ENC; |
| 15208 | } |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 15209 | if (options & XML_PARSE_BIG_LINES) { |
| 15210 | ctxt->options |= XML_PARSE_BIG_LINES; |
| 15211 | options -= XML_PARSE_BIG_LINES; |
| 15212 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 15213 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15214 | return (options); |
| 15215 | } |
| 15216 | |
| 15217 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15218 | * xmlCtxtUseOptions: |
| 15219 | * @ctxt: an XML parser context |
| 15220 | * @options: a combination of xmlParserOption |
| 15221 | * |
| 15222 | * Applies the options to the parser context |
| 15223 | * |
| 15224 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15225 | * in case of error. |
| 15226 | */ |
| 15227 | int |
| 15228 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 15229 | { |
| 15230 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 15231 | } |
| 15232 | |
| 15233 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15234 | * xmlDoRead: |
| 15235 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15236 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15237 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15238 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15239 | * @reuse: keep the context for reuse |
| 15240 | * |
| 15241 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15242 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15243 | * Returns the resulting document tree or NULL |
| 15244 | */ |
| 15245 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15246 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 15247 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15248 | { |
| 15249 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15250 | |
| 15251 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15252 | if (encoding != NULL) { |
| 15253 | xmlCharEncodingHandlerPtr hdlr; |
| 15254 | |
| 15255 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15256 | if (hdlr != NULL) |
| 15257 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15258 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15259 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 15260 | (ctxt->input->filename == NULL)) |
| 15261 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15262 | xmlParseDocument(ctxt); |
| 15263 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 15264 | ret = ctxt->myDoc; |
| 15265 | else { |
| 15266 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15267 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15268 | xmlFreeDoc(ctxt->myDoc); |
| 15269 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15270 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15271 | ctxt->myDoc = NULL; |
| 15272 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15273 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15274 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15275 | |
| 15276 | return (ret); |
| 15277 | } |
| 15278 | |
| 15279 | /** |
| 15280 | * xmlReadDoc: |
| 15281 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15282 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15283 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15284 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15285 | * |
| 15286 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15287 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15288 | * Returns the resulting document tree |
| 15289 | */ |
| 15290 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15291 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15292 | { |
| 15293 | xmlParserCtxtPtr ctxt; |
| 15294 | |
| 15295 | if (cur == NULL) |
| 15296 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15297 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15298 | |
| 15299 | ctxt = xmlCreateDocParserCtxt(cur); |
| 15300 | if (ctxt == NULL) |
| 15301 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15302 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15303 | } |
| 15304 | |
| 15305 | /** |
| 15306 | * xmlReadFile: |
| 15307 | * @filename: a file or URL |
| 15308 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15309 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15310 | * |
| 15311 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15312 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15313 | * Returns the resulting document tree |
| 15314 | */ |
| 15315 | xmlDocPtr |
| 15316 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 15317 | { |
| 15318 | xmlParserCtxtPtr ctxt; |
| 15319 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15320 | xmlInitParser(); |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15321 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15322 | if (ctxt == NULL) |
| 15323 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15324 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15325 | } |
| 15326 | |
| 15327 | /** |
| 15328 | * xmlReadMemory: |
| 15329 | * @buffer: a pointer to a char array |
| 15330 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15331 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15332 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15333 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15334 | * |
| 15335 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15336 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15337 | * Returns the resulting document tree |
| 15338 | */ |
| 15339 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15340 | 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] | 15341 | { |
| 15342 | xmlParserCtxtPtr ctxt; |
| 15343 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15344 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15345 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 15346 | if (ctxt == NULL) |
| 15347 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15348 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15349 | } |
| 15350 | |
| 15351 | /** |
| 15352 | * xmlReadFd: |
| 15353 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15354 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15355 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15356 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15357 | * |
| 15358 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15359 | * NOTE that the file descriptor will not be closed when the |
| 15360 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15361 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15362 | * Returns the resulting document tree |
| 15363 | */ |
| 15364 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15365 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15366 | { |
| 15367 | xmlParserCtxtPtr ctxt; |
| 15368 | xmlParserInputBufferPtr input; |
| 15369 | xmlParserInputPtr stream; |
| 15370 | |
| 15371 | if (fd < 0) |
| 15372 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15373 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15374 | |
| 15375 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15376 | if (input == NULL) |
| 15377 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15378 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15379 | ctxt = xmlNewParserCtxt(); |
| 15380 | if (ctxt == NULL) { |
| 15381 | xmlFreeParserInputBuffer(input); |
| 15382 | return (NULL); |
| 15383 | } |
| 15384 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15385 | if (stream == NULL) { |
| 15386 | xmlFreeParserInputBuffer(input); |
| 15387 | xmlFreeParserCtxt(ctxt); |
| 15388 | return (NULL); |
| 15389 | } |
| 15390 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15391 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15392 | } |
| 15393 | |
| 15394 | /** |
| 15395 | * xmlReadIO: |
| 15396 | * @ioread: an I/O read function |
| 15397 | * @ioclose: an I/O close function |
| 15398 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15399 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15400 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15401 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15402 | * |
| 15403 | * 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] | 15404 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15405 | * Returns the resulting document tree |
| 15406 | */ |
| 15407 | xmlDocPtr |
| 15408 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15409 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15410 | { |
| 15411 | xmlParserCtxtPtr ctxt; |
| 15412 | xmlParserInputBufferPtr input; |
| 15413 | xmlParserInputPtr stream; |
| 15414 | |
| 15415 | if (ioread == NULL) |
| 15416 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15417 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15418 | |
| 15419 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15420 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15421 | if (input == NULL) { |
| 15422 | if (ioclose != NULL) |
| 15423 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15424 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15425 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15426 | ctxt = xmlNewParserCtxt(); |
| 15427 | if (ctxt == NULL) { |
| 15428 | xmlFreeParserInputBuffer(input); |
| 15429 | return (NULL); |
| 15430 | } |
| 15431 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15432 | if (stream == NULL) { |
| 15433 | xmlFreeParserInputBuffer(input); |
| 15434 | xmlFreeParserCtxt(ctxt); |
| 15435 | return (NULL); |
| 15436 | } |
| 15437 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15438 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15439 | } |
| 15440 | |
| 15441 | /** |
| 15442 | * xmlCtxtReadDoc: |
| 15443 | * @ctxt: an XML parser context |
| 15444 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15445 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15446 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15447 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15448 | * |
| 15449 | * parse an XML in-memory document and build a tree. |
| 15450 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15451 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15452 | * Returns the resulting document tree |
| 15453 | */ |
| 15454 | xmlDocPtr |
| 15455 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15456 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15457 | { |
| 15458 | xmlParserInputPtr stream; |
| 15459 | |
| 15460 | if (cur == NULL) |
| 15461 | return (NULL); |
| 15462 | if (ctxt == NULL) |
| 15463 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15464 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15465 | |
| 15466 | xmlCtxtReset(ctxt); |
| 15467 | |
| 15468 | stream = xmlNewStringInputStream(ctxt, cur); |
| 15469 | if (stream == NULL) { |
| 15470 | return (NULL); |
| 15471 | } |
| 15472 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15473 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15474 | } |
| 15475 | |
| 15476 | /** |
| 15477 | * xmlCtxtReadFile: |
| 15478 | * @ctxt: an XML parser context |
| 15479 | * @filename: a file or URL |
| 15480 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15481 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15482 | * |
| 15483 | * parse an XML file from the filesystem or the network. |
| 15484 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15485 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15486 | * Returns the resulting document tree |
| 15487 | */ |
| 15488 | xmlDocPtr |
| 15489 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 15490 | const char *encoding, int options) |
| 15491 | { |
| 15492 | xmlParserInputPtr stream; |
| 15493 | |
| 15494 | if (filename == NULL) |
| 15495 | return (NULL); |
| 15496 | if (ctxt == NULL) |
| 15497 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15498 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15499 | |
| 15500 | xmlCtxtReset(ctxt); |
| 15501 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 15502 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15503 | if (stream == NULL) { |
| 15504 | return (NULL); |
| 15505 | } |
| 15506 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15507 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15508 | } |
| 15509 | |
| 15510 | /** |
| 15511 | * xmlCtxtReadMemory: |
| 15512 | * @ctxt: an XML parser context |
| 15513 | * @buffer: a pointer to a char array |
| 15514 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15515 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15516 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15517 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15518 | * |
| 15519 | * parse an XML in-memory document and build a tree. |
| 15520 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15521 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15522 | * Returns the resulting document tree |
| 15523 | */ |
| 15524 | xmlDocPtr |
| 15525 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15526 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15527 | { |
| 15528 | xmlParserInputBufferPtr input; |
| 15529 | xmlParserInputPtr stream; |
| 15530 | |
| 15531 | if (ctxt == NULL) |
| 15532 | return (NULL); |
| 15533 | if (buffer == NULL) |
| 15534 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15535 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15536 | |
| 15537 | xmlCtxtReset(ctxt); |
| 15538 | |
| 15539 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 15540 | if (input == NULL) { |
| 15541 | return(NULL); |
| 15542 | } |
| 15543 | |
| 15544 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15545 | if (stream == NULL) { |
| 15546 | xmlFreeParserInputBuffer(input); |
| 15547 | return(NULL); |
| 15548 | } |
| 15549 | |
| 15550 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15551 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15552 | } |
| 15553 | |
| 15554 | /** |
| 15555 | * xmlCtxtReadFd: |
| 15556 | * @ctxt: an XML parser context |
| 15557 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15558 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15559 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15560 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15561 | * |
| 15562 | * parse an XML from a file descriptor and build a tree. |
| 15563 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15564 | * NOTE that the file descriptor will not be closed when the |
| 15565 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15566 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15567 | * Returns the resulting document tree |
| 15568 | */ |
| 15569 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15570 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 15571 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15572 | { |
| 15573 | xmlParserInputBufferPtr input; |
| 15574 | xmlParserInputPtr stream; |
| 15575 | |
| 15576 | if (fd < 0) |
| 15577 | return (NULL); |
| 15578 | if (ctxt == NULL) |
| 15579 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15580 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15581 | |
| 15582 | xmlCtxtReset(ctxt); |
| 15583 | |
| 15584 | |
| 15585 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15586 | if (input == NULL) |
| 15587 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15588 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15589 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15590 | if (stream == NULL) { |
| 15591 | xmlFreeParserInputBuffer(input); |
| 15592 | return (NULL); |
| 15593 | } |
| 15594 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15595 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15596 | } |
| 15597 | |
| 15598 | /** |
| 15599 | * xmlCtxtReadIO: |
| 15600 | * @ctxt: an XML parser context |
| 15601 | * @ioread: an I/O read function |
| 15602 | * @ioclose: an I/O close function |
| 15603 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15604 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15605 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15606 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15607 | * |
| 15608 | * parse an XML document from I/O functions and source and build a tree. |
| 15609 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15610 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15611 | * Returns the resulting document tree |
| 15612 | */ |
| 15613 | xmlDocPtr |
| 15614 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 15615 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15616 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15617 | const char *encoding, int options) |
| 15618 | { |
| 15619 | xmlParserInputBufferPtr input; |
| 15620 | xmlParserInputPtr stream; |
| 15621 | |
| 15622 | if (ioread == NULL) |
| 15623 | return (NULL); |
| 15624 | if (ctxt == NULL) |
| 15625 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15626 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15627 | |
| 15628 | xmlCtxtReset(ctxt); |
| 15629 | |
| 15630 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15631 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15632 | if (input == NULL) { |
| 15633 | if (ioclose != NULL) |
| 15634 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15635 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15636 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15637 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15638 | if (stream == NULL) { |
| 15639 | xmlFreeParserInputBuffer(input); |
| 15640 | return (NULL); |
| 15641 | } |
| 15642 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15643 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15644 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15645 | |
| 15646 | #define bottom_parser |
| 15647 | #include "elfgcchack.h" |