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; |
| 2112 | } |
| 2113 | cur++; |
| 2114 | res++; |
| 2115 | if (*cur == 0) { |
| 2116 | ctxt->input->cur = cur; |
| 2117 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 2118 | cur = ctxt->input->cur; |
| 2119 | } |
| 2120 | } |
| 2121 | ctxt->input->cur = cur; |
| 2122 | } else { |
| 2123 | int cur; |
| 2124 | do { |
| 2125 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 2126 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2127 | NEXT; |
| 2128 | cur = CUR; |
| 2129 | res++; |
| 2130 | } |
| 2131 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 2132 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 2133 | xmlPopInput(ctxt); |
| 2134 | cur = CUR; |
| 2135 | } |
| 2136 | /* |
| 2137 | * Need to handle support of entities branching here |
| 2138 | */ |
| 2139 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 2140 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 2141 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2142 | return(res); |
| 2143 | } |
| 2144 | |
| 2145 | /************************************************************************ |
| 2146 | * * |
| 2147 | * Commodity functions to handle entities * |
| 2148 | * * |
| 2149 | ************************************************************************/ |
| 2150 | |
| 2151 | /** |
| 2152 | * xmlPopInput: |
| 2153 | * @ctxt: an XML parser context |
| 2154 | * |
| 2155 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 2156 | * pop it and return the next char. |
| 2157 | * |
| 2158 | * Returns the current xmlChar in the parser context |
| 2159 | */ |
| 2160 | xmlChar |
| 2161 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2162 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2163 | if (xmlParserDebugEntities) |
| 2164 | xmlGenericError(xmlGenericErrorContext, |
| 2165 | "Popping input %d\n", ctxt->inputNr); |
| 2166 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2167 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2168 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2169 | return(xmlPopInput(ctxt)); |
| 2170 | return(CUR); |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * xmlPushInput: |
| 2175 | * @ctxt: an XML parser context |
| 2176 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 2177 | * |
| 2178 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 2179 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2180 | * 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] | 2181 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2182 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2183 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2184 | int ret; |
| 2185 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2186 | |
| 2187 | if (xmlParserDebugEntities) { |
| 2188 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 2189 | xmlGenericError(xmlGenericErrorContext, |
| 2190 | "%s(%d): ", ctxt->input->filename, |
| 2191 | ctxt->input->line); |
| 2192 | xmlGenericError(xmlGenericErrorContext, |
| 2193 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 2194 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2195 | ret = inputPush(ctxt, input); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2196 | if (ctxt->instate == XML_PARSER_EOF) |
| 2197 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2198 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2199 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | /** |
| 2203 | * xmlParseCharRef: |
| 2204 | * @ctxt: an XML parser context |
| 2205 | * |
| 2206 | * parse Reference declarations |
| 2207 | * |
| 2208 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2209 | * '&#x' [0-9a-fA-F]+ ';' |
| 2210 | * |
| 2211 | * [ WFC: Legal Character ] |
| 2212 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2213 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2214 | * |
| 2215 | * Returns the value parsed (as an int), 0 in case of error |
| 2216 | */ |
| 2217 | int |
| 2218 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2219 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2220 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2221 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2222 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2223 | /* |
| 2224 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2225 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2226 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2227 | (NXT(2) == 'x')) { |
| 2228 | SKIP(3); |
| 2229 | GROW; |
| 2230 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2231 | if (count++ > 20) { |
| 2232 | count = 0; |
| 2233 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2234 | if (ctxt->instate == XML_PARSER_EOF) |
| 2235 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2236 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2237 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2238 | val = val * 16 + (CUR - '0'); |
| 2239 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2240 | val = val * 16 + (CUR - 'a') + 10; |
| 2241 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2242 | val = val * 16 + (CUR - 'A') + 10; |
| 2243 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2244 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2245 | val = 0; |
| 2246 | break; |
| 2247 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2248 | if (val > 0x10FFFF) |
| 2249 | outofrange = val; |
| 2250 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2251 | NEXT; |
| 2252 | count++; |
| 2253 | } |
| 2254 | if (RAW == ';') { |
| 2255 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2256 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2257 | ctxt->nbChars ++; |
| 2258 | ctxt->input->cur++; |
| 2259 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2260 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2261 | SKIP(2); |
| 2262 | GROW; |
| 2263 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2264 | if (count++ > 20) { |
| 2265 | count = 0; |
| 2266 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2267 | if (ctxt->instate == XML_PARSER_EOF) |
| 2268 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2269 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2270 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2271 | val = val * 10 + (CUR - '0'); |
| 2272 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2273 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2274 | val = 0; |
| 2275 | break; |
| 2276 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2277 | if (val > 0x10FFFF) |
| 2278 | outofrange = val; |
| 2279 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2280 | NEXT; |
| 2281 | count++; |
| 2282 | } |
| 2283 | if (RAW == ';') { |
| 2284 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2285 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2286 | ctxt->nbChars ++; |
| 2287 | ctxt->input->cur++; |
| 2288 | } |
| 2289 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2290 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * [ WFC: Legal Character ] |
| 2295 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2296 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2297 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2298 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2299 | return(val); |
| 2300 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2301 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2302 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2303 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | } |
| 2305 | return(0); |
| 2306 | } |
| 2307 | |
| 2308 | /** |
| 2309 | * xmlParseStringCharRef: |
| 2310 | * @ctxt: an XML parser context |
| 2311 | * @str: a pointer to an index in the string |
| 2312 | * |
| 2313 | * parse Reference declarations, variant parsing from a string rather |
| 2314 | * than an an input flow. |
| 2315 | * |
| 2316 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2317 | * '&#x' [0-9a-fA-F]+ ';' |
| 2318 | * |
| 2319 | * [ WFC: Legal Character ] |
| 2320 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2321 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2322 | * |
| 2323 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2324 | * updated to the current value of the index |
| 2325 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2326 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2327 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2328 | const xmlChar *ptr; |
| 2329 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2330 | unsigned int val = 0; |
| 2331 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2332 | |
| 2333 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2334 | ptr = *str; |
| 2335 | cur = *ptr; |
| 2336 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2337 | ptr += 3; |
| 2338 | cur = *ptr; |
| 2339 | while (cur != ';') { /* Non input consuming loop */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2340 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | val = val * 16 + (cur - '0'); |
| 2342 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2343 | val = val * 16 + (cur - 'a') + 10; |
| 2344 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2345 | val = val * 16 + (cur - 'A') + 10; |
| 2346 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2347 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2348 | val = 0; |
| 2349 | break; |
| 2350 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2351 | if (val > 0x10FFFF) |
| 2352 | outofrange = val; |
| 2353 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2354 | ptr++; |
| 2355 | cur = *ptr; |
| 2356 | } |
| 2357 | if (cur == ';') |
| 2358 | ptr++; |
| 2359 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2360 | ptr += 2; |
| 2361 | cur = *ptr; |
| 2362 | while (cur != ';') { /* Non input consuming loops */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2363 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2364 | val = val * 10 + (cur - '0'); |
| 2365 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2366 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2367 | val = 0; |
| 2368 | break; |
| 2369 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2370 | if (val > 0x10FFFF) |
| 2371 | outofrange = val; |
| 2372 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2373 | ptr++; |
| 2374 | cur = *ptr; |
| 2375 | } |
| 2376 | if (cur == ';') |
| 2377 | ptr++; |
| 2378 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2379 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2380 | return(0); |
| 2381 | } |
| 2382 | *str = ptr; |
| 2383 | |
| 2384 | /* |
| 2385 | * [ WFC: Legal Character ] |
| 2386 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2387 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2388 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2389 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2390 | return(val); |
| 2391 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2392 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2393 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2394 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2395 | } |
| 2396 | return(0); |
| 2397 | } |
| 2398 | |
| 2399 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2400 | * xmlNewBlanksWrapperInputStream: |
| 2401 | * @ctxt: an XML parser context |
| 2402 | * @entity: an Entity pointer |
| 2403 | * |
| 2404 | * Create a new input stream for wrapping |
| 2405 | * blanks around a PEReference |
| 2406 | * |
| 2407 | * Returns the new input stream or NULL |
| 2408 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2409 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2410 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2411 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2412 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2413 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2414 | xmlParserInputPtr input; |
| 2415 | xmlChar *buffer; |
| 2416 | size_t length; |
| 2417 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2418 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2419 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2420 | return(NULL); |
| 2421 | } |
| 2422 | if (xmlParserDebugEntities) |
| 2423 | xmlGenericError(xmlGenericErrorContext, |
| 2424 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2425 | input = xmlNewInputStream(ctxt); |
| 2426 | if (input == NULL) { |
| 2427 | return(NULL); |
| 2428 | } |
| 2429 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2430 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2431 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2432 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2433 | xmlFree(input); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2434 | return(NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2435 | } |
| 2436 | buffer [0] = ' '; |
| 2437 | buffer [1] = '%'; |
| 2438 | buffer [length-3] = ';'; |
| 2439 | buffer [length-2] = ' '; |
| 2440 | buffer [length-1] = 0; |
| 2441 | memcpy(buffer + 2, entity->name, length - 5); |
| 2442 | input->free = deallocblankswrapper; |
| 2443 | input->base = buffer; |
| 2444 | input->cur = buffer; |
| 2445 | input->length = length; |
| 2446 | input->end = &buffer[length]; |
| 2447 | return(input); |
| 2448 | } |
| 2449 | |
| 2450 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2451 | * xmlParserHandlePEReference: |
| 2452 | * @ctxt: the parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2453 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2454 | * [69] PEReference ::= '%' Name ';' |
| 2455 | * |
| 2456 | * [ WFC: No Recursion ] |
| 2457 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2458 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2459 | * |
| 2460 | * [ WFC: Entity Declared ] |
| 2461 | * In a document without any DTD, a document with only an internal DTD |
| 2462 | * subset which contains no parameter entity references, or a document |
| 2463 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2464 | * entity must precede any reference to it... |
| 2465 | * |
| 2466 | * [ VC: Entity Declared ] |
| 2467 | * In a document with an external subset or external parameter entities |
| 2468 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2469 | * must precede any reference to it... |
| 2470 | * |
| 2471 | * [ WFC: In DTD ] |
| 2472 | * Parameter-entity references may only appear in the DTD. |
| 2473 | * NOTE: misleading but this is handled. |
| 2474 | * |
| 2475 | * A PEReference may have been detected in the current input stream |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2476 | * the handling is done accordingly to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2477 | * http://www.w3.org/TR/REC-xml#entproc |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2478 | * i.e. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2479 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2480 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2481 | */ |
| 2482 | void |
| 2483 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2484 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2485 | xmlEntityPtr entity = NULL; |
| 2486 | xmlParserInputPtr input; |
| 2487 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2488 | if (RAW != '%') return; |
| 2489 | switch(ctxt->instate) { |
| 2490 | case XML_PARSER_CDATA_SECTION: |
| 2491 | return; |
| 2492 | case XML_PARSER_COMMENT: |
| 2493 | return; |
| 2494 | case XML_PARSER_START_TAG: |
| 2495 | return; |
| 2496 | case XML_PARSER_END_TAG: |
| 2497 | return; |
| 2498 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2499 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | case XML_PARSER_PROLOG: |
| 2502 | case XML_PARSER_START: |
| 2503 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2504 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | case XML_PARSER_ENTITY_DECL: |
| 2507 | case XML_PARSER_CONTENT: |
| 2508 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2509 | case XML_PARSER_PI: |
| 2510 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2511 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | /* we just ignore it there */ |
| 2513 | return; |
| 2514 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2515 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2516 | return; |
| 2517 | case XML_PARSER_ENTITY_VALUE: |
| 2518 | /* |
| 2519 | * NOTE: in the case of entity values, we don't do the |
| 2520 | * substitution here since we need the literal |
| 2521 | * entity value to be able to save the internal |
| 2522 | * subset of the document. |
| 2523 | * This will be handled by xmlStringDecodeEntities |
| 2524 | */ |
| 2525 | return; |
| 2526 | case XML_PARSER_DTD: |
| 2527 | /* |
| 2528 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2529 | * In the internal DTD subset, parameter-entity references |
| 2530 | * can occur only where markup declarations can occur, not |
| 2531 | * within markup declarations. |
| 2532 | * In that case this is handled in xmlParseMarkupDecl |
| 2533 | */ |
| 2534 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2535 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2536 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2537 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2538 | break; |
| 2539 | case XML_PARSER_IGNORE: |
| 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | NEXT; |
| 2544 | name = xmlParseName(ctxt); |
| 2545 | if (xmlParserDebugEntities) |
| 2546 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2547 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2548 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2549 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2550 | } else { |
| 2551 | if (RAW == ';') { |
| 2552 | NEXT; |
| 2553 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2554 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 2555 | if (ctxt->instate == XML_PARSER_EOF) |
| 2556 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2557 | if (entity == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2558 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2559 | /* |
| 2560 | * [ WFC: Entity Declared ] |
| 2561 | * In a document without any DTD, a document with only an |
| 2562 | * internal DTD subset which contains no parameter entity |
| 2563 | * references, or a document with "standalone='yes'", ... |
| 2564 | * ... The declaration of a parameter entity must precede |
| 2565 | * any reference to it... |
| 2566 | */ |
| 2567 | if ((ctxt->standalone == 1) || |
| 2568 | ((ctxt->hasExternalSubset == 0) && |
| 2569 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2570 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2571 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2572 | } else { |
| 2573 | /* |
| 2574 | * [ VC: Entity Declared ] |
| 2575 | * In a document with an external subset or external |
| 2576 | * parameter entities with "standalone='no'", ... |
| 2577 | * ... The declaration of a parameter entity must precede |
| 2578 | * any reference to it... |
| 2579 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2580 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2581 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2582 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2583 | name, NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2584 | } else |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2585 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2586 | "PEReference: %%%s; not found\n", |
| 2587 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2588 | ctxt->valid = 0; |
| 2589 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2590 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2591 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2592 | if (xmlPushInput(ctxt, input) < 0) |
| 2593 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2594 | } else { |
| 2595 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2596 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2597 | xmlChar start[4]; |
| 2598 | xmlCharEncoding enc; |
| 2599 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2600 | /* |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2601 | * Note: external parameter entities will not be loaded, it |
| 2602 | * is not required for a non-validating parser, unless the |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2603 | * option of validating, or substituting entities were |
| 2604 | * given. Doing so is far more secure as the parser will |
| 2605 | * only process data coming from the document entity by |
| 2606 | * default. |
| 2607 | */ |
| 2608 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 2609 | ((ctxt->options & XML_PARSE_NOENT) == 0) && |
| 2610 | ((ctxt->options & XML_PARSE_DTDVALID) == 0) && |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 2611 | ((ctxt->options & XML_PARSE_DTDLOAD) == 0) && |
| 2612 | ((ctxt->options & XML_PARSE_DTDATTR) == 0) && |
| 2613 | (ctxt->replaceEntities == 0) && |
Daniel Veillard | 9cd1c3c | 2014-04-22 15:30:56 +0800 | [diff] [blame] | 2614 | (ctxt->validate == 0)) |
| 2615 | return; |
| 2616 | |
| 2617 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | * handle the extra spaces added before and after |
| 2619 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2620 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2621 | */ |
| 2622 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2623 | if (xmlPushInput(ctxt, input) < 0) |
| 2624 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2625 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2626 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2627 | * Get the 4 first bytes and decode the charset |
| 2628 | * if enc != XML_CHAR_ENCODING_NONE |
| 2629 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2630 | * Note that, since we may have some non-UTF8 |
| 2631 | * encoding (like UTF16, bug 135229), the 'length' |
| 2632 | * is not known, but we can calculate based upon |
| 2633 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2634 | */ |
| 2635 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2636 | if (ctxt->instate == XML_PARSER_EOF) |
| 2637 | return; |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2638 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2639 | start[0] = RAW; |
| 2640 | start[1] = NXT(1); |
| 2641 | start[2] = NXT(2); |
| 2642 | start[3] = NXT(3); |
| 2643 | enc = xmlDetectCharEncoding(start, 4); |
| 2644 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2645 | xmlSwitchEncoding(ctxt, enc); |
| 2646 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2649 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2650 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2651 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | xmlParseTextDecl(ctxt); |
| 2653 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2654 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2655 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2656 | "PEReference: %s is not a parameter entity\n", |
| 2657 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2661 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2662 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | /* |
| 2667 | * Macro used to grow the current buffer. |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2668 | * buffer##_size is expected to be a size_t |
| 2669 | * mem_error: is expected to handle memory allocation failures |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2670 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2671 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2672 | xmlChar *tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2673 | size_t new_size = buffer##_size * 2 + n; \ |
| 2674 | if (new_size < buffer##_size) goto mem_error; \ |
| 2675 | tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2676 | if (tmp == NULL) goto mem_error; \ |
| 2677 | buffer = tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2678 | buffer##_size = new_size; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
| 2681 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2682 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | * @ctxt: the parser context |
| 2684 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2685 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2686 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2687 | * @end: an end marker xmlChar, 0 if none |
| 2688 | * @end2: an end marker xmlChar, 0 if none |
| 2689 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2690 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2691 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2692 | * |
| 2693 | * [67] Reference ::= EntityRef | CharRef |
| 2694 | * |
| 2695 | * [69] PEReference ::= '%' Name ';' |
| 2696 | * |
| 2697 | * Returns A newly allocated string with the substitution done. The caller |
| 2698 | * must deallocate it ! |
| 2699 | */ |
| 2700 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2701 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2702 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2703 | xmlChar *buffer = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2704 | size_t buffer_size = 0; |
| 2705 | size_t nbchars = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2706 | |
| 2707 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2708 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2709 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | xmlEntityPtr ent; |
| 2711 | int c,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2713 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2714 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2715 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2717 | if (((ctxt->depth > 40) && |
| 2718 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2719 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2720 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2721 | return(NULL); |
| 2722 | } |
| 2723 | |
| 2724 | /* |
| 2725 | * allocate a translation buffer. |
| 2726 | */ |
| 2727 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2728 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2729 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | |
| 2731 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2732 | * 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] | 2733 | * we are operating on already parsed values. |
| 2734 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2735 | if (str < last) |
| 2736 | c = CUR_SCHAR(str, l); |
| 2737 | else |
| 2738 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2739 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2740 | (c != end2) && (c != end3)) { |
| 2741 | |
| 2742 | if (c == 0) break; |
| 2743 | if ((c == '&') && (str[1] == '#')) { |
| 2744 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2745 | if (val != 0) { |
| 2746 | COPY_BUF(0,buffer,nbchars,val); |
| 2747 | } |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2748 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2749 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2751 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2752 | if (xmlParserDebugEntities) |
| 2753 | xmlGenericError(xmlGenericErrorContext, |
| 2754 | "String decoding Entity Reference: %.30s\n", |
| 2755 | str); |
| 2756 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2757 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2758 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2759 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2760 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2761 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2762 | if ((ent != NULL) && |
| 2763 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2764 | if (ent->content != NULL) { |
| 2765 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2766 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2767 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2768 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2769 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2770 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2771 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2772 | } |
| 2773 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2774 | ctxt->depth++; |
| 2775 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2776 | 0, 0, 0); |
| 2777 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2778 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | if (rep != NULL) { |
| 2780 | current = rep; |
| 2781 | while (*current != 0) { /* non input consuming loop */ |
| 2782 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2783 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2784 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2785 | goto int_error; |
| 2786 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2787 | } |
| 2788 | } |
| 2789 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2790 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } else if (ent != NULL) { |
| 2793 | int i = xmlStrlen(ent->name); |
| 2794 | const xmlChar *cur = ent->name; |
| 2795 | |
| 2796 | buffer[nbchars++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2797 | if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 5bd3c06 | 2011-12-16 18:53:35 +0800 | [diff] [blame] | 2798 | growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2799 | } |
| 2800 | for (;i > 0;i--) |
| 2801 | buffer[nbchars++] = *cur++; |
| 2802 | buffer[nbchars++] = ';'; |
| 2803 | } |
| 2804 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2805 | if (xmlParserDebugEntities) |
| 2806 | xmlGenericError(xmlGenericErrorContext, |
| 2807 | "String decoding PE Reference: %.30s\n", str); |
| 2808 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2809 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2810 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2811 | if (ent != NULL) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 2812 | ctxt->nbentities += ent->checked / 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2813 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2814 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2815 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2816 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | ctxt->depth++; |
| 2818 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2819 | 0, 0, 0); |
| 2820 | ctxt->depth--; |
| 2821 | if (rep != NULL) { |
| 2822 | current = rep; |
| 2823 | while (*current != 0) { /* non input consuming loop */ |
| 2824 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2825 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 2826 | if (xmlParserEntityCheck(ctxt, nbchars, ent, 0)) |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2827 | goto int_error; |
| 2828 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2829 | } |
| 2830 | } |
| 2831 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2832 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2833 | } |
| 2834 | } |
| 2835 | } else { |
| 2836 | COPY_BUF(l,buffer,nbchars,c); |
| 2837 | str += l; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2838 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
| 2839 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2840 | } |
| 2841 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2842 | if (str < last) |
| 2843 | c = CUR_SCHAR(str, l); |
| 2844 | else |
| 2845 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2846 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2847 | buffer[nbchars] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2848 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2849 | |
| 2850 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2851 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2852 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2853 | if (rep != NULL) |
| 2854 | xmlFree(rep); |
| 2855 | if (buffer != NULL) |
| 2856 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2857 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2860 | /** |
| 2861 | * xmlStringDecodeEntities: |
| 2862 | * @ctxt: the parser context |
| 2863 | * @str: the input string |
| 2864 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2865 | * @end: an end marker xmlChar, 0 if none |
| 2866 | * @end2: an end marker xmlChar, 0 if none |
| 2867 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2868 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2869 | * Takes a entity string content and process to do the adequate substitutions. |
| 2870 | * |
| 2871 | * [67] Reference ::= EntityRef | CharRef |
| 2872 | * |
| 2873 | * [69] PEReference ::= '%' Name ';' |
| 2874 | * |
| 2875 | * Returns A newly allocated string with the substitution done. The caller |
| 2876 | * must deallocate it ! |
| 2877 | */ |
| 2878 | xmlChar * |
| 2879 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2880 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2881 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2882 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2883 | end, end2, end3)); |
| 2884 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2885 | |
| 2886 | /************************************************************************ |
| 2887 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2888 | * Commodity functions, cleanup needed ? * |
| 2889 | * * |
| 2890 | ************************************************************************/ |
| 2891 | |
| 2892 | /** |
| 2893 | * areBlanks: |
| 2894 | * @ctxt: an XML parser context |
| 2895 | * @str: a xmlChar * |
| 2896 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2897 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | * |
| 2899 | * Is this a sequence of blank chars that one can ignore ? |
| 2900 | * |
| 2901 | * Returns 1 if ignorable 0 otherwise. |
| 2902 | */ |
| 2903 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2904 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2905 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2906 | int i, ret; |
| 2907 | xmlNodePtr lastChild; |
| 2908 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2909 | /* |
| 2910 | * Don't spend time trying to differentiate them, the same callback is |
| 2911 | * used ! |
| 2912 | */ |
| 2913 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2914 | return(0); |
| 2915 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2916 | /* |
| 2917 | * Check for xml:space value. |
| 2918 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2919 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2920 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2921 | return(0); |
| 2922 | |
| 2923 | /* |
| 2924 | * Check that the string is made of blanks |
| 2925 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2926 | if (blank_chars == 0) { |
| 2927 | for (i = 0;i < len;i++) |
| 2928 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2929 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2930 | |
| 2931 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2932 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2933 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2934 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2935 | if (ctxt->myDoc != NULL) { |
| 2936 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2937 | if (ret == 0) return(1); |
| 2938 | if (ret == 1) return(0); |
| 2939 | } |
| 2940 | |
| 2941 | /* |
| 2942 | * Otherwise, heuristic :-\ |
| 2943 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2944 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2945 | if ((ctxt->node->children == NULL) && |
| 2946 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2947 | |
| 2948 | lastChild = xmlGetLastChild(ctxt->node); |
| 2949 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2950 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2951 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2952 | } else if (xmlNodeIsText(lastChild)) |
| 2953 | return(0); |
| 2954 | else if ((ctxt->node->children != NULL) && |
| 2955 | (xmlNodeIsText(ctxt->node->children))) |
| 2956 | return(0); |
| 2957 | return(1); |
| 2958 | } |
| 2959 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2960 | /************************************************************************ |
| 2961 | * * |
| 2962 | * Extra stuff for namespace support * |
| 2963 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2964 | * * |
| 2965 | ************************************************************************/ |
| 2966 | |
| 2967 | /** |
| 2968 | * xmlSplitQName: |
| 2969 | * @ctxt: an XML parser context |
| 2970 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2971 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2972 | * |
| 2973 | * parse an UTF8 encoded XML qualified name string |
| 2974 | * |
| 2975 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2976 | * |
| 2977 | * [NS 6] Prefix ::= NCName |
| 2978 | * |
| 2979 | * [NS 7] LocalPart ::= NCName |
| 2980 | * |
| 2981 | * Returns the local part, and prefix is updated |
| 2982 | * to get the Prefix if any. |
| 2983 | */ |
| 2984 | |
| 2985 | xmlChar * |
| 2986 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2987 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2988 | xmlChar *buffer = NULL; |
| 2989 | int len = 0; |
| 2990 | int max = XML_MAX_NAMELEN; |
| 2991 | xmlChar *ret = NULL; |
| 2992 | const xmlChar *cur = name; |
| 2993 | int c; |
| 2994 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2995 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | *prefix = NULL; |
| 2997 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2998 | if (cur == NULL) return(NULL); |
| 2999 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3000 | #ifndef XML_XML_NAMESPACE |
| 3001 | /* xml: prefix is not really a namespace */ |
| 3002 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 3003 | (cur[2] == 'l') && (cur[3] == ':')) |
| 3004 | return(xmlStrdup(name)); |
| 3005 | #endif |
| 3006 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3007 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3008 | if (cur[0] == ':') |
| 3009 | return(xmlStrdup(name)); |
| 3010 | |
| 3011 | c = *cur++; |
| 3012 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 3013 | buf[len++] = c; |
| 3014 | c = *cur++; |
| 3015 | } |
| 3016 | if (len >= max) { |
| 3017 | /* |
| 3018 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3019 | * for the processing speed. |
| 3020 | */ |
| 3021 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3022 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3023 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3024 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3025 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3026 | return(NULL); |
| 3027 | } |
| 3028 | memcpy(buffer, buf, len); |
| 3029 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 3030 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3031 | xmlChar *tmp; |
| 3032 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3033 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3034 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3035 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3036 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3037 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3038 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | return(NULL); |
| 3040 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3041 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3042 | } |
| 3043 | buffer[len++] = c; |
| 3044 | c = *cur++; |
| 3045 | } |
| 3046 | buffer[len] = 0; |
| 3047 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3048 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3049 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3050 | if (buffer != NULL) |
| 3051 | xmlFree(buffer); |
| 3052 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3053 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3054 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3055 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3056 | if (buffer == NULL) |
| 3057 | ret = xmlStrndup(buf, len); |
| 3058 | else { |
| 3059 | ret = buffer; |
| 3060 | buffer = NULL; |
| 3061 | max = XML_MAX_NAMELEN; |
| 3062 | } |
| 3063 | |
| 3064 | |
| 3065 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3066 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3068 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 3069 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3070 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3071 | len = 0; |
| 3072 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3073 | /* |
| 3074 | * Check that the first character is proper to start |
| 3075 | * a new name |
| 3076 | */ |
| 3077 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 3078 | ((c >= 0x41) && (c <= 0x5A)) || |
| 3079 | (c == '_') || (c == ':'))) { |
| 3080 | int l; |
| 3081 | int first = CUR_SCHAR(cur, l); |
| 3082 | |
| 3083 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3084 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3085 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3086 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | cur++; |
| 3090 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3091 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 3092 | buf[len++] = c; |
| 3093 | c = *cur++; |
| 3094 | } |
| 3095 | if (len >= max) { |
| 3096 | /* |
| 3097 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3098 | * for the processing speed. |
| 3099 | */ |
| 3100 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3101 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3102 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3103 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3104 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3105 | return(NULL); |
| 3106 | } |
| 3107 | memcpy(buffer, buf, len); |
| 3108 | while (c != 0) { /* tested bigname2.xml */ |
| 3109 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3110 | xmlChar *tmp; |
| 3111 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3112 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3113 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3114 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3115 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3116 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3117 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3118 | return(NULL); |
| 3119 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3120 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3121 | } |
| 3122 | buffer[len++] = c; |
| 3123 | c = *cur++; |
| 3124 | } |
| 3125 | buffer[len] = 0; |
| 3126 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3127 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3128 | if (buffer == NULL) |
| 3129 | ret = xmlStrndup(buf, len); |
| 3130 | else { |
| 3131 | ret = buffer; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | return(ret); |
| 3136 | } |
| 3137 | |
| 3138 | /************************************************************************ |
| 3139 | * * |
| 3140 | * The parser itself * |
| 3141 | * Relates to http://www.w3.org/TR/REC-xml * |
| 3142 | * * |
| 3143 | ************************************************************************/ |
| 3144 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3145 | /************************************************************************ |
| 3146 | * * |
| 3147 | * Routines to parse Name, NCName and NmToken * |
| 3148 | * * |
| 3149 | ************************************************************************/ |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3150 | #ifdef DEBUG |
| 3151 | static unsigned long nbParseName = 0; |
| 3152 | static unsigned long nbParseNmToken = 0; |
| 3153 | static unsigned long nbParseNCName = 0; |
| 3154 | static unsigned long nbParseNCNameComplex = 0; |
| 3155 | static unsigned long nbParseNameComplex = 0; |
| 3156 | static unsigned long nbParseStringName = 0; |
| 3157 | #endif |
| 3158 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3159 | /* |
| 3160 | * The two following functions are related to the change of accepted |
| 3161 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 3162 | * They correspond to the modified production [4] and the new production [4a] |
| 3163 | * changes in that revision. Also note that the macros used for the |
| 3164 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 3165 | * anymore. |
| 3166 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 3167 | * new XML_PARSE_OLD10 option is given to the parser. |
| 3168 | */ |
| 3169 | static int |
| 3170 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 3171 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3172 | /* |
| 3173 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3174 | * Update 5 of XML-1.0 |
| 3175 | */ |
| 3176 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3177 | (((c >= 'a') && (c <= 'z')) || |
| 3178 | ((c >= 'A') && (c <= 'Z')) || |
| 3179 | (c == '_') || (c == ':') || |
| 3180 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3181 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3182 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3183 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3184 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3185 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3186 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3187 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3188 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3189 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3190 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3191 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3192 | return(1); |
| 3193 | } else { |
| 3194 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3195 | return(1); |
| 3196 | } |
| 3197 | return(0); |
| 3198 | } |
| 3199 | |
| 3200 | static int |
| 3201 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 3202 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3203 | /* |
| 3204 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3205 | * Update 5 of XML-1.0 |
| 3206 | */ |
| 3207 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3208 | (((c >= 'a') && (c <= 'z')) || |
| 3209 | ((c >= 'A') && (c <= 'Z')) || |
| 3210 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3211 | (c == '_') || (c == ':') || |
| 3212 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3213 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3214 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3215 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3216 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3217 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3218 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3219 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3220 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3221 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3222 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3223 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3224 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3225 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3226 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3227 | return(1); |
| 3228 | } else { |
| 3229 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3230 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3231 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3232 | (IS_COMBINING(c)) || |
| 3233 | (IS_EXTENDER(c))) |
| 3234 | return(1); |
| 3235 | } |
| 3236 | return(0); |
| 3237 | } |
| 3238 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3239 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3240 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3241 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3242 | static const xmlChar * |
| 3243 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3244 | int len = 0, l; |
| 3245 | int c; |
| 3246 | int count = 0; |
| 3247 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3248 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3249 | nbParseNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3250 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3251 | |
| 3252 | /* |
| 3253 | * Handler for more complex cases |
| 3254 | */ |
| 3255 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3256 | if (ctxt->instate == XML_PARSER_EOF) |
| 3257 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3258 | c = CUR_CHAR(l); |
| 3259 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3260 | /* |
| 3261 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3262 | * Update 5 of XML-1.0 |
| 3263 | */ |
| 3264 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3265 | (!(((c >= 'a') && (c <= 'z')) || |
| 3266 | ((c >= 'A') && (c <= 'Z')) || |
| 3267 | (c == '_') || (c == ':') || |
| 3268 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3269 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3270 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3271 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3272 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3273 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3274 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3275 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3276 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3277 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3278 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3279 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3280 | return(NULL); |
| 3281 | } |
| 3282 | len += l; |
| 3283 | NEXTL(l); |
| 3284 | c = CUR_CHAR(l); |
| 3285 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3286 | (((c >= 'a') && (c <= 'z')) || |
| 3287 | ((c >= 'A') && (c <= 'Z')) || |
| 3288 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3289 | (c == '_') || (c == ':') || |
| 3290 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3291 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3292 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3293 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3294 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3295 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3296 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3297 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3298 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3299 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3300 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3301 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3302 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3303 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3304 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3305 | )) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3306 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3307 | count = 0; |
| 3308 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3309 | if (ctxt->instate == XML_PARSER_EOF) |
| 3310 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3311 | } |
| 3312 | len += l; |
| 3313 | NEXTL(l); |
| 3314 | c = CUR_CHAR(l); |
| 3315 | } |
| 3316 | } else { |
| 3317 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3318 | (!IS_LETTER(c) && (c != '_') && |
| 3319 | (c != ':'))) { |
| 3320 | return(NULL); |
| 3321 | } |
| 3322 | len += l; |
| 3323 | NEXTL(l); |
| 3324 | c = CUR_CHAR(l); |
| 3325 | |
| 3326 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3327 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3328 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3329 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3330 | (IS_COMBINING(c)) || |
| 3331 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3332 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3333 | count = 0; |
| 3334 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3335 | if (ctxt->instate == XML_PARSER_EOF) |
| 3336 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3337 | } |
| 3338 | len += l; |
| 3339 | NEXTL(l); |
| 3340 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3341 | if (c == 0) { |
| 3342 | count = 0; |
| 3343 | GROW; |
| 3344 | if (ctxt->instate == XML_PARSER_EOF) |
| 3345 | return(NULL); |
| 3346 | c = CUR_CHAR(l); |
| 3347 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3350 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3351 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3352 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3353 | return(NULL); |
| 3354 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3355 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3356 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3357 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3358 | } |
| 3359 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3360 | /** |
| 3361 | * xmlParseName: |
| 3362 | * @ctxt: an XML parser context |
| 3363 | * |
| 3364 | * parse an XML name. |
| 3365 | * |
| 3366 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3367 | * CombiningChar | Extender |
| 3368 | * |
| 3369 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3370 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3371 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3372 | * |
| 3373 | * Returns the Name parsed or NULL |
| 3374 | */ |
| 3375 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3376 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3377 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3378 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3379 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3380 | int count = 0; |
| 3381 | |
| 3382 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3383 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3384 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3385 | nbParseName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3386 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3387 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3388 | /* |
| 3389 | * Accelerator for simple ASCII names |
| 3390 | */ |
| 3391 | in = ctxt->input->cur; |
| 3392 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3393 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3394 | (*in == '_') || (*in == ':')) { |
| 3395 | in++; |
| 3396 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3397 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3398 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3399 | (*in == '_') || (*in == '-') || |
| 3400 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3401 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3402 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3403 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3404 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3405 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3406 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3407 | return(NULL); |
| 3408 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3409 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3410 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3411 | ctxt->nbChars += count; |
| 3412 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3413 | if (ret == NULL) |
| 3414 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3415 | return(ret); |
| 3416 | } |
| 3417 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3418 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3419 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3420 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3421 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3422 | static const xmlChar * |
| 3423 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3424 | int len = 0, l; |
| 3425 | int c; |
| 3426 | int count = 0; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3427 | 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] | 3428 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3429 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3430 | nbParseNCNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3431 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3432 | |
| 3433 | /* |
| 3434 | * Handler for more complex cases |
| 3435 | */ |
| 3436 | GROW; |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3437 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3438 | c = CUR_CHAR(l); |
| 3439 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3440 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3441 | return(NULL); |
| 3442 | } |
| 3443 | |
| 3444 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3445 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3446 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3447 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3448 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3449 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3450 | return(NULL); |
| 3451 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3452 | count = 0; |
| 3453 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3454 | if (ctxt->instate == XML_PARSER_EOF) |
| 3455 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3456 | } |
| 3457 | len += l; |
| 3458 | NEXTL(l); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3459 | end = ctxt->input->cur; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3460 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3461 | if (c == 0) { |
| 3462 | count = 0; |
| 3463 | GROW; |
| 3464 | if (ctxt->instate == XML_PARSER_EOF) |
| 3465 | return(NULL); |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3466 | end = ctxt->input->cur; |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3467 | c = CUR_CHAR(l); |
| 3468 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3469 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3470 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3471 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3472 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3473 | return(NULL); |
| 3474 | } |
Daniel Veillard | dcc1950 | 2013-05-22 22:56:45 +0200 | [diff] [blame] | 3475 | return(xmlDictLookup(ctxt->dict, end - len, len)); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | /** |
| 3479 | * xmlParseNCName: |
| 3480 | * @ctxt: an XML parser context |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3481 | * @len: length of the string parsed |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3482 | * |
| 3483 | * parse an XML name. |
| 3484 | * |
| 3485 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3486 | * CombiningChar | Extender |
| 3487 | * |
| 3488 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3489 | * |
| 3490 | * Returns the Name parsed or NULL |
| 3491 | */ |
| 3492 | |
| 3493 | static const xmlChar * |
| 3494 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 3495 | const xmlChar *in; |
| 3496 | const xmlChar *ret; |
| 3497 | int count = 0; |
| 3498 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3499 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3500 | nbParseNCName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3501 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3502 | |
| 3503 | /* |
| 3504 | * Accelerator for simple ASCII names |
| 3505 | */ |
| 3506 | in = ctxt->input->cur; |
| 3507 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3508 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3509 | (*in == '_')) { |
| 3510 | in++; |
| 3511 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3512 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3513 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3514 | (*in == '_') || (*in == '-') || |
| 3515 | (*in == '.')) |
| 3516 | in++; |
| 3517 | if ((*in > 0) && (*in < 0x80)) { |
| 3518 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3519 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3520 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3521 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3522 | return(NULL); |
| 3523 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3524 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3525 | ctxt->input->cur = in; |
| 3526 | ctxt->nbChars += count; |
| 3527 | ctxt->input->col += count; |
| 3528 | if (ret == NULL) { |
| 3529 | xmlErrMemory(ctxt, NULL); |
| 3530 | } |
| 3531 | return(ret); |
| 3532 | } |
| 3533 | } |
| 3534 | return(xmlParseNCNameComplex(ctxt)); |
| 3535 | } |
| 3536 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3537 | /** |
| 3538 | * xmlParseNameAndCompare: |
| 3539 | * @ctxt: an XML parser context |
| 3540 | * |
| 3541 | * parse an XML name and compares for match |
| 3542 | * (specialized for endtag parsing) |
| 3543 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3544 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3545 | * and the name for mismatch |
| 3546 | */ |
| 3547 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3548 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3549 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3550 | register const xmlChar *cmp = other; |
| 3551 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3552 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3553 | |
| 3554 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3555 | if (ctxt->instate == XML_PARSER_EOF) |
| 3556 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3557 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3558 | in = ctxt->input->cur; |
| 3559 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3560 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3561 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3562 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3563 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3564 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3565 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3566 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3567 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3568 | } |
| 3569 | /* failure (or end of input buffer), check with full function */ |
| 3570 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3571 | /* strings coming from the dictionnary direct compare possible */ |
| 3572 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3573 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3574 | } |
| 3575 | return ret; |
| 3576 | } |
| 3577 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3578 | /** |
| 3579 | * xmlParseStringName: |
| 3580 | * @ctxt: an XML parser context |
| 3581 | * @str: a pointer to the string pointer (IN/OUT) |
| 3582 | * |
| 3583 | * parse an XML name. |
| 3584 | * |
| 3585 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3586 | * CombiningChar | Extender |
| 3587 | * |
| 3588 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3589 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3590 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3591 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3592 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3593 | * is updated to the current location in the string. |
| 3594 | */ |
| 3595 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3596 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3597 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3598 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3599 | const xmlChar *cur = *str; |
| 3600 | int len = 0, l; |
| 3601 | int c; |
| 3602 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3603 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3604 | nbParseStringName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3605 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3606 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3607 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3608 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3609 | return(NULL); |
| 3610 | } |
| 3611 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3612 | COPY_BUF(l,buf,len,c); |
| 3613 | cur += l; |
| 3614 | c = CUR_SCHAR(cur, l); |
| 3615 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3616 | COPY_BUF(l,buf,len,c); |
| 3617 | cur += l; |
| 3618 | c = CUR_SCHAR(cur, l); |
| 3619 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3620 | /* |
| 3621 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3622 | * for the processing speed. |
| 3623 | */ |
| 3624 | xmlChar *buffer; |
| 3625 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3626 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3627 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3628 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3629 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3630 | return(NULL); |
| 3631 | } |
| 3632 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3633 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3634 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3635 | xmlChar *tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3636 | |
| 3637 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3638 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3639 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3640 | xmlFree(buffer); |
| 3641 | return(NULL); |
| 3642 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3643 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3644 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3645 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3646 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3647 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3648 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3649 | return(NULL); |
| 3650 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3651 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3652 | } |
| 3653 | COPY_BUF(l,buffer,len,c); |
| 3654 | cur += l; |
| 3655 | c = CUR_SCHAR(cur, l); |
| 3656 | } |
| 3657 | buffer[len] = 0; |
| 3658 | *str = cur; |
| 3659 | return(buffer); |
| 3660 | } |
| 3661 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3662 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3663 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3664 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3665 | return(NULL); |
| 3666 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3667 | *str = cur; |
| 3668 | return(xmlStrndup(buf, len)); |
| 3669 | } |
| 3670 | |
| 3671 | /** |
| 3672 | * xmlParseNmtoken: |
| 3673 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3674 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3675 | * parse an XML Nmtoken. |
| 3676 | * |
| 3677 | * [7] Nmtoken ::= (NameChar)+ |
| 3678 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3679 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3680 | * |
| 3681 | * Returns the Nmtoken parsed or NULL |
| 3682 | */ |
| 3683 | |
| 3684 | xmlChar * |
| 3685 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3686 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3687 | int len = 0, l; |
| 3688 | int c; |
| 3689 | int count = 0; |
| 3690 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3691 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3692 | nbParseNmToken++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3693 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3694 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3695 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3696 | if (ctxt->instate == XML_PARSER_EOF) |
| 3697 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3698 | c = CUR_CHAR(l); |
| 3699 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3700 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3701 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3702 | count = 0; |
| 3703 | GROW; |
| 3704 | } |
| 3705 | COPY_BUF(l,buf,len,c); |
| 3706 | NEXTL(l); |
| 3707 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3708 | if (c == 0) { |
| 3709 | count = 0; |
| 3710 | GROW; |
| 3711 | if (ctxt->instate == XML_PARSER_EOF) |
| 3712 | return(NULL); |
| 3713 | c = CUR_CHAR(l); |
| 3714 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3715 | if (len >= XML_MAX_NAMELEN) { |
| 3716 | /* |
| 3717 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3718 | * for the processing speed. |
| 3719 | */ |
| 3720 | xmlChar *buffer; |
| 3721 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3722 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3723 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3724 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3725 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3726 | return(NULL); |
| 3727 | } |
| 3728 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3729 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3730 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | count = 0; |
| 3732 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3733 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3734 | xmlFree(buffer); |
| 3735 | return(NULL); |
| 3736 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3737 | } |
| 3738 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3739 | xmlChar *tmp; |
| 3740 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3741 | if ((max > XML_MAX_NAME_LENGTH) && |
| 3742 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3743 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3744 | xmlFree(buffer); |
| 3745 | return(NULL); |
| 3746 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3747 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3748 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3749 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3750 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3751 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3752 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3753 | return(NULL); |
| 3754 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3755 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3756 | } |
| 3757 | COPY_BUF(l,buffer,len,c); |
| 3758 | NEXTL(l); |
| 3759 | c = CUR_CHAR(l); |
| 3760 | } |
| 3761 | buffer[len] = 0; |
| 3762 | return(buffer); |
| 3763 | } |
| 3764 | } |
| 3765 | if (len == 0) |
| 3766 | return(NULL); |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3767 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3768 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3769 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3770 | return(NULL); |
| 3771 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3772 | return(xmlStrndup(buf, len)); |
| 3773 | } |
| 3774 | |
| 3775 | /** |
| 3776 | * xmlParseEntityValue: |
| 3777 | * @ctxt: an XML parser context |
| 3778 | * @orig: if non-NULL store a copy of the original entity value |
| 3779 | * |
| 3780 | * parse a value for ENTITY declarations |
| 3781 | * |
| 3782 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3783 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3784 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3785 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3786 | */ |
| 3787 | |
| 3788 | xmlChar * |
| 3789 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3790 | xmlChar *buf = NULL; |
| 3791 | int len = 0; |
| 3792 | int size = XML_PARSER_BUFFER_SIZE; |
| 3793 | int c, l; |
| 3794 | xmlChar stop; |
| 3795 | xmlChar *ret = NULL; |
| 3796 | const xmlChar *cur = NULL; |
| 3797 | xmlParserInputPtr input; |
| 3798 | |
| 3799 | if (RAW == '"') stop = '"'; |
| 3800 | else if (RAW == '\'') stop = '\''; |
| 3801 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3802 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3803 | return(NULL); |
| 3804 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3805 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3807 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3808 | return(NULL); |
| 3809 | } |
| 3810 | |
| 3811 | /* |
| 3812 | * The content of the entity definition is copied in a buffer. |
| 3813 | */ |
| 3814 | |
| 3815 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3816 | input = ctxt->input; |
| 3817 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3818 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3819 | xmlFree(buf); |
| 3820 | return(NULL); |
| 3821 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3822 | NEXT; |
| 3823 | c = CUR_CHAR(l); |
| 3824 | /* |
| 3825 | * NOTE: 4.4.5 Included in Literal |
| 3826 | * When a parameter entity reference appears in a literal entity |
| 3827 | * value, ... a single or double quote character in the replacement |
| 3828 | * text is always treated as a normal data character and will not |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3829 | * terminate the literal. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3830 | * In practice it means we stop the loop only when back at parsing |
| 3831 | * the initial entity and the quote is found |
| 3832 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3833 | while (((IS_CHAR(c)) && ((c != stop) || /* checked */ |
| 3834 | (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3835 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3836 | xmlChar *tmp; |
| 3837 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3838 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3839 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3840 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3841 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3842 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3843 | return(NULL); |
| 3844 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3845 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3846 | } |
| 3847 | COPY_BUF(l,buf,len,c); |
| 3848 | NEXTL(l); |
| 3849 | /* |
| 3850 | * Pop-up of finished entities. |
| 3851 | */ |
| 3852 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3853 | xmlPopInput(ctxt); |
| 3854 | |
| 3855 | GROW; |
| 3856 | c = CUR_CHAR(l); |
| 3857 | if (c == 0) { |
| 3858 | GROW; |
| 3859 | c = CUR_CHAR(l); |
| 3860 | } |
| 3861 | } |
| 3862 | buf[len] = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3863 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3864 | xmlFree(buf); |
| 3865 | return(NULL); |
| 3866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3867 | |
| 3868 | /* |
| 3869 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3870 | * reference constructs. Note Charref will be handled in |
| 3871 | * xmlStringDecodeEntities() |
| 3872 | */ |
| 3873 | cur = buf; |
| 3874 | while (*cur != 0) { /* non input consuming */ |
| 3875 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3876 | xmlChar *name; |
| 3877 | xmlChar tmp = *cur; |
| 3878 | |
| 3879 | cur++; |
| 3880 | name = xmlParseStringName(ctxt, &cur); |
| 3881 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3882 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3884 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3885 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3886 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3887 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3888 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3889 | } |
| 3890 | if (name != NULL) |
| 3891 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3892 | if (*cur == 0) |
| 3893 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | } |
| 3895 | cur++; |
| 3896 | } |
| 3897 | |
| 3898 | /* |
| 3899 | * Then PEReference entities are substituted. |
| 3900 | */ |
| 3901 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3902 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3903 | xmlFree(buf); |
| 3904 | } else { |
| 3905 | NEXT; |
| 3906 | /* |
| 3907 | * NOTE: 4.4.7 Bypassed |
| 3908 | * When a general entity reference appears in the EntityValue in |
| 3909 | * an entity declaration, it is bypassed and left as is. |
| 3910 | * so XML_SUBSTITUTE_REF is not set here. |
| 3911 | */ |
| 3912 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3913 | 0, 0, 0); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3914 | if (orig != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3915 | *orig = buf; |
| 3916 | else |
| 3917 | xmlFree(buf); |
| 3918 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3919 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3920 | return(ret); |
| 3921 | } |
| 3922 | |
| 3923 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3924 | * xmlParseAttValueComplex: |
| 3925 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3926 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3927 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3928 | * |
| 3929 | * parse a value for an attribute, this is the fallback function |
| 3930 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3931 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3932 | * |
| 3933 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3934 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3935 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3936 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3937 | xmlChar limit = 0; |
| 3938 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3939 | xmlChar *rep = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3940 | size_t len = 0; |
| 3941 | size_t buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3942 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3943 | xmlChar *current = NULL; |
| 3944 | xmlEntityPtr ent; |
| 3945 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3946 | if (NXT(0) == '"') { |
| 3947 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3948 | limit = '"'; |
| 3949 | NEXT; |
| 3950 | } else if (NXT(0) == '\'') { |
| 3951 | limit = '\''; |
| 3952 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3953 | NEXT; |
| 3954 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3955 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3956 | return(NULL); |
| 3957 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3958 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3959 | /* |
| 3960 | * allocate a translation buffer. |
| 3961 | */ |
| 3962 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3963 | buf = (xmlChar *) xmlMallocAtomic(buf_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3964 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3965 | |
| 3966 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3967 | * 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] | 3968 | */ |
| 3969 | c = CUR_CHAR(l); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3970 | while (((NXT(0) != limit) && /* checked */ |
| 3971 | (IS_CHAR(c)) && (c != '<')) && |
| 3972 | (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3973 | /* |
| 3974 | * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE |
| 3975 | * special option is given |
| 3976 | */ |
| 3977 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 3978 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3979 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3980 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3981 | goto mem_error; |
| 3982 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3983 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3984 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3985 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3986 | if (NXT(1) == '#') { |
| 3987 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3988 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3989 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3990 | if (ctxt->replaceEntities) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3991 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3992 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3993 | } |
| 3994 | buf[len++] = '&'; |
| 3995 | } else { |
| 3996 | /* |
| 3997 | * The reparsing will be done in xmlStringGetNodeList() |
| 3998 | * called by the attribute() function in SAX.c |
| 3999 | */ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4000 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4001 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 4002 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4003 | buf[len++] = '&'; |
| 4004 | buf[len++] = '#'; |
| 4005 | buf[len++] = '3'; |
| 4006 | buf[len++] = '8'; |
| 4007 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4008 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 4009 | } else if (val != 0) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4010 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4011 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4012 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4013 | len += xmlCopyChar(0, &buf[len], val); |
| 4014 | } |
| 4015 | } else { |
| 4016 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 4017 | ctxt->nbentities++; |
| 4018 | if (ent != NULL) |
| 4019 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4020 | if ((ent != NULL) && |
| 4021 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4022 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4023 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4024 | } |
| 4025 | if ((ctxt->replaceEntities == 0) && |
| 4026 | (ent->content[0] == '&')) { |
| 4027 | buf[len++] = '&'; |
| 4028 | buf[len++] = '#'; |
| 4029 | buf[len++] = '3'; |
| 4030 | buf[len++] = '8'; |
| 4031 | buf[len++] = ';'; |
| 4032 | } else { |
| 4033 | buf[len++] = ent->content[0]; |
| 4034 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4035 | } else if ((ent != NULL) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4036 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4037 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 4038 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4039 | XML_SUBSTITUTE_REF, |
| 4040 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4041 | if (rep != NULL) { |
| 4042 | current = rep; |
| 4043 | while (*current != 0) { /* non input consuming */ |
Daniel Veillard | 283d502 | 2009-08-25 17:18:39 +0200 | [diff] [blame] | 4044 | if ((*current == 0xD) || (*current == 0xA) || |
| 4045 | (*current == 0x9)) { |
| 4046 | buf[len++] = 0x20; |
| 4047 | current++; |
| 4048 | } else |
| 4049 | buf[len++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4050 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4051 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4052 | } |
| 4053 | } |
| 4054 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4055 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4056 | } |
| 4057 | } else { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4058 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4059 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4060 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4061 | if (ent->content != NULL) |
| 4062 | buf[len++] = ent->content[0]; |
| 4063 | } |
| 4064 | } else if (ent != NULL) { |
| 4065 | int i = xmlStrlen(ent->name); |
| 4066 | const xmlChar *cur = ent->name; |
| 4067 | |
| 4068 | /* |
| 4069 | * This may look absurd but is needed to detect |
| 4070 | * entities problems |
| 4071 | */ |
| 4072 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4073 | (ent->content != NULL) && (ent->checked == 0)) { |
| 4074 | unsigned long oldnbent = ctxt->nbentities; |
| 4075 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4076 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4077 | XML_SUBSTITUTE_REF, 0, 0, 0); |
Daniel Veillard | a3f1e3e | 2013-03-11 13:57:53 +0800 | [diff] [blame] | 4078 | |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4079 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4080 | if (rep != NULL) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 4081 | if (xmlStrchr(rep, '<')) |
| 4082 | ent->checked |= 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4083 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4084 | rep = NULL; |
| 4085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | /* |
| 4089 | * Just output the reference |
| 4090 | */ |
| 4091 | buf[len++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4092 | while (len + i + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4093 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4094 | } |
| 4095 | for (;i > 0;i--) |
| 4096 | buf[len++] = *cur++; |
| 4097 | buf[len++] = ';'; |
| 4098 | } |
| 4099 | } |
| 4100 | } else { |
| 4101 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4102 | if ((len != 0) || (!normalize)) { |
| 4103 | if ((!normalize) || (!in_space)) { |
| 4104 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4105 | while (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4106 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4107 | } |
| 4108 | } |
| 4109 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4110 | } |
| 4111 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4112 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4113 | COPY_BUF(l,buf,len,c); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4114 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4115 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | NEXTL(l); |
| 4119 | } |
| 4120 | GROW; |
| 4121 | c = CUR_CHAR(l); |
| 4122 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4123 | if (ctxt->instate == XML_PARSER_EOF) |
| 4124 | goto error; |
| 4125 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4126 | if ((in_space) && (normalize)) { |
Daniel Veillard | 6a36fbe | 2012-10-29 10:39:55 +0800 | [diff] [blame] | 4127 | while ((len > 0) && (buf[len - 1] == 0x20)) len--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4128 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4129 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4130 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4131 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 4133 | if ((c != 0) && (!IS_CHAR(c))) { |
| 4134 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 4135 | "invalid character in attribute value\n"); |
| 4136 | } else { |
| 4137 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 4138 | "AttValue: ' expected\n"); |
| 4139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4140 | } else |
| 4141 | NEXT; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4142 | |
| 4143 | /* |
| 4144 | * There we potentially risk an overflow, don't allow attribute value of |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4145 | * length more than INT_MAX it is a very reasonnable assumption ! |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4146 | */ |
| 4147 | if (len >= INT_MAX) { |
| 4148 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4149 | "AttValue length too long\n"); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4150 | goto mem_error; |
| 4151 | } |
| 4152 | |
| 4153 | if (attlen != NULL) *attlen = (int) len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4154 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4155 | |
| 4156 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4157 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4158 | error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4159 | if (buf != NULL) |
| 4160 | xmlFree(buf); |
| 4161 | if (rep != NULL) |
| 4162 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4163 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4167 | * xmlParseAttValue: |
| 4168 | * @ctxt: an XML parser context |
| 4169 | * |
| 4170 | * parse a value for an attribute |
| 4171 | * Note: the parser won't do substitution of entities here, this |
| 4172 | * will be handled later in xmlStringGetNodeList |
| 4173 | * |
| 4174 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 4175 | * "'" ([^<&'] | Reference)* "'" |
| 4176 | * |
| 4177 | * 3.3.3 Attribute-Value Normalization: |
| 4178 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4179 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4180 | * - a character reference is processed by appending the referenced |
| 4181 | * character to the attribute value |
| 4182 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4183 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4184 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 4185 | * appending #x20 to the normalized value, except that only a single |
| 4186 | * #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] | 4187 | * parsed entity or the literal entity value of an internal parsed entity |
| 4188 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4189 | * If the declared value is not CDATA, then the XML processor must further |
| 4190 | * process the normalized attribute value by discarding any leading and |
| 4191 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4192 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4193 | * All attributes for which no declaration has been read should be treated |
| 4194 | * by a non-validating parser as if declared CDATA. |
| 4195 | * |
| 4196 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 4197 | */ |
| 4198 | |
| 4199 | |
| 4200 | xmlChar * |
| 4201 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 4202 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4203 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4207 | * xmlParseSystemLiteral: |
| 4208 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4209 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | * parse an XML Literal |
| 4211 | * |
| 4212 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 4213 | * |
| 4214 | * Returns the SystemLiteral parsed or NULL |
| 4215 | */ |
| 4216 | |
| 4217 | xmlChar * |
| 4218 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 4219 | xmlChar *buf = NULL; |
| 4220 | int len = 0; |
| 4221 | int size = XML_PARSER_BUFFER_SIZE; |
| 4222 | int cur, l; |
| 4223 | xmlChar stop; |
| 4224 | int state = ctxt->instate; |
| 4225 | int count = 0; |
| 4226 | |
| 4227 | SHRINK; |
| 4228 | if (RAW == '"') { |
| 4229 | NEXT; |
| 4230 | stop = '"'; |
| 4231 | } else if (RAW == '\'') { |
| 4232 | NEXT; |
| 4233 | stop = '\''; |
| 4234 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4235 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4236 | return(NULL); |
| 4237 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4238 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4239 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4240 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4241 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4242 | return(NULL); |
| 4243 | } |
| 4244 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 4245 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4246 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4247 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4248 | xmlChar *tmp; |
| 4249 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4250 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4251 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4252 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral"); |
| 4253 | xmlFree(buf); |
| 4254 | ctxt->instate = (xmlParserInputState) state; |
| 4255 | return(NULL); |
| 4256 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4257 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4258 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4259 | if (tmp == NULL) { |
| 4260 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4261 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4262 | ctxt->instate = (xmlParserInputState) state; |
| 4263 | return(NULL); |
| 4264 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4265 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4266 | } |
| 4267 | count++; |
| 4268 | if (count > 50) { |
| 4269 | GROW; |
| 4270 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4271 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4272 | xmlFree(buf); |
| 4273 | return(NULL); |
| 4274 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4275 | } |
| 4276 | COPY_BUF(l,buf,len,cur); |
| 4277 | NEXTL(l); |
| 4278 | cur = CUR_CHAR(l); |
| 4279 | if (cur == 0) { |
| 4280 | GROW; |
| 4281 | SHRINK; |
| 4282 | cur = CUR_CHAR(l); |
| 4283 | } |
| 4284 | } |
| 4285 | buf[len] = 0; |
| 4286 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4287 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4288 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4289 | } else { |
| 4290 | NEXT; |
| 4291 | } |
| 4292 | return(buf); |
| 4293 | } |
| 4294 | |
| 4295 | /** |
| 4296 | * xmlParsePubidLiteral: |
| 4297 | * @ctxt: an XML parser context |
| 4298 | * |
| 4299 | * parse an XML public literal |
| 4300 | * |
| 4301 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 4302 | * |
| 4303 | * Returns the PubidLiteral parsed or NULL. |
| 4304 | */ |
| 4305 | |
| 4306 | xmlChar * |
| 4307 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 4308 | xmlChar *buf = NULL; |
| 4309 | int len = 0; |
| 4310 | int size = XML_PARSER_BUFFER_SIZE; |
| 4311 | xmlChar cur; |
| 4312 | xmlChar stop; |
| 4313 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4314 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4315 | |
| 4316 | SHRINK; |
| 4317 | if (RAW == '"') { |
| 4318 | NEXT; |
| 4319 | stop = '"'; |
| 4320 | } else if (RAW == '\'') { |
| 4321 | NEXT; |
| 4322 | stop = '\''; |
| 4323 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4324 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4325 | return(NULL); |
| 4326 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4327 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4328 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4329 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | return(NULL); |
| 4331 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4332 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4333 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4334 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4335 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4336 | xmlChar *tmp; |
| 4337 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4338 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4339 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4340 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID"); |
| 4341 | xmlFree(buf); |
| 4342 | return(NULL); |
| 4343 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4344 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4345 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4346 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4347 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4348 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4349 | return(NULL); |
| 4350 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4351 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4352 | } |
| 4353 | buf[len++] = cur; |
| 4354 | count++; |
| 4355 | if (count > 50) { |
| 4356 | GROW; |
| 4357 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4358 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4359 | xmlFree(buf); |
| 4360 | return(NULL); |
| 4361 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4362 | } |
| 4363 | NEXT; |
| 4364 | cur = CUR; |
| 4365 | if (cur == 0) { |
| 4366 | GROW; |
| 4367 | SHRINK; |
| 4368 | cur = CUR; |
| 4369 | } |
| 4370 | } |
| 4371 | buf[len] = 0; |
| 4372 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4373 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4374 | } else { |
| 4375 | NEXT; |
| 4376 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4377 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4378 | return(buf); |
| 4379 | } |
| 4380 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4381 | static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4382 | |
| 4383 | /* |
| 4384 | * used for the test in the inner loop of the char data testing |
| 4385 | */ |
| 4386 | static const unsigned char test_char_data[256] = { |
| 4387 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4388 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 4389 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4390 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4391 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 4392 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 4393 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 4394 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 4395 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 4396 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 4397 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 4398 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 4399 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4400 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4401 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4402 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4403 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4404 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4405 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 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 | }; |
| 4420 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4421 | /** |
| 4422 | * xmlParseCharData: |
| 4423 | * @ctxt: an XML parser context |
| 4424 | * @cdata: int indicating whether we are within a CDATA section |
| 4425 | * |
| 4426 | * parse a CharData section. |
| 4427 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4428 | * |
| 4429 | * The right angle bracket (>) may be represented using the string ">", |
| 4430 | * and must, for compatibility, be escaped using ">" or a character |
| 4431 | * reference when it appears in the string "]]>" in content, when that |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4432 | * string is not marking the end of a CDATA section. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4433 | * |
| 4434 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4435 | */ |
| 4436 | |
| 4437 | void |
| 4438 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4439 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4440 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4441 | int line = ctxt->input->line; |
| 4442 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4443 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4444 | |
| 4445 | SHRINK; |
| 4446 | GROW; |
| 4447 | /* |
| 4448 | * Accelerated common case where input don't need to be |
| 4449 | * modified before passing it to the handler. |
| 4450 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4451 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4452 | in = ctxt->input->cur; |
| 4453 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4454 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4455 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4456 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4457 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4458 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4459 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4460 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4461 | goto get_more_space; |
| 4462 | } |
| 4463 | if (*in == '<') { |
| 4464 | nbchar = in - ctxt->input->cur; |
| 4465 | if (nbchar > 0) { |
| 4466 | const xmlChar *tmp = ctxt->input->cur; |
| 4467 | ctxt->input->cur = in; |
| 4468 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4469 | if ((ctxt->sax != NULL) && |
| 4470 | (ctxt->sax->ignorableWhitespace != |
| 4471 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4472 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4473 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4474 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4475 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4476 | } else { |
| 4477 | if (ctxt->sax->characters != NULL) |
| 4478 | ctxt->sax->characters(ctxt->userData, |
| 4479 | tmp, nbchar); |
| 4480 | if (*ctxt->space == -1) |
| 4481 | *ctxt->space = -2; |
| 4482 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4483 | } else if ((ctxt->sax != NULL) && |
| 4484 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4485 | ctxt->sax->characters(ctxt->userData, |
| 4486 | tmp, nbchar); |
| 4487 | } |
| 4488 | } |
| 4489 | return; |
| 4490 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4491 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4492 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4493 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4494 | while (test_char_data[*in]) { |
| 4495 | in++; |
| 4496 | ccol++; |
| 4497 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4498 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4499 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4500 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4501 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4502 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4503 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4504 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4505 | } |
| 4506 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4507 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4508 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4509 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4510 | return; |
| 4511 | } |
| 4512 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4513 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4514 | goto get_more; |
| 4515 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4516 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4517 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4518 | if ((ctxt->sax != NULL) && |
| 4519 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4520 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4521 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4522 | const xmlChar *tmp = ctxt->input->cur; |
| 4523 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4524 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4525 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4526 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4527 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4528 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4529 | } else { |
| 4530 | if (ctxt->sax->characters != NULL) |
| 4531 | ctxt->sax->characters(ctxt->userData, |
| 4532 | tmp, nbchar); |
| 4533 | if (*ctxt->space == -1) |
| 4534 | *ctxt->space = -2; |
| 4535 | } |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4536 | line = ctxt->input->line; |
| 4537 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4538 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4539 | if (ctxt->sax->characters != NULL) |
| 4540 | ctxt->sax->characters(ctxt->userData, |
| 4541 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4542 | line = ctxt->input->line; |
| 4543 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4544 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4545 | /* something really bad happened in the SAX callback */ |
| 4546 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4547 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4548 | } |
| 4549 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4550 | if (*in == 0xD) { |
| 4551 | in++; |
| 4552 | if (*in == 0xA) { |
| 4553 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4554 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4555 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4556 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4557 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4558 | in--; |
| 4559 | } |
| 4560 | if (*in == '<') { |
| 4561 | return; |
| 4562 | } |
| 4563 | if (*in == '&') { |
| 4564 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4565 | } |
| 4566 | SHRINK; |
| 4567 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4568 | if (ctxt->instate == XML_PARSER_EOF) |
| 4569 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4570 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4571 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4572 | nbchar = 0; |
| 4573 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4574 | ctxt->input->line = line; |
| 4575 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4576 | xmlParseCharDataComplex(ctxt, cdata); |
| 4577 | } |
| 4578 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4579 | /** |
| 4580 | * xmlParseCharDataComplex: |
| 4581 | * @ctxt: an XML parser context |
| 4582 | * @cdata: int indicating whether we are within a CDATA section |
| 4583 | * |
| 4584 | * parse a CharData section.this is the fallback function |
| 4585 | * of xmlParseCharData() when the parsing requires handling |
| 4586 | * of non-ASCII characters. |
| 4587 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4588 | static void |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4589 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4590 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4591 | int nbchar = 0; |
| 4592 | int cur, l; |
| 4593 | int count = 0; |
| 4594 | |
| 4595 | SHRINK; |
| 4596 | GROW; |
| 4597 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4598 | while ((cur != '<') && /* checked */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4599 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4600 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4601 | if ((cur == ']') && (NXT(1) == ']') && |
| 4602 | (NXT(2) == '>')) { |
| 4603 | if (cdata) break; |
| 4604 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4605 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4606 | } |
| 4607 | } |
| 4608 | COPY_BUF(l,buf,nbchar,cur); |
| 4609 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4610 | buf[nbchar] = 0; |
| 4611 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4612 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4613 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4614 | */ |
| 4615 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4616 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4617 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4618 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4619 | buf, nbchar); |
| 4620 | } else { |
| 4621 | if (ctxt->sax->characters != NULL) |
| 4622 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4623 | if ((ctxt->sax->characters != |
| 4624 | ctxt->sax->ignorableWhitespace) && |
| 4625 | (*ctxt->space == -1)) |
| 4626 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4630 | /* something really bad happened in the SAX callback */ |
| 4631 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4632 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4633 | } |
| 4634 | count++; |
| 4635 | if (count > 50) { |
| 4636 | GROW; |
| 4637 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4638 | if (ctxt->instate == XML_PARSER_EOF) |
| 4639 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | } |
| 4641 | NEXTL(l); |
| 4642 | cur = CUR_CHAR(l); |
| 4643 | } |
| 4644 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4645 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4646 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4647 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4648 | */ |
| 4649 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4650 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4651 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4652 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4653 | } else { |
| 4654 | if (ctxt->sax->characters != NULL) |
| 4655 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4656 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4657 | (*ctxt->space == -1)) |
| 4658 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4659 | } |
| 4660 | } |
| 4661 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4662 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4663 | /* Generate the error and skip the offending character */ |
| 4664 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4665 | "PCDATA invalid Char value %d\n", |
| 4666 | cur); |
| 4667 | NEXTL(l); |
| 4668 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
| 4671 | /** |
| 4672 | * xmlParseExternalID: |
| 4673 | * @ctxt: an XML parser context |
| 4674 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4675 | * @strict: indicate whether we should restrict parsing to only |
| 4676 | * production [75], see NOTE below |
| 4677 | * |
| 4678 | * Parse an External ID or a Public ID |
| 4679 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4680 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4681 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4682 | * |
| 4683 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4684 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4685 | * |
| 4686 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4687 | * |
| 4688 | * Returns the function returns SystemLiteral and in the second |
| 4689 | * case publicID receives PubidLiteral, is strict is off |
| 4690 | * it is possible to return NULL and have publicID set. |
| 4691 | */ |
| 4692 | |
| 4693 | xmlChar * |
| 4694 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4695 | xmlChar *URI = NULL; |
| 4696 | |
| 4697 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4698 | |
| 4699 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4700 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4702 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4703 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4704 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4705 | } |
| 4706 | SKIP_BLANKS; |
| 4707 | URI = xmlParseSystemLiteral(ctxt); |
| 4708 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4709 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4710 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4711 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4712 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4713 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4714 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4715 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4716 | } |
| 4717 | SKIP_BLANKS; |
| 4718 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4719 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4720 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4721 | } |
| 4722 | if (strict) { |
| 4723 | /* |
| 4724 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4725 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4726 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4727 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4728 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4729 | } |
| 4730 | } else { |
| 4731 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4732 | * We handle [83] so we return immediately, if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4733 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4734 | * point of view that's a nice mess. |
| 4735 | */ |
| 4736 | const xmlChar *ptr; |
| 4737 | GROW; |
| 4738 | |
| 4739 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4740 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4741 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4742 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4743 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4744 | } |
| 4745 | SKIP_BLANKS; |
| 4746 | URI = xmlParseSystemLiteral(ctxt); |
| 4747 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4748 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4749 | } |
| 4750 | } |
| 4751 | return(URI); |
| 4752 | } |
| 4753 | |
| 4754 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4755 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4756 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4757 | * @buf: the already parsed part of the buffer |
| 4758 | * @len: number of bytes filles in the buffer |
| 4759 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4760 | * |
| 4761 | * Skip an XML (SGML) comment <!-- .... --> |
| 4762 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4763 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4764 | * 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] | 4765 | * |
| 4766 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4767 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4768 | static void |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4769 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, |
| 4770 | size_t len, size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4771 | int q, ql; |
| 4772 | int r, rl; |
| 4773 | int cur, l; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4774 | size_t count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4775 | int inputid; |
| 4776 | |
| 4777 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4778 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4780 | len = 0; |
| 4781 | size = XML_PARSER_BUFFER_SIZE; |
| 4782 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4783 | if (buf == NULL) { |
| 4784 | xmlErrMemory(ctxt, NULL); |
| 4785 | return; |
| 4786 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4787 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4788 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4789 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4790 | if (q == 0) |
| 4791 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4792 | if (!IS_CHAR(q)) { |
| 4793 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4794 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4795 | q); |
| 4796 | xmlFree (buf); |
| 4797 | return; |
| 4798 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4799 | NEXTL(ql); |
| 4800 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4801 | if (r == 0) |
| 4802 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4803 | if (!IS_CHAR(r)) { |
| 4804 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4805 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4806 | q); |
| 4807 | xmlFree (buf); |
| 4808 | return; |
| 4809 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4810 | NEXTL(rl); |
| 4811 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4812 | if (cur == 0) |
| 4813 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4814 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4815 | ((cur != '>') || |
| 4816 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4817 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4818 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4820 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4821 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4822 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4823 | "Comment too big found", NULL); |
| 4824 | xmlFree (buf); |
| 4825 | return; |
| 4826 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4827 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4828 | xmlChar *new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4829 | size_t new_size; |
| 4830 | |
| 4831 | new_size = size * 2; |
| 4832 | new_buf = (xmlChar *) xmlRealloc(buf, new_size); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4833 | if (new_buf == NULL) { |
| 4834 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4835 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4836 | return; |
| 4837 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4838 | buf = new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4839 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4840 | } |
| 4841 | COPY_BUF(ql,buf,len,q); |
| 4842 | q = r; |
| 4843 | ql = rl; |
| 4844 | r = cur; |
| 4845 | rl = l; |
| 4846 | |
| 4847 | count++; |
| 4848 | if (count > 50) { |
| 4849 | GROW; |
| 4850 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4851 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4852 | xmlFree(buf); |
| 4853 | return; |
| 4854 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4855 | } |
| 4856 | NEXTL(l); |
| 4857 | cur = CUR_CHAR(l); |
| 4858 | if (cur == 0) { |
| 4859 | SHRINK; |
| 4860 | GROW; |
| 4861 | cur = CUR_CHAR(l); |
| 4862 | } |
| 4863 | } |
| 4864 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4865 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4866 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4867 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4868 | } else if (!IS_CHAR(cur)) { |
| 4869 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4870 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4871 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4872 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4873 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4874 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4875 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4876 | } |
| 4877 | NEXT; |
| 4878 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4879 | (!ctxt->disableSAX)) |
| 4880 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4881 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4882 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4883 | return; |
| 4884 | not_terminated: |
| 4885 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4886 | "Comment not terminated\n", NULL); |
| 4887 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4888 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4889 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4890 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4891 | /** |
| 4892 | * xmlParseComment: |
| 4893 | * @ctxt: an XML parser context |
| 4894 | * |
| 4895 | * Skip an XML (SGML) comment <!-- .... --> |
| 4896 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4897 | * must not occur within comments. " |
| 4898 | * |
| 4899 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4900 | */ |
| 4901 | void |
| 4902 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4903 | xmlChar *buf = NULL; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4904 | size_t size = XML_PARSER_BUFFER_SIZE; |
| 4905 | size_t len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4906 | xmlParserInputState state; |
| 4907 | const xmlChar *in; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4908 | size_t nbchar = 0; |
| 4909 | int ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4910 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4911 | |
| 4912 | /* |
| 4913 | * Check that there is a comment right here. |
| 4914 | */ |
| 4915 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4916 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4917 | state = ctxt->instate; |
| 4918 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4919 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4920 | SKIP(4); |
| 4921 | SHRINK; |
| 4922 | GROW; |
| 4923 | |
| 4924 | /* |
| 4925 | * Accelerated common case where input don't need to be |
| 4926 | * modified before passing it to the handler. |
| 4927 | */ |
| 4928 | in = ctxt->input->cur; |
| 4929 | do { |
| 4930 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4931 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4932 | ctxt->input->line++; ctxt->input->col = 1; |
| 4933 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4934 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4935 | } |
| 4936 | get_more: |
| 4937 | ccol = ctxt->input->col; |
| 4938 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4939 | ((*in >= 0x20) && (*in < '-')) || |
| 4940 | (*in == 0x09)) { |
| 4941 | in++; |
| 4942 | ccol++; |
| 4943 | } |
| 4944 | ctxt->input->col = ccol; |
| 4945 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4946 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4947 | ctxt->input->line++; ctxt->input->col = 1; |
| 4948 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4949 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4950 | goto get_more; |
| 4951 | } |
| 4952 | nbchar = in - ctxt->input->cur; |
| 4953 | /* |
| 4954 | * save current set of data |
| 4955 | */ |
| 4956 | if (nbchar > 0) { |
| 4957 | if ((ctxt->sax != NULL) && |
| 4958 | (ctxt->sax->comment != NULL)) { |
| 4959 | if (buf == NULL) { |
| 4960 | if ((*in == '-') && (in[1] == '-')) |
| 4961 | size = nbchar + 1; |
| 4962 | else |
| 4963 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4964 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4965 | if (buf == NULL) { |
| 4966 | xmlErrMemory(ctxt, NULL); |
| 4967 | ctxt->instate = state; |
| 4968 | return; |
| 4969 | } |
| 4970 | len = 0; |
| 4971 | } else if (len + nbchar + 1 >= size) { |
| 4972 | xmlChar *new_buf; |
| 4973 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4974 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4975 | size * sizeof(xmlChar)); |
| 4976 | if (new_buf == NULL) { |
| 4977 | xmlFree (buf); |
| 4978 | xmlErrMemory(ctxt, NULL); |
| 4979 | ctxt->instate = state; |
| 4980 | return; |
| 4981 | } |
| 4982 | buf = new_buf; |
| 4983 | } |
| 4984 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4985 | len += nbchar; |
| 4986 | buf[len] = 0; |
| 4987 | } |
| 4988 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4989 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4990 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4991 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4992 | "Comment too big found", NULL); |
| 4993 | xmlFree (buf); |
| 4994 | return; |
| 4995 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4996 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4997 | if (*in == 0xA) { |
| 4998 | in++; |
| 4999 | ctxt->input->line++; ctxt->input->col = 1; |
| 5000 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5001 | if (*in == 0xD) { |
| 5002 | in++; |
| 5003 | if (*in == 0xA) { |
| 5004 | ctxt->input->cur = in; |
| 5005 | in++; |
| 5006 | ctxt->input->line++; ctxt->input->col = 1; |
| 5007 | continue; /* while */ |
| 5008 | } |
| 5009 | in--; |
| 5010 | } |
| 5011 | SHRINK; |
| 5012 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5013 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5014 | xmlFree(buf); |
| 5015 | return; |
| 5016 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5017 | in = ctxt->input->cur; |
| 5018 | if (*in == '-') { |
| 5019 | if (in[1] == '-') { |
| 5020 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 5021 | if (ctxt->input->id != inputid) { |
| 5022 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5023 | "comment doesn't start and stop in the same entity\n"); |
| 5024 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5025 | SKIP(3); |
| 5026 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 5027 | (!ctxt->disableSAX)) { |
| 5028 | if (buf != NULL) |
| 5029 | ctxt->sax->comment(ctxt->userData, buf); |
| 5030 | else |
| 5031 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 5032 | } |
| 5033 | if (buf != NULL) |
| 5034 | xmlFree(buf); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5035 | if (ctxt->instate != XML_PARSER_EOF) |
| 5036 | ctxt->instate = state; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5037 | return; |
| 5038 | } |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5039 | if (buf != NULL) { |
| 5040 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5041 | "Double hyphen within comment: " |
| 5042 | "<!--%.50s\n", |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5043 | buf); |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 5044 | } else |
| 5045 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 5046 | "Double hyphen within comment\n", NULL); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5047 | in++; |
| 5048 | ctxt->input->col++; |
| 5049 | } |
| 5050 | in++; |
| 5051 | ctxt->input->col++; |
| 5052 | goto get_more; |
| 5053 | } |
| 5054 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 5055 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 5056 | ctxt->instate = state; |
| 5057 | return; |
| 5058 | } |
| 5059 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5060 | |
| 5061 | /** |
| 5062 | * xmlParsePITarget: |
| 5063 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5064 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5065 | * parse the name of a PI |
| 5066 | * |
| 5067 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 5068 | * |
| 5069 | * Returns the PITarget name or NULL |
| 5070 | */ |
| 5071 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5072 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5073 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5074 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5075 | |
| 5076 | name = xmlParseName(ctxt); |
| 5077 | if ((name != NULL) && |
| 5078 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 5079 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 5080 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 5081 | int i; |
| 5082 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 5083 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5084 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5085 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5086 | return(name); |
| 5087 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5088 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5089 | return(name); |
| 5090 | } |
| 5091 | for (i = 0;;i++) { |
| 5092 | if (xmlW3CPIs[i] == NULL) break; |
| 5093 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 5094 | return(name); |
| 5095 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5096 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 5097 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 5098 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5099 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5100 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5101 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5102 | "colon are forbidden from PI names '%s'\n", name, NULL, NULL); |
| 5103 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5104 | return(name); |
| 5105 | } |
| 5106 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5107 | #ifdef LIBXML_CATALOG_ENABLED |
| 5108 | /** |
| 5109 | * xmlParseCatalogPI: |
| 5110 | * @ctxt: an XML parser context |
| 5111 | * @catalog: the PI value string |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5112 | * |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5113 | * parse an XML Catalog Processing Instruction. |
| 5114 | * |
| 5115 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 5116 | * |
| 5117 | * Occurs only if allowed by the user and if happening in the Misc |
| 5118 | * part of the document before any doctype informations |
| 5119 | * This will add the given catalog to the parsing context in order |
| 5120 | * to be used if there is a resolution need further down in the document |
| 5121 | */ |
| 5122 | |
| 5123 | static void |
| 5124 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 5125 | xmlChar *URL = NULL; |
| 5126 | const xmlChar *tmp, *base; |
| 5127 | xmlChar marker; |
| 5128 | |
| 5129 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5130 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5131 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 5132 | goto error; |
| 5133 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5134 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5135 | if (*tmp != '=') { |
| 5136 | return; |
| 5137 | } |
| 5138 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5139 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5140 | marker = *tmp; |
| 5141 | if ((marker != '\'') && (marker != '"')) |
| 5142 | goto error; |
| 5143 | tmp++; |
| 5144 | base = tmp; |
| 5145 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 5146 | if (*tmp == 0) |
| 5147 | goto error; |
| 5148 | URL = xmlStrndup(base, tmp - base); |
| 5149 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5150 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5151 | if (*tmp != 0) |
| 5152 | goto error; |
| 5153 | |
| 5154 | if (URL != NULL) { |
| 5155 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 5156 | xmlFree(URL); |
| 5157 | } |
| 5158 | return; |
| 5159 | |
| 5160 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5161 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 5162 | "Catalog PI syntax error: %s\n", |
| 5163 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5164 | if (URL != NULL) |
| 5165 | xmlFree(URL); |
| 5166 | } |
| 5167 | #endif |
| 5168 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5169 | /** |
| 5170 | * xmlParsePI: |
| 5171 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5172 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | * parse an XML Processing Instruction. |
| 5174 | * |
| 5175 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 5176 | * |
| 5177 | * The processing is transfered to SAX once parsed. |
| 5178 | */ |
| 5179 | |
| 5180 | void |
| 5181 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 5182 | xmlChar *buf = NULL; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5183 | size_t len = 0; |
| 5184 | size_t size = XML_PARSER_BUFFER_SIZE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5185 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5186 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5187 | xmlParserInputState state; |
| 5188 | int count = 0; |
| 5189 | |
| 5190 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 5191 | xmlParserInputPtr input = ctxt->input; |
| 5192 | state = ctxt->instate; |
| 5193 | ctxt->instate = XML_PARSER_PI; |
| 5194 | /* |
| 5195 | * this is a Processing Instruction. |
| 5196 | */ |
| 5197 | SKIP(2); |
| 5198 | SHRINK; |
| 5199 | |
| 5200 | /* |
| 5201 | * Parse the target name and check for special support like |
| 5202 | * namespace. |
| 5203 | */ |
| 5204 | target = xmlParsePITarget(ctxt); |
| 5205 | if (target != NULL) { |
| 5206 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5207 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5208 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5209 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5210 | } |
| 5211 | SKIP(2); |
| 5212 | |
| 5213 | /* |
| 5214 | * SAX: PI detected. |
| 5215 | */ |
| 5216 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5217 | (ctxt->sax->processingInstruction != NULL)) |
| 5218 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5219 | target, NULL); |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5220 | if (ctxt->instate != XML_PARSER_EOF) |
| 5221 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5222 | return; |
| 5223 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5224 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5225 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5226 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5227 | ctxt->instate = state; |
| 5228 | return; |
| 5229 | } |
| 5230 | cur = CUR; |
| 5231 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5232 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5233 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5234 | } |
| 5235 | SKIP_BLANKS; |
| 5236 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 5237 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5238 | ((cur != '?') || (NXT(1) != '>'))) { |
| 5239 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5240 | xmlChar *tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5241 | size_t new_size = size * 2; |
| 5242 | tmp = (xmlChar *) xmlRealloc(buf, new_size); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5243 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5244 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5245 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5246 | ctxt->instate = state; |
| 5247 | return; |
| 5248 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5249 | buf = tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5250 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5251 | } |
| 5252 | count++; |
| 5253 | if (count > 50) { |
| 5254 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5255 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5256 | xmlFree(buf); |
| 5257 | return; |
| 5258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5259 | count = 0; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5260 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5261 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5262 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5263 | "PI %s too big found", target); |
| 5264 | xmlFree(buf); |
| 5265 | ctxt->instate = state; |
| 5266 | return; |
| 5267 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5268 | } |
| 5269 | COPY_BUF(l,buf,len,cur); |
| 5270 | NEXTL(l); |
| 5271 | cur = CUR_CHAR(l); |
| 5272 | if (cur == 0) { |
| 5273 | SHRINK; |
| 5274 | GROW; |
| 5275 | cur = CUR_CHAR(l); |
| 5276 | } |
| 5277 | } |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5278 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5279 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5280 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5281 | "PI %s too big found", target); |
| 5282 | xmlFree(buf); |
| 5283 | ctxt->instate = state; |
| 5284 | return; |
| 5285 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5286 | buf[len] = 0; |
| 5287 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5288 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5289 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5290 | } else { |
| 5291 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5292 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5293 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5294 | } |
| 5295 | SKIP(2); |
| 5296 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5297 | #ifdef LIBXML_CATALOG_ENABLED |
| 5298 | if (((state == XML_PARSER_MISC) || |
| 5299 | (state == XML_PARSER_START)) && |
| 5300 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 5301 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 5302 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 5303 | (allow == XML_CATA_ALLOW_ALL)) |
| 5304 | xmlParseCatalogPI(ctxt, buf); |
| 5305 | } |
| 5306 | #endif |
| 5307 | |
| 5308 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | /* |
| 5310 | * SAX: PI detected. |
| 5311 | */ |
| 5312 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5313 | (ctxt->sax->processingInstruction != NULL)) |
| 5314 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5315 | target, buf); |
| 5316 | } |
| 5317 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5318 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5319 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5320 | } |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5321 | if (ctxt->instate != XML_PARSER_EOF) |
| 5322 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5323 | } |
| 5324 | } |
| 5325 | |
| 5326 | /** |
| 5327 | * xmlParseNotationDecl: |
| 5328 | * @ctxt: an XML parser context |
| 5329 | * |
| 5330 | * parse a notation declaration |
| 5331 | * |
| 5332 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 5333 | * |
| 5334 | * Hence there is actually 3 choices: |
| 5335 | * 'PUBLIC' S PubidLiteral |
| 5336 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 5337 | * and 'SYSTEM' S SystemLiteral |
| 5338 | * |
| 5339 | * See the NOTE on xmlParseExternalID(). |
| 5340 | */ |
| 5341 | |
| 5342 | void |
| 5343 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5344 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5345 | xmlChar *Pubid; |
| 5346 | xmlChar *Systemid; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5347 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5348 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5349 | xmlParserInputPtr input = ctxt->input; |
| 5350 | SHRINK; |
| 5351 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5352 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5353 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5354 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5355 | return; |
| 5356 | } |
| 5357 | SKIP_BLANKS; |
| 5358 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5359 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5360 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5361 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5362 | return; |
| 5363 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5364 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5365 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5366 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5367 | return; |
| 5368 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5369 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5370 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5371 | "colon are forbidden from notation names '%s'\n", |
| 5372 | name, NULL, NULL); |
| 5373 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5374 | SKIP_BLANKS; |
| 5375 | |
| 5376 | /* |
| 5377 | * Parse the IDs. |
| 5378 | */ |
| 5379 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 5380 | SKIP_BLANKS; |
| 5381 | |
| 5382 | if (RAW == '>') { |
| 5383 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5384 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5385 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5386 | } |
| 5387 | NEXT; |
| 5388 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5389 | (ctxt->sax->notationDecl != NULL)) |
| 5390 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 5391 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5392 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5393 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5394 | if (Systemid != NULL) xmlFree(Systemid); |
| 5395 | if (Pubid != NULL) xmlFree(Pubid); |
| 5396 | } |
| 5397 | } |
| 5398 | |
| 5399 | /** |
| 5400 | * xmlParseEntityDecl: |
| 5401 | * @ctxt: an XML parser context |
| 5402 | * |
| 5403 | * parse <!ENTITY declarations |
| 5404 | * |
| 5405 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 5406 | * |
| 5407 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 5408 | * |
| 5409 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 5410 | * |
| 5411 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 5412 | * |
| 5413 | * [74] PEDef ::= EntityValue | ExternalID |
| 5414 | * |
| 5415 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 5416 | * |
| 5417 | * [ VC: Notation Declared ] |
| 5418 | * The Name must match the declared name of a notation. |
| 5419 | */ |
| 5420 | |
| 5421 | void |
| 5422 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5423 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5424 | xmlChar *value = NULL; |
| 5425 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5426 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5427 | int isParameter = 0; |
| 5428 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5429 | int skipped; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5430 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5431 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5432 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5433 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5434 | SHRINK; |
| 5435 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5436 | skipped = SKIP_BLANKS; |
| 5437 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5438 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5439 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5440 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5441 | |
| 5442 | if (RAW == '%') { |
| 5443 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5444 | skipped = SKIP_BLANKS; |
| 5445 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5446 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5447 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5448 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5449 | isParameter = 1; |
| 5450 | } |
| 5451 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5452 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5453 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5454 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5455 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5456 | return; |
| 5457 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5458 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5459 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5460 | "colon are forbidden from entities names '%s'\n", |
| 5461 | name, NULL, NULL); |
| 5462 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5463 | skipped = SKIP_BLANKS; |
| 5464 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5465 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5466 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5467 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5468 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5469 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5470 | /* |
| 5471 | * handle the various case of definitions... |
| 5472 | */ |
| 5473 | if (isParameter) { |
| 5474 | if ((RAW == '"') || (RAW == '\'')) { |
| 5475 | value = xmlParseEntityValue(ctxt, &orig); |
| 5476 | if (value) { |
| 5477 | if ((ctxt->sax != NULL) && |
| 5478 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5479 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5480 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5481 | NULL, NULL, value); |
| 5482 | } |
| 5483 | } else { |
| 5484 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5485 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5486 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5487 | } |
| 5488 | if (URI) { |
| 5489 | xmlURIPtr uri; |
| 5490 | |
| 5491 | uri = xmlParseURI((const char *) URI); |
| 5492 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5493 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5494 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5495 | /* |
| 5496 | * This really ought to be a well formedness error |
| 5497 | * but the XML Core WG decided otherwise c.f. issue |
| 5498 | * E26 of the XML erratas. |
| 5499 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | } else { |
| 5501 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5502 | /* |
| 5503 | * Okay this is foolish to block those but not |
| 5504 | * invalid URIs. |
| 5505 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5506 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5507 | } else { |
| 5508 | if ((ctxt->sax != NULL) && |
| 5509 | (!ctxt->disableSAX) && |
| 5510 | (ctxt->sax->entityDecl != NULL)) |
| 5511 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5512 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5513 | literal, URI, NULL); |
| 5514 | } |
| 5515 | xmlFreeURI(uri); |
| 5516 | } |
| 5517 | } |
| 5518 | } |
| 5519 | } else { |
| 5520 | if ((RAW == '"') || (RAW == '\'')) { |
| 5521 | value = xmlParseEntityValue(ctxt, &orig); |
| 5522 | if ((ctxt->sax != NULL) && |
| 5523 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5524 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5525 | XML_INTERNAL_GENERAL_ENTITY, |
| 5526 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5527 | /* |
| 5528 | * For expat compatibility in SAX mode. |
| 5529 | */ |
| 5530 | if ((ctxt->myDoc == NULL) || |
| 5531 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5532 | if (ctxt->myDoc == NULL) { |
| 5533 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5534 | if (ctxt->myDoc == NULL) { |
| 5535 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5536 | return; |
| 5537 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5538 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5539 | } |
| 5540 | if (ctxt->myDoc->intSubset == NULL) |
| 5541 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5542 | BAD_CAST "fake", NULL, NULL); |
| 5543 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5544 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5545 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5546 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5547 | } else { |
| 5548 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5549 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5550 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5551 | } |
| 5552 | if (URI) { |
| 5553 | xmlURIPtr uri; |
| 5554 | |
| 5555 | uri = xmlParseURI((const char *)URI); |
| 5556 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5557 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5558 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5559 | /* |
| 5560 | * This really ought to be a well formedness error |
| 5561 | * but the XML Core WG decided otherwise c.f. issue |
| 5562 | * E26 of the XML erratas. |
| 5563 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5564 | } else { |
| 5565 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5566 | /* |
| 5567 | * Okay this is foolish to block those but not |
| 5568 | * invalid URIs. |
| 5569 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5570 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5571 | } |
| 5572 | xmlFreeURI(uri); |
| 5573 | } |
| 5574 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5575 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5576 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5577 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5578 | } |
| 5579 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5580 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5581 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5582 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5583 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5584 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5585 | } |
| 5586 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5587 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5588 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5589 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5590 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5591 | literal, URI, ndata); |
| 5592 | } else { |
| 5593 | if ((ctxt->sax != NULL) && |
| 5594 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5595 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5596 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5597 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5598 | /* |
| 5599 | * For expat compatibility in SAX mode. |
| 5600 | * assuming the entity repalcement was asked for |
| 5601 | */ |
| 5602 | if ((ctxt->replaceEntities != 0) && |
| 5603 | ((ctxt->myDoc == NULL) || |
| 5604 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5605 | if (ctxt->myDoc == NULL) { |
| 5606 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5607 | if (ctxt->myDoc == NULL) { |
| 5608 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5609 | return; |
| 5610 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5611 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | if (ctxt->myDoc->intSubset == NULL) |
| 5615 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5616 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5617 | xmlSAX2EntityDecl(ctxt, name, |
| 5618 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5619 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5620 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5621 | } |
| 5622 | } |
| 5623 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 5624 | if (ctxt->instate == XML_PARSER_EOF) |
| 5625 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5626 | SKIP_BLANKS; |
| 5627 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5628 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5629 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5630 | } else { |
| 5631 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5632 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5633 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5634 | } |
| 5635 | NEXT; |
| 5636 | } |
| 5637 | if (orig != NULL) { |
| 5638 | /* |
| 5639 | * Ugly mechanism to save the raw entity value. |
| 5640 | */ |
| 5641 | xmlEntityPtr cur = NULL; |
| 5642 | |
| 5643 | if (isParameter) { |
| 5644 | if ((ctxt->sax != NULL) && |
| 5645 | (ctxt->sax->getParameterEntity != NULL)) |
| 5646 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5647 | } else { |
| 5648 | if ((ctxt->sax != NULL) && |
| 5649 | (ctxt->sax->getEntity != NULL)) |
| 5650 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5651 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5652 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5653 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5654 | } |
| 5655 | if (cur != NULL) { |
| 5656 | if (cur->orig != NULL) |
| 5657 | xmlFree(orig); |
| 5658 | else |
| 5659 | cur->orig = orig; |
| 5660 | } else |
| 5661 | xmlFree(orig); |
| 5662 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | if (value != NULL) xmlFree(value); |
| 5664 | if (URI != NULL) xmlFree(URI); |
| 5665 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | /** |
| 5670 | * xmlParseDefaultDecl: |
| 5671 | * @ctxt: an XML parser context |
| 5672 | * @value: Receive a possible fixed default value for the attribute |
| 5673 | * |
| 5674 | * Parse an attribute default declaration |
| 5675 | * |
| 5676 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5677 | * |
| 5678 | * [ VC: Required Attribute ] |
| 5679 | * if the default declaration is the keyword #REQUIRED, then the |
| 5680 | * attribute must be specified for all elements of the type in the |
| 5681 | * attribute-list declaration. |
| 5682 | * |
| 5683 | * [ VC: Attribute Default Legal ] |
| 5684 | * The declared default value must meet the lexical constraints of |
| 5685 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5686 | * |
| 5687 | * [ VC: Fixed Attribute Default ] |
| 5688 | * if an attribute has a default value declared with the #FIXED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5689 | * keyword, instances of that attribute must match the default value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5690 | * |
| 5691 | * [ WFC: No < in Attribute Values ] |
| 5692 | * handled in xmlParseAttValue() |
| 5693 | * |
| 5694 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5695 | * or XML_ATTRIBUTE_FIXED. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5696 | */ |
| 5697 | |
| 5698 | int |
| 5699 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5700 | int val; |
| 5701 | xmlChar *ret; |
| 5702 | |
| 5703 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5704 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5705 | SKIP(9); |
| 5706 | return(XML_ATTRIBUTE_REQUIRED); |
| 5707 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5708 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5709 | SKIP(8); |
| 5710 | return(XML_ATTRIBUTE_IMPLIED); |
| 5711 | } |
| 5712 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5713 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5714 | SKIP(6); |
| 5715 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5716 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5717 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5718 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | } |
| 5720 | SKIP_BLANKS; |
| 5721 | } |
| 5722 | ret = xmlParseAttValue(ctxt); |
| 5723 | ctxt->instate = XML_PARSER_DTD; |
| 5724 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5725 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5726 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5727 | } else |
| 5728 | *value = ret; |
| 5729 | return(val); |
| 5730 | } |
| 5731 | |
| 5732 | /** |
| 5733 | * xmlParseNotationType: |
| 5734 | * @ctxt: an XML parser context |
| 5735 | * |
| 5736 | * parse an Notation attribute type. |
| 5737 | * |
| 5738 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5739 | * |
| 5740 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5741 | * |
| 5742 | * [ VC: Notation Attributes ] |
| 5743 | * Values of this type must match one of the notation names included |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5744 | * in the declaration; all notation names in the declaration must be declared. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5745 | * |
| 5746 | * Returns: the notation attribute tree built while parsing |
| 5747 | */ |
| 5748 | |
| 5749 | xmlEnumerationPtr |
| 5750 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5751 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5752 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5753 | |
| 5754 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5755 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5756 | return(NULL); |
| 5757 | } |
| 5758 | SHRINK; |
| 5759 | do { |
| 5760 | NEXT; |
| 5761 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5762 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5763 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5764 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5765 | "Name expected in NOTATION declaration\n"); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5766 | xmlFreeEnumeration(ret); |
| 5767 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5768 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5769 | tmp = ret; |
| 5770 | while (tmp != NULL) { |
| 5771 | if (xmlStrEqual(name, tmp->name)) { |
| 5772 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5773 | "standalone: attribute notation value token %s duplicated\n", |
| 5774 | name, NULL); |
| 5775 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5776 | xmlFree((xmlChar *) name); |
| 5777 | break; |
| 5778 | } |
| 5779 | tmp = tmp->next; |
| 5780 | } |
| 5781 | if (tmp == NULL) { |
| 5782 | cur = xmlCreateEnumeration(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5783 | if (cur == NULL) { |
| 5784 | xmlFreeEnumeration(ret); |
| 5785 | return(NULL); |
| 5786 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5787 | if (last == NULL) ret = last = cur; |
| 5788 | else { |
| 5789 | last->next = cur; |
| 5790 | last = cur; |
| 5791 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5792 | } |
| 5793 | SKIP_BLANKS; |
| 5794 | } while (RAW == '|'); |
| 5795 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5796 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5797 | xmlFreeEnumeration(ret); |
| 5798 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5799 | } |
| 5800 | NEXT; |
| 5801 | return(ret); |
| 5802 | } |
| 5803 | |
| 5804 | /** |
| 5805 | * xmlParseEnumerationType: |
| 5806 | * @ctxt: an XML parser context |
| 5807 | * |
| 5808 | * parse an Enumeration attribute type. |
| 5809 | * |
| 5810 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5811 | * |
| 5812 | * [ VC: Enumeration ] |
| 5813 | * Values of this type must match one of the Nmtoken tokens in |
| 5814 | * the declaration |
| 5815 | * |
| 5816 | * Returns: the enumeration attribute tree built while parsing |
| 5817 | */ |
| 5818 | |
| 5819 | xmlEnumerationPtr |
| 5820 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5821 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5822 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5823 | |
| 5824 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5825 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | return(NULL); |
| 5827 | } |
| 5828 | SHRINK; |
| 5829 | do { |
| 5830 | NEXT; |
| 5831 | SKIP_BLANKS; |
| 5832 | name = xmlParseNmtoken(ctxt); |
| 5833 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5834 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5835 | return(ret); |
| 5836 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5837 | tmp = ret; |
| 5838 | while (tmp != NULL) { |
| 5839 | if (xmlStrEqual(name, tmp->name)) { |
| 5840 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5841 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5842 | name, NULL); |
| 5843 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5844 | xmlFree(name); |
| 5845 | break; |
| 5846 | } |
| 5847 | tmp = tmp->next; |
| 5848 | } |
| 5849 | if (tmp == NULL) { |
| 5850 | cur = xmlCreateEnumeration(name); |
| 5851 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5852 | xmlFree(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5853 | if (cur == NULL) { |
| 5854 | xmlFreeEnumeration(ret); |
| 5855 | return(NULL); |
| 5856 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5857 | if (last == NULL) ret = last = cur; |
| 5858 | else { |
| 5859 | last->next = cur; |
| 5860 | last = cur; |
| 5861 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5862 | } |
| 5863 | SKIP_BLANKS; |
| 5864 | } while (RAW == '|'); |
| 5865 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5866 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5867 | return(ret); |
| 5868 | } |
| 5869 | NEXT; |
| 5870 | return(ret); |
| 5871 | } |
| 5872 | |
| 5873 | /** |
| 5874 | * xmlParseEnumeratedType: |
| 5875 | * @ctxt: an XML parser context |
| 5876 | * @tree: the enumeration tree built while parsing |
| 5877 | * |
| 5878 | * parse an Enumerated attribute type. |
| 5879 | * |
| 5880 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5881 | * |
| 5882 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5883 | * |
| 5884 | * |
| 5885 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5886 | */ |
| 5887 | |
| 5888 | int |
| 5889 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5890 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5891 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5892 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5893 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5894 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5895 | return(0); |
| 5896 | } |
| 5897 | SKIP_BLANKS; |
| 5898 | *tree = xmlParseNotationType(ctxt); |
| 5899 | if (*tree == NULL) return(0); |
| 5900 | return(XML_ATTRIBUTE_NOTATION); |
| 5901 | } |
| 5902 | *tree = xmlParseEnumerationType(ctxt); |
| 5903 | if (*tree == NULL) return(0); |
| 5904 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5905 | } |
| 5906 | |
| 5907 | /** |
| 5908 | * xmlParseAttributeType: |
| 5909 | * @ctxt: an XML parser context |
| 5910 | * @tree: the enumeration tree built while parsing |
| 5911 | * |
| 5912 | * parse the Attribute list def for an element |
| 5913 | * |
| 5914 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5915 | * |
| 5916 | * [55] StringType ::= 'CDATA' |
| 5917 | * |
| 5918 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5919 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5920 | * |
| 5921 | * Validity constraints for attribute values syntax are checked in |
| 5922 | * xmlValidateAttributeValue() |
| 5923 | * |
| 5924 | * [ VC: ID ] |
| 5925 | * Values of type ID must match the Name production. A name must not |
| 5926 | * appear more than once in an XML document as a value of this type; |
| 5927 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5928 | * |
| 5929 | * [ VC: One ID per Element Type ] |
| 5930 | * No element type may have more than one ID attribute specified. |
| 5931 | * |
| 5932 | * [ VC: ID Attribute Default ] |
| 5933 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5934 | * |
| 5935 | * [ VC: IDREF ] |
| 5936 | * Values of type IDREF must match the Name production, and values |
| 5937 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5938 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5939 | * values must match the value of some ID attribute. |
| 5940 | * |
| 5941 | * [ VC: Entity Name ] |
| 5942 | * Values of type ENTITY must match the Name production, values |
| 5943 | * of type ENTITIES must match Names; each Entity Name must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5944 | * name of an unparsed entity declared in the DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | * |
| 5946 | * [ VC: Name Token ] |
| 5947 | * Values of type NMTOKEN must match the Nmtoken production; values |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5948 | * of type NMTOKENS must match Nmtokens. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | * |
| 5950 | * Returns the attribute type |
| 5951 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5952 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5953 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5954 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5955 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5956 | SKIP(5); |
| 5957 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5958 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5959 | SKIP(6); |
| 5960 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5961 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5962 | SKIP(5); |
| 5963 | return(XML_ATTRIBUTE_IDREF); |
| 5964 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5965 | SKIP(2); |
| 5966 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5967 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5968 | SKIP(6); |
| 5969 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5970 | } 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] | 5971 | SKIP(8); |
| 5972 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5973 | } 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] | 5974 | SKIP(8); |
| 5975 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5976 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5977 | SKIP(7); |
| 5978 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5979 | } |
| 5980 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5981 | } |
| 5982 | |
| 5983 | /** |
| 5984 | * xmlParseAttributeListDecl: |
| 5985 | * @ctxt: an XML parser context |
| 5986 | * |
| 5987 | * : parse the Attribute list def for an element |
| 5988 | * |
| 5989 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5990 | * |
| 5991 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5992 | * |
| 5993 | */ |
| 5994 | void |
| 5995 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5996 | const xmlChar *elemName; |
| 5997 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5998 | xmlEnumerationPtr tree; |
| 5999 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6000 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6001 | xmlParserInputPtr input = ctxt->input; |
| 6002 | |
| 6003 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6004 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6005 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6006 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6007 | } |
| 6008 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6009 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6010 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6011 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6012 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6013 | return; |
| 6014 | } |
| 6015 | SKIP_BLANKS; |
| 6016 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6017 | while ((RAW != '>') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | const xmlChar *check = CUR_PTR; |
| 6019 | int type; |
| 6020 | int def; |
| 6021 | xmlChar *defaultValue = NULL; |
| 6022 | |
| 6023 | GROW; |
| 6024 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6025 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6026 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6027 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6028 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6029 | break; |
| 6030 | } |
| 6031 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6032 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6033 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6034 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6035 | break; |
| 6036 | } |
| 6037 | SKIP_BLANKS; |
| 6038 | |
| 6039 | type = xmlParseAttributeType(ctxt, &tree); |
| 6040 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6041 | break; |
| 6042 | } |
| 6043 | |
| 6044 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6045 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6046 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6047 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6048 | if (tree != NULL) |
| 6049 | xmlFreeEnumeration(tree); |
| 6050 | break; |
| 6051 | } |
| 6052 | SKIP_BLANKS; |
| 6053 | |
| 6054 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 6055 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6056 | if (defaultValue != NULL) |
| 6057 | xmlFree(defaultValue); |
| 6058 | if (tree != NULL) |
| 6059 | xmlFreeEnumeration(tree); |
| 6060 | break; |
| 6061 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 6062 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 6063 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6064 | |
| 6065 | GROW; |
| 6066 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6067 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6068 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6070 | if (defaultValue != NULL) |
| 6071 | xmlFree(defaultValue); |
| 6072 | if (tree != NULL) |
| 6073 | xmlFreeEnumeration(tree); |
| 6074 | break; |
| 6075 | } |
| 6076 | SKIP_BLANKS; |
| 6077 | } |
| 6078 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6079 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6080 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6081 | if (defaultValue != NULL) |
| 6082 | xmlFree(defaultValue); |
| 6083 | if (tree != NULL) |
| 6084 | xmlFreeEnumeration(tree); |
| 6085 | break; |
| 6086 | } |
| 6087 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 6088 | (ctxt->sax->attributeDecl != NULL)) |
| 6089 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 6090 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6091 | else if (tree != NULL) |
| 6092 | xmlFreeEnumeration(tree); |
| 6093 | |
| 6094 | if ((ctxt->sax2) && (defaultValue != NULL) && |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6095 | (def != XML_ATTRIBUTE_IMPLIED) && |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6096 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 6097 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 6098 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 6099 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6100 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 6101 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | if (defaultValue != NULL) |
| 6103 | xmlFree(defaultValue); |
| 6104 | GROW; |
| 6105 | } |
| 6106 | if (RAW == '>') { |
| 6107 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6108 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6109 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 6110 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6111 | } |
| 6112 | NEXT; |
| 6113 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6114 | } |
| 6115 | } |
| 6116 | |
| 6117 | /** |
| 6118 | * xmlParseElementMixedContentDecl: |
| 6119 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6120 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6121 | * |
| 6122 | * parse the declaration for a Mixed Element content |
| 6123 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6124 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 6126 | * '(' S? '#PCDATA' S? ')' |
| 6127 | * |
| 6128 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 6129 | * |
| 6130 | * [ VC: No Duplicate Types ] |
| 6131 | * The same name must not appear more than once in a single |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6132 | * mixed-content declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6133 | * |
| 6134 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 6135 | */ |
| 6136 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6137 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6138 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6139 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6140 | |
| 6141 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6142 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6143 | SKIP(7); |
| 6144 | SKIP_BLANKS; |
| 6145 | SHRINK; |
| 6146 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6147 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6148 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6149 | "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] | 6150 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6151 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6153 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6154 | if (ret == NULL) |
| 6155 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6156 | if (RAW == '*') { |
| 6157 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6158 | NEXT; |
| 6159 | } |
| 6160 | return(ret); |
| 6161 | } |
| 6162 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6163 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6164 | if (ret == NULL) return(NULL); |
| 6165 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6166 | while ((RAW == '|') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6167 | NEXT; |
| 6168 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6169 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | if (ret == NULL) return(NULL); |
| 6171 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6172 | if (cur != NULL) |
| 6173 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6174 | cur = ret; |
| 6175 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6176 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6177 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6178 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6179 | if (n->c1 != NULL) |
| 6180 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6181 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6182 | if (n != NULL) |
| 6183 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6184 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6185 | } |
| 6186 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6187 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6188 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6189 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6190 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6191 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6192 | return(NULL); |
| 6193 | } |
| 6194 | SKIP_BLANKS; |
| 6195 | GROW; |
| 6196 | } |
| 6197 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 6198 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6199 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6200 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6201 | if (cur->c2 != NULL) |
| 6202 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6203 | } |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6204 | if (ret != NULL) |
| 6205 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6206 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6207 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6208 | "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] | 6209 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6210 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6211 | SKIP(2); |
| 6212 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6213 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6214 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6215 | return(NULL); |
| 6216 | } |
| 6217 | |
| 6218 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6219 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6220 | } |
| 6221 | return(ret); |
| 6222 | } |
| 6223 | |
| 6224 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6225 | * xmlParseElementChildrenContentDeclPriv: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6226 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6227 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6228 | * @depth: the level of recursion |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6229 | * |
| 6230 | * parse the declaration for a Mixed Element content |
| 6231 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6232 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | * |
| 6234 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6235 | * |
| 6236 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6237 | * |
| 6238 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6239 | * |
| 6240 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6241 | * |
| 6242 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6243 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6244 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6245 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6246 | * construct is contained in the replacement text for a parameter |
| 6247 | * entity, both must be contained in the same replacement text. For |
| 6248 | * interoperability, if a parameter-entity reference appears in a |
| 6249 | * choice, seq, or Mixed construct, its replacement text should not |
| 6250 | * be empty, and neither the first nor last non-blank character of |
| 6251 | * the replacement text should be a connector (| or ,). |
| 6252 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6253 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | * hierarchy. |
| 6255 | */ |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6256 | static xmlElementContentPtr |
| 6257 | xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk, |
| 6258 | int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6259 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6260 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | xmlChar type = 0; |
| 6262 | |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6263 | if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 6264 | (depth > 2048)) { |
| 6265 | xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, |
| 6266 | "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n", |
| 6267 | depth); |
| 6268 | return(NULL); |
| 6269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6270 | SKIP_BLANKS; |
| 6271 | GROW; |
| 6272 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6273 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6274 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6275 | /* Recurse on first child */ |
| 6276 | NEXT; |
| 6277 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6278 | cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6279 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6280 | SKIP_BLANKS; |
| 6281 | GROW; |
| 6282 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6283 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6284 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6285 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6286 | return(NULL); |
| 6287 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6288 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6289 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6290 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6291 | return(NULL); |
| 6292 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | GROW; |
| 6294 | if (RAW == '?') { |
| 6295 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6296 | NEXT; |
| 6297 | } else if (RAW == '*') { |
| 6298 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6299 | NEXT; |
| 6300 | } else if (RAW == '+') { |
| 6301 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6302 | NEXT; |
| 6303 | } else { |
| 6304 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6305 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6306 | GROW; |
| 6307 | } |
| 6308 | SKIP_BLANKS; |
| 6309 | SHRINK; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6310 | while ((RAW != ')') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6311 | /* |
| 6312 | * Each loop we parse one separator and one element. |
| 6313 | */ |
| 6314 | if (RAW == ',') { |
| 6315 | if (type == 0) type = CUR; |
| 6316 | |
| 6317 | /* |
| 6318 | * Detect "Name | Name , Name" error |
| 6319 | */ |
| 6320 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6321 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6322 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6323 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6324 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6325 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6326 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6327 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6328 | return(NULL); |
| 6329 | } |
| 6330 | NEXT; |
| 6331 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6332 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6333 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6334 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6335 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 6336 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6337 | return(NULL); |
| 6338 | } |
| 6339 | if (last == NULL) { |
| 6340 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6341 | if (ret != NULL) |
| 6342 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6343 | ret = cur = op; |
| 6344 | } else { |
| 6345 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6346 | if (op != NULL) |
| 6347 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6348 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6349 | if (last != NULL) |
| 6350 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6351 | cur =op; |
| 6352 | last = NULL; |
| 6353 | } |
| 6354 | } else if (RAW == '|') { |
| 6355 | if (type == 0) type = CUR; |
| 6356 | |
| 6357 | /* |
| 6358 | * Detect "Name , Name | Name" error |
| 6359 | */ |
| 6360 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6361 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6362 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6363 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6364 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6365 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6366 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6367 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6368 | return(NULL); |
| 6369 | } |
| 6370 | NEXT; |
| 6371 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6372 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6373 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6374 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6375 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6376 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6377 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6378 | return(NULL); |
| 6379 | } |
| 6380 | if (last == NULL) { |
| 6381 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6382 | if (ret != NULL) |
| 6383 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6384 | ret = cur = op; |
| 6385 | } else { |
| 6386 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6387 | if (op != NULL) |
| 6388 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6389 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6390 | if (last != NULL) |
| 6391 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6392 | cur =op; |
| 6393 | last = NULL; |
| 6394 | } |
| 6395 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6396 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 6397 | if ((last != NULL) && (last != ret)) |
| 6398 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6400 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | return(NULL); |
| 6402 | } |
| 6403 | GROW; |
| 6404 | SKIP_BLANKS; |
| 6405 | GROW; |
| 6406 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6407 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6408 | /* Recurse on second child */ |
| 6409 | NEXT; |
| 6410 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6411 | last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6412 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6413 | SKIP_BLANKS; |
| 6414 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6415 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6416 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6417 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6418 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6419 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6420 | return(NULL); |
| 6421 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6422 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6423 | if (last == NULL) { |
| 6424 | if (ret != NULL) |
| 6425 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 6426 | return(NULL); |
| 6427 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6428 | if (RAW == '?') { |
| 6429 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6430 | NEXT; |
| 6431 | } else if (RAW == '*') { |
| 6432 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6433 | NEXT; |
| 6434 | } else if (RAW == '+') { |
| 6435 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6436 | NEXT; |
| 6437 | } else { |
| 6438 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6439 | } |
| 6440 | } |
| 6441 | SKIP_BLANKS; |
| 6442 | GROW; |
| 6443 | } |
| 6444 | if ((cur != NULL) && (last != NULL)) { |
| 6445 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6446 | if (last != NULL) |
| 6447 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6448 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6449 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6450 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6451 | "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] | 6452 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6453 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6454 | NEXT; |
| 6455 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6456 | if (ret != NULL) { |
| 6457 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 6458 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6459 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6460 | else |
| 6461 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6462 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6463 | NEXT; |
| 6464 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6465 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6466 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6467 | cur = ret; |
| 6468 | /* |
| 6469 | * Some normalization: |
| 6470 | * (a | b* | c?)* == (a | b | c)* |
| 6471 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6472 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6473 | if ((cur->c1 != NULL) && |
| 6474 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6475 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6476 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6477 | if ((cur->c2 != NULL) && |
| 6478 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6479 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6480 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6481 | cur = cur->c2; |
| 6482 | } |
| 6483 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6484 | NEXT; |
| 6485 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6486 | if (ret != NULL) { |
| 6487 | int found = 0; |
| 6488 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6489 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6490 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6491 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6492 | else |
| 6493 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6494 | /* |
| 6495 | * Some normalization: |
| 6496 | * (a | b*)+ == (a | b)* |
| 6497 | * (a | b?)+ == (a | b)* |
| 6498 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6499 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6500 | if ((cur->c1 != NULL) && |
| 6501 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6502 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6503 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6504 | found = 1; |
| 6505 | } |
| 6506 | if ((cur->c2 != NULL) && |
| 6507 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6508 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6509 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6510 | found = 1; |
| 6511 | } |
| 6512 | cur = cur->c2; |
| 6513 | } |
| 6514 | if (found) |
| 6515 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6516 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6517 | NEXT; |
| 6518 | } |
| 6519 | return(ret); |
| 6520 | } |
| 6521 | |
| 6522 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6523 | * xmlParseElementChildrenContentDecl: |
| 6524 | * @ctxt: an XML parser context |
| 6525 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6526 | * |
| 6527 | * parse the declaration for a Mixed Element content |
| 6528 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 6529 | * |
| 6530 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6531 | * |
| 6532 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6533 | * |
| 6534 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6535 | * |
| 6536 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6537 | * |
| 6538 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6539 | * TODO Parameter-entity replacement text must be properly nested |
| 6540 | * with parenthesized groups. That is to say, if either of the |
| 6541 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6542 | * construct is contained in the replacement text for a parameter |
| 6543 | * entity, both must be contained in the same replacement text. For |
| 6544 | * interoperability, if a parameter-entity reference appears in a |
| 6545 | * choice, seq, or Mixed construct, its replacement text should not |
| 6546 | * be empty, and neither the first nor last non-blank character of |
| 6547 | * the replacement text should be a connector (| or ,). |
| 6548 | * |
| 6549 | * Returns the tree of xmlElementContentPtr describing the element |
| 6550 | * hierarchy. |
| 6551 | */ |
| 6552 | xmlElementContentPtr |
| 6553 | xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
| 6554 | /* stub left for API/ABI compat */ |
| 6555 | return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1)); |
| 6556 | } |
| 6557 | |
| 6558 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6559 | * xmlParseElementContentDecl: |
| 6560 | * @ctxt: an XML parser context |
| 6561 | * @name: the name of the element being defined. |
| 6562 | * @result: the Element Content pointer will be stored here if any |
| 6563 | * |
| 6564 | * parse the declaration for an Element content either Mixed or Children, |
| 6565 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6566 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6567 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6568 | * |
| 6569 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6570 | */ |
| 6571 | |
| 6572 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6573 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6574 | xmlElementContentPtr *result) { |
| 6575 | |
| 6576 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6577 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6578 | int res; |
| 6579 | |
| 6580 | *result = NULL; |
| 6581 | |
| 6582 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6583 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6584 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6585 | return(-1); |
| 6586 | } |
| 6587 | NEXT; |
| 6588 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6589 | if (ctxt->instate == XML_PARSER_EOF) |
| 6590 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6591 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6592 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6593 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6594 | res = XML_ELEMENT_TYPE_MIXED; |
| 6595 | } else { |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6596 | tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6597 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6598 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6599 | SKIP_BLANKS; |
| 6600 | *result = tree; |
| 6601 | return(res); |
| 6602 | } |
| 6603 | |
| 6604 | /** |
| 6605 | * xmlParseElementDecl: |
| 6606 | * @ctxt: an XML parser context |
| 6607 | * |
| 6608 | * parse an Element declaration. |
| 6609 | * |
| 6610 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6611 | * |
| 6612 | * [ VC: Unique Element Type Declaration ] |
| 6613 | * No element type may be declared more than once |
| 6614 | * |
| 6615 | * Returns the type of the element, or -1 in case of error |
| 6616 | */ |
| 6617 | int |
| 6618 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6619 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | int ret = -1; |
| 6621 | xmlElementContentPtr content = NULL; |
| 6622 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6623 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6624 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6625 | xmlParserInputPtr input = ctxt->input; |
| 6626 | |
| 6627 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6628 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6629 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6630 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6631 | } |
| 6632 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6633 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6634 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6635 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6636 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6637 | return(-1); |
| 6638 | } |
| 6639 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6640 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6641 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6642 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6643 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | } |
| 6645 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6646 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6647 | SKIP(5); |
| 6648 | /* |
| 6649 | * Element must always be empty. |
| 6650 | */ |
| 6651 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6652 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6653 | (NXT(2) == 'Y')) { |
| 6654 | SKIP(3); |
| 6655 | /* |
| 6656 | * Element is a generic container. |
| 6657 | */ |
| 6658 | ret = XML_ELEMENT_TYPE_ANY; |
| 6659 | } else if (RAW == '(') { |
| 6660 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6661 | } else { |
| 6662 | /* |
| 6663 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6664 | */ |
| 6665 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6666 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6667 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6668 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6669 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6670 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6671 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6672 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6673 | return(-1); |
| 6674 | } |
| 6675 | |
| 6676 | SKIP_BLANKS; |
| 6677 | /* |
| 6678 | * Pop-up of finished entities. |
| 6679 | */ |
| 6680 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6681 | xmlPopInput(ctxt); |
| 6682 | SKIP_BLANKS; |
| 6683 | |
| 6684 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6685 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6686 | if (content != NULL) { |
| 6687 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6688 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6689 | } else { |
| 6690 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6691 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6692 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6693 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6694 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6695 | NEXT; |
| 6696 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6697 | (ctxt->sax->elementDecl != NULL)) { |
| 6698 | if (content != NULL) |
| 6699 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6701 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6702 | if ((content != NULL) && (content->parent == NULL)) { |
| 6703 | /* |
| 6704 | * this is a trick: if xmlAddElementDecl is called, |
| 6705 | * instead of copying the full tree it is plugged directly |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6706 | * if called from the parser. Avoid duplicating the |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6707 | * interfaces or change the API/ABI |
| 6708 | */ |
| 6709 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6710 | } |
| 6711 | } else if (content != NULL) { |
| 6712 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6713 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6714 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6715 | } |
| 6716 | return(ret); |
| 6717 | } |
| 6718 | |
| 6719 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6720 | * xmlParseConditionalSections |
| 6721 | * @ctxt: an XML parser context |
| 6722 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6723 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6724 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6725 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6726 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6727 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6728 | */ |
| 6729 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6730 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6731 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6732 | int id = ctxt->input->id; |
| 6733 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6734 | SKIP(3); |
| 6735 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6736 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6737 | SKIP(7); |
| 6738 | SKIP_BLANKS; |
| 6739 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6740 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6741 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6742 | if (ctxt->input->id != id) { |
| 6743 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6744 | "All markup of the conditional section is not in the same entity\n", |
| 6745 | NULL, NULL); |
| 6746 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6747 | NEXT; |
| 6748 | } |
| 6749 | if (xmlParserDebugEntities) { |
| 6750 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6751 | xmlGenericError(xmlGenericErrorContext, |
| 6752 | "%s(%d): ", ctxt->input->filename, |
| 6753 | ctxt->input->line); |
| 6754 | xmlGenericError(xmlGenericErrorContext, |
| 6755 | "Entering INCLUDE Conditional Section\n"); |
| 6756 | } |
| 6757 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6758 | while (((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6759 | (NXT(2) != '>'))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6760 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6761 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6762 | |
| 6763 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6764 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6765 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6766 | NEXT; |
| 6767 | } else if (RAW == '%') { |
| 6768 | xmlParsePEReference(ctxt); |
| 6769 | } else |
| 6770 | xmlParseMarkupDecl(ctxt); |
| 6771 | |
| 6772 | /* |
| 6773 | * Pop-up of finished entities. |
| 6774 | */ |
| 6775 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6776 | xmlPopInput(ctxt); |
| 6777 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6778 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6779 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6780 | break; |
| 6781 | } |
| 6782 | } |
| 6783 | if (xmlParserDebugEntities) { |
| 6784 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6785 | xmlGenericError(xmlGenericErrorContext, |
| 6786 | "%s(%d): ", ctxt->input->filename, |
| 6787 | ctxt->input->line); |
| 6788 | xmlGenericError(xmlGenericErrorContext, |
| 6789 | "Leaving INCLUDE Conditional Section\n"); |
| 6790 | } |
| 6791 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6792 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6793 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6794 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6795 | int depth = 0; |
| 6796 | |
| 6797 | SKIP(6); |
| 6798 | SKIP_BLANKS; |
| 6799 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6800 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6801 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6802 | if (ctxt->input->id != id) { |
| 6803 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6804 | "All markup of the conditional section is not in the same entity\n", |
| 6805 | NULL, NULL); |
| 6806 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6807 | NEXT; |
| 6808 | } |
| 6809 | if (xmlParserDebugEntities) { |
| 6810 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6811 | xmlGenericError(xmlGenericErrorContext, |
| 6812 | "%s(%d): ", ctxt->input->filename, |
| 6813 | ctxt->input->line); |
| 6814 | xmlGenericError(xmlGenericErrorContext, |
| 6815 | "Entering IGNORE Conditional Section\n"); |
| 6816 | } |
| 6817 | |
| 6818 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6819 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6820 | * But disable SAX event generating DTD building in the meantime |
| 6821 | */ |
| 6822 | state = ctxt->disableSAX; |
| 6823 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6824 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6825 | ctxt->instate = XML_PARSER_IGNORE; |
| 6826 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6827 | while (((depth >= 0) && (RAW != 0)) && |
| 6828 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6829 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6830 | depth++; |
| 6831 | SKIP(3); |
| 6832 | continue; |
| 6833 | } |
| 6834 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6835 | if (--depth >= 0) SKIP(3); |
| 6836 | continue; |
| 6837 | } |
| 6838 | NEXT; |
| 6839 | continue; |
| 6840 | } |
| 6841 | |
| 6842 | ctxt->disableSAX = state; |
| 6843 | ctxt->instate = instate; |
| 6844 | |
| 6845 | if (xmlParserDebugEntities) { |
| 6846 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6847 | xmlGenericError(xmlGenericErrorContext, |
| 6848 | "%s(%d): ", ctxt->input->filename, |
| 6849 | ctxt->input->line); |
| 6850 | xmlGenericError(xmlGenericErrorContext, |
| 6851 | "Leaving IGNORE Conditional Section\n"); |
| 6852 | } |
| 6853 | |
| 6854 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6855 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6856 | } |
| 6857 | |
| 6858 | if (RAW == 0) |
| 6859 | SHRINK; |
| 6860 | |
| 6861 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6862 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6863 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6864 | if (ctxt->input->id != id) { |
| 6865 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6866 | "All markup of the conditional section is not in the same entity\n", |
| 6867 | NULL, NULL); |
| 6868 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6869 | SKIP(3); |
| 6870 | } |
| 6871 | } |
| 6872 | |
| 6873 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6874 | * xmlParseMarkupDecl: |
| 6875 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6876 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6877 | * parse Markup declarations |
| 6878 | * |
| 6879 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6880 | * NotationDecl | PI | Comment |
| 6881 | * |
| 6882 | * [ VC: Proper Declaration/PE Nesting ] |
| 6883 | * Parameter-entity replacement text must be properly nested with |
| 6884 | * markup declarations. That is to say, if either the first character |
| 6885 | * or the last character of a markup declaration (markupdecl above) is |
| 6886 | * contained in the replacement text for a parameter-entity reference, |
| 6887 | * both must be contained in the same replacement text. |
| 6888 | * |
| 6889 | * [ WFC: PEs in Internal Subset ] |
| 6890 | * In the internal DTD subset, parameter-entity references can occur |
| 6891 | * only where markup declarations can occur, not within markup declarations. |
| 6892 | * (This does not apply to references that occur in external parameter |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6893 | * entities or to the external subset.) |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6894 | */ |
| 6895 | void |
| 6896 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6897 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6898 | if (CUR == '<') { |
| 6899 | if (NXT(1) == '!') { |
| 6900 | switch (NXT(2)) { |
| 6901 | case 'E': |
| 6902 | if (NXT(3) == 'L') |
| 6903 | xmlParseElementDecl(ctxt); |
| 6904 | else if (NXT(3) == 'N') |
| 6905 | xmlParseEntityDecl(ctxt); |
| 6906 | break; |
| 6907 | case 'A': |
| 6908 | xmlParseAttributeListDecl(ctxt); |
| 6909 | break; |
| 6910 | case 'N': |
| 6911 | xmlParseNotationDecl(ctxt); |
| 6912 | break; |
| 6913 | case '-': |
| 6914 | xmlParseComment(ctxt); |
| 6915 | break; |
| 6916 | default: |
| 6917 | /* there is an error but it will be detected later */ |
| 6918 | break; |
| 6919 | } |
| 6920 | } else if (NXT(1) == '?') { |
| 6921 | xmlParsePI(ctxt); |
| 6922 | } |
| 6923 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6924 | /* |
| 6925 | * This is only for internal subset. On external entities, |
| 6926 | * the replacement is done before parsing stage |
| 6927 | */ |
| 6928 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6929 | xmlParsePEReference(ctxt); |
| 6930 | |
| 6931 | /* |
| 6932 | * Conditional sections are allowed from entities included |
| 6933 | * by PE References in the internal subset. |
| 6934 | */ |
| 6935 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6936 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6937 | xmlParseConditionalSections(ctxt); |
| 6938 | } |
| 6939 | } |
| 6940 | |
| 6941 | ctxt->instate = XML_PARSER_DTD; |
| 6942 | } |
| 6943 | |
| 6944 | /** |
| 6945 | * xmlParseTextDecl: |
| 6946 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6947 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6948 | * parse an XML declaration header for external entities |
| 6949 | * |
| 6950 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6951 | */ |
| 6952 | |
| 6953 | void |
| 6954 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6955 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6956 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6957 | |
| 6958 | /* |
| 6959 | * We know that '<?xml' is here. |
| 6960 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6961 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6962 | SKIP(5); |
| 6963 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6964 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6965 | return; |
| 6966 | } |
| 6967 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6968 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6969 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6970 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6971 | } |
| 6972 | SKIP_BLANKS; |
| 6973 | |
| 6974 | /* |
| 6975 | * We may have the VersionInfo here. |
| 6976 | */ |
| 6977 | version = xmlParseVersionInfo(ctxt); |
| 6978 | if (version == NULL) |
| 6979 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6980 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6981 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6982 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6983 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6984 | } |
| 6985 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6986 | ctxt->input->version = version; |
| 6987 | |
| 6988 | /* |
| 6989 | * We must have the encoding declaration |
| 6990 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6991 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6992 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6993 | /* |
| 6994 | * The XML REC instructs us to stop parsing right here |
| 6995 | */ |
| 6996 | return; |
| 6997 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6998 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 6999 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 7000 | "Missing encoding in text declaration\n"); |
| 7001 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7002 | |
| 7003 | SKIP_BLANKS; |
| 7004 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 7005 | SKIP(2); |
| 7006 | } else if (RAW == '>') { |
| 7007 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7008 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7009 | NEXT; |
| 7010 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7011 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 7012 | MOVETO_ENDTAG(CUR_PTR); |
| 7013 | NEXT; |
| 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7018 | * xmlParseExternalSubset: |
| 7019 | * @ctxt: an XML parser context |
| 7020 | * @ExternalID: the external identifier |
| 7021 | * @SystemID: the system identifier (or URL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7022 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7023 | * parse Markup declarations from an external subset |
| 7024 | * |
| 7025 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 7026 | * |
| 7027 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 7028 | */ |
| 7029 | void |
| 7030 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 7031 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 7032 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7033 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7034 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 7035 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 7036 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 7037 | xmlChar start[4]; |
| 7038 | xmlCharEncoding enc; |
| 7039 | |
| 7040 | start[0] = RAW; |
| 7041 | start[1] = NXT(1); |
| 7042 | start[2] = NXT(2); |
| 7043 | start[3] = NXT(3); |
| 7044 | enc = xmlDetectCharEncoding(start, 4); |
| 7045 | if (enc != XML_CHAR_ENCODING_NONE) |
| 7046 | xmlSwitchEncoding(ctxt, enc); |
| 7047 | } |
| 7048 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7049 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7050 | xmlParseTextDecl(ctxt); |
| 7051 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 7052 | /* |
| 7053 | * The XML REC instructs us to stop parsing right here |
| 7054 | */ |
| 7055 | ctxt->instate = XML_PARSER_EOF; |
| 7056 | return; |
| 7057 | } |
| 7058 | } |
| 7059 | if (ctxt->myDoc == NULL) { |
| 7060 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 7061 | if (ctxt->myDoc == NULL) { |
| 7062 | xmlErrMemory(ctxt, "New Doc failed"); |
| 7063 | return; |
| 7064 | } |
| 7065 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7066 | } |
| 7067 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 7068 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 7069 | |
| 7070 | ctxt->instate = XML_PARSER_DTD; |
| 7071 | ctxt->external = 1; |
| 7072 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 7073 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7074 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7075 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7076 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7077 | |
| 7078 | GROW; |
| 7079 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 7080 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7081 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7082 | NEXT; |
| 7083 | } else if (RAW == '%') { |
| 7084 | xmlParsePEReference(ctxt); |
| 7085 | } else |
| 7086 | xmlParseMarkupDecl(ctxt); |
| 7087 | |
| 7088 | /* |
| 7089 | * Pop-up of finished entities. |
| 7090 | */ |
| 7091 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7092 | xmlPopInput(ctxt); |
| 7093 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7094 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7095 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7096 | break; |
| 7097 | } |
| 7098 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7099 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7100 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7101 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7102 | } |
| 7103 | |
| 7104 | } |
| 7105 | |
| 7106 | /** |
| 7107 | * xmlParseReference: |
| 7108 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7109 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7110 | * parse and handle entity references in content, depending on the SAX |
| 7111 | * interface, this may end-up in a call to character() if this is a |
| 7112 | * CharRef, a predefined entity, if there is no reference() callback. |
| 7113 | * or if the parser was asked to switch to that mode. |
| 7114 | * |
| 7115 | * [67] Reference ::= EntityRef | CharRef |
| 7116 | */ |
| 7117 | void |
| 7118 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 7119 | xmlEntityPtr ent; |
| 7120 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7121 | int was_checked; |
| 7122 | xmlNodePtr list = NULL; |
| 7123 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7124 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7125 | |
| 7126 | if (RAW != '&') |
| 7127 | return; |
| 7128 | |
| 7129 | /* |
| 7130 | * Simple case of a CharRef |
| 7131 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7132 | if (NXT(1) == '#') { |
| 7133 | int i = 0; |
| 7134 | xmlChar out[10]; |
| 7135 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7136 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7137 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 7138 | if (value == 0) |
| 7139 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7140 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 7141 | /* |
| 7142 | * So we are using non-UTF-8 buffers |
| 7143 | * Check that the char fit on 8bits, if not |
| 7144 | * generate a CharRef. |
| 7145 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7146 | if (value <= 0xFF) { |
| 7147 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7148 | out[1] = 0; |
| 7149 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7150 | (!ctxt->disableSAX)) |
| 7151 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 7152 | } else { |
| 7153 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7154 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7155 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7156 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7157 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7158 | (!ctxt->disableSAX)) |
| 7159 | ctxt->sax->reference(ctxt->userData, out); |
| 7160 | } |
| 7161 | } else { |
| 7162 | /* |
| 7163 | * Just encode the value in UTF-8 |
| 7164 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7165 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7166 | out[i] = 0; |
| 7167 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7168 | (!ctxt->disableSAX)) |
| 7169 | ctxt->sax->characters(ctxt->userData, out, i); |
| 7170 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7171 | return; |
| 7172 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7173 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7174 | /* |
| 7175 | * We are seeing an entity reference |
| 7176 | */ |
| 7177 | ent = xmlParseEntityRef(ctxt); |
| 7178 | if (ent == NULL) return; |
| 7179 | if (!ctxt->wellFormed) |
| 7180 | return; |
| 7181 | was_checked = ent->checked; |
| 7182 | |
| 7183 | /* special case of predefined entities */ |
| 7184 | if ((ent->name == NULL) || |
| 7185 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 7186 | val = ent->content; |
| 7187 | if (val == NULL) return; |
| 7188 | /* |
| 7189 | * inline the entity. |
| 7190 | */ |
| 7191 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7192 | (!ctxt->disableSAX)) |
| 7193 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 7194 | return; |
| 7195 | } |
| 7196 | |
| 7197 | /* |
| 7198 | * The first reference to the entity trigger a parsing phase |
| 7199 | * where the ent->children is filled with the result from |
| 7200 | * the parsing. |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7201 | * Note: external parsed entities will not be loaded, it is not |
| 7202 | * required for a non-validating parser, unless the parsing option |
| 7203 | * of validating, or substituting entities were given. Doing so is |
| 7204 | * far more secure as the parser will only process data coming from |
| 7205 | * the document entity by default. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7206 | */ |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7207 | if ((ent->checked == 0) && |
| 7208 | ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
| 7209 | (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7210 | unsigned long oldnbent = ctxt->nbentities; |
| 7211 | |
| 7212 | /* |
| 7213 | * This is a bit hackish but this seems the best |
| 7214 | * way to make sure both SAX and DOM entity support |
| 7215 | * behaves okay. |
| 7216 | */ |
| 7217 | void *user_data; |
| 7218 | if (ctxt->userData == ctxt) |
| 7219 | user_data = NULL; |
| 7220 | else |
| 7221 | user_data = ctxt->userData; |
| 7222 | |
| 7223 | /* |
| 7224 | * Check that this entity is well formed |
| 7225 | * 4.3.2: An internal general parsed entity is well-formed |
| 7226 | * if its replacement text matches the production labeled |
| 7227 | * content. |
| 7228 | */ |
| 7229 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7230 | ctxt->depth++; |
| 7231 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 7232 | user_data, &list); |
| 7233 | ctxt->depth--; |
| 7234 | |
| 7235 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7236 | ctxt->depth++; |
| 7237 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 7238 | user_data, ctxt->depth, ent->URI, |
| 7239 | ent->ExternalID, &list); |
| 7240 | ctxt->depth--; |
| 7241 | } else { |
| 7242 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7243 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7244 | "invalid entity type found\n", NULL); |
| 7245 | } |
| 7246 | |
| 7247 | /* |
| 7248 | * Store the number of entities needing parsing for this entity |
| 7249 | * content and do checkings |
| 7250 | */ |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7251 | ent->checked = (ctxt->nbentities - oldnbent + 1) * 2; |
| 7252 | if ((ent->content != NULL) && (xmlStrchr(ent->content, '<'))) |
| 7253 | ent->checked |= 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7254 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7255 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7256 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7257 | return; |
| 7258 | } |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7259 | if (xmlParserEntityCheck(ctxt, 0, ent, 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7260 | xmlFreeNodeList(list); |
| 7261 | return; |
| 7262 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7263 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7264 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 7265 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 7266 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 7267 | (ent->children == NULL)) { |
| 7268 | ent->children = list; |
| 7269 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7270 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7271 | * Prune it directly in the generated document |
| 7272 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7273 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7274 | if (((list->type == XML_TEXT_NODE) && |
| 7275 | (list->next == NULL)) || |
| 7276 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7277 | list->parent = (xmlNodePtr) ent; |
| 7278 | list = NULL; |
| 7279 | ent->owner = 1; |
| 7280 | } else { |
| 7281 | ent->owner = 0; |
| 7282 | while (list != NULL) { |
| 7283 | list->parent = (xmlNodePtr) ctxt->node; |
| 7284 | list->doc = ctxt->myDoc; |
| 7285 | if (list->next == NULL) |
| 7286 | ent->last = list; |
| 7287 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7288 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7289 | list = ent->children; |
| 7290 | #ifdef LIBXML_LEGACY_ENABLED |
| 7291 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7292 | xmlAddEntityReference(ent, list, NULL); |
| 7293 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7294 | } |
| 7295 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7296 | ent->owner = 1; |
| 7297 | while (list != NULL) { |
| 7298 | list->parent = (xmlNodePtr) ent; |
Rob Richards | c794eb5 | 2011-02-18 12:17:17 -0500 | [diff] [blame] | 7299 | xmlSetTreeDoc(list, ent->doc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7300 | if (list->next == NULL) |
| 7301 | ent->last = list; |
| 7302 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7303 | } |
| 7304 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7305 | } else { |
| 7306 | xmlFreeNodeList(list); |
| 7307 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7308 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7309 | } else if ((ret != XML_ERR_OK) && |
| 7310 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 7311 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7312 | "Entity '%s' failed to parse\n", ent->name); |
| 7313 | } else if (list != NULL) { |
| 7314 | xmlFreeNodeList(list); |
| 7315 | list = NULL; |
| 7316 | } |
| 7317 | if (ent->checked == 0) |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7318 | ent->checked = 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7319 | } else if (ent->checked != 1) { |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7320 | ctxt->nbentities += ent->checked / 2; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7321 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7322 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7323 | /* |
| 7324 | * Now that the entity content has been gathered |
| 7325 | * provide it to the application, this can take different forms based |
| 7326 | * on the parsing modes. |
| 7327 | */ |
| 7328 | if (ent->children == NULL) { |
| 7329 | /* |
| 7330 | * Probably running in SAX mode and the callbacks don't |
| 7331 | * build the entity content. So unless we already went |
| 7332 | * though parsing for first checking go though the entity |
| 7333 | * content to generate callbacks associated to the entity |
| 7334 | */ |
| 7335 | if (was_checked != 0) { |
| 7336 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7337 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7338 | * This is a bit hackish but this seems the best |
| 7339 | * way to make sure both SAX and DOM entity support |
| 7340 | * behaves okay. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7341 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7342 | if (ctxt->userData == ctxt) |
| 7343 | user_data = NULL; |
| 7344 | else |
| 7345 | user_data = ctxt->userData; |
| 7346 | |
| 7347 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7348 | ctxt->depth++; |
| 7349 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 7350 | ent->content, user_data, NULL); |
| 7351 | ctxt->depth--; |
| 7352 | } else if (ent->etype == |
| 7353 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7354 | ctxt->depth++; |
| 7355 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 7356 | ctxt->sax, user_data, ctxt->depth, |
| 7357 | ent->URI, ent->ExternalID, NULL); |
| 7358 | ctxt->depth--; |
| 7359 | } else { |
| 7360 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7361 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7362 | "invalid entity type found\n", NULL); |
| 7363 | } |
| 7364 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 7365 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 7366 | return; |
| 7367 | } |
| 7368 | } |
| 7369 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7370 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7371 | /* |
| 7372 | * Entity reference callback comes second, it's somewhat |
| 7373 | * superfluous but a compatibility to historical behaviour |
| 7374 | */ |
| 7375 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7376 | } |
| 7377 | return; |
| 7378 | } |
| 7379 | |
| 7380 | /* |
| 7381 | * If we didn't get any children for the entity being built |
| 7382 | */ |
| 7383 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7384 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7385 | /* |
| 7386 | * Create a node. |
| 7387 | */ |
| 7388 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7389 | return; |
| 7390 | } |
| 7391 | |
| 7392 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 7393 | /* |
| 7394 | * There is a problem on the handling of _private for entities |
| 7395 | * (bug 155816): Should we copy the content of the field from |
| 7396 | * the entity (possibly overwriting some value set by the user |
| 7397 | * when a copy is created), should we leave it alone, or should |
| 7398 | * we try to take care of different situations? The problem |
| 7399 | * is exacerbated by the usage of this field by the xmlReader. |
| 7400 | * To fix this bug, we look at _private on the created node |
| 7401 | * and, if it's NULL, we copy in whatever was in the entity. |
| 7402 | * If it's not NULL we leave it alone. This is somewhat of a |
| 7403 | * hack - maybe we should have further tests to determine |
| 7404 | * what to do. |
| 7405 | */ |
| 7406 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 7407 | /* |
| 7408 | * Seems we are generating the DOM content, do |
| 7409 | * a simple tree copy for all references except the first |
| 7410 | * In the first occurrence list contains the replacement. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7411 | */ |
| 7412 | if (((list == NULL) && (ent->owner == 0)) || |
| 7413 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7414 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 7415 | |
| 7416 | /* |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7417 | * We are copying here, make sure there is no abuse |
| 7418 | */ |
| 7419 | ctxt->sizeentcopy += ent->length; |
| 7420 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7421 | return; |
| 7422 | |
| 7423 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7424 | * when operating on a reader, the entities definitions |
| 7425 | * are always owning the entities subtree. |
| 7426 | if (ctxt->parseMode == XML_PARSE_READER) |
| 7427 | ent->owner = 1; |
| 7428 | */ |
| 7429 | |
| 7430 | cur = ent->children; |
| 7431 | while (cur != NULL) { |
| 7432 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7433 | if (nw != NULL) { |
| 7434 | if (nw->_private == NULL) |
| 7435 | nw->_private = cur->_private; |
| 7436 | if (firstChild == NULL){ |
| 7437 | firstChild = nw; |
| 7438 | } |
| 7439 | nw = xmlAddChild(ctxt->node, nw); |
| 7440 | } |
| 7441 | if (cur == ent->last) { |
| 7442 | /* |
| 7443 | * needed to detect some strange empty |
| 7444 | * node cases in the reader tests |
| 7445 | */ |
| 7446 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 7447 | (nw != NULL) && |
| 7448 | (nw->type == XML_ELEMENT_NODE) && |
| 7449 | (nw->children == NULL)) |
| 7450 | nw->extra = 1; |
| 7451 | |
| 7452 | break; |
| 7453 | } |
| 7454 | cur = cur->next; |
| 7455 | } |
| 7456 | #ifdef LIBXML_LEGACY_ENABLED |
| 7457 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7458 | xmlAddEntityReference(ent, firstChild, nw); |
| 7459 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 28f5e1a | 2012-09-04 11:18:39 +0800 | [diff] [blame] | 7460 | } else if ((list == NULL) || (ctxt->inputNr > 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7461 | xmlNodePtr nw = NULL, cur, next, last, |
| 7462 | firstChild = NULL; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 7463 | |
| 7464 | /* |
| 7465 | * We are copying here, make sure there is no abuse |
| 7466 | */ |
| 7467 | ctxt->sizeentcopy += ent->length; |
| 7468 | if (xmlParserEntityCheck(ctxt, 0, ent, ctxt->sizeentcopy)) |
| 7469 | return; |
| 7470 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7471 | /* |
| 7472 | * Copy the entity child list and make it the new |
| 7473 | * entity child list. The goal is to make sure any |
| 7474 | * ID or REF referenced will be the one from the |
| 7475 | * document content and not the entity copy. |
| 7476 | */ |
| 7477 | cur = ent->children; |
| 7478 | ent->children = NULL; |
| 7479 | last = ent->last; |
| 7480 | ent->last = NULL; |
| 7481 | while (cur != NULL) { |
| 7482 | next = cur->next; |
| 7483 | cur->next = NULL; |
| 7484 | cur->parent = NULL; |
| 7485 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7486 | if (nw != NULL) { |
| 7487 | if (nw->_private == NULL) |
| 7488 | nw->_private = cur->_private; |
| 7489 | if (firstChild == NULL){ |
| 7490 | firstChild = cur; |
| 7491 | } |
| 7492 | xmlAddChild((xmlNodePtr) ent, nw); |
| 7493 | xmlAddChild(ctxt->node, cur); |
| 7494 | } |
| 7495 | if (cur == last) |
| 7496 | break; |
| 7497 | cur = next; |
| 7498 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 7499 | if (ent->owner == 0) |
| 7500 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7501 | #ifdef LIBXML_LEGACY_ENABLED |
| 7502 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7503 | xmlAddEntityReference(ent, firstChild, nw); |
| 7504 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7505 | } else { |
| 7506 | const xmlChar *nbktext; |
| 7507 | |
| 7508 | /* |
| 7509 | * the name change is to avoid coalescing of the |
| 7510 | * node with a possible previous text one which |
| 7511 | * would make ent->children a dangling pointer |
| 7512 | */ |
| 7513 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 7514 | -1); |
| 7515 | if (ent->children->type == XML_TEXT_NODE) |
| 7516 | ent->children->name = nbktext; |
| 7517 | if ((ent->last != ent->children) && |
| 7518 | (ent->last->type == XML_TEXT_NODE)) |
| 7519 | ent->last->name = nbktext; |
| 7520 | xmlAddChildList(ctxt->node, ent->children); |
| 7521 | } |
| 7522 | |
| 7523 | /* |
| 7524 | * This is to avoid a nasty side effect, see |
| 7525 | * characters() in SAX.c |
| 7526 | */ |
| 7527 | ctxt->nodemem = 0; |
| 7528 | ctxt->nodelen = 0; |
| 7529 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7530 | } |
| 7531 | } |
| 7532 | } |
| 7533 | |
| 7534 | /** |
| 7535 | * xmlParseEntityRef: |
| 7536 | * @ctxt: an XML parser context |
| 7537 | * |
| 7538 | * parse ENTITY references declarations |
| 7539 | * |
| 7540 | * [68] EntityRef ::= '&' Name ';' |
| 7541 | * |
| 7542 | * [ WFC: Entity Declared ] |
| 7543 | * In a document without any DTD, a document with only an internal DTD |
| 7544 | * subset which contains no parameter entity references, or a document |
| 7545 | * with "standalone='yes'", the Name given in the entity reference |
| 7546 | * must match that in an entity declaration, except that well-formed |
| 7547 | * documents need not declare any of the following entities: amp, lt, |
| 7548 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7549 | * any reference to it. Similarly, the declaration of a general entity |
| 7550 | * must precede any reference to it which appears in a default value in an |
| 7551 | * attribute-list declaration. Note that if entities are declared in the |
| 7552 | * external subset or in external parameter entities, a non-validating |
| 7553 | * processor is not obligated to read and process their declarations; |
| 7554 | * for such documents, the rule that an entity must be declared is a |
| 7555 | * well-formedness constraint only if standalone='yes'. |
| 7556 | * |
| 7557 | * [ WFC: Parsed Entity ] |
| 7558 | * An entity reference must not contain the name of an unparsed entity |
| 7559 | * |
| 7560 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7561 | */ |
| 7562 | xmlEntityPtr |
| 7563 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7564 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7565 | xmlEntityPtr ent = NULL; |
| 7566 | |
| 7567 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7568 | if (ctxt->instate == XML_PARSER_EOF) |
| 7569 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7570 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7571 | if (RAW != '&') |
| 7572 | return(NULL); |
| 7573 | NEXT; |
| 7574 | name = xmlParseName(ctxt); |
| 7575 | if (name == NULL) { |
| 7576 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7577 | "xmlParseEntityRef: no name\n"); |
| 7578 | return(NULL); |
| 7579 | } |
| 7580 | if (RAW != ';') { |
| 7581 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7582 | return(NULL); |
| 7583 | } |
| 7584 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7585 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7586 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7587 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7588 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7589 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7590 | ent = xmlGetPredefinedEntity(name); |
| 7591 | if (ent != NULL) |
| 7592 | return(ent); |
| 7593 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7594 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7595 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7596 | * Increase the number of entity references parsed |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7597 | */ |
| 7598 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7599 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7600 | /* |
| 7601 | * Ask first SAX for entity resolution, otherwise try the |
| 7602 | * entities which may have stored in the parser context. |
| 7603 | */ |
| 7604 | if (ctxt->sax != NULL) { |
| 7605 | if (ctxt->sax->getEntity != NULL) |
| 7606 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7607 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7608 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7609 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7610 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7611 | (ctxt->userData==ctxt)) { |
| 7612 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7613 | } |
| 7614 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7615 | if (ctxt->instate == XML_PARSER_EOF) |
| 7616 | return(NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7617 | /* |
| 7618 | * [ WFC: Entity Declared ] |
| 7619 | * In a document without any DTD, a document with only an |
| 7620 | * internal DTD subset which contains no parameter entity |
| 7621 | * references, or a document with "standalone='yes'", the |
| 7622 | * Name given in the entity reference must match that in an |
| 7623 | * entity declaration, except that well-formed documents |
| 7624 | * need not declare any of the following entities: amp, lt, |
| 7625 | * gt, apos, quot. |
| 7626 | * The declaration of a parameter entity must precede any |
| 7627 | * reference to it. |
| 7628 | * Similarly, the declaration of a general entity must |
| 7629 | * precede any reference to it which appears in a default |
| 7630 | * value in an attribute-list declaration. Note that if |
| 7631 | * entities are declared in the external subset or in |
| 7632 | * external parameter entities, a non-validating processor |
| 7633 | * is not obligated to read and process their declarations; |
| 7634 | * for such documents, the rule that an entity must be |
| 7635 | * declared is a well-formedness constraint only if |
| 7636 | * standalone='yes'. |
| 7637 | */ |
| 7638 | if (ent == NULL) { |
| 7639 | if ((ctxt->standalone == 1) || |
| 7640 | ((ctxt->hasExternalSubset == 0) && |
| 7641 | (ctxt->hasPErefs == 0))) { |
| 7642 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7643 | "Entity '%s' not defined\n", name); |
| 7644 | } else { |
| 7645 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7646 | "Entity '%s' not defined\n", name); |
| 7647 | if ((ctxt->inSubset == 0) && |
| 7648 | (ctxt->sax != NULL) && |
| 7649 | (ctxt->sax->reference != NULL)) { |
| 7650 | ctxt->sax->reference(ctxt->userData, name); |
| 7651 | } |
| 7652 | } |
| 7653 | ctxt->valid = 0; |
| 7654 | } |
| 7655 | |
| 7656 | /* |
| 7657 | * [ WFC: Parsed Entity ] |
| 7658 | * An entity reference must not contain the name of an |
| 7659 | * unparsed entity |
| 7660 | */ |
| 7661 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7662 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7663 | "Entity reference to unparsed entity %s\n", name); |
| 7664 | } |
| 7665 | |
| 7666 | /* |
| 7667 | * [ WFC: No External Entity References ] |
| 7668 | * Attribute values cannot contain direct or indirect |
| 7669 | * entity references to external entities. |
| 7670 | */ |
| 7671 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7672 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7673 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7674 | "Attribute references external entity '%s'\n", name); |
| 7675 | } |
| 7676 | /* |
| 7677 | * [ WFC: No < in Attribute Values ] |
| 7678 | * The replacement text of any entity referred to directly or |
| 7679 | * indirectly in an attribute value (other than "<") must |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7680 | * not contain a <. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7681 | */ |
| 7682 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
Daniel Veillard | cff2546 | 2013-03-11 15:57:55 +0800 | [diff] [blame] | 7683 | (ent != NULL) && |
| 7684 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 7685 | if ((ent->checked & 1) || ((ent->checked == 0) && |
| 7686 | (ent->content != NULL) &&(xmlStrchr(ent->content, '<')))) { |
| 7687 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7688 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7689 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7690 | } |
| 7691 | |
| 7692 | /* |
| 7693 | * Internal check, no parameter entities here ... |
| 7694 | */ |
| 7695 | else { |
| 7696 | switch (ent->etype) { |
| 7697 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7698 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7699 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7700 | "Attempt to reference the parameter entity '%s'\n", |
| 7701 | name); |
| 7702 | break; |
| 7703 | default: |
| 7704 | break; |
| 7705 | } |
| 7706 | } |
| 7707 | |
| 7708 | /* |
| 7709 | * [ WFC: No Recursion ] |
| 7710 | * A parsed entity must not contain a recursive reference |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7711 | * to itself, either directly or indirectly. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7712 | * Done somewhere else |
| 7713 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7714 | return(ent); |
| 7715 | } |
| 7716 | |
| 7717 | /** |
| 7718 | * xmlParseStringEntityRef: |
| 7719 | * @ctxt: an XML parser context |
| 7720 | * @str: a pointer to an index in the string |
| 7721 | * |
| 7722 | * parse ENTITY references declarations, but this version parses it from |
| 7723 | * a string value. |
| 7724 | * |
| 7725 | * [68] EntityRef ::= '&' Name ';' |
| 7726 | * |
| 7727 | * [ WFC: Entity Declared ] |
| 7728 | * In a document without any DTD, a document with only an internal DTD |
| 7729 | * subset which contains no parameter entity references, or a document |
| 7730 | * with "standalone='yes'", the Name given in the entity reference |
| 7731 | * must match that in an entity declaration, except that well-formed |
| 7732 | * documents need not declare any of the following entities: amp, lt, |
| 7733 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7734 | * any reference to it. Similarly, the declaration of a general entity |
| 7735 | * must precede any reference to it which appears in a default value in an |
| 7736 | * attribute-list declaration. Note that if entities are declared in the |
| 7737 | * external subset or in external parameter entities, a non-validating |
| 7738 | * processor is not obligated to read and process their declarations; |
| 7739 | * for such documents, the rule that an entity must be declared is a |
| 7740 | * well-formedness constraint only if standalone='yes'. |
| 7741 | * |
| 7742 | * [ WFC: Parsed Entity ] |
| 7743 | * An entity reference must not contain the name of an unparsed entity |
| 7744 | * |
| 7745 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7746 | * is updated to the current location in the string. |
| 7747 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7748 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7749 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7750 | xmlChar *name; |
| 7751 | const xmlChar *ptr; |
| 7752 | xmlChar cur; |
| 7753 | xmlEntityPtr ent = NULL; |
| 7754 | |
| 7755 | if ((str == NULL) || (*str == NULL)) |
| 7756 | return(NULL); |
| 7757 | ptr = *str; |
| 7758 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7759 | if (cur != '&') |
| 7760 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7761 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7762 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7763 | name = xmlParseStringName(ctxt, &ptr); |
| 7764 | if (name == NULL) { |
| 7765 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7766 | "xmlParseStringEntityRef: no name\n"); |
| 7767 | *str = ptr; |
| 7768 | return(NULL); |
| 7769 | } |
| 7770 | if (*ptr != ';') { |
| 7771 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7772 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7773 | *str = ptr; |
| 7774 | return(NULL); |
| 7775 | } |
| 7776 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7777 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7778 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7779 | /* |
Jan Pokorný | 9a85d40 | 2013-11-29 23:26:25 +0100 | [diff] [blame] | 7780 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7781 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7782 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7783 | ent = xmlGetPredefinedEntity(name); |
| 7784 | if (ent != NULL) { |
| 7785 | xmlFree(name); |
| 7786 | *str = ptr; |
| 7787 | return(ent); |
| 7788 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7789 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7790 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7791 | /* |
| 7792 | * Increate the number of entity references parsed |
| 7793 | */ |
| 7794 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7795 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7796 | /* |
| 7797 | * Ask first SAX for entity resolution, otherwise try the |
| 7798 | * entities which may have stored in the parser context. |
| 7799 | */ |
| 7800 | if (ctxt->sax != NULL) { |
| 7801 | if (ctxt->sax->getEntity != NULL) |
| 7802 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7803 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7804 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7805 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7806 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7807 | } |
| 7808 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7809 | if (ctxt->instate == XML_PARSER_EOF) { |
| 7810 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 7811 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7812 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7813 | |
| 7814 | /* |
| 7815 | * [ WFC: Entity Declared ] |
| 7816 | * In a document without any DTD, a document with only an |
| 7817 | * internal DTD subset which contains no parameter entity |
| 7818 | * references, or a document with "standalone='yes'", the |
| 7819 | * Name given in the entity reference must match that in an |
| 7820 | * entity declaration, except that well-formed documents |
| 7821 | * need not declare any of the following entities: amp, lt, |
| 7822 | * gt, apos, quot. |
| 7823 | * The declaration of a parameter entity must precede any |
| 7824 | * reference to it. |
| 7825 | * Similarly, the declaration of a general entity must |
| 7826 | * precede any reference to it which appears in a default |
| 7827 | * value in an attribute-list declaration. Note that if |
| 7828 | * entities are declared in the external subset or in |
| 7829 | * external parameter entities, a non-validating processor |
| 7830 | * is not obligated to read and process their declarations; |
| 7831 | * for such documents, the rule that an entity must be |
| 7832 | * declared is a well-formedness constraint only if |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7833 | * standalone='yes'. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7834 | */ |
| 7835 | if (ent == NULL) { |
| 7836 | if ((ctxt->standalone == 1) || |
| 7837 | ((ctxt->hasExternalSubset == 0) && |
| 7838 | (ctxt->hasPErefs == 0))) { |
| 7839 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7840 | "Entity '%s' not defined\n", name); |
| 7841 | } else { |
| 7842 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7843 | "Entity '%s' not defined\n", |
| 7844 | name); |
| 7845 | } |
| 7846 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7847 | } |
| 7848 | |
| 7849 | /* |
| 7850 | * [ WFC: Parsed Entity ] |
| 7851 | * An entity reference must not contain the name of an |
| 7852 | * unparsed entity |
| 7853 | */ |
| 7854 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7855 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7856 | "Entity reference to unparsed entity %s\n", name); |
| 7857 | } |
| 7858 | |
| 7859 | /* |
| 7860 | * [ WFC: No External Entity References ] |
| 7861 | * Attribute values cannot contain direct or indirect |
| 7862 | * entity references to external entities. |
| 7863 | */ |
| 7864 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7865 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7866 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7867 | "Attribute references external entity '%s'\n", name); |
| 7868 | } |
| 7869 | /* |
| 7870 | * [ WFC: No < in Attribute Values ] |
| 7871 | * The replacement text of any entity referred to directly or |
| 7872 | * indirectly in an attribute value (other than "<") must |
| 7873 | * not contain a <. |
| 7874 | */ |
| 7875 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7876 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7877 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7878 | (xmlStrchr(ent->content, '<'))) { |
| 7879 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7880 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7881 | name); |
| 7882 | } |
| 7883 | |
| 7884 | /* |
| 7885 | * Internal check, no parameter entities here ... |
| 7886 | */ |
| 7887 | else { |
| 7888 | switch (ent->etype) { |
| 7889 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7890 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7891 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7892 | "Attempt to reference the parameter entity '%s'\n", |
| 7893 | name); |
| 7894 | break; |
| 7895 | default: |
| 7896 | break; |
| 7897 | } |
| 7898 | } |
| 7899 | |
| 7900 | /* |
| 7901 | * [ WFC: No Recursion ] |
| 7902 | * A parsed entity must not contain a recursive reference |
| 7903 | * to itself, either directly or indirectly. |
| 7904 | * Done somewhere else |
| 7905 | */ |
| 7906 | |
| 7907 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7908 | *str = ptr; |
| 7909 | return(ent); |
| 7910 | } |
| 7911 | |
| 7912 | /** |
| 7913 | * xmlParsePEReference: |
| 7914 | * @ctxt: an XML parser context |
| 7915 | * |
| 7916 | * parse PEReference declarations |
| 7917 | * The entity content is handled directly by pushing it's content as |
| 7918 | * a new input stream. |
| 7919 | * |
| 7920 | * [69] PEReference ::= '%' Name ';' |
| 7921 | * |
| 7922 | * [ WFC: No Recursion ] |
| 7923 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7924 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7925 | * |
| 7926 | * [ WFC: Entity Declared ] |
| 7927 | * In a document without any DTD, a document with only an internal DTD |
| 7928 | * subset which contains no parameter entity references, or a document |
| 7929 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7930 | * entity must precede any reference to it... |
| 7931 | * |
| 7932 | * [ VC: Entity Declared ] |
| 7933 | * In a document with an external subset or external parameter entities |
| 7934 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7935 | * must precede any reference to it... |
| 7936 | * |
| 7937 | * [ WFC: In DTD ] |
| 7938 | * Parameter-entity references may only appear in the DTD. |
| 7939 | * NOTE: misleading but this is handled. |
| 7940 | */ |
| 7941 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7942 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7943 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7944 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7945 | xmlEntityPtr entity = NULL; |
| 7946 | xmlParserInputPtr input; |
| 7947 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7948 | if (RAW != '%') |
| 7949 | return; |
| 7950 | NEXT; |
| 7951 | name = xmlParseName(ctxt); |
| 7952 | if (name == NULL) { |
| 7953 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7954 | "xmlParsePEReference: no name\n"); |
| 7955 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7956 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7957 | if (RAW != ';') { |
| 7958 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7959 | return; |
| 7960 | } |
| 7961 | |
| 7962 | NEXT; |
| 7963 | |
| 7964 | /* |
| 7965 | * Increate the number of entity references parsed |
| 7966 | */ |
| 7967 | ctxt->nbentities++; |
| 7968 | |
| 7969 | /* |
| 7970 | * Request the entity from SAX |
| 7971 | */ |
| 7972 | if ((ctxt->sax != NULL) && |
| 7973 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 7974 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 7975 | if (ctxt->instate == XML_PARSER_EOF) |
| 7976 | return; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7977 | if (entity == NULL) { |
| 7978 | /* |
| 7979 | * [ WFC: Entity Declared ] |
| 7980 | * In a document without any DTD, a document with only an |
| 7981 | * internal DTD subset which contains no parameter entity |
| 7982 | * references, or a document with "standalone='yes'", ... |
| 7983 | * ... The declaration of a parameter entity must precede |
| 7984 | * any reference to it... |
| 7985 | */ |
| 7986 | if ((ctxt->standalone == 1) || |
| 7987 | ((ctxt->hasExternalSubset == 0) && |
| 7988 | (ctxt->hasPErefs == 0))) { |
| 7989 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7990 | "PEReference: %%%s; not found\n", |
| 7991 | name); |
| 7992 | } else { |
| 7993 | /* |
| 7994 | * [ VC: Entity Declared ] |
| 7995 | * In a document with an external subset or external |
| 7996 | * parameter entities with "standalone='no'", ... |
| 7997 | * ... The declaration of a parameter entity must |
| 7998 | * precede any reference to it... |
| 7999 | */ |
| 8000 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8001 | "PEReference: %%%s; not found\n", |
| 8002 | name, NULL); |
| 8003 | ctxt->valid = 0; |
| 8004 | } |
| 8005 | } else { |
| 8006 | /* |
| 8007 | * Internal checking in case the entity quest barfed |
| 8008 | */ |
| 8009 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8010 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8011 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8012 | "Internal: %%%s; is not a parameter entity\n", |
| 8013 | name, NULL); |
| 8014 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 8015 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 8016 | if (xmlPushInput(ctxt, input) < 0) |
| 8017 | return; |
| 8018 | } else { |
| 8019 | /* |
| 8020 | * TODO !!! |
| 8021 | * handle the extra spaces added before and after |
| 8022 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 8023 | */ |
| 8024 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8025 | if (xmlPushInput(ctxt, input) < 0) |
| 8026 | return; |
| 8027 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8028 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 8029 | (IS_BLANK_CH(NXT(5)))) { |
| 8030 | xmlParseTextDecl(ctxt); |
| 8031 | if (ctxt->errNo == |
| 8032 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 8033 | /* |
| 8034 | * The XML REC instructs us to stop parsing |
| 8035 | * right here |
| 8036 | */ |
| 8037 | ctxt->instate = XML_PARSER_EOF; |
| 8038 | return; |
| 8039 | } |
| 8040 | } |
| 8041 | } |
| 8042 | } |
| 8043 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8047 | * xmlLoadEntityContent: |
| 8048 | * @ctxt: an XML parser context |
| 8049 | * @entity: an unloaded system entity |
| 8050 | * |
| 8051 | * Load the original content of the given system entity from the |
| 8052 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 8053 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 8054 | * |
| 8055 | * Returns 0 in case of success and -1 in case of failure |
| 8056 | */ |
| 8057 | static int |
| 8058 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 8059 | xmlParserInputPtr input; |
| 8060 | xmlBufferPtr buf; |
| 8061 | int l, c; |
| 8062 | int count = 0; |
| 8063 | |
| 8064 | if ((ctxt == NULL) || (entity == NULL) || |
| 8065 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 8066 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 8067 | (entity->content != NULL)) { |
| 8068 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8069 | "xmlLoadEntityContent parameter error"); |
| 8070 | return(-1); |
| 8071 | } |
| 8072 | |
| 8073 | if (xmlParserDebugEntities) |
| 8074 | xmlGenericError(xmlGenericErrorContext, |
| 8075 | "Reading %s entity content input\n", entity->name); |
| 8076 | |
| 8077 | buf = xmlBufferCreate(); |
| 8078 | if (buf == NULL) { |
| 8079 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8080 | "xmlLoadEntityContent parameter error"); |
| 8081 | return(-1); |
| 8082 | } |
| 8083 | |
| 8084 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8085 | if (input == NULL) { |
| 8086 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8087 | "xmlLoadEntityContent input error"); |
| 8088 | xmlBufferFree(buf); |
| 8089 | return(-1); |
| 8090 | } |
| 8091 | |
| 8092 | /* |
| 8093 | * Push the entity as the current input, read char by char |
| 8094 | * saving to the buffer until the end of the entity or an error |
| 8095 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 8096 | if (xmlPushInput(ctxt, input) < 0) { |
| 8097 | xmlBufferFree(buf); |
| 8098 | return(-1); |
| 8099 | } |
| 8100 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8101 | GROW; |
| 8102 | c = CUR_CHAR(l); |
| 8103 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 8104 | (IS_CHAR(c))) { |
| 8105 | xmlBufferAdd(buf, ctxt->input->cur, l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8106 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8107 | count = 0; |
| 8108 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8109 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8110 | xmlBufferFree(buf); |
| 8111 | return(-1); |
| 8112 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8113 | } |
| 8114 | NEXTL(l); |
| 8115 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8116 | if (c == 0) { |
| 8117 | count = 0; |
| 8118 | GROW; |
| 8119 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8120 | xmlBufferFree(buf); |
| 8121 | return(-1); |
| 8122 | } |
| 8123 | c = CUR_CHAR(l); |
| 8124 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8125 | } |
| 8126 | |
| 8127 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 8128 | xmlPopInput(ctxt); |
| 8129 | } else if (!IS_CHAR(c)) { |
| 8130 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 8131 | "xmlLoadEntityContent: invalid char value %d\n", |
| 8132 | c); |
| 8133 | xmlBufferFree(buf); |
| 8134 | return(-1); |
| 8135 | } |
| 8136 | entity->content = buf->content; |
| 8137 | buf->content = NULL; |
| 8138 | xmlBufferFree(buf); |
| 8139 | |
| 8140 | return(0); |
| 8141 | } |
| 8142 | |
| 8143 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8144 | * xmlParseStringPEReference: |
| 8145 | * @ctxt: an XML parser context |
| 8146 | * @str: a pointer to an index in the string |
| 8147 | * |
| 8148 | * parse PEReference declarations |
| 8149 | * |
| 8150 | * [69] PEReference ::= '%' Name ';' |
| 8151 | * |
| 8152 | * [ WFC: No Recursion ] |
| 8153 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8154 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8155 | * |
| 8156 | * [ WFC: Entity Declared ] |
| 8157 | * In a document without any DTD, a document with only an internal DTD |
| 8158 | * subset which contains no parameter entity references, or a document |
| 8159 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 8160 | * entity must precede any reference to it... |
| 8161 | * |
| 8162 | * [ VC: Entity Declared ] |
| 8163 | * In a document with an external subset or external parameter entities |
| 8164 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 8165 | * must precede any reference to it... |
| 8166 | * |
| 8167 | * [ WFC: In DTD ] |
| 8168 | * Parameter-entity references may only appear in the DTD. |
| 8169 | * NOTE: misleading but this is handled. |
| 8170 | * |
| 8171 | * Returns the string of the entity content. |
| 8172 | * str is updated to the current value of the index |
| 8173 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 8174 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8175 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 8176 | const xmlChar *ptr; |
| 8177 | xmlChar cur; |
| 8178 | xmlChar *name; |
| 8179 | xmlEntityPtr entity = NULL; |
| 8180 | |
| 8181 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 8182 | ptr = *str; |
| 8183 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8184 | if (cur != '%') |
| 8185 | return(NULL); |
| 8186 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8187 | name = xmlParseStringName(ctxt, &ptr); |
| 8188 | if (name == NULL) { |
| 8189 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8190 | "xmlParseStringPEReference: no name\n"); |
| 8191 | *str = ptr; |
| 8192 | return(NULL); |
| 8193 | } |
| 8194 | cur = *ptr; |
| 8195 | if (cur != ';') { |
| 8196 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 8197 | xmlFree(name); |
| 8198 | *str = ptr; |
| 8199 | return(NULL); |
| 8200 | } |
| 8201 | ptr++; |
| 8202 | |
| 8203 | /* |
| 8204 | * Increate the number of entity references parsed |
| 8205 | */ |
| 8206 | ctxt->nbentities++; |
| 8207 | |
| 8208 | /* |
| 8209 | * Request the entity from SAX |
| 8210 | */ |
| 8211 | if ((ctxt->sax != NULL) && |
| 8212 | (ctxt->sax->getParameterEntity != NULL)) |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8213 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 8214 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8215 | xmlFree(name); |
Jüri Aedla | 9ca816b | 2013-04-16 22:00:13 +0800 | [diff] [blame] | 8216 | return(NULL); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8217 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8218 | if (entity == NULL) { |
| 8219 | /* |
| 8220 | * [ WFC: Entity Declared ] |
| 8221 | * In a document without any DTD, a document with only an |
| 8222 | * internal DTD subset which contains no parameter entity |
| 8223 | * references, or a document with "standalone='yes'", ... |
| 8224 | * ... The declaration of a parameter entity must precede |
| 8225 | * any reference to it... |
| 8226 | */ |
| 8227 | if ((ctxt->standalone == 1) || |
| 8228 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 8229 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 8230 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8231 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8232 | /* |
| 8233 | * [ VC: Entity Declared ] |
| 8234 | * In a document with an external subset or external |
| 8235 | * parameter entities with "standalone='no'", ... |
| 8236 | * ... The declaration of a parameter entity must |
| 8237 | * precede any reference to it... |
| 8238 | */ |
| 8239 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8240 | "PEReference: %%%s; not found\n", |
| 8241 | name, NULL); |
| 8242 | ctxt->valid = 0; |
| 8243 | } |
| 8244 | } else { |
| 8245 | /* |
| 8246 | * Internal checking in case the entity quest barfed |
| 8247 | */ |
| 8248 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8249 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8250 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8251 | "%%%s; is not a parameter entity\n", |
| 8252 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8253 | } |
| 8254 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8255 | ctxt->hasPErefs = 1; |
| 8256 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8257 | *str = ptr; |
| 8258 | return(entity); |
| 8259 | } |
| 8260 | |
| 8261 | /** |
| 8262 | * xmlParseDocTypeDecl: |
| 8263 | * @ctxt: an XML parser context |
| 8264 | * |
| 8265 | * parse a DOCTYPE declaration |
| 8266 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8267 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8268 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8269 | * |
| 8270 | * [ VC: Root Element Type ] |
| 8271 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8272 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8273 | */ |
| 8274 | |
| 8275 | void |
| 8276 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8277 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8278 | xmlChar *ExternalID = NULL; |
| 8279 | xmlChar *URI = NULL; |
| 8280 | |
| 8281 | /* |
| 8282 | * We know that '<!DOCTYPE' has been detected. |
| 8283 | */ |
| 8284 | SKIP(9); |
| 8285 | |
| 8286 | SKIP_BLANKS; |
| 8287 | |
| 8288 | /* |
| 8289 | * Parse the DOCTYPE name. |
| 8290 | */ |
| 8291 | name = xmlParseName(ctxt); |
| 8292 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8293 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8294 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8295 | } |
| 8296 | ctxt->intSubName = name; |
| 8297 | |
| 8298 | SKIP_BLANKS; |
| 8299 | |
| 8300 | /* |
| 8301 | * Check for SystemID and ExternalID |
| 8302 | */ |
| 8303 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 8304 | |
| 8305 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 8306 | ctxt->hasExternalSubset = 1; |
| 8307 | } |
| 8308 | ctxt->extSubURI = URI; |
| 8309 | ctxt->extSubSystem = ExternalID; |
| 8310 | |
| 8311 | SKIP_BLANKS; |
| 8312 | |
| 8313 | /* |
| 8314 | * Create and update the internal subset. |
| 8315 | */ |
| 8316 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 8317 | (!ctxt->disableSAX)) |
| 8318 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 8319 | if (ctxt->instate == XML_PARSER_EOF) |
| 8320 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8321 | |
| 8322 | /* |
| 8323 | * Is there any internal subset declarations ? |
| 8324 | * they are handled separately in xmlParseInternalSubset() |
| 8325 | */ |
| 8326 | if (RAW == '[') |
| 8327 | return; |
| 8328 | |
| 8329 | /* |
| 8330 | * We should be at the end of the DOCTYPE declaration. |
| 8331 | */ |
| 8332 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8333 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8334 | } |
| 8335 | NEXT; |
| 8336 | } |
| 8337 | |
| 8338 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8339 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8340 | * @ctxt: an XML parser context |
| 8341 | * |
| 8342 | * parse the internal subset declaration |
| 8343 | * |
| 8344 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8345 | */ |
| 8346 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8347 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8348 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 8349 | /* |
| 8350 | * Is there any DTD definition ? |
| 8351 | */ |
| 8352 | if (RAW == '[') { |
| 8353 | ctxt->instate = XML_PARSER_DTD; |
| 8354 | NEXT; |
| 8355 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8356 | * Parse the succession of Markup declarations and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8357 | * PEReferences. |
| 8358 | * Subsequence (markupdecl | PEReference | S)* |
| 8359 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8360 | while ((RAW != ']') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8361 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8362 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8363 | |
| 8364 | SKIP_BLANKS; |
| 8365 | xmlParseMarkupDecl(ctxt); |
| 8366 | xmlParsePEReference(ctxt); |
| 8367 | |
| 8368 | /* |
| 8369 | * Pop-up of finished entities. |
| 8370 | */ |
| 8371 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8372 | xmlPopInput(ctxt); |
| 8373 | |
| 8374 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8375 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8376 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8377 | break; |
| 8378 | } |
| 8379 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8380 | if (RAW == ']') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8381 | NEXT; |
| 8382 | SKIP_BLANKS; |
| 8383 | } |
| 8384 | } |
| 8385 | |
| 8386 | /* |
| 8387 | * We should be at the end of the DOCTYPE declaration. |
| 8388 | */ |
| 8389 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8390 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8391 | } |
| 8392 | NEXT; |
| 8393 | } |
| 8394 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8395 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8396 | /** |
| 8397 | * xmlParseAttribute: |
| 8398 | * @ctxt: an XML parser context |
| 8399 | * @value: a xmlChar ** used to store the value of the attribute |
| 8400 | * |
| 8401 | * parse an attribute |
| 8402 | * |
| 8403 | * [41] Attribute ::= Name Eq AttValue |
| 8404 | * |
| 8405 | * [ WFC: No External Entity References ] |
| 8406 | * Attribute values cannot contain direct or indirect entity references |
| 8407 | * to external entities. |
| 8408 | * |
| 8409 | * [ WFC: No < in Attribute Values ] |
| 8410 | * The replacement text of any entity referred to directly or indirectly in |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8411 | * an attribute value (other than "<") must not contain a <. |
| 8412 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8413 | * [ VC: Attribute Value Type ] |
| 8414 | * The attribute must have been declared; the value must be of the type |
| 8415 | * declared for it. |
| 8416 | * |
| 8417 | * [25] Eq ::= S? '=' S? |
| 8418 | * |
| 8419 | * With namespace: |
| 8420 | * |
| 8421 | * [NS 11] Attribute ::= QName Eq AttValue |
| 8422 | * |
| 8423 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 8424 | * definition. |
| 8425 | * |
| 8426 | * Returns the attribute name, and the value in *value. |
| 8427 | */ |
| 8428 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8429 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8430 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8431 | const xmlChar *name; |
| 8432 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8433 | |
| 8434 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 8435 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8436 | name = xmlParseName(ctxt); |
| 8437 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8438 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8439 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8440 | return(NULL); |
| 8441 | } |
| 8442 | |
| 8443 | /* |
| 8444 | * read the value |
| 8445 | */ |
| 8446 | SKIP_BLANKS; |
| 8447 | if (RAW == '=') { |
| 8448 | NEXT; |
| 8449 | SKIP_BLANKS; |
| 8450 | val = xmlParseAttValue(ctxt); |
| 8451 | ctxt->instate = XML_PARSER_CONTENT; |
| 8452 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8453 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8454 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8455 | return(NULL); |
| 8456 | } |
| 8457 | |
| 8458 | /* |
| 8459 | * Check that xml:lang conforms to the specification |
| 8460 | * No more registered as an error, just generate a warning now |
| 8461 | * since this was deprecated in XML second edition |
| 8462 | */ |
| 8463 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 8464 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8465 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8466 | "Malformed value for xml:lang : %s\n", |
| 8467 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8468 | } |
| 8469 | } |
| 8470 | |
| 8471 | /* |
| 8472 | * Check that xml:space conforms to the specification |
| 8473 | */ |
| 8474 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 8475 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 8476 | *(ctxt->space) = 0; |
| 8477 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 8478 | *(ctxt->space) = 1; |
| 8479 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8480 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 8481 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8482 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8483 | } |
| 8484 | } |
| 8485 | |
| 8486 | *value = val; |
| 8487 | return(name); |
| 8488 | } |
| 8489 | |
| 8490 | /** |
| 8491 | * xmlParseStartTag: |
| 8492 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8493 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8494 | * parse a start of tag either for rule element or |
| 8495 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8496 | * |
| 8497 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8498 | * |
| 8499 | * [ WFC: Unique Att Spec ] |
| 8500 | * 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] | 8501 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8502 | * |
| 8503 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8504 | * |
| 8505 | * [ WFC: Unique Att Spec ] |
| 8506 | * 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] | 8507 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8508 | * |
| 8509 | * With namespace: |
| 8510 | * |
| 8511 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8512 | * |
| 8513 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8514 | * |
| 8515 | * Returns the element name parsed |
| 8516 | */ |
| 8517 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8518 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8519 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8520 | const xmlChar *name; |
| 8521 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8522 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8523 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8524 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8525 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8526 | int i; |
| 8527 | |
| 8528 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8529 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8530 | |
| 8531 | name = xmlParseName(ctxt); |
| 8532 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8533 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8534 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8535 | return(NULL); |
| 8536 | } |
| 8537 | |
| 8538 | /* |
| 8539 | * Now parse the attributes, it ends up with the ending |
| 8540 | * |
| 8541 | * (S Attribute)* S? |
| 8542 | */ |
| 8543 | SKIP_BLANKS; |
| 8544 | GROW; |
| 8545 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8546 | while (((RAW != '>') && |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8547 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8548 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8549 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8550 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8551 | |
| 8552 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 8553 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8554 | /* |
| 8555 | * [ WFC: Unique Att Spec ] |
| 8556 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8557 | * start-tag or empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8558 | */ |
| 8559 | for (i = 0; i < nbatts;i += 2) { |
| 8560 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8561 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8562 | xmlFree(attvalue); |
| 8563 | goto failed; |
| 8564 | } |
| 8565 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8566 | /* |
| 8567 | * Add the pair to atts |
| 8568 | */ |
| 8569 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8570 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8571 | atts = (const xmlChar **) |
| 8572 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8573 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8574 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8575 | if (attvalue != NULL) |
| 8576 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8577 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8578 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8579 | ctxt->atts = atts; |
| 8580 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8581 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8582 | const xmlChar **n; |
| 8583 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8584 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8585 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8586 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8587 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8588 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8589 | if (attvalue != NULL) |
| 8590 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8591 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8592 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8593 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8594 | ctxt->atts = atts; |
| 8595 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8596 | } |
| 8597 | atts[nbatts++] = attname; |
| 8598 | atts[nbatts++] = attvalue; |
| 8599 | atts[nbatts] = NULL; |
| 8600 | atts[nbatts + 1] = NULL; |
| 8601 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | if (attvalue != NULL) |
| 8603 | xmlFree(attvalue); |
| 8604 | } |
| 8605 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 8606 | failed: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8607 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8608 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8609 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8610 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8611 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8612 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8613 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8614 | } |
| 8615 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8616 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8617 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8618 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8619 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8620 | break; |
| 8621 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8622 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8623 | GROW; |
| 8624 | } |
| 8625 | |
| 8626 | /* |
| 8627 | * SAX: Start of Element ! |
| 8628 | */ |
| 8629 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8630 | (!ctxt->disableSAX)) { |
| 8631 | if (nbatts > 0) |
| 8632 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8633 | else |
| 8634 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8635 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8636 | |
| 8637 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8638 | /* Free only the content strings */ |
| 8639 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8640 | if (atts[i] != NULL) |
| 8641 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8642 | } |
| 8643 | return(name); |
| 8644 | } |
| 8645 | |
| 8646 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8647 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8648 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8649 | * @line: line of the start tag |
| 8650 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8651 | * |
| 8652 | * parse an end of tag |
| 8653 | * |
| 8654 | * [42] ETag ::= '</' Name S? '>' |
| 8655 | * |
| 8656 | * With namespace |
| 8657 | * |
| 8658 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8659 | */ |
| 8660 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8661 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8662 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8663 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8664 | |
| 8665 | GROW; |
| 8666 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8667 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8668 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8669 | return; |
| 8670 | } |
| 8671 | SKIP(2); |
| 8672 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8673 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8674 | |
| 8675 | /* |
| 8676 | * We should definitely be at the ending "S? '>'" part |
| 8677 | */ |
| 8678 | GROW; |
| 8679 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8680 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8681 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8682 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8683 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8684 | |
| 8685 | /* |
| 8686 | * [ WFC: Element Type Match ] |
| 8687 | * 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] | 8688 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8689 | * |
| 8690 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8691 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8692 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8693 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8694 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8695 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8696 | } |
| 8697 | |
| 8698 | /* |
| 8699 | * SAX: End of Tag |
| 8700 | */ |
| 8701 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8702 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8703 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8704 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8705 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8706 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8707 | return; |
| 8708 | } |
| 8709 | |
| 8710 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8711 | * xmlParseEndTag: |
| 8712 | * @ctxt: an XML parser context |
| 8713 | * |
| 8714 | * parse an end of tag |
| 8715 | * |
| 8716 | * [42] ETag ::= '</' Name S? '>' |
| 8717 | * |
| 8718 | * With namespace |
| 8719 | * |
| 8720 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8721 | */ |
| 8722 | |
| 8723 | void |
| 8724 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8725 | xmlParseEndTag1(ctxt, 0); |
| 8726 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8727 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8728 | |
| 8729 | /************************************************************************ |
| 8730 | * * |
| 8731 | * SAX 2 specific operations * |
| 8732 | * * |
| 8733 | ************************************************************************/ |
| 8734 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8735 | /* |
| 8736 | * xmlGetNamespace: |
| 8737 | * @ctxt: an XML parser context |
| 8738 | * @prefix: the prefix to lookup |
| 8739 | * |
| 8740 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8741 | * The prefix must come from the @ctxt->dict dictionnary |
| 8742 | * |
| 8743 | * Returns the namespace name or NULL if not bound |
| 8744 | */ |
| 8745 | static const xmlChar * |
| 8746 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8747 | int i; |
| 8748 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8749 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8750 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8751 | if (ctxt->nsTab[i] == prefix) { |
| 8752 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8753 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8754 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8755 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8756 | return(NULL); |
| 8757 | } |
| 8758 | |
| 8759 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8760 | * xmlParseQName: |
| 8761 | * @ctxt: an XML parser context |
| 8762 | * @prefix: pointer to store the prefix part |
| 8763 | * |
| 8764 | * parse an XML Namespace QName |
| 8765 | * |
| 8766 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8767 | * [7] Prefix ::= NCName |
| 8768 | * [8] LocalPart ::= NCName |
| 8769 | * |
| 8770 | * Returns the Name parsed or NULL |
| 8771 | */ |
| 8772 | |
| 8773 | static const xmlChar * |
| 8774 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8775 | const xmlChar *l, *p; |
| 8776 | |
| 8777 | GROW; |
| 8778 | |
| 8779 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8780 | if (l == NULL) { |
| 8781 | if (CUR == ':') { |
| 8782 | l = xmlParseName(ctxt); |
| 8783 | if (l != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8784 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8785 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8786 | *prefix = NULL; |
| 8787 | return(l); |
| 8788 | } |
| 8789 | } |
| 8790 | return(NULL); |
| 8791 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8792 | if (CUR == ':') { |
| 8793 | NEXT; |
| 8794 | p = l; |
| 8795 | l = xmlParseNCName(ctxt); |
| 8796 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8797 | xmlChar *tmp; |
| 8798 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8799 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8800 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8801 | l = xmlParseNmtoken(ctxt); |
| 8802 | if (l == NULL) |
| 8803 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8804 | else { |
| 8805 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8806 | xmlFree((char *)l); |
| 8807 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8808 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8809 | if (tmp != NULL) xmlFree(tmp); |
| 8810 | *prefix = NULL; |
| 8811 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8812 | } |
| 8813 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8814 | xmlChar *tmp; |
| 8815 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8816 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8817 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8818 | NEXT; |
| 8819 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8820 | if (tmp != NULL) { |
| 8821 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8822 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8823 | if (tmp != NULL) xmlFree(tmp); |
| 8824 | *prefix = p; |
| 8825 | return(l); |
| 8826 | } |
| 8827 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8828 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8829 | if (tmp != NULL) xmlFree(tmp); |
| 8830 | *prefix = p; |
| 8831 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8832 | } |
| 8833 | *prefix = p; |
| 8834 | } else |
| 8835 | *prefix = NULL; |
| 8836 | return(l); |
| 8837 | } |
| 8838 | |
| 8839 | /** |
| 8840 | * xmlParseQNameAndCompare: |
| 8841 | * @ctxt: an XML parser context |
| 8842 | * @name: the localname |
| 8843 | * @prefix: the prefix, if any. |
| 8844 | * |
| 8845 | * parse an XML name and compares for match |
| 8846 | * (specialized for endtag parsing) |
| 8847 | * |
| 8848 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8849 | * and the name for mismatch |
| 8850 | */ |
| 8851 | |
| 8852 | static const xmlChar * |
| 8853 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8854 | xmlChar const *prefix) { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8855 | const xmlChar *cmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8856 | const xmlChar *in; |
| 8857 | const xmlChar *ret; |
| 8858 | const xmlChar *prefix2; |
| 8859 | |
| 8860 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8861 | |
| 8862 | GROW; |
| 8863 | in = ctxt->input->cur; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8864 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8865 | cmp = prefix; |
| 8866 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8867 | ++in; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8868 | ++cmp; |
| 8869 | } |
| 8870 | if ((*cmp == 0) && (*in == ':')) { |
| 8871 | in++; |
| 8872 | cmp = name; |
| 8873 | while (*in != 0 && *in == *cmp) { |
| 8874 | ++in; |
| 8875 | ++cmp; |
| 8876 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8877 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8878 | /* success */ |
| 8879 | ctxt->input->cur = in; |
| 8880 | return((const xmlChar*) 1); |
| 8881 | } |
| 8882 | } |
| 8883 | /* |
| 8884 | * all strings coms from the dictionary, equality can be done directly |
| 8885 | */ |
| 8886 | ret = xmlParseQName (ctxt, &prefix2); |
| 8887 | if ((ret == name) && (prefix == prefix2)) |
| 8888 | return((const xmlChar*) 1); |
| 8889 | return ret; |
| 8890 | } |
| 8891 | |
| 8892 | /** |
| 8893 | * xmlParseAttValueInternal: |
| 8894 | * @ctxt: an XML parser context |
| 8895 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8896 | * @alloc: whether the attribute was reallocated as a new string |
| 8897 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8898 | * |
| 8899 | * parse a value for an attribute. |
| 8900 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8901 | * directly from the data buffer. |
| 8902 | * |
| 8903 | * 3.3.3 Attribute-Value Normalization: |
| 8904 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8905 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8906 | * - a character reference is processed by appending the referenced |
| 8907 | * character to the attribute value |
| 8908 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8909 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8910 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8911 | * appending #x20 to the normalized value, except that only a single |
| 8912 | * #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] | 8913 | * parsed entity or the literal entity value of an internal parsed entity |
| 8914 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8915 | * If the declared value is not CDATA, then the XML processor must further |
| 8916 | * process the normalized attribute value by discarding any leading and |
| 8917 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8918 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8919 | * All attributes for which no declaration has been read should be treated |
| 8920 | * by a non-validating parser as if declared CDATA. |
| 8921 | * |
| 8922 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8923 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8924 | */ |
| 8925 | |
| 8926 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8927 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8928 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8929 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8930 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8931 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8932 | xmlChar *ret = NULL; |
| 8933 | |
| 8934 | GROW; |
| 8935 | in = (xmlChar *) CUR_PTR; |
| 8936 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8937 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8938 | return (NULL); |
| 8939 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8940 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8941 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8942 | /* |
| 8943 | * try to handle in this routine the most common case where no |
| 8944 | * allocation of a new string is required and where content is |
| 8945 | * pure ASCII. |
| 8946 | */ |
| 8947 | limit = *in++; |
| 8948 | end = ctxt->input->end; |
| 8949 | start = in; |
| 8950 | if (in >= end) { |
| 8951 | const xmlChar *oldbase = ctxt->input->base; |
| 8952 | GROW; |
| 8953 | if (oldbase != ctxt->input->base) { |
| 8954 | long delta = ctxt->input->base - oldbase; |
| 8955 | start = start + delta; |
| 8956 | in = in + delta; |
| 8957 | } |
| 8958 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8959 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8960 | if (normalize) { |
| 8961 | /* |
| 8962 | * Skip any leading spaces |
| 8963 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8964 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8965 | ((*in == 0x20) || (*in == 0x9) || |
| 8966 | (*in == 0xA) || (*in == 0xD))) { |
| 8967 | in++; |
| 8968 | start = in; |
| 8969 | if (in >= end) { |
| 8970 | const xmlChar *oldbase = ctxt->input->base; |
| 8971 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8972 | if (ctxt->instate == XML_PARSER_EOF) |
| 8973 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8974 | if (oldbase != ctxt->input->base) { |
| 8975 | long delta = ctxt->input->base - oldbase; |
| 8976 | start = start + delta; |
| 8977 | in = in + delta; |
| 8978 | } |
| 8979 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8980 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8981 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8982 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8983 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8984 | return(NULL); |
| 8985 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8986 | } |
| 8987 | } |
| 8988 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8989 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8990 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 8991 | if (in >= end) { |
| 8992 | const xmlChar *oldbase = ctxt->input->base; |
| 8993 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8994 | if (ctxt->instate == XML_PARSER_EOF) |
| 8995 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8996 | if (oldbase != ctxt->input->base) { |
| 8997 | long delta = ctxt->input->base - oldbase; |
| 8998 | start = start + delta; |
| 8999 | in = in + delta; |
| 9000 | } |
| 9001 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9002 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9003 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9004 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9005 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9006 | return(NULL); |
| 9007 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9008 | } |
| 9009 | } |
| 9010 | last = in; |
| 9011 | /* |
| 9012 | * skip the trailing blanks |
| 9013 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 9014 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9015 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9016 | ((*in == 0x20) || (*in == 0x9) || |
| 9017 | (*in == 0xA) || (*in == 0xD))) { |
| 9018 | in++; |
| 9019 | if (in >= end) { |
| 9020 | const xmlChar *oldbase = ctxt->input->base; |
| 9021 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9022 | if (ctxt->instate == XML_PARSER_EOF) |
| 9023 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9024 | if (oldbase != ctxt->input->base) { |
| 9025 | long delta = ctxt->input->base - oldbase; |
| 9026 | start = start + delta; |
| 9027 | in = in + delta; |
| 9028 | last = last + delta; |
| 9029 | } |
| 9030 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9031 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9032 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9033 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9034 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9035 | return(NULL); |
| 9036 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9037 | } |
| 9038 | } |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9039 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9040 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9041 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9042 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9043 | return(NULL); |
| 9044 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9045 | if (*in != limit) goto need_complex; |
| 9046 | } else { |
| 9047 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 9048 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 9049 | in++; |
| 9050 | if (in >= end) { |
| 9051 | const xmlChar *oldbase = ctxt->input->base; |
| 9052 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9053 | if (ctxt->instate == XML_PARSER_EOF) |
| 9054 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9055 | if (oldbase != ctxt->input->base) { |
| 9056 | long delta = ctxt->input->base - oldbase; |
| 9057 | start = start + delta; |
| 9058 | in = in + delta; |
| 9059 | } |
| 9060 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9061 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9062 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9063 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9064 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9065 | return(NULL); |
| 9066 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9067 | } |
| 9068 | } |
| 9069 | last = in; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9070 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 9071 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9072 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 9073 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 9074 | return(NULL); |
| 9075 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9076 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9077 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9078 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9079 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9080 | *len = last - start; |
| 9081 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9082 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9083 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9084 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9085 | } |
| 9086 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9087 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9088 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9089 | need_complex: |
| 9090 | if (alloc) *alloc = 1; |
| 9091 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9092 | } |
| 9093 | |
| 9094 | /** |
| 9095 | * xmlParseAttribute2: |
| 9096 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9097 | * @pref: the element prefix |
| 9098 | * @elem: the element name |
| 9099 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9100 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9101 | * @len: an int * to save the length of the attribute |
| 9102 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9103 | * |
| 9104 | * parse an attribute in the new SAX2 framework. |
| 9105 | * |
| 9106 | * Returns the attribute name, and the value in *value, . |
| 9107 | */ |
| 9108 | |
| 9109 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9110 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9111 | const xmlChar * pref, const xmlChar * elem, |
| 9112 | const xmlChar ** prefix, xmlChar ** value, |
| 9113 | int *len, int *alloc) |
| 9114 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9115 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 9116 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9117 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9118 | |
| 9119 | *value = NULL; |
| 9120 | GROW; |
| 9121 | name = xmlParseQName(ctxt, prefix); |
| 9122 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9123 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9124 | "error parsing attribute name\n"); |
| 9125 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9126 | } |
| 9127 | |
| 9128 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9129 | * get the type if needed |
| 9130 | */ |
| 9131 | if (ctxt->attsSpecial != NULL) { |
| 9132 | int type; |
| 9133 | |
| 9134 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9135 | pref, elem, *prefix, name); |
| 9136 | if (type != 0) |
| 9137 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
| 9140 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9141 | * read the value |
| 9142 | */ |
| 9143 | SKIP_BLANKS; |
| 9144 | if (RAW == '=') { |
| 9145 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9146 | SKIP_BLANKS; |
| 9147 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 9148 | if (normalize) { |
| 9149 | /* |
| 9150 | * Sometimes a second normalisation pass for spaces is needed |
| 9151 | * but that only happens if charrefs or entities refernces |
| 9152 | * have been used in the attribute value, i.e. the attribute |
| 9153 | * value have been extracted in an allocated string already. |
| 9154 | */ |
| 9155 | if (*alloc) { |
| 9156 | const xmlChar *val2; |
| 9157 | |
| 9158 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9159 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9160 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 9161 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9162 | } |
| 9163 | } |
| 9164 | } |
| 9165 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9166 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9167 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 9168 | "Specification mandate value for attribute %s\n", |
| 9169 | name); |
| 9170 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9173 | if (*prefix == ctxt->str_xml) { |
| 9174 | /* |
| 9175 | * Check that xml:lang conforms to the specification |
| 9176 | * No more registered as an error, just generate a warning now |
| 9177 | * since this was deprecated in XML second edition |
| 9178 | */ |
| 9179 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 9180 | internal_val = xmlStrndup(val, *len); |
| 9181 | if (!xmlCheckLanguageID(internal_val)) { |
| 9182 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 9183 | "Malformed value for xml:lang : %s\n", |
| 9184 | internal_val, NULL); |
| 9185 | } |
| 9186 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9187 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9188 | /* |
| 9189 | * Check that xml:space conforms to the specification |
| 9190 | */ |
| 9191 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 9192 | internal_val = xmlStrndup(val, *len); |
| 9193 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 9194 | *(ctxt->space) = 0; |
| 9195 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 9196 | *(ctxt->space) = 1; |
| 9197 | else { |
| 9198 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 9199 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 9200 | internal_val, NULL); |
| 9201 | } |
| 9202 | } |
| 9203 | if (internal_val) { |
| 9204 | xmlFree(internal_val); |
| 9205 | } |
| 9206 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9207 | |
| 9208 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9209 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9210 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9211 | /** |
| 9212 | * xmlParseStartTag2: |
| 9213 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9214 | * |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9215 | * parse a start of tag either for rule element or |
| 9216 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 9217 | * This routine is called when running SAX2 parsing |
| 9218 | * |
| 9219 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 9220 | * |
| 9221 | * [ WFC: Unique Att Spec ] |
| 9222 | * 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] | 9223 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9224 | * |
| 9225 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 9226 | * |
| 9227 | * [ WFC: Unique Att Spec ] |
| 9228 | * 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] | 9229 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9230 | * |
| 9231 | * With namespace: |
| 9232 | * |
| 9233 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 9234 | * |
| 9235 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 9236 | * |
| 9237 | * Returns the element name parsed |
| 9238 | */ |
| 9239 | |
| 9240 | static const xmlChar * |
| 9241 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9242 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9243 | const xmlChar *localname; |
| 9244 | const xmlChar *prefix; |
| 9245 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9246 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9247 | const xmlChar *nsname; |
| 9248 | xmlChar *attvalue; |
| 9249 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9250 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9251 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9252 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9253 | const xmlChar *base; |
| 9254 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9255 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9256 | |
| 9257 | if (RAW != '<') return(NULL); |
| 9258 | NEXT1; |
| 9259 | |
| 9260 | /* |
| 9261 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 9262 | * point since the attribute values may be stored as pointers to |
| 9263 | * the buffer and calling SHRINK would destroy them ! |
| 9264 | * The Shrinking is only possible once the full set of attribute |
| 9265 | * callbacks have been done. |
| 9266 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9267 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9268 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9269 | base = ctxt->input->base; |
| 9270 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9271 | oldline = ctxt->input->line; |
| 9272 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9273 | nbatts = 0; |
| 9274 | nratts = 0; |
| 9275 | nbdef = 0; |
| 9276 | nbNs = 0; |
| 9277 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9278 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 9279 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9280 | |
| 9281 | localname = xmlParseQName(ctxt, &prefix); |
| 9282 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9283 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9284 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9285 | return(NULL); |
| 9286 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9287 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9288 | |
| 9289 | /* |
| 9290 | * Now parse the attributes, it ends up with the ending |
| 9291 | * |
| 9292 | * (S Attribute)* S? |
| 9293 | */ |
| 9294 | SKIP_BLANKS; |
| 9295 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9296 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9297 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9298 | while (((RAW != '>') && |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9299 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9300 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9301 | const xmlChar *q = CUR_PTR; |
| 9302 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9303 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9304 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9305 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 9306 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9307 | if (ctxt->input->base != base) { |
| 9308 | if ((attvalue != NULL) && (alloc != 0)) |
| 9309 | xmlFree(attvalue); |
| 9310 | attvalue = NULL; |
| 9311 | goto base_changed; |
| 9312 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9313 | if ((attname != NULL) && (attvalue != NULL)) { |
| 9314 | if (len < 0) len = xmlStrlen(attvalue); |
| 9315 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9316 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9317 | xmlURIPtr uri; |
| 9318 | |
Daniel Veillard | c836ba6 | 2014-07-14 16:39:50 +0800 | [diff] [blame^] | 9319 | if (URL == NULL) { |
| 9320 | xmlErrMemory(ctxt, "dictionary allocation failure"); |
| 9321 | if ((attvalue != NULL) && (alloc != 0)) |
| 9322 | xmlFree(attvalue); |
| 9323 | return(NULL); |
| 9324 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9325 | if (*URL != 0) { |
| 9326 | uri = xmlParseURI((const char *) URL); |
| 9327 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9328 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9329 | "xmlns: '%s' is not a valid URI\n", |
| 9330 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9331 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 9332 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9333 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9334 | "xmlns: URI %s is not absolute\n", |
| 9335 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9336 | } |
| 9337 | xmlFreeURI(uri); |
| 9338 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9339 | if (URL == ctxt->str_xml_ns) { |
| 9340 | if (attname != ctxt->str_xml) { |
| 9341 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9342 | "xml namespace URI cannot be the default namespace\n", |
| 9343 | NULL, NULL, NULL); |
| 9344 | } |
| 9345 | goto skip_default_ns; |
| 9346 | } |
| 9347 | if ((len == 29) && |
| 9348 | (xmlStrEqual(URL, |
| 9349 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9350 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9351 | "reuse of the xmlns namespace name is forbidden\n", |
| 9352 | NULL, NULL, NULL); |
| 9353 | goto skip_default_ns; |
| 9354 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9355 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9356 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9357 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9358 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9359 | for (j = 1;j <= nbNs;j++) |
| 9360 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9361 | break; |
| 9362 | if (j <= nbNs) |
| 9363 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 9364 | else |
| 9365 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9366 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9367 | if (alloc != 0) xmlFree(attvalue); |
| 9368 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9369 | continue; |
| 9370 | } |
| 9371 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9372 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9373 | xmlURIPtr uri; |
| 9374 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9375 | if (attname == ctxt->str_xml) { |
| 9376 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9377 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9378 | "xml namespace prefix mapped to wrong URI\n", |
| 9379 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9380 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9381 | /* |
| 9382 | * Do not keep a namespace definition node |
| 9383 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9384 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9385 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9386 | if (URL == ctxt->str_xml_ns) { |
| 9387 | if (attname != ctxt->str_xml) { |
| 9388 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9389 | "xml namespace URI mapped to wrong prefix\n", |
| 9390 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9391 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9392 | goto skip_ns; |
| 9393 | } |
| 9394 | if (attname == ctxt->str_xmlns) { |
| 9395 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9396 | "redefinition of the xmlns prefix is forbidden\n", |
| 9397 | NULL, NULL, NULL); |
| 9398 | goto skip_ns; |
| 9399 | } |
| 9400 | if ((len == 29) && |
| 9401 | (xmlStrEqual(URL, |
| 9402 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9403 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9404 | "reuse of the xmlns namespace name is forbidden\n", |
| 9405 | NULL, NULL, NULL); |
| 9406 | goto skip_ns; |
| 9407 | } |
| 9408 | if ((URL == NULL) || (URL[0] == 0)) { |
| 9409 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9410 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 9411 | attname, NULL, NULL); |
| 9412 | goto skip_ns; |
| 9413 | } else { |
| 9414 | uri = xmlParseURI((const char *) URL); |
| 9415 | if (uri == NULL) { |
| 9416 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9417 | "xmlns:%s: '%s' is not a valid URI\n", |
| 9418 | attname, URL, NULL); |
| 9419 | } else { |
| 9420 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 9421 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9422 | "xmlns:%s: URI %s is not absolute\n", |
| 9423 | attname, URL, NULL); |
| 9424 | } |
| 9425 | xmlFreeURI(uri); |
| 9426 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9427 | } |
| 9428 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9429 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9430 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9431 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9432 | for (j = 1;j <= nbNs;j++) |
| 9433 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9434 | break; |
| 9435 | if (j <= nbNs) |
| 9436 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 9437 | else |
| 9438 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9439 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9440 | if (alloc != 0) xmlFree(attvalue); |
| 9441 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 9442 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9443 | continue; |
| 9444 | } |
| 9445 | |
| 9446 | /* |
| 9447 | * Add the pair to atts |
| 9448 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9449 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9450 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9451 | if (attvalue[len] == 0) |
| 9452 | xmlFree(attvalue); |
| 9453 | goto failed; |
| 9454 | } |
| 9455 | maxatts = ctxt->maxatts; |
| 9456 | atts = ctxt->atts; |
| 9457 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9458 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9459 | atts[nbatts++] = attname; |
| 9460 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9461 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9462 | atts[nbatts++] = attvalue; |
| 9463 | attvalue += len; |
| 9464 | atts[nbatts++] = attvalue; |
| 9465 | /* |
| 9466 | * tag if some deallocation is needed |
| 9467 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9468 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9469 | } else { |
| 9470 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 9471 | xmlFree(attvalue); |
| 9472 | } |
| 9473 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9474 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9475 | |
| 9476 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9477 | if (ctxt->instate == XML_PARSER_EOF) |
| 9478 | break; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9479 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9480 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9481 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9482 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9483 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9484 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9485 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9486 | } |
| 9487 | SKIP_BLANKS; |
| 9488 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 9489 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9490 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9491 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9492 | break; |
| 9493 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9494 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9495 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9496 | } |
| 9497 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9498 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9499 | * The attributes defaulting |
| 9500 | */ |
| 9501 | if (ctxt->attsDefault != NULL) { |
| 9502 | xmlDefAttrsPtr defaults; |
| 9503 | |
| 9504 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 9505 | if (defaults != NULL) { |
| 9506 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9507 | attname = defaults->values[5 * i]; |
| 9508 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9509 | |
| 9510 | /* |
| 9511 | * special work for namespaces defaulted defs |
| 9512 | */ |
| 9513 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 9514 | /* |
| 9515 | * check that it's not a defined namespace |
| 9516 | */ |
| 9517 | for (j = 1;j <= nbNs;j++) |
| 9518 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9519 | break; |
| 9520 | if (j <= nbNs) continue; |
| 9521 | |
| 9522 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9523 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9524 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9525 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9526 | nbNs++; |
| 9527 | } |
| 9528 | } else if (aprefix == ctxt->str_xmlns) { |
| 9529 | /* |
| 9530 | * check that it's not a defined namespace |
| 9531 | */ |
| 9532 | for (j = 1;j <= nbNs;j++) |
| 9533 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9534 | break; |
| 9535 | if (j <= nbNs) continue; |
| 9536 | |
| 9537 | nsname = xmlGetNamespace(ctxt, attname); |
| 9538 | if (nsname != defaults->values[2]) { |
| 9539 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9540 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9541 | nbNs++; |
| 9542 | } |
| 9543 | } else { |
| 9544 | /* |
| 9545 | * check that it's not a defined attribute |
| 9546 | */ |
| 9547 | for (j = 0;j < nbatts;j+=5) { |
| 9548 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 9549 | break; |
| 9550 | } |
| 9551 | if (j < nbatts) continue; |
| 9552 | |
| 9553 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9554 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 9555 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9556 | } |
| 9557 | maxatts = ctxt->maxatts; |
| 9558 | atts = ctxt->atts; |
| 9559 | } |
| 9560 | atts[nbatts++] = attname; |
| 9561 | atts[nbatts++] = aprefix; |
| 9562 | if (aprefix == NULL) |
| 9563 | atts[nbatts++] = NULL; |
| 9564 | else |
| 9565 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9566 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 9567 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 9568 | if ((ctxt->standalone == 1) && |
| 9569 | (defaults->values[5 * i + 4] != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9570 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9571 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 9572 | attname, localname); |
| 9573 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9574 | nbdef++; |
| 9575 | } |
| 9576 | } |
| 9577 | } |
| 9578 | } |
| 9579 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9580 | /* |
| 9581 | * The attributes checkings |
| 9582 | */ |
| 9583 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 9584 | /* |
| 9585 | * The default namespace does not apply to attribute names. |
| 9586 | */ |
| 9587 | if (atts[i + 1] != NULL) { |
| 9588 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 9589 | if (nsname == NULL) { |
| 9590 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9591 | "Namespace prefix %s for %s on %s is not defined\n", |
| 9592 | atts[i + 1], atts[i], localname); |
| 9593 | } |
| 9594 | atts[i + 2] = nsname; |
| 9595 | } else |
| 9596 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9597 | /* |
| 9598 | * [ WFC: Unique Att Spec ] |
| 9599 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9600 | * start-tag or empty-element tag. |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9601 | * As extended by the Namespace in XML REC. |
| 9602 | */ |
| 9603 | for (j = 0; j < i;j += 5) { |
| 9604 | if (atts[i] == atts[j]) { |
| 9605 | if (atts[i+1] == atts[j+1]) { |
| 9606 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 9607 | break; |
| 9608 | } |
| 9609 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 9610 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 9611 | "Namespaced Attribute %s in '%s' redefined\n", |
| 9612 | atts[i], nsname, NULL); |
| 9613 | break; |
| 9614 | } |
| 9615 | } |
| 9616 | } |
| 9617 | } |
| 9618 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9619 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9620 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9621 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9622 | "Namespace prefix %s on %s is not defined\n", |
| 9623 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9624 | } |
| 9625 | *pref = prefix; |
| 9626 | *URI = nsname; |
| 9627 | |
| 9628 | /* |
| 9629 | * SAX: Start of Element ! |
| 9630 | */ |
| 9631 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9632 | (!ctxt->disableSAX)) { |
| 9633 | if (nbNs > 0) |
| 9634 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9635 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9636 | nbatts / 5, nbdef, atts); |
| 9637 | else |
| 9638 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9639 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9640 | } |
| 9641 | |
| 9642 | /* |
| 9643 | * Free up attribute allocated strings if needed |
| 9644 | */ |
| 9645 | if (attval != 0) { |
| 9646 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9647 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9648 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9649 | } |
| 9650 | |
| 9651 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9652 | |
| 9653 | base_changed: |
| 9654 | /* |
| 9655 | * the attribute strings are valid iif the base didn't changed |
| 9656 | */ |
| 9657 | if (attval != 0) { |
| 9658 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9659 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9660 | xmlFree((xmlChar *) atts[i]); |
| 9661 | } |
| 9662 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9663 | ctxt->input->line = oldline; |
| 9664 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9665 | if (ctxt->wellFormed == 1) { |
| 9666 | goto reparse; |
| 9667 | } |
| 9668 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9669 | } |
| 9670 | |
| 9671 | /** |
| 9672 | * xmlParseEndTag2: |
| 9673 | * @ctxt: an XML parser context |
| 9674 | * @line: line of the start tag |
| 9675 | * @nsNr: number of namespaces on the start tag |
| 9676 | * |
| 9677 | * parse an end of tag |
| 9678 | * |
| 9679 | * [42] ETag ::= '</' Name S? '>' |
| 9680 | * |
| 9681 | * With namespace |
| 9682 | * |
| 9683 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9684 | */ |
| 9685 | |
| 9686 | static void |
| 9687 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9688 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9689 | const xmlChar *name; |
| 9690 | |
| 9691 | GROW; |
| 9692 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9693 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9694 | return; |
| 9695 | } |
| 9696 | SKIP(2); |
| 9697 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9698 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9699 | if (ctxt->input->cur[tlen] == '>') { |
| 9700 | ctxt->input->cur += tlen + 1; |
| 9701 | goto done; |
| 9702 | } |
| 9703 | ctxt->input->cur += tlen; |
| 9704 | name = (xmlChar*)1; |
| 9705 | } else { |
| 9706 | if (prefix == NULL) |
| 9707 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9708 | else |
| 9709 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9710 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9711 | |
| 9712 | /* |
| 9713 | * We should definitely be at the ending "S? '>'" part |
| 9714 | */ |
| 9715 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9716 | if (ctxt->instate == XML_PARSER_EOF) |
| 9717 | return; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9718 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9719 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9720 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9721 | } else |
| 9722 | NEXT1; |
| 9723 | |
| 9724 | /* |
| 9725 | * [ WFC: Element Type Match ] |
| 9726 | * 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] | 9727 | * start-tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9728 | * |
| 9729 | */ |
| 9730 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9731 | if (name == NULL) name = BAD_CAST "unparseable"; |
Daniel Veillard | 852505b | 2009-08-23 15:44:48 +0200 | [diff] [blame] | 9732 | if ((line == 0) && (ctxt->node != NULL)) |
| 9733 | line = ctxt->node->line; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9734 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9735 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9736 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9737 | } |
| 9738 | |
| 9739 | /* |
| 9740 | * SAX: End of Tag |
| 9741 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9742 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9743 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9744 | (!ctxt->disableSAX)) |
| 9745 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9746 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9747 | spacePop(ctxt); |
| 9748 | if (nsNr != 0) |
| 9749 | nsPop(ctxt, nsNr); |
| 9750 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9751 | } |
| 9752 | |
| 9753 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9754 | * xmlParseCDSect: |
| 9755 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9756 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9757 | * Parse escaped pure raw content. |
| 9758 | * |
| 9759 | * [18] CDSect ::= CDStart CData CDEnd |
| 9760 | * |
| 9761 | * [19] CDStart ::= '<![CDATA[' |
| 9762 | * |
| 9763 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9764 | * |
| 9765 | * [21] CDEnd ::= ']]>' |
| 9766 | */ |
| 9767 | void |
| 9768 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9769 | xmlChar *buf = NULL; |
| 9770 | int len = 0; |
| 9771 | int size = XML_PARSER_BUFFER_SIZE; |
| 9772 | int r, rl; |
| 9773 | int s, sl; |
| 9774 | int cur, l; |
| 9775 | int count = 0; |
| 9776 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9777 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9778 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9779 | SKIP(9); |
| 9780 | } else |
| 9781 | return; |
| 9782 | |
| 9783 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9784 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9785 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9786 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9787 | ctxt->instate = XML_PARSER_CONTENT; |
| 9788 | return; |
| 9789 | } |
| 9790 | NEXTL(rl); |
| 9791 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9792 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9793 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9794 | ctxt->instate = XML_PARSER_CONTENT; |
| 9795 | return; |
| 9796 | } |
| 9797 | NEXTL(sl); |
| 9798 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9799 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9800 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9801 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9802 | return; |
| 9803 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9804 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9805 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9806 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9807 | xmlChar *tmp; |
| 9808 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9809 | if ((size > XML_MAX_TEXT_LENGTH) && |
| 9810 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9811 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
| 9812 | "CData section too big found", NULL); |
| 9813 | xmlFree (buf); |
| 9814 | return; |
| 9815 | } |
| 9816 | tmp = (xmlChar *) xmlRealloc(buf, size * 2 * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9817 | if (tmp == NULL) { |
| 9818 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9819 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9820 | return; |
| 9821 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9822 | buf = tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9823 | size *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9824 | } |
| 9825 | COPY_BUF(rl,buf,len,r); |
| 9826 | r = s; |
| 9827 | rl = sl; |
| 9828 | s = cur; |
| 9829 | sl = l; |
| 9830 | count++; |
| 9831 | if (count > 50) { |
| 9832 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9833 | if (ctxt->instate == XML_PARSER_EOF) { |
| 9834 | xmlFree(buf); |
| 9835 | return; |
| 9836 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9837 | count = 0; |
| 9838 | } |
| 9839 | NEXTL(l); |
| 9840 | cur = CUR_CHAR(l); |
| 9841 | } |
| 9842 | buf[len] = 0; |
| 9843 | ctxt->instate = XML_PARSER_CONTENT; |
| 9844 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9845 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9846 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9847 | xmlFree(buf); |
| 9848 | return; |
| 9849 | } |
| 9850 | NEXTL(l); |
| 9851 | |
| 9852 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9853 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9854 | */ |
| 9855 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9856 | if (ctxt->sax->cdataBlock != NULL) |
| 9857 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9858 | else if (ctxt->sax->characters != NULL) |
| 9859 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9860 | } |
| 9861 | xmlFree(buf); |
| 9862 | } |
| 9863 | |
| 9864 | /** |
| 9865 | * xmlParseContent: |
| 9866 | * @ctxt: an XML parser context |
| 9867 | * |
| 9868 | * Parse a content: |
| 9869 | * |
| 9870 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9871 | */ |
| 9872 | |
| 9873 | void |
| 9874 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9875 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9876 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9877 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9878 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9879 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9880 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9881 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9882 | |
| 9883 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9884 | * First case : a Processing Instruction. |
| 9885 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9886 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9887 | xmlParsePI(ctxt); |
| 9888 | } |
| 9889 | |
| 9890 | /* |
| 9891 | * Second case : a CDSection |
| 9892 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9893 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9894 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9895 | xmlParseCDSect(ctxt); |
| 9896 | } |
| 9897 | |
| 9898 | /* |
| 9899 | * Third case : a comment |
| 9900 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9901 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9902 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9903 | xmlParseComment(ctxt); |
| 9904 | ctxt->instate = XML_PARSER_CONTENT; |
| 9905 | } |
| 9906 | |
| 9907 | /* |
| 9908 | * Fourth case : a sub-element. |
| 9909 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9910 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9911 | xmlParseElement(ctxt); |
| 9912 | } |
| 9913 | |
| 9914 | /* |
| 9915 | * Fifth case : a reference. If if has not been resolved, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9916 | * parsing returns it's Name, create the node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9917 | */ |
| 9918 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9919 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9920 | xmlParseReference(ctxt); |
| 9921 | } |
| 9922 | |
| 9923 | /* |
| 9924 | * Last case, text. Note that References are handled directly. |
| 9925 | */ |
| 9926 | else { |
| 9927 | xmlParseCharData(ctxt, 0); |
| 9928 | } |
| 9929 | |
| 9930 | GROW; |
| 9931 | /* |
| 9932 | * Pop-up of finished entities. |
| 9933 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9934 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9935 | xmlPopInput(ctxt); |
| 9936 | SHRINK; |
| 9937 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9938 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9939 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9940 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9941 | ctxt->instate = XML_PARSER_EOF; |
| 9942 | break; |
| 9943 | } |
| 9944 | } |
| 9945 | } |
| 9946 | |
| 9947 | /** |
| 9948 | * xmlParseElement: |
| 9949 | * @ctxt: an XML parser context |
| 9950 | * |
| 9951 | * parse an XML element, this is highly recursive |
| 9952 | * |
| 9953 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9954 | * |
| 9955 | * [ WFC: Element Type Match ] |
| 9956 | * 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] | 9957 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9958 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9959 | */ |
| 9960 | |
| 9961 | void |
| 9962 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 9963 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 9964 | const xmlChar *prefix = NULL; |
| 9965 | const xmlChar *URI = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9966 | xmlParserNodeInfo node_info; |
Daniel Veillard | ed35d3d | 2012-05-11 10:52:27 +0800 | [diff] [blame] | 9967 | int line, tlen = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9968 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9969 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9970 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 9971 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 9972 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9973 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9974 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 9975 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9976 | ctxt->instate = XML_PARSER_EOF; |
| 9977 | return; |
| 9978 | } |
| 9979 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9980 | /* Capture start position */ |
| 9981 | if (ctxt->record_info) { |
| 9982 | node_info.begin_pos = ctxt->input->consumed + |
| 9983 | (CUR_PTR - ctxt->input->base); |
| 9984 | node_info.begin_line = ctxt->input->line; |
| 9985 | } |
| 9986 | |
| 9987 | if (ctxt->spaceNr == 0) |
| 9988 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 9989 | else if (*ctxt->space == -2) |
| 9990 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9991 | else |
| 9992 | spacePush(ctxt, *ctxt->space); |
| 9993 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9994 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9995 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9996 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9997 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9998 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9999 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10000 | else |
| 10001 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10002 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 10003 | if (ctxt->instate == XML_PARSER_EOF) |
| 10004 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10005 | if (name == NULL) { |
| 10006 | spacePop(ctxt); |
| 10007 | return; |
| 10008 | } |
| 10009 | namePush(ctxt, name); |
| 10010 | ret = ctxt->node; |
| 10011 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10012 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10013 | /* |
| 10014 | * [ VC: Root Element Type ] |
| 10015 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10016 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10017 | */ |
| 10018 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 10019 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 10020 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10021 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10022 | |
| 10023 | /* |
| 10024 | * Check for an Empty Element. |
| 10025 | */ |
| 10026 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 10027 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10028 | if (ctxt->sax2) { |
| 10029 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 10030 | (!ctxt->disableSAX)) |
| 10031 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
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 | } else { |
| 10034 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 10035 | (!ctxt->disableSAX)) |
| 10036 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10037 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10038 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10039 | namePop(ctxt); |
| 10040 | spacePop(ctxt); |
| 10041 | if (nsNr != ctxt->nsNr) |
| 10042 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10043 | if ( ret != NULL && ctxt->record_info ) { |
| 10044 | node_info.end_pos = ctxt->input->consumed + |
| 10045 | (CUR_PTR - ctxt->input->base); |
| 10046 | node_info.end_line = ctxt->input->line; |
| 10047 | node_info.node = ret; |
| 10048 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10049 | } |
| 10050 | return; |
| 10051 | } |
| 10052 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 10053 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10054 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10055 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 10056 | "Couldn't find end of Start Tag %s line %d\n", |
| 10057 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10058 | |
| 10059 | /* |
| 10060 | * end of parsing of this node. |
| 10061 | */ |
| 10062 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10063 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10064 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10065 | if (nsNr != ctxt->nsNr) |
| 10066 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10067 | |
| 10068 | /* |
| 10069 | * Capture end position and add node |
| 10070 | */ |
| 10071 | if ( ret != NULL && ctxt->record_info ) { |
| 10072 | node_info.end_pos = ctxt->input->consumed + |
| 10073 | (CUR_PTR - ctxt->input->base); |
| 10074 | node_info.end_line = ctxt->input->line; |
| 10075 | node_info.node = ret; |
| 10076 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10077 | } |
| 10078 | return; |
| 10079 | } |
| 10080 | |
| 10081 | /* |
| 10082 | * Parse the content of the element: |
| 10083 | */ |
| 10084 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10085 | if (ctxt->instate == XML_PARSER_EOF) |
| 10086 | return; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10087 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10088 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 10089 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10090 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10091 | |
| 10092 | /* |
| 10093 | * end of parsing of this node. |
| 10094 | */ |
| 10095 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10096 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10097 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10098 | if (nsNr != ctxt->nsNr) |
| 10099 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10100 | return; |
| 10101 | } |
| 10102 | |
| 10103 | /* |
| 10104 | * parse the end of tag: '</' should be here. |
| 10105 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10106 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10107 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10108 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10109 | } |
| 10110 | #ifdef LIBXML_SAX1_ENABLED |
| 10111 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10112 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10113 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10114 | |
| 10115 | /* |
| 10116 | * Capture end position and add node |
| 10117 | */ |
| 10118 | if ( ret != NULL && ctxt->record_info ) { |
| 10119 | node_info.end_pos = ctxt->input->consumed + |
| 10120 | (CUR_PTR - ctxt->input->base); |
| 10121 | node_info.end_line = ctxt->input->line; |
| 10122 | node_info.node = ret; |
| 10123 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10124 | } |
| 10125 | } |
| 10126 | |
| 10127 | /** |
| 10128 | * xmlParseVersionNum: |
| 10129 | * @ctxt: an XML parser context |
| 10130 | * |
| 10131 | * parse the XML version value. |
| 10132 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10133 | * [26] VersionNum ::= '1.' [0-9]+ |
| 10134 | * |
| 10135 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10136 | * |
| 10137 | * Returns the string giving the XML version number, or NULL |
| 10138 | */ |
| 10139 | xmlChar * |
| 10140 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 10141 | xmlChar *buf = NULL; |
| 10142 | int len = 0; |
| 10143 | int size = 10; |
| 10144 | xmlChar cur; |
| 10145 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10146 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10147 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10148 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10149 | return(NULL); |
| 10150 | } |
| 10151 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10152 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10153 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10154 | return(NULL); |
| 10155 | } |
| 10156 | buf[len++] = cur; |
| 10157 | NEXT; |
| 10158 | cur=CUR; |
| 10159 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10160 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10161 | return(NULL); |
| 10162 | } |
| 10163 | buf[len++] = cur; |
| 10164 | NEXT; |
| 10165 | cur=CUR; |
| 10166 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10167 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10168 | xmlChar *tmp; |
| 10169 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10170 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10171 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10172 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10173 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10174 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10175 | return(NULL); |
| 10176 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10177 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10178 | } |
| 10179 | buf[len++] = cur; |
| 10180 | NEXT; |
| 10181 | cur=CUR; |
| 10182 | } |
| 10183 | buf[len] = 0; |
| 10184 | return(buf); |
| 10185 | } |
| 10186 | |
| 10187 | /** |
| 10188 | * xmlParseVersionInfo: |
| 10189 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10190 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10191 | * parse the XML version. |
| 10192 | * |
| 10193 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10194 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10195 | * [25] Eq ::= S? '=' S? |
| 10196 | * |
| 10197 | * Returns the version string, e.g. "1.0" |
| 10198 | */ |
| 10199 | |
| 10200 | xmlChar * |
| 10201 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 10202 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10203 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10204 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10205 | SKIP(7); |
| 10206 | SKIP_BLANKS; |
| 10207 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10208 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10209 | return(NULL); |
| 10210 | } |
| 10211 | NEXT; |
| 10212 | SKIP_BLANKS; |
| 10213 | if (RAW == '"') { |
| 10214 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10215 | version = xmlParseVersionNum(ctxt); |
| 10216 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10217 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10218 | } else |
| 10219 | NEXT; |
| 10220 | } else if (RAW == '\''){ |
| 10221 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10222 | version = xmlParseVersionNum(ctxt); |
| 10223 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10224 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10225 | } else |
| 10226 | NEXT; |
| 10227 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10228 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10229 | } |
| 10230 | } |
| 10231 | return(version); |
| 10232 | } |
| 10233 | |
| 10234 | /** |
| 10235 | * xmlParseEncName: |
| 10236 | * @ctxt: an XML parser context |
| 10237 | * |
| 10238 | * parse the XML encoding name |
| 10239 | * |
| 10240 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 10241 | * |
| 10242 | * Returns the encoding name value or NULL |
| 10243 | */ |
| 10244 | xmlChar * |
| 10245 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 10246 | xmlChar *buf = NULL; |
| 10247 | int len = 0; |
| 10248 | int size = 10; |
| 10249 | xmlChar cur; |
| 10250 | |
| 10251 | cur = CUR; |
| 10252 | if (((cur >= 'a') && (cur <= 'z')) || |
| 10253 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10254 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10255 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10256 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10257 | return(NULL); |
| 10258 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10259 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10260 | buf[len++] = cur; |
| 10261 | NEXT; |
| 10262 | cur = CUR; |
| 10263 | while (((cur >= 'a') && (cur <= 'z')) || |
| 10264 | ((cur >= 'A') && (cur <= 'Z')) || |
| 10265 | ((cur >= '0') && (cur <= '9')) || |
| 10266 | (cur == '.') || (cur == '_') || |
| 10267 | (cur == '-')) { |
| 10268 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10269 | xmlChar *tmp; |
| 10270 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10271 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10272 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10273 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10274 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10275 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10276 | return(NULL); |
| 10277 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10278 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10279 | } |
| 10280 | buf[len++] = cur; |
| 10281 | NEXT; |
| 10282 | cur = CUR; |
| 10283 | if (cur == 0) { |
| 10284 | SHRINK; |
| 10285 | GROW; |
| 10286 | cur = CUR; |
| 10287 | } |
| 10288 | } |
| 10289 | buf[len] = 0; |
| 10290 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10291 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10292 | } |
| 10293 | return(buf); |
| 10294 | } |
| 10295 | |
| 10296 | /** |
| 10297 | * xmlParseEncodingDecl: |
| 10298 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10299 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10300 | * parse the XML encoding declaration |
| 10301 | * |
| 10302 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 10303 | * |
| 10304 | * this setups the conversion filters. |
| 10305 | * |
| 10306 | * Returns the encoding value or NULL |
| 10307 | */ |
| 10308 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10309 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10310 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 10311 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10312 | |
| 10313 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10314 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10315 | SKIP(8); |
| 10316 | SKIP_BLANKS; |
| 10317 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10318 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10319 | return(NULL); |
| 10320 | } |
| 10321 | NEXT; |
| 10322 | SKIP_BLANKS; |
| 10323 | if (RAW == '"') { |
| 10324 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10325 | encoding = xmlParseEncName(ctxt); |
| 10326 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10327 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10328 | } else |
| 10329 | NEXT; |
| 10330 | } else if (RAW == '\''){ |
| 10331 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10332 | encoding = xmlParseEncName(ctxt); |
| 10333 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10334 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10335 | } else |
| 10336 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 10337 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10338 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10339 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 10340 | |
| 10341 | /* |
| 10342 | * Non standard parsing, allowing the user to ignore encoding |
| 10343 | */ |
| 10344 | if (ctxt->options & XML_PARSE_IGNORE_ENC) |
| 10345 | return(encoding); |
| 10346 | |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10347 | /* |
| 10348 | * UTF-16 encoding stwich has already taken place at this stage, |
| 10349 | * more over the little-endian/big-endian selection is already done |
| 10350 | */ |
| 10351 | if ((encoding != NULL) && |
| 10352 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 10353 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10354 | /* |
| 10355 | * If no encoding was passed to the parser, that we are |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10356 | * using UTF-16 and no decoder is present i.e. the |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10357 | * document is apparently UTF-8 compatible, then raise an |
| 10358 | * encoding mismatch fatal error |
| 10359 | */ |
| 10360 | if ((ctxt->encoding == NULL) && |
| 10361 | (ctxt->input->buf != NULL) && |
| 10362 | (ctxt->input->buf->encoder == NULL)) { |
| 10363 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 10364 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 10365 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10366 | if (ctxt->encoding != NULL) |
| 10367 | xmlFree((xmlChar *) ctxt->encoding); |
| 10368 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10369 | } |
| 10370 | /* |
| 10371 | * UTF-8 encoding is handled natively |
| 10372 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10373 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10374 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 10375 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10376 | if (ctxt->encoding != NULL) |
| 10377 | xmlFree((xmlChar *) ctxt->encoding); |
| 10378 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10379 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10380 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10381 | xmlCharEncodingHandlerPtr handler; |
| 10382 | |
| 10383 | if (ctxt->input->encoding != NULL) |
| 10384 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 10385 | ctxt->input->encoding = encoding; |
| 10386 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10387 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 10388 | if (handler != NULL) { |
| 10389 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10390 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10391 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10392 | "Unsupported encoding %s\n", encoding); |
| 10393 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10394 | } |
| 10395 | } |
| 10396 | } |
| 10397 | return(encoding); |
| 10398 | } |
| 10399 | |
| 10400 | /** |
| 10401 | * xmlParseSDDecl: |
| 10402 | * @ctxt: an XML parser context |
| 10403 | * |
| 10404 | * parse the XML standalone declaration |
| 10405 | * |
| 10406 | * [32] SDDecl ::= S 'standalone' Eq |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10407 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10408 | * |
| 10409 | * [ VC: Standalone Document Declaration ] |
| 10410 | * TODO The standalone document declaration must have the value "no" |
| 10411 | * if any external markup declarations contain declarations of: |
| 10412 | * - attributes with default values, if elements to which these |
| 10413 | * attributes apply appear in the document without specifications |
| 10414 | * of values for these attributes, or |
| 10415 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 10416 | * to those entities appear in the document, or |
| 10417 | * - attributes with values subject to normalization, where the |
| 10418 | * attribute appears in the document with a value which will change |
| 10419 | * as a result of normalization, or |
| 10420 | * - element types with element content, if white space occurs directly |
| 10421 | * within any instance of those types. |
| 10422 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10423 | * Returns: |
| 10424 | * 1 if standalone="yes" |
| 10425 | * 0 if standalone="no" |
| 10426 | * -2 if standalone attribute is missing or invalid |
| 10427 | * (A standalone value of -2 means that the XML declaration was found, |
| 10428 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10429 | */ |
| 10430 | |
| 10431 | int |
| 10432 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10433 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10434 | |
| 10435 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10436 | 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] | 10437 | SKIP(10); |
| 10438 | SKIP_BLANKS; |
| 10439 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10440 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10441 | return(standalone); |
| 10442 | } |
| 10443 | NEXT; |
| 10444 | SKIP_BLANKS; |
| 10445 | if (RAW == '\''){ |
| 10446 | NEXT; |
| 10447 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10448 | standalone = 0; |
| 10449 | SKIP(2); |
| 10450 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10451 | (NXT(2) == 's')) { |
| 10452 | standalone = 1; |
| 10453 | SKIP(3); |
| 10454 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10455 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | } |
| 10457 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10458 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10459 | } else |
| 10460 | NEXT; |
| 10461 | } else if (RAW == '"'){ |
| 10462 | NEXT; |
| 10463 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10464 | standalone = 0; |
| 10465 | SKIP(2); |
| 10466 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10467 | (NXT(2) == 's')) { |
| 10468 | standalone = 1; |
| 10469 | SKIP(3); |
| 10470 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10471 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10472 | } |
| 10473 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10474 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10475 | } else |
| 10476 | NEXT; |
| 10477 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10478 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10479 | } |
| 10480 | } |
| 10481 | return(standalone); |
| 10482 | } |
| 10483 | |
| 10484 | /** |
| 10485 | * xmlParseXMLDecl: |
| 10486 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10487 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10488 | * parse an XML declaration header |
| 10489 | * |
| 10490 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 10491 | */ |
| 10492 | |
| 10493 | void |
| 10494 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 10495 | xmlChar *version; |
| 10496 | |
| 10497 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 10498 | * This value for standalone indicates that the document has an |
| 10499 | * XML declaration but it does not have a standalone attribute. |
| 10500 | * It will be overwritten later if a standalone attribute is found. |
| 10501 | */ |
| 10502 | ctxt->input->standalone = -2; |
| 10503 | |
| 10504 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10505 | * We know that '<?xml' is here. |
| 10506 | */ |
| 10507 | SKIP(5); |
| 10508 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10509 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10510 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 10511 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10512 | } |
| 10513 | SKIP_BLANKS; |
| 10514 | |
| 10515 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10516 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10517 | */ |
| 10518 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10519 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10520 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10521 | } else { |
| 10522 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 10523 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10524 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10525 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10526 | if (ctxt->options & XML_PARSE_OLD10) { |
| 10527 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10528 | "Unsupported version '%s'\n", |
| 10529 | version); |
| 10530 | } else { |
| 10531 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 10532 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 10533 | "Unsupported version '%s'\n", |
| 10534 | version, NULL); |
| 10535 | } else { |
| 10536 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10537 | "Unsupported version '%s'\n", |
| 10538 | version); |
| 10539 | } |
| 10540 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10541 | } |
| 10542 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 10543 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10544 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 10545 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10546 | |
| 10547 | /* |
| 10548 | * We may have the encoding declaration |
| 10549 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10550 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10551 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10552 | SKIP(2); |
| 10553 | return; |
| 10554 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10555 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10556 | } |
| 10557 | xmlParseEncodingDecl(ctxt); |
| 10558 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10559 | /* |
| 10560 | * The XML REC instructs us to stop parsing right here |
| 10561 | */ |
| 10562 | return; |
| 10563 | } |
| 10564 | |
| 10565 | /* |
| 10566 | * We may have the standalone status. |
| 10567 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10568 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10569 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10570 | SKIP(2); |
| 10571 | return; |
| 10572 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10573 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10574 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 10575 | |
| 10576 | /* |
| 10577 | * We can grow the input buffer freely at that point |
| 10578 | */ |
| 10579 | GROW; |
| 10580 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10581 | SKIP_BLANKS; |
| 10582 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 10583 | |
| 10584 | SKIP_BLANKS; |
| 10585 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10586 | SKIP(2); |
| 10587 | } else if (RAW == '>') { |
| 10588 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10589 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10590 | NEXT; |
| 10591 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10592 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10593 | MOVETO_ENDTAG(CUR_PTR); |
| 10594 | NEXT; |
| 10595 | } |
| 10596 | } |
| 10597 | |
| 10598 | /** |
| 10599 | * xmlParseMisc: |
| 10600 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10601 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10602 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10603 | * |
| 10604 | * [27] Misc ::= Comment | PI | S |
| 10605 | */ |
| 10606 | |
| 10607 | void |
| 10608 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 10609 | while ((ctxt->instate != XML_PARSER_EOF) && |
| 10610 | (((RAW == '<') && (NXT(1) == '?')) || |
| 10611 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
| 10612 | IS_BLANK_CH(CUR))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10613 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10614 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10615 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10616 | NEXT; |
| 10617 | } else |
| 10618 | xmlParseComment(ctxt); |
| 10619 | } |
| 10620 | } |
| 10621 | |
| 10622 | /** |
| 10623 | * xmlParseDocument: |
| 10624 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10625 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10626 | * parse an XML document (and build a tree if using the standard SAX |
| 10627 | * interface). |
| 10628 | * |
| 10629 | * [1] document ::= prolog element Misc* |
| 10630 | * |
| 10631 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 10632 | * |
| 10633 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10634 | * as a result of the parsing. |
| 10635 | */ |
| 10636 | |
| 10637 | int |
| 10638 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 10639 | xmlChar start[4]; |
| 10640 | xmlCharEncoding enc; |
| 10641 | |
| 10642 | xmlInitParser(); |
| 10643 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10644 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10645 | return(-1); |
| 10646 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10647 | GROW; |
| 10648 | |
| 10649 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10650 | * SAX: detecting the level. |
| 10651 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10652 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10653 | |
| 10654 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10655 | * SAX: beginning of the document processing. |
| 10656 | */ |
| 10657 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10658 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10659 | if (ctxt->instate == XML_PARSER_EOF) |
| 10660 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10661 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 10662 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10663 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10664 | /* |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10665 | * Get the 4 first bytes and decode the charset |
| 10666 | * if enc != XML_CHAR_ENCODING_NONE |
| 10667 | * plug some encoding conversion routines. |
| 10668 | */ |
| 10669 | start[0] = RAW; |
| 10670 | start[1] = NXT(1); |
| 10671 | start[2] = NXT(2); |
| 10672 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10673 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10674 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10675 | xmlSwitchEncoding(ctxt, enc); |
| 10676 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10677 | } |
| 10678 | |
| 10679 | |
| 10680 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10681 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10682 | } |
| 10683 | |
| 10684 | /* |
| 10685 | * Check for the XMLDecl in the Prolog. |
Daniel Veillard | 7e385bd | 2009-08-26 11:38:49 +0200 | [diff] [blame] | 10686 | * do not GROW here to avoid the detected encoder to decode more |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10687 | * than just the first line, unless the amount of data is really |
| 10688 | * too small to hold "<?xml version="1.0" encoding="foo" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10689 | */ |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10690 | if ((ctxt->input->end - ctxt->input->cur) < 35) { |
| 10691 | GROW; |
| 10692 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10693 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10694 | |
| 10695 | /* |
| 10696 | * Note that we will switch encoding on the fly. |
| 10697 | */ |
| 10698 | xmlParseXMLDecl(ctxt); |
| 10699 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10700 | /* |
| 10701 | * The XML REC instructs us to stop parsing right here |
| 10702 | */ |
| 10703 | return(-1); |
| 10704 | } |
| 10705 | ctxt->standalone = ctxt->input->standalone; |
| 10706 | SKIP_BLANKS; |
| 10707 | } else { |
| 10708 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10709 | } |
| 10710 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10711 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10712 | if (ctxt->instate == XML_PARSER_EOF) |
| 10713 | return(-1); |
Daniel Veillard | 63588f4 | 2013-05-10 14:01:46 +0800 | [diff] [blame] | 10714 | if ((ctxt->myDoc != NULL) && (ctxt->input != NULL) && |
| 10715 | (ctxt->input->buf != NULL) && (ctxt->input->buf->compressed >= 0)) { |
| 10716 | ctxt->myDoc->compression = ctxt->input->buf->compressed; |
| 10717 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10718 | |
| 10719 | /* |
| 10720 | * The Misc part of the Prolog |
| 10721 | */ |
| 10722 | GROW; |
| 10723 | xmlParseMisc(ctxt); |
| 10724 | |
| 10725 | /* |
| 10726 | * Then possibly doc type declaration(s) and more Misc |
| 10727 | * (doctypedecl Misc*)? |
| 10728 | */ |
| 10729 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10730 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10731 | |
| 10732 | ctxt->inSubset = 1; |
| 10733 | xmlParseDocTypeDecl(ctxt); |
| 10734 | if (RAW == '[') { |
| 10735 | ctxt->instate = XML_PARSER_DTD; |
| 10736 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10737 | if (ctxt->instate == XML_PARSER_EOF) |
| 10738 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10739 | } |
| 10740 | |
| 10741 | /* |
| 10742 | * Create and update the external subset. |
| 10743 | */ |
| 10744 | ctxt->inSubset = 2; |
| 10745 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10746 | (!ctxt->disableSAX)) |
| 10747 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10748 | ctxt->extSubSystem, ctxt->extSubURI); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10749 | if (ctxt->instate == XML_PARSER_EOF) |
| 10750 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10751 | ctxt->inSubset = 0; |
| 10752 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10753 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10754 | |
| 10755 | ctxt->instate = XML_PARSER_PROLOG; |
| 10756 | xmlParseMisc(ctxt); |
| 10757 | } |
| 10758 | |
| 10759 | /* |
| 10760 | * Time to start parsing the tree itself |
| 10761 | */ |
| 10762 | GROW; |
| 10763 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10764 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10765 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10766 | } else { |
| 10767 | ctxt->instate = XML_PARSER_CONTENT; |
| 10768 | xmlParseElement(ctxt); |
| 10769 | ctxt->instate = XML_PARSER_EPILOG; |
| 10770 | |
| 10771 | |
| 10772 | /* |
| 10773 | * The Misc part at the end |
| 10774 | */ |
| 10775 | xmlParseMisc(ctxt); |
| 10776 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10777 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10778 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10779 | } |
| 10780 | ctxt->instate = XML_PARSER_EOF; |
| 10781 | } |
| 10782 | |
| 10783 | /* |
| 10784 | * SAX: end of the document processing. |
| 10785 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10786 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10787 | ctxt->sax->endDocument(ctxt->userData); |
| 10788 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10789 | /* |
| 10790 | * Remove locally kept entity definitions if the tree was not built |
| 10791 | */ |
| 10792 | if ((ctxt->myDoc != NULL) && |
| 10793 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10794 | xmlFreeDoc(ctxt->myDoc); |
| 10795 | ctxt->myDoc = NULL; |
| 10796 | } |
| 10797 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10798 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10799 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10800 | if (ctxt->valid) |
| 10801 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10802 | if (ctxt->nsWellFormed) |
| 10803 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10804 | if (ctxt->options & XML_PARSE_OLD10) |
| 10805 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10806 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10807 | if (! ctxt->wellFormed) { |
| 10808 | ctxt->valid = 0; |
| 10809 | return(-1); |
| 10810 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10811 | return(0); |
| 10812 | } |
| 10813 | |
| 10814 | /** |
| 10815 | * xmlParseExtParsedEnt: |
| 10816 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10817 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10818 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10819 | * An external general parsed entity is well-formed if it matches the |
| 10820 | * production labeled extParsedEnt. |
| 10821 | * |
| 10822 | * [78] extParsedEnt ::= TextDecl? content |
| 10823 | * |
| 10824 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10825 | * as a result of the parsing. |
| 10826 | */ |
| 10827 | |
| 10828 | int |
| 10829 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10830 | xmlChar start[4]; |
| 10831 | xmlCharEncoding enc; |
| 10832 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10833 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10834 | return(-1); |
| 10835 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10836 | xmlDefaultSAXHandlerInit(); |
| 10837 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10838 | xmlDetectSAX2(ctxt); |
| 10839 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10840 | GROW; |
| 10841 | |
| 10842 | /* |
| 10843 | * SAX: beginning of the document processing. |
| 10844 | */ |
| 10845 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10846 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10847 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10848 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10849 | * Get the 4 first bytes and decode the charset |
| 10850 | * if enc != XML_CHAR_ENCODING_NONE |
| 10851 | * plug some encoding conversion routines. |
| 10852 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10853 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10854 | start[0] = RAW; |
| 10855 | start[1] = NXT(1); |
| 10856 | start[2] = NXT(2); |
| 10857 | start[3] = NXT(3); |
| 10858 | enc = xmlDetectCharEncoding(start, 4); |
| 10859 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10860 | xmlSwitchEncoding(ctxt, enc); |
| 10861 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10862 | } |
| 10863 | |
| 10864 | |
| 10865 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10866 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10867 | } |
| 10868 | |
| 10869 | /* |
| 10870 | * Check for the XMLDecl in the Prolog. |
| 10871 | */ |
| 10872 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10873 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10874 | |
| 10875 | /* |
| 10876 | * Note that we will switch encoding on the fly. |
| 10877 | */ |
| 10878 | xmlParseXMLDecl(ctxt); |
| 10879 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10880 | /* |
| 10881 | * The XML REC instructs us to stop parsing right here |
| 10882 | */ |
| 10883 | return(-1); |
| 10884 | } |
| 10885 | SKIP_BLANKS; |
| 10886 | } else { |
| 10887 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10888 | } |
| 10889 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10890 | ctxt->sax->startDocument(ctxt->userData); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10891 | if (ctxt->instate == XML_PARSER_EOF) |
| 10892 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10893 | |
| 10894 | /* |
| 10895 | * Doing validity checking on chunk doesn't make sense |
| 10896 | */ |
| 10897 | ctxt->instate = XML_PARSER_CONTENT; |
| 10898 | ctxt->validate = 0; |
| 10899 | ctxt->loadsubset = 0; |
| 10900 | ctxt->depth = 0; |
| 10901 | |
| 10902 | xmlParseContent(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 10903 | if (ctxt->instate == XML_PARSER_EOF) |
| 10904 | return(-1); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10905 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10906 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10907 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10908 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10909 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10910 | } |
| 10911 | |
| 10912 | /* |
| 10913 | * SAX: end of the document processing. |
| 10914 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10915 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10916 | ctxt->sax->endDocument(ctxt->userData); |
| 10917 | |
| 10918 | if (! ctxt->wellFormed) return(-1); |
| 10919 | return(0); |
| 10920 | } |
| 10921 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10922 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10923 | /************************************************************************ |
| 10924 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10925 | * Progressive parsing interfaces * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10926 | * * |
| 10927 | ************************************************************************/ |
| 10928 | |
| 10929 | /** |
| 10930 | * xmlParseLookupSequence: |
| 10931 | * @ctxt: an XML parser context |
| 10932 | * @first: the first char to lookup |
| 10933 | * @next: the next char to lookup or zero |
| 10934 | * @third: the next char to lookup or zero |
| 10935 | * |
| 10936 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10937 | * (first) is available in the input stream. |
| 10938 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10939 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10940 | * parser, do not use liberally. |
| 10941 | * |
| 10942 | * Returns the index to the current parsing point if the full sequence |
| 10943 | * is available, -1 otherwise. |
| 10944 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10945 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10946 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10947 | xmlChar next, xmlChar third) { |
| 10948 | int base, len; |
| 10949 | xmlParserInputPtr in; |
| 10950 | const xmlChar *buf; |
| 10951 | |
| 10952 | in = ctxt->input; |
| 10953 | if (in == NULL) return(-1); |
| 10954 | base = in->cur - in->base; |
| 10955 | if (base < 0) return(-1); |
| 10956 | if (ctxt->checkIndex > base) |
| 10957 | base = ctxt->checkIndex; |
| 10958 | if (in->buf == NULL) { |
| 10959 | buf = in->base; |
| 10960 | len = in->length; |
| 10961 | } else { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 10962 | buf = xmlBufContent(in->buf->buffer); |
| 10963 | len = xmlBufUse(in->buf->buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10964 | } |
| 10965 | /* take into account the sequence length */ |
| 10966 | if (third) len -= 2; |
| 10967 | else if (next) len --; |
| 10968 | for (;base < len;base++) { |
| 10969 | if (buf[base] == first) { |
| 10970 | if (third != 0) { |
| 10971 | if ((buf[base + 1] != next) || |
| 10972 | (buf[base + 2] != third)) continue; |
| 10973 | } else if (next != 0) { |
| 10974 | if (buf[base + 1] != next) continue; |
| 10975 | } |
| 10976 | ctxt->checkIndex = 0; |
| 10977 | #ifdef DEBUG_PUSH |
| 10978 | if (next == 0) |
| 10979 | xmlGenericError(xmlGenericErrorContext, |
| 10980 | "PP: lookup '%c' found at %d\n", |
| 10981 | first, base); |
| 10982 | else if (third == 0) |
| 10983 | xmlGenericError(xmlGenericErrorContext, |
| 10984 | "PP: lookup '%c%c' found at %d\n", |
| 10985 | first, next, base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10986 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10987 | xmlGenericError(xmlGenericErrorContext, |
| 10988 | "PP: lookup '%c%c%c' found at %d\n", |
| 10989 | first, next, third, base); |
| 10990 | #endif |
| 10991 | return(base - (in->cur - in->base)); |
| 10992 | } |
| 10993 | } |
| 10994 | ctxt->checkIndex = base; |
| 10995 | #ifdef DEBUG_PUSH |
| 10996 | if (next == 0) |
| 10997 | xmlGenericError(xmlGenericErrorContext, |
| 10998 | "PP: lookup '%c' failed\n", first); |
| 10999 | else if (third == 0) |
| 11000 | xmlGenericError(xmlGenericErrorContext, |
| 11001 | "PP: lookup '%c%c' failed\n", first, next); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11002 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11003 | xmlGenericError(xmlGenericErrorContext, |
| 11004 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 11005 | #endif |
| 11006 | return(-1); |
| 11007 | } |
| 11008 | |
| 11009 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11010 | * xmlParseGetLasts: |
| 11011 | * @ctxt: an XML parser context |
| 11012 | * @lastlt: pointer to store the last '<' from the input |
| 11013 | * @lastgt: pointer to store the last '>' from the input |
| 11014 | * |
| 11015 | * Lookup the last < and > in the current chunk |
| 11016 | */ |
| 11017 | static void |
| 11018 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 11019 | const xmlChar **lastgt) { |
| 11020 | const xmlChar *tmp; |
| 11021 | |
| 11022 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 11023 | xmlGenericError(xmlGenericErrorContext, |
| 11024 | "Internal error: xmlParseGetLasts\n"); |
| 11025 | return; |
| 11026 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11027 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11028 | tmp = ctxt->input->end; |
| 11029 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11030 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11031 | if (tmp < ctxt->input->base) { |
| 11032 | *lastlt = NULL; |
| 11033 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11034 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11035 | *lastlt = tmp; |
| 11036 | tmp++; |
| 11037 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 11038 | if (*tmp == '\'') { |
| 11039 | tmp++; |
| 11040 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 11041 | if (tmp < ctxt->input->end) tmp++; |
| 11042 | } else if (*tmp == '"') { |
| 11043 | tmp++; |
| 11044 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 11045 | if (tmp < ctxt->input->end) tmp++; |
| 11046 | } else |
| 11047 | tmp++; |
| 11048 | } |
| 11049 | if (tmp < ctxt->input->end) |
| 11050 | *lastgt = tmp; |
| 11051 | else { |
| 11052 | tmp = *lastlt; |
| 11053 | tmp--; |
| 11054 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 11055 | if (tmp >= ctxt->input->base) |
| 11056 | *lastgt = tmp; |
| 11057 | else |
| 11058 | *lastgt = NULL; |
| 11059 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11060 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11061 | } else { |
| 11062 | *lastlt = NULL; |
| 11063 | *lastgt = NULL; |
| 11064 | } |
| 11065 | } |
| 11066 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11067 | * xmlCheckCdataPush: |
| 11068 | * @cur: pointer to the bock of characters |
| 11069 | * @len: length of the block in bytes |
| 11070 | * |
| 11071 | * Check that the block of characters is okay as SCdata content [20] |
| 11072 | * |
| 11073 | * Returns the number of bytes to pass if okay, a negative index where an |
| 11074 | * UTF-8 error occured otherwise |
| 11075 | */ |
| 11076 | static int |
| 11077 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 11078 | int ix; |
| 11079 | unsigned char c; |
| 11080 | int codepoint; |
| 11081 | |
| 11082 | if ((utf == NULL) || (len <= 0)) |
| 11083 | return(0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11084 | |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11085 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 11086 | c = utf[ix]; |
| 11087 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 11088 | if (c >= 0x20) |
| 11089 | ix++; |
| 11090 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 11091 | ix++; |
| 11092 | else |
| 11093 | return(-ix); |
| 11094 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 11095 | if (ix + 2 > len) return(ix); |
| 11096 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 11097 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11098 | codepoint = (utf[ix] & 0x1f) << 6; |
| 11099 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11100 | if (!xmlIsCharQ(codepoint)) |
| 11101 | return(-ix); |
| 11102 | ix += 2; |
| 11103 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 11104 | if (ix + 3 > len) return(ix); |
| 11105 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11106 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 11107 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11108 | codepoint = (utf[ix] & 0xf) << 12; |
| 11109 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 11110 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11111 | if (!xmlIsCharQ(codepoint)) |
| 11112 | return(-ix); |
| 11113 | ix += 3; |
| 11114 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 11115 | if (ix + 4 > len) return(ix); |
| 11116 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11117 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 11118 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 11119 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11120 | codepoint = (utf[ix] & 0x7) << 18; |
| 11121 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 11122 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 11123 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11124 | if (!xmlIsCharQ(codepoint)) |
| 11125 | return(-ix); |
| 11126 | ix += 4; |
| 11127 | } else /* unknown encoding */ |
| 11128 | return(-ix); |
| 11129 | } |
| 11130 | return(ix); |
| 11131 | } |
| 11132 | |
| 11133 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11134 | * xmlParseTryOrFinish: |
| 11135 | * @ctxt: an XML parser context |
| 11136 | * @terminate: last chunk indicator |
| 11137 | * |
| 11138 | * Try to progress on parsing |
| 11139 | * |
| 11140 | * Returns zero if no parsing was possible |
| 11141 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11142 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11143 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 11144 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11145 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11146 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11147 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11148 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11149 | if (ctxt->input == NULL) |
| 11150 | return(0); |
| 11151 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11152 | #ifdef DEBUG_PUSH |
| 11153 | switch (ctxt->instate) { |
| 11154 | case XML_PARSER_EOF: |
| 11155 | xmlGenericError(xmlGenericErrorContext, |
| 11156 | "PP: try EOF\n"); break; |
| 11157 | case XML_PARSER_START: |
| 11158 | xmlGenericError(xmlGenericErrorContext, |
| 11159 | "PP: try START\n"); break; |
| 11160 | case XML_PARSER_MISC: |
| 11161 | xmlGenericError(xmlGenericErrorContext, |
| 11162 | "PP: try MISC\n");break; |
| 11163 | case XML_PARSER_COMMENT: |
| 11164 | xmlGenericError(xmlGenericErrorContext, |
| 11165 | "PP: try COMMENT\n");break; |
| 11166 | case XML_PARSER_PROLOG: |
| 11167 | xmlGenericError(xmlGenericErrorContext, |
| 11168 | "PP: try PROLOG\n");break; |
| 11169 | case XML_PARSER_START_TAG: |
| 11170 | xmlGenericError(xmlGenericErrorContext, |
| 11171 | "PP: try START_TAG\n");break; |
| 11172 | case XML_PARSER_CONTENT: |
| 11173 | xmlGenericError(xmlGenericErrorContext, |
| 11174 | "PP: try CONTENT\n");break; |
| 11175 | case XML_PARSER_CDATA_SECTION: |
| 11176 | xmlGenericError(xmlGenericErrorContext, |
| 11177 | "PP: try CDATA_SECTION\n");break; |
| 11178 | case XML_PARSER_END_TAG: |
| 11179 | xmlGenericError(xmlGenericErrorContext, |
| 11180 | "PP: try END_TAG\n");break; |
| 11181 | case XML_PARSER_ENTITY_DECL: |
| 11182 | xmlGenericError(xmlGenericErrorContext, |
| 11183 | "PP: try ENTITY_DECL\n");break; |
| 11184 | case XML_PARSER_ENTITY_VALUE: |
| 11185 | xmlGenericError(xmlGenericErrorContext, |
| 11186 | "PP: try ENTITY_VALUE\n");break; |
| 11187 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11188 | xmlGenericError(xmlGenericErrorContext, |
| 11189 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 11190 | case XML_PARSER_DTD: |
| 11191 | xmlGenericError(xmlGenericErrorContext, |
| 11192 | "PP: try DTD\n");break; |
| 11193 | case XML_PARSER_EPILOG: |
| 11194 | xmlGenericError(xmlGenericErrorContext, |
| 11195 | "PP: try EPILOG\n");break; |
| 11196 | case XML_PARSER_PI: |
| 11197 | xmlGenericError(xmlGenericErrorContext, |
| 11198 | "PP: try PI\n");break; |
| 11199 | case XML_PARSER_IGNORE: |
| 11200 | xmlGenericError(xmlGenericErrorContext, |
| 11201 | "PP: try IGNORE\n");break; |
| 11202 | } |
| 11203 | #endif |
| 11204 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11205 | if ((ctxt->input != NULL) && |
| 11206 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11207 | xmlSHRINK(ctxt); |
| 11208 | ctxt->checkIndex = 0; |
| 11209 | } |
| 11210 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 11211 | |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11212 | while (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11213 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11214 | return(0); |
| 11215 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11216 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11217 | /* |
| 11218 | * Pop-up of finished entities. |
| 11219 | */ |
| 11220 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11221 | xmlPopInput(ctxt); |
| 11222 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11223 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11224 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11225 | avail = ctxt->input->length - |
| 11226 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11227 | else { |
| 11228 | /* |
| 11229 | * If we are operating on converted input, try to flush |
| 11230 | * remainng chars to avoid them stalling in the non-converted |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11231 | * buffer. But do not do this in document start where |
| 11232 | * encoding="..." may not have been read and we work on a |
| 11233 | * guessed encoding. |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11234 | */ |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 11235 | if ((ctxt->instate != XML_PARSER_START) && |
| 11236 | (ctxt->input->buf->raw != NULL) && |
| 11237 | (xmlBufIsEmpty(ctxt->input->buf->raw) == 0)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11238 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 11239 | ctxt->input); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11240 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11241 | |
| 11242 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11243 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 11244 | base, current); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11245 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11246 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11247 | (ctxt->input->cur - ctxt->input->base); |
| 11248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11249 | if (avail < 1) |
| 11250 | goto done; |
| 11251 | switch (ctxt->instate) { |
| 11252 | case XML_PARSER_EOF: |
| 11253 | /* |
| 11254 | * Document parsing is done ! |
| 11255 | */ |
| 11256 | goto done; |
| 11257 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11258 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 11259 | xmlChar start[4]; |
| 11260 | xmlCharEncoding enc; |
| 11261 | |
| 11262 | /* |
| 11263 | * Very first chars read from the document flow. |
| 11264 | */ |
| 11265 | if (avail < 4) |
| 11266 | goto done; |
| 11267 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11268 | /* |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11269 | * Get the 4 first bytes and decode the charset |
| 11270 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11271 | * plug some encoding conversion routines, |
| 11272 | * else xmlSwitchEncoding will set to (default) |
| 11273 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11274 | */ |
| 11275 | start[0] = RAW; |
| 11276 | start[1] = NXT(1); |
| 11277 | start[2] = NXT(2); |
| 11278 | start[3] = NXT(3); |
| 11279 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11280 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11281 | break; |
| 11282 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11283 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 11284 | if (avail < 2) |
| 11285 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11286 | cur = ctxt->input->cur[0]; |
| 11287 | next = ctxt->input->cur[1]; |
| 11288 | if (cur == 0) { |
| 11289 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11290 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11291 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11292 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11293 | ctxt->instate = XML_PARSER_EOF; |
| 11294 | #ifdef DEBUG_PUSH |
| 11295 | xmlGenericError(xmlGenericErrorContext, |
| 11296 | "PP: entering EOF\n"); |
| 11297 | #endif |
| 11298 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11299 | ctxt->sax->endDocument(ctxt->userData); |
| 11300 | goto done; |
| 11301 | } |
| 11302 | if ((cur == '<') && (next == '?')) { |
| 11303 | /* PI or XML decl */ |
| 11304 | if (avail < 5) return(ret); |
| 11305 | if ((!terminate) && |
| 11306 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11307 | return(ret); |
| 11308 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11309 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11310 | &xmlDefaultSAXLocator); |
| 11311 | if ((ctxt->input->cur[2] == 'x') && |
| 11312 | (ctxt->input->cur[3] == 'm') && |
| 11313 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11314 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11315 | ret += 5; |
| 11316 | #ifdef DEBUG_PUSH |
| 11317 | xmlGenericError(xmlGenericErrorContext, |
| 11318 | "PP: Parsing XML Decl\n"); |
| 11319 | #endif |
| 11320 | xmlParseXMLDecl(ctxt); |
| 11321 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 11322 | /* |
| 11323 | * The XML REC instructs us to stop parsing right |
| 11324 | * here |
| 11325 | */ |
| 11326 | ctxt->instate = XML_PARSER_EOF; |
| 11327 | return(0); |
| 11328 | } |
| 11329 | ctxt->standalone = ctxt->input->standalone; |
| 11330 | if ((ctxt->encoding == NULL) && |
| 11331 | (ctxt->input->encoding != NULL)) |
| 11332 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 11333 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11334 | (!ctxt->disableSAX)) |
| 11335 | ctxt->sax->startDocument(ctxt->userData); |
| 11336 | ctxt->instate = XML_PARSER_MISC; |
| 11337 | #ifdef DEBUG_PUSH |
| 11338 | xmlGenericError(xmlGenericErrorContext, |
| 11339 | "PP: entering MISC\n"); |
| 11340 | #endif |
| 11341 | } else { |
| 11342 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 11343 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11344 | (!ctxt->disableSAX)) |
| 11345 | ctxt->sax->startDocument(ctxt->userData); |
| 11346 | ctxt->instate = XML_PARSER_MISC; |
| 11347 | #ifdef DEBUG_PUSH |
| 11348 | xmlGenericError(xmlGenericErrorContext, |
| 11349 | "PP: entering MISC\n"); |
| 11350 | #endif |
| 11351 | } |
| 11352 | } else { |
| 11353 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11354 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11355 | &xmlDefaultSAXLocator); |
| 11356 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11357 | if (ctxt->version == NULL) { |
| 11358 | xmlErrMemory(ctxt, NULL); |
| 11359 | break; |
| 11360 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11361 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11362 | (!ctxt->disableSAX)) |
| 11363 | ctxt->sax->startDocument(ctxt->userData); |
| 11364 | ctxt->instate = XML_PARSER_MISC; |
| 11365 | #ifdef DEBUG_PUSH |
| 11366 | xmlGenericError(xmlGenericErrorContext, |
| 11367 | "PP: entering MISC\n"); |
| 11368 | #endif |
| 11369 | } |
| 11370 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11371 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11372 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 11373 | const xmlChar *prefix = NULL; |
| 11374 | const xmlChar *URI = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11375 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11376 | |
| 11377 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11378 | goto done; |
| 11379 | cur = ctxt->input->cur[0]; |
| 11380 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11381 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11382 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11383 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11384 | ctxt->sax->endDocument(ctxt->userData); |
| 11385 | goto done; |
| 11386 | } |
| 11387 | if (!terminate) { |
| 11388 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 11389 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11390 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11391 | goto done; |
| 11392 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11393 | goto done; |
| 11394 | } |
| 11395 | } |
| 11396 | if (ctxt->spaceNr == 0) |
| 11397 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 11398 | else if (*ctxt->space == -2) |
| 11399 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11400 | else |
| 11401 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11402 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11403 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11404 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11405 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11406 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11407 | else |
| 11408 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11409 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11410 | if (ctxt->instate == XML_PARSER_EOF) |
| 11411 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11412 | if (name == NULL) { |
| 11413 | spacePop(ctxt); |
| 11414 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11415 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11416 | ctxt->sax->endDocument(ctxt->userData); |
| 11417 | goto done; |
| 11418 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11419 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11420 | /* |
| 11421 | * [ VC: Root Element Type ] |
| 11422 | * The Name in the document type declaration must match |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11423 | * the element type of the root element. |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11424 | */ |
| 11425 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 11426 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 11427 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11428 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11429 | |
| 11430 | /* |
| 11431 | * Check for an Empty Element. |
| 11432 | */ |
| 11433 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 11434 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11435 | |
| 11436 | if (ctxt->sax2) { |
| 11437 | if ((ctxt->sax != NULL) && |
| 11438 | (ctxt->sax->endElementNs != NULL) && |
| 11439 | (!ctxt->disableSAX)) |
| 11440 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 11441 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 11442 | if (ctxt->nsNr - nsNr > 0) |
| 11443 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11444 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11445 | } else { |
| 11446 | if ((ctxt->sax != NULL) && |
| 11447 | (ctxt->sax->endElement != NULL) && |
| 11448 | (!ctxt->disableSAX)) |
| 11449 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11450 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11451 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11452 | if (ctxt->instate == XML_PARSER_EOF) |
| 11453 | goto done; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11454 | spacePop(ctxt); |
| 11455 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11456 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11457 | } else { |
| 11458 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11459 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11460 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11461 | break; |
| 11462 | } |
| 11463 | if (RAW == '>') { |
| 11464 | NEXT; |
| 11465 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 11466 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11467 | "Couldn't find end of Start Tag %s\n", |
| 11468 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11469 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11470 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11471 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11472 | if (ctxt->sax2) |
| 11473 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11474 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11475 | else |
| 11476 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11477 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11478 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11479 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11480 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11481 | break; |
| 11482 | } |
| 11483 | case XML_PARSER_CONTENT: { |
| 11484 | const xmlChar *test; |
| 11485 | unsigned int cons; |
| 11486 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11487 | goto done; |
| 11488 | cur = ctxt->input->cur[0]; |
| 11489 | next = ctxt->input->cur[1]; |
| 11490 | |
| 11491 | test = CUR_PTR; |
| 11492 | cons = ctxt->input->consumed; |
| 11493 | if ((cur == '<') && (next == '/')) { |
| 11494 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11495 | break; |
| 11496 | } else if ((cur == '<') && (next == '?')) { |
| 11497 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11498 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11499 | ctxt->progressive = XML_PARSER_PI; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11500 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11501 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11502 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11503 | ctxt->instate = XML_PARSER_CONTENT; |
| 11504 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11505 | } else if ((cur == '<') && (next != '!')) { |
| 11506 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11507 | break; |
| 11508 | } else if ((cur == '<') && (next == '!') && |
| 11509 | (ctxt->input->cur[2] == '-') && |
| 11510 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 11511 | int term; |
| 11512 | |
| 11513 | if (avail < 4) |
| 11514 | goto done; |
| 11515 | ctxt->input->cur += 4; |
| 11516 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 11517 | ctxt->input->cur -= 4; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11518 | if ((!terminate) && (term < 0)) { |
| 11519 | ctxt->progressive = XML_PARSER_COMMENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11520 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11521 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11522 | xmlParseComment(ctxt); |
| 11523 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11524 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11525 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 11526 | (ctxt->input->cur[2] == '[') && |
| 11527 | (ctxt->input->cur[3] == 'C') && |
| 11528 | (ctxt->input->cur[4] == 'D') && |
| 11529 | (ctxt->input->cur[5] == 'A') && |
| 11530 | (ctxt->input->cur[6] == 'T') && |
| 11531 | (ctxt->input->cur[7] == 'A') && |
| 11532 | (ctxt->input->cur[8] == '[')) { |
| 11533 | SKIP(9); |
| 11534 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11535 | break; |
| 11536 | } else if ((cur == '<') && (next == '!') && |
| 11537 | (avail < 9)) { |
| 11538 | goto done; |
| 11539 | } else if (cur == '&') { |
| 11540 | if ((!terminate) && |
| 11541 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 11542 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11543 | xmlParseReference(ctxt); |
| 11544 | } else { |
| 11545 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 11546 | /* |
| 11547 | * Goal of the following test is: |
| 11548 | * - minimize calls to the SAX 'character' callback |
| 11549 | * when they are mergeable |
| 11550 | * - handle an problem for isBlank when we only parse |
| 11551 | * a sequence of blank chars and the next one is |
| 11552 | * not available to check against '<' presence. |
| 11553 | * - tries to homogenize the differences in SAX |
| 11554 | * callbacks between the push and pull versions |
| 11555 | * of the parser. |
| 11556 | */ |
| 11557 | if ((ctxt->inputNr == 1) && |
| 11558 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 11559 | if (!terminate) { |
| 11560 | if (ctxt->progressive) { |
| 11561 | if ((lastlt == NULL) || |
| 11562 | (ctxt->input->cur > lastlt)) |
| 11563 | goto done; |
| 11564 | } else if (xmlParseLookupSequence(ctxt, |
| 11565 | '<', 0, 0) < 0) { |
| 11566 | goto done; |
| 11567 | } |
| 11568 | } |
| 11569 | } |
| 11570 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11571 | xmlParseCharData(ctxt, 0); |
| 11572 | } |
| 11573 | /* |
| 11574 | * Pop-up of finished entities. |
| 11575 | */ |
| 11576 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11577 | xmlPopInput(ctxt); |
| 11578 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11579 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 11580 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11581 | ctxt->instate = XML_PARSER_EOF; |
| 11582 | break; |
| 11583 | } |
| 11584 | break; |
| 11585 | } |
| 11586 | case XML_PARSER_END_TAG: |
| 11587 | if (avail < 2) |
| 11588 | goto done; |
| 11589 | if (!terminate) { |
| 11590 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11591 | /* > can be found unescaped in attribute values */ |
| 11592 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11593 | goto done; |
| 11594 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11595 | goto done; |
| 11596 | } |
| 11597 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11598 | if (ctxt->sax2) { |
| 11599 | xmlParseEndTag2(ctxt, |
| 11600 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 11601 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11602 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11603 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11604 | } |
| 11605 | #ifdef LIBXML_SAX1_ENABLED |
| 11606 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11607 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11608 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11609 | if (ctxt->instate == XML_PARSER_EOF) { |
| 11610 | /* Nothing */ |
| 11611 | } else if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11612 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11613 | } else { |
| 11614 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11615 | } |
| 11616 | break; |
| 11617 | case XML_PARSER_CDATA_SECTION: { |
| 11618 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11619 | * The Push mode need to have the SAX callback for |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11620 | * cdataBlock merge back contiguous callbacks. |
| 11621 | */ |
| 11622 | int base; |
| 11623 | |
| 11624 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 11625 | if (base < 0) { |
| 11626 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11627 | int tmp; |
| 11628 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11629 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11630 | XML_PARSER_BIG_BUFFER_SIZE); |
| 11631 | if (tmp < 0) { |
| 11632 | tmp = -tmp; |
| 11633 | ctxt->input->cur += tmp; |
| 11634 | goto encoding_error; |
| 11635 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11636 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 11637 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11638 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11639 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11640 | else if (ctxt->sax->characters != NULL) |
| 11641 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11642 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11643 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11644 | if (ctxt->instate == XML_PARSER_EOF) |
| 11645 | goto done; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11646 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11647 | ctxt->checkIndex = 0; |
| 11648 | } |
| 11649 | goto done; |
| 11650 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11651 | int tmp; |
| 11652 | |
| 11653 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 11654 | if ((tmp < 0) || (tmp != base)) { |
| 11655 | tmp = -tmp; |
| 11656 | ctxt->input->cur += tmp; |
| 11657 | goto encoding_error; |
| 11658 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 11659 | if ((ctxt->sax != NULL) && (base == 0) && |
| 11660 | (ctxt->sax->cdataBlock != NULL) && |
| 11661 | (!ctxt->disableSAX)) { |
| 11662 | /* |
| 11663 | * Special case to provide identical behaviour |
| 11664 | * between pull and push parsers on enpty CDATA |
| 11665 | * sections |
| 11666 | */ |
| 11667 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 11668 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 11669 | "<![CDATA[", 9))) |
| 11670 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11671 | BAD_CAST "", 0); |
| 11672 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11673 | (!ctxt->disableSAX)) { |
| 11674 | if (ctxt->sax->cdataBlock != NULL) |
| 11675 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11676 | ctxt->input->cur, base); |
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, |
| 11679 | ctxt->input->cur, base); |
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 | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 11683 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11684 | ctxt->checkIndex = 0; |
| 11685 | ctxt->instate = XML_PARSER_CONTENT; |
| 11686 | #ifdef DEBUG_PUSH |
| 11687 | xmlGenericError(xmlGenericErrorContext, |
| 11688 | "PP: entering CONTENT\n"); |
| 11689 | #endif |
| 11690 | } |
| 11691 | break; |
| 11692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11693 | case XML_PARSER_MISC: |
| 11694 | SKIP_BLANKS; |
| 11695 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11696 | avail = ctxt->input->length - |
| 11697 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11698 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11699 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11700 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11701 | if (avail < 2) |
| 11702 | goto done; |
| 11703 | cur = ctxt->input->cur[0]; |
| 11704 | next = ctxt->input->cur[1]; |
| 11705 | if ((cur == '<') && (next == '?')) { |
| 11706 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11707 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11708 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11709 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11710 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11711 | #ifdef DEBUG_PUSH |
| 11712 | xmlGenericError(xmlGenericErrorContext, |
| 11713 | "PP: Parsing PI\n"); |
| 11714 | #endif |
| 11715 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11716 | if (ctxt->instate == XML_PARSER_EOF) |
| 11717 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11718 | ctxt->instate = XML_PARSER_MISC; |
| 11719 | ctxt->progressive = 1; |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11720 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11721 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11722 | (ctxt->input->cur[2] == '-') && |
| 11723 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11724 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11725 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11726 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11727 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11728 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11729 | #ifdef DEBUG_PUSH |
| 11730 | xmlGenericError(xmlGenericErrorContext, |
| 11731 | "PP: Parsing Comment\n"); |
| 11732 | #endif |
| 11733 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11734 | if (ctxt->instate == XML_PARSER_EOF) |
| 11735 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11736 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11737 | ctxt->progressive = 1; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11738 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11739 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11740 | (ctxt->input->cur[2] == 'D') && |
| 11741 | (ctxt->input->cur[3] == 'O') && |
| 11742 | (ctxt->input->cur[4] == 'C') && |
| 11743 | (ctxt->input->cur[5] == 'T') && |
| 11744 | (ctxt->input->cur[6] == 'Y') && |
| 11745 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11746 | (ctxt->input->cur[8] == 'E')) { |
| 11747 | if ((!terminate) && |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11748 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) { |
| 11749 | ctxt->progressive = XML_PARSER_DTD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11750 | goto done; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11751 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11752 | #ifdef DEBUG_PUSH |
| 11753 | xmlGenericError(xmlGenericErrorContext, |
| 11754 | "PP: Parsing internal subset\n"); |
| 11755 | #endif |
| 11756 | ctxt->inSubset = 1; |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 11757 | ctxt->progressive = 0; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11758 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11759 | xmlParseDocTypeDecl(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11760 | if (ctxt->instate == XML_PARSER_EOF) |
| 11761 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11762 | if (RAW == '[') { |
| 11763 | ctxt->instate = XML_PARSER_DTD; |
| 11764 | #ifdef DEBUG_PUSH |
| 11765 | xmlGenericError(xmlGenericErrorContext, |
| 11766 | "PP: entering DTD\n"); |
| 11767 | #endif |
| 11768 | } else { |
| 11769 | /* |
| 11770 | * Create and update the external subset. |
| 11771 | */ |
| 11772 | ctxt->inSubset = 2; |
| 11773 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11774 | (ctxt->sax->externalSubset != NULL)) |
| 11775 | ctxt->sax->externalSubset(ctxt->userData, |
| 11776 | ctxt->intSubName, ctxt->extSubSystem, |
| 11777 | ctxt->extSubURI); |
| 11778 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11779 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11780 | ctxt->instate = XML_PARSER_PROLOG; |
| 11781 | #ifdef DEBUG_PUSH |
| 11782 | xmlGenericError(xmlGenericErrorContext, |
| 11783 | "PP: entering PROLOG\n"); |
| 11784 | #endif |
| 11785 | } |
| 11786 | } else if ((cur == '<') && (next == '!') && |
| 11787 | (avail < 9)) { |
| 11788 | goto done; |
| 11789 | } else { |
| 11790 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11791 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11792 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11793 | #ifdef DEBUG_PUSH |
| 11794 | xmlGenericError(xmlGenericErrorContext, |
| 11795 | "PP: entering START_TAG\n"); |
| 11796 | #endif |
| 11797 | } |
| 11798 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11799 | case XML_PARSER_PROLOG: |
| 11800 | SKIP_BLANKS; |
| 11801 | if (ctxt->input->buf == NULL) |
| 11802 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11803 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11804 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11805 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11806 | if (avail < 2) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11807 | goto done; |
| 11808 | cur = ctxt->input->cur[0]; |
| 11809 | next = ctxt->input->cur[1]; |
| 11810 | if ((cur == '<') && (next == '?')) { |
| 11811 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11812 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11813 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11814 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11815 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11816 | #ifdef DEBUG_PUSH |
| 11817 | xmlGenericError(xmlGenericErrorContext, |
| 11818 | "PP: Parsing PI\n"); |
| 11819 | #endif |
| 11820 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11821 | if (ctxt->instate == XML_PARSER_EOF) |
| 11822 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11823 | ctxt->instate = XML_PARSER_PROLOG; |
| 11824 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11825 | } else if ((cur == '<') && (next == '!') && |
| 11826 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11827 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11828 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11829 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11830 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11831 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11832 | #ifdef DEBUG_PUSH |
| 11833 | xmlGenericError(xmlGenericErrorContext, |
| 11834 | "PP: Parsing Comment\n"); |
| 11835 | #endif |
| 11836 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11837 | if (ctxt->instate == XML_PARSER_EOF) |
| 11838 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11839 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11840 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11841 | } else if ((cur == '<') && (next == '!') && |
| 11842 | (avail < 4)) { |
| 11843 | goto done; |
| 11844 | } else { |
| 11845 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11846 | if (ctxt->progressive == 0) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11847 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11848 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11849 | #ifdef DEBUG_PUSH |
| 11850 | xmlGenericError(xmlGenericErrorContext, |
| 11851 | "PP: entering START_TAG\n"); |
| 11852 | #endif |
| 11853 | } |
| 11854 | break; |
| 11855 | case XML_PARSER_EPILOG: |
| 11856 | SKIP_BLANKS; |
| 11857 | if (ctxt->input->buf == NULL) |
| 11858 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11859 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11860 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11861 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11862 | if (avail < 2) |
| 11863 | goto done; |
| 11864 | cur = ctxt->input->cur[0]; |
| 11865 | next = ctxt->input->cur[1]; |
| 11866 | if ((cur == '<') && (next == '?')) { |
| 11867 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11868 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11869 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11870 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11871 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11872 | #ifdef DEBUG_PUSH |
| 11873 | xmlGenericError(xmlGenericErrorContext, |
| 11874 | "PP: Parsing PI\n"); |
| 11875 | #endif |
| 11876 | xmlParsePI(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11877 | if (ctxt->instate == XML_PARSER_EOF) |
| 11878 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11879 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11880 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11881 | } else if ((cur == '<') && (next == '!') && |
| 11882 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11883 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11884 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11885 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11886 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11887 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11888 | #ifdef DEBUG_PUSH |
| 11889 | xmlGenericError(xmlGenericErrorContext, |
| 11890 | "PP: Parsing Comment\n"); |
| 11891 | #endif |
| 11892 | xmlParseComment(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 11893 | if (ctxt->instate == XML_PARSER_EOF) |
| 11894 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11895 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11896 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11897 | } else if ((cur == '<') && (next == '!') && |
| 11898 | (avail < 4)) { |
| 11899 | goto done; |
| 11900 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11901 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11902 | ctxt->instate = XML_PARSER_EOF; |
| 11903 | #ifdef DEBUG_PUSH |
| 11904 | xmlGenericError(xmlGenericErrorContext, |
| 11905 | "PP: entering EOF\n"); |
| 11906 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11907 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11908 | ctxt->sax->endDocument(ctxt->userData); |
| 11909 | goto done; |
| 11910 | } |
| 11911 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11912 | case XML_PARSER_DTD: { |
| 11913 | /* |
| 11914 | * Sorry but progressive parsing of the internal subset |
| 11915 | * is not expected to be supported. We first check that |
| 11916 | * the full content of the internal subset is available and |
| 11917 | * the parsing is launched only at that point. |
| 11918 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11919 | * section and not in a ']]>' sequence which are conditional |
| 11920 | * sections (whoever argued to keep that crap in XML deserve |
| 11921 | * a place in hell !). |
| 11922 | */ |
| 11923 | int base, i; |
| 11924 | xmlChar *buf; |
| 11925 | xmlChar quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11926 | size_t use; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11927 | |
| 11928 | base = ctxt->input->cur - ctxt->input->base; |
| 11929 | if (base < 0) return(0); |
| 11930 | if (ctxt->checkIndex > base) |
| 11931 | base = ctxt->checkIndex; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11932 | buf = xmlBufContent(ctxt->input->buf->buffer); |
| 11933 | use = xmlBufUse(ctxt->input->buf->buffer); |
| 11934 | for (;(unsigned int) base < use; base++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11935 | if (quote != 0) { |
| 11936 | if (buf[base] == quote) |
| 11937 | quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11938 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11939 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11940 | if ((quote == 0) && (buf[base] == '<')) { |
| 11941 | int found = 0; |
| 11942 | /* special handling of comments */ |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11943 | if (((unsigned int) base + 4 < use) && |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11944 | (buf[base + 1] == '!') && |
| 11945 | (buf[base + 2] == '-') && |
| 11946 | (buf[base + 3] == '-')) { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11947 | for (;(unsigned int) base + 3 < use; base++) { |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11948 | if ((buf[base] == '-') && |
| 11949 | (buf[base + 1] == '-') && |
| 11950 | (buf[base + 2] == '>')) { |
| 11951 | found = 1; |
| 11952 | base += 2; |
| 11953 | break; |
| 11954 | } |
| 11955 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11956 | if (!found) { |
| 11957 | #if 0 |
| 11958 | fprintf(stderr, "unfinished comment\n"); |
| 11959 | #endif |
| 11960 | break; /* for */ |
| 11961 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11962 | continue; |
| 11963 | } |
| 11964 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11965 | if (buf[base] == '"') { |
| 11966 | quote = '"'; |
| 11967 | continue; |
| 11968 | } |
| 11969 | if (buf[base] == '\'') { |
| 11970 | quote = '\''; |
| 11971 | continue; |
| 11972 | } |
| 11973 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11974 | #if 0 |
| 11975 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 11976 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 11977 | #endif |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11978 | if ((unsigned int) base +1 >= use) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11979 | break; |
| 11980 | if (buf[base + 1] == ']') { |
| 11981 | /* conditional crap, skip both ']' ! */ |
| 11982 | base++; |
| 11983 | continue; |
| 11984 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11985 | for (i = 1; (unsigned int) base + i < use; i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11986 | if (buf[base + i] == '>') { |
| 11987 | #if 0 |
| 11988 | fprintf(stderr, "found\n"); |
| 11989 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11990 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11991 | } |
| 11992 | if (!IS_BLANK_CH(buf[base + i])) { |
| 11993 | #if 0 |
| 11994 | fprintf(stderr, "not found\n"); |
| 11995 | #endif |
| 11996 | goto not_end_of_int_subset; |
| 11997 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11998 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11999 | #if 0 |
| 12000 | fprintf(stderr, "end of stream\n"); |
| 12001 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12002 | break; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12003 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12004 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 12005 | not_end_of_int_subset: |
| 12006 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12007 | } |
| 12008 | /* |
| 12009 | * We didn't found the end of the Internal subset |
| 12010 | */ |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12011 | if (quote == 0) |
| 12012 | ctxt->checkIndex = base; |
| 12013 | else |
| 12014 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12015 | #ifdef DEBUG_PUSH |
| 12016 | if (next == 0) |
| 12017 | xmlGenericError(xmlGenericErrorContext, |
| 12018 | "PP: lookup of int subset end filed\n"); |
| 12019 | #endif |
| 12020 | goto done; |
| 12021 | |
| 12022 | found_end_int_subset: |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12023 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12024 | xmlParseInternalSubset(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12025 | if (ctxt->instate == XML_PARSER_EOF) |
| 12026 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12027 | ctxt->inSubset = 2; |
| 12028 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 12029 | (ctxt->sax->externalSubset != NULL)) |
| 12030 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 12031 | ctxt->extSubSystem, ctxt->extSubURI); |
| 12032 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 12033 | xmlCleanSpecialAttr(ctxt); |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12034 | if (ctxt->instate == XML_PARSER_EOF) |
| 12035 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12036 | ctxt->instate = XML_PARSER_PROLOG; |
| 12037 | ctxt->checkIndex = 0; |
| 12038 | #ifdef DEBUG_PUSH |
| 12039 | xmlGenericError(xmlGenericErrorContext, |
| 12040 | "PP: entering PROLOG\n"); |
| 12041 | #endif |
| 12042 | break; |
| 12043 | } |
| 12044 | case XML_PARSER_COMMENT: |
| 12045 | xmlGenericError(xmlGenericErrorContext, |
| 12046 | "PP: internal error, state == COMMENT\n"); |
| 12047 | ctxt->instate = XML_PARSER_CONTENT; |
| 12048 | #ifdef DEBUG_PUSH |
| 12049 | xmlGenericError(xmlGenericErrorContext, |
| 12050 | "PP: entering CONTENT\n"); |
| 12051 | #endif |
| 12052 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 12053 | case XML_PARSER_IGNORE: |
| 12054 | xmlGenericError(xmlGenericErrorContext, |
| 12055 | "PP: internal error, state == IGNORE"); |
| 12056 | ctxt->instate = XML_PARSER_DTD; |
| 12057 | #ifdef DEBUG_PUSH |
| 12058 | xmlGenericError(xmlGenericErrorContext, |
| 12059 | "PP: entering DTD\n"); |
| 12060 | #endif |
| 12061 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12062 | case XML_PARSER_PI: |
| 12063 | xmlGenericError(xmlGenericErrorContext, |
| 12064 | "PP: internal error, state == PI\n"); |
| 12065 | ctxt->instate = XML_PARSER_CONTENT; |
| 12066 | #ifdef DEBUG_PUSH |
| 12067 | xmlGenericError(xmlGenericErrorContext, |
| 12068 | "PP: entering CONTENT\n"); |
| 12069 | #endif |
| 12070 | break; |
| 12071 | case XML_PARSER_ENTITY_DECL: |
| 12072 | xmlGenericError(xmlGenericErrorContext, |
| 12073 | "PP: internal error, state == ENTITY_DECL\n"); |
| 12074 | ctxt->instate = XML_PARSER_DTD; |
| 12075 | #ifdef DEBUG_PUSH |
| 12076 | xmlGenericError(xmlGenericErrorContext, |
| 12077 | "PP: entering DTD\n"); |
| 12078 | #endif |
| 12079 | break; |
| 12080 | case XML_PARSER_ENTITY_VALUE: |
| 12081 | xmlGenericError(xmlGenericErrorContext, |
| 12082 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 12083 | ctxt->instate = XML_PARSER_CONTENT; |
| 12084 | #ifdef DEBUG_PUSH |
| 12085 | xmlGenericError(xmlGenericErrorContext, |
| 12086 | "PP: entering DTD\n"); |
| 12087 | #endif |
| 12088 | break; |
| 12089 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 12090 | xmlGenericError(xmlGenericErrorContext, |
| 12091 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 12092 | ctxt->instate = XML_PARSER_START_TAG; |
| 12093 | #ifdef DEBUG_PUSH |
| 12094 | xmlGenericError(xmlGenericErrorContext, |
| 12095 | "PP: entering START_TAG\n"); |
| 12096 | #endif |
| 12097 | break; |
| 12098 | case XML_PARSER_SYSTEM_LITERAL: |
| 12099 | xmlGenericError(xmlGenericErrorContext, |
| 12100 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 12101 | ctxt->instate = XML_PARSER_START_TAG; |
| 12102 | #ifdef DEBUG_PUSH |
| 12103 | xmlGenericError(xmlGenericErrorContext, |
| 12104 | "PP: entering START_TAG\n"); |
| 12105 | #endif |
| 12106 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 12107 | case XML_PARSER_PUBLIC_LITERAL: |
| 12108 | xmlGenericError(xmlGenericErrorContext, |
| 12109 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 12110 | ctxt->instate = XML_PARSER_START_TAG; |
| 12111 | #ifdef DEBUG_PUSH |
| 12112 | xmlGenericError(xmlGenericErrorContext, |
| 12113 | "PP: entering START_TAG\n"); |
| 12114 | #endif |
| 12115 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12116 | } |
| 12117 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12118 | done: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12119 | #ifdef DEBUG_PUSH |
| 12120 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 12121 | #endif |
| 12122 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 12123 | encoding_error: |
| 12124 | { |
| 12125 | char buffer[150]; |
| 12126 | |
| 12127 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 12128 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 12129 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 12130 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 12131 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 12132 | BAD_CAST buffer, NULL); |
| 12133 | } |
| 12134 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12135 | } |
| 12136 | |
| 12137 | /** |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12138 | * xmlParseCheckTransition: |
| 12139 | * @ctxt: an XML parser context |
| 12140 | * @chunk: a char array |
| 12141 | * @size: the size in byte of the chunk |
| 12142 | * |
| 12143 | * Check depending on the current parser state if the chunk given must be |
| 12144 | * processed immediately or one need more data to advance on parsing. |
| 12145 | * |
| 12146 | * Returns -1 in case of error, 0 if the push is not needed and 1 if needed |
| 12147 | */ |
| 12148 | static int |
| 12149 | xmlParseCheckTransition(xmlParserCtxtPtr ctxt, const char *chunk, int size) { |
| 12150 | if ((ctxt == NULL) || (chunk == NULL) || (size < 0)) |
| 12151 | return(-1); |
| 12152 | if (ctxt->instate == XML_PARSER_START_TAG) { |
| 12153 | if (memchr(chunk, '>', size) != NULL) |
| 12154 | return(1); |
| 12155 | return(0); |
| 12156 | } |
| 12157 | if (ctxt->progressive == XML_PARSER_COMMENT) { |
| 12158 | if (memchr(chunk, '>', size) != NULL) |
| 12159 | return(1); |
| 12160 | return(0); |
| 12161 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12162 | if (ctxt->instate == XML_PARSER_CDATA_SECTION) { |
| 12163 | if (memchr(chunk, '>', size) != NULL) |
| 12164 | return(1); |
| 12165 | return(0); |
| 12166 | } |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 12167 | if (ctxt->progressive == XML_PARSER_PI) { |
| 12168 | if (memchr(chunk, '>', size) != NULL) |
| 12169 | return(1); |
| 12170 | return(0); |
| 12171 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12172 | if (ctxt->instate == XML_PARSER_END_TAG) { |
| 12173 | if (memchr(chunk, '>', size) != NULL) |
| 12174 | return(1); |
| 12175 | return(0); |
| 12176 | } |
| 12177 | if ((ctxt->progressive == XML_PARSER_DTD) || |
| 12178 | (ctxt->instate == XML_PARSER_DTD)) { |
Dan Winship | cf8f042 | 2012-12-21 11:13:31 +0800 | [diff] [blame] | 12179 | if (memchr(chunk, '>', size) != NULL) |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12180 | return(1); |
| 12181 | return(0); |
| 12182 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12183 | return(1); |
| 12184 | } |
| 12185 | |
| 12186 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12187 | * xmlParseChunk: |
| 12188 | * @ctxt: an XML parser context |
| 12189 | * @chunk: an char array |
| 12190 | * @size: the size in byte of the chunk |
| 12191 | * @terminate: last chunk indicator |
| 12192 | * |
| 12193 | * Parse a Chunk of memory |
| 12194 | * |
| 12195 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 12196 | */ |
| 12197 | int |
| 12198 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 12199 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12200 | int end_in_lf = 0; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12201 | int remain = 0; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12202 | size_t old_avail = 0; |
| 12203 | size_t avail = 0; |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12204 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12205 | if (ctxt == NULL) |
| 12206 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 12207 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12208 | return(ctxt->errNo); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 12209 | if (ctxt->instate == XML_PARSER_EOF) |
| 12210 | return(-1); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 12211 | if (ctxt->instate == XML_PARSER_START) |
| 12212 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12213 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 12214 | (chunk[size - 1] == '\r')) { |
| 12215 | end_in_lf = 1; |
| 12216 | size--; |
| 12217 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12218 | |
| 12219 | xmldecl_done: |
| 12220 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12221 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12222 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12223 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12224 | size_t cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12225 | int res; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12226 | |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12227 | old_avail = xmlBufUse(ctxt->input->buf->buffer); |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12228 | /* |
| 12229 | * Specific handling if we autodetected an encoding, we should not |
| 12230 | * push more than the first line ... which depend on the encoding |
| 12231 | * And only push the rest once the final encoding was detected |
| 12232 | */ |
| 12233 | if ((ctxt->instate == XML_PARSER_START) && (ctxt->input != NULL) && |
| 12234 | (ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL)) { |
Nikolay Sivov | 7304683 | 2010-01-19 15:38:05 +0100 | [diff] [blame] | 12235 | unsigned int len = 45; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12236 | |
| 12237 | if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12238 | BAD_CAST "UTF-16")) || |
| 12239 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12240 | BAD_CAST "UTF16"))) |
| 12241 | len = 90; |
| 12242 | else if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12243 | BAD_CAST "UCS-4")) || |
| 12244 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12245 | BAD_CAST "UCS4"))) |
| 12246 | len = 180; |
| 12247 | |
| 12248 | if (ctxt->input->buf->rawconsumed < len) |
| 12249 | len -= ctxt->input->buf->rawconsumed; |
| 12250 | |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12251 | /* |
| 12252 | * Change size for reading the initial declaration only |
| 12253 | * if size is greater than len. Otherwise, memmove in xmlBufferAdd |
| 12254 | * will blindly copy extra bytes from memory. |
| 12255 | */ |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 12256 | if ((unsigned int) size > len) { |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12257 | remain = size - len; |
| 12258 | size = len; |
| 12259 | } else { |
| 12260 | remain = 0; |
| 12261 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12262 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12263 | res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12264 | if (res < 0) { |
| 12265 | ctxt->errNo = XML_PARSER_EOF; |
| 12266 | ctxt->disableSAX = 1; |
| 12267 | return (XML_PARSER_EOF); |
| 12268 | } |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12269 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12270 | #ifdef DEBUG_PUSH |
| 12271 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12272 | #endif |
| 12273 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12274 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 12275 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 12276 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 12277 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 12278 | (in->raw != NULL)) { |
| 12279 | int nbchars; |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12280 | size_t base = xmlBufGetInputBase(in->buffer, ctxt->input); |
| 12281 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12282 | |
Daniel Veillard | bf058dc | 2013-02-13 18:19:42 +0800 | [diff] [blame] | 12283 | nbchars = xmlCharEncInput(in, terminate); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12284 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12285 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12286 | xmlGenericError(xmlGenericErrorContext, |
| 12287 | "xmlParseChunk: encoder error\n"); |
| 12288 | return(XML_ERR_INVALID_ENCODING); |
| 12289 | } |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12290 | xmlBufSetInputBaseCur(in->buffer, ctxt->input, base, current); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12291 | } |
| 12292 | } |
| 12293 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12294 | if (remain != 0) { |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12295 | xmlParseTryOrFinish(ctxt, 0); |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12296 | } else { |
| 12297 | if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) |
| 12298 | avail = xmlBufUse(ctxt->input->buf->buffer); |
| 12299 | /* |
| 12300 | * Depending on the current state it may not be such |
| 12301 | * a good idea to try parsing if there is nothing in the chunk |
| 12302 | * which would be worth doing a parser state transition and we |
| 12303 | * need to wait for more data |
| 12304 | */ |
| 12305 | if ((terminate) || (avail > XML_MAX_TEXT_LENGTH) || |
| 12306 | (old_avail == 0) || (avail == 0) || |
| 12307 | (xmlParseCheckTransition(ctxt, |
| 12308 | (const char *)&ctxt->input->base[old_avail], |
| 12309 | avail - old_avail))) |
| 12310 | xmlParseTryOrFinish(ctxt, terminate); |
| 12311 | } |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12312 | if (ctxt->instate == XML_PARSER_EOF) |
| 12313 | return(ctxt->errNo); |
| 12314 | |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12315 | if ((ctxt->input != NULL) && |
| 12316 | (((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || |
| 12317 | ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && |
| 12318 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 12319 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
| 12320 | ctxt->instate = XML_PARSER_EOF; |
| 12321 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12322 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 12323 | return(ctxt->errNo); |
| 12324 | |
| 12325 | if (remain != 0) { |
| 12326 | chunk += size; |
| 12327 | size = remain; |
| 12328 | remain = 0; |
| 12329 | goto xmldecl_done; |
| 12330 | } |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12331 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 12332 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12333 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 12334 | ctxt->input); |
| 12335 | size_t current = ctxt->input->cur - ctxt->input->base; |
| 12336 | |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12337 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
Daniel Veillard | de0cc20 | 2013-02-12 16:55:34 +0800 | [diff] [blame] | 12338 | |
| 12339 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 12340 | base, current); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12341 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12342 | if (terminate) { |
| 12343 | /* |
| 12344 | * Check for termination |
| 12345 | */ |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12346 | int cur_avail = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12347 | |
| 12348 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12349 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12350 | cur_avail = ctxt->input->length - |
| 12351 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12352 | else |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12353 | cur_avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 12354 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12355 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12356 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12357 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 12358 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12359 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12360 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12361 | if ((ctxt->instate == XML_PARSER_EPILOG) && (cur_avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12362 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12363 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12364 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 12365 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12366 | ctxt->sax->endDocument(ctxt->userData); |
| 12367 | } |
| 12368 | ctxt->instate = XML_PARSER_EOF; |
| 12369 | } |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 12370 | if (ctxt->wellFormed == 0) |
| 12371 | return((xmlParserErrors) ctxt->errNo); |
| 12372 | else |
| 12373 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12374 | } |
| 12375 | |
| 12376 | /************************************************************************ |
| 12377 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12378 | * I/O front end functions to the parser * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12379 | * * |
| 12380 | ************************************************************************/ |
| 12381 | |
| 12382 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12383 | * xmlCreatePushParserCtxt: |
| 12384 | * @sax: a SAX handler |
| 12385 | * @user_data: The user data returned on SAX callbacks |
| 12386 | * @chunk: a pointer to an array of chars |
| 12387 | * @size: number of chars in the array |
| 12388 | * @filename: an optional file name or URI |
| 12389 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12390 | * Create a parser context for using the XML parser in push mode. |
| 12391 | * If @buffer and @size are non-NULL, the data is used to detect |
| 12392 | * the encoding. The remaining characters will be parsed so they |
| 12393 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12394 | * To allow content encoding detection, @size should be >= 4 |
| 12395 | * The value of @filename is used for fetching external entities |
| 12396 | * and error/warning reports. |
| 12397 | * |
| 12398 | * Returns the new parser context or NULL |
| 12399 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12400 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12401 | xmlParserCtxtPtr |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12402 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12403 | const char *chunk, int size, const char *filename) { |
| 12404 | xmlParserCtxtPtr ctxt; |
| 12405 | xmlParserInputPtr inputStream; |
| 12406 | xmlParserInputBufferPtr buf; |
| 12407 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12408 | |
| 12409 | /* |
| 12410 | * plug some encoding conversion routines |
| 12411 | */ |
| 12412 | if ((chunk != NULL) && (size >= 4)) |
| 12413 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12414 | |
| 12415 | buf = xmlAllocParserInputBuffer(enc); |
| 12416 | if (buf == NULL) return(NULL); |
| 12417 | |
| 12418 | ctxt = xmlNewParserCtxt(); |
| 12419 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12420 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12421 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12422 | return(NULL); |
| 12423 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12424 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12425 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 12426 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12427 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12428 | xmlFreeParserInputBuffer(buf); |
| 12429 | xmlFreeParserCtxt(ctxt); |
| 12430 | return(NULL); |
| 12431 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12432 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12433 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12434 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12435 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12436 | xmlFree(ctxt->sax); |
| 12437 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12438 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12439 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12440 | xmlFreeParserInputBuffer(buf); |
| 12441 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12442 | return(NULL); |
| 12443 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12444 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12445 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12446 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12447 | else |
| 12448 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12449 | if (user_data != NULL) |
| 12450 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12451 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12452 | if (filename == NULL) { |
| 12453 | ctxt->directory = NULL; |
| 12454 | } else { |
| 12455 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12456 | } |
| 12457 | |
| 12458 | inputStream = xmlNewInputStream(ctxt); |
| 12459 | if (inputStream == NULL) { |
| 12460 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12461 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12462 | return(NULL); |
| 12463 | } |
| 12464 | |
| 12465 | if (filename == NULL) |
| 12466 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12467 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 12468 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 12469 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12470 | if (inputStream->filename == NULL) { |
| 12471 | xmlFreeParserCtxt(ctxt); |
| 12472 | xmlFreeParserInputBuffer(buf); |
| 12473 | return(NULL); |
| 12474 | } |
| 12475 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12476 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 12477 | xmlBufResetInput(inputStream->buf->buffer, inputStream); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12478 | inputPush(ctxt, inputStream); |
| 12479 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 12480 | /* |
| 12481 | * If the caller didn't provide an initial 'chunk' for determining |
| 12482 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 12483 | * that it can be automatically determined later |
| 12484 | */ |
| 12485 | if ((size == 0) || (chunk == NULL)) { |
| 12486 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 12487 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12488 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12489 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12490 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12491 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12492 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12493 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12494 | #ifdef DEBUG_PUSH |
| 12495 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12496 | #endif |
| 12497 | } |
| 12498 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 12499 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12500 | xmlSwitchEncoding(ctxt, enc); |
| 12501 | } |
| 12502 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12503 | return(ctxt); |
| 12504 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 12505 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12506 | |
| 12507 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12508 | * xmlStopParser: |
| 12509 | * @ctxt: an XML parser context |
| 12510 | * |
| 12511 | * Blocks further parser processing |
| 12512 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12513 | void |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12514 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 12515 | if (ctxt == NULL) |
| 12516 | return; |
| 12517 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | e50ba81 | 2013-04-11 15:54:51 +0800 | [diff] [blame] | 12518 | ctxt->errNo = XML_ERR_USER_STOP; |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12519 | ctxt->disableSAX = 1; |
| 12520 | if (ctxt->input != NULL) { |
| 12521 | ctxt->input->cur = BAD_CAST""; |
| 12522 | ctxt->input->base = ctxt->input->cur; |
| 12523 | } |
| 12524 | } |
| 12525 | |
| 12526 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12527 | * xmlCreateIOParserCtxt: |
| 12528 | * @sax: a SAX handler |
| 12529 | * @user_data: The user data returned on SAX callbacks |
| 12530 | * @ioread: an I/O read function |
| 12531 | * @ioclose: an I/O close function |
| 12532 | * @ioctx: an I/O handler |
| 12533 | * @enc: the charset encoding if known |
| 12534 | * |
| 12535 | * Create a parser context for using the XML parser with an existing |
| 12536 | * I/O stream |
| 12537 | * |
| 12538 | * Returns the new parser context or NULL |
| 12539 | */ |
| 12540 | xmlParserCtxtPtr |
| 12541 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 12542 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 12543 | void *ioctx, xmlCharEncoding enc) { |
| 12544 | xmlParserCtxtPtr ctxt; |
| 12545 | xmlParserInputPtr inputStream; |
| 12546 | xmlParserInputBufferPtr buf; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12547 | |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 12548 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12549 | |
| 12550 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12551 | if (buf == NULL) { |
| 12552 | if (ioclose != NULL) |
| 12553 | ioclose(ioctx); |
| 12554 | return (NULL); |
| 12555 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12556 | |
| 12557 | ctxt = xmlNewParserCtxt(); |
| 12558 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12559 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12560 | return(NULL); |
| 12561 | } |
| 12562 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12563 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12564 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12565 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12566 | xmlFree(ctxt->sax); |
| 12567 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12568 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12569 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12570 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12571 | return(NULL); |
| 12572 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12573 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12574 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12575 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12576 | else |
| 12577 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12578 | if (user_data != NULL) |
| 12579 | ctxt->userData = user_data; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12580 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12581 | |
| 12582 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 12583 | if (inputStream == NULL) { |
| 12584 | xmlFreeParserCtxt(ctxt); |
| 12585 | return(NULL); |
| 12586 | } |
| 12587 | inputPush(ctxt, inputStream); |
| 12588 | |
| 12589 | return(ctxt); |
| 12590 | } |
| 12591 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12592 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12593 | /************************************************************************ |
| 12594 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12595 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12596 | * * |
| 12597 | ************************************************************************/ |
| 12598 | |
| 12599 | /** |
| 12600 | * xmlIOParseDTD: |
| 12601 | * @sax: the SAX handler block or NULL |
| 12602 | * @input: an Input Buffer |
| 12603 | * @enc: the charset encoding if known |
| 12604 | * |
| 12605 | * Load and parse a DTD |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12606 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12607 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12608 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12609 | */ |
| 12610 | |
| 12611 | xmlDtdPtr |
| 12612 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 12613 | xmlCharEncoding enc) { |
| 12614 | xmlDtdPtr ret = NULL; |
| 12615 | xmlParserCtxtPtr ctxt; |
| 12616 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12617 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12618 | |
| 12619 | if (input == NULL) |
| 12620 | return(NULL); |
| 12621 | |
| 12622 | ctxt = xmlNewParserCtxt(); |
| 12623 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12624 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12625 | return(NULL); |
| 12626 | } |
| 12627 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12628 | /* We are loading a DTD */ |
| 12629 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12630 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12631 | /* |
| 12632 | * Set-up the SAX context |
| 12633 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12634 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12635 | if (ctxt->sax != NULL) |
| 12636 | xmlFree(ctxt->sax); |
| 12637 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 12638 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12639 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12640 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12641 | |
| 12642 | /* |
| 12643 | * generate a parser input from the I/O handler |
| 12644 | */ |
| 12645 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12646 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12647 | if (pinput == NULL) { |
| 12648 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12649 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12650 | xmlFreeParserCtxt(ctxt); |
| 12651 | return(NULL); |
| 12652 | } |
| 12653 | |
| 12654 | /* |
| 12655 | * plug some encoding conversion routines here. |
| 12656 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12657 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 12658 | if (sax != NULL) ctxt->sax = NULL; |
| 12659 | xmlFreeParserCtxt(ctxt); |
| 12660 | return(NULL); |
| 12661 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12662 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12663 | xmlSwitchEncoding(ctxt, enc); |
| 12664 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12665 | |
| 12666 | pinput->filename = NULL; |
| 12667 | pinput->line = 1; |
| 12668 | pinput->col = 1; |
| 12669 | pinput->base = ctxt->input->cur; |
| 12670 | pinput->cur = ctxt->input->cur; |
| 12671 | pinput->free = NULL; |
| 12672 | |
| 12673 | /* |
| 12674 | * let's parse that entity knowing it's an external subset. |
| 12675 | */ |
| 12676 | ctxt->inSubset = 2; |
| 12677 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12678 | if (ctxt->myDoc == NULL) { |
| 12679 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12680 | return(NULL); |
| 12681 | } |
| 12682 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12683 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12684 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12685 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12686 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 12687 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12688 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12689 | * Get the 4 first bytes and decode the charset |
| 12690 | * if enc != XML_CHAR_ENCODING_NONE |
| 12691 | * plug some encoding conversion routines. |
| 12692 | */ |
| 12693 | start[0] = RAW; |
| 12694 | start[1] = NXT(1); |
| 12695 | start[2] = NXT(2); |
| 12696 | start[3] = NXT(3); |
| 12697 | enc = xmlDetectCharEncoding(start, 4); |
| 12698 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12699 | xmlSwitchEncoding(ctxt, enc); |
| 12700 | } |
| 12701 | } |
| 12702 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12703 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 12704 | |
| 12705 | if (ctxt->myDoc != NULL) { |
| 12706 | if (ctxt->wellFormed) { |
| 12707 | ret = ctxt->myDoc->extSubset; |
| 12708 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 12709 | if (ret != NULL) { |
| 12710 | xmlNodePtr tmp; |
| 12711 | |
| 12712 | ret->doc = NULL; |
| 12713 | tmp = ret->children; |
| 12714 | while (tmp != NULL) { |
| 12715 | tmp->doc = NULL; |
| 12716 | tmp = tmp->next; |
| 12717 | } |
| 12718 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12719 | } else { |
| 12720 | ret = NULL; |
| 12721 | } |
| 12722 | xmlFreeDoc(ctxt->myDoc); |
| 12723 | ctxt->myDoc = NULL; |
| 12724 | } |
| 12725 | if (sax != NULL) ctxt->sax = NULL; |
| 12726 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12727 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12728 | return(ret); |
| 12729 | } |
| 12730 | |
| 12731 | /** |
| 12732 | * xmlSAXParseDTD: |
| 12733 | * @sax: the SAX handler block |
| 12734 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12735 | * @SystemID: a NAME* containing the URL to the DTD |
| 12736 | * |
| 12737 | * Load and parse an external subset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12738 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12739 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12740 | */ |
| 12741 | |
| 12742 | xmlDtdPtr |
| 12743 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 12744 | const xmlChar *SystemID) { |
| 12745 | xmlDtdPtr ret = NULL; |
| 12746 | xmlParserCtxtPtr ctxt; |
| 12747 | xmlParserInputPtr input = NULL; |
| 12748 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12749 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12750 | |
| 12751 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 12752 | |
| 12753 | ctxt = xmlNewParserCtxt(); |
| 12754 | if (ctxt == NULL) { |
| 12755 | return(NULL); |
| 12756 | } |
| 12757 | |
Daniel Veillard | dd8367d | 2014-06-11 16:54:32 +0800 | [diff] [blame] | 12758 | /* We are loading a DTD */ |
| 12759 | ctxt->options |= XML_PARSE_DTDLOAD; |
| 12760 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12761 | /* |
| 12762 | * Set-up the SAX context |
| 12763 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12764 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12765 | if (ctxt->sax != NULL) |
| 12766 | xmlFree(ctxt->sax); |
| 12767 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 12768 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12769 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12770 | |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12771 | /* |
| 12772 | * Canonicalise the system ID |
| 12773 | */ |
| 12774 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 12775 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12776 | xmlFreeParserCtxt(ctxt); |
| 12777 | return(NULL); |
| 12778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12779 | |
| 12780 | /* |
| 12781 | * Ask the Entity resolver to load the damn thing |
| 12782 | */ |
| 12783 | |
| 12784 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 12785 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 12786 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12787 | if (input == NULL) { |
| 12788 | if (sax != NULL) ctxt->sax = NULL; |
| 12789 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12790 | if (systemIdCanonic != NULL) |
| 12791 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12792 | return(NULL); |
| 12793 | } |
| 12794 | |
| 12795 | /* |
| 12796 | * plug some encoding conversion routines here. |
| 12797 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12798 | if (xmlPushInput(ctxt, input) < 0) { |
| 12799 | if (sax != NULL) ctxt->sax = NULL; |
| 12800 | xmlFreeParserCtxt(ctxt); |
| 12801 | if (systemIdCanonic != NULL) |
| 12802 | xmlFree(systemIdCanonic); |
| 12803 | return(NULL); |
| 12804 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12805 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12806 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 12807 | xmlSwitchEncoding(ctxt, enc); |
| 12808 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12809 | |
| 12810 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12811 | input->filename = (char *) systemIdCanonic; |
| 12812 | else |
| 12813 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12814 | input->line = 1; |
| 12815 | input->col = 1; |
| 12816 | input->base = ctxt->input->cur; |
| 12817 | input->cur = ctxt->input->cur; |
| 12818 | input->free = NULL; |
| 12819 | |
| 12820 | /* |
| 12821 | * let's parse that entity knowing it's an external subset. |
| 12822 | */ |
| 12823 | ctxt->inSubset = 2; |
| 12824 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12825 | if (ctxt->myDoc == NULL) { |
| 12826 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12827 | if (sax != NULL) ctxt->sax = NULL; |
| 12828 | xmlFreeParserCtxt(ctxt); |
| 12829 | return(NULL); |
| 12830 | } |
| 12831 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12832 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12833 | ExternalID, SystemID); |
| 12834 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 12835 | |
| 12836 | if (ctxt->myDoc != NULL) { |
| 12837 | if (ctxt->wellFormed) { |
| 12838 | ret = ctxt->myDoc->extSubset; |
| 12839 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 12840 | if (ret != NULL) { |
| 12841 | xmlNodePtr tmp; |
| 12842 | |
| 12843 | ret->doc = NULL; |
| 12844 | tmp = ret->children; |
| 12845 | while (tmp != NULL) { |
| 12846 | tmp->doc = NULL; |
| 12847 | tmp = tmp->next; |
| 12848 | } |
| 12849 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12850 | } else { |
| 12851 | ret = NULL; |
| 12852 | } |
| 12853 | xmlFreeDoc(ctxt->myDoc); |
| 12854 | ctxt->myDoc = NULL; |
| 12855 | } |
| 12856 | if (sax != NULL) ctxt->sax = NULL; |
| 12857 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12858 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12859 | return(ret); |
| 12860 | } |
| 12861 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12862 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12863 | /** |
| 12864 | * xmlParseDTD: |
| 12865 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12866 | * @SystemID: a NAME* containing the URL to the DTD |
| 12867 | * |
| 12868 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12869 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12870 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12871 | */ |
| 12872 | |
| 12873 | xmlDtdPtr |
| 12874 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 12875 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 12876 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12877 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12878 | |
| 12879 | /************************************************************************ |
| 12880 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12881 | * Front ends when parsing an Entity * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12882 | * * |
| 12883 | ************************************************************************/ |
| 12884 | |
| 12885 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12886 | * xmlParseCtxtExternalEntity: |
| 12887 | * @ctx: the existing parsing context |
| 12888 | * @URL: the URL for the entity to load |
| 12889 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12890 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12891 | * |
| 12892 | * Parse an external general entity within an existing parsing context |
| 12893 | * An external general parsed entity is well-formed if it matches the |
| 12894 | * production labeled extParsedEnt. |
| 12895 | * |
| 12896 | * [78] extParsedEnt ::= TextDecl? content |
| 12897 | * |
| 12898 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12899 | * the parser error code otherwise |
| 12900 | */ |
| 12901 | |
| 12902 | int |
| 12903 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12904 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12905 | xmlParserCtxtPtr ctxt; |
| 12906 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12907 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12908 | xmlSAXHandlerPtr oldsax = NULL; |
| 12909 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12910 | xmlChar start[4]; |
| 12911 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12912 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12913 | if (ctx == NULL) return(-1); |
| 12914 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12915 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12916 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12917 | return(XML_ERR_ENTITY_LOOP); |
| 12918 | } |
| 12919 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12920 | if (lst != NULL) |
| 12921 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12922 | if ((URL == NULL) && (ID == NULL)) |
| 12923 | return(-1); |
| 12924 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12925 | return(-1); |
| 12926 | |
Rob Richards | 798743a | 2009-06-19 13:54:25 -0400 | [diff] [blame] | 12927 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, ctx); |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12928 | if (ctxt == NULL) { |
| 12929 | return(-1); |
| 12930 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12931 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12932 | oldsax = ctxt->sax; |
| 12933 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12934 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12935 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12936 | if (newDoc == NULL) { |
| 12937 | xmlFreeParserCtxt(ctxt); |
| 12938 | return(-1); |
| 12939 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12940 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12941 | if (ctx->myDoc->dict) { |
| 12942 | newDoc->dict = ctx->myDoc->dict; |
| 12943 | xmlDictReference(newDoc->dict); |
| 12944 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12945 | if (ctx->myDoc != NULL) { |
| 12946 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12947 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12948 | } |
| 12949 | if (ctx->myDoc->URL != NULL) { |
| 12950 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12951 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12952 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12953 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12954 | ctxt->sax = oldsax; |
| 12955 | xmlFreeParserCtxt(ctxt); |
| 12956 | newDoc->intSubset = NULL; |
| 12957 | newDoc->extSubset = NULL; |
| 12958 | xmlFreeDoc(newDoc); |
| 12959 | return(-1); |
| 12960 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12961 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12962 | nodePush(ctxt, newDoc->children); |
| 12963 | if (ctx->myDoc == NULL) { |
| 12964 | ctxt->myDoc = newDoc; |
| 12965 | } else { |
| 12966 | ctxt->myDoc = ctx->myDoc; |
| 12967 | newDoc->children->doc = ctx->myDoc; |
| 12968 | } |
| 12969 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12970 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12971 | * Get the 4 first bytes and decode the charset |
| 12972 | * if enc != XML_CHAR_ENCODING_NONE |
| 12973 | * plug some encoding conversion routines. |
| 12974 | */ |
| 12975 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12976 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12977 | start[0] = RAW; |
| 12978 | start[1] = NXT(1); |
| 12979 | start[2] = NXT(2); |
| 12980 | start[3] = NXT(3); |
| 12981 | enc = xmlDetectCharEncoding(start, 4); |
| 12982 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12983 | xmlSwitchEncoding(ctxt, enc); |
| 12984 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12985 | } |
| 12986 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12987 | /* |
| 12988 | * Parse a possible text declaration first |
| 12989 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12990 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12991 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12992 | /* |
| 12993 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 12994 | */ |
| 12995 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 12996 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12997 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12998 | "Version mismatch between document and entity\n"); |
| 12999 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13000 | } |
| 13001 | |
| 13002 | /* |
Daniel Veillard | 4aa68ab | 2012-04-02 17:50:54 +0800 | [diff] [blame] | 13003 | * If the user provided its own SAX callbacks then reuse the |
| 13004 | * useData callback field, otherwise the expected setup in a |
| 13005 | * DOM builder is to have userData == ctxt |
| 13006 | */ |
| 13007 | if (ctx->userData == ctx) |
| 13008 | ctxt->userData = ctxt; |
| 13009 | else |
| 13010 | ctxt->userData = ctx->userData; |
| 13011 | |
| 13012 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13013 | * Doing validity checking on chunk doesn't make sense |
| 13014 | */ |
| 13015 | ctxt->instate = XML_PARSER_CONTENT; |
| 13016 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13017 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13018 | ctxt->loadsubset = ctx->loadsubset; |
| 13019 | ctxt->depth = ctx->depth + 1; |
| 13020 | ctxt->replaceEntities = ctx->replaceEntities; |
| 13021 | if (ctxt->validate) { |
| 13022 | ctxt->vctxt.error = ctx->vctxt.error; |
| 13023 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13024 | } else { |
| 13025 | ctxt->vctxt.error = NULL; |
| 13026 | ctxt->vctxt.warning = NULL; |
| 13027 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 13028 | ctxt->vctxt.nodeTab = NULL; |
| 13029 | ctxt->vctxt.nodeNr = 0; |
| 13030 | ctxt->vctxt.nodeMax = 0; |
| 13031 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13032 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13033 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13034 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13035 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13036 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13037 | ctxt->dictNames = ctx->dictNames; |
| 13038 | ctxt->attsDefault = ctx->attsDefault; |
| 13039 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 13040 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13041 | |
| 13042 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13043 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 13044 | ctx->validate = ctxt->validate; |
| 13045 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13046 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13047 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13048 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13049 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13050 | } |
| 13051 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13052 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13053 | } |
| 13054 | |
| 13055 | if (!ctxt->wellFormed) { |
| 13056 | if (ctxt->errNo == 0) |
| 13057 | ret = 1; |
| 13058 | else |
| 13059 | ret = ctxt->errNo; |
| 13060 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13061 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13062 | xmlNodePtr cur; |
| 13063 | |
| 13064 | /* |
| 13065 | * Return the newly created nodeset after unlinking it from |
| 13066 | * they pseudo parent. |
| 13067 | */ |
| 13068 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13069 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13070 | while (cur != NULL) { |
| 13071 | cur->parent = NULL; |
| 13072 | cur = cur->next; |
| 13073 | } |
| 13074 | newDoc->children->children = NULL; |
| 13075 | } |
| 13076 | ret = 0; |
| 13077 | } |
| 13078 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13079 | ctxt->dict = NULL; |
| 13080 | ctxt->attsDefault = NULL; |
| 13081 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13082 | xmlFreeParserCtxt(ctxt); |
| 13083 | newDoc->intSubset = NULL; |
| 13084 | newDoc->extSubset = NULL; |
| 13085 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 13086 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13087 | return(ret); |
| 13088 | } |
| 13089 | |
| 13090 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13091 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13092 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13093 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13094 | * @sax: the SAX handler bloc (possibly NULL) |
| 13095 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13096 | * @depth: Used for loop detection, use 0 |
| 13097 | * @URL: the URL for the entity to load |
| 13098 | * @ID: the System ID for the entity to load |
| 13099 | * @list: the return value for the set of parsed nodes |
| 13100 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13101 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13102 | * |
| 13103 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13104 | * the parser error code otherwise |
| 13105 | */ |
| 13106 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13107 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13108 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 13109 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13110 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13111 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13112 | xmlParserCtxtPtr ctxt; |
| 13113 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13114 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13115 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13116 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13117 | xmlChar start[4]; |
| 13118 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13119 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13120 | if (((depth > 40) && |
| 13121 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13122 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13123 | return(XML_ERR_ENTITY_LOOP); |
| 13124 | } |
| 13125 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13126 | if (list != NULL) |
| 13127 | *list = NULL; |
| 13128 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13129 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13130 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13131 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13132 | |
| 13133 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13134 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 13135 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13136 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13137 | if (oldctxt != NULL) { |
| 13138 | ctxt->_private = oldctxt->_private; |
| 13139 | ctxt->loadsubset = oldctxt->loadsubset; |
| 13140 | ctxt->validate = oldctxt->validate; |
| 13141 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13142 | ctxt->record_info = oldctxt->record_info; |
| 13143 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 13144 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 13145 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13146 | } else { |
| 13147 | /* |
| 13148 | * Doing validity checking on chunk without context |
| 13149 | * doesn't make sense |
| 13150 | */ |
| 13151 | ctxt->_private = NULL; |
| 13152 | ctxt->validate = 0; |
| 13153 | ctxt->external = 2; |
| 13154 | ctxt->loadsubset = 0; |
| 13155 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13156 | if (sax != NULL) { |
| 13157 | oldsax = ctxt->sax; |
| 13158 | ctxt->sax = sax; |
| 13159 | if (user_data != NULL) |
| 13160 | ctxt->userData = user_data; |
| 13161 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13162 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13163 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13164 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13165 | ctxt->node_seq.maximum = 0; |
| 13166 | ctxt->node_seq.length = 0; |
| 13167 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13168 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13169 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13170 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13171 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13172 | newDoc->intSubset = doc->intSubset; |
| 13173 | newDoc->extSubset = doc->extSubset; |
| 13174 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13175 | xmlDictReference(newDoc->dict); |
| 13176 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13177 | if (doc->URL != NULL) { |
| 13178 | newDoc->URL = xmlStrdup(doc->URL); |
| 13179 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13180 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13181 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13182 | if (sax != NULL) |
| 13183 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13184 | ctxt->node_seq.maximum = 0; |
| 13185 | ctxt->node_seq.length = 0; |
| 13186 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13187 | xmlFreeParserCtxt(ctxt); |
| 13188 | newDoc->intSubset = NULL; |
| 13189 | newDoc->extSubset = NULL; |
| 13190 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13191 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13192 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13193 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13194 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13195 | ctxt->myDoc = doc; |
| 13196 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13197 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13198 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13199 | * Get the 4 first bytes and decode the charset |
| 13200 | * if enc != XML_CHAR_ENCODING_NONE |
| 13201 | * plug some encoding conversion routines. |
| 13202 | */ |
| 13203 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13204 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13205 | start[0] = RAW; |
| 13206 | start[1] = NXT(1); |
| 13207 | start[2] = NXT(2); |
| 13208 | start[3] = NXT(3); |
| 13209 | enc = xmlDetectCharEncoding(start, 4); |
| 13210 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13211 | xmlSwitchEncoding(ctxt, enc); |
| 13212 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13213 | } |
| 13214 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13215 | /* |
| 13216 | * Parse a possible text declaration first |
| 13217 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13218 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13219 | xmlParseTextDecl(ctxt); |
| 13220 | } |
| 13221 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13222 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13223 | ctxt->depth = depth; |
| 13224 | |
| 13225 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13226 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13227 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13228 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13229 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13230 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13231 | } |
| 13232 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13233 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13234 | } |
| 13235 | |
| 13236 | if (!ctxt->wellFormed) { |
| 13237 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13238 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13239 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13240 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13241 | } else { |
| 13242 | if (list != NULL) { |
| 13243 | xmlNodePtr cur; |
| 13244 | |
| 13245 | /* |
| 13246 | * Return the newly created nodeset after unlinking it from |
| 13247 | * they pseudo parent. |
| 13248 | */ |
| 13249 | cur = newDoc->children->children; |
| 13250 | *list = cur; |
| 13251 | while (cur != NULL) { |
| 13252 | cur->parent = NULL; |
| 13253 | cur = cur->next; |
| 13254 | } |
| 13255 | newDoc->children->children = NULL; |
| 13256 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13257 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13258 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13259 | |
| 13260 | /* |
| 13261 | * Record in the parent context the number of entities replacement |
| 13262 | * done when parsing that reference. |
| 13263 | */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 13264 | if (oldctxt != NULL) |
| 13265 | oldctxt->nbentities += ctxt->nbentities; |
| 13266 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13267 | /* |
| 13268 | * Also record the size of the entity parsed |
| 13269 | */ |
| 13270 | if (ctxt->input != NULL) { |
| 13271 | oldctxt->sizeentities += ctxt->input->consumed; |
| 13272 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 13273 | } |
| 13274 | /* |
| 13275 | * And record the last error if any |
| 13276 | */ |
| 13277 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13278 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13279 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13280 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13281 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 13282 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 13283 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 13284 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13285 | ctxt->node_seq.maximum = 0; |
| 13286 | ctxt->node_seq.length = 0; |
| 13287 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13288 | xmlFreeParserCtxt(ctxt); |
| 13289 | newDoc->intSubset = NULL; |
| 13290 | newDoc->extSubset = NULL; |
| 13291 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13292 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13293 | return(ret); |
| 13294 | } |
| 13295 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13296 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13297 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13298 | * xmlParseExternalEntity: |
| 13299 | * @doc: the document the chunk pertains to |
| 13300 | * @sax: the SAX handler bloc (possibly NULL) |
| 13301 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13302 | * @depth: Used for loop detection, use 0 |
| 13303 | * @URL: the URL for the entity to load |
| 13304 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13305 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13306 | * |
| 13307 | * Parse an external general entity |
| 13308 | * An external general parsed entity is well-formed if it matches the |
| 13309 | * production labeled extParsedEnt. |
| 13310 | * |
| 13311 | * [78] extParsedEnt ::= TextDecl? content |
| 13312 | * |
| 13313 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13314 | * the parser error code otherwise |
| 13315 | */ |
| 13316 | |
| 13317 | int |
| 13318 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13319 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13320 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13321 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13322 | } |
| 13323 | |
| 13324 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 13325 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13326 | * @doc: the document the chunk pertains to |
| 13327 | * @sax: the SAX handler bloc (possibly NULL) |
| 13328 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13329 | * @depth: Used for loop detection, use 0 |
| 13330 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13331 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13332 | * |
| 13333 | * Parse a well-balanced chunk of an XML document |
| 13334 | * called by the parser |
| 13335 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13336 | * the content production in the XML grammar: |
| 13337 | * |
| 13338 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13339 | * |
| 13340 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13341 | * the parser error code otherwise |
| 13342 | */ |
| 13343 | |
| 13344 | int |
| 13345 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13346 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13347 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 13348 | depth, string, lst, 0 ); |
| 13349 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13350 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13351 | |
| 13352 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13353 | * xmlParseBalancedChunkMemoryInternal: |
| 13354 | * @oldctxt: the existing parsing context |
| 13355 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13356 | * @user_data: the user data field for the parser context |
| 13357 | * @lst: the return value for the set of parsed nodes |
| 13358 | * |
| 13359 | * |
| 13360 | * Parse a well-balanced chunk of an XML document |
| 13361 | * called by the parser |
| 13362 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13363 | * the content production in the XML grammar: |
| 13364 | * |
| 13365 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13366 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13367 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13368 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13369 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13370 | * 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] | 13371 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13372 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13373 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13374 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 13375 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 13376 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13377 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13378 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13379 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13380 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13381 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13382 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13383 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13384 | #ifdef SAX2 |
| 13385 | int i; |
| 13386 | #endif |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13387 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13388 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13389 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13390 | return(XML_ERR_ENTITY_LOOP); |
| 13391 | } |
| 13392 | |
| 13393 | |
| 13394 | if (lst != NULL) |
| 13395 | *lst = NULL; |
| 13396 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13397 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13398 | |
| 13399 | size = xmlStrlen(string); |
| 13400 | |
| 13401 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13402 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13403 | if (user_data != NULL) |
| 13404 | ctxt->userData = user_data; |
| 13405 | else |
| 13406 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13407 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13408 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13409 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13410 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13411 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13412 | |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13413 | #ifdef SAX2 |
| 13414 | /* propagate namespaces down the entity */ |
| 13415 | for (i = 0;i < oldctxt->nsNr;i += 2) { |
| 13416 | nsPush(ctxt, oldctxt->nsTab[i], oldctxt->nsTab[i+1]); |
| 13417 | } |
| 13418 | #endif |
| 13419 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13420 | oldsax = ctxt->sax; |
| 13421 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13422 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13423 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 13424 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13425 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 13426 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13427 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13428 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13429 | if (newDoc == NULL) { |
| 13430 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13431 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13432 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13433 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13434 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13435 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13436 | newDoc->dict = ctxt->dict; |
| 13437 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13438 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13439 | } else { |
| 13440 | ctxt->myDoc = oldctxt->myDoc; |
| 13441 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13442 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13443 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13444 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13445 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13446 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13447 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13448 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13449 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13450 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13451 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13452 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13453 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13454 | ctxt->myDoc->children = NULL; |
| 13455 | ctxt->myDoc->last = NULL; |
| 13456 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13457 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13458 | ctxt->instate = XML_PARSER_CONTENT; |
| 13459 | ctxt->depth = oldctxt->depth + 1; |
| 13460 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13461 | ctxt->validate = 0; |
| 13462 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 13463 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 13464 | /* |
| 13465 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13466 | */ |
| 13467 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13468 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13469 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13470 | ctxt->attsDefault = oldctxt->attsDefault; |
| 13471 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13472 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13473 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13474 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13475 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13476 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13477 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13478 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13479 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13480 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13481 | } |
| 13482 | |
| 13483 | if (!ctxt->wellFormed) { |
| 13484 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13485 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13486 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13487 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13488 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13489 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13490 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13491 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13492 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13493 | xmlNodePtr cur; |
| 13494 | |
| 13495 | /* |
| 13496 | * Return the newly created nodeset after unlinking it from |
| 13497 | * they pseudo parent. |
| 13498 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13499 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13500 | *lst = cur; |
| 13501 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13502 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 13503 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 13504 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 13505 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 13506 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 13507 | oldctxt->myDoc, cur); |
| 13508 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13509 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13510 | cur->parent = NULL; |
| 13511 | cur = cur->next; |
| 13512 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13513 | ctxt->myDoc->children->children = NULL; |
| 13514 | } |
| 13515 | if (ctxt->myDoc != NULL) { |
| 13516 | xmlFreeNode(ctxt->myDoc->children); |
| 13517 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13518 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13519 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13520 | |
| 13521 | /* |
| 13522 | * Record in the parent context the number of entities replacement |
| 13523 | * done when parsing that reference. |
| 13524 | */ |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 13525 | if (oldctxt != NULL) |
| 13526 | oldctxt->nbentities += ctxt->nbentities; |
| 13527 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13528 | /* |
| 13529 | * Also record the last error if any |
| 13530 | */ |
| 13531 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13532 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13533 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13534 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13535 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13536 | ctxt->attsDefault = NULL; |
| 13537 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13538 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13539 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13540 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13541 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13542 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13543 | return(ret); |
| 13544 | } |
| 13545 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13546 | /** |
| 13547 | * xmlParseInNodeContext: |
| 13548 | * @node: the context node |
| 13549 | * @data: the input string |
| 13550 | * @datalen: the input string length in bytes |
| 13551 | * @options: a combination of xmlParserOption |
| 13552 | * @lst: the return value for the set of parsed nodes |
| 13553 | * |
| 13554 | * Parse a well-balanced chunk of an XML document |
| 13555 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 13556 | * |
| 13557 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 13558 | * the content production in the XML grammar: |
| 13559 | * |
| 13560 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13561 | * |
| 13562 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13563 | * error code otherwise |
| 13564 | */ |
| 13565 | xmlParserErrors |
| 13566 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 13567 | int options, xmlNodePtr *lst) { |
| 13568 | #ifdef SAX2 |
| 13569 | xmlParserCtxtPtr ctxt; |
| 13570 | xmlDocPtr doc = NULL; |
| 13571 | xmlNodePtr fake, cur; |
| 13572 | int nsnr = 0; |
| 13573 | |
| 13574 | xmlParserErrors ret = XML_ERR_OK; |
| 13575 | |
| 13576 | /* |
| 13577 | * check all input parameters, grab the document |
| 13578 | */ |
| 13579 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 13580 | return(XML_ERR_INTERNAL_ERROR); |
| 13581 | switch (node->type) { |
| 13582 | case XML_ELEMENT_NODE: |
| 13583 | case XML_ATTRIBUTE_NODE: |
| 13584 | case XML_TEXT_NODE: |
| 13585 | case XML_CDATA_SECTION_NODE: |
| 13586 | case XML_ENTITY_REF_NODE: |
| 13587 | case XML_PI_NODE: |
| 13588 | case XML_COMMENT_NODE: |
| 13589 | case XML_DOCUMENT_NODE: |
| 13590 | case XML_HTML_DOCUMENT_NODE: |
| 13591 | break; |
| 13592 | default: |
| 13593 | return(XML_ERR_INTERNAL_ERROR); |
| 13594 | |
| 13595 | } |
| 13596 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 13597 | (node->type != XML_DOCUMENT_NODE) && |
| 13598 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 13599 | node = node->parent; |
| 13600 | if (node == NULL) |
| 13601 | return(XML_ERR_INTERNAL_ERROR); |
| 13602 | if (node->type == XML_ELEMENT_NODE) |
| 13603 | doc = node->doc; |
| 13604 | else |
| 13605 | doc = (xmlDocPtr) node; |
| 13606 | if (doc == NULL) |
| 13607 | return(XML_ERR_INTERNAL_ERROR); |
| 13608 | |
| 13609 | /* |
| 13610 | * allocate a context and set-up everything not related to the |
| 13611 | * node position in the tree |
| 13612 | */ |
| 13613 | if (doc->type == XML_DOCUMENT_NODE) |
| 13614 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 13615 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13616 | else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13617 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13618 | /* |
| 13619 | * When parsing in context, it makes no sense to add implied |
| 13620 | * elements like html/body/etc... |
| 13621 | */ |
| 13622 | options |= HTML_PARSE_NOIMPLIED; |
| 13623 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13624 | #endif |
| 13625 | else |
| 13626 | return(XML_ERR_INTERNAL_ERROR); |
| 13627 | |
| 13628 | if (ctxt == NULL) |
| 13629 | return(XML_ERR_NO_MEMORY); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13630 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13631 | /* |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13632 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 13633 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 13634 | * we must wait until the last moment to free the original one. |
| 13635 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13636 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13637 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13638 | xmlDictFree(ctxt->dict); |
| 13639 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13640 | } else |
| 13641 | options |= XML_PARSE_NODICT; |
| 13642 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13643 | if (doc->encoding != NULL) { |
| 13644 | xmlCharEncodingHandlerPtr hdlr; |
| 13645 | |
| 13646 | if (ctxt->encoding != NULL) |
| 13647 | xmlFree((xmlChar *) ctxt->encoding); |
| 13648 | ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); |
| 13649 | |
Nikolay Sivov | d4a5d98 | 2013-04-30 17:45:36 +0400 | [diff] [blame] | 13650 | hdlr = xmlFindCharEncodingHandler((const char *) doc->encoding); |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13651 | if (hdlr != NULL) { |
| 13652 | xmlSwitchToEncoding(ctxt, hdlr); |
| 13653 | } else { |
| 13654 | return(XML_ERR_UNSUPPORTED_ENCODING); |
| 13655 | } |
| 13656 | } |
| 13657 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13658 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13659 | xmlDetectSAX2(ctxt); |
| 13660 | ctxt->myDoc = doc; |
Daniel Veillard | 6faa126 | 2014-03-21 17:05:51 +0800 | [diff] [blame] | 13661 | /* parsing in context, i.e. as within existing content */ |
| 13662 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13663 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13664 | fake = xmlNewComment(NULL); |
| 13665 | if (fake == NULL) { |
| 13666 | xmlFreeParserCtxt(ctxt); |
| 13667 | return(XML_ERR_NO_MEMORY); |
| 13668 | } |
| 13669 | xmlAddChild(node, fake); |
| 13670 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13671 | if (node->type == XML_ELEMENT_NODE) { |
| 13672 | nodePush(ctxt, node); |
| 13673 | /* |
| 13674 | * initialize the SAX2 namespaces stack |
| 13675 | */ |
| 13676 | cur = node; |
| 13677 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 13678 | xmlNsPtr ns = cur->nsDef; |
| 13679 | const xmlChar *iprefix, *ihref; |
| 13680 | |
| 13681 | while (ns != NULL) { |
| 13682 | if (ctxt->dict) { |
| 13683 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 13684 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 13685 | } else { |
| 13686 | iprefix = ns->prefix; |
| 13687 | ihref = ns->href; |
| 13688 | } |
| 13689 | |
| 13690 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 13691 | nsPush(ctxt, iprefix, ihref); |
| 13692 | nsnr++; |
| 13693 | } |
| 13694 | ns = ns->next; |
| 13695 | } |
| 13696 | cur = cur->parent; |
| 13697 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13698 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13699 | |
| 13700 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 13701 | /* |
| 13702 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13703 | */ |
| 13704 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13705 | } |
| 13706 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 13707 | #ifdef LIBXML_HTML_ENABLED |
| 13708 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 13709 | __htmlParseContent(ctxt); |
| 13710 | else |
| 13711 | #endif |
| 13712 | xmlParseContent(ctxt); |
| 13713 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13714 | nsPop(ctxt, nsnr); |
| 13715 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 13716 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13717 | } else if (RAW != 0) { |
| 13718 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 13719 | } |
| 13720 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 13721 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13722 | ctxt->wellFormed = 0; |
| 13723 | } |
| 13724 | |
| 13725 | if (!ctxt->wellFormed) { |
| 13726 | if (ctxt->errNo == 0) |
| 13727 | ret = XML_ERR_INTERNAL_ERROR; |
| 13728 | else |
| 13729 | ret = (xmlParserErrors)ctxt->errNo; |
| 13730 | } else { |
| 13731 | ret = XML_ERR_OK; |
| 13732 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13733 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13734 | /* |
| 13735 | * Return the newly created nodeset after unlinking it from |
| 13736 | * the pseudo sibling. |
| 13737 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13738 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13739 | cur = fake->next; |
| 13740 | fake->next = NULL; |
| 13741 | node->last = fake; |
| 13742 | |
| 13743 | if (cur != NULL) { |
| 13744 | cur->prev = NULL; |
| 13745 | } |
| 13746 | |
| 13747 | *lst = cur; |
| 13748 | |
| 13749 | while (cur != NULL) { |
| 13750 | cur->parent = NULL; |
| 13751 | cur = cur->next; |
| 13752 | } |
| 13753 | |
| 13754 | xmlUnlinkNode(fake); |
| 13755 | xmlFreeNode(fake); |
| 13756 | |
| 13757 | |
| 13758 | if (ret != XML_ERR_OK) { |
| 13759 | xmlFreeNodeList(*lst); |
| 13760 | *lst = NULL; |
| 13761 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13762 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 13763 | if (doc->dict != NULL) |
| 13764 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13765 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13766 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13767 | return(ret); |
| 13768 | #else /* !SAX2 */ |
| 13769 | return(XML_ERR_INTERNAL_ERROR); |
| 13770 | #endif |
| 13771 | } |
| 13772 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13773 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13774 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13775 | * xmlParseBalancedChunkMemoryRecover: |
| 13776 | * @doc: the document the chunk pertains to |
| 13777 | * @sax: the SAX handler bloc (possibly NULL) |
| 13778 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13779 | * @depth: Used for loop detection, use 0 |
| 13780 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13781 | * @lst: the return value for the set of parsed nodes |
| 13782 | * @recover: return nodes even if the data is broken (use 0) |
| 13783 | * |
| 13784 | * |
| 13785 | * Parse a well-balanced chunk of an XML document |
| 13786 | * called by the parser |
| 13787 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13788 | * the content production in the XML grammar: |
| 13789 | * |
| 13790 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13791 | * |
| 13792 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13793 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13794 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13795 | * 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] | 13796 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 13797 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13798 | */ |
| 13799 | int |
| 13800 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13801 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13802 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13803 | xmlParserCtxtPtr ctxt; |
| 13804 | xmlDocPtr newDoc; |
| 13805 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13806 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13807 | int size; |
| 13808 | int ret = 0; |
| 13809 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13810 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13811 | return(XML_ERR_ENTITY_LOOP); |
| 13812 | } |
| 13813 | |
| 13814 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13815 | if (lst != NULL) |
| 13816 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13817 | if (string == NULL) |
| 13818 | return(-1); |
| 13819 | |
| 13820 | size = xmlStrlen(string); |
| 13821 | |
| 13822 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 13823 | if (ctxt == NULL) return(-1); |
| 13824 | ctxt->userData = ctxt; |
| 13825 | if (sax != NULL) { |
| 13826 | oldsax = ctxt->sax; |
| 13827 | ctxt->sax = sax; |
| 13828 | if (user_data != NULL) |
| 13829 | ctxt->userData = user_data; |
| 13830 | } |
| 13831 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13832 | if (newDoc == NULL) { |
| 13833 | xmlFreeParserCtxt(ctxt); |
| 13834 | return(-1); |
| 13835 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13836 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13837 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 13838 | xmlDictFree(ctxt->dict); |
| 13839 | ctxt->dict = doc->dict; |
| 13840 | xmlDictReference(ctxt->dict); |
| 13841 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13842 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13843 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 13844 | ctxt->dictNames = 1; |
| 13845 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13846 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13847 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13848 | if (doc != NULL) { |
| 13849 | newDoc->intSubset = doc->intSubset; |
| 13850 | newDoc->extSubset = doc->extSubset; |
| 13851 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13852 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13853 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13854 | if (sax != NULL) |
| 13855 | ctxt->sax = oldsax; |
| 13856 | xmlFreeParserCtxt(ctxt); |
| 13857 | newDoc->intSubset = NULL; |
| 13858 | newDoc->extSubset = NULL; |
| 13859 | xmlFreeDoc(newDoc); |
| 13860 | return(-1); |
| 13861 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13862 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 13863 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13864 | if (doc == NULL) { |
| 13865 | ctxt->myDoc = newDoc; |
| 13866 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 13867 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13868 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13869 | /* Ensure that doc has XML spec namespace */ |
| 13870 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 13871 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13872 | } |
| 13873 | ctxt->instate = XML_PARSER_CONTENT; |
| 13874 | ctxt->depth = depth; |
| 13875 | |
| 13876 | /* |
| 13877 | * Doing validity checking on chunk doesn't make sense |
| 13878 | */ |
| 13879 | ctxt->validate = 0; |
| 13880 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13881 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13882 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 13883 | if ( doc != NULL ){ |
| 13884 | content = doc->children; |
| 13885 | doc->children = NULL; |
| 13886 | xmlParseContent(ctxt); |
| 13887 | doc->children = content; |
| 13888 | } |
| 13889 | else { |
| 13890 | xmlParseContent(ctxt); |
| 13891 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13892 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13893 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13894 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13895 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13896 | } |
| 13897 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13898 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13899 | } |
| 13900 | |
| 13901 | if (!ctxt->wellFormed) { |
| 13902 | if (ctxt->errNo == 0) |
| 13903 | ret = 1; |
| 13904 | else |
| 13905 | ret = ctxt->errNo; |
| 13906 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13907 | ret = 0; |
| 13908 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13909 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13910 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 13911 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13912 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13913 | /* |
| 13914 | * Return the newly created nodeset after unlinking it from |
| 13915 | * they pseudo parent. |
| 13916 | */ |
| 13917 | cur = newDoc->children->children; |
| 13918 | *lst = cur; |
| 13919 | while (cur != NULL) { |
| 13920 | xmlSetTreeDoc(cur, doc); |
| 13921 | cur->parent = NULL; |
| 13922 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13923 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13924 | newDoc->children->children = NULL; |
| 13925 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13926 | |
| 13927 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13928 | ctxt->sax = oldsax; |
| 13929 | xmlFreeParserCtxt(ctxt); |
| 13930 | newDoc->intSubset = NULL; |
| 13931 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13932 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13933 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13934 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13935 | return(ret); |
| 13936 | } |
| 13937 | |
| 13938 | /** |
| 13939 | * xmlSAXParseEntity: |
| 13940 | * @sax: the SAX handler block |
| 13941 | * @filename: the filename |
| 13942 | * |
| 13943 | * parse an XML external entity out of context and build a tree. |
| 13944 | * It use the given SAX function block to handle the parsing callback. |
| 13945 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13946 | * |
| 13947 | * [78] extParsedEnt ::= TextDecl? content |
| 13948 | * |
| 13949 | * This correspond to a "Well Balanced" chunk |
| 13950 | * |
| 13951 | * Returns the resulting document tree |
| 13952 | */ |
| 13953 | |
| 13954 | xmlDocPtr |
| 13955 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13956 | xmlDocPtr ret; |
| 13957 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13958 | |
| 13959 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13960 | if (ctxt == NULL) { |
| 13961 | return(NULL); |
| 13962 | } |
| 13963 | if (sax != NULL) { |
| 13964 | if (ctxt->sax != NULL) |
| 13965 | xmlFree(ctxt->sax); |
| 13966 | ctxt->sax = sax; |
| 13967 | ctxt->userData = NULL; |
| 13968 | } |
| 13969 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13970 | xmlParseExtParsedEnt(ctxt); |
| 13971 | |
| 13972 | if (ctxt->wellFormed) |
| 13973 | ret = ctxt->myDoc; |
| 13974 | else { |
| 13975 | ret = NULL; |
| 13976 | xmlFreeDoc(ctxt->myDoc); |
| 13977 | ctxt->myDoc = NULL; |
| 13978 | } |
| 13979 | if (sax != NULL) |
| 13980 | ctxt->sax = NULL; |
| 13981 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13982 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13983 | return(ret); |
| 13984 | } |
| 13985 | |
| 13986 | /** |
| 13987 | * xmlParseEntity: |
| 13988 | * @filename: the filename |
| 13989 | * |
| 13990 | * parse an XML external entity out of context and build a tree. |
| 13991 | * |
| 13992 | * [78] extParsedEnt ::= TextDecl? content |
| 13993 | * |
| 13994 | * This correspond to a "Well Balanced" chunk |
| 13995 | * |
| 13996 | * Returns the resulting document tree |
| 13997 | */ |
| 13998 | |
| 13999 | xmlDocPtr |
| 14000 | xmlParseEntity(const char *filename) { |
| 14001 | return(xmlSAXParseEntity(NULL, filename)); |
| 14002 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14003 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14004 | |
| 14005 | /** |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14006 | * xmlCreateEntityParserCtxtInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14007 | * @URL: the entity URL |
| 14008 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 14009 | * @base: a possible base for the target URI |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14010 | * @pctx: parser context used to set options on new context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14011 | * |
| 14012 | * Create a parser context for an external entity |
| 14013 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14014 | * by default if found at compile-time. |
| 14015 | * |
| 14016 | * Returns the new parser context or NULL |
| 14017 | */ |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14018 | static xmlParserCtxtPtr |
| 14019 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 14020 | const xmlChar *base, xmlParserCtxtPtr pctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14021 | xmlParserCtxtPtr ctxt; |
| 14022 | xmlParserInputPtr inputStream; |
| 14023 | char *directory = NULL; |
| 14024 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14025 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14026 | ctxt = xmlNewParserCtxt(); |
| 14027 | if (ctxt == NULL) { |
| 14028 | return(NULL); |
| 14029 | } |
| 14030 | |
Daniel Veillard | 48247b4 | 2009-07-10 16:12:46 +0200 | [diff] [blame] | 14031 | if (pctx != NULL) { |
| 14032 | ctxt->options = pctx->options; |
| 14033 | ctxt->_private = pctx->_private; |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14034 | } |
| 14035 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14036 | uri = xmlBuildURI(URL, base); |
| 14037 | |
| 14038 | if (uri == NULL) { |
| 14039 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 14040 | if (inputStream == NULL) { |
| 14041 | xmlFreeParserCtxt(ctxt); |
| 14042 | return(NULL); |
| 14043 | } |
| 14044 | |
| 14045 | inputPush(ctxt, inputStream); |
| 14046 | |
| 14047 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14048 | directory = xmlParserGetDirectory((char *)URL); |
| 14049 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14050 | ctxt->directory = directory; |
| 14051 | } else { |
| 14052 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 14053 | if (inputStream == NULL) { |
| 14054 | xmlFree(uri); |
| 14055 | xmlFreeParserCtxt(ctxt); |
| 14056 | return(NULL); |
| 14057 | } |
| 14058 | |
| 14059 | inputPush(ctxt, inputStream); |
| 14060 | |
| 14061 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 14062 | directory = xmlParserGetDirectory((char *)uri); |
| 14063 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14064 | ctxt->directory = directory; |
| 14065 | xmlFree(uri); |
| 14066 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14067 | return(ctxt); |
| 14068 | } |
| 14069 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 14070 | /** |
| 14071 | * xmlCreateEntityParserCtxt: |
| 14072 | * @URL: the entity URL |
| 14073 | * @ID: the entity PUBLIC ID |
| 14074 | * @base: a possible base for the target URI |
| 14075 | * |
| 14076 | * Create a parser context for an external entity |
| 14077 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14078 | * by default if found at compile-time. |
| 14079 | * |
| 14080 | * Returns the new parser context or NULL |
| 14081 | */ |
| 14082 | xmlParserCtxtPtr |
| 14083 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 14084 | const xmlChar *base) { |
| 14085 | return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); |
| 14086 | |
| 14087 | } |
| 14088 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14089 | /************************************************************************ |
| 14090 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14091 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14092 | * * |
| 14093 | ************************************************************************/ |
| 14094 | |
| 14095 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14096 | * xmlCreateURLParserCtxt: |
| 14097 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14098 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14099 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14100 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14101 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14102 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14103 | * |
| 14104 | * Returns the new parser context or NULL |
| 14105 | */ |
| 14106 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14107 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14108 | { |
| 14109 | xmlParserCtxtPtr ctxt; |
| 14110 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14111 | char *directory = NULL; |
| 14112 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14113 | ctxt = xmlNewParserCtxt(); |
| 14114 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14115 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14116 | return(NULL); |
| 14117 | } |
| 14118 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14119 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14120 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 14121 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14122 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 14123 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14124 | if (inputStream == NULL) { |
| 14125 | xmlFreeParserCtxt(ctxt); |
| 14126 | return(NULL); |
| 14127 | } |
| 14128 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14129 | inputPush(ctxt, inputStream); |
| 14130 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 14131 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14132 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 14133 | ctxt->directory = directory; |
| 14134 | |
| 14135 | return(ctxt); |
| 14136 | } |
| 14137 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14138 | /** |
| 14139 | * xmlCreateFileParserCtxt: |
| 14140 | * @filename: the filename |
| 14141 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14142 | * Create a parser context for a file content. |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14143 | * Automatic support for ZLIB/Compress compressed document is provided |
| 14144 | * by default if found at compile-time. |
| 14145 | * |
| 14146 | * Returns the new parser context or NULL |
| 14147 | */ |
| 14148 | xmlParserCtxtPtr |
| 14149 | xmlCreateFileParserCtxt(const char *filename) |
| 14150 | { |
| 14151 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 14152 | } |
| 14153 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14154 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14155 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14156 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14157 | * @sax: the SAX handler block |
| 14158 | * @filename: the filename |
| 14159 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14160 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14161 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14162 | * |
| 14163 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14164 | * compressed document is provided by default if found at compile-time. |
| 14165 | * It use the given SAX function block to handle the parsing callback. |
| 14166 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14167 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 14168 | * User data (void *) is stored within the parser context in the |
| 14169 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14170 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14171 | * Returns the resulting document tree |
| 14172 | */ |
| 14173 | |
| 14174 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14175 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 14176 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14177 | xmlDocPtr ret; |
| 14178 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14179 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 14180 | xmlInitParser(); |
| 14181 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14182 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14183 | if (ctxt == NULL) { |
| 14184 | return(NULL); |
| 14185 | } |
| 14186 | if (sax != NULL) { |
| 14187 | if (ctxt->sax != NULL) |
| 14188 | xmlFree(ctxt->sax); |
| 14189 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14190 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14191 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14192 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 14193 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14195 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 14196 | if (ctxt->directory == NULL) |
| 14197 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14198 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 14199 | ctxt->recovery = recovery; |
| 14200 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14201 | xmlParseDocument(ctxt); |
| 14202 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14203 | if ((ctxt->wellFormed) || recovery) { |
| 14204 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 14205 | if (ret != NULL) { |
| 14206 | if (ctxt->input->buf->compressed > 0) |
| 14207 | ret->compression = 9; |
| 14208 | else |
| 14209 | ret->compression = ctxt->input->buf->compressed; |
| 14210 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14211 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14212 | else { |
| 14213 | ret = NULL; |
| 14214 | xmlFreeDoc(ctxt->myDoc); |
| 14215 | ctxt->myDoc = NULL; |
| 14216 | } |
| 14217 | if (sax != NULL) |
| 14218 | ctxt->sax = NULL; |
| 14219 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14220 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14221 | return(ret); |
| 14222 | } |
| 14223 | |
| 14224 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14225 | * xmlSAXParseFile: |
| 14226 | * @sax: the SAX handler block |
| 14227 | * @filename: the filename |
| 14228 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14229 | * documents |
| 14230 | * |
| 14231 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14232 | * compressed document is provided by default if found at compile-time. |
| 14233 | * It use the given SAX function block to handle the parsing callback. |
| 14234 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14235 | * |
| 14236 | * Returns the resulting document tree |
| 14237 | */ |
| 14238 | |
| 14239 | xmlDocPtr |
| 14240 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 14241 | int recovery) { |
| 14242 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 14243 | } |
| 14244 | |
| 14245 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14246 | * xmlRecoverDoc: |
| 14247 | * @cur: a pointer to an array of xmlChar |
| 14248 | * |
| 14249 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14250 | * In the case the document is not Well Formed, a attempt to build a |
| 14251 | * tree is tried anyway |
| 14252 | * |
| 14253 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14254 | */ |
| 14255 | |
| 14256 | xmlDocPtr |
Daniel Veillard | f39eafa | 2009-08-20 19:15:08 +0200 | [diff] [blame] | 14257 | xmlRecoverDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14258 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 14259 | } |
| 14260 | |
| 14261 | /** |
| 14262 | * xmlParseFile: |
| 14263 | * @filename: the filename |
| 14264 | * |
| 14265 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14266 | * compressed document is provided by default if found at compile-time. |
| 14267 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 14268 | * Returns the resulting document tree if the file was wellformed, |
| 14269 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14270 | */ |
| 14271 | |
| 14272 | xmlDocPtr |
| 14273 | xmlParseFile(const char *filename) { |
| 14274 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 14275 | } |
| 14276 | |
| 14277 | /** |
| 14278 | * xmlRecoverFile: |
| 14279 | * @filename: the filename |
| 14280 | * |
| 14281 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14282 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14283 | * In the case the document is not Well Formed, it attempts to build |
| 14284 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14285 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14286 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14287 | */ |
| 14288 | |
| 14289 | xmlDocPtr |
| 14290 | xmlRecoverFile(const char *filename) { |
| 14291 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 14292 | } |
| 14293 | |
| 14294 | |
| 14295 | /** |
| 14296 | * xmlSetupParserForBuffer: |
| 14297 | * @ctxt: an XML parser context |
| 14298 | * @buffer: a xmlChar * buffer |
| 14299 | * @filename: a file name |
| 14300 | * |
| 14301 | * Setup the parser context to parse a new buffer; Clears any prior |
| 14302 | * contents from the parser context. The buffer parameter must not be |
| 14303 | * NULL, but the filename parameter can be |
| 14304 | */ |
| 14305 | void |
| 14306 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 14307 | const char* filename) |
| 14308 | { |
| 14309 | xmlParserInputPtr input; |
| 14310 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14311 | if ((ctxt == NULL) || (buffer == NULL)) |
| 14312 | return; |
| 14313 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14314 | input = xmlNewInputStream(ctxt); |
| 14315 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 14316 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14317 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14318 | return; |
| 14319 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14320 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14321 | xmlClearParserCtxt(ctxt); |
| 14322 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 14323 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14324 | input->base = buffer; |
| 14325 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 14326 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14327 | inputPush(ctxt, input); |
| 14328 | } |
| 14329 | |
| 14330 | /** |
| 14331 | * xmlSAXUserParseFile: |
| 14332 | * @sax: a SAX handler |
| 14333 | * @user_data: The user data returned on SAX callbacks |
| 14334 | * @filename: a file name |
| 14335 | * |
| 14336 | * parse an XML file and call the given SAX handler routines. |
| 14337 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14338 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14339 | * Returns 0 in case of success or a error number otherwise |
| 14340 | */ |
| 14341 | int |
| 14342 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 14343 | const char *filename) { |
| 14344 | int ret = 0; |
| 14345 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14346 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14347 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14348 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 14349 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14350 | xmlFree(ctxt->sax); |
| 14351 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14352 | xmlDetectSAX2(ctxt); |
| 14353 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14354 | if (user_data != NULL) |
| 14355 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14356 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14357 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14358 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14359 | if (ctxt->wellFormed) |
| 14360 | ret = 0; |
| 14361 | else { |
| 14362 | if (ctxt->errNo != 0) |
| 14363 | ret = ctxt->errNo; |
| 14364 | else |
| 14365 | ret = -1; |
| 14366 | } |
| 14367 | if (sax != NULL) |
| 14368 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14369 | if (ctxt->myDoc != NULL) { |
| 14370 | xmlFreeDoc(ctxt->myDoc); |
| 14371 | ctxt->myDoc = NULL; |
| 14372 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14373 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14374 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14375 | return ret; |
| 14376 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14377 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14378 | |
| 14379 | /************************************************************************ |
| 14380 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14381 | * Front ends when parsing from memory * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14382 | * * |
| 14383 | ************************************************************************/ |
| 14384 | |
| 14385 | /** |
| 14386 | * xmlCreateMemoryParserCtxt: |
| 14387 | * @buffer: a pointer to a char array |
| 14388 | * @size: the size of the array |
| 14389 | * |
| 14390 | * Create a parser context for an XML in-memory document. |
| 14391 | * |
| 14392 | * Returns the new parser context or NULL |
| 14393 | */ |
| 14394 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14395 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14396 | xmlParserCtxtPtr ctxt; |
| 14397 | xmlParserInputPtr input; |
| 14398 | xmlParserInputBufferPtr buf; |
| 14399 | |
| 14400 | if (buffer == NULL) |
| 14401 | return(NULL); |
| 14402 | if (size <= 0) |
| 14403 | return(NULL); |
| 14404 | |
| 14405 | ctxt = xmlNewParserCtxt(); |
| 14406 | if (ctxt == NULL) |
| 14407 | return(NULL); |
| 14408 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 14409 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14410 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14411 | if (buf == NULL) { |
| 14412 | xmlFreeParserCtxt(ctxt); |
| 14413 | return(NULL); |
| 14414 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14415 | |
| 14416 | input = xmlNewInputStream(ctxt); |
| 14417 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14418 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14419 | xmlFreeParserCtxt(ctxt); |
| 14420 | return(NULL); |
| 14421 | } |
| 14422 | |
| 14423 | input->filename = NULL; |
| 14424 | input->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14425 | xmlBufResetInput(input->buf->buffer, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14426 | |
| 14427 | inputPush(ctxt, input); |
| 14428 | return(ctxt); |
| 14429 | } |
| 14430 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14431 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14432 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14433 | * xmlSAXParseMemoryWithData: |
| 14434 | * @sax: the SAX handler block |
| 14435 | * @buffer: an pointer to a char array |
| 14436 | * @size: the size of the array |
| 14437 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14438 | * documents |
| 14439 | * @data: the userdata |
| 14440 | * |
| 14441 | * parse an XML in-memory block and use the given SAX function block |
| 14442 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14443 | * DOM tree building routines. |
| 14444 | * |
| 14445 | * User data (void *) is stored within the parser context in the |
| 14446 | * context's _private member, so it is available nearly everywhere in libxml |
| 14447 | * |
| 14448 | * Returns the resulting document tree |
| 14449 | */ |
| 14450 | |
| 14451 | xmlDocPtr |
| 14452 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 14453 | int size, int recovery, void *data) { |
| 14454 | xmlDocPtr ret; |
| 14455 | xmlParserCtxtPtr ctxt; |
| 14456 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14457 | xmlInitParser(); |
| 14458 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14459 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14460 | if (ctxt == NULL) return(NULL); |
| 14461 | if (sax != NULL) { |
| 14462 | if (ctxt->sax != NULL) |
| 14463 | xmlFree(ctxt->sax); |
| 14464 | ctxt->sax = sax; |
| 14465 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14466 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14467 | if (data!=NULL) { |
| 14468 | ctxt->_private=data; |
| 14469 | } |
| 14470 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 14471 | ctxt->recovery = recovery; |
| 14472 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14473 | xmlParseDocument(ctxt); |
| 14474 | |
| 14475 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14476 | else { |
| 14477 | ret = NULL; |
| 14478 | xmlFreeDoc(ctxt->myDoc); |
| 14479 | ctxt->myDoc = NULL; |
| 14480 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14481 | if (sax != NULL) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14482 | ctxt->sax = NULL; |
| 14483 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14484 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14485 | return(ret); |
| 14486 | } |
| 14487 | |
| 14488 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14489 | * xmlSAXParseMemory: |
| 14490 | * @sax: the SAX handler block |
| 14491 | * @buffer: an pointer to a char array |
| 14492 | * @size: the size of the array |
| 14493 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 14494 | * documents |
| 14495 | * |
| 14496 | * parse an XML in-memory block and use the given SAX function block |
| 14497 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14498 | * DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14499 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14500 | * Returns the resulting document tree |
| 14501 | */ |
| 14502 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14503 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 14504 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14505 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14506 | } |
| 14507 | |
| 14508 | /** |
| 14509 | * xmlParseMemory: |
| 14510 | * @buffer: an pointer to a char array |
| 14511 | * @size: the size of the array |
| 14512 | * |
| 14513 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14514 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14515 | * Returns the resulting document tree |
| 14516 | */ |
| 14517 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14518 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14519 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 14520 | } |
| 14521 | |
| 14522 | /** |
| 14523 | * xmlRecoverMemory: |
| 14524 | * @buffer: an pointer to a char array |
| 14525 | * @size: the size of the array |
| 14526 | * |
| 14527 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14528 | * In the case the document is not Well Formed, an attempt to |
| 14529 | * build a tree is tried anyway |
| 14530 | * |
| 14531 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14532 | */ |
| 14533 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14534 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14535 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 14536 | } |
| 14537 | |
| 14538 | /** |
| 14539 | * xmlSAXUserParseMemory: |
| 14540 | * @sax: a SAX handler |
| 14541 | * @user_data: The user data returned on SAX callbacks |
| 14542 | * @buffer: an in-memory XML document input |
| 14543 | * @size: the length of the XML document in bytes |
| 14544 | * |
| 14545 | * A better SAX parsing routine. |
| 14546 | * 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] | 14547 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14548 | * Returns 0 in case of success or a error number otherwise |
| 14549 | */ |
| 14550 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14551 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14552 | int ret = 0; |
| 14553 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14554 | |
| 14555 | xmlInitParser(); |
| 14556 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14557 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14558 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14559 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 14560 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 14561 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14562 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14563 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 14564 | if (user_data != NULL) |
| 14565 | ctxt->userData = user_data; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14566 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14567 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14568 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14569 | if (ctxt->wellFormed) |
| 14570 | ret = 0; |
| 14571 | else { |
| 14572 | if (ctxt->errNo != 0) |
| 14573 | ret = ctxt->errNo; |
| 14574 | else |
| 14575 | ret = -1; |
| 14576 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14577 | if (sax != NULL) |
| 14578 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14579 | if (ctxt->myDoc != NULL) { |
| 14580 | xmlFreeDoc(ctxt->myDoc); |
| 14581 | ctxt->myDoc = NULL; |
| 14582 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14583 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14584 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14585 | return ret; |
| 14586 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14587 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14588 | |
| 14589 | /** |
| 14590 | * xmlCreateDocParserCtxt: |
| 14591 | * @cur: a pointer to an array of xmlChar |
| 14592 | * |
| 14593 | * Creates a parser context for an XML in-memory document. |
| 14594 | * |
| 14595 | * Returns the new parser context or NULL |
| 14596 | */ |
| 14597 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14598 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14599 | int len; |
| 14600 | |
| 14601 | if (cur == NULL) |
| 14602 | return(NULL); |
| 14603 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14604 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14605 | } |
| 14606 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14607 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14608 | /** |
| 14609 | * xmlSAXParseDoc: |
| 14610 | * @sax: the SAX handler block |
| 14611 | * @cur: a pointer to an array of xmlChar |
| 14612 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14613 | * documents |
| 14614 | * |
| 14615 | * parse an XML in-memory document and build a tree. |
| 14616 | * It use the given SAX function block to handle the parsing callback. |
| 14617 | * If sax is NULL, fallback to the default DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14618 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14619 | * Returns the resulting document tree |
| 14620 | */ |
| 14621 | |
| 14622 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14623 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14624 | xmlDocPtr ret; |
| 14625 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14626 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14627 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14628 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14629 | |
| 14630 | |
| 14631 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14632 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14633 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14634 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14635 | ctxt->sax = sax; |
| 14636 | ctxt->userData = NULL; |
| 14637 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14638 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14639 | |
| 14640 | xmlParseDocument(ctxt); |
| 14641 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14642 | else { |
| 14643 | ret = NULL; |
| 14644 | xmlFreeDoc(ctxt->myDoc); |
| 14645 | ctxt->myDoc = NULL; |
| 14646 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14647 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14648 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14649 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14650 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14651 | return(ret); |
| 14652 | } |
| 14653 | |
| 14654 | /** |
| 14655 | * xmlParseDoc: |
| 14656 | * @cur: a pointer to an array of xmlChar |
| 14657 | * |
| 14658 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14659 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14660 | * Returns the resulting document tree |
| 14661 | */ |
| 14662 | |
| 14663 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14664 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14665 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 14666 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14667 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14668 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14669 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14670 | /************************************************************************ |
| 14671 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14672 | * Specific function to keep track of entities references * |
| 14673 | * and used by the XSLT debugger * |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14674 | * * |
| 14675 | ************************************************************************/ |
| 14676 | |
| 14677 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 14678 | |
| 14679 | /** |
| 14680 | * xmlAddEntityReference: |
| 14681 | * @ent : A valid entity |
| 14682 | * @firstNode : A valid first node for children of entity |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14683 | * @lastNode : A valid last node of children entity |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14684 | * |
| 14685 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 14686 | */ |
| 14687 | static void |
| 14688 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 14689 | xmlNodePtr lastNode) |
| 14690 | { |
| 14691 | if (xmlEntityRefFunc != NULL) { |
| 14692 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 14693 | } |
| 14694 | } |
| 14695 | |
| 14696 | |
| 14697 | /** |
| 14698 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 14699 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14700 | * |
| 14701 | * Set the function to call call back when a xml reference has been made |
| 14702 | */ |
| 14703 | void |
| 14704 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 14705 | { |
| 14706 | xmlEntityRefFunc = func; |
| 14707 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14708 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14709 | |
| 14710 | /************************************************************************ |
| 14711 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14712 | * Miscellaneous * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14713 | * * |
| 14714 | ************************************************************************/ |
| 14715 | |
| 14716 | #ifdef LIBXML_XPATH_ENABLED |
| 14717 | #include <libxml/xpath.h> |
| 14718 | #endif |
| 14719 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 14720 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14721 | static int xmlParserInitialized = 0; |
| 14722 | |
| 14723 | /** |
| 14724 | * xmlInitParser: |
| 14725 | * |
| 14726 | * Initialization function for the XML parser. |
| 14727 | * This is not reentrant. Call once before processing in case of |
| 14728 | * use in multithreaded programs. |
| 14729 | */ |
| 14730 | |
| 14731 | void |
| 14732 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 14733 | if (xmlParserInitialized != 0) |
| 14734 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14735 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14736 | #ifdef LIBXML_THREAD_ENABLED |
| 14737 | __xmlGlobalInitMutexLock(); |
| 14738 | if (xmlParserInitialized == 0) { |
| 14739 | #endif |
Daniel Veillard | 7dd7080 | 2009-06-04 11:08:39 +0200 | [diff] [blame] | 14740 | xmlInitThreads(); |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 14741 | xmlInitGlobals(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14742 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 14743 | (xmlGenericError == NULL)) |
| 14744 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14745 | xmlInitMemory(); |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 14746 | xmlInitializeDict(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14747 | xmlInitCharEncodingHandlers(); |
| 14748 | xmlDefaultSAXHandlerInit(); |
| 14749 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14750 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14751 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14752 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14753 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14754 | htmlInitAutoClose(); |
| 14755 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14756 | #endif |
| 14757 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14758 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14759 | #endif |
Daniel Veillard | 054c716 | 2014-01-26 15:02:25 +0100 | [diff] [blame] | 14760 | #ifdef LIBXML_CATALOG_ENABLED |
| 14761 | xmlInitializeCatalog(); |
| 14762 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14763 | xmlParserInitialized = 1; |
| 14764 | #ifdef LIBXML_THREAD_ENABLED |
| 14765 | } |
| 14766 | __xmlGlobalInitMutexUnlock(); |
| 14767 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14768 | } |
| 14769 | |
| 14770 | /** |
| 14771 | * xmlCleanupParser: |
| 14772 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 14773 | * This function name is somewhat misleading. It does not clean up |
| 14774 | * parser state, it cleans up memory allocated by the library itself. |
| 14775 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 14776 | * related global memory allocated for the library processing. |
| 14777 | * It doesn't deallocate any document related memory. One should |
| 14778 | * call xmlCleanupParser() only when the process has finished using |
| 14779 | * the library and all XML/HTML documents built with it. |
| 14780 | * See also xmlInitParser() which has the opposite function of preparing |
| 14781 | * the library for operations. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 14782 | * |
| 14783 | * WARNING: if your application is multithreaded or has plugin support |
| 14784 | * calling this may crash the application if another thread or |
| 14785 | * a plugin is still using libxml2. It's sometimes very hard to |
| 14786 | * guess if libxml2 is in use in the application, some libraries |
| 14787 | * or plugins may use it without notice. In case of doubt abstain |
| 14788 | * from calling this function or do it just before calling exit() |
| 14789 | * to avoid leak reports from valgrind ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14790 | */ |
| 14791 | |
| 14792 | void |
| 14793 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 14794 | if (!xmlParserInitialized) |
| 14795 | return; |
| 14796 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14797 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 14798 | #ifdef LIBXML_CATALOG_ENABLED |
| 14799 | xmlCatalogCleanup(); |
| 14800 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 14801 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 14802 | xmlCleanupInputCallbacks(); |
| 14803 | #ifdef LIBXML_OUTPUT_ENABLED |
| 14804 | xmlCleanupOutputCallbacks(); |
| 14805 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14806 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14807 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 14808 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14809 | #endif |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 14810 | xmlResetLastError(); |
Alexander Pastukhov | 704d8c5 | 2013-04-23 13:02:11 +0800 | [diff] [blame] | 14811 | xmlCleanupGlobals(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 14812 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14813 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 14814 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14815 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14816 | |
| 14817 | /************************************************************************ |
| 14818 | * * |
| 14819 | * New set (2.6.0) of simpler and more flexible APIs * |
| 14820 | * * |
| 14821 | ************************************************************************/ |
| 14822 | |
| 14823 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14824 | * DICT_FREE: |
| 14825 | * @str: a string |
| 14826 | * |
| 14827 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 14828 | * current scope |
| 14829 | */ |
| 14830 | #define DICT_FREE(str) \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14831 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14832 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 14833 | xmlFree((char *)(str)); |
| 14834 | |
| 14835 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14836 | * xmlCtxtReset: |
| 14837 | * @ctxt: an XML parser context |
| 14838 | * |
| 14839 | * Reset a parser context |
| 14840 | */ |
| 14841 | void |
| 14842 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 14843 | { |
| 14844 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14845 | xmlDictPtr dict; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14846 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14847 | if (ctxt == NULL) |
| 14848 | return; |
| 14849 | |
| 14850 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14851 | |
| 14852 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 14853 | xmlFreeInputStream(input); |
| 14854 | } |
| 14855 | ctxt->inputNr = 0; |
| 14856 | ctxt->input = NULL; |
| 14857 | |
| 14858 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 14859 | if (ctxt->spaceTab != NULL) { |
| 14860 | ctxt->spaceTab[0] = -1; |
| 14861 | ctxt->space = &ctxt->spaceTab[0]; |
| 14862 | } else { |
| 14863 | ctxt->space = NULL; |
| 14864 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14865 | |
| 14866 | |
| 14867 | ctxt->nodeNr = 0; |
| 14868 | ctxt->node = NULL; |
| 14869 | |
| 14870 | ctxt->nameNr = 0; |
| 14871 | ctxt->name = NULL; |
| 14872 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14873 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14874 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14875 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14876 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14877 | DICT_FREE(ctxt->directory); |
| 14878 | ctxt->directory = NULL; |
| 14879 | DICT_FREE(ctxt->extSubURI); |
| 14880 | ctxt->extSubURI = NULL; |
| 14881 | DICT_FREE(ctxt->extSubSystem); |
| 14882 | ctxt->extSubSystem = NULL; |
| 14883 | if (ctxt->myDoc != NULL) |
| 14884 | xmlFreeDoc(ctxt->myDoc); |
| 14885 | ctxt->myDoc = NULL; |
| 14886 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14887 | ctxt->standalone = -1; |
| 14888 | ctxt->hasExternalSubset = 0; |
| 14889 | ctxt->hasPErefs = 0; |
| 14890 | ctxt->html = 0; |
| 14891 | ctxt->external = 0; |
| 14892 | ctxt->instate = XML_PARSER_START; |
| 14893 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14894 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14895 | ctxt->wellFormed = 1; |
| 14896 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 14897 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14898 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14899 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14900 | ctxt->vctxt.userData = ctxt; |
| 14901 | ctxt->vctxt.error = xmlParserValidityError; |
| 14902 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14903 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14904 | ctxt->record_info = 0; |
| 14905 | ctxt->nbChars = 0; |
| 14906 | ctxt->checkIndex = 0; |
| 14907 | ctxt->inSubset = 0; |
| 14908 | ctxt->errNo = XML_ERR_OK; |
| 14909 | ctxt->depth = 0; |
| 14910 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 14911 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 14912 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14913 | ctxt->sizeentities = 0; |
Daniel Veillard | 23f05e0 | 2013-02-19 10:21:49 +0800 | [diff] [blame] | 14914 | ctxt->sizeentcopy = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14915 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 14916 | |
| 14917 | if (ctxt->attsDefault != NULL) { |
| 14918 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 14919 | ctxt->attsDefault = NULL; |
| 14920 | } |
| 14921 | if (ctxt->attsSpecial != NULL) { |
| 14922 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 14923 | ctxt->attsSpecial = NULL; |
| 14924 | } |
| 14925 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14926 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14927 | if (ctxt->catalogs != NULL) |
| 14928 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14929 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 14930 | if (ctxt->lastError.code != XML_ERR_OK) |
| 14931 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14932 | } |
| 14933 | |
| 14934 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14935 | * xmlCtxtResetPush: |
| 14936 | * @ctxt: an XML parser context |
| 14937 | * @chunk: a pointer to an array of chars |
| 14938 | * @size: number of chars in the array |
| 14939 | * @filename: an optional file name or URI |
| 14940 | * @encoding: the document encoding, or NULL |
| 14941 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14942 | * Reset a push parser context |
| 14943 | * |
| 14944 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14945 | */ |
| 14946 | int |
| 14947 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 14948 | int size, const char *filename, const char *encoding) |
| 14949 | { |
| 14950 | xmlParserInputPtr inputStream; |
| 14951 | xmlParserInputBufferPtr buf; |
| 14952 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 14953 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14954 | if (ctxt == NULL) |
| 14955 | return(1); |
| 14956 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14957 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 14958 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 14959 | |
| 14960 | buf = xmlAllocParserInputBuffer(enc); |
| 14961 | if (buf == NULL) |
| 14962 | return(1); |
| 14963 | |
| 14964 | if (ctxt == NULL) { |
| 14965 | xmlFreeParserInputBuffer(buf); |
| 14966 | return(1); |
| 14967 | } |
| 14968 | |
| 14969 | xmlCtxtReset(ctxt); |
| 14970 | |
| 14971 | if (ctxt->pushTab == NULL) { |
| 14972 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 14973 | sizeof(xmlChar *)); |
| 14974 | if (ctxt->pushTab == NULL) { |
| 14975 | xmlErrMemory(ctxt, NULL); |
| 14976 | xmlFreeParserInputBuffer(buf); |
| 14977 | return(1); |
| 14978 | } |
| 14979 | } |
| 14980 | |
| 14981 | if (filename == NULL) { |
| 14982 | ctxt->directory = NULL; |
| 14983 | } else { |
| 14984 | ctxt->directory = xmlParserGetDirectory(filename); |
| 14985 | } |
| 14986 | |
| 14987 | inputStream = xmlNewInputStream(ctxt); |
| 14988 | if (inputStream == NULL) { |
| 14989 | xmlFreeParserInputBuffer(buf); |
| 14990 | return(1); |
| 14991 | } |
| 14992 | |
| 14993 | if (filename == NULL) |
| 14994 | inputStream->filename = NULL; |
| 14995 | else |
| 14996 | inputStream->filename = (char *) |
| 14997 | xmlCanonicPath((const xmlChar *) filename); |
| 14998 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14999 | xmlBufResetInput(buf->buffer, inputStream); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15000 | |
| 15001 | inputPush(ctxt, inputStream); |
| 15002 | |
| 15003 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 15004 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15005 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 15006 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15007 | |
| 15008 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 15009 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 15010 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15011 | #ifdef DEBUG_PUSH |
| 15012 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 15013 | #endif |
| 15014 | } |
| 15015 | |
| 15016 | if (encoding != NULL) { |
| 15017 | xmlCharEncodingHandlerPtr hdlr; |
| 15018 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15019 | if (ctxt->encoding != NULL) |
| 15020 | xmlFree((xmlChar *) ctxt->encoding); |
| 15021 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15022 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15023 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15024 | if (hdlr != NULL) { |
| 15025 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15026 | } else { |
| 15027 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 15028 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 15029 | } |
| 15030 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 15031 | xmlSwitchEncoding(ctxt, enc); |
| 15032 | } |
| 15033 | |
| 15034 | return(0); |
| 15035 | } |
| 15036 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15037 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 15038 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15039 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15040 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15041 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15042 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15043 | * |
| 15044 | * Applies the options to the parser context |
| 15045 | * |
| 15046 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15047 | * in case of error. |
| 15048 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15049 | static int |
| 15050 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15051 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 15052 | if (ctxt == NULL) |
| 15053 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15054 | if (encoding != NULL) { |
| 15055 | if (ctxt->encoding != NULL) |
| 15056 | xmlFree((xmlChar *) ctxt->encoding); |
| 15057 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 15058 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15059 | if (options & XML_PARSE_RECOVER) { |
| 15060 | ctxt->recovery = 1; |
| 15061 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15062 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15063 | } else |
| 15064 | ctxt->recovery = 0; |
| 15065 | if (options & XML_PARSE_DTDLOAD) { |
| 15066 | ctxt->loadsubset = XML_DETECT_IDS; |
| 15067 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15068 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15069 | } else |
| 15070 | ctxt->loadsubset = 0; |
| 15071 | if (options & XML_PARSE_DTDATTR) { |
| 15072 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 15073 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15074 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15075 | } |
| 15076 | if (options & XML_PARSE_NOENT) { |
| 15077 | ctxt->replaceEntities = 1; |
| 15078 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 15079 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15080 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15081 | } else |
| 15082 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15083 | if (options & XML_PARSE_PEDANTIC) { |
| 15084 | ctxt->pedantic = 1; |
| 15085 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15086 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15087 | } else |
| 15088 | ctxt->pedantic = 0; |
| 15089 | if (options & XML_PARSE_NOBLANKS) { |
| 15090 | ctxt->keepBlanks = 0; |
| 15091 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 15092 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15093 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15094 | } else |
| 15095 | ctxt->keepBlanks = 1; |
| 15096 | if (options & XML_PARSE_DTDVALID) { |
| 15097 | ctxt->validate = 1; |
| 15098 | if (options & XML_PARSE_NOWARNING) |
| 15099 | ctxt->vctxt.warning = NULL; |
| 15100 | if (options & XML_PARSE_NOERROR) |
| 15101 | ctxt->vctxt.error = NULL; |
| 15102 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15103 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15104 | } else |
| 15105 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 15106 | if (options & XML_PARSE_NOWARNING) { |
| 15107 | ctxt->sax->warning = NULL; |
| 15108 | options -= XML_PARSE_NOWARNING; |
| 15109 | } |
| 15110 | if (options & XML_PARSE_NOERROR) { |
| 15111 | ctxt->sax->error = NULL; |
| 15112 | ctxt->sax->fatalError = NULL; |
| 15113 | options -= XML_PARSE_NOERROR; |
| 15114 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15115 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15116 | if (options & XML_PARSE_SAX1) { |
| 15117 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 15118 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 15119 | ctxt->sax->startElementNs = NULL; |
| 15120 | ctxt->sax->endElementNs = NULL; |
| 15121 | ctxt->sax->initialized = 1; |
| 15122 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15123 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15124 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 15125 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15126 | if (options & XML_PARSE_NODICT) { |
| 15127 | ctxt->dictNames = 0; |
| 15128 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15129 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15130 | } else { |
| 15131 | ctxt->dictNames = 1; |
| 15132 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15133 | if (options & XML_PARSE_NOCDATA) { |
| 15134 | ctxt->sax->cdataBlock = NULL; |
| 15135 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 15136 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 15137 | } |
| 15138 | if (options & XML_PARSE_NSCLEAN) { |
| 15139 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 15140 | options -= XML_PARSE_NSCLEAN; |
| 15141 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15142 | if (options & XML_PARSE_NONET) { |
| 15143 | ctxt->options |= XML_PARSE_NONET; |
| 15144 | options -= XML_PARSE_NONET; |
| 15145 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 15146 | if (options & XML_PARSE_COMPACT) { |
| 15147 | ctxt->options |= XML_PARSE_COMPACT; |
| 15148 | options -= XML_PARSE_COMPACT; |
| 15149 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 15150 | if (options & XML_PARSE_OLD10) { |
| 15151 | ctxt->options |= XML_PARSE_OLD10; |
| 15152 | options -= XML_PARSE_OLD10; |
| 15153 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15154 | if (options & XML_PARSE_NOBASEFIX) { |
| 15155 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 15156 | options -= XML_PARSE_NOBASEFIX; |
| 15157 | } |
| 15158 | if (options & XML_PARSE_HUGE) { |
| 15159 | ctxt->options |= XML_PARSE_HUGE; |
| 15160 | options -= XML_PARSE_HUGE; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 15161 | if (ctxt->dict != NULL) |
| 15162 | xmlDictSetLimit(ctxt->dict, 0); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15163 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 15164 | if (options & XML_PARSE_OLDSAX) { |
| 15165 | ctxt->options |= XML_PARSE_OLDSAX; |
| 15166 | options -= XML_PARSE_OLDSAX; |
| 15167 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 15168 | if (options & XML_PARSE_IGNORE_ENC) { |
| 15169 | ctxt->options |= XML_PARSE_IGNORE_ENC; |
| 15170 | options -= XML_PARSE_IGNORE_ENC; |
| 15171 | } |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 15172 | if (options & XML_PARSE_BIG_LINES) { |
| 15173 | ctxt->options |= XML_PARSE_BIG_LINES; |
| 15174 | options -= XML_PARSE_BIG_LINES; |
| 15175 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 15176 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15177 | return (options); |
| 15178 | } |
| 15179 | |
| 15180 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15181 | * xmlCtxtUseOptions: |
| 15182 | * @ctxt: an XML parser context |
| 15183 | * @options: a combination of xmlParserOption |
| 15184 | * |
| 15185 | * Applies the options to the parser context |
| 15186 | * |
| 15187 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15188 | * in case of error. |
| 15189 | */ |
| 15190 | int |
| 15191 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 15192 | { |
| 15193 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 15194 | } |
| 15195 | |
| 15196 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15197 | * xmlDoRead: |
| 15198 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15199 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15200 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15201 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15202 | * @reuse: keep the context for reuse |
| 15203 | * |
| 15204 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15205 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15206 | * Returns the resulting document tree or NULL |
| 15207 | */ |
| 15208 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15209 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 15210 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15211 | { |
| 15212 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15213 | |
| 15214 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15215 | if (encoding != NULL) { |
| 15216 | xmlCharEncodingHandlerPtr hdlr; |
| 15217 | |
| 15218 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15219 | if (hdlr != NULL) |
| 15220 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15221 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15222 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 15223 | (ctxt->input->filename == NULL)) |
| 15224 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15225 | xmlParseDocument(ctxt); |
| 15226 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 15227 | ret = ctxt->myDoc; |
| 15228 | else { |
| 15229 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15230 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15231 | xmlFreeDoc(ctxt->myDoc); |
| 15232 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15233 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15234 | ctxt->myDoc = NULL; |
| 15235 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15236 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15237 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15238 | |
| 15239 | return (ret); |
| 15240 | } |
| 15241 | |
| 15242 | /** |
| 15243 | * xmlReadDoc: |
| 15244 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15245 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15246 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15247 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15248 | * |
| 15249 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15250 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15251 | * Returns the resulting document tree |
| 15252 | */ |
| 15253 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15254 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15255 | { |
| 15256 | xmlParserCtxtPtr ctxt; |
| 15257 | |
| 15258 | if (cur == NULL) |
| 15259 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15260 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15261 | |
| 15262 | ctxt = xmlCreateDocParserCtxt(cur); |
| 15263 | if (ctxt == NULL) |
| 15264 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15265 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15266 | } |
| 15267 | |
| 15268 | /** |
| 15269 | * xmlReadFile: |
| 15270 | * @filename: a file or URL |
| 15271 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15272 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15273 | * |
| 15274 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15275 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15276 | * Returns the resulting document tree |
| 15277 | */ |
| 15278 | xmlDocPtr |
| 15279 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 15280 | { |
| 15281 | xmlParserCtxtPtr ctxt; |
| 15282 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15283 | xmlInitParser(); |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15284 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15285 | if (ctxt == NULL) |
| 15286 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15287 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15288 | } |
| 15289 | |
| 15290 | /** |
| 15291 | * xmlReadMemory: |
| 15292 | * @buffer: a pointer to a char array |
| 15293 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15294 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15295 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15296 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15297 | * |
| 15298 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15299 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15300 | * Returns the resulting document tree |
| 15301 | */ |
| 15302 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15303 | 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] | 15304 | { |
| 15305 | xmlParserCtxtPtr ctxt; |
| 15306 | |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15307 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15308 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 15309 | if (ctxt == NULL) |
| 15310 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15311 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15312 | } |
| 15313 | |
| 15314 | /** |
| 15315 | * xmlReadFd: |
| 15316 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15317 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15318 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15319 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15320 | * |
| 15321 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15322 | * NOTE that the file descriptor will not be closed when the |
| 15323 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15324 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15325 | * Returns the resulting document tree |
| 15326 | */ |
| 15327 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15328 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15329 | { |
| 15330 | xmlParserCtxtPtr ctxt; |
| 15331 | xmlParserInputBufferPtr input; |
| 15332 | xmlParserInputPtr stream; |
| 15333 | |
| 15334 | if (fd < 0) |
| 15335 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15336 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15337 | |
| 15338 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15339 | if (input == NULL) |
| 15340 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15341 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15342 | ctxt = xmlNewParserCtxt(); |
| 15343 | if (ctxt == NULL) { |
| 15344 | xmlFreeParserInputBuffer(input); |
| 15345 | return (NULL); |
| 15346 | } |
| 15347 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15348 | if (stream == NULL) { |
| 15349 | xmlFreeParserInputBuffer(input); |
| 15350 | xmlFreeParserCtxt(ctxt); |
| 15351 | return (NULL); |
| 15352 | } |
| 15353 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15354 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15355 | } |
| 15356 | |
| 15357 | /** |
| 15358 | * xmlReadIO: |
| 15359 | * @ioread: an I/O read function |
| 15360 | * @ioclose: an I/O close function |
| 15361 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15362 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15363 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15364 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15365 | * |
| 15366 | * 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] | 15367 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15368 | * Returns the resulting document tree |
| 15369 | */ |
| 15370 | xmlDocPtr |
| 15371 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15372 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15373 | { |
| 15374 | xmlParserCtxtPtr ctxt; |
| 15375 | xmlParserInputBufferPtr input; |
| 15376 | xmlParserInputPtr stream; |
| 15377 | |
| 15378 | if (ioread == NULL) |
| 15379 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15380 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15381 | |
| 15382 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15383 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15384 | if (input == NULL) { |
| 15385 | if (ioclose != NULL) |
| 15386 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15387 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15388 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15389 | ctxt = xmlNewParserCtxt(); |
| 15390 | if (ctxt == NULL) { |
| 15391 | xmlFreeParserInputBuffer(input); |
| 15392 | return (NULL); |
| 15393 | } |
| 15394 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15395 | if (stream == NULL) { |
| 15396 | xmlFreeParserInputBuffer(input); |
| 15397 | xmlFreeParserCtxt(ctxt); |
| 15398 | return (NULL); |
| 15399 | } |
| 15400 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15401 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15402 | } |
| 15403 | |
| 15404 | /** |
| 15405 | * xmlCtxtReadDoc: |
| 15406 | * @ctxt: an XML parser context |
| 15407 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15408 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15409 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15410 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15411 | * |
| 15412 | * parse an XML in-memory document and build a tree. |
| 15413 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15414 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15415 | * Returns the resulting document tree |
| 15416 | */ |
| 15417 | xmlDocPtr |
| 15418 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15419 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15420 | { |
| 15421 | xmlParserInputPtr stream; |
| 15422 | |
| 15423 | if (cur == NULL) |
| 15424 | return (NULL); |
| 15425 | if (ctxt == NULL) |
| 15426 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15427 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15428 | |
| 15429 | xmlCtxtReset(ctxt); |
| 15430 | |
| 15431 | stream = xmlNewStringInputStream(ctxt, cur); |
| 15432 | if (stream == NULL) { |
| 15433 | return (NULL); |
| 15434 | } |
| 15435 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15436 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15437 | } |
| 15438 | |
| 15439 | /** |
| 15440 | * xmlCtxtReadFile: |
| 15441 | * @ctxt: an XML parser context |
| 15442 | * @filename: a file or URL |
| 15443 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15444 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15445 | * |
| 15446 | * parse an XML file from the filesystem or the network. |
| 15447 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15448 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15449 | * Returns the resulting document tree |
| 15450 | */ |
| 15451 | xmlDocPtr |
| 15452 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 15453 | const char *encoding, int options) |
| 15454 | { |
| 15455 | xmlParserInputPtr stream; |
| 15456 | |
| 15457 | if (filename == NULL) |
| 15458 | return (NULL); |
| 15459 | if (ctxt == NULL) |
| 15460 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15461 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15462 | |
| 15463 | xmlCtxtReset(ctxt); |
| 15464 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 15465 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15466 | if (stream == NULL) { |
| 15467 | return (NULL); |
| 15468 | } |
| 15469 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15470 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15471 | } |
| 15472 | |
| 15473 | /** |
| 15474 | * xmlCtxtReadMemory: |
| 15475 | * @ctxt: an XML parser context |
| 15476 | * @buffer: a pointer to a char array |
| 15477 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15478 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15479 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15480 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15481 | * |
| 15482 | * parse an XML in-memory document and build a tree. |
| 15483 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15484 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15485 | * Returns the resulting document tree |
| 15486 | */ |
| 15487 | xmlDocPtr |
| 15488 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15489 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15490 | { |
| 15491 | xmlParserInputBufferPtr input; |
| 15492 | xmlParserInputPtr stream; |
| 15493 | |
| 15494 | if (ctxt == NULL) |
| 15495 | return (NULL); |
| 15496 | if (buffer == 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 | |
| 15502 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 15503 | if (input == NULL) { |
| 15504 | return(NULL); |
| 15505 | } |
| 15506 | |
| 15507 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15508 | if (stream == NULL) { |
| 15509 | xmlFreeParserInputBuffer(input); |
| 15510 | return(NULL); |
| 15511 | } |
| 15512 | |
| 15513 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15514 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15515 | } |
| 15516 | |
| 15517 | /** |
| 15518 | * xmlCtxtReadFd: |
| 15519 | * @ctxt: an XML parser context |
| 15520 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15521 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15522 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15523 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15524 | * |
| 15525 | * parse an XML from a file descriptor and build a tree. |
| 15526 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15527 | * NOTE that the file descriptor will not be closed when the |
| 15528 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15529 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15530 | * Returns the resulting document tree |
| 15531 | */ |
| 15532 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15533 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 15534 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15535 | { |
| 15536 | xmlParserInputBufferPtr input; |
| 15537 | xmlParserInputPtr stream; |
| 15538 | |
| 15539 | if (fd < 0) |
| 15540 | return (NULL); |
| 15541 | if (ctxt == NULL) |
| 15542 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15543 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15544 | |
| 15545 | xmlCtxtReset(ctxt); |
| 15546 | |
| 15547 | |
| 15548 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15549 | if (input == NULL) |
| 15550 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15551 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15552 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15553 | if (stream == NULL) { |
| 15554 | xmlFreeParserInputBuffer(input); |
| 15555 | return (NULL); |
| 15556 | } |
| 15557 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15558 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15559 | } |
| 15560 | |
| 15561 | /** |
| 15562 | * xmlCtxtReadIO: |
| 15563 | * @ctxt: an XML parser context |
| 15564 | * @ioread: an I/O read function |
| 15565 | * @ioclose: an I/O close function |
| 15566 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15567 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15568 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15569 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15570 | * |
| 15571 | * parse an XML document from I/O functions and source and build a tree. |
| 15572 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15573 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15574 | * Returns the resulting document tree |
| 15575 | */ |
| 15576 | xmlDocPtr |
| 15577 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 15578 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15579 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15580 | const char *encoding, int options) |
| 15581 | { |
| 15582 | xmlParserInputBufferPtr input; |
| 15583 | xmlParserInputPtr stream; |
| 15584 | |
| 15585 | if (ioread == NULL) |
| 15586 | return (NULL); |
| 15587 | if (ctxt == NULL) |
| 15588 | return (NULL); |
Daniel Veillard | 4e1476c | 2013-12-09 15:23:40 +0800 | [diff] [blame] | 15589 | xmlInitParser(); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15590 | |
| 15591 | xmlCtxtReset(ctxt); |
| 15592 | |
| 15593 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15594 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15595 | if (input == NULL) { |
| 15596 | if (ioclose != NULL) |
| 15597 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15598 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15599 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15600 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15601 | if (stream == NULL) { |
| 15602 | xmlFreeParserInputBuffer(input); |
| 15603 | return (NULL); |
| 15604 | } |
| 15605 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15606 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15607 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15608 | |
| 15609 | #define bottom_parser |
| 15610 | #include "elfgcchack.h" |