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 | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 125 | xmlEntityPtr ent) |
| 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); |
| 133 | if (size != 0) { |
| 134 | /* |
| 135 | * Do the check based on the replacement size of the entity |
| 136 | */ |
| 137 | if (size < XML_PARSER_BIG_ENTITY) |
| 138 | return(0); |
| 139 | |
| 140 | /* |
| 141 | * A limit on the amount of text data reasonably used |
| 142 | */ |
| 143 | if (ctxt->input != NULL) { |
| 144 | consumed = ctxt->input->consumed + |
| 145 | (ctxt->input->cur - ctxt->input->base); |
| 146 | } |
| 147 | consumed += ctxt->sizeentities; |
| 148 | |
| 149 | if ((size < XML_PARSER_NON_LINEAR * consumed) && |
| 150 | (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed)) |
| 151 | return (0); |
| 152 | } else if (ent != NULL) { |
| 153 | /* |
| 154 | * use the number of parsed entities in the replacement |
| 155 | */ |
| 156 | size = ent->checked; |
| 157 | |
| 158 | /* |
| 159 | * The amount of data parsed counting entities size only once |
| 160 | */ |
| 161 | if (ctxt->input != NULL) { |
| 162 | consumed = ctxt->input->consumed + |
| 163 | (ctxt->input->cur - ctxt->input->base); |
| 164 | } |
| 165 | consumed += ctxt->sizeentities; |
| 166 | |
| 167 | /* |
| 168 | * Check the density of entities for the amount of data |
| 169 | * knowing an entity reference will take at least 3 bytes |
| 170 | */ |
| 171 | if (size * 3 < consumed * XML_PARSER_NON_LINEAR) |
| 172 | return (0); |
| 173 | } else { |
| 174 | /* |
| 175 | * strange we got no data for checking just return |
| 176 | */ |
| 177 | return (0); |
| 178 | } |
| 179 | |
| 180 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 181 | return (1); |
| 182 | } |
| 183 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 184 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 185 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 186 | * |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 187 | * arbitrary depth limit for the XML documents that we allow to |
| 188 | * process. This is not a limitation of the parser but a safety |
| 189 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 190 | * parser option. |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 191 | */ |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 192 | unsigned int xmlParserMaxDepth = 256; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 193 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 194 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 195 | |
| 196 | #define SAX2 1 |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 197 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 198 | #define XML_PARSER_BUFFER_SIZE 100 |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 199 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 200 | |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 201 | /** |
| 202 | * XML_PARSER_CHUNK_SIZE |
| 203 | * |
| 204 | * When calling GROW that's the minimal amount of data |
| 205 | * the parser expected to have received. It is not a hard |
| 206 | * limit but an optimization when reading strings like Names |
| 207 | * It is not strictly needed as long as inputs available characters |
| 208 | * are followed by 0, which should be provided by the I/O level |
| 209 | */ |
| 210 | #define XML_PARSER_CHUNK_SIZE 100 |
| 211 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 212 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 213 | * List of XML prefixed PI allowed by W3C specs |
| 214 | */ |
| 215 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 216 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 217 | "xml-stylesheet", |
Daniel Veillard | 4c4653e | 2011-06-05 11:29:29 +0800 | [diff] [blame] | 218 | "xml-model", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 219 | NULL |
| 220 | }; |
| 221 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 222 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 223 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 224 | static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 225 | const xmlChar **str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 226 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 227 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 228 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 229 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 230 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 231 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 232 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 233 | static int |
| 234 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, |
| 235 | const char *encoding); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 236 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 237 | static void |
| 238 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 239 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 240 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 241 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 242 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 243 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 244 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 245 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 246 | static int |
| 247 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity); |
| 248 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 249 | /************************************************************************ |
| 250 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 251 | * Some factorized error routines * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 252 | * * |
| 253 | ************************************************************************/ |
| 254 | |
| 255 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 256 | * xmlErrAttributeDup: |
| 257 | * @ctxt: an XML parser context |
| 258 | * @prefix: the attribute prefix |
| 259 | * @localname: the attribute localname |
| 260 | * |
| 261 | * Handle a redefinition of attribute error |
| 262 | */ |
| 263 | static void |
| 264 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 265 | const xmlChar * localname) |
| 266 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 267 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 268 | (ctxt->instate == XML_PARSER_EOF)) |
| 269 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 270 | if (ctxt != NULL) |
| 271 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 272 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 273 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 274 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 275 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 276 | (const char *) localname, NULL, NULL, 0, 0, |
| 277 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 278 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 279 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 280 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 281 | (const char *) prefix, (const char *) localname, |
| 282 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 283 | localname); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 284 | if (ctxt != NULL) { |
| 285 | ctxt->wellFormed = 0; |
| 286 | if (ctxt->recovery == 0) |
| 287 | ctxt->disableSAX = 1; |
| 288 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * xmlFatalErr: |
| 293 | * @ctxt: an XML parser context |
| 294 | * @error: the error number |
| 295 | * @extra: extra information string |
| 296 | * |
| 297 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 298 | */ |
| 299 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 300 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 301 | { |
| 302 | const char *errmsg; |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 303 | char errstr[129] = ""; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 304 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 305 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 306 | (ctxt->instate == XML_PARSER_EOF)) |
| 307 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 308 | switch (error) { |
| 309 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 310 | errmsg = "CharRef: invalid hexadecimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 311 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 312 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 313 | errmsg = "CharRef: invalid decimal value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 314 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 315 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 316 | errmsg = "CharRef: invalid value"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 317 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 318 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 319 | errmsg = "internal error"; |
| 320 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 321 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 322 | errmsg = "PEReference at end of document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 323 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 324 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 325 | errmsg = "PEReference in prolog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 326 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 327 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 328 | errmsg = "PEReference in epilog"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 329 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 330 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 331 | errmsg = "PEReference: no name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 332 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 333 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 334 | errmsg = "PEReference: expecting ';'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 335 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 336 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 337 | errmsg = "Detected an entity reference loop"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 338 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 339 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 340 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 341 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 342 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 343 | errmsg = "PEReferences forbidden in internal subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 344 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 345 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 346 | errmsg = "EntityValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 347 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 348 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 349 | errmsg = "AttValue: \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 350 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 351 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 352 | errmsg = "Unescaped '<' not allowed in attributes values"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 353 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 354 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 355 | errmsg = "SystemLiteral \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 356 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 357 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 358 | errmsg = "Unfinished System or Public ID \" or ' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 359 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 360 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 361 | errmsg = "Sequence ']]>' not allowed in content"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 362 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 363 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 364 | errmsg = "SYSTEM or PUBLIC, the URI is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 365 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 366 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 367 | errmsg = "PUBLIC, the Public Identifier is missing"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 368 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 369 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 370 | errmsg = "Comment must not contain '--' (double-hyphen)"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 371 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 372 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 373 | errmsg = "xmlParsePI : no target name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 374 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 375 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 376 | errmsg = "Invalid PI name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 377 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 378 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 379 | errmsg = "NOTATION: Name expected here"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 380 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 381 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 382 | errmsg = "'>' required to close NOTATION declaration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 383 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 384 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 385 | errmsg = "Entity value required"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 386 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 387 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 388 | errmsg = "Fragment not allowed"; |
| 389 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 390 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 391 | errmsg = "'(' required to start ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 392 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 393 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 394 | errmsg = "NmToken expected in ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 395 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 396 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 397 | errmsg = "')' required to finish ATTLIST enumeration"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 398 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 399 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 400 | errmsg = "MixedContentDecl : '|' or ')*' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 401 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 402 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 403 | errmsg = "MixedContentDecl : '#PCDATA' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 404 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 405 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 406 | errmsg = "ContentDecl : Name or '(' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 407 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 408 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 409 | errmsg = "ContentDecl : ',' '|' or ')' expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 410 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 411 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 412 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 413 | "PEReference: forbidden within markup decl in internal subset"; |
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_GT_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 416 | errmsg = "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_CONDSEC_INVALID: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 419 | errmsg = "XML conditional section '[' 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_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 422 | errmsg = "Content error in the external subset"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 423 | break; |
| 424 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 425 | errmsg = |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 426 | "conditional section INCLUDE or IGNORE keyword expected"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 427 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 428 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 429 | errmsg = "XML conditional section not closed"; |
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_XMLDECL_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 432 | errmsg = "Text declaration '<?xml' required"; |
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_XMLDECL_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 435 | errmsg = "parsing XML declaration: '?>' 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_ENTITY_STANDALONE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 438 | errmsg = "external parsed entities cannot be standalone"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 439 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 440 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 441 | errmsg = "EntityRef: expecting ';'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 442 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 443 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 444 | errmsg = "DOCTYPE improperly terminated"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 445 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 446 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 447 | errmsg = "EndTag: '</' not found"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 448 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 449 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 450 | errmsg = "expected '='"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 451 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 452 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 453 | errmsg = "String not closed expecting \" or '"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 454 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 455 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 456 | errmsg = "String not started expecting ' or \""; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 457 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 458 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 459 | errmsg = "Invalid XML encoding name"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 460 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 461 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 462 | errmsg = "standalone accepts only 'yes' or 'no'"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 463 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 464 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 465 | errmsg = "Document is empty"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 466 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 467 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 468 | errmsg = "Extra content at the end of the document"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 469 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 470 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 471 | errmsg = "chunk is not well balanced"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 472 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 473 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 474 | errmsg = "extra content at the end of well balanced chunk"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 475 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 476 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 477 | errmsg = "Malformed declaration expecting version"; |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 478 | break; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 479 | case XML_ERR_NAME_TOO_LONG: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 480 | errmsg = "Name too long use XML_PARSE_HUGE option"; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 481 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 482 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 483 | case: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 484 | errmsg = ""; |
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 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 487 | default: |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 488 | errmsg = "Unregistered error message"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 489 | } |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 490 | if (info == NULL) |
| 491 | snprintf(errstr, 128, "%s\n", errmsg); |
| 492 | else |
| 493 | snprintf(errstr, 128, "%s: %%s\n", errmsg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 494 | if (ctxt != NULL) |
| 495 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 496 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | e7bf892 | 2012-07-30 20:09:25 +0800 | [diff] [blame] | 497 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, &errstr[0], |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 498 | info); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 499 | if (ctxt != NULL) { |
| 500 | ctxt->wellFormed = 0; |
| 501 | if (ctxt->recovery == 0) |
| 502 | ctxt->disableSAX = 1; |
| 503 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 506 | /** |
| 507 | * xmlFatalErrMsg: |
| 508 | * @ctxt: an XML parser context |
| 509 | * @error: the error number |
| 510 | * @msg: the error message |
| 511 | * |
| 512 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 513 | */ |
| 514 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 515 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 516 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 517 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 518 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 519 | (ctxt->instate == XML_PARSER_EOF)) |
| 520 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 521 | if (ctxt != NULL) |
| 522 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 523 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 524 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 525 | if (ctxt != NULL) { |
| 526 | ctxt->wellFormed = 0; |
| 527 | if (ctxt->recovery == 0) |
| 528 | ctxt->disableSAX = 1; |
| 529 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 533 | * xmlWarningMsg: |
| 534 | * @ctxt: an XML parser context |
| 535 | * @error: the error number |
| 536 | * @msg: the error message |
| 537 | * @str1: extra data |
| 538 | * @str2: extra data |
| 539 | * |
| 540 | * Handle a warning. |
| 541 | */ |
| 542 | static void |
| 543 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 544 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 545 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 546 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 547 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 548 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 549 | (ctxt->instate == XML_PARSER_EOF)) |
| 550 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 551 | if ((ctxt != NULL) && (ctxt->sax != NULL) && |
| 552 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 553 | schannel = ctxt->sax->serror; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 554 | if (ctxt != NULL) { |
| 555 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 556 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 557 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 558 | ctxt, NULL, XML_FROM_PARSER, error, |
| 559 | XML_ERR_WARNING, NULL, 0, |
| 560 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 561 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 562 | } else { |
| 563 | __xmlRaiseError(schannel, NULL, NULL, |
| 564 | ctxt, NULL, XML_FROM_PARSER, error, |
| 565 | XML_ERR_WARNING, NULL, 0, |
| 566 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 567 | msg, (const char *) str1, (const char *) str2); |
| 568 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
| 572 | * xmlValidityError: |
| 573 | * @ctxt: an XML parser context |
| 574 | * @error: the error number |
| 575 | * @msg: the error message |
| 576 | * @str1: extra data |
| 577 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 578 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 579 | */ |
| 580 | static void |
| 581 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 582 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 583 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 584 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 585 | |
| 586 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 587 | (ctxt->instate == XML_PARSER_EOF)) |
| 588 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 589 | if (ctxt != NULL) { |
| 590 | ctxt->errNo = error; |
| 591 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 592 | schannel = ctxt->sax->serror; |
| 593 | } |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 594 | if (ctxt != NULL) { |
| 595 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 596 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 597 | ctxt, NULL, XML_FROM_DTD, error, |
| 598 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 599 | (const char *) str2, NULL, 0, 0, |
| 600 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 601 | ctxt->valid = 0; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 602 | } else { |
| 603 | __xmlRaiseError(schannel, NULL, NULL, |
| 604 | ctxt, NULL, XML_FROM_DTD, error, |
| 605 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 606 | (const char *) str2, NULL, 0, 0, |
| 607 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 608 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 612 | * xmlFatalErrMsgInt: |
| 613 | * @ctxt: an XML parser context |
| 614 | * @error: the error number |
| 615 | * @msg: the error message |
| 616 | * @val: an integer value |
| 617 | * |
| 618 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 619 | */ |
| 620 | static void |
| 621 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 622 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 623 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 624 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 625 | (ctxt->instate == XML_PARSER_EOF)) |
| 626 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 627 | if (ctxt != NULL) |
| 628 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 629 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 630 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 631 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 632 | if (ctxt != NULL) { |
| 633 | ctxt->wellFormed = 0; |
| 634 | if (ctxt->recovery == 0) |
| 635 | ctxt->disableSAX = 1; |
| 636 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 640 | * xmlFatalErrMsgStrIntStr: |
| 641 | * @ctxt: an XML parser context |
| 642 | * @error: the error number |
| 643 | * @msg: the error message |
| 644 | * @str1: an string info |
| 645 | * @val: an integer value |
| 646 | * @str2: an string info |
| 647 | * |
| 648 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 649 | */ |
| 650 | static void |
| 651 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 652 | const char *msg, const xmlChar *str1, int val, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 653 | const xmlChar *str2) |
| 654 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 655 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 656 | (ctxt->instate == XML_PARSER_EOF)) |
| 657 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 658 | if (ctxt != NULL) |
| 659 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 660 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 661 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 662 | NULL, 0, (const char *) str1, (const char *) str2, |
| 663 | NULL, val, 0, msg, str1, val, str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 664 | if (ctxt != NULL) { |
| 665 | ctxt->wellFormed = 0; |
| 666 | if (ctxt->recovery == 0) |
| 667 | ctxt->disableSAX = 1; |
| 668 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 672 | * xmlFatalErrMsgStr: |
| 673 | * @ctxt: an XML parser context |
| 674 | * @error: the error number |
| 675 | * @msg: the error message |
| 676 | * @val: a string value |
| 677 | * |
| 678 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 679 | */ |
| 680 | static void |
| 681 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 682 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 683 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 684 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 685 | (ctxt->instate == XML_PARSER_EOF)) |
| 686 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 687 | if (ctxt != NULL) |
| 688 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 689 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 690 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 691 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 692 | val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 693 | if (ctxt != NULL) { |
| 694 | ctxt->wellFormed = 0; |
| 695 | if (ctxt->recovery == 0) |
| 696 | ctxt->disableSAX = 1; |
| 697 | } |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 701 | * xmlErrMsgStr: |
| 702 | * @ctxt: an XML parser context |
| 703 | * @error: the error number |
| 704 | * @msg: the error message |
| 705 | * @val: a string value |
| 706 | * |
| 707 | * Handle a non fatal parser error |
| 708 | */ |
| 709 | static void |
| 710 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 711 | const char *msg, const xmlChar * val) |
| 712 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 713 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 714 | (ctxt->instate == XML_PARSER_EOF)) |
| 715 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 716 | if (ctxt != NULL) |
| 717 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 718 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 719 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 720 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 721 | val); |
| 722 | } |
| 723 | |
| 724 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 725 | * xmlNsErr: |
| 726 | * @ctxt: an XML parser context |
| 727 | * @error: the error number |
| 728 | * @msg: the message |
| 729 | * @info1: extra information string |
| 730 | * @info2: extra information string |
| 731 | * |
| 732 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 733 | */ |
| 734 | static void |
| 735 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 736 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 737 | const xmlChar * info1, const xmlChar * info2, |
| 738 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 739 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 740 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 741 | (ctxt->instate == XML_PARSER_EOF)) |
| 742 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 743 | if (ctxt != NULL) |
| 744 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 745 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 746 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 747 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 748 | info1, info2, info3); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 749 | if (ctxt != NULL) |
| 750 | ctxt->nsWellFormed = 0; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 753 | /** |
| 754 | * xmlNsWarn |
| 755 | * @ctxt: an XML parser context |
| 756 | * @error: the error number |
| 757 | * @msg: the message |
| 758 | * @info1: extra information string |
| 759 | * @info2: extra information string |
| 760 | * |
Daniel Veillard | 288bb62 | 2012-05-07 15:01:29 +0800 | [diff] [blame] | 761 | * Handle a namespace warning error |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 762 | */ |
| 763 | static void |
| 764 | xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 765 | const char *msg, |
| 766 | const xmlChar * info1, const xmlChar * info2, |
| 767 | const xmlChar * info3) |
| 768 | { |
| 769 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 770 | (ctxt->instate == XML_PARSER_EOF)) |
| 771 | return; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 772 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
| 773 | XML_ERR_WARNING, NULL, 0, (const char *) info1, |
| 774 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 775 | info1, info2, info3); |
| 776 | } |
| 777 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 778 | /************************************************************************ |
| 779 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 780 | * Library wide options * |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 781 | * * |
| 782 | ************************************************************************/ |
| 783 | |
| 784 | /** |
| 785 | * xmlHasFeature: |
| 786 | * @feature: the feature to be examined |
| 787 | * |
| 788 | * Examines if the library has been compiled with a given feature. |
| 789 | * |
| 790 | * Returns a non-zero value if the feature exist, otherwise zero. |
| 791 | * Returns zero (0) if the feature does not exist or an unknown |
| 792 | * unknown feature is requested, non-zero otherwise. |
| 793 | */ |
| 794 | int |
| 795 | xmlHasFeature(xmlFeature feature) |
| 796 | { |
| 797 | switch (feature) { |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 798 | case XML_WITH_THREAD: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 799 | #ifdef LIBXML_THREAD_ENABLED |
| 800 | return(1); |
| 801 | #else |
| 802 | return(0); |
| 803 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 804 | case XML_WITH_TREE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 805 | #ifdef LIBXML_TREE_ENABLED |
| 806 | return(1); |
| 807 | #else |
| 808 | return(0); |
| 809 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 810 | case XML_WITH_OUTPUT: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 811 | #ifdef LIBXML_OUTPUT_ENABLED |
| 812 | return(1); |
| 813 | #else |
| 814 | return(0); |
| 815 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 816 | case XML_WITH_PUSH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 817 | #ifdef LIBXML_PUSH_ENABLED |
| 818 | return(1); |
| 819 | #else |
| 820 | return(0); |
| 821 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 822 | case XML_WITH_READER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 823 | #ifdef LIBXML_READER_ENABLED |
| 824 | return(1); |
| 825 | #else |
| 826 | return(0); |
| 827 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 828 | case XML_WITH_PATTERN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 829 | #ifdef LIBXML_PATTERN_ENABLED |
| 830 | return(1); |
| 831 | #else |
| 832 | return(0); |
| 833 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 834 | case XML_WITH_WRITER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 835 | #ifdef LIBXML_WRITER_ENABLED |
| 836 | return(1); |
| 837 | #else |
| 838 | return(0); |
| 839 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 840 | case XML_WITH_SAX1: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 841 | #ifdef LIBXML_SAX1_ENABLED |
| 842 | return(1); |
| 843 | #else |
| 844 | return(0); |
| 845 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 846 | case XML_WITH_FTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 847 | #ifdef LIBXML_FTP_ENABLED |
| 848 | return(1); |
| 849 | #else |
| 850 | return(0); |
| 851 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 852 | case XML_WITH_HTTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 853 | #ifdef LIBXML_HTTP_ENABLED |
| 854 | return(1); |
| 855 | #else |
| 856 | return(0); |
| 857 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 858 | case XML_WITH_VALID: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 859 | #ifdef LIBXML_VALID_ENABLED |
| 860 | return(1); |
| 861 | #else |
| 862 | return(0); |
| 863 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 864 | case XML_WITH_HTML: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 865 | #ifdef LIBXML_HTML_ENABLED |
| 866 | return(1); |
| 867 | #else |
| 868 | return(0); |
| 869 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 870 | case XML_WITH_LEGACY: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 871 | #ifdef LIBXML_LEGACY_ENABLED |
| 872 | return(1); |
| 873 | #else |
| 874 | return(0); |
| 875 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 876 | case XML_WITH_C14N: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 877 | #ifdef LIBXML_C14N_ENABLED |
| 878 | return(1); |
| 879 | #else |
| 880 | return(0); |
| 881 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 882 | case XML_WITH_CATALOG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 883 | #ifdef LIBXML_CATALOG_ENABLED |
| 884 | return(1); |
| 885 | #else |
| 886 | return(0); |
| 887 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 888 | case XML_WITH_XPATH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 889 | #ifdef LIBXML_XPATH_ENABLED |
| 890 | return(1); |
| 891 | #else |
| 892 | return(0); |
| 893 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 894 | case XML_WITH_XPTR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 895 | #ifdef LIBXML_XPTR_ENABLED |
| 896 | return(1); |
| 897 | #else |
| 898 | return(0); |
| 899 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 900 | case XML_WITH_XINCLUDE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 901 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 902 | return(1); |
| 903 | #else |
| 904 | return(0); |
| 905 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 906 | case XML_WITH_ICONV: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 907 | #ifdef LIBXML_ICONV_ENABLED |
| 908 | return(1); |
| 909 | #else |
| 910 | return(0); |
| 911 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 912 | case XML_WITH_ISO8859X: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 913 | #ifdef LIBXML_ISO8859X_ENABLED |
| 914 | return(1); |
| 915 | #else |
| 916 | return(0); |
| 917 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 918 | case XML_WITH_UNICODE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 919 | #ifdef LIBXML_UNICODE_ENABLED |
| 920 | return(1); |
| 921 | #else |
| 922 | return(0); |
| 923 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 924 | case XML_WITH_REGEXP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 925 | #ifdef LIBXML_REGEXP_ENABLED |
| 926 | return(1); |
| 927 | #else |
| 928 | return(0); |
| 929 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 930 | case XML_WITH_AUTOMATA: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 931 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 932 | return(1); |
| 933 | #else |
| 934 | return(0); |
| 935 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 936 | case XML_WITH_EXPR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 937 | #ifdef LIBXML_EXPR_ENABLED |
| 938 | return(1); |
| 939 | #else |
| 940 | return(0); |
| 941 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 942 | case XML_WITH_SCHEMAS: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 943 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 944 | return(1); |
| 945 | #else |
| 946 | return(0); |
| 947 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 948 | case XML_WITH_SCHEMATRON: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 949 | #ifdef LIBXML_SCHEMATRON_ENABLED |
| 950 | return(1); |
| 951 | #else |
| 952 | return(0); |
| 953 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 954 | case XML_WITH_MODULES: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 955 | #ifdef LIBXML_MODULES_ENABLED |
| 956 | return(1); |
| 957 | #else |
| 958 | return(0); |
| 959 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 960 | case XML_WITH_DEBUG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 961 | #ifdef LIBXML_DEBUG_ENABLED |
| 962 | return(1); |
| 963 | #else |
| 964 | return(0); |
| 965 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 966 | case XML_WITH_DEBUG_MEM: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 967 | #ifdef DEBUG_MEMORY_LOCATION |
| 968 | return(1); |
| 969 | #else |
| 970 | return(0); |
| 971 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 972 | case XML_WITH_DEBUG_RUN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 973 | #ifdef LIBXML_DEBUG_RUNTIME |
| 974 | return(1); |
| 975 | #else |
| 976 | return(0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 977 | #endif |
Daniel Veillard | 75acfee | 2006-07-13 06:29:56 +0000 | [diff] [blame] | 978 | case XML_WITH_ZLIB: |
| 979 | #ifdef LIBXML_ZLIB_ENABLED |
| 980 | return(1); |
| 981 | #else |
| 982 | return(0); |
| 983 | #endif |
Anders F Bjorklund | eae5261 | 2011-09-18 16:59:13 +0200 | [diff] [blame] | 984 | case XML_WITH_LZMA: |
| 985 | #ifdef LIBXML_LZMA_ENABLED |
| 986 | return(1); |
| 987 | #else |
| 988 | return(0); |
| 989 | #endif |
Giuseppe Iuculano | 48f7dcb | 2010-11-04 17:42:42 +0100 | [diff] [blame] | 990 | case XML_WITH_ICU: |
| 991 | #ifdef LIBXML_ICU_ENABLED |
| 992 | return(1); |
| 993 | #else |
| 994 | return(0); |
| 995 | #endif |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 996 | default: |
| 997 | break; |
| 998 | } |
| 999 | return(0); |
| 1000 | } |
| 1001 | |
| 1002 | /************************************************************************ |
| 1003 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1004 | * SAX2 defaulted attributes handling * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1005 | * * |
| 1006 | ************************************************************************/ |
| 1007 | |
| 1008 | /** |
| 1009 | * xmlDetectSAX2: |
| 1010 | * @ctxt: an XML parser context |
| 1011 | * |
| 1012 | * Do the SAX2 detection and specific intialization |
| 1013 | */ |
| 1014 | static void |
| 1015 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 1016 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1017 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1018 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 1019 | ((ctxt->sax->startElementNs != NULL) || |
| 1020 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 1021 | #else |
| 1022 | ctxt->sax2 = 1; |
| 1023 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1024 | |
| 1025 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 1026 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 1027 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1028 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 1029 | (ctxt->str_xml_ns == NULL)) { |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 1030 | xmlErrMemory(ctxt, NULL); |
| 1031 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1034 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 1035 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 1036 | struct _xmlDefAttrs { |
| 1037 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 1038 | int maxAttrs; /* the size of the array */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1039 | const xmlChar *values[5]; /* array of localname/prefix/values/external */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1040 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1041 | |
| 1042 | /** |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1043 | * xmlAttrNormalizeSpace: |
| 1044 | * @src: the source string |
| 1045 | * @dst: the target string |
| 1046 | * |
| 1047 | * Normalize the space in non CDATA attribute values: |
| 1048 | * If the attribute type is not CDATA, then the XML processor MUST further |
| 1049 | * process the normalized attribute value by discarding any leading and |
| 1050 | * trailing space (#x20) characters, and by replacing sequences of space |
| 1051 | * (#x20) characters by a single space (#x20) character. |
| 1052 | * Note that the size of dst need to be at least src, and if one doesn't need |
| 1053 | * to preserve dst (and it doesn't come from a dictionary or read-only) then |
| 1054 | * passing src as dst is just fine. |
| 1055 | * |
| 1056 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1057 | * is needed. |
| 1058 | */ |
| 1059 | static xmlChar * |
| 1060 | xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst) |
| 1061 | { |
| 1062 | if ((src == NULL) || (dst == NULL)) |
| 1063 | return(NULL); |
| 1064 | |
| 1065 | while (*src == 0x20) src++; |
| 1066 | while (*src != 0) { |
| 1067 | if (*src == 0x20) { |
| 1068 | while (*src == 0x20) src++; |
| 1069 | if (*src != 0) |
| 1070 | *dst++ = 0x20; |
| 1071 | } else { |
| 1072 | *dst++ = *src++; |
| 1073 | } |
| 1074 | } |
| 1075 | *dst = 0; |
| 1076 | if (dst == src) |
| 1077 | return(NULL); |
| 1078 | return(dst); |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * xmlAttrNormalizeSpace2: |
| 1083 | * @src: the source string |
| 1084 | * |
| 1085 | * Normalize the space in non CDATA attribute values, a slightly more complex |
| 1086 | * front end to avoid allocation problems when running on attribute values |
| 1087 | * coming from the input. |
| 1088 | * |
| 1089 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1090 | * is needed. |
| 1091 | */ |
| 1092 | static const xmlChar * |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1093 | xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len) |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1094 | { |
| 1095 | int i; |
| 1096 | int remove_head = 0; |
| 1097 | int need_realloc = 0; |
| 1098 | const xmlChar *cur; |
| 1099 | |
| 1100 | if ((ctxt == NULL) || (src == NULL) || (len == NULL)) |
| 1101 | return(NULL); |
| 1102 | i = *len; |
| 1103 | if (i <= 0) |
| 1104 | return(NULL); |
| 1105 | |
| 1106 | cur = src; |
| 1107 | while (*cur == 0x20) { |
| 1108 | cur++; |
| 1109 | remove_head++; |
| 1110 | } |
| 1111 | while (*cur != 0) { |
| 1112 | if (*cur == 0x20) { |
| 1113 | cur++; |
| 1114 | if ((*cur == 0x20) || (*cur == 0)) { |
| 1115 | need_realloc = 1; |
| 1116 | break; |
| 1117 | } |
| 1118 | } else |
| 1119 | cur++; |
| 1120 | } |
| 1121 | if (need_realloc) { |
| 1122 | xmlChar *ret; |
| 1123 | |
| 1124 | ret = xmlStrndup(src + remove_head, i - remove_head + 1); |
| 1125 | if (ret == NULL) { |
| 1126 | xmlErrMemory(ctxt, NULL); |
| 1127 | return(NULL); |
| 1128 | } |
| 1129 | xmlAttrNormalizeSpace(ret, ret); |
| 1130 | *len = (int) strlen((const char *)ret); |
| 1131 | return(ret); |
| 1132 | } else if (remove_head) { |
| 1133 | *len -= remove_head; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1134 | memmove(src, src + remove_head, 1 + *len); |
| 1135 | return(src); |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1136 | } |
| 1137 | return(NULL); |
| 1138 | } |
| 1139 | |
| 1140 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1141 | * xmlAddDefAttrs: |
| 1142 | * @ctxt: an XML parser context |
| 1143 | * @fullname: the element fullname |
| 1144 | * @fullattr: the attribute fullname |
| 1145 | * @value: the attribute value |
| 1146 | * |
| 1147 | * Add a defaulted attribute for an element |
| 1148 | */ |
| 1149 | static void |
| 1150 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 1151 | const xmlChar *fullname, |
| 1152 | const xmlChar *fullattr, |
| 1153 | const xmlChar *value) { |
| 1154 | xmlDefAttrsPtr defaults; |
| 1155 | int len; |
| 1156 | const xmlChar *name; |
| 1157 | const xmlChar *prefix; |
| 1158 | |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 1159 | /* |
| 1160 | * Allows to detect attribute redefinitions |
| 1161 | */ |
| 1162 | if (ctxt->attsSpecial != NULL) { |
| 1163 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1164 | return; |
| 1165 | } |
| 1166 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1167 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1168 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1169 | if (ctxt->attsDefault == NULL) |
| 1170 | goto mem_error; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1174 | * split the element name into prefix:localname , the string found |
| 1175 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1176 | */ |
| 1177 | name = xmlSplitQName3(fullname, &len); |
| 1178 | if (name == NULL) { |
| 1179 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 1180 | prefix = NULL; |
| 1181 | } else { |
| 1182 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1183 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * make sure there is some storage |
| 1188 | */ |
| 1189 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 1190 | if (defaults == NULL) { |
| 1191 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1192 | (4 * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1193 | if (defaults == NULL) |
| 1194 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1195 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1196 | defaults->maxAttrs = 4; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1197 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1198 | defaults, NULL) < 0) { |
| 1199 | xmlFree(defaults); |
| 1200 | goto mem_error; |
| 1201 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1202 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1203 | xmlDefAttrsPtr temp; |
| 1204 | |
| 1205 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1206 | (2 * defaults->maxAttrs * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1207 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1208 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1209 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1210 | defaults->maxAttrs *= 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1211 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1212 | defaults, NULL) < 0) { |
| 1213 | xmlFree(defaults); |
| 1214 | goto mem_error; |
| 1215 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | /* |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 1219 | * Split the element name into prefix:localname , the string found |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1220 | * are within the DTD and hen not associated to namespace names. |
| 1221 | */ |
| 1222 | name = xmlSplitQName3(fullattr, &len); |
| 1223 | if (name == NULL) { |
| 1224 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 1225 | prefix = NULL; |
| 1226 | } else { |
| 1227 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1228 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 1229 | } |
| 1230 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1231 | defaults->values[5 * defaults->nbAttrs] = name; |
| 1232 | defaults->values[5 * defaults->nbAttrs + 1] = prefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1233 | /* intern the string and precompute the end */ |
| 1234 | len = xmlStrlen(value); |
| 1235 | value = xmlDictLookup(ctxt->dict, value, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1236 | defaults->values[5 * defaults->nbAttrs + 2] = value; |
| 1237 | defaults->values[5 * defaults->nbAttrs + 3] = value + len; |
| 1238 | if (ctxt->external) |
| 1239 | defaults->values[5 * defaults->nbAttrs + 4] = BAD_CAST "external"; |
| 1240 | else |
| 1241 | defaults->values[5 * defaults->nbAttrs + 4] = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1242 | defaults->nbAttrs++; |
| 1243 | |
| 1244 | return; |
| 1245 | |
| 1246 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1247 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1251 | /** |
| 1252 | * xmlAddSpecialAttr: |
| 1253 | * @ctxt: an XML parser context |
| 1254 | * @fullname: the element fullname |
| 1255 | * @fullattr: the attribute fullname |
| 1256 | * @type: the attribute type |
| 1257 | * |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1258 | * Register this attribute type |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1259 | */ |
| 1260 | static void |
| 1261 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 1262 | const xmlChar *fullname, |
| 1263 | const xmlChar *fullattr, |
| 1264 | int type) |
| 1265 | { |
| 1266 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1267 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1268 | if (ctxt->attsSpecial == NULL) |
| 1269 | goto mem_error; |
| 1270 | } |
| 1271 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1272 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1273 | return; |
| 1274 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1275 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 1276 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1277 | return; |
| 1278 | |
| 1279 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1280 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1281 | return; |
| 1282 | } |
| 1283 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1284 | /** |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1285 | * xmlCleanSpecialAttrCallback: |
| 1286 | * |
| 1287 | * Removes CDATA attributes from the special attribute table |
| 1288 | */ |
| 1289 | static void |
| 1290 | xmlCleanSpecialAttrCallback(void *payload, void *data, |
| 1291 | const xmlChar *fullname, const xmlChar *fullattr, |
| 1292 | const xmlChar *unused ATTRIBUTE_UNUSED) { |
| 1293 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data; |
| 1294 | |
Daniel Veillard | b3edafd | 2008-01-11 08:00:57 +0000 | [diff] [blame] | 1295 | if (((long) payload) == XML_ATTRIBUTE_CDATA) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1296 | xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL); |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | /** |
| 1301 | * xmlCleanSpecialAttr: |
| 1302 | * @ctxt: an XML parser context |
| 1303 | * |
| 1304 | * Trim the list of attributes defined to remove all those of type |
| 1305 | * CDATA as they are not special. This call should be done when finishing |
| 1306 | * to parse the DTD and before starting to parse the document root. |
| 1307 | */ |
| 1308 | static void |
| 1309 | xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt) |
| 1310 | { |
| 1311 | if (ctxt->attsSpecial == NULL) |
| 1312 | return; |
| 1313 | |
| 1314 | xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt); |
| 1315 | |
| 1316 | if (xmlHashSize(ctxt->attsSpecial) == 0) { |
| 1317 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 1318 | ctxt->attsSpecial = NULL; |
| 1319 | } |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | /** |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1324 | * xmlCheckLanguageID: |
| 1325 | * @lang: pointer to the string value |
| 1326 | * |
| 1327 | * Checks that the value conforms to the LanguageID production: |
| 1328 | * |
| 1329 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 1330 | * the XML Second edition. |
| 1331 | * |
| 1332 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 1333 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 1334 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 1335 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 1336 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 1337 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 1338 | * |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1339 | * The current REC reference the sucessors of RFC 1766, currently 5646 |
| 1340 | * |
| 1341 | * http://www.rfc-editor.org/rfc/rfc5646.txt |
| 1342 | * langtag = language |
| 1343 | * ["-" script] |
| 1344 | * ["-" region] |
| 1345 | * *("-" variant) |
| 1346 | * *("-" extension) |
| 1347 | * ["-" privateuse] |
| 1348 | * language = 2*3ALPHA ; shortest ISO 639 code |
| 1349 | * ["-" extlang] ; sometimes followed by |
| 1350 | * ; extended language subtags |
| 1351 | * / 4ALPHA ; or reserved for future use |
| 1352 | * / 5*8ALPHA ; or registered language subtag |
| 1353 | * |
| 1354 | * extlang = 3ALPHA ; selected ISO 639 codes |
| 1355 | * *2("-" 3ALPHA) ; permanently reserved |
| 1356 | * |
| 1357 | * script = 4ALPHA ; ISO 15924 code |
| 1358 | * |
| 1359 | * region = 2ALPHA ; ISO 3166-1 code |
| 1360 | * / 3DIGIT ; UN M.49 code |
| 1361 | * |
| 1362 | * variant = 5*8alphanum ; registered variants |
| 1363 | * / (DIGIT 3alphanum) |
| 1364 | * |
| 1365 | * extension = singleton 1*("-" (2*8alphanum)) |
| 1366 | * |
| 1367 | * ; Single alphanumerics |
| 1368 | * ; "x" reserved for private use |
| 1369 | * singleton = DIGIT ; 0 - 9 |
| 1370 | * / %x41-57 ; A - W |
| 1371 | * / %x59-5A ; Y - Z |
| 1372 | * / %x61-77 ; a - w |
| 1373 | * / %x79-7A ; y - z |
| 1374 | * |
| 1375 | * it sounds right to still allow Irregular i-xxx IANA and user codes too |
| 1376 | * The parser below doesn't try to cope with extension or privateuse |
| 1377 | * that could be added but that's not interoperable anyway |
| 1378 | * |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1379 | * Returns 1 if correct 0 otherwise |
| 1380 | **/ |
| 1381 | int |
| 1382 | xmlCheckLanguageID(const xmlChar * lang) |
| 1383 | { |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1384 | const xmlChar *cur = lang, *nxt; |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (cur == NULL) |
| 1387 | return (0); |
| 1388 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1389 | ((cur[0] == 'I') && (cur[1] == '-')) || |
| 1390 | ((cur[0] == 'x') && (cur[1] == '-')) || |
| 1391 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1392 | /* |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1393 | * Still allow IANA code and user code which were coming |
| 1394 | * from the previous version of the XML-1.0 specification |
| 1395 | * it's deprecated but we should not fail |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1396 | */ |
| 1397 | cur += 2; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1398 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1399 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1400 | cur++; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1401 | return(cur[0] == 0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1402 | } |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1403 | nxt = cur; |
| 1404 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1405 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1406 | nxt++; |
| 1407 | if (nxt - cur >= 4) { |
| 1408 | /* |
| 1409 | * Reserved |
| 1410 | */ |
| 1411 | if ((nxt - cur > 8) || (nxt[0] != 0)) |
| 1412 | return(0); |
| 1413 | return(1); |
| 1414 | } |
| 1415 | if (nxt - cur < 2) |
| 1416 | return(0); |
| 1417 | /* we got an ISO 639 code */ |
| 1418 | if (nxt[0] == 0) |
| 1419 | return(1); |
| 1420 | if (nxt[0] != '-') |
| 1421 | return(0); |
| 1422 | |
| 1423 | nxt++; |
| 1424 | cur = nxt; |
| 1425 | /* now we can have extlang or script or region or variant */ |
| 1426 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1427 | goto region_m49; |
| 1428 | |
| 1429 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1430 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1431 | nxt++; |
| 1432 | if (nxt - cur == 4) |
| 1433 | goto script; |
| 1434 | if (nxt - cur == 2) |
| 1435 | goto region; |
| 1436 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1437 | goto variant; |
| 1438 | if (nxt - cur != 3) |
| 1439 | return(0); |
| 1440 | /* we parsed an extlang */ |
| 1441 | if (nxt[0] == 0) |
| 1442 | return(1); |
| 1443 | if (nxt[0] != '-') |
| 1444 | return(0); |
| 1445 | |
| 1446 | nxt++; |
| 1447 | cur = nxt; |
| 1448 | /* now we can have script or region or variant */ |
| 1449 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1450 | goto region_m49; |
| 1451 | |
| 1452 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1453 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1454 | nxt++; |
| 1455 | if (nxt - cur == 2) |
| 1456 | goto region; |
| 1457 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1458 | goto variant; |
| 1459 | if (nxt - cur != 4) |
| 1460 | return(0); |
| 1461 | /* we parsed a script */ |
| 1462 | script: |
| 1463 | if (nxt[0] == 0) |
| 1464 | return(1); |
| 1465 | if (nxt[0] != '-') |
| 1466 | return(0); |
| 1467 | |
| 1468 | nxt++; |
| 1469 | cur = nxt; |
| 1470 | /* now we can have region or variant */ |
| 1471 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1472 | goto region_m49; |
| 1473 | |
| 1474 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1475 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1476 | nxt++; |
| 1477 | |
| 1478 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1479 | goto variant; |
| 1480 | if (nxt - cur != 2) |
| 1481 | return(0); |
| 1482 | /* we parsed a region */ |
| 1483 | region: |
| 1484 | if (nxt[0] == 0) |
| 1485 | return(1); |
| 1486 | if (nxt[0] != '-') |
| 1487 | return(0); |
| 1488 | |
| 1489 | nxt++; |
| 1490 | cur = nxt; |
| 1491 | /* now we can just have a variant */ |
| 1492 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1493 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1494 | nxt++; |
| 1495 | |
| 1496 | if ((nxt - cur < 5) || (nxt - cur > 8)) |
| 1497 | return(0); |
| 1498 | |
| 1499 | /* we parsed a variant */ |
| 1500 | variant: |
| 1501 | if (nxt[0] == 0) |
| 1502 | return(1); |
| 1503 | if (nxt[0] != '-') |
| 1504 | return(0); |
| 1505 | /* extensions and private use subtags not checked */ |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1506 | return (1); |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1507 | |
| 1508 | region_m49: |
| 1509 | if (((nxt[1] >= '0') && (nxt[1] <= '9')) && |
| 1510 | ((nxt[2] >= '0') && (nxt[2] <= '9'))) { |
| 1511 | nxt += 3; |
| 1512 | goto region; |
| 1513 | } |
| 1514 | return(0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1517 | /************************************************************************ |
| 1518 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 1519 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1520 | * * |
| 1521 | ************************************************************************/ |
| 1522 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 1523 | static xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 1524 | const xmlChar ** str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1525 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1526 | #ifdef SAX2 |
| 1527 | /** |
| 1528 | * nsPush: |
| 1529 | * @ctxt: an XML parser context |
| 1530 | * @prefix: the namespace prefix or NULL |
| 1531 | * @URL: the namespace name |
| 1532 | * |
| 1533 | * Pushes a new parser namespace on top of the ns stack |
| 1534 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 1535 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 1536 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1537 | */ |
| 1538 | static int |
| 1539 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 1540 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1541 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 1542 | int i; |
Daniel Veillard | 711b15d | 2012-10-25 19:23:26 +0800 | [diff] [blame] | 1543 | for (i = ctxt->nsNr - 2;i >= 0;i -= 2) { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1544 | if (ctxt->nsTab[i] == prefix) { |
| 1545 | /* in scope */ |
| 1546 | if (ctxt->nsTab[i + 1] == URL) |
| 1547 | return(-2); |
| 1548 | /* out of scope keep it */ |
| 1549 | break; |
| 1550 | } |
| 1551 | } |
| 1552 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1553 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 1554 | ctxt->nsMax = 10; |
| 1555 | ctxt->nsNr = 0; |
| 1556 | ctxt->nsTab = (const xmlChar **) |
| 1557 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 1558 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1559 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1560 | ctxt->nsMax = 0; |
| 1561 | return (-1); |
| 1562 | } |
| 1563 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1564 | const xmlChar ** tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1565 | ctxt->nsMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1566 | tmp = (const xmlChar **) xmlRealloc((char *) ctxt->nsTab, |
| 1567 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 1568 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1569 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1570 | ctxt->nsMax /= 2; |
| 1571 | return (-1); |
| 1572 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1573 | ctxt->nsTab = tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1574 | } |
| 1575 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 1576 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 1577 | return (ctxt->nsNr); |
| 1578 | } |
| 1579 | /** |
| 1580 | * nsPop: |
| 1581 | * @ctxt: an XML parser context |
| 1582 | * @nr: the number to pop |
| 1583 | * |
| 1584 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 1585 | * |
| 1586 | * Returns the number of namespaces removed |
| 1587 | */ |
| 1588 | static int |
| 1589 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 1590 | { |
| 1591 | int i; |
| 1592 | |
| 1593 | if (ctxt->nsTab == NULL) return(0); |
| 1594 | if (ctxt->nsNr < nr) { |
| 1595 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 1596 | nr = ctxt->nsNr; |
| 1597 | } |
| 1598 | if (ctxt->nsNr <= 0) |
| 1599 | return (0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1600 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1601 | for (i = 0;i < nr;i++) { |
| 1602 | ctxt->nsNr--; |
| 1603 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 1604 | } |
| 1605 | return(nr); |
| 1606 | } |
| 1607 | #endif |
| 1608 | |
| 1609 | static int |
| 1610 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 1611 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1612 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1613 | int maxatts; |
| 1614 | |
| 1615 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1616 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1617 | atts = (const xmlChar **) |
| 1618 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1619 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1620 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1621 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 1622 | if (attallocs == NULL) goto mem_error; |
| 1623 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1624 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1625 | } else if (nr + 5 > ctxt->maxatts) { |
| 1626 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1627 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 1628 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1629 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1630 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1631 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 1632 | (maxatts / 5) * sizeof(int)); |
| 1633 | if (attallocs == NULL) goto mem_error; |
| 1634 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1635 | ctxt->maxatts = maxatts; |
| 1636 | } |
| 1637 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1638 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1639 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1640 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1643 | /** |
| 1644 | * inputPush: |
| 1645 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1646 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1647 | * |
| 1648 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1649 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1650 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1651 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1652 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1653 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 1654 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1655 | if ((ctxt == NULL) || (value == NULL)) |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1656 | return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1657 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 1658 | ctxt->inputMax *= 2; |
| 1659 | ctxt->inputTab = |
| 1660 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 1661 | ctxt->inputMax * |
| 1662 | sizeof(ctxt->inputTab[0])); |
| 1663 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1664 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1665 | xmlFreeInputStream(value); |
| 1666 | ctxt->inputMax /= 2; |
| 1667 | value = NULL; |
| 1668 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | ctxt->inputTab[ctxt->inputNr] = value; |
| 1672 | ctxt->input = value; |
| 1673 | return (ctxt->inputNr++); |
| 1674 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1675 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1676 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1677 | * @ctxt: an XML parser context |
| 1678 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1679 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1680 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1681 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1682 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1683 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1684 | inputPop(xmlParserCtxtPtr ctxt) |
| 1685 | { |
| 1686 | xmlParserInputPtr ret; |
| 1687 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1688 | if (ctxt == NULL) |
| 1689 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1690 | if (ctxt->inputNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1691 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1692 | ctxt->inputNr--; |
| 1693 | if (ctxt->inputNr > 0) |
| 1694 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 1695 | else |
| 1696 | ctxt->input = NULL; |
| 1697 | ret = ctxt->inputTab[ctxt->inputNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1698 | ctxt->inputTab[ctxt->inputNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1699 | return (ret); |
| 1700 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1701 | /** |
| 1702 | * nodePush: |
| 1703 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1704 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1705 | * |
| 1706 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1707 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1708 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1709 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1710 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1711 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1712 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1713 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1714 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1715 | xmlNodePtr *tmp; |
| 1716 | |
| 1717 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1718 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1719 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1720 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1721 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1722 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1723 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1724 | ctxt->nodeTab = tmp; |
| 1725 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1726 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1727 | if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) && |
| 1728 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1729 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1730 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1731 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1732 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1733 | return(-1); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1734 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1735 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1736 | ctxt->node = value; |
| 1737 | return (ctxt->nodeNr++); |
| 1738 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1739 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1740 | /** |
| 1741 | * nodePop: |
| 1742 | * @ctxt: an XML parser context |
| 1743 | * |
| 1744 | * Pops the top element node from the node stack |
| 1745 | * |
| 1746 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1747 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1748 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1749 | nodePop(xmlParserCtxtPtr ctxt) |
| 1750 | { |
| 1751 | xmlNodePtr ret; |
| 1752 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1753 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1754 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1755 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1756 | ctxt->nodeNr--; |
| 1757 | if (ctxt->nodeNr > 0) |
| 1758 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1759 | else |
| 1760 | ctxt->node = NULL; |
| 1761 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1762 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1763 | return (ret); |
| 1764 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1765 | |
| 1766 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1767 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1768 | * nameNsPush: |
| 1769 | * @ctxt: an XML parser context |
| 1770 | * @value: the element name |
| 1771 | * @prefix: the element prefix |
| 1772 | * @URI: the element namespace name |
| 1773 | * |
| 1774 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1775 | * |
| 1776 | * Returns -1 in case of error, the index in the stack otherwise |
| 1777 | */ |
| 1778 | static int |
| 1779 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1780 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1781 | { |
| 1782 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1783 | const xmlChar * *tmp; |
| 1784 | void **tmp2; |
| 1785 | ctxt->nameMax *= 2; |
| 1786 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1787 | ctxt->nameMax * |
| 1788 | sizeof(ctxt->nameTab[0])); |
| 1789 | if (tmp == NULL) { |
| 1790 | ctxt->nameMax /= 2; |
| 1791 | goto mem_error; |
| 1792 | } |
| 1793 | ctxt->nameTab = tmp; |
| 1794 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1795 | ctxt->nameMax * 3 * |
| 1796 | sizeof(ctxt->pushTab[0])); |
| 1797 | if (tmp2 == NULL) { |
| 1798 | ctxt->nameMax /= 2; |
| 1799 | goto mem_error; |
| 1800 | } |
| 1801 | ctxt->pushTab = tmp2; |
| 1802 | } |
| 1803 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1804 | ctxt->name = value; |
| 1805 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1806 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1807 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1808 | return (ctxt->nameNr++); |
| 1809 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1810 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1811 | return (-1); |
| 1812 | } |
| 1813 | /** |
| 1814 | * nameNsPop: |
| 1815 | * @ctxt: an XML parser context |
| 1816 | * |
| 1817 | * Pops the top element/prefix/URI name from the name stack |
| 1818 | * |
| 1819 | * Returns the name just removed |
| 1820 | */ |
| 1821 | static const xmlChar * |
| 1822 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1823 | { |
| 1824 | const xmlChar *ret; |
| 1825 | |
| 1826 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1827 | return (NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1828 | ctxt->nameNr--; |
| 1829 | if (ctxt->nameNr > 0) |
| 1830 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1831 | else |
| 1832 | ctxt->name = NULL; |
| 1833 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1834 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1835 | return (ret); |
| 1836 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1837 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1838 | |
| 1839 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1840 | * namePush: |
| 1841 | * @ctxt: an XML parser context |
| 1842 | * @value: the element name |
| 1843 | * |
| 1844 | * Pushes a new element name on top of the name stack |
| 1845 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1846 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1847 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1848 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1849 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1850 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1851 | if (ctxt == NULL) return (-1); |
| 1852 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1853 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1854 | const xmlChar * *tmp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1855 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1856 | ctxt->nameMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1857 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1858 | if (tmp == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1859 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1860 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1861 | ctxt->nameTab = tmp; |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1862 | ctxt->nameMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1863 | } |
| 1864 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1865 | ctxt->name = value; |
| 1866 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1867 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1868 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1869 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1870 | } |
| 1871 | /** |
| 1872 | * namePop: |
| 1873 | * @ctxt: an XML parser context |
| 1874 | * |
| 1875 | * Pops the top element name from the name stack |
| 1876 | * |
| 1877 | * Returns the name just removed |
| 1878 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1879 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1880 | namePop(xmlParserCtxtPtr ctxt) |
| 1881 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1882 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1883 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1884 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1885 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1886 | ctxt->nameNr--; |
| 1887 | if (ctxt->nameNr > 0) |
| 1888 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1889 | else |
| 1890 | ctxt->name = NULL; |
| 1891 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1892 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1893 | return (ret); |
| 1894 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1896 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1897 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1898 | int *tmp; |
| 1899 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1900 | ctxt->spaceMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1901 | tmp = (int *) xmlRealloc(ctxt->spaceTab, |
| 1902 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1903 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1904 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1905 | ctxt->spaceMax /=2; |
| 1906 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1907 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1908 | ctxt->spaceTab = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1909 | } |
| 1910 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1911 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1912 | return(ctxt->spaceNr++); |
| 1913 | } |
| 1914 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1915 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1916 | int ret; |
| 1917 | if (ctxt->spaceNr <= 0) return(0); |
| 1918 | ctxt->spaceNr--; |
| 1919 | if (ctxt->spaceNr > 0) |
| 1920 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1921 | else |
Daniel Veillard | dbcbbd2 | 2006-06-18 19:55:20 +0000 | [diff] [blame] | 1922 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1923 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1924 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1925 | return(ret); |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Macros for accessing the content. Those should be used only by the parser, |
| 1930 | * and not exported. |
| 1931 | * |
| 1932 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1933 | * use them |
| 1934 | * |
| 1935 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1936 | * To be used with extreme caution since operations consuming |
| 1937 | * characters may move the input buffer to a different location ! |
| 1938 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1939 | * This should be used internally by the parser |
| 1940 | * only to compare to ASCII values otherwise it would break when |
| 1941 | * running with UTF-8 encoding. |
| 1942 | * RAW same as CUR but in the input buffer, bypass any token |
| 1943 | * extraction that may have been done |
| 1944 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1945 | * to compare on ASCII based substring. |
| 1946 | * 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] | 1947 | * strings without newlines within the parser. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1948 | * 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] | 1949 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1950 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1951 | * |
| 1952 | * NEXT Skip to the next character, this does the proper decoding |
| 1953 | * 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] | 1954 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1955 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1956 | * to the number of xmlChars used for the encoding [0-5]. |
| 1957 | * CUR_SCHAR same but operate on a string instead of the context |
| 1958 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1959 | * the index |
| 1960 | * GROW, SHRINK handling of input buffers |
| 1961 | */ |
| 1962 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1963 | #define RAW (*ctxt->input->cur) |
| 1964 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1965 | #define NXT(val) ctxt->input->cur[(val)] |
| 1966 | #define CUR_PTR ctxt->input->cur |
| 1967 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1968 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1969 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1970 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1971 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1972 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1973 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1974 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1975 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1976 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1977 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1978 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1979 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1980 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1981 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1982 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1983 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 1984 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 1985 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1987 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1988 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1989 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1990 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1991 | xmlPopInput(ctxt); \ |
| 1992 | } while (0) |
| 1993 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1994 | #define SKIPL(val) do { \ |
| 1995 | int skipl; \ |
| 1996 | for(skipl=0; skipl<val; skipl++) { \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1997 | if (*(ctxt->input->cur) == '\n') { \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1998 | ctxt->input->line++; ctxt->input->col = 1; \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 1999 | } else ctxt->input->col++; \ |
| 2000 | ctxt->nbChars++; \ |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 2001 | ctxt->input->cur++; \ |
| 2002 | } \ |
| 2003 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 2004 | if ((*ctxt->input->cur == 0) && \ |
| 2005 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 2006 | xmlPopInput(ctxt); \ |
| 2007 | } while (0) |
| 2008 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2009 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 2010 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 2011 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2012 | xmlSHRINK (ctxt); |
| 2013 | |
| 2014 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 2015 | xmlParserInputShrink(ctxt->input); |
| 2016 | if ((*ctxt->input->cur == 0) && |
| 2017 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2018 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2019 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2020 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2021 | #define GROW if ((ctxt->progressive == 0) && \ |
| 2022 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2023 | xmlGROW (ctxt); |
| 2024 | |
| 2025 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2026 | if ((((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || |
| 2027 | ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && |
Daniel Veillard | 153cf15 | 2012-10-26 13:50:47 +0800 | [diff] [blame] | 2028 | ((ctxt->input->buf) && (ctxt->input->buf->readcallback != xmlNop)) && |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2029 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 2030 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
Daniel Veillard | 0df83ca | 2012-07-30 15:41:10 +0800 | [diff] [blame] | 2031 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 2032 | } |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2033 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Daniel Veillard | 59df783 | 2010-02-02 10:24:01 +0100 | [diff] [blame] | 2034 | if ((ctxt->input->cur != NULL) && (*ctxt->input->cur == 0) && |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2035 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2036 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2038 | |
| 2039 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 2040 | |
| 2041 | #define NEXT xmlNextChar(ctxt) |
| 2042 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2043 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2044 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2045 | ctxt->input->cur++; \ |
| 2046 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2047 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2048 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 2049 | } |
| 2050 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2051 | #define NEXTL(l) do { \ |
| 2052 | if (*(ctxt->input->cur) == '\n') { \ |
| 2053 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 2054 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2055 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2056 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2057 | } while (0) |
| 2058 | |
| 2059 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 2060 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 2061 | |
| 2062 | #define COPY_BUF(l,b,i,v) \ |
| 2063 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2064 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2065 | |
| 2066 | /** |
| 2067 | * xmlSkipBlankChars: |
| 2068 | * @ctxt: the XML parser context |
| 2069 | * |
| 2070 | * skip all blanks character found at that point in the input streams. |
| 2071 | * It pops up finished entities in the process if allowable at that point. |
| 2072 | * |
| 2073 | * Returns the number of space chars skipped |
| 2074 | */ |
| 2075 | |
| 2076 | int |
| 2077 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2078 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2079 | |
| 2080 | /* |
| 2081 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 2082 | * the ASCII range. |
| 2083 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2084 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 2085 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2086 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2087 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2088 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2089 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2090 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2091 | if (*cur == '\n') { |
| 2092 | ctxt->input->line++; ctxt->input->col = 1; |
| 2093 | } |
| 2094 | cur++; |
| 2095 | res++; |
| 2096 | if (*cur == 0) { |
| 2097 | ctxt->input->cur = cur; |
| 2098 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 2099 | cur = ctxt->input->cur; |
| 2100 | } |
| 2101 | } |
| 2102 | ctxt->input->cur = cur; |
| 2103 | } else { |
| 2104 | int cur; |
| 2105 | do { |
| 2106 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 2107 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2108 | NEXT; |
| 2109 | cur = CUR; |
| 2110 | res++; |
| 2111 | } |
| 2112 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 2113 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 2114 | xmlPopInput(ctxt); |
| 2115 | cur = CUR; |
| 2116 | } |
| 2117 | /* |
| 2118 | * Need to handle support of entities branching here |
| 2119 | */ |
| 2120 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 2121 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 2122 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2123 | return(res); |
| 2124 | } |
| 2125 | |
| 2126 | /************************************************************************ |
| 2127 | * * |
| 2128 | * Commodity functions to handle entities * |
| 2129 | * * |
| 2130 | ************************************************************************/ |
| 2131 | |
| 2132 | /** |
| 2133 | * xmlPopInput: |
| 2134 | * @ctxt: an XML parser context |
| 2135 | * |
| 2136 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 2137 | * pop it and return the next char. |
| 2138 | * |
| 2139 | * Returns the current xmlChar in the parser context |
| 2140 | */ |
| 2141 | xmlChar |
| 2142 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2143 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2144 | if (xmlParserDebugEntities) |
| 2145 | xmlGenericError(xmlGenericErrorContext, |
| 2146 | "Popping input %d\n", ctxt->inputNr); |
| 2147 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2148 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2149 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2150 | return(xmlPopInput(ctxt)); |
| 2151 | return(CUR); |
| 2152 | } |
| 2153 | |
| 2154 | /** |
| 2155 | * xmlPushInput: |
| 2156 | * @ctxt: an XML parser context |
| 2157 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 2158 | * |
| 2159 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 2160 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2161 | * 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] | 2162 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2163 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2164 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2165 | int ret; |
| 2166 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2167 | |
| 2168 | if (xmlParserDebugEntities) { |
| 2169 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 2170 | xmlGenericError(xmlGenericErrorContext, |
| 2171 | "%s(%d): ", ctxt->input->filename, |
| 2172 | ctxt->input->line); |
| 2173 | xmlGenericError(xmlGenericErrorContext, |
| 2174 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 2175 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2176 | ret = inputPush(ctxt, input); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2177 | if (ctxt->instate == XML_PARSER_EOF) |
| 2178 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2179 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2180 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /** |
| 2184 | * xmlParseCharRef: |
| 2185 | * @ctxt: an XML parser context |
| 2186 | * |
| 2187 | * parse Reference declarations |
| 2188 | * |
| 2189 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2190 | * '&#x' [0-9a-fA-F]+ ';' |
| 2191 | * |
| 2192 | * [ WFC: Legal Character ] |
| 2193 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2194 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2195 | * |
| 2196 | * Returns the value parsed (as an int), 0 in case of error |
| 2197 | */ |
| 2198 | int |
| 2199 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2200 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2201 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2202 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2203 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2204 | /* |
| 2205 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2206 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2207 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2208 | (NXT(2) == 'x')) { |
| 2209 | SKIP(3); |
| 2210 | GROW; |
| 2211 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2212 | if (count++ > 20) { |
| 2213 | count = 0; |
| 2214 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2215 | if (ctxt->instate == XML_PARSER_EOF) |
| 2216 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2217 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2218 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2219 | val = val * 16 + (CUR - '0'); |
| 2220 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2221 | val = val * 16 + (CUR - 'a') + 10; |
| 2222 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2223 | val = val * 16 + (CUR - 'A') + 10; |
| 2224 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2225 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2226 | val = 0; |
| 2227 | break; |
| 2228 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2229 | if (val > 0x10FFFF) |
| 2230 | outofrange = val; |
| 2231 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2232 | NEXT; |
| 2233 | count++; |
| 2234 | } |
| 2235 | if (RAW == ';') { |
| 2236 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2237 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2238 | ctxt->nbChars ++; |
| 2239 | ctxt->input->cur++; |
| 2240 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2241 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2242 | SKIP(2); |
| 2243 | GROW; |
| 2244 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2245 | if (count++ > 20) { |
| 2246 | count = 0; |
| 2247 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2248 | if (ctxt->instate == XML_PARSER_EOF) |
| 2249 | return(0); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2250 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2251 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2252 | val = val * 10 + (CUR - '0'); |
| 2253 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2254 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2255 | val = 0; |
| 2256 | break; |
| 2257 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2258 | if (val > 0x10FFFF) |
| 2259 | outofrange = val; |
| 2260 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2261 | NEXT; |
| 2262 | count++; |
| 2263 | } |
| 2264 | if (RAW == ';') { |
| 2265 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2266 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2267 | ctxt->nbChars ++; |
| 2268 | ctxt->input->cur++; |
| 2269 | } |
| 2270 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2271 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | * [ WFC: Legal Character ] |
| 2276 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2277 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2278 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2279 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2280 | return(val); |
| 2281 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2282 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2283 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2284 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2285 | } |
| 2286 | return(0); |
| 2287 | } |
| 2288 | |
| 2289 | /** |
| 2290 | * xmlParseStringCharRef: |
| 2291 | * @ctxt: an XML parser context |
| 2292 | * @str: a pointer to an index in the string |
| 2293 | * |
| 2294 | * parse Reference declarations, variant parsing from a string rather |
| 2295 | * than an an input flow. |
| 2296 | * |
| 2297 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2298 | * '&#x' [0-9a-fA-F]+ ';' |
| 2299 | * |
| 2300 | * [ WFC: Legal Character ] |
| 2301 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2302 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | * |
| 2304 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2305 | * updated to the current value of the index |
| 2306 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2307 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2308 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2309 | const xmlChar *ptr; |
| 2310 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2311 | unsigned int val = 0; |
| 2312 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | |
| 2314 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2315 | ptr = *str; |
| 2316 | cur = *ptr; |
| 2317 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2318 | ptr += 3; |
| 2319 | cur = *ptr; |
| 2320 | while (cur != ';') { /* Non input consuming loop */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2321 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2322 | val = val * 16 + (cur - '0'); |
| 2323 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2324 | val = val * 16 + (cur - 'a') + 10; |
| 2325 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2326 | val = val * 16 + (cur - 'A') + 10; |
| 2327 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2328 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2329 | val = 0; |
| 2330 | break; |
| 2331 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2332 | if (val > 0x10FFFF) |
| 2333 | outofrange = val; |
| 2334 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2335 | ptr++; |
| 2336 | cur = *ptr; |
| 2337 | } |
| 2338 | if (cur == ';') |
| 2339 | ptr++; |
| 2340 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2341 | ptr += 2; |
| 2342 | cur = *ptr; |
| 2343 | while (cur != ';') { /* Non input consuming loops */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2344 | if ((cur >= '0') && (cur <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2345 | val = val * 10 + (cur - '0'); |
| 2346 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2347 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_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 { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2360 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2361 | return(0); |
| 2362 | } |
| 2363 | *str = ptr; |
| 2364 | |
| 2365 | /* |
| 2366 | * [ WFC: Legal Character ] |
| 2367 | * Characters referred to using character references must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2368 | * production for Char. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2369 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2370 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2371 | return(val); |
| 2372 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2373 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2374 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2375 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2376 | } |
| 2377 | return(0); |
| 2378 | } |
| 2379 | |
| 2380 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2381 | * xmlNewBlanksWrapperInputStream: |
| 2382 | * @ctxt: an XML parser context |
| 2383 | * @entity: an Entity pointer |
| 2384 | * |
| 2385 | * Create a new input stream for wrapping |
| 2386 | * blanks around a PEReference |
| 2387 | * |
| 2388 | * Returns the new input stream or NULL |
| 2389 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2390 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2391 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2392 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2393 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2394 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2395 | xmlParserInputPtr input; |
| 2396 | xmlChar *buffer; |
| 2397 | size_t length; |
| 2398 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2399 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2400 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2401 | return(NULL); |
| 2402 | } |
| 2403 | if (xmlParserDebugEntities) |
| 2404 | xmlGenericError(xmlGenericErrorContext, |
| 2405 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2406 | input = xmlNewInputStream(ctxt); |
| 2407 | if (input == NULL) { |
| 2408 | return(NULL); |
| 2409 | } |
| 2410 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2411 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2412 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2413 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2414 | xmlFree(input); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2415 | return(NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2416 | } |
| 2417 | buffer [0] = ' '; |
| 2418 | buffer [1] = '%'; |
| 2419 | buffer [length-3] = ';'; |
| 2420 | buffer [length-2] = ' '; |
| 2421 | buffer [length-1] = 0; |
| 2422 | memcpy(buffer + 2, entity->name, length - 5); |
| 2423 | input->free = deallocblankswrapper; |
| 2424 | input->base = buffer; |
| 2425 | input->cur = buffer; |
| 2426 | input->length = length; |
| 2427 | input->end = &buffer[length]; |
| 2428 | return(input); |
| 2429 | } |
| 2430 | |
| 2431 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | * xmlParserHandlePEReference: |
| 2433 | * @ctxt: the parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2434 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2435 | * [69] PEReference ::= '%' Name ';' |
| 2436 | * |
| 2437 | * [ WFC: No Recursion ] |
| 2438 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2439 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2440 | * |
| 2441 | * [ WFC: Entity Declared ] |
| 2442 | * In a document without any DTD, a document with only an internal DTD |
| 2443 | * subset which contains no parameter entity references, or a document |
| 2444 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2445 | * entity must precede any reference to it... |
| 2446 | * |
| 2447 | * [ VC: Entity Declared ] |
| 2448 | * In a document with an external subset or external parameter entities |
| 2449 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2450 | * must precede any reference to it... |
| 2451 | * |
| 2452 | * [ WFC: In DTD ] |
| 2453 | * Parameter-entity references may only appear in the DTD. |
| 2454 | * NOTE: misleading but this is handled. |
| 2455 | * |
| 2456 | * A PEReference may have been detected in the current input stream |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2457 | * the handling is done accordingly to |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2458 | * http://www.w3.org/TR/REC-xml#entproc |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2459 | * i.e. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2460 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2461 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | */ |
| 2463 | void |
| 2464 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2465 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2466 | xmlEntityPtr entity = NULL; |
| 2467 | xmlParserInputPtr input; |
| 2468 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | if (RAW != '%') return; |
| 2470 | switch(ctxt->instate) { |
| 2471 | case XML_PARSER_CDATA_SECTION: |
| 2472 | return; |
| 2473 | case XML_PARSER_COMMENT: |
| 2474 | return; |
| 2475 | case XML_PARSER_START_TAG: |
| 2476 | return; |
| 2477 | case XML_PARSER_END_TAG: |
| 2478 | return; |
| 2479 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2480 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2481 | return; |
| 2482 | case XML_PARSER_PROLOG: |
| 2483 | case XML_PARSER_START: |
| 2484 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2485 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | case XML_PARSER_ENTITY_DECL: |
| 2488 | case XML_PARSER_CONTENT: |
| 2489 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2490 | case XML_PARSER_PI: |
| 2491 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2492 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2493 | /* we just ignore it there */ |
| 2494 | return; |
| 2495 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2496 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | case XML_PARSER_ENTITY_VALUE: |
| 2499 | /* |
| 2500 | * NOTE: in the case of entity values, we don't do the |
| 2501 | * substitution here since we need the literal |
| 2502 | * entity value to be able to save the internal |
| 2503 | * subset of the document. |
| 2504 | * This will be handled by xmlStringDecodeEntities |
| 2505 | */ |
| 2506 | return; |
| 2507 | case XML_PARSER_DTD: |
| 2508 | /* |
| 2509 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2510 | * In the internal DTD subset, parameter-entity references |
| 2511 | * can occur only where markup declarations can occur, not |
| 2512 | * within markup declarations. |
| 2513 | * In that case this is handled in xmlParseMarkupDecl |
| 2514 | */ |
| 2515 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2516 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2517 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2518 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2519 | break; |
| 2520 | case XML_PARSER_IGNORE: |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | NEXT; |
| 2525 | name = xmlParseName(ctxt); |
| 2526 | if (xmlParserDebugEntities) |
| 2527 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2528 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2529 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2530 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2531 | } else { |
| 2532 | if (RAW == ';') { |
| 2533 | NEXT; |
| 2534 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2535 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 2536 | if (entity == NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2537 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2538 | /* |
| 2539 | * [ WFC: Entity Declared ] |
| 2540 | * In a document without any DTD, a document with only an |
| 2541 | * internal DTD subset which contains no parameter entity |
| 2542 | * references, or a document with "standalone='yes'", ... |
| 2543 | * ... The declaration of a parameter entity must precede |
| 2544 | * any reference to it... |
| 2545 | */ |
| 2546 | if ((ctxt->standalone == 1) || |
| 2547 | ((ctxt->hasExternalSubset == 0) && |
| 2548 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2549 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2550 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2551 | } else { |
| 2552 | /* |
| 2553 | * [ VC: Entity Declared ] |
| 2554 | * In a document with an external subset or external |
| 2555 | * parameter entities with "standalone='no'", ... |
| 2556 | * ... The declaration of a parameter entity must precede |
| 2557 | * any reference to it... |
| 2558 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2559 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2560 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2561 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2562 | name, NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2563 | } else |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2564 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2565 | "PEReference: %%%s; not found\n", |
| 2566 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2567 | ctxt->valid = 0; |
| 2568 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2569 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2570 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2571 | if (xmlPushInput(ctxt, input) < 0) |
| 2572 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2573 | } else { |
| 2574 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2575 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2576 | xmlChar start[4]; |
| 2577 | xmlCharEncoding enc; |
| 2578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2579 | /* |
| 2580 | * handle the extra spaces added before and after |
| 2581 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2582 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | */ |
| 2584 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2585 | if (xmlPushInput(ctxt, input) < 0) |
| 2586 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2587 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2588 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2589 | * Get the 4 first bytes and decode the charset |
| 2590 | * if enc != XML_CHAR_ENCODING_NONE |
| 2591 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2592 | * Note that, since we may have some non-UTF8 |
| 2593 | * encoding (like UTF16, bug 135229), the 'length' |
| 2594 | * is not known, but we can calculate based upon |
| 2595 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2596 | */ |
| 2597 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 2598 | if (ctxt->instate == XML_PARSER_EOF) |
| 2599 | return; |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2600 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2601 | start[0] = RAW; |
| 2602 | start[1] = NXT(1); |
| 2603 | start[2] = NXT(2); |
| 2604 | start[3] = NXT(3); |
| 2605 | enc = xmlDetectCharEncoding(start, 4); |
| 2606 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2607 | xmlSwitchEncoding(ctxt, enc); |
| 2608 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2612 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2613 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2614 | xmlParseTextDecl(ctxt); |
| 2615 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2616 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2617 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2618 | "PEReference: %s is not a parameter entity\n", |
| 2619 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2623 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2624 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | /* |
| 2629 | * Macro used to grow the current buffer. |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2630 | * buffer##_size is expected to be a size_t |
| 2631 | * mem_error: is expected to handle memory allocation failures |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2632 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2633 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2634 | xmlChar *tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2635 | size_t new_size = buffer##_size * 2 + n; \ |
| 2636 | if (new_size < buffer##_size) goto mem_error; \ |
| 2637 | tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2638 | if (tmp == NULL) goto mem_error; \ |
| 2639 | buffer = tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2640 | buffer##_size = new_size; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2644 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2645 | * @ctxt: the parser context |
| 2646 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2647 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2648 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2649 | * @end: an end marker xmlChar, 0 if none |
| 2650 | * @end2: an end marker xmlChar, 0 if none |
| 2651 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2652 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2653 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2654 | * |
| 2655 | * [67] Reference ::= EntityRef | CharRef |
| 2656 | * |
| 2657 | * [69] PEReference ::= '%' Name ';' |
| 2658 | * |
| 2659 | * Returns A newly allocated string with the substitution done. The caller |
| 2660 | * must deallocate it ! |
| 2661 | */ |
| 2662 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2663 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2664 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2665 | xmlChar *buffer = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2666 | size_t buffer_size = 0; |
| 2667 | size_t nbchars = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | |
| 2669 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2670 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2671 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2672 | xmlEntityPtr ent; |
| 2673 | int c,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2675 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2676 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2677 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2678 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2679 | if (((ctxt->depth > 40) && |
| 2680 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2681 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2682 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2683 | return(NULL); |
| 2684 | } |
| 2685 | |
| 2686 | /* |
| 2687 | * allocate a translation buffer. |
| 2688 | */ |
| 2689 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2690 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2691 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2692 | |
| 2693 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2694 | * 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] | 2695 | * we are operating on already parsed values. |
| 2696 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2697 | if (str < last) |
| 2698 | c = CUR_SCHAR(str, l); |
| 2699 | else |
| 2700 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2701 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2702 | (c != end2) && (c != end3)) { |
| 2703 | |
| 2704 | if (c == 0) break; |
| 2705 | if ((c == '&') && (str[1] == '#')) { |
| 2706 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2707 | if (val != 0) { |
| 2708 | COPY_BUF(0,buffer,nbchars,val); |
| 2709 | } |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2710 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2711 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2712 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2713 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2714 | if (xmlParserDebugEntities) |
| 2715 | xmlGenericError(xmlGenericErrorContext, |
| 2716 | "String decoding Entity Reference: %.30s\n", |
| 2717 | str); |
| 2718 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2719 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2720 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2721 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2722 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2723 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2724 | if ((ent != NULL) && |
| 2725 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2726 | if (ent->content != NULL) { |
| 2727 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2728 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2729 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2730 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2731 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2732 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2733 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2734 | } |
| 2735 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2736 | ctxt->depth++; |
| 2737 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2738 | 0, 0, 0); |
| 2739 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2740 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | if (rep != NULL) { |
| 2742 | current = rep; |
| 2743 | while (*current != 0) { /* non input consuming loop */ |
| 2744 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2745 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2746 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2747 | goto int_error; |
| 2748 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2752 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2753 | } |
| 2754 | } else if (ent != NULL) { |
| 2755 | int i = xmlStrlen(ent->name); |
| 2756 | const xmlChar *cur = ent->name; |
| 2757 | |
| 2758 | buffer[nbchars++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2759 | if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 5bd3c06 | 2011-12-16 18:53:35 +0800 | [diff] [blame] | 2760 | growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2761 | } |
| 2762 | for (;i > 0;i--) |
| 2763 | buffer[nbchars++] = *cur++; |
| 2764 | buffer[nbchars++] = ';'; |
| 2765 | } |
| 2766 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2767 | if (xmlParserDebugEntities) |
| 2768 | xmlGenericError(xmlGenericErrorContext, |
| 2769 | "String decoding PE Reference: %.30s\n", str); |
| 2770 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2771 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2772 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2773 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2774 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2775 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2776 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2777 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2779 | ctxt->depth++; |
| 2780 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2781 | 0, 0, 0); |
| 2782 | ctxt->depth--; |
| 2783 | if (rep != NULL) { |
| 2784 | current = rep; |
| 2785 | while (*current != 0) { /* non input consuming loop */ |
| 2786 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2787 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2788 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2789 | goto int_error; |
| 2790 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2794 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2795 | } |
| 2796 | } |
| 2797 | } else { |
| 2798 | COPY_BUF(l,buffer,nbchars,c); |
| 2799 | str += l; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2800 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
| 2801 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2802 | } |
| 2803 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2804 | if (str < last) |
| 2805 | c = CUR_SCHAR(str, l); |
| 2806 | else |
| 2807 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2808 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2809 | buffer[nbchars] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2810 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2811 | |
| 2812 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2813 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2814 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2815 | if (rep != NULL) |
| 2816 | xmlFree(rep); |
| 2817 | if (buffer != NULL) |
| 2818 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2819 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2822 | /** |
| 2823 | * xmlStringDecodeEntities: |
| 2824 | * @ctxt: the parser context |
| 2825 | * @str: the input string |
| 2826 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2827 | * @end: an end marker xmlChar, 0 if none |
| 2828 | * @end2: an end marker xmlChar, 0 if none |
| 2829 | * @end3: an end marker xmlChar, 0 if none |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 2830 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2831 | * Takes a entity string content and process to do the adequate substitutions. |
| 2832 | * |
| 2833 | * [67] Reference ::= EntityRef | CharRef |
| 2834 | * |
| 2835 | * [69] PEReference ::= '%' Name ';' |
| 2836 | * |
| 2837 | * Returns A newly allocated string with the substitution done. The caller |
| 2838 | * must deallocate it ! |
| 2839 | */ |
| 2840 | xmlChar * |
| 2841 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2842 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2843 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2844 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2845 | end, end2, end3)); |
| 2846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2847 | |
| 2848 | /************************************************************************ |
| 2849 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2850 | * Commodity functions, cleanup needed ? * |
| 2851 | * * |
| 2852 | ************************************************************************/ |
| 2853 | |
| 2854 | /** |
| 2855 | * areBlanks: |
| 2856 | * @ctxt: an XML parser context |
| 2857 | * @str: a xmlChar * |
| 2858 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2859 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2860 | * |
| 2861 | * Is this a sequence of blank chars that one can ignore ? |
| 2862 | * |
| 2863 | * Returns 1 if ignorable 0 otherwise. |
| 2864 | */ |
| 2865 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2866 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2867 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2868 | int i, ret; |
| 2869 | xmlNodePtr lastChild; |
| 2870 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2871 | /* |
| 2872 | * Don't spend time trying to differentiate them, the same callback is |
| 2873 | * used ! |
| 2874 | */ |
| 2875 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2876 | return(0); |
| 2877 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2878 | /* |
| 2879 | * Check for xml:space value. |
| 2880 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2881 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2882 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2883 | return(0); |
| 2884 | |
| 2885 | /* |
| 2886 | * Check that the string is made of blanks |
| 2887 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2888 | if (blank_chars == 0) { |
| 2889 | for (i = 0;i < len;i++) |
| 2890 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2891 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2892 | |
| 2893 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2894 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2895 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2896 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2897 | if (ctxt->myDoc != NULL) { |
| 2898 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2899 | if (ret == 0) return(1); |
| 2900 | if (ret == 1) return(0); |
| 2901 | } |
| 2902 | |
| 2903 | /* |
| 2904 | * Otherwise, heuristic :-\ |
| 2905 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2906 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2907 | if ((ctxt->node->children == NULL) && |
| 2908 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2909 | |
| 2910 | lastChild = xmlGetLastChild(ctxt->node); |
| 2911 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2912 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2913 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2914 | } else if (xmlNodeIsText(lastChild)) |
| 2915 | return(0); |
| 2916 | else if ((ctxt->node->children != NULL) && |
| 2917 | (xmlNodeIsText(ctxt->node->children))) |
| 2918 | return(0); |
| 2919 | return(1); |
| 2920 | } |
| 2921 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2922 | /************************************************************************ |
| 2923 | * * |
| 2924 | * Extra stuff for namespace support * |
| 2925 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2926 | * * |
| 2927 | ************************************************************************/ |
| 2928 | |
| 2929 | /** |
| 2930 | * xmlSplitQName: |
| 2931 | * @ctxt: an XML parser context |
| 2932 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2933 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2934 | * |
| 2935 | * parse an UTF8 encoded XML qualified name string |
| 2936 | * |
| 2937 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2938 | * |
| 2939 | * [NS 6] Prefix ::= NCName |
| 2940 | * |
| 2941 | * [NS 7] LocalPart ::= NCName |
| 2942 | * |
| 2943 | * Returns the local part, and prefix is updated |
| 2944 | * to get the Prefix if any. |
| 2945 | */ |
| 2946 | |
| 2947 | xmlChar * |
| 2948 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2949 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2950 | xmlChar *buffer = NULL; |
| 2951 | int len = 0; |
| 2952 | int max = XML_MAX_NAMELEN; |
| 2953 | xmlChar *ret = NULL; |
| 2954 | const xmlChar *cur = name; |
| 2955 | int c; |
| 2956 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2957 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2958 | *prefix = NULL; |
| 2959 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2960 | if (cur == NULL) return(NULL); |
| 2961 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2962 | #ifndef XML_XML_NAMESPACE |
| 2963 | /* xml: prefix is not really a namespace */ |
| 2964 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2965 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2966 | return(xmlStrdup(name)); |
| 2967 | #endif |
| 2968 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2969 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2970 | if (cur[0] == ':') |
| 2971 | return(xmlStrdup(name)); |
| 2972 | |
| 2973 | c = *cur++; |
| 2974 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2975 | buf[len++] = c; |
| 2976 | c = *cur++; |
| 2977 | } |
| 2978 | if (len >= max) { |
| 2979 | /* |
| 2980 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2981 | * for the processing speed. |
| 2982 | */ |
| 2983 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2984 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2985 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2986 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2987 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2988 | return(NULL); |
| 2989 | } |
| 2990 | memcpy(buffer, buf, len); |
| 2991 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2992 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2993 | xmlChar *tmp; |
| 2994 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2995 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2996 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2997 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2998 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2999 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3000 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3001 | return(NULL); |
| 3002 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3003 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3004 | } |
| 3005 | buffer[len++] = c; |
| 3006 | c = *cur++; |
| 3007 | } |
| 3008 | buffer[len] = 0; |
| 3009 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 3010 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3011 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3012 | if (buffer != NULL) |
| 3013 | xmlFree(buffer); |
| 3014 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3015 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 3016 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3017 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3018 | if (buffer == NULL) |
| 3019 | ret = xmlStrndup(buf, len); |
| 3020 | else { |
| 3021 | ret = buffer; |
| 3022 | buffer = NULL; |
| 3023 | max = XML_MAX_NAMELEN; |
| 3024 | } |
| 3025 | |
| 3026 | |
| 3027 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3028 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3029 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3030 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 3031 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 3032 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3033 | len = 0; |
| 3034 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3035 | /* |
| 3036 | * Check that the first character is proper to start |
| 3037 | * a new name |
| 3038 | */ |
| 3039 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 3040 | ((c >= 0x41) && (c <= 0x5A)) || |
| 3041 | (c == '_') || (c == ':'))) { |
| 3042 | int l; |
| 3043 | int first = CUR_SCHAR(cur, l); |
| 3044 | |
| 3045 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3046 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3047 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3048 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3049 | } |
| 3050 | } |
| 3051 | cur++; |
| 3052 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3053 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 3054 | buf[len++] = c; |
| 3055 | c = *cur++; |
| 3056 | } |
| 3057 | if (len >= max) { |
| 3058 | /* |
| 3059 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3060 | * for the processing speed. |
| 3061 | */ |
| 3062 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3063 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3064 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3065 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3066 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | return(NULL); |
| 3068 | } |
| 3069 | memcpy(buffer, buf, len); |
| 3070 | while (c != 0) { /* tested bigname2.xml */ |
| 3071 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3072 | xmlChar *tmp; |
| 3073 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3074 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3075 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3076 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3077 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3078 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3079 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3080 | return(NULL); |
| 3081 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3082 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3083 | } |
| 3084 | buffer[len++] = c; |
| 3085 | c = *cur++; |
| 3086 | } |
| 3087 | buffer[len] = 0; |
| 3088 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3089 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3090 | if (buffer == NULL) |
| 3091 | ret = xmlStrndup(buf, len); |
| 3092 | else { |
| 3093 | ret = buffer; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | return(ret); |
| 3098 | } |
| 3099 | |
| 3100 | /************************************************************************ |
| 3101 | * * |
| 3102 | * The parser itself * |
| 3103 | * Relates to http://www.w3.org/TR/REC-xml * |
| 3104 | * * |
| 3105 | ************************************************************************/ |
| 3106 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3107 | /************************************************************************ |
| 3108 | * * |
| 3109 | * Routines to parse Name, NCName and NmToken * |
| 3110 | * * |
| 3111 | ************************************************************************/ |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3112 | #ifdef DEBUG |
| 3113 | static unsigned long nbParseName = 0; |
| 3114 | static unsigned long nbParseNmToken = 0; |
| 3115 | static unsigned long nbParseNCName = 0; |
| 3116 | static unsigned long nbParseNCNameComplex = 0; |
| 3117 | static unsigned long nbParseNameComplex = 0; |
| 3118 | static unsigned long nbParseStringName = 0; |
| 3119 | #endif |
| 3120 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3121 | /* |
| 3122 | * The two following functions are related to the change of accepted |
| 3123 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 3124 | * They correspond to the modified production [4] and the new production [4a] |
| 3125 | * changes in that revision. Also note that the macros used for the |
| 3126 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 3127 | * anymore. |
| 3128 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 3129 | * new XML_PARSE_OLD10 option is given to the parser. |
| 3130 | */ |
| 3131 | static int |
| 3132 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 3133 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3134 | /* |
| 3135 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3136 | * Update 5 of XML-1.0 |
| 3137 | */ |
| 3138 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3139 | (((c >= 'a') && (c <= 'z')) || |
| 3140 | ((c >= 'A') && (c <= 'Z')) || |
| 3141 | (c == '_') || (c == ':') || |
| 3142 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3143 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3144 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3145 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3146 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3147 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3148 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3149 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3150 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3151 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3152 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3153 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3154 | return(1); |
| 3155 | } else { |
| 3156 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3157 | return(1); |
| 3158 | } |
| 3159 | return(0); |
| 3160 | } |
| 3161 | |
| 3162 | static int |
| 3163 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 3164 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3165 | /* |
| 3166 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3167 | * Update 5 of XML-1.0 |
| 3168 | */ |
| 3169 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3170 | (((c >= 'a') && (c <= 'z')) || |
| 3171 | ((c >= 'A') && (c <= 'Z')) || |
| 3172 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3173 | (c == '_') || (c == ':') || |
| 3174 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3175 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3176 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3177 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3178 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3179 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3180 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3181 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3182 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3183 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3184 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3185 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3186 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3187 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3188 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3189 | return(1); |
| 3190 | } else { |
| 3191 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3192 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3193 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3194 | (IS_COMBINING(c)) || |
| 3195 | (IS_EXTENDER(c))) |
| 3196 | return(1); |
| 3197 | } |
| 3198 | return(0); |
| 3199 | } |
| 3200 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3201 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3202 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3203 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3204 | static const xmlChar * |
| 3205 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3206 | int len = 0, l; |
| 3207 | int c; |
| 3208 | int count = 0; |
| 3209 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3210 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3211 | nbParseNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3212 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3213 | |
| 3214 | /* |
| 3215 | * Handler for more complex cases |
| 3216 | */ |
| 3217 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3218 | if (ctxt->instate == XML_PARSER_EOF) |
| 3219 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3220 | c = CUR_CHAR(l); |
| 3221 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3222 | /* |
| 3223 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3224 | * Update 5 of XML-1.0 |
| 3225 | */ |
| 3226 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3227 | (!(((c >= 'a') && (c <= 'z')) || |
| 3228 | ((c >= 'A') && (c <= 'Z')) || |
| 3229 | (c == '_') || (c == ':') || |
| 3230 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3231 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3232 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3233 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3234 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3235 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3236 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3237 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3238 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3239 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3240 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3241 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3242 | return(NULL); |
| 3243 | } |
| 3244 | len += l; |
| 3245 | NEXTL(l); |
| 3246 | c = CUR_CHAR(l); |
| 3247 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3248 | (((c >= 'a') && (c <= 'z')) || |
| 3249 | ((c >= 'A') && (c <= 'Z')) || |
| 3250 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3251 | (c == '_') || (c == ':') || |
| 3252 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3253 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3254 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3255 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3256 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3257 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3258 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3259 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3260 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3261 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3262 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3263 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3264 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3265 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3266 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3267 | )) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3268 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3269 | count = 0; |
| 3270 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3271 | if (ctxt->instate == XML_PARSER_EOF) |
| 3272 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3273 | } |
| 3274 | len += l; |
| 3275 | NEXTL(l); |
| 3276 | c = CUR_CHAR(l); |
| 3277 | } |
| 3278 | } else { |
| 3279 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3280 | (!IS_LETTER(c) && (c != '_') && |
| 3281 | (c != ':'))) { |
| 3282 | return(NULL); |
| 3283 | } |
| 3284 | len += l; |
| 3285 | NEXTL(l); |
| 3286 | c = CUR_CHAR(l); |
| 3287 | |
| 3288 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3289 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3290 | (c == '.') || (c == '-') || |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3291 | (c == '_') || (c == ':') || |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3292 | (IS_COMBINING(c)) || |
| 3293 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3294 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3295 | count = 0; |
| 3296 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3297 | if (ctxt->instate == XML_PARSER_EOF) |
| 3298 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3299 | } |
| 3300 | len += l; |
| 3301 | NEXTL(l); |
| 3302 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3303 | if (c == 0) { |
| 3304 | count = 0; |
| 3305 | GROW; |
| 3306 | if (ctxt->instate == XML_PARSER_EOF) |
| 3307 | return(NULL); |
| 3308 | c = CUR_CHAR(l); |
| 3309 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3310 | } |
| 3311 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3312 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3313 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3314 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3315 | return(NULL); |
| 3316 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3317 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3318 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3319 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3320 | } |
| 3321 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3322 | /** |
| 3323 | * xmlParseName: |
| 3324 | * @ctxt: an XML parser context |
| 3325 | * |
| 3326 | * parse an XML name. |
| 3327 | * |
| 3328 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3329 | * CombiningChar | Extender |
| 3330 | * |
| 3331 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3332 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3333 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3334 | * |
| 3335 | * Returns the Name parsed or NULL |
| 3336 | */ |
| 3337 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3338 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3339 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3340 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3341 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3342 | int count = 0; |
| 3343 | |
| 3344 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3345 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3346 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3347 | nbParseName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3348 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3349 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3350 | /* |
| 3351 | * Accelerator for simple ASCII names |
| 3352 | */ |
| 3353 | in = ctxt->input->cur; |
| 3354 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3355 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3356 | (*in == '_') || (*in == ':')) { |
| 3357 | in++; |
| 3358 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3359 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3360 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3361 | (*in == '_') || (*in == '-') || |
| 3362 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3363 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3364 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3365 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3366 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3367 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3368 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Name"); |
| 3369 | return(NULL); |
| 3370 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3371 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3372 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3373 | ctxt->nbChars += count; |
| 3374 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3375 | if (ret == NULL) |
| 3376 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3377 | return(ret); |
| 3378 | } |
| 3379 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3380 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3381 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3382 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3383 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3384 | static const xmlChar * |
| 3385 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3386 | int len = 0, l; |
| 3387 | int c; |
| 3388 | int count = 0; |
| 3389 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3390 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3391 | nbParseNCNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3392 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3393 | |
| 3394 | /* |
| 3395 | * Handler for more complex cases |
| 3396 | */ |
| 3397 | GROW; |
| 3398 | c = CUR_CHAR(l); |
| 3399 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3400 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3401 | return(NULL); |
| 3402 | } |
| 3403 | |
| 3404 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3405 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3406 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3407 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3408 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3409 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3410 | return(NULL); |
| 3411 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3412 | count = 0; |
| 3413 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3414 | if (ctxt->instate == XML_PARSER_EOF) |
| 3415 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3416 | } |
| 3417 | len += l; |
| 3418 | NEXTL(l); |
| 3419 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3420 | if (c == 0) { |
| 3421 | count = 0; |
| 3422 | GROW; |
| 3423 | if (ctxt->instate == XML_PARSER_EOF) |
| 3424 | return(NULL); |
| 3425 | c = CUR_CHAR(l); |
| 3426 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3427 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3428 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3429 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3430 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3431 | return(NULL); |
| 3432 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3433 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3434 | } |
| 3435 | |
| 3436 | /** |
| 3437 | * xmlParseNCName: |
| 3438 | * @ctxt: an XML parser context |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3439 | * @len: length of the string parsed |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3440 | * |
| 3441 | * parse an XML name. |
| 3442 | * |
| 3443 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3444 | * CombiningChar | Extender |
| 3445 | * |
| 3446 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3447 | * |
| 3448 | * Returns the Name parsed or NULL |
| 3449 | */ |
| 3450 | |
| 3451 | static const xmlChar * |
| 3452 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 3453 | const xmlChar *in; |
| 3454 | const xmlChar *ret; |
| 3455 | int count = 0; |
| 3456 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3457 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3458 | nbParseNCName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3459 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3460 | |
| 3461 | /* |
| 3462 | * Accelerator for simple ASCII names |
| 3463 | */ |
| 3464 | in = ctxt->input->cur; |
| 3465 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3466 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3467 | (*in == '_')) { |
| 3468 | in++; |
| 3469 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3470 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3471 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3472 | (*in == '_') || (*in == '-') || |
| 3473 | (*in == '.')) |
| 3474 | in++; |
| 3475 | if ((*in > 0) && (*in < 0x80)) { |
| 3476 | count = in - ctxt->input->cur; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3477 | if ((count > XML_MAX_NAME_LENGTH) && |
| 3478 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3479 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3480 | return(NULL); |
| 3481 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3482 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3483 | ctxt->input->cur = in; |
| 3484 | ctxt->nbChars += count; |
| 3485 | ctxt->input->col += count; |
| 3486 | if (ret == NULL) { |
| 3487 | xmlErrMemory(ctxt, NULL); |
| 3488 | } |
| 3489 | return(ret); |
| 3490 | } |
| 3491 | } |
| 3492 | return(xmlParseNCNameComplex(ctxt)); |
| 3493 | } |
| 3494 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3495 | /** |
| 3496 | * xmlParseNameAndCompare: |
| 3497 | * @ctxt: an XML parser context |
| 3498 | * |
| 3499 | * parse an XML name and compares for match |
| 3500 | * (specialized for endtag parsing) |
| 3501 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3502 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3503 | * and the name for mismatch |
| 3504 | */ |
| 3505 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3506 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3507 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3508 | register const xmlChar *cmp = other; |
| 3509 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3510 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3511 | |
| 3512 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3513 | if (ctxt->instate == XML_PARSER_EOF) |
| 3514 | return(NULL); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3515 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3516 | in = ctxt->input->cur; |
| 3517 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3518 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3519 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3520 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3521 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3522 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3523 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3524 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3525 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3526 | } |
| 3527 | /* failure (or end of input buffer), check with full function */ |
| 3528 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3529 | /* strings coming from the dictionnary direct compare possible */ |
| 3530 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3531 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3532 | } |
| 3533 | return ret; |
| 3534 | } |
| 3535 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | /** |
| 3537 | * xmlParseStringName: |
| 3538 | * @ctxt: an XML parser context |
| 3539 | * @str: a pointer to the string pointer (IN/OUT) |
| 3540 | * |
| 3541 | * parse an XML name. |
| 3542 | * |
| 3543 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3544 | * CombiningChar | Extender |
| 3545 | * |
| 3546 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3547 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3548 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3549 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3550 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3551 | * is updated to the current location in the string. |
| 3552 | */ |
| 3553 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3554 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3555 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3556 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3557 | const xmlChar *cur = *str; |
| 3558 | int len = 0, l; |
| 3559 | int c; |
| 3560 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3561 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3562 | nbParseStringName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3563 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3564 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3565 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3566 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3567 | return(NULL); |
| 3568 | } |
| 3569 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3570 | COPY_BUF(l,buf,len,c); |
| 3571 | cur += l; |
| 3572 | c = CUR_SCHAR(cur, l); |
| 3573 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3574 | COPY_BUF(l,buf,len,c); |
| 3575 | cur += l; |
| 3576 | c = CUR_SCHAR(cur, l); |
| 3577 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3578 | /* |
| 3579 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3580 | * for the processing speed. |
| 3581 | */ |
| 3582 | xmlChar *buffer; |
| 3583 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3584 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3585 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3586 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3587 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3588 | return(NULL); |
| 3589 | } |
| 3590 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3591 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3592 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3593 | xmlChar *tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3594 | |
| 3595 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3596 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3597 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3598 | xmlFree(buffer); |
| 3599 | return(NULL); |
| 3600 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3601 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3602 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3603 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3604 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3605 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3606 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3607 | return(NULL); |
| 3608 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3609 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3610 | } |
| 3611 | COPY_BUF(l,buffer,len,c); |
| 3612 | cur += l; |
| 3613 | c = CUR_SCHAR(cur, l); |
| 3614 | } |
| 3615 | buffer[len] = 0; |
| 3616 | *str = cur; |
| 3617 | return(buffer); |
| 3618 | } |
| 3619 | } |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3620 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3621 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3622 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NCName"); |
| 3623 | return(NULL); |
| 3624 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3625 | *str = cur; |
| 3626 | return(xmlStrndup(buf, len)); |
| 3627 | } |
| 3628 | |
| 3629 | /** |
| 3630 | * xmlParseNmtoken: |
| 3631 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3632 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3633 | * parse an XML Nmtoken. |
| 3634 | * |
| 3635 | * [7] Nmtoken ::= (NameChar)+ |
| 3636 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3637 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3638 | * |
| 3639 | * Returns the Nmtoken parsed or NULL |
| 3640 | */ |
| 3641 | |
| 3642 | xmlChar * |
| 3643 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3644 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3645 | int len = 0, l; |
| 3646 | int c; |
| 3647 | int count = 0; |
| 3648 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3649 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3650 | nbParseNmToken++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3651 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3652 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3653 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3654 | if (ctxt->instate == XML_PARSER_EOF) |
| 3655 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3656 | c = CUR_CHAR(l); |
| 3657 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3658 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3659 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3660 | count = 0; |
| 3661 | GROW; |
| 3662 | } |
| 3663 | COPY_BUF(l,buf,len,c); |
| 3664 | NEXTL(l); |
| 3665 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3666 | if (c == 0) { |
| 3667 | count = 0; |
| 3668 | GROW; |
| 3669 | if (ctxt->instate == XML_PARSER_EOF) |
| 3670 | return(NULL); |
| 3671 | c = CUR_CHAR(l); |
| 3672 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3673 | if (len >= XML_MAX_NAMELEN) { |
| 3674 | /* |
| 3675 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3676 | * for the processing speed. |
| 3677 | */ |
| 3678 | xmlChar *buffer; |
| 3679 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3680 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3681 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3682 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3683 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | return(NULL); |
| 3685 | } |
| 3686 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3687 | while (xmlIsNameChar(ctxt, c)) { |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 3688 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3689 | count = 0; |
| 3690 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3691 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3692 | xmlFree(buffer); |
| 3693 | return(NULL); |
| 3694 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3695 | } |
| 3696 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3697 | xmlChar *tmp; |
| 3698 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3699 | if ((max > XML_MAX_NAME_LENGTH) && |
| 3700 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3701 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3702 | xmlFree(buffer); |
| 3703 | return(NULL); |
| 3704 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3705 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3706 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3707 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3708 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3709 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3710 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3711 | return(NULL); |
| 3712 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3713 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3714 | } |
| 3715 | COPY_BUF(l,buffer,len,c); |
| 3716 | NEXTL(l); |
| 3717 | c = CUR_CHAR(l); |
| 3718 | } |
| 3719 | buffer[len] = 0; |
| 3720 | return(buffer); |
| 3721 | } |
| 3722 | } |
| 3723 | if (len == 0) |
| 3724 | return(NULL); |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 3725 | if ((len > XML_MAX_NAME_LENGTH) && |
| 3726 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3727 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "NmToken"); |
| 3728 | return(NULL); |
| 3729 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3730 | return(xmlStrndup(buf, len)); |
| 3731 | } |
| 3732 | |
| 3733 | /** |
| 3734 | * xmlParseEntityValue: |
| 3735 | * @ctxt: an XML parser context |
| 3736 | * @orig: if non-NULL store a copy of the original entity value |
| 3737 | * |
| 3738 | * parse a value for ENTITY declarations |
| 3739 | * |
| 3740 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3741 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3742 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3743 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3744 | */ |
| 3745 | |
| 3746 | xmlChar * |
| 3747 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3748 | xmlChar *buf = NULL; |
| 3749 | int len = 0; |
| 3750 | int size = XML_PARSER_BUFFER_SIZE; |
| 3751 | int c, l; |
| 3752 | xmlChar stop; |
| 3753 | xmlChar *ret = NULL; |
| 3754 | const xmlChar *cur = NULL; |
| 3755 | xmlParserInputPtr input; |
| 3756 | |
| 3757 | if (RAW == '"') stop = '"'; |
| 3758 | else if (RAW == '\'') stop = '\''; |
| 3759 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3760 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3761 | return(NULL); |
| 3762 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3763 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3764 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3765 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3766 | return(NULL); |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * The content of the entity definition is copied in a buffer. |
| 3771 | */ |
| 3772 | |
| 3773 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3774 | input = ctxt->input; |
| 3775 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3776 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3777 | xmlFree(buf); |
| 3778 | return(NULL); |
| 3779 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3780 | NEXT; |
| 3781 | c = CUR_CHAR(l); |
| 3782 | /* |
| 3783 | * NOTE: 4.4.5 Included in Literal |
| 3784 | * When a parameter entity reference appears in a literal entity |
| 3785 | * value, ... a single or double quote character in the replacement |
| 3786 | * text is always treated as a normal data character and will not |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3787 | * terminate the literal. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3788 | * In practice it means we stop the loop only when back at parsing |
| 3789 | * the initial entity and the quote is found |
| 3790 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3791 | while (((IS_CHAR(c)) && ((c != stop) || /* checked */ |
| 3792 | (ctxt->input != input))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3793 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3794 | xmlChar *tmp; |
| 3795 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3796 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3797 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3798 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3799 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3800 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3801 | return(NULL); |
| 3802 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3803 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3804 | } |
| 3805 | COPY_BUF(l,buf,len,c); |
| 3806 | NEXTL(l); |
| 3807 | /* |
| 3808 | * Pop-up of finished entities. |
| 3809 | */ |
| 3810 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3811 | xmlPopInput(ctxt); |
| 3812 | |
| 3813 | GROW; |
| 3814 | c = CUR_CHAR(l); |
| 3815 | if (c == 0) { |
| 3816 | GROW; |
| 3817 | c = CUR_CHAR(l); |
| 3818 | } |
| 3819 | } |
| 3820 | buf[len] = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3821 | if (ctxt->instate == XML_PARSER_EOF) { |
| 3822 | xmlFree(buf); |
| 3823 | return(NULL); |
| 3824 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* |
| 3827 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3828 | * reference constructs. Note Charref will be handled in |
| 3829 | * xmlStringDecodeEntities() |
| 3830 | */ |
| 3831 | cur = buf; |
| 3832 | while (*cur != 0) { /* non input consuming */ |
| 3833 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3834 | xmlChar *name; |
| 3835 | xmlChar tmp = *cur; |
| 3836 | |
| 3837 | cur++; |
| 3838 | name = xmlParseStringName(ctxt, &cur); |
| 3839 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3840 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3841 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3842 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3843 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3844 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3845 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3846 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3847 | } |
| 3848 | if (name != NULL) |
| 3849 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3850 | if (*cur == 0) |
| 3851 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3852 | } |
| 3853 | cur++; |
| 3854 | } |
| 3855 | |
| 3856 | /* |
| 3857 | * Then PEReference entities are substituted. |
| 3858 | */ |
| 3859 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3860 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | xmlFree(buf); |
| 3862 | } else { |
| 3863 | NEXT; |
| 3864 | /* |
| 3865 | * NOTE: 4.4.7 Bypassed |
| 3866 | * When a general entity reference appears in the EntityValue in |
| 3867 | * an entity declaration, it is bypassed and left as is. |
| 3868 | * so XML_SUBSTITUTE_REF is not set here. |
| 3869 | */ |
| 3870 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3871 | 0, 0, 0); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3872 | if (orig != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3873 | *orig = buf; |
| 3874 | else |
| 3875 | xmlFree(buf); |
| 3876 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3877 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3878 | return(ret); |
| 3879 | } |
| 3880 | |
| 3881 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3882 | * xmlParseAttValueComplex: |
| 3883 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3884 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3885 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3886 | * |
| 3887 | * parse a value for an attribute, this is the fallback function |
| 3888 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3889 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3890 | * |
| 3891 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3892 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3893 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3894 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3895 | xmlChar limit = 0; |
| 3896 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3897 | xmlChar *rep = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3898 | size_t len = 0; |
| 3899 | size_t buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3900 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3901 | xmlChar *current = NULL; |
| 3902 | xmlEntityPtr ent; |
| 3903 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3904 | if (NXT(0) == '"') { |
| 3905 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3906 | limit = '"'; |
| 3907 | NEXT; |
| 3908 | } else if (NXT(0) == '\'') { |
| 3909 | limit = '\''; |
| 3910 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3911 | NEXT; |
| 3912 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3913 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3914 | return(NULL); |
| 3915 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3916 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3917 | /* |
| 3918 | * allocate a translation buffer. |
| 3919 | */ |
| 3920 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3921 | buf = (xmlChar *) xmlMallocAtomic(buf_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3922 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3923 | |
| 3924 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3925 | * 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] | 3926 | */ |
| 3927 | c = CUR_CHAR(l); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 3928 | while (((NXT(0) != limit) && /* checked */ |
| 3929 | (IS_CHAR(c)) && (c != '<')) && |
| 3930 | (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3931 | /* |
| 3932 | * Impose a reasonable limit on attribute size, unless XML_PARSE_HUGE |
| 3933 | * special option is given |
| 3934 | */ |
| 3935 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 3936 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 3937 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 3938 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 3939 | goto mem_error; |
| 3940 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3941 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3942 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3943 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3944 | if (NXT(1) == '#') { |
| 3945 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3946 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3947 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3948 | if (ctxt->replaceEntities) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3949 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3950 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3951 | } |
| 3952 | buf[len++] = '&'; |
| 3953 | } else { |
| 3954 | /* |
| 3955 | * The reparsing will be done in xmlStringGetNodeList() |
| 3956 | * called by the attribute() function in SAX.c |
| 3957 | */ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3958 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3959 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3960 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3961 | buf[len++] = '&'; |
| 3962 | buf[len++] = '#'; |
| 3963 | buf[len++] = '3'; |
| 3964 | buf[len++] = '8'; |
| 3965 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3966 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 3967 | } else if (val != 0) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3968 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3969 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3970 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3971 | len += xmlCopyChar(0, &buf[len], val); |
| 3972 | } |
| 3973 | } else { |
| 3974 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 3975 | ctxt->nbentities++; |
| 3976 | if (ent != NULL) |
| 3977 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3978 | if ((ent != NULL) && |
| 3979 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3980 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3981 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3982 | } |
| 3983 | if ((ctxt->replaceEntities == 0) && |
| 3984 | (ent->content[0] == '&')) { |
| 3985 | buf[len++] = '&'; |
| 3986 | buf[len++] = '#'; |
| 3987 | buf[len++] = '3'; |
| 3988 | buf[len++] = '8'; |
| 3989 | buf[len++] = ';'; |
| 3990 | } else { |
| 3991 | buf[len++] = ent->content[0]; |
| 3992 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 3993 | } else if ((ent != NULL) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3994 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3995 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 3996 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3997 | XML_SUBSTITUTE_REF, |
| 3998 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3999 | if (rep != NULL) { |
| 4000 | current = rep; |
| 4001 | while (*current != 0) { /* non input consuming */ |
Daniel Veillard | 283d502 | 2009-08-25 17:18:39 +0200 | [diff] [blame] | 4002 | if ((*current == 0xD) || (*current == 0xA) || |
| 4003 | (*current == 0x9)) { |
| 4004 | buf[len++] = 0x20; |
| 4005 | current++; |
| 4006 | } else |
| 4007 | buf[len++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4008 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4009 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4013 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } else { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4016 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4017 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 4018 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4019 | if (ent->content != NULL) |
| 4020 | buf[len++] = ent->content[0]; |
| 4021 | } |
| 4022 | } else if (ent != NULL) { |
| 4023 | int i = xmlStrlen(ent->name); |
| 4024 | const xmlChar *cur = ent->name; |
| 4025 | |
| 4026 | /* |
| 4027 | * This may look absurd but is needed to detect |
| 4028 | * entities problems |
| 4029 | */ |
| 4030 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 4031 | (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4032 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4033 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 4034 | if (rep != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4035 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4036 | rep = NULL; |
| 4037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | /* |
| 4041 | * Just output the reference |
| 4042 | */ |
| 4043 | buf[len++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4044 | while (len + i + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4045 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4046 | } |
| 4047 | for (;i > 0;i--) |
| 4048 | buf[len++] = *cur++; |
| 4049 | buf[len++] = ';'; |
| 4050 | } |
| 4051 | } |
| 4052 | } else { |
| 4053 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4054 | if ((len != 0) || (!normalize)) { |
| 4055 | if ((!normalize) || (!in_space)) { |
| 4056 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4057 | while (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4058 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4059 | } |
| 4060 | } |
| 4061 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | } |
| 4063 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4064 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4065 | COPY_BUF(l,buf,len,c); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4066 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 4067 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4068 | } |
| 4069 | } |
| 4070 | NEXTL(l); |
| 4071 | } |
| 4072 | GROW; |
| 4073 | c = CUR_CHAR(l); |
| 4074 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4075 | if (ctxt->instate == XML_PARSER_EOF) |
| 4076 | goto error; |
| 4077 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4078 | if ((in_space) && (normalize)) { |
Daniel Veillard | 6a36fbe | 2012-10-29 10:39:55 +0800 | [diff] [blame] | 4079 | while ((len > 0) && (buf[len - 1] == 0x20)) len--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4080 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4081 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4082 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4083 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4084 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 4085 | if ((c != 0) && (!IS_CHAR(c))) { |
| 4086 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 4087 | "invalid character in attribute value\n"); |
| 4088 | } else { |
| 4089 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 4090 | "AttValue: ' expected\n"); |
| 4091 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | } else |
| 4093 | NEXT; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4094 | |
| 4095 | /* |
| 4096 | * There we potentially risk an overflow, don't allow attribute value of |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4097 | * length more than INT_MAX it is a very reasonnable assumption ! |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4098 | */ |
| 4099 | if (len >= INT_MAX) { |
| 4100 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 4101 | "AttValue length too long\n"); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 4102 | goto mem_error; |
| 4103 | } |
| 4104 | |
| 4105 | if (attlen != NULL) *attlen = (int) len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4106 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4107 | |
| 4108 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4109 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4110 | error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 4111 | if (buf != NULL) |
| 4112 | xmlFree(buf); |
| 4113 | if (rep != NULL) |
| 4114 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4115 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4119 | * xmlParseAttValue: |
| 4120 | * @ctxt: an XML parser context |
| 4121 | * |
| 4122 | * parse a value for an attribute |
| 4123 | * Note: the parser won't do substitution of entities here, this |
| 4124 | * will be handled later in xmlStringGetNodeList |
| 4125 | * |
| 4126 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 4127 | * "'" ([^<&'] | Reference)* "'" |
| 4128 | * |
| 4129 | * 3.3.3 Attribute-Value Normalization: |
| 4130 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4131 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4132 | * - a character reference is processed by appending the referenced |
| 4133 | * character to the attribute value |
| 4134 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4135 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4136 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 4137 | * appending #x20 to the normalized value, except that only a single |
| 4138 | * #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] | 4139 | * parsed entity or the literal entity value of an internal parsed entity |
| 4140 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4141 | * If the declared value is not CDATA, then the XML processor must further |
| 4142 | * process the normalized attribute value by discarding any leading and |
| 4143 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4144 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4145 | * All attributes for which no declaration has been read should be treated |
| 4146 | * by a non-validating parser as if declared CDATA. |
| 4147 | * |
| 4148 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 4149 | */ |
| 4150 | |
| 4151 | |
| 4152 | xmlChar * |
| 4153 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 4154 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4155 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4159 | * xmlParseSystemLiteral: |
| 4160 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4161 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4162 | * parse an XML Literal |
| 4163 | * |
| 4164 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 4165 | * |
| 4166 | * Returns the SystemLiteral parsed or NULL |
| 4167 | */ |
| 4168 | |
| 4169 | xmlChar * |
| 4170 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 4171 | xmlChar *buf = NULL; |
| 4172 | int len = 0; |
| 4173 | int size = XML_PARSER_BUFFER_SIZE; |
| 4174 | int cur, l; |
| 4175 | xmlChar stop; |
| 4176 | int state = ctxt->instate; |
| 4177 | int count = 0; |
| 4178 | |
| 4179 | SHRINK; |
| 4180 | if (RAW == '"') { |
| 4181 | NEXT; |
| 4182 | stop = '"'; |
| 4183 | } else if (RAW == '\'') { |
| 4184 | NEXT; |
| 4185 | stop = '\''; |
| 4186 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4187 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4188 | return(NULL); |
| 4189 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4190 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4191 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4192 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4193 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4194 | return(NULL); |
| 4195 | } |
| 4196 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 4197 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4198 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4199 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4200 | xmlChar *tmp; |
| 4201 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4202 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4203 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4204 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "SystemLiteral"); |
| 4205 | xmlFree(buf); |
| 4206 | ctxt->instate = (xmlParserInputState) state; |
| 4207 | return(NULL); |
| 4208 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4210 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4211 | if (tmp == NULL) { |
| 4212 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4213 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4214 | ctxt->instate = (xmlParserInputState) state; |
| 4215 | return(NULL); |
| 4216 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4217 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4218 | } |
| 4219 | count++; |
| 4220 | if (count > 50) { |
| 4221 | GROW; |
| 4222 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4223 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4224 | xmlFree(buf); |
| 4225 | return(NULL); |
| 4226 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4227 | } |
| 4228 | COPY_BUF(l,buf,len,cur); |
| 4229 | NEXTL(l); |
| 4230 | cur = CUR_CHAR(l); |
| 4231 | if (cur == 0) { |
| 4232 | GROW; |
| 4233 | SHRINK; |
| 4234 | cur = CUR_CHAR(l); |
| 4235 | } |
| 4236 | } |
| 4237 | buf[len] = 0; |
| 4238 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4239 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4240 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4241 | } else { |
| 4242 | NEXT; |
| 4243 | } |
| 4244 | return(buf); |
| 4245 | } |
| 4246 | |
| 4247 | /** |
| 4248 | * xmlParsePubidLiteral: |
| 4249 | * @ctxt: an XML parser context |
| 4250 | * |
| 4251 | * parse an XML public literal |
| 4252 | * |
| 4253 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 4254 | * |
| 4255 | * Returns the PubidLiteral parsed or NULL. |
| 4256 | */ |
| 4257 | |
| 4258 | xmlChar * |
| 4259 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 4260 | xmlChar *buf = NULL; |
| 4261 | int len = 0; |
| 4262 | int size = XML_PARSER_BUFFER_SIZE; |
| 4263 | xmlChar cur; |
| 4264 | xmlChar stop; |
| 4265 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4266 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4267 | |
| 4268 | SHRINK; |
| 4269 | if (RAW == '"') { |
| 4270 | NEXT; |
| 4271 | stop = '"'; |
| 4272 | } else if (RAW == '\'') { |
| 4273 | NEXT; |
| 4274 | stop = '\''; |
| 4275 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4276 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4277 | return(NULL); |
| 4278 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4279 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4280 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4281 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4282 | return(NULL); |
| 4283 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4284 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4285 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4286 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4287 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4288 | xmlChar *tmp; |
| 4289 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 4290 | if ((size > XML_MAX_NAME_LENGTH) && |
| 4291 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4292 | xmlFatalErr(ctxt, XML_ERR_NAME_TOO_LONG, "Public ID"); |
| 4293 | xmlFree(buf); |
| 4294 | return(NULL); |
| 4295 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4296 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4297 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4298 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4299 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4300 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4301 | return(NULL); |
| 4302 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4303 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4304 | } |
| 4305 | buf[len++] = cur; |
| 4306 | count++; |
| 4307 | if (count > 50) { |
| 4308 | GROW; |
| 4309 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4310 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4311 | xmlFree(buf); |
| 4312 | return(NULL); |
| 4313 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4314 | } |
| 4315 | NEXT; |
| 4316 | cur = CUR; |
| 4317 | if (cur == 0) { |
| 4318 | GROW; |
| 4319 | SHRINK; |
| 4320 | cur = CUR; |
| 4321 | } |
| 4322 | } |
| 4323 | buf[len] = 0; |
| 4324 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4325 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4326 | } else { |
| 4327 | NEXT; |
| 4328 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4329 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4330 | return(buf); |
| 4331 | } |
| 4332 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4333 | static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4334 | |
| 4335 | /* |
| 4336 | * used for the test in the inner loop of the char data testing |
| 4337 | */ |
| 4338 | static const unsigned char test_char_data[256] = { |
| 4339 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4340 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 4341 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4342 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4343 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 4344 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 4345 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 4346 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 4347 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 4348 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 4349 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 4350 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 4351 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4352 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4353 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4354 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4355 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4356 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4357 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4358 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4359 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4360 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4361 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4362 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4363 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4364 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4365 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4366 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4367 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4368 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4369 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4370 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4371 | }; |
| 4372 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | /** |
| 4374 | * xmlParseCharData: |
| 4375 | * @ctxt: an XML parser context |
| 4376 | * @cdata: int indicating whether we are within a CDATA section |
| 4377 | * |
| 4378 | * parse a CharData section. |
| 4379 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4380 | * |
| 4381 | * The right angle bracket (>) may be represented using the string ">", |
| 4382 | * and must, for compatibility, be escaped using ">" or a character |
| 4383 | * reference when it appears in the string "]]>" in content, when that |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4384 | * string is not marking the end of a CDATA section. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4385 | * |
| 4386 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4387 | */ |
| 4388 | |
| 4389 | void |
| 4390 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4391 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4392 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4393 | int line = ctxt->input->line; |
| 4394 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4395 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4396 | |
| 4397 | SHRINK; |
| 4398 | GROW; |
| 4399 | /* |
| 4400 | * Accelerated common case where input don't need to be |
| 4401 | * modified before passing it to the handler. |
| 4402 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4403 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4404 | in = ctxt->input->cur; |
| 4405 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4406 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4407 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4408 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4409 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4410 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4411 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4412 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4413 | goto get_more_space; |
| 4414 | } |
| 4415 | if (*in == '<') { |
| 4416 | nbchar = in - ctxt->input->cur; |
| 4417 | if (nbchar > 0) { |
| 4418 | const xmlChar *tmp = ctxt->input->cur; |
| 4419 | ctxt->input->cur = in; |
| 4420 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4421 | if ((ctxt->sax != NULL) && |
| 4422 | (ctxt->sax->ignorableWhitespace != |
| 4423 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4424 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4425 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4426 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4427 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4428 | } else { |
| 4429 | if (ctxt->sax->characters != NULL) |
| 4430 | ctxt->sax->characters(ctxt->userData, |
| 4431 | tmp, nbchar); |
| 4432 | if (*ctxt->space == -1) |
| 4433 | *ctxt->space = -2; |
| 4434 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4435 | } else if ((ctxt->sax != NULL) && |
| 4436 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4437 | ctxt->sax->characters(ctxt->userData, |
| 4438 | tmp, nbchar); |
| 4439 | } |
| 4440 | } |
| 4441 | return; |
| 4442 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4443 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4444 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4445 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4446 | while (test_char_data[*in]) { |
| 4447 | in++; |
| 4448 | ccol++; |
| 4449 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4450 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4451 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4452 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4453 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4454 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4455 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4456 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4457 | } |
| 4458 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4459 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4460 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4461 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4462 | return; |
| 4463 | } |
| 4464 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4465 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4466 | goto get_more; |
| 4467 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4468 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4469 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4470 | if ((ctxt->sax != NULL) && |
| 4471 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4472 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4473 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4474 | const xmlChar *tmp = ctxt->input->cur; |
| 4475 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4476 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4477 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4478 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4479 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4480 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4481 | } else { |
| 4482 | if (ctxt->sax->characters != NULL) |
| 4483 | ctxt->sax->characters(ctxt->userData, |
| 4484 | tmp, nbchar); |
| 4485 | if (*ctxt->space == -1) |
| 4486 | *ctxt->space = -2; |
| 4487 | } |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4488 | line = ctxt->input->line; |
| 4489 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4490 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4491 | if (ctxt->sax->characters != NULL) |
| 4492 | ctxt->sax->characters(ctxt->userData, |
| 4493 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4494 | line = ctxt->input->line; |
| 4495 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4496 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4497 | /* something really bad happened in the SAX callback */ |
| 4498 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4499 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4500 | } |
| 4501 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4502 | if (*in == 0xD) { |
| 4503 | in++; |
| 4504 | if (*in == 0xA) { |
| 4505 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4506 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4507 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4508 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4509 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4510 | in--; |
| 4511 | } |
| 4512 | if (*in == '<') { |
| 4513 | return; |
| 4514 | } |
| 4515 | if (*in == '&') { |
| 4516 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4517 | } |
| 4518 | SHRINK; |
| 4519 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4520 | if (ctxt->instate == XML_PARSER_EOF) |
| 4521 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4522 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4523 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4524 | nbchar = 0; |
| 4525 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4526 | ctxt->input->line = line; |
| 4527 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4528 | xmlParseCharDataComplex(ctxt, cdata); |
| 4529 | } |
| 4530 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4531 | /** |
| 4532 | * xmlParseCharDataComplex: |
| 4533 | * @ctxt: an XML parser context |
| 4534 | * @cdata: int indicating whether we are within a CDATA section |
| 4535 | * |
| 4536 | * parse a CharData section.this is the fallback function |
| 4537 | * of xmlParseCharData() when the parsing requires handling |
| 4538 | * of non-ASCII characters. |
| 4539 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4540 | static void |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4541 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4542 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4543 | int nbchar = 0; |
| 4544 | int cur, l; |
| 4545 | int count = 0; |
| 4546 | |
| 4547 | SHRINK; |
| 4548 | GROW; |
| 4549 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4550 | while ((cur != '<') && /* checked */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4551 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4552 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4553 | if ((cur == ']') && (NXT(1) == ']') && |
| 4554 | (NXT(2) == '>')) { |
| 4555 | if (cdata) break; |
| 4556 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4557 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4558 | } |
| 4559 | } |
| 4560 | COPY_BUF(l,buf,nbchar,cur); |
| 4561 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4562 | buf[nbchar] = 0; |
| 4563 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4564 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4565 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4566 | */ |
| 4567 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4568 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4569 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4570 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4571 | buf, nbchar); |
| 4572 | } else { |
| 4573 | if (ctxt->sax->characters != NULL) |
| 4574 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4575 | if ((ctxt->sax->characters != |
| 4576 | ctxt->sax->ignorableWhitespace) && |
| 4577 | (*ctxt->space == -1)) |
| 4578 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4579 | } |
| 4580 | } |
| 4581 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4582 | /* something really bad happened in the SAX callback */ |
| 4583 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4584 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4585 | } |
| 4586 | count++; |
| 4587 | if (count > 50) { |
| 4588 | GROW; |
| 4589 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4590 | if (ctxt->instate == XML_PARSER_EOF) |
| 4591 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4592 | } |
| 4593 | NEXTL(l); |
| 4594 | cur = CUR_CHAR(l); |
| 4595 | } |
| 4596 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4597 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4598 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4599 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | */ |
| 4601 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4602 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4603 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4604 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4605 | } else { |
| 4606 | if (ctxt->sax->characters != NULL) |
| 4607 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4608 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4609 | (*ctxt->space == -1)) |
| 4610 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4611 | } |
| 4612 | } |
| 4613 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4614 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4615 | /* Generate the error and skip the offending character */ |
| 4616 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4617 | "PCDATA invalid Char value %d\n", |
| 4618 | cur); |
| 4619 | NEXTL(l); |
| 4620 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | /** |
| 4624 | * xmlParseExternalID: |
| 4625 | * @ctxt: an XML parser context |
| 4626 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4627 | * @strict: indicate whether we should restrict parsing to only |
| 4628 | * production [75], see NOTE below |
| 4629 | * |
| 4630 | * Parse an External ID or a Public ID |
| 4631 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4632 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4633 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4634 | * |
| 4635 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4636 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4637 | * |
| 4638 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4639 | * |
| 4640 | * Returns the function returns SystemLiteral and in the second |
| 4641 | * case publicID receives PubidLiteral, is strict is off |
| 4642 | * it is possible to return NULL and have publicID set. |
| 4643 | */ |
| 4644 | |
| 4645 | xmlChar * |
| 4646 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4647 | xmlChar *URI = NULL; |
| 4648 | |
| 4649 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4650 | |
| 4651 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4652 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4653 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4654 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4655 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4656 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4657 | } |
| 4658 | SKIP_BLANKS; |
| 4659 | URI = xmlParseSystemLiteral(ctxt); |
| 4660 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4661 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4662 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4663 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4664 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4665 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4666 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4667 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4668 | } |
| 4669 | SKIP_BLANKS; |
| 4670 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4671 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4672 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4673 | } |
| 4674 | if (strict) { |
| 4675 | /* |
| 4676 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4677 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4678 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4679 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4680 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4681 | } |
| 4682 | } else { |
| 4683 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4684 | * We handle [83] so we return immediately, if |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4685 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4686 | * point of view that's a nice mess. |
| 4687 | */ |
| 4688 | const xmlChar *ptr; |
| 4689 | GROW; |
| 4690 | |
| 4691 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4692 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 4693 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4694 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4695 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4696 | } |
| 4697 | SKIP_BLANKS; |
| 4698 | URI = xmlParseSystemLiteral(ctxt); |
| 4699 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4700 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | } |
| 4702 | } |
| 4703 | return(URI); |
| 4704 | } |
| 4705 | |
| 4706 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4707 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4708 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4709 | * @buf: the already parsed part of the buffer |
| 4710 | * @len: number of bytes filles in the buffer |
| 4711 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4712 | * |
| 4713 | * Skip an XML (SGML) comment <!-- .... --> |
| 4714 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4715 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4716 | * 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] | 4717 | * |
| 4718 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4719 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4720 | static void |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4721 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, |
| 4722 | size_t len, size_t size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4723 | int q, ql; |
| 4724 | int r, rl; |
| 4725 | int cur, l; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4726 | size_t count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4727 | int inputid; |
| 4728 | |
| 4729 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4731 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4732 | len = 0; |
| 4733 | size = XML_PARSER_BUFFER_SIZE; |
| 4734 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4735 | if (buf == NULL) { |
| 4736 | xmlErrMemory(ctxt, NULL); |
| 4737 | return; |
| 4738 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4739 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4740 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4741 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4742 | if (q == 0) |
| 4743 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4744 | if (!IS_CHAR(q)) { |
| 4745 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4746 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4747 | q); |
| 4748 | xmlFree (buf); |
| 4749 | return; |
| 4750 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4751 | NEXTL(ql); |
| 4752 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4753 | if (r == 0) |
| 4754 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4755 | if (!IS_CHAR(r)) { |
| 4756 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4757 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4758 | q); |
| 4759 | xmlFree (buf); |
| 4760 | return; |
| 4761 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | NEXTL(rl); |
| 4763 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4764 | if (cur == 0) |
| 4765 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4766 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4767 | ((cur != '>') || |
| 4768 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4769 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4770 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4771 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4772 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4773 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4774 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4775 | "Comment too big found", NULL); |
| 4776 | xmlFree (buf); |
| 4777 | return; |
| 4778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4780 | xmlChar *new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4781 | size_t new_size; |
| 4782 | |
| 4783 | new_size = size * 2; |
| 4784 | new_buf = (xmlChar *) xmlRealloc(buf, new_size); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4785 | if (new_buf == NULL) { |
| 4786 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4787 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | return; |
| 4789 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4790 | buf = new_buf; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4791 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4792 | } |
| 4793 | COPY_BUF(ql,buf,len,q); |
| 4794 | q = r; |
| 4795 | ql = rl; |
| 4796 | r = cur; |
| 4797 | rl = l; |
| 4798 | |
| 4799 | count++; |
| 4800 | if (count > 50) { |
| 4801 | GROW; |
| 4802 | count = 0; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4803 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4804 | xmlFree(buf); |
| 4805 | return; |
| 4806 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4807 | } |
| 4808 | NEXTL(l); |
| 4809 | cur = CUR_CHAR(l); |
| 4810 | if (cur == 0) { |
| 4811 | SHRINK; |
| 4812 | GROW; |
| 4813 | cur = CUR_CHAR(l); |
| 4814 | } |
| 4815 | } |
| 4816 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4817 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4818 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4820 | } else if (!IS_CHAR(cur)) { |
| 4821 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4822 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4823 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4824 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4825 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4826 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4827 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4828 | } |
| 4829 | NEXT; |
| 4830 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4831 | (!ctxt->disableSAX)) |
| 4832 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4833 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4834 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4835 | return; |
| 4836 | not_terminated: |
| 4837 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4838 | "Comment not terminated\n", NULL); |
| 4839 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4840 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4841 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4842 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4843 | /** |
| 4844 | * xmlParseComment: |
| 4845 | * @ctxt: an XML parser context |
| 4846 | * |
| 4847 | * Skip an XML (SGML) comment <!-- .... --> |
| 4848 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4849 | * must not occur within comments. " |
| 4850 | * |
| 4851 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4852 | */ |
| 4853 | void |
| 4854 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4855 | xmlChar *buf = NULL; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4856 | size_t size = XML_PARSER_BUFFER_SIZE; |
| 4857 | size_t len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4858 | xmlParserInputState state; |
| 4859 | const xmlChar *in; |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4860 | size_t nbchar = 0; |
| 4861 | int ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4862 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4863 | |
| 4864 | /* |
| 4865 | * Check that there is a comment right here. |
| 4866 | */ |
| 4867 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4868 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4869 | state = ctxt->instate; |
| 4870 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4871 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4872 | SKIP(4); |
| 4873 | SHRINK; |
| 4874 | GROW; |
| 4875 | |
| 4876 | /* |
| 4877 | * Accelerated common case where input don't need to be |
| 4878 | * modified before passing it to the handler. |
| 4879 | */ |
| 4880 | in = ctxt->input->cur; |
| 4881 | do { |
| 4882 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4883 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4884 | ctxt->input->line++; ctxt->input->col = 1; |
| 4885 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4886 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4887 | } |
| 4888 | get_more: |
| 4889 | ccol = ctxt->input->col; |
| 4890 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4891 | ((*in >= 0x20) && (*in < '-')) || |
| 4892 | (*in == 0x09)) { |
| 4893 | in++; |
| 4894 | ccol++; |
| 4895 | } |
| 4896 | ctxt->input->col = ccol; |
| 4897 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4898 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4899 | ctxt->input->line++; ctxt->input->col = 1; |
| 4900 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4901 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4902 | goto get_more; |
| 4903 | } |
| 4904 | nbchar = in - ctxt->input->cur; |
| 4905 | /* |
| 4906 | * save current set of data |
| 4907 | */ |
| 4908 | if (nbchar > 0) { |
| 4909 | if ((ctxt->sax != NULL) && |
| 4910 | (ctxt->sax->comment != NULL)) { |
| 4911 | if (buf == NULL) { |
| 4912 | if ((*in == '-') && (in[1] == '-')) |
| 4913 | size = nbchar + 1; |
| 4914 | else |
| 4915 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4916 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4917 | if (buf == NULL) { |
| 4918 | xmlErrMemory(ctxt, NULL); |
| 4919 | ctxt->instate = state; |
| 4920 | return; |
| 4921 | } |
| 4922 | len = 0; |
| 4923 | } else if (len + nbchar + 1 >= size) { |
| 4924 | xmlChar *new_buf; |
| 4925 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4926 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4927 | size * sizeof(xmlChar)); |
| 4928 | if (new_buf == NULL) { |
| 4929 | xmlFree (buf); |
| 4930 | xmlErrMemory(ctxt, NULL); |
| 4931 | ctxt->instate = state; |
| 4932 | return; |
| 4933 | } |
| 4934 | buf = new_buf; |
| 4935 | } |
| 4936 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4937 | len += nbchar; |
| 4938 | buf[len] = 0; |
| 4939 | } |
| 4940 | } |
Daniel Veillard | 58f73ac | 2012-07-19 11:58:47 +0800 | [diff] [blame] | 4941 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 4942 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 4943 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4944 | "Comment too big found", NULL); |
| 4945 | xmlFree (buf); |
| 4946 | return; |
| 4947 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4948 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4949 | if (*in == 0xA) { |
| 4950 | in++; |
| 4951 | ctxt->input->line++; ctxt->input->col = 1; |
| 4952 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4953 | if (*in == 0xD) { |
| 4954 | in++; |
| 4955 | if (*in == 0xA) { |
| 4956 | ctxt->input->cur = in; |
| 4957 | in++; |
| 4958 | ctxt->input->line++; ctxt->input->col = 1; |
| 4959 | continue; /* while */ |
| 4960 | } |
| 4961 | in--; |
| 4962 | } |
| 4963 | SHRINK; |
| 4964 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 4965 | if (ctxt->instate == XML_PARSER_EOF) { |
| 4966 | xmlFree(buf); |
| 4967 | return; |
| 4968 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4969 | in = ctxt->input->cur; |
| 4970 | if (*in == '-') { |
| 4971 | if (in[1] == '-') { |
| 4972 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4973 | if (ctxt->input->id != inputid) { |
| 4974 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4975 | "comment doesn't start and stop in the same entity\n"); |
| 4976 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4977 | SKIP(3); |
| 4978 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4979 | (!ctxt->disableSAX)) { |
| 4980 | if (buf != NULL) |
| 4981 | ctxt->sax->comment(ctxt->userData, buf); |
| 4982 | else |
| 4983 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 4984 | } |
| 4985 | if (buf != NULL) |
| 4986 | xmlFree(buf); |
| 4987 | ctxt->instate = state; |
| 4988 | return; |
| 4989 | } |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 4990 | if (buf != NULL) { |
| 4991 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 4992 | "Double hyphen within comment: " |
| 4993 | "<!--%.50s\n", |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4994 | buf); |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 4995 | } else |
| 4996 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 4997 | "Double hyphen within comment\n", NULL); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4998 | in++; |
| 4999 | ctxt->input->col++; |
| 5000 | } |
| 5001 | in++; |
| 5002 | ctxt->input->col++; |
| 5003 | goto get_more; |
| 5004 | } |
| 5005 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 5006 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 5007 | ctxt->instate = state; |
| 5008 | return; |
| 5009 | } |
| 5010 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5011 | |
| 5012 | /** |
| 5013 | * xmlParsePITarget: |
| 5014 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5015 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5016 | * parse the name of a PI |
| 5017 | * |
| 5018 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 5019 | * |
| 5020 | * Returns the PITarget name or NULL |
| 5021 | */ |
| 5022 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5023 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5024 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5025 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5026 | |
| 5027 | name = xmlParseName(ctxt); |
| 5028 | if ((name != NULL) && |
| 5029 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 5030 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 5031 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 5032 | int i; |
| 5033 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 5034 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5035 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5036 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5037 | return(name); |
| 5038 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5039 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5040 | return(name); |
| 5041 | } |
| 5042 | for (i = 0;;i++) { |
| 5043 | if (xmlW3CPIs[i] == NULL) break; |
| 5044 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 5045 | return(name); |
| 5046 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5047 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 5048 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 5049 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5050 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5051 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5052 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5053 | "colon are forbidden from PI names '%s'\n", name, NULL, NULL); |
| 5054 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5055 | return(name); |
| 5056 | } |
| 5057 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5058 | #ifdef LIBXML_CATALOG_ENABLED |
| 5059 | /** |
| 5060 | * xmlParseCatalogPI: |
| 5061 | * @ctxt: an XML parser context |
| 5062 | * @catalog: the PI value string |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5063 | * |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5064 | * parse an XML Catalog Processing Instruction. |
| 5065 | * |
| 5066 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 5067 | * |
| 5068 | * Occurs only if allowed by the user and if happening in the Misc |
| 5069 | * part of the document before any doctype informations |
| 5070 | * This will add the given catalog to the parsing context in order |
| 5071 | * to be used if there is a resolution need further down in the document |
| 5072 | */ |
| 5073 | |
| 5074 | static void |
| 5075 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 5076 | xmlChar *URL = NULL; |
| 5077 | const xmlChar *tmp, *base; |
| 5078 | xmlChar marker; |
| 5079 | |
| 5080 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5081 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5082 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 5083 | goto error; |
| 5084 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5085 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5086 | if (*tmp != '=') { |
| 5087 | return; |
| 5088 | } |
| 5089 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5090 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5091 | marker = *tmp; |
| 5092 | if ((marker != '\'') && (marker != '"')) |
| 5093 | goto error; |
| 5094 | tmp++; |
| 5095 | base = tmp; |
| 5096 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 5097 | if (*tmp == 0) |
| 5098 | goto error; |
| 5099 | URL = xmlStrndup(base, tmp - base); |
| 5100 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5101 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5102 | if (*tmp != 0) |
| 5103 | goto error; |
| 5104 | |
| 5105 | if (URL != NULL) { |
| 5106 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 5107 | xmlFree(URL); |
| 5108 | } |
| 5109 | return; |
| 5110 | |
| 5111 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5112 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 5113 | "Catalog PI syntax error: %s\n", |
| 5114 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5115 | if (URL != NULL) |
| 5116 | xmlFree(URL); |
| 5117 | } |
| 5118 | #endif |
| 5119 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5120 | /** |
| 5121 | * xmlParsePI: |
| 5122 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5123 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5124 | * parse an XML Processing Instruction. |
| 5125 | * |
| 5126 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 5127 | * |
| 5128 | * The processing is transfered to SAX once parsed. |
| 5129 | */ |
| 5130 | |
| 5131 | void |
| 5132 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 5133 | xmlChar *buf = NULL; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5134 | size_t len = 0; |
| 5135 | size_t size = XML_PARSER_BUFFER_SIZE; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5136 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5137 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5138 | xmlParserInputState state; |
| 5139 | int count = 0; |
| 5140 | |
| 5141 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 5142 | xmlParserInputPtr input = ctxt->input; |
| 5143 | state = ctxt->instate; |
| 5144 | ctxt->instate = XML_PARSER_PI; |
| 5145 | /* |
| 5146 | * this is a Processing Instruction. |
| 5147 | */ |
| 5148 | SKIP(2); |
| 5149 | SHRINK; |
| 5150 | |
| 5151 | /* |
| 5152 | * Parse the target name and check for special support like |
| 5153 | * namespace. |
| 5154 | */ |
| 5155 | target = xmlParsePITarget(ctxt); |
| 5156 | if (target != NULL) { |
| 5157 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5158 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5159 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5160 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5161 | } |
| 5162 | SKIP(2); |
| 5163 | |
| 5164 | /* |
| 5165 | * SAX: PI detected. |
| 5166 | */ |
| 5167 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5168 | (ctxt->sax->processingInstruction != NULL)) |
| 5169 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5170 | target, NULL); |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5171 | if (ctxt->instate != XML_PARSER_EOF) |
| 5172 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | return; |
| 5174 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5175 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5176 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5177 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5178 | ctxt->instate = state; |
| 5179 | return; |
| 5180 | } |
| 5181 | cur = CUR; |
| 5182 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5183 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5184 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5185 | } |
| 5186 | SKIP_BLANKS; |
| 5187 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 5188 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5189 | ((cur != '?') || (NXT(1) != '>'))) { |
| 5190 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5191 | xmlChar *tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5192 | size_t new_size = size * 2; |
| 5193 | tmp = (xmlChar *) xmlRealloc(buf, new_size); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5194 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5195 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5196 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5197 | ctxt->instate = state; |
| 5198 | return; |
| 5199 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5200 | buf = tmp; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5201 | size = new_size; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | } |
| 5203 | count++; |
| 5204 | if (count > 50) { |
| 5205 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5206 | if (ctxt->instate == XML_PARSER_EOF) { |
| 5207 | xmlFree(buf); |
| 5208 | return; |
| 5209 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5210 | count = 0; |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5211 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5212 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5213 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5214 | "PI %s too big found", target); |
| 5215 | xmlFree(buf); |
| 5216 | ctxt->instate = state; |
| 5217 | return; |
| 5218 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5219 | } |
| 5220 | COPY_BUF(l,buf,len,cur); |
| 5221 | NEXTL(l); |
| 5222 | cur = CUR_CHAR(l); |
| 5223 | if (cur == 0) { |
| 5224 | SHRINK; |
| 5225 | GROW; |
| 5226 | cur = CUR_CHAR(l); |
| 5227 | } |
| 5228 | } |
Daniel Veillard | 5130481 | 2012-07-19 20:34:26 +0800 | [diff] [blame] | 5229 | if ((len > XML_MAX_TEXT_LENGTH) && |
| 5230 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 5231 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5232 | "PI %s too big found", target); |
| 5233 | xmlFree(buf); |
| 5234 | ctxt->instate = state; |
| 5235 | return; |
| 5236 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5237 | buf[len] = 0; |
| 5238 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5239 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5240 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5241 | } else { |
| 5242 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5243 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5244 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5245 | } |
| 5246 | SKIP(2); |
| 5247 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5248 | #ifdef LIBXML_CATALOG_ENABLED |
| 5249 | if (((state == XML_PARSER_MISC) || |
| 5250 | (state == XML_PARSER_START)) && |
| 5251 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 5252 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 5253 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 5254 | (allow == XML_CATA_ALLOW_ALL)) |
| 5255 | xmlParseCatalogPI(ctxt, buf); |
| 5256 | } |
| 5257 | #endif |
| 5258 | |
| 5259 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5260 | /* |
| 5261 | * SAX: PI detected. |
| 5262 | */ |
| 5263 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5264 | (ctxt->sax->processingInstruction != NULL)) |
| 5265 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5266 | target, buf); |
| 5267 | } |
| 5268 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5269 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5270 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5271 | } |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5272 | if (ctxt->instate != XML_PARSER_EOF) |
| 5273 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | /** |
| 5278 | * xmlParseNotationDecl: |
| 5279 | * @ctxt: an XML parser context |
| 5280 | * |
| 5281 | * parse a notation declaration |
| 5282 | * |
| 5283 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 5284 | * |
| 5285 | * Hence there is actually 3 choices: |
| 5286 | * 'PUBLIC' S PubidLiteral |
| 5287 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 5288 | * and 'SYSTEM' S SystemLiteral |
| 5289 | * |
| 5290 | * See the NOTE on xmlParseExternalID(). |
| 5291 | */ |
| 5292 | |
| 5293 | void |
| 5294 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5295 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5296 | xmlChar *Pubid; |
| 5297 | xmlChar *Systemid; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5298 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5299 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5300 | xmlParserInputPtr input = ctxt->input; |
| 5301 | SHRINK; |
| 5302 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5303 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5304 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5305 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5306 | return; |
| 5307 | } |
| 5308 | SKIP_BLANKS; |
| 5309 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5310 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5311 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5312 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5313 | return; |
| 5314 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5315 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5316 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5317 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5318 | return; |
| 5319 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5320 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5321 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5322 | "colon are forbidden from notation names '%s'\n", |
| 5323 | name, NULL, NULL); |
| 5324 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5325 | SKIP_BLANKS; |
| 5326 | |
| 5327 | /* |
| 5328 | * Parse the IDs. |
| 5329 | */ |
| 5330 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 5331 | SKIP_BLANKS; |
| 5332 | |
| 5333 | if (RAW == '>') { |
| 5334 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5335 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5336 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5337 | } |
| 5338 | NEXT; |
| 5339 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5340 | (ctxt->sax->notationDecl != NULL)) |
| 5341 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 5342 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5343 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5344 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5345 | if (Systemid != NULL) xmlFree(Systemid); |
| 5346 | if (Pubid != NULL) xmlFree(Pubid); |
| 5347 | } |
| 5348 | } |
| 5349 | |
| 5350 | /** |
| 5351 | * xmlParseEntityDecl: |
| 5352 | * @ctxt: an XML parser context |
| 5353 | * |
| 5354 | * parse <!ENTITY declarations |
| 5355 | * |
| 5356 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 5357 | * |
| 5358 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 5359 | * |
| 5360 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 5361 | * |
| 5362 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 5363 | * |
| 5364 | * [74] PEDef ::= EntityValue | ExternalID |
| 5365 | * |
| 5366 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 5367 | * |
| 5368 | * [ VC: Notation Declared ] |
| 5369 | * The Name must match the declared name of a notation. |
| 5370 | */ |
| 5371 | |
| 5372 | void |
| 5373 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5374 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5375 | xmlChar *value = NULL; |
| 5376 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5377 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5378 | int isParameter = 0; |
| 5379 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5380 | int skipped; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5381 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5382 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5383 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5384 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5385 | SHRINK; |
| 5386 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5387 | skipped = SKIP_BLANKS; |
| 5388 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5389 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5390 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5391 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5392 | |
| 5393 | if (RAW == '%') { |
| 5394 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5395 | skipped = SKIP_BLANKS; |
| 5396 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5397 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5398 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5399 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5400 | isParameter = 1; |
| 5401 | } |
| 5402 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5403 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5404 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5405 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5406 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5407 | return; |
| 5408 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5409 | if (xmlStrchr(name, ':') != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5410 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5411 | "colon are forbidden from entities names '%s'\n", |
| 5412 | name, NULL, NULL); |
| 5413 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5414 | skipped = SKIP_BLANKS; |
| 5415 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5416 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5417 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5418 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5419 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5420 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5421 | /* |
| 5422 | * handle the various case of definitions... |
| 5423 | */ |
| 5424 | if (isParameter) { |
| 5425 | if ((RAW == '"') || (RAW == '\'')) { |
| 5426 | value = xmlParseEntityValue(ctxt, &orig); |
| 5427 | if (value) { |
| 5428 | if ((ctxt->sax != NULL) && |
| 5429 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5430 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5431 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5432 | NULL, NULL, value); |
| 5433 | } |
| 5434 | } else { |
| 5435 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5436 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5437 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5438 | } |
| 5439 | if (URI) { |
| 5440 | xmlURIPtr uri; |
| 5441 | |
| 5442 | uri = xmlParseURI((const char *) URI); |
| 5443 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5444 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5445 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5446 | /* |
| 5447 | * This really ought to be a well formedness error |
| 5448 | * but the XML Core WG decided otherwise c.f. issue |
| 5449 | * E26 of the XML erratas. |
| 5450 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | } else { |
| 5452 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5453 | /* |
| 5454 | * Okay this is foolish to block those but not |
| 5455 | * invalid URIs. |
| 5456 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5457 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5458 | } else { |
| 5459 | if ((ctxt->sax != NULL) && |
| 5460 | (!ctxt->disableSAX) && |
| 5461 | (ctxt->sax->entityDecl != NULL)) |
| 5462 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5463 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5464 | literal, URI, NULL); |
| 5465 | } |
| 5466 | xmlFreeURI(uri); |
| 5467 | } |
| 5468 | } |
| 5469 | } |
| 5470 | } else { |
| 5471 | if ((RAW == '"') || (RAW == '\'')) { |
| 5472 | value = xmlParseEntityValue(ctxt, &orig); |
| 5473 | if ((ctxt->sax != NULL) && |
| 5474 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5475 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5476 | XML_INTERNAL_GENERAL_ENTITY, |
| 5477 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5478 | /* |
| 5479 | * For expat compatibility in SAX mode. |
| 5480 | */ |
| 5481 | if ((ctxt->myDoc == NULL) || |
| 5482 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5483 | if (ctxt->myDoc == NULL) { |
| 5484 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5485 | if (ctxt->myDoc == NULL) { |
| 5486 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5487 | return; |
| 5488 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5489 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5490 | } |
| 5491 | if (ctxt->myDoc->intSubset == NULL) |
| 5492 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5493 | BAD_CAST "fake", NULL, NULL); |
| 5494 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5495 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5496 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5497 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5498 | } else { |
| 5499 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5500 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5501 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5502 | } |
| 5503 | if (URI) { |
| 5504 | xmlURIPtr uri; |
| 5505 | |
| 5506 | uri = xmlParseURI((const char *)URI); |
| 5507 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5508 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5509 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5510 | /* |
| 5511 | * This really ought to be a well formedness error |
| 5512 | * but the XML Core WG decided otherwise c.f. issue |
| 5513 | * E26 of the XML erratas. |
| 5514 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5515 | } else { |
| 5516 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5517 | /* |
| 5518 | * Okay this is foolish to block those but not |
| 5519 | * invalid URIs. |
| 5520 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5521 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5522 | } |
| 5523 | xmlFreeURI(uri); |
| 5524 | } |
| 5525 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5526 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5527 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5528 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5529 | } |
| 5530 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5531 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5532 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5533 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5534 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5535 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5536 | } |
| 5537 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5538 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5539 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5540 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5541 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5542 | literal, URI, ndata); |
| 5543 | } else { |
| 5544 | if ((ctxt->sax != NULL) && |
| 5545 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5546 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5547 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5548 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5549 | /* |
| 5550 | * For expat compatibility in SAX mode. |
| 5551 | * assuming the entity repalcement was asked for |
| 5552 | */ |
| 5553 | if ((ctxt->replaceEntities != 0) && |
| 5554 | ((ctxt->myDoc == NULL) || |
| 5555 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5556 | if (ctxt->myDoc == NULL) { |
| 5557 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5558 | if (ctxt->myDoc == NULL) { |
| 5559 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5560 | return; |
| 5561 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5562 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | if (ctxt->myDoc->intSubset == NULL) |
| 5566 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5567 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5568 | xmlSAX2EntityDecl(ctxt, name, |
| 5569 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5570 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5571 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | } |
| 5573 | } |
| 5574 | } |
| 5575 | SKIP_BLANKS; |
| 5576 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5577 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5578 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5579 | } else { |
| 5580 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5581 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5582 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5583 | } |
| 5584 | NEXT; |
| 5585 | } |
| 5586 | if (orig != NULL) { |
| 5587 | /* |
| 5588 | * Ugly mechanism to save the raw entity value. |
| 5589 | */ |
| 5590 | xmlEntityPtr cur = NULL; |
| 5591 | |
| 5592 | if (isParameter) { |
| 5593 | if ((ctxt->sax != NULL) && |
| 5594 | (ctxt->sax->getParameterEntity != NULL)) |
| 5595 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5596 | } else { |
| 5597 | if ((ctxt->sax != NULL) && |
| 5598 | (ctxt->sax->getEntity != NULL)) |
| 5599 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5600 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5601 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5602 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5603 | } |
| 5604 | if (cur != NULL) { |
| 5605 | if (cur->orig != NULL) |
| 5606 | xmlFree(orig); |
| 5607 | else |
| 5608 | cur->orig = orig; |
| 5609 | } else |
| 5610 | xmlFree(orig); |
| 5611 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5612 | if (value != NULL) xmlFree(value); |
| 5613 | if (URI != NULL) xmlFree(URI); |
| 5614 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | /** |
| 5619 | * xmlParseDefaultDecl: |
| 5620 | * @ctxt: an XML parser context |
| 5621 | * @value: Receive a possible fixed default value for the attribute |
| 5622 | * |
| 5623 | * Parse an attribute default declaration |
| 5624 | * |
| 5625 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5626 | * |
| 5627 | * [ VC: Required Attribute ] |
| 5628 | * if the default declaration is the keyword #REQUIRED, then the |
| 5629 | * attribute must be specified for all elements of the type in the |
| 5630 | * attribute-list declaration. |
| 5631 | * |
| 5632 | * [ VC: Attribute Default Legal ] |
| 5633 | * The declared default value must meet the lexical constraints of |
| 5634 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5635 | * |
| 5636 | * [ VC: Fixed Attribute Default ] |
| 5637 | * if an attribute has a default value declared with the #FIXED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5638 | * keyword, instances of that attribute must match the default value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5639 | * |
| 5640 | * [ WFC: No < in Attribute Values ] |
| 5641 | * handled in xmlParseAttValue() |
| 5642 | * |
| 5643 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5644 | * or XML_ATTRIBUTE_FIXED. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5645 | */ |
| 5646 | |
| 5647 | int |
| 5648 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5649 | int val; |
| 5650 | xmlChar *ret; |
| 5651 | |
| 5652 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5653 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5654 | SKIP(9); |
| 5655 | return(XML_ATTRIBUTE_REQUIRED); |
| 5656 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5657 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5658 | SKIP(8); |
| 5659 | return(XML_ATTRIBUTE_IMPLIED); |
| 5660 | } |
| 5661 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5662 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5663 | SKIP(6); |
| 5664 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5665 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5666 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5667 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5668 | } |
| 5669 | SKIP_BLANKS; |
| 5670 | } |
| 5671 | ret = xmlParseAttValue(ctxt); |
| 5672 | ctxt->instate = XML_PARSER_DTD; |
| 5673 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5674 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5675 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5676 | } else |
| 5677 | *value = ret; |
| 5678 | return(val); |
| 5679 | } |
| 5680 | |
| 5681 | /** |
| 5682 | * xmlParseNotationType: |
| 5683 | * @ctxt: an XML parser context |
| 5684 | * |
| 5685 | * parse an Notation attribute type. |
| 5686 | * |
| 5687 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5688 | * |
| 5689 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5690 | * |
| 5691 | * [ VC: Notation Attributes ] |
| 5692 | * Values of this type must match one of the notation names included |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5693 | * in the declaration; all notation names in the declaration must be declared. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | * |
| 5695 | * Returns: the notation attribute tree built while parsing |
| 5696 | */ |
| 5697 | |
| 5698 | xmlEnumerationPtr |
| 5699 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5700 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5701 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5702 | |
| 5703 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5704 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5705 | return(NULL); |
| 5706 | } |
| 5707 | SHRINK; |
| 5708 | do { |
| 5709 | NEXT; |
| 5710 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5711 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5713 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5714 | "Name expected in NOTATION declaration\n"); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5715 | xmlFreeEnumeration(ret); |
| 5716 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5717 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5718 | tmp = ret; |
| 5719 | while (tmp != NULL) { |
| 5720 | if (xmlStrEqual(name, tmp->name)) { |
| 5721 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5722 | "standalone: attribute notation value token %s duplicated\n", |
| 5723 | name, NULL); |
| 5724 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5725 | xmlFree((xmlChar *) name); |
| 5726 | break; |
| 5727 | } |
| 5728 | tmp = tmp->next; |
| 5729 | } |
| 5730 | if (tmp == NULL) { |
| 5731 | cur = xmlCreateEnumeration(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5732 | if (cur == NULL) { |
| 5733 | xmlFreeEnumeration(ret); |
| 5734 | return(NULL); |
| 5735 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5736 | if (last == NULL) ret = last = cur; |
| 5737 | else { |
| 5738 | last->next = cur; |
| 5739 | last = cur; |
| 5740 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5741 | } |
| 5742 | SKIP_BLANKS; |
| 5743 | } while (RAW == '|'); |
| 5744 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5745 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5746 | xmlFreeEnumeration(ret); |
| 5747 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5748 | } |
| 5749 | NEXT; |
| 5750 | return(ret); |
| 5751 | } |
| 5752 | |
| 5753 | /** |
| 5754 | * xmlParseEnumerationType: |
| 5755 | * @ctxt: an XML parser context |
| 5756 | * |
| 5757 | * parse an Enumeration attribute type. |
| 5758 | * |
| 5759 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5760 | * |
| 5761 | * [ VC: Enumeration ] |
| 5762 | * Values of this type must match one of the Nmtoken tokens in |
| 5763 | * the declaration |
| 5764 | * |
| 5765 | * Returns: the enumeration attribute tree built while parsing |
| 5766 | */ |
| 5767 | |
| 5768 | xmlEnumerationPtr |
| 5769 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5770 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5771 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5772 | |
| 5773 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5774 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5775 | return(NULL); |
| 5776 | } |
| 5777 | SHRINK; |
| 5778 | do { |
| 5779 | NEXT; |
| 5780 | SKIP_BLANKS; |
| 5781 | name = xmlParseNmtoken(ctxt); |
| 5782 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5783 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5784 | return(ret); |
| 5785 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5786 | tmp = ret; |
| 5787 | while (tmp != NULL) { |
| 5788 | if (xmlStrEqual(name, tmp->name)) { |
| 5789 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5790 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5791 | name, NULL); |
| 5792 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5793 | xmlFree(name); |
| 5794 | break; |
| 5795 | } |
| 5796 | tmp = tmp->next; |
| 5797 | } |
| 5798 | if (tmp == NULL) { |
| 5799 | cur = xmlCreateEnumeration(name); |
| 5800 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5801 | xmlFree(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5802 | if (cur == NULL) { |
| 5803 | xmlFreeEnumeration(ret); |
| 5804 | return(NULL); |
| 5805 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5806 | if (last == NULL) ret = last = cur; |
| 5807 | else { |
| 5808 | last->next = cur; |
| 5809 | last = cur; |
| 5810 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5811 | } |
| 5812 | SKIP_BLANKS; |
| 5813 | } while (RAW == '|'); |
| 5814 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5815 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5816 | return(ret); |
| 5817 | } |
| 5818 | NEXT; |
| 5819 | return(ret); |
| 5820 | } |
| 5821 | |
| 5822 | /** |
| 5823 | * xmlParseEnumeratedType: |
| 5824 | * @ctxt: an XML parser context |
| 5825 | * @tree: the enumeration tree built while parsing |
| 5826 | * |
| 5827 | * parse an Enumerated attribute type. |
| 5828 | * |
| 5829 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5830 | * |
| 5831 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5832 | * |
| 5833 | * |
| 5834 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5835 | */ |
| 5836 | |
| 5837 | int |
| 5838 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5839 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5840 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5841 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5842 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5843 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5844 | return(0); |
| 5845 | } |
| 5846 | SKIP_BLANKS; |
| 5847 | *tree = xmlParseNotationType(ctxt); |
| 5848 | if (*tree == NULL) return(0); |
| 5849 | return(XML_ATTRIBUTE_NOTATION); |
| 5850 | } |
| 5851 | *tree = xmlParseEnumerationType(ctxt); |
| 5852 | if (*tree == NULL) return(0); |
| 5853 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5854 | } |
| 5855 | |
| 5856 | /** |
| 5857 | * xmlParseAttributeType: |
| 5858 | * @ctxt: an XML parser context |
| 5859 | * @tree: the enumeration tree built while parsing |
| 5860 | * |
| 5861 | * parse the Attribute list def for an element |
| 5862 | * |
| 5863 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5864 | * |
| 5865 | * [55] StringType ::= 'CDATA' |
| 5866 | * |
| 5867 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5868 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5869 | * |
| 5870 | * Validity constraints for attribute values syntax are checked in |
| 5871 | * xmlValidateAttributeValue() |
| 5872 | * |
| 5873 | * [ VC: ID ] |
| 5874 | * Values of type ID must match the Name production. A name must not |
| 5875 | * appear more than once in an XML document as a value of this type; |
| 5876 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5877 | * |
| 5878 | * [ VC: One ID per Element Type ] |
| 5879 | * No element type may have more than one ID attribute specified. |
| 5880 | * |
| 5881 | * [ VC: ID Attribute Default ] |
| 5882 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5883 | * |
| 5884 | * [ VC: IDREF ] |
| 5885 | * Values of type IDREF must match the Name production, and values |
| 5886 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5887 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5888 | * values must match the value of some ID attribute. |
| 5889 | * |
| 5890 | * [ VC: Entity Name ] |
| 5891 | * Values of type ENTITY must match the Name production, values |
| 5892 | * of type ENTITIES must match Names; each Entity Name must match the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5893 | * name of an unparsed entity declared in the DTD. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5894 | * |
| 5895 | * [ VC: Name Token ] |
| 5896 | * Values of type NMTOKEN must match the Nmtoken production; values |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5897 | * of type NMTOKENS must match Nmtokens. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5898 | * |
| 5899 | * Returns the attribute type |
| 5900 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 5901 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5902 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5903 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5904 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5905 | SKIP(5); |
| 5906 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5907 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5908 | SKIP(6); |
| 5909 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5910 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5911 | SKIP(5); |
| 5912 | return(XML_ATTRIBUTE_IDREF); |
| 5913 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5914 | SKIP(2); |
| 5915 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5916 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5917 | SKIP(6); |
| 5918 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5919 | } 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] | 5920 | SKIP(8); |
| 5921 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5922 | } 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] | 5923 | SKIP(8); |
| 5924 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5925 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5926 | SKIP(7); |
| 5927 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5928 | } |
| 5929 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5930 | } |
| 5931 | |
| 5932 | /** |
| 5933 | * xmlParseAttributeListDecl: |
| 5934 | * @ctxt: an XML parser context |
| 5935 | * |
| 5936 | * : parse the Attribute list def for an element |
| 5937 | * |
| 5938 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5939 | * |
| 5940 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5941 | * |
| 5942 | */ |
| 5943 | void |
| 5944 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5945 | const xmlChar *elemName; |
| 5946 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5947 | xmlEnumerationPtr tree; |
| 5948 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5949 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5950 | xmlParserInputPtr input = ctxt->input; |
| 5951 | |
| 5952 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5953 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5954 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5955 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5956 | } |
| 5957 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5958 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5959 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5960 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5961 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5962 | return; |
| 5963 | } |
| 5964 | SKIP_BLANKS; |
| 5965 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 5966 | while ((RAW != '>') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | const xmlChar *check = CUR_PTR; |
| 5968 | int type; |
| 5969 | int def; |
| 5970 | xmlChar *defaultValue = NULL; |
| 5971 | |
| 5972 | GROW; |
| 5973 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5974 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5975 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5976 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5977 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5978 | break; |
| 5979 | } |
| 5980 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5981 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5982 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5983 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5984 | break; |
| 5985 | } |
| 5986 | SKIP_BLANKS; |
| 5987 | |
| 5988 | type = xmlParseAttributeType(ctxt, &tree); |
| 5989 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5990 | break; |
| 5991 | } |
| 5992 | |
| 5993 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5994 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5995 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5996 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5997 | if (tree != NULL) |
| 5998 | xmlFreeEnumeration(tree); |
| 5999 | break; |
| 6000 | } |
| 6001 | SKIP_BLANKS; |
| 6002 | |
| 6003 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 6004 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6005 | if (defaultValue != NULL) |
| 6006 | xmlFree(defaultValue); |
| 6007 | if (tree != NULL) |
| 6008 | xmlFreeEnumeration(tree); |
| 6009 | break; |
| 6010 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 6011 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 6012 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6013 | |
| 6014 | GROW; |
| 6015 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6016 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6017 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6019 | if (defaultValue != NULL) |
| 6020 | xmlFree(defaultValue); |
| 6021 | if (tree != NULL) |
| 6022 | xmlFreeEnumeration(tree); |
| 6023 | break; |
| 6024 | } |
| 6025 | SKIP_BLANKS; |
| 6026 | } |
| 6027 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6028 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6029 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | if (defaultValue != NULL) |
| 6031 | xmlFree(defaultValue); |
| 6032 | if (tree != NULL) |
| 6033 | xmlFreeEnumeration(tree); |
| 6034 | break; |
| 6035 | } |
| 6036 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 6037 | (ctxt->sax->attributeDecl != NULL)) |
| 6038 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 6039 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6040 | else if (tree != NULL) |
| 6041 | xmlFreeEnumeration(tree); |
| 6042 | |
| 6043 | if ((ctxt->sax2) && (defaultValue != NULL) && |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6044 | (def != XML_ATTRIBUTE_IMPLIED) && |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6045 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 6046 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 6047 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 6048 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6049 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 6050 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6051 | if (defaultValue != NULL) |
| 6052 | xmlFree(defaultValue); |
| 6053 | GROW; |
| 6054 | } |
| 6055 | if (RAW == '>') { |
| 6056 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6057 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6058 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 6059 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6060 | } |
| 6061 | NEXT; |
| 6062 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
| 6066 | /** |
| 6067 | * xmlParseElementMixedContentDecl: |
| 6068 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6069 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6070 | * |
| 6071 | * parse the declaration for a Mixed Element content |
| 6072 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6073 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6074 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 6075 | * '(' S? '#PCDATA' S? ')' |
| 6076 | * |
| 6077 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 6078 | * |
| 6079 | * [ VC: No Duplicate Types ] |
| 6080 | * The same name must not appear more than once in a single |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6081 | * mixed-content declaration. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6082 | * |
| 6083 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 6084 | */ |
| 6085 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6086 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6087 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6088 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6089 | |
| 6090 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6091 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6092 | SKIP(7); |
| 6093 | SKIP_BLANKS; |
| 6094 | SHRINK; |
| 6095 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6096 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6097 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6098 | "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] | 6099 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6100 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6101 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6102 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6103 | if (ret == NULL) |
| 6104 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6105 | if (RAW == '*') { |
| 6106 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6107 | NEXT; |
| 6108 | } |
| 6109 | return(ret); |
| 6110 | } |
| 6111 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6112 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6113 | if (ret == NULL) return(NULL); |
| 6114 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6115 | while ((RAW == '|') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6116 | NEXT; |
| 6117 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6118 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6119 | if (ret == NULL) return(NULL); |
| 6120 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6121 | if (cur != NULL) |
| 6122 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6123 | cur = ret; |
| 6124 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6125 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6126 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6127 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6128 | if (n->c1 != NULL) |
| 6129 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6130 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6131 | if (n != NULL) |
| 6132 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6133 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6134 | } |
| 6135 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6136 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6137 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6138 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6139 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6140 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6141 | return(NULL); |
| 6142 | } |
| 6143 | SKIP_BLANKS; |
| 6144 | GROW; |
| 6145 | } |
| 6146 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 6147 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6148 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6149 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6150 | if (cur->c2 != NULL) |
| 6151 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | } |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 6153 | if (ret != NULL) |
| 6154 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6155 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6156 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6157 | "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] | 6158 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6159 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6160 | SKIP(2); |
| 6161 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6162 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6163 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6164 | return(NULL); |
| 6165 | } |
| 6166 | |
| 6167 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6168 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6169 | } |
| 6170 | return(ret); |
| 6171 | } |
| 6172 | |
| 6173 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6174 | * xmlParseElementChildrenContentDeclPriv: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6175 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6176 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6177 | * @depth: the level of recursion |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6178 | * |
| 6179 | * parse the declaration for a Mixed Element content |
| 6180 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6181 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6182 | * |
| 6183 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6184 | * |
| 6185 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6186 | * |
| 6187 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6188 | * |
| 6189 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6190 | * |
| 6191 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6192 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6193 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6194 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6195 | * construct is contained in the replacement text for a parameter |
| 6196 | * entity, both must be contained in the same replacement text. For |
| 6197 | * interoperability, if a parameter-entity reference appears in a |
| 6198 | * choice, seq, or Mixed construct, its replacement text should not |
| 6199 | * be empty, and neither the first nor last non-blank character of |
| 6200 | * the replacement text should be a connector (| or ,). |
| 6201 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6202 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6203 | * hierarchy. |
| 6204 | */ |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6205 | static xmlElementContentPtr |
| 6206 | xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk, |
| 6207 | int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6208 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6209 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6210 | xmlChar type = 0; |
| 6211 | |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6212 | if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 6213 | (depth > 2048)) { |
| 6214 | xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, |
| 6215 | "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n", |
| 6216 | depth); |
| 6217 | return(NULL); |
| 6218 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6219 | SKIP_BLANKS; |
| 6220 | GROW; |
| 6221 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6222 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6223 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6224 | /* Recurse on first child */ |
| 6225 | NEXT; |
| 6226 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6227 | cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6228 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6229 | SKIP_BLANKS; |
| 6230 | GROW; |
| 6231 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6232 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6233 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6234 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6235 | return(NULL); |
| 6236 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6237 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6238 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6239 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6240 | return(NULL); |
| 6241 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6242 | GROW; |
| 6243 | if (RAW == '?') { |
| 6244 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6245 | NEXT; |
| 6246 | } else if (RAW == '*') { |
| 6247 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6248 | NEXT; |
| 6249 | } else if (RAW == '+') { |
| 6250 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6251 | NEXT; |
| 6252 | } else { |
| 6253 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6254 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6255 | GROW; |
| 6256 | } |
| 6257 | SKIP_BLANKS; |
| 6258 | SHRINK; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6259 | while ((RAW != ')') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6260 | /* |
| 6261 | * Each loop we parse one separator and one element. |
| 6262 | */ |
| 6263 | if (RAW == ',') { |
| 6264 | if (type == 0) type = CUR; |
| 6265 | |
| 6266 | /* |
| 6267 | * Detect "Name | Name , Name" error |
| 6268 | */ |
| 6269 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6270 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6271 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6272 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6273 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6274 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6275 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6276 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6277 | return(NULL); |
| 6278 | } |
| 6279 | NEXT; |
| 6280 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6281 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6283 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6284 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 6285 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6286 | return(NULL); |
| 6287 | } |
| 6288 | if (last == NULL) { |
| 6289 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6290 | if (ret != NULL) |
| 6291 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6292 | ret = cur = op; |
| 6293 | } else { |
| 6294 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6295 | if (op != NULL) |
| 6296 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6297 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6298 | if (last != NULL) |
| 6299 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6300 | cur =op; |
| 6301 | last = NULL; |
| 6302 | } |
| 6303 | } else if (RAW == '|') { |
| 6304 | if (type == 0) type = CUR; |
| 6305 | |
| 6306 | /* |
| 6307 | * Detect "Name , Name | Name" error |
| 6308 | */ |
| 6309 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6310 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6311 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6312 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6313 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6314 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6315 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6316 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6317 | return(NULL); |
| 6318 | } |
| 6319 | NEXT; |
| 6320 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6321 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6322 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6323 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6324 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6325 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6326 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6327 | return(NULL); |
| 6328 | } |
| 6329 | if (last == NULL) { |
| 6330 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6331 | if (ret != NULL) |
| 6332 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6333 | ret = cur = op; |
| 6334 | } else { |
| 6335 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6336 | if (op != NULL) |
| 6337 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6338 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6339 | if (last != NULL) |
| 6340 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6341 | cur =op; |
| 6342 | last = NULL; |
| 6343 | } |
| 6344 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6345 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 6346 | if ((last != NULL) && (last != ret)) |
| 6347 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6348 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6349 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | return(NULL); |
| 6351 | } |
| 6352 | GROW; |
| 6353 | SKIP_BLANKS; |
| 6354 | GROW; |
| 6355 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6356 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6357 | /* Recurse on second child */ |
| 6358 | NEXT; |
| 6359 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6360 | last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6361 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6362 | SKIP_BLANKS; |
| 6363 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6364 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6365 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6366 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6367 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6368 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6369 | return(NULL); |
| 6370 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6371 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6372 | if (last == NULL) { |
| 6373 | if (ret != NULL) |
| 6374 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 6375 | return(NULL); |
| 6376 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6377 | if (RAW == '?') { |
| 6378 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6379 | NEXT; |
| 6380 | } else if (RAW == '*') { |
| 6381 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6382 | NEXT; |
| 6383 | } else if (RAW == '+') { |
| 6384 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6385 | NEXT; |
| 6386 | } else { |
| 6387 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6388 | } |
| 6389 | } |
| 6390 | SKIP_BLANKS; |
| 6391 | GROW; |
| 6392 | } |
| 6393 | if ((cur != NULL) && (last != NULL)) { |
| 6394 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6395 | if (last != NULL) |
| 6396 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6397 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6398 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6399 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6400 | "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] | 6401 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | NEXT; |
| 6404 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6405 | if (ret != NULL) { |
| 6406 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 6407 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6408 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6409 | else |
| 6410 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6411 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6412 | NEXT; |
| 6413 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6414 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6415 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6416 | cur = ret; |
| 6417 | /* |
| 6418 | * Some normalization: |
| 6419 | * (a | b* | c?)* == (a | b | c)* |
| 6420 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6421 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6422 | if ((cur->c1 != NULL) && |
| 6423 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6424 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6425 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6426 | if ((cur->c2 != NULL) && |
| 6427 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6428 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6429 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6430 | cur = cur->c2; |
| 6431 | } |
| 6432 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6433 | NEXT; |
| 6434 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6435 | if (ret != NULL) { |
| 6436 | int found = 0; |
| 6437 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6438 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6439 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6440 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6441 | else |
| 6442 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6443 | /* |
| 6444 | * Some normalization: |
| 6445 | * (a | b*)+ == (a | b)* |
| 6446 | * (a | b?)+ == (a | b)* |
| 6447 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6448 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6449 | if ((cur->c1 != NULL) && |
| 6450 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6451 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6452 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6453 | found = 1; |
| 6454 | } |
| 6455 | if ((cur->c2 != NULL) && |
| 6456 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6457 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6458 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6459 | found = 1; |
| 6460 | } |
| 6461 | cur = cur->c2; |
| 6462 | } |
| 6463 | if (found) |
| 6464 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6465 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6466 | NEXT; |
| 6467 | } |
| 6468 | return(ret); |
| 6469 | } |
| 6470 | |
| 6471 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6472 | * xmlParseElementChildrenContentDecl: |
| 6473 | * @ctxt: an XML parser context |
| 6474 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6475 | * |
| 6476 | * parse the declaration for a Mixed Element content |
| 6477 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 6478 | * |
| 6479 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6480 | * |
| 6481 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6482 | * |
| 6483 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6484 | * |
| 6485 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6486 | * |
| 6487 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6488 | * TODO Parameter-entity replacement text must be properly nested |
| 6489 | * with parenthesized groups. That is to say, if either of the |
| 6490 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6491 | * construct is contained in the replacement text for a parameter |
| 6492 | * entity, both must be contained in the same replacement text. For |
| 6493 | * interoperability, if a parameter-entity reference appears in a |
| 6494 | * choice, seq, or Mixed construct, its replacement text should not |
| 6495 | * be empty, and neither the first nor last non-blank character of |
| 6496 | * the replacement text should be a connector (| or ,). |
| 6497 | * |
| 6498 | * Returns the tree of xmlElementContentPtr describing the element |
| 6499 | * hierarchy. |
| 6500 | */ |
| 6501 | xmlElementContentPtr |
| 6502 | xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
| 6503 | /* stub left for API/ABI compat */ |
| 6504 | return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1)); |
| 6505 | } |
| 6506 | |
| 6507 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6508 | * xmlParseElementContentDecl: |
| 6509 | * @ctxt: an XML parser context |
| 6510 | * @name: the name of the element being defined. |
| 6511 | * @result: the Element Content pointer will be stored here if any |
| 6512 | * |
| 6513 | * parse the declaration for an Element content either Mixed or Children, |
| 6514 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6515 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6516 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6517 | * |
| 6518 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6519 | */ |
| 6520 | |
| 6521 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6522 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6523 | xmlElementContentPtr *result) { |
| 6524 | |
| 6525 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6526 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6527 | int res; |
| 6528 | |
| 6529 | *result = NULL; |
| 6530 | |
| 6531 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6532 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6533 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6534 | return(-1); |
| 6535 | } |
| 6536 | NEXT; |
| 6537 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6538 | if (ctxt->instate == XML_PARSER_EOF) |
| 6539 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6540 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6541 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6542 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6543 | res = XML_ELEMENT_TYPE_MIXED; |
| 6544 | } else { |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6545 | tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6546 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6547 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6548 | SKIP_BLANKS; |
| 6549 | *result = tree; |
| 6550 | return(res); |
| 6551 | } |
| 6552 | |
| 6553 | /** |
| 6554 | * xmlParseElementDecl: |
| 6555 | * @ctxt: an XML parser context |
| 6556 | * |
| 6557 | * parse an Element declaration. |
| 6558 | * |
| 6559 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6560 | * |
| 6561 | * [ VC: Unique Element Type Declaration ] |
| 6562 | * No element type may be declared more than once |
| 6563 | * |
| 6564 | * Returns the type of the element, or -1 in case of error |
| 6565 | */ |
| 6566 | int |
| 6567 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6568 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6569 | int ret = -1; |
| 6570 | xmlElementContentPtr content = NULL; |
| 6571 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6572 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6573 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6574 | xmlParserInputPtr input = ctxt->input; |
| 6575 | |
| 6576 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6577 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6578 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6579 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6580 | } |
| 6581 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6582 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6583 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6584 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6585 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6586 | return(-1); |
| 6587 | } |
| 6588 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6589 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6590 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6591 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6592 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6593 | } |
| 6594 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6595 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6596 | SKIP(5); |
| 6597 | /* |
| 6598 | * Element must always be empty. |
| 6599 | */ |
| 6600 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6601 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6602 | (NXT(2) == 'Y')) { |
| 6603 | SKIP(3); |
| 6604 | /* |
| 6605 | * Element is a generic container. |
| 6606 | */ |
| 6607 | ret = XML_ELEMENT_TYPE_ANY; |
| 6608 | } else if (RAW == '(') { |
| 6609 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6610 | } else { |
| 6611 | /* |
| 6612 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6613 | */ |
| 6614 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6615 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6616 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6617 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6619 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6621 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6622 | return(-1); |
| 6623 | } |
| 6624 | |
| 6625 | SKIP_BLANKS; |
| 6626 | /* |
| 6627 | * Pop-up of finished entities. |
| 6628 | */ |
| 6629 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6630 | xmlPopInput(ctxt); |
| 6631 | SKIP_BLANKS; |
| 6632 | |
| 6633 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6634 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6635 | if (content != NULL) { |
| 6636 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6637 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6638 | } else { |
| 6639 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6640 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6641 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6642 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6643 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6644 | NEXT; |
| 6645 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6646 | (ctxt->sax->elementDecl != NULL)) { |
| 6647 | if (content != NULL) |
| 6648 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6649 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6650 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6651 | if ((content != NULL) && (content->parent == NULL)) { |
| 6652 | /* |
| 6653 | * this is a trick: if xmlAddElementDecl is called, |
| 6654 | * instead of copying the full tree it is plugged directly |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6655 | * if called from the parser. Avoid duplicating the |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6656 | * interfaces or change the API/ABI |
| 6657 | */ |
| 6658 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6659 | } |
| 6660 | } else if (content != NULL) { |
| 6661 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6662 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6663 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6664 | } |
| 6665 | return(ret); |
| 6666 | } |
| 6667 | |
| 6668 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6669 | * xmlParseConditionalSections |
| 6670 | * @ctxt: an XML parser context |
| 6671 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6672 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6673 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6674 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6675 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6676 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6677 | */ |
| 6678 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6679 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6680 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6681 | int id = ctxt->input->id; |
| 6682 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6683 | SKIP(3); |
| 6684 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6685 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6686 | SKIP(7); |
| 6687 | SKIP_BLANKS; |
| 6688 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6689 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6690 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6691 | if (ctxt->input->id != id) { |
| 6692 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6693 | "All markup of the conditional section is not in the same entity\n", |
| 6694 | NULL, NULL); |
| 6695 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6696 | NEXT; |
| 6697 | } |
| 6698 | if (xmlParserDebugEntities) { |
| 6699 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6700 | xmlGenericError(xmlGenericErrorContext, |
| 6701 | "%s(%d): ", ctxt->input->filename, |
| 6702 | ctxt->input->line); |
| 6703 | xmlGenericError(xmlGenericErrorContext, |
| 6704 | "Entering INCLUDE Conditional Section\n"); |
| 6705 | } |
| 6706 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6707 | while (((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6708 | (NXT(2) != '>'))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6709 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6710 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6711 | |
| 6712 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6713 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6714 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6715 | NEXT; |
| 6716 | } else if (RAW == '%') { |
| 6717 | xmlParsePEReference(ctxt); |
| 6718 | } else |
| 6719 | xmlParseMarkupDecl(ctxt); |
| 6720 | |
| 6721 | /* |
| 6722 | * Pop-up of finished entities. |
| 6723 | */ |
| 6724 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6725 | xmlPopInput(ctxt); |
| 6726 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6727 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6728 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6729 | break; |
| 6730 | } |
| 6731 | } |
| 6732 | if (xmlParserDebugEntities) { |
| 6733 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6734 | xmlGenericError(xmlGenericErrorContext, |
| 6735 | "%s(%d): ", ctxt->input->filename, |
| 6736 | ctxt->input->line); |
| 6737 | xmlGenericError(xmlGenericErrorContext, |
| 6738 | "Leaving INCLUDE Conditional Section\n"); |
| 6739 | } |
| 6740 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6741 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6742 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6743 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6744 | int depth = 0; |
| 6745 | |
| 6746 | SKIP(6); |
| 6747 | SKIP_BLANKS; |
| 6748 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6749 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6751 | if (ctxt->input->id != id) { |
| 6752 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6753 | "All markup of the conditional section is not in the same entity\n", |
| 6754 | NULL, NULL); |
| 6755 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6756 | NEXT; |
| 6757 | } |
| 6758 | if (xmlParserDebugEntities) { |
| 6759 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6760 | xmlGenericError(xmlGenericErrorContext, |
| 6761 | "%s(%d): ", ctxt->input->filename, |
| 6762 | ctxt->input->line); |
| 6763 | xmlGenericError(xmlGenericErrorContext, |
| 6764 | "Entering IGNORE Conditional Section\n"); |
| 6765 | } |
| 6766 | |
| 6767 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6768 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6769 | * But disable SAX event generating DTD building in the meantime |
| 6770 | */ |
| 6771 | state = ctxt->disableSAX; |
| 6772 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6773 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6774 | ctxt->instate = XML_PARSER_IGNORE; |
| 6775 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 6776 | while (((depth >= 0) && (RAW != 0)) && |
| 6777 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6778 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6779 | depth++; |
| 6780 | SKIP(3); |
| 6781 | continue; |
| 6782 | } |
| 6783 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6784 | if (--depth >= 0) SKIP(3); |
| 6785 | continue; |
| 6786 | } |
| 6787 | NEXT; |
| 6788 | continue; |
| 6789 | } |
| 6790 | |
| 6791 | ctxt->disableSAX = state; |
| 6792 | ctxt->instate = instate; |
| 6793 | |
| 6794 | if (xmlParserDebugEntities) { |
| 6795 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6796 | xmlGenericError(xmlGenericErrorContext, |
| 6797 | "%s(%d): ", ctxt->input->filename, |
| 6798 | ctxt->input->line); |
| 6799 | xmlGenericError(xmlGenericErrorContext, |
| 6800 | "Leaving IGNORE Conditional Section\n"); |
| 6801 | } |
| 6802 | |
| 6803 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6804 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | if (RAW == 0) |
| 6808 | SHRINK; |
| 6809 | |
| 6810 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6811 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6812 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6813 | if (ctxt->input->id != id) { |
| 6814 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6815 | "All markup of the conditional section is not in the same entity\n", |
| 6816 | NULL, NULL); |
| 6817 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6818 | SKIP(3); |
| 6819 | } |
| 6820 | } |
| 6821 | |
| 6822 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6823 | * xmlParseMarkupDecl: |
| 6824 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6825 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6826 | * parse Markup declarations |
| 6827 | * |
| 6828 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6829 | * NotationDecl | PI | Comment |
| 6830 | * |
| 6831 | * [ VC: Proper Declaration/PE Nesting ] |
| 6832 | * Parameter-entity replacement text must be properly nested with |
| 6833 | * markup declarations. That is to say, if either the first character |
| 6834 | * or the last character of a markup declaration (markupdecl above) is |
| 6835 | * contained in the replacement text for a parameter-entity reference, |
| 6836 | * both must be contained in the same replacement text. |
| 6837 | * |
| 6838 | * [ WFC: PEs in Internal Subset ] |
| 6839 | * In the internal DTD subset, parameter-entity references can occur |
| 6840 | * only where markup declarations can occur, not within markup declarations. |
| 6841 | * (This does not apply to references that occur in external parameter |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6842 | * entities or to the external subset.) |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6843 | */ |
| 6844 | void |
| 6845 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6846 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6847 | if (CUR == '<') { |
| 6848 | if (NXT(1) == '!') { |
| 6849 | switch (NXT(2)) { |
| 6850 | case 'E': |
| 6851 | if (NXT(3) == 'L') |
| 6852 | xmlParseElementDecl(ctxt); |
| 6853 | else if (NXT(3) == 'N') |
| 6854 | xmlParseEntityDecl(ctxt); |
| 6855 | break; |
| 6856 | case 'A': |
| 6857 | xmlParseAttributeListDecl(ctxt); |
| 6858 | break; |
| 6859 | case 'N': |
| 6860 | xmlParseNotationDecl(ctxt); |
| 6861 | break; |
| 6862 | case '-': |
| 6863 | xmlParseComment(ctxt); |
| 6864 | break; |
| 6865 | default: |
| 6866 | /* there is an error but it will be detected later */ |
| 6867 | break; |
| 6868 | } |
| 6869 | } else if (NXT(1) == '?') { |
| 6870 | xmlParsePI(ctxt); |
| 6871 | } |
| 6872 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6873 | /* |
| 6874 | * This is only for internal subset. On external entities, |
| 6875 | * the replacement is done before parsing stage |
| 6876 | */ |
| 6877 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6878 | xmlParsePEReference(ctxt); |
| 6879 | |
| 6880 | /* |
| 6881 | * Conditional sections are allowed from entities included |
| 6882 | * by PE References in the internal subset. |
| 6883 | */ |
| 6884 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6885 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6886 | xmlParseConditionalSections(ctxt); |
| 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | ctxt->instate = XML_PARSER_DTD; |
| 6891 | } |
| 6892 | |
| 6893 | /** |
| 6894 | * xmlParseTextDecl: |
| 6895 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6896 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6897 | * parse an XML declaration header for external entities |
| 6898 | * |
| 6899 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6900 | */ |
| 6901 | |
| 6902 | void |
| 6903 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6904 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6905 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6906 | |
| 6907 | /* |
| 6908 | * We know that '<?xml' is here. |
| 6909 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6910 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6911 | SKIP(5); |
| 6912 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6913 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6914 | return; |
| 6915 | } |
| 6916 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6917 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6918 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6919 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6920 | } |
| 6921 | SKIP_BLANKS; |
| 6922 | |
| 6923 | /* |
| 6924 | * We may have the VersionInfo here. |
| 6925 | */ |
| 6926 | version = xmlParseVersionInfo(ctxt); |
| 6927 | if (version == NULL) |
| 6928 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6929 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6930 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6931 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6932 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6933 | } |
| 6934 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6935 | ctxt->input->version = version; |
| 6936 | |
| 6937 | /* |
| 6938 | * We must have the encoding declaration |
| 6939 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6940 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6941 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6942 | /* |
| 6943 | * The XML REC instructs us to stop parsing right here |
| 6944 | */ |
| 6945 | return; |
| 6946 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6947 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 6948 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 6949 | "Missing encoding in text declaration\n"); |
| 6950 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6951 | |
| 6952 | SKIP_BLANKS; |
| 6953 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 6954 | SKIP(2); |
| 6955 | } else if (RAW == '>') { |
| 6956 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6957 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6958 | NEXT; |
| 6959 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6960 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6961 | MOVETO_ENDTAG(CUR_PTR); |
| 6962 | NEXT; |
| 6963 | } |
| 6964 | } |
| 6965 | |
| 6966 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6967 | * xmlParseExternalSubset: |
| 6968 | * @ctxt: an XML parser context |
| 6969 | * @ExternalID: the external identifier |
| 6970 | * @SystemID: the system identifier (or URL) |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 6971 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6972 | * parse Markup declarations from an external subset |
| 6973 | * |
| 6974 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 6975 | * |
| 6976 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 6977 | */ |
| 6978 | void |
| 6979 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 6980 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 6981 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6982 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6983 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 6984 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6985 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 6986 | xmlChar start[4]; |
| 6987 | xmlCharEncoding enc; |
| 6988 | |
| 6989 | start[0] = RAW; |
| 6990 | start[1] = NXT(1); |
| 6991 | start[2] = NXT(2); |
| 6992 | start[3] = NXT(3); |
| 6993 | enc = xmlDetectCharEncoding(start, 4); |
| 6994 | if (enc != XML_CHAR_ENCODING_NONE) |
| 6995 | xmlSwitchEncoding(ctxt, enc); |
| 6996 | } |
| 6997 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6998 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6999 | xmlParseTextDecl(ctxt); |
| 7000 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 7001 | /* |
| 7002 | * The XML REC instructs us to stop parsing right here |
| 7003 | */ |
| 7004 | ctxt->instate = XML_PARSER_EOF; |
| 7005 | return; |
| 7006 | } |
| 7007 | } |
| 7008 | if (ctxt->myDoc == NULL) { |
| 7009 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 7010 | if (ctxt->myDoc == NULL) { |
| 7011 | xmlErrMemory(ctxt, "New Doc failed"); |
| 7012 | return; |
| 7013 | } |
| 7014 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7015 | } |
| 7016 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 7017 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 7018 | |
| 7019 | ctxt->instate = XML_PARSER_DTD; |
| 7020 | ctxt->external = 1; |
| 7021 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 7022 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7023 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7024 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7025 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7026 | |
| 7027 | GROW; |
| 7028 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 7029 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7030 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | NEXT; |
| 7032 | } else if (RAW == '%') { |
| 7033 | xmlParsePEReference(ctxt); |
| 7034 | } else |
| 7035 | xmlParseMarkupDecl(ctxt); |
| 7036 | |
| 7037 | /* |
| 7038 | * Pop-up of finished entities. |
| 7039 | */ |
| 7040 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7041 | xmlPopInput(ctxt); |
| 7042 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7043 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7044 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7045 | break; |
| 7046 | } |
| 7047 | } |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7048 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7049 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7050 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | } |
| 7054 | |
| 7055 | /** |
| 7056 | * xmlParseReference: |
| 7057 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7058 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7059 | * parse and handle entity references in content, depending on the SAX |
| 7060 | * interface, this may end-up in a call to character() if this is a |
| 7061 | * CharRef, a predefined entity, if there is no reference() callback. |
| 7062 | * or if the parser was asked to switch to that mode. |
| 7063 | * |
| 7064 | * [67] Reference ::= EntityRef | CharRef |
| 7065 | */ |
| 7066 | void |
| 7067 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 7068 | xmlEntityPtr ent; |
| 7069 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7070 | int was_checked; |
| 7071 | xmlNodePtr list = NULL; |
| 7072 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7073 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7074 | |
| 7075 | if (RAW != '&') |
| 7076 | return; |
| 7077 | |
| 7078 | /* |
| 7079 | * Simple case of a CharRef |
| 7080 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7081 | if (NXT(1) == '#') { |
| 7082 | int i = 0; |
| 7083 | xmlChar out[10]; |
| 7084 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7085 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7086 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 7087 | if (value == 0) |
| 7088 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7089 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 7090 | /* |
| 7091 | * So we are using non-UTF-8 buffers |
| 7092 | * Check that the char fit on 8bits, if not |
| 7093 | * generate a CharRef. |
| 7094 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7095 | if (value <= 0xFF) { |
| 7096 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7097 | out[1] = 0; |
| 7098 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7099 | (!ctxt->disableSAX)) |
| 7100 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 7101 | } else { |
| 7102 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7103 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7104 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 7105 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7106 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7107 | (!ctxt->disableSAX)) |
| 7108 | ctxt->sax->reference(ctxt->userData, out); |
| 7109 | } |
| 7110 | } else { |
| 7111 | /* |
| 7112 | * Just encode the value in UTF-8 |
| 7113 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7114 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7115 | out[i] = 0; |
| 7116 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7117 | (!ctxt->disableSAX)) |
| 7118 | ctxt->sax->characters(ctxt->userData, out, i); |
| 7119 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7120 | return; |
| 7121 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7122 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7123 | /* |
| 7124 | * We are seeing an entity reference |
| 7125 | */ |
| 7126 | ent = xmlParseEntityRef(ctxt); |
| 7127 | if (ent == NULL) return; |
| 7128 | if (!ctxt->wellFormed) |
| 7129 | return; |
| 7130 | was_checked = ent->checked; |
| 7131 | |
| 7132 | /* special case of predefined entities */ |
| 7133 | if ((ent->name == NULL) || |
| 7134 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 7135 | val = ent->content; |
| 7136 | if (val == NULL) return; |
| 7137 | /* |
| 7138 | * inline the entity. |
| 7139 | */ |
| 7140 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 7141 | (!ctxt->disableSAX)) |
| 7142 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 7143 | return; |
| 7144 | } |
| 7145 | |
| 7146 | /* |
| 7147 | * The first reference to the entity trigger a parsing phase |
| 7148 | * where the ent->children is filled with the result from |
| 7149 | * the parsing. |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7150 | * Note: external parsed entities will not be loaded, it is not |
| 7151 | * required for a non-validating parser, unless the parsing option |
| 7152 | * of validating, or substituting entities were given. Doing so is |
| 7153 | * far more secure as the parser will only process data coming from |
| 7154 | * the document entity by default. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7155 | */ |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame] | 7156 | if ((ent->checked == 0) && |
| 7157 | ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
| 7158 | (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7159 | unsigned long oldnbent = ctxt->nbentities; |
| 7160 | |
| 7161 | /* |
| 7162 | * This is a bit hackish but this seems the best |
| 7163 | * way to make sure both SAX and DOM entity support |
| 7164 | * behaves okay. |
| 7165 | */ |
| 7166 | void *user_data; |
| 7167 | if (ctxt->userData == ctxt) |
| 7168 | user_data = NULL; |
| 7169 | else |
| 7170 | user_data = ctxt->userData; |
| 7171 | |
| 7172 | /* |
| 7173 | * Check that this entity is well formed |
| 7174 | * 4.3.2: An internal general parsed entity is well-formed |
| 7175 | * if its replacement text matches the production labeled |
| 7176 | * content. |
| 7177 | */ |
| 7178 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7179 | ctxt->depth++; |
| 7180 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 7181 | user_data, &list); |
| 7182 | ctxt->depth--; |
| 7183 | |
| 7184 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7185 | ctxt->depth++; |
| 7186 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 7187 | user_data, ctxt->depth, ent->URI, |
| 7188 | ent->ExternalID, &list); |
| 7189 | ctxt->depth--; |
| 7190 | } else { |
| 7191 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7192 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7193 | "invalid entity type found\n", NULL); |
| 7194 | } |
| 7195 | |
| 7196 | /* |
| 7197 | * Store the number of entities needing parsing for this entity |
| 7198 | * content and do checkings |
| 7199 | */ |
| 7200 | ent->checked = ctxt->nbentities - oldnbent; |
| 7201 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7202 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7203 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 7204 | return; |
| 7205 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7206 | if (xmlParserEntityCheck(ctxt, 0, ent)) { |
| 7207 | xmlFreeNodeList(list); |
| 7208 | return; |
| 7209 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7210 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7211 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 7212 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 7213 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 7214 | (ent->children == NULL)) { |
| 7215 | ent->children = list; |
| 7216 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7217 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7218 | * Prune it directly in the generated document |
| 7219 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7220 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7221 | if (((list->type == XML_TEXT_NODE) && |
| 7222 | (list->next == NULL)) || |
| 7223 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7224 | list->parent = (xmlNodePtr) ent; |
| 7225 | list = NULL; |
| 7226 | ent->owner = 1; |
| 7227 | } else { |
| 7228 | ent->owner = 0; |
| 7229 | while (list != NULL) { |
| 7230 | list->parent = (xmlNodePtr) ctxt->node; |
| 7231 | list->doc = ctxt->myDoc; |
| 7232 | if (list->next == NULL) |
| 7233 | ent->last = list; |
| 7234 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7235 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7236 | list = ent->children; |
| 7237 | #ifdef LIBXML_LEGACY_ENABLED |
| 7238 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7239 | xmlAddEntityReference(ent, list, NULL); |
| 7240 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7241 | } |
| 7242 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7243 | ent->owner = 1; |
| 7244 | while (list != NULL) { |
| 7245 | list->parent = (xmlNodePtr) ent; |
Rob Richards | c794eb5 | 2011-02-18 12:17:17 -0500 | [diff] [blame] | 7246 | xmlSetTreeDoc(list, ent->doc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7247 | if (list->next == NULL) |
| 7248 | ent->last = list; |
| 7249 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7250 | } |
| 7251 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7252 | } else { |
| 7253 | xmlFreeNodeList(list); |
| 7254 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7255 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7256 | } else if ((ret != XML_ERR_OK) && |
| 7257 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 7258 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7259 | "Entity '%s' failed to parse\n", ent->name); |
| 7260 | } else if (list != NULL) { |
| 7261 | xmlFreeNodeList(list); |
| 7262 | list = NULL; |
| 7263 | } |
| 7264 | if (ent->checked == 0) |
| 7265 | ent->checked = 1; |
| 7266 | } else if (ent->checked != 1) { |
| 7267 | ctxt->nbentities += ent->checked; |
| 7268 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7269 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7270 | /* |
| 7271 | * Now that the entity content has been gathered |
| 7272 | * provide it to the application, this can take different forms based |
| 7273 | * on the parsing modes. |
| 7274 | */ |
| 7275 | if (ent->children == NULL) { |
| 7276 | /* |
| 7277 | * Probably running in SAX mode and the callbacks don't |
| 7278 | * build the entity content. So unless we already went |
| 7279 | * though parsing for first checking go though the entity |
| 7280 | * content to generate callbacks associated to the entity |
| 7281 | */ |
| 7282 | if (was_checked != 0) { |
| 7283 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7284 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7285 | * This is a bit hackish but this seems the best |
| 7286 | * way to make sure both SAX and DOM entity support |
| 7287 | * behaves okay. |
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 | if (ctxt->userData == ctxt) |
| 7290 | user_data = NULL; |
| 7291 | else |
| 7292 | user_data = ctxt->userData; |
| 7293 | |
| 7294 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7295 | ctxt->depth++; |
| 7296 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 7297 | ent->content, user_data, NULL); |
| 7298 | ctxt->depth--; |
| 7299 | } else if (ent->etype == |
| 7300 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7301 | ctxt->depth++; |
| 7302 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 7303 | ctxt->sax, user_data, ctxt->depth, |
| 7304 | ent->URI, ent->ExternalID, NULL); |
| 7305 | ctxt->depth--; |
| 7306 | } else { |
| 7307 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7308 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7309 | "invalid entity type found\n", NULL); |
| 7310 | } |
| 7311 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 7312 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 7313 | return; |
| 7314 | } |
| 7315 | } |
| 7316 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7317 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7318 | /* |
| 7319 | * Entity reference callback comes second, it's somewhat |
| 7320 | * superfluous but a compatibility to historical behaviour |
| 7321 | */ |
| 7322 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7323 | } |
| 7324 | return; |
| 7325 | } |
| 7326 | |
| 7327 | /* |
| 7328 | * If we didn't get any children for the entity being built |
| 7329 | */ |
| 7330 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7331 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7332 | /* |
| 7333 | * Create a node. |
| 7334 | */ |
| 7335 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7336 | return; |
| 7337 | } |
| 7338 | |
| 7339 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 7340 | /* |
| 7341 | * There is a problem on the handling of _private for entities |
| 7342 | * (bug 155816): Should we copy the content of the field from |
| 7343 | * the entity (possibly overwriting some value set by the user |
| 7344 | * when a copy is created), should we leave it alone, or should |
| 7345 | * we try to take care of different situations? The problem |
| 7346 | * is exacerbated by the usage of this field by the xmlReader. |
| 7347 | * To fix this bug, we look at _private on the created node |
| 7348 | * and, if it's NULL, we copy in whatever was in the entity. |
| 7349 | * If it's not NULL we leave it alone. This is somewhat of a |
| 7350 | * hack - maybe we should have further tests to determine |
| 7351 | * what to do. |
| 7352 | */ |
| 7353 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 7354 | /* |
| 7355 | * Seems we are generating the DOM content, do |
| 7356 | * a simple tree copy for all references except the first |
| 7357 | * In the first occurrence list contains the replacement. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7358 | */ |
| 7359 | if (((list == NULL) && (ent->owner == 0)) || |
| 7360 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7361 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 7362 | |
| 7363 | /* |
| 7364 | * when operating on a reader, the entities definitions |
| 7365 | * are always owning the entities subtree. |
| 7366 | if (ctxt->parseMode == XML_PARSE_READER) |
| 7367 | ent->owner = 1; |
| 7368 | */ |
| 7369 | |
| 7370 | cur = ent->children; |
| 7371 | while (cur != NULL) { |
| 7372 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7373 | if (nw != NULL) { |
| 7374 | if (nw->_private == NULL) |
| 7375 | nw->_private = cur->_private; |
| 7376 | if (firstChild == NULL){ |
| 7377 | firstChild = nw; |
| 7378 | } |
| 7379 | nw = xmlAddChild(ctxt->node, nw); |
| 7380 | } |
| 7381 | if (cur == ent->last) { |
| 7382 | /* |
| 7383 | * needed to detect some strange empty |
| 7384 | * node cases in the reader tests |
| 7385 | */ |
| 7386 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 7387 | (nw != NULL) && |
| 7388 | (nw->type == XML_ELEMENT_NODE) && |
| 7389 | (nw->children == NULL)) |
| 7390 | nw->extra = 1; |
| 7391 | |
| 7392 | break; |
| 7393 | } |
| 7394 | cur = cur->next; |
| 7395 | } |
| 7396 | #ifdef LIBXML_LEGACY_ENABLED |
| 7397 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7398 | xmlAddEntityReference(ent, firstChild, nw); |
| 7399 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 28f5e1a | 2012-09-04 11:18:39 +0800 | [diff] [blame] | 7400 | } else if ((list == NULL) || (ctxt->inputNr > 0)) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7401 | xmlNodePtr nw = NULL, cur, next, last, |
| 7402 | firstChild = NULL; |
| 7403 | /* |
| 7404 | * Copy the entity child list and make it the new |
| 7405 | * entity child list. The goal is to make sure any |
| 7406 | * ID or REF referenced will be the one from the |
| 7407 | * document content and not the entity copy. |
| 7408 | */ |
| 7409 | cur = ent->children; |
| 7410 | ent->children = NULL; |
| 7411 | last = ent->last; |
| 7412 | ent->last = NULL; |
| 7413 | while (cur != NULL) { |
| 7414 | next = cur->next; |
| 7415 | cur->next = NULL; |
| 7416 | cur->parent = NULL; |
| 7417 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7418 | if (nw != NULL) { |
| 7419 | if (nw->_private == NULL) |
| 7420 | nw->_private = cur->_private; |
| 7421 | if (firstChild == NULL){ |
| 7422 | firstChild = cur; |
| 7423 | } |
| 7424 | xmlAddChild((xmlNodePtr) ent, nw); |
| 7425 | xmlAddChild(ctxt->node, cur); |
| 7426 | } |
| 7427 | if (cur == last) |
| 7428 | break; |
| 7429 | cur = next; |
| 7430 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 7431 | if (ent->owner == 0) |
| 7432 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7433 | #ifdef LIBXML_LEGACY_ENABLED |
| 7434 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7435 | xmlAddEntityReference(ent, firstChild, nw); |
| 7436 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7437 | } else { |
| 7438 | const xmlChar *nbktext; |
| 7439 | |
| 7440 | /* |
| 7441 | * the name change is to avoid coalescing of the |
| 7442 | * node with a possible previous text one which |
| 7443 | * would make ent->children a dangling pointer |
| 7444 | */ |
| 7445 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 7446 | -1); |
| 7447 | if (ent->children->type == XML_TEXT_NODE) |
| 7448 | ent->children->name = nbktext; |
| 7449 | if ((ent->last != ent->children) && |
| 7450 | (ent->last->type == XML_TEXT_NODE)) |
| 7451 | ent->last->name = nbktext; |
| 7452 | xmlAddChildList(ctxt->node, ent->children); |
| 7453 | } |
| 7454 | |
| 7455 | /* |
| 7456 | * This is to avoid a nasty side effect, see |
| 7457 | * characters() in SAX.c |
| 7458 | */ |
| 7459 | ctxt->nodemem = 0; |
| 7460 | ctxt->nodelen = 0; |
| 7461 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7462 | } |
| 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | /** |
| 7467 | * xmlParseEntityRef: |
| 7468 | * @ctxt: an XML parser context |
| 7469 | * |
| 7470 | * parse ENTITY references declarations |
| 7471 | * |
| 7472 | * [68] EntityRef ::= '&' Name ';' |
| 7473 | * |
| 7474 | * [ WFC: Entity Declared ] |
| 7475 | * In a document without any DTD, a document with only an internal DTD |
| 7476 | * subset which contains no parameter entity references, or a document |
| 7477 | * with "standalone='yes'", the Name given in the entity reference |
| 7478 | * must match that in an entity declaration, except that well-formed |
| 7479 | * documents need not declare any of the following entities: amp, lt, |
| 7480 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7481 | * any reference to it. Similarly, the declaration of a general entity |
| 7482 | * must precede any reference to it which appears in a default value in an |
| 7483 | * attribute-list declaration. Note that if entities are declared in the |
| 7484 | * external subset or in external parameter entities, a non-validating |
| 7485 | * processor is not obligated to read and process their declarations; |
| 7486 | * for such documents, the rule that an entity must be declared is a |
| 7487 | * well-formedness constraint only if standalone='yes'. |
| 7488 | * |
| 7489 | * [ WFC: Parsed Entity ] |
| 7490 | * An entity reference must not contain the name of an unparsed entity |
| 7491 | * |
| 7492 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7493 | */ |
| 7494 | xmlEntityPtr |
| 7495 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7496 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7497 | xmlEntityPtr ent = NULL; |
| 7498 | |
| 7499 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 7500 | if (ctxt->instate == XML_PARSER_EOF) |
| 7501 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7502 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7503 | if (RAW != '&') |
| 7504 | return(NULL); |
| 7505 | NEXT; |
| 7506 | name = xmlParseName(ctxt); |
| 7507 | if (name == NULL) { |
| 7508 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7509 | "xmlParseEntityRef: no name\n"); |
| 7510 | return(NULL); |
| 7511 | } |
| 7512 | if (RAW != ';') { |
| 7513 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7514 | return(NULL); |
| 7515 | } |
| 7516 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7517 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7518 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7519 | * Predefined entities override any extra definition |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7520 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7521 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7522 | ent = xmlGetPredefinedEntity(name); |
| 7523 | if (ent != NULL) |
| 7524 | return(ent); |
| 7525 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7526 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7527 | /* |
Jan Pokorný | 81d7a82 | 2012-09-13 15:56:51 +0200 | [diff] [blame] | 7528 | * Increase the number of entity references parsed |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7529 | */ |
| 7530 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7531 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7532 | /* |
| 7533 | * Ask first SAX for entity resolution, otherwise try the |
| 7534 | * entities which may have stored in the parser context. |
| 7535 | */ |
| 7536 | if (ctxt->sax != NULL) { |
| 7537 | if (ctxt->sax->getEntity != NULL) |
| 7538 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7539 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7540 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7541 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7542 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7543 | (ctxt->userData==ctxt)) { |
| 7544 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7545 | } |
| 7546 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7547 | /* |
| 7548 | * [ WFC: Entity Declared ] |
| 7549 | * In a document without any DTD, a document with only an |
| 7550 | * internal DTD subset which contains no parameter entity |
| 7551 | * references, or a document with "standalone='yes'", the |
| 7552 | * Name given in the entity reference must match that in an |
| 7553 | * entity declaration, except that well-formed documents |
| 7554 | * need not declare any of the following entities: amp, lt, |
| 7555 | * gt, apos, quot. |
| 7556 | * The declaration of a parameter entity must precede any |
| 7557 | * reference to it. |
| 7558 | * Similarly, the declaration of a general entity must |
| 7559 | * precede any reference to it which appears in a default |
| 7560 | * value in an attribute-list declaration. Note that if |
| 7561 | * entities are declared in the external subset or in |
| 7562 | * external parameter entities, a non-validating processor |
| 7563 | * is not obligated to read and process their declarations; |
| 7564 | * for such documents, the rule that an entity must be |
| 7565 | * declared is a well-formedness constraint only if |
| 7566 | * standalone='yes'. |
| 7567 | */ |
| 7568 | if (ent == NULL) { |
| 7569 | if ((ctxt->standalone == 1) || |
| 7570 | ((ctxt->hasExternalSubset == 0) && |
| 7571 | (ctxt->hasPErefs == 0))) { |
| 7572 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7573 | "Entity '%s' not defined\n", name); |
| 7574 | } else { |
| 7575 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7576 | "Entity '%s' not defined\n", name); |
| 7577 | if ((ctxt->inSubset == 0) && |
| 7578 | (ctxt->sax != NULL) && |
| 7579 | (ctxt->sax->reference != NULL)) { |
| 7580 | ctxt->sax->reference(ctxt->userData, name); |
| 7581 | } |
| 7582 | } |
| 7583 | ctxt->valid = 0; |
| 7584 | } |
| 7585 | |
| 7586 | /* |
| 7587 | * [ WFC: Parsed Entity ] |
| 7588 | * An entity reference must not contain the name of an |
| 7589 | * unparsed entity |
| 7590 | */ |
| 7591 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7592 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7593 | "Entity reference to unparsed entity %s\n", name); |
| 7594 | } |
| 7595 | |
| 7596 | /* |
| 7597 | * [ WFC: No External Entity References ] |
| 7598 | * Attribute values cannot contain direct or indirect |
| 7599 | * entity references to external entities. |
| 7600 | */ |
| 7601 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7602 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7603 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7604 | "Attribute references external entity '%s'\n", name); |
| 7605 | } |
| 7606 | /* |
| 7607 | * [ WFC: No < in Attribute Values ] |
| 7608 | * The replacement text of any entity referred to directly or |
| 7609 | * indirectly in an attribute value (other than "<") must |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7610 | * not contain a <. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7611 | */ |
| 7612 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7613 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7614 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7615 | (xmlStrchr(ent->content, '<'))) { |
| 7616 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7617 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7618 | } |
| 7619 | |
| 7620 | /* |
| 7621 | * Internal check, no parameter entities here ... |
| 7622 | */ |
| 7623 | else { |
| 7624 | switch (ent->etype) { |
| 7625 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7626 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7627 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7628 | "Attempt to reference the parameter entity '%s'\n", |
| 7629 | name); |
| 7630 | break; |
| 7631 | default: |
| 7632 | break; |
| 7633 | } |
| 7634 | } |
| 7635 | |
| 7636 | /* |
| 7637 | * [ WFC: No Recursion ] |
| 7638 | * A parsed entity must not contain a recursive reference |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7639 | * to itself, either directly or indirectly. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7640 | * Done somewhere else |
| 7641 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7642 | return(ent); |
| 7643 | } |
| 7644 | |
| 7645 | /** |
| 7646 | * xmlParseStringEntityRef: |
| 7647 | * @ctxt: an XML parser context |
| 7648 | * @str: a pointer to an index in the string |
| 7649 | * |
| 7650 | * parse ENTITY references declarations, but this version parses it from |
| 7651 | * a string value. |
| 7652 | * |
| 7653 | * [68] EntityRef ::= '&' Name ';' |
| 7654 | * |
| 7655 | * [ WFC: Entity Declared ] |
| 7656 | * In a document without any DTD, a document with only an internal DTD |
| 7657 | * subset which contains no parameter entity references, or a document |
| 7658 | * with "standalone='yes'", the Name given in the entity reference |
| 7659 | * must match that in an entity declaration, except that well-formed |
| 7660 | * documents need not declare any of the following entities: amp, lt, |
| 7661 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7662 | * any reference to it. Similarly, the declaration of a general entity |
| 7663 | * must precede any reference to it which appears in a default value in an |
| 7664 | * attribute-list declaration. Note that if entities are declared in the |
| 7665 | * external subset or in external parameter entities, a non-validating |
| 7666 | * processor is not obligated to read and process their declarations; |
| 7667 | * for such documents, the rule that an entity must be declared is a |
| 7668 | * well-formedness constraint only if standalone='yes'. |
| 7669 | * |
| 7670 | * [ WFC: Parsed Entity ] |
| 7671 | * An entity reference must not contain the name of an unparsed entity |
| 7672 | * |
| 7673 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7674 | * is updated to the current location in the string. |
| 7675 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7676 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7677 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7678 | xmlChar *name; |
| 7679 | const xmlChar *ptr; |
| 7680 | xmlChar cur; |
| 7681 | xmlEntityPtr ent = NULL; |
| 7682 | |
| 7683 | if ((str == NULL) || (*str == NULL)) |
| 7684 | return(NULL); |
| 7685 | ptr = *str; |
| 7686 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7687 | if (cur != '&') |
| 7688 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7689 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7690 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7691 | name = xmlParseStringName(ctxt, &ptr); |
| 7692 | if (name == NULL) { |
| 7693 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7694 | "xmlParseStringEntityRef: no name\n"); |
| 7695 | *str = ptr; |
| 7696 | return(NULL); |
| 7697 | } |
| 7698 | if (*ptr != ';') { |
| 7699 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7700 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7701 | *str = ptr; |
| 7702 | return(NULL); |
| 7703 | } |
| 7704 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7705 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7706 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7707 | /* |
| 7708 | * Predefined entites override any extra definition |
| 7709 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7710 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7711 | ent = xmlGetPredefinedEntity(name); |
| 7712 | if (ent != NULL) { |
| 7713 | xmlFree(name); |
| 7714 | *str = ptr; |
| 7715 | return(ent); |
| 7716 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7717 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7718 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7719 | /* |
| 7720 | * Increate the number of entity references parsed |
| 7721 | */ |
| 7722 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7723 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7724 | /* |
| 7725 | * Ask first SAX for entity resolution, otherwise try the |
| 7726 | * entities which may have stored in the parser context. |
| 7727 | */ |
| 7728 | if (ctxt->sax != NULL) { |
| 7729 | if (ctxt->sax->getEntity != NULL) |
| 7730 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7731 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7732 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7733 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7734 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7735 | } |
| 7736 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7737 | |
| 7738 | /* |
| 7739 | * [ WFC: Entity Declared ] |
| 7740 | * In a document without any DTD, a document with only an |
| 7741 | * internal DTD subset which contains no parameter entity |
| 7742 | * references, or a document with "standalone='yes'", the |
| 7743 | * Name given in the entity reference must match that in an |
| 7744 | * entity declaration, except that well-formed documents |
| 7745 | * need not declare any of the following entities: amp, lt, |
| 7746 | * gt, apos, quot. |
| 7747 | * The declaration of a parameter entity must precede any |
| 7748 | * reference to it. |
| 7749 | * Similarly, the declaration of a general entity must |
| 7750 | * precede any reference to it which appears in a default |
| 7751 | * value in an attribute-list declaration. Note that if |
| 7752 | * entities are declared in the external subset or in |
| 7753 | * external parameter entities, a non-validating processor |
| 7754 | * is not obligated to read and process their declarations; |
| 7755 | * for such documents, the rule that an entity must be |
| 7756 | * declared is a well-formedness constraint only if |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7757 | * standalone='yes'. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7758 | */ |
| 7759 | if (ent == NULL) { |
| 7760 | if ((ctxt->standalone == 1) || |
| 7761 | ((ctxt->hasExternalSubset == 0) && |
| 7762 | (ctxt->hasPErefs == 0))) { |
| 7763 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7764 | "Entity '%s' not defined\n", name); |
| 7765 | } else { |
| 7766 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7767 | "Entity '%s' not defined\n", |
| 7768 | name); |
| 7769 | } |
| 7770 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7771 | } |
| 7772 | |
| 7773 | /* |
| 7774 | * [ WFC: Parsed Entity ] |
| 7775 | * An entity reference must not contain the name of an |
| 7776 | * unparsed entity |
| 7777 | */ |
| 7778 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7779 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7780 | "Entity reference to unparsed entity %s\n", name); |
| 7781 | } |
| 7782 | |
| 7783 | /* |
| 7784 | * [ WFC: No External Entity References ] |
| 7785 | * Attribute values cannot contain direct or indirect |
| 7786 | * entity references to external entities. |
| 7787 | */ |
| 7788 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7789 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7790 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7791 | "Attribute references external entity '%s'\n", name); |
| 7792 | } |
| 7793 | /* |
| 7794 | * [ WFC: No < in Attribute Values ] |
| 7795 | * The replacement text of any entity referred to directly or |
| 7796 | * indirectly in an attribute value (other than "<") must |
| 7797 | * not contain a <. |
| 7798 | */ |
| 7799 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7800 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7801 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7802 | (xmlStrchr(ent->content, '<'))) { |
| 7803 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7804 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7805 | name); |
| 7806 | } |
| 7807 | |
| 7808 | /* |
| 7809 | * Internal check, no parameter entities here ... |
| 7810 | */ |
| 7811 | else { |
| 7812 | switch (ent->etype) { |
| 7813 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7814 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7815 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7816 | "Attempt to reference the parameter entity '%s'\n", |
| 7817 | name); |
| 7818 | break; |
| 7819 | default: |
| 7820 | break; |
| 7821 | } |
| 7822 | } |
| 7823 | |
| 7824 | /* |
| 7825 | * [ WFC: No Recursion ] |
| 7826 | * A parsed entity must not contain a recursive reference |
| 7827 | * to itself, either directly or indirectly. |
| 7828 | * Done somewhere else |
| 7829 | */ |
| 7830 | |
| 7831 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7832 | *str = ptr; |
| 7833 | return(ent); |
| 7834 | } |
| 7835 | |
| 7836 | /** |
| 7837 | * xmlParsePEReference: |
| 7838 | * @ctxt: an XML parser context |
| 7839 | * |
| 7840 | * parse PEReference declarations |
| 7841 | * The entity content is handled directly by pushing it's content as |
| 7842 | * a new input stream. |
| 7843 | * |
| 7844 | * [69] PEReference ::= '%' Name ';' |
| 7845 | * |
| 7846 | * [ WFC: No Recursion ] |
| 7847 | * A parsed entity must not contain a recursive |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 7848 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7849 | * |
| 7850 | * [ WFC: Entity Declared ] |
| 7851 | * In a document without any DTD, a document with only an internal DTD |
| 7852 | * subset which contains no parameter entity references, or a document |
| 7853 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7854 | * entity must precede any reference to it... |
| 7855 | * |
| 7856 | * [ VC: Entity Declared ] |
| 7857 | * In a document with an external subset or external parameter entities |
| 7858 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7859 | * must precede any reference to it... |
| 7860 | * |
| 7861 | * [ WFC: In DTD ] |
| 7862 | * Parameter-entity references may only appear in the DTD. |
| 7863 | * NOTE: misleading but this is handled. |
| 7864 | */ |
| 7865 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7866 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7867 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7868 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7869 | xmlEntityPtr entity = NULL; |
| 7870 | xmlParserInputPtr input; |
| 7871 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7872 | if (RAW != '%') |
| 7873 | return; |
| 7874 | NEXT; |
| 7875 | name = xmlParseName(ctxt); |
| 7876 | if (name == NULL) { |
| 7877 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7878 | "xmlParsePEReference: no name\n"); |
| 7879 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7880 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7881 | if (RAW != ';') { |
| 7882 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7883 | return; |
| 7884 | } |
| 7885 | |
| 7886 | NEXT; |
| 7887 | |
| 7888 | /* |
| 7889 | * Increate the number of entity references parsed |
| 7890 | */ |
| 7891 | ctxt->nbentities++; |
| 7892 | |
| 7893 | /* |
| 7894 | * Request the entity from SAX |
| 7895 | */ |
| 7896 | if ((ctxt->sax != NULL) && |
| 7897 | (ctxt->sax->getParameterEntity != NULL)) |
| 7898 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7899 | name); |
| 7900 | if (entity == NULL) { |
| 7901 | /* |
| 7902 | * [ WFC: Entity Declared ] |
| 7903 | * In a document without any DTD, a document with only an |
| 7904 | * internal DTD subset which contains no parameter entity |
| 7905 | * references, or a document with "standalone='yes'", ... |
| 7906 | * ... The declaration of a parameter entity must precede |
| 7907 | * any reference to it... |
| 7908 | */ |
| 7909 | if ((ctxt->standalone == 1) || |
| 7910 | ((ctxt->hasExternalSubset == 0) && |
| 7911 | (ctxt->hasPErefs == 0))) { |
| 7912 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7913 | "PEReference: %%%s; not found\n", |
| 7914 | name); |
| 7915 | } else { |
| 7916 | /* |
| 7917 | * [ VC: Entity Declared ] |
| 7918 | * In a document with an external subset or external |
| 7919 | * parameter entities with "standalone='no'", ... |
| 7920 | * ... The declaration of a parameter entity must |
| 7921 | * precede any reference to it... |
| 7922 | */ |
| 7923 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7924 | "PEReference: %%%s; not found\n", |
| 7925 | name, NULL); |
| 7926 | ctxt->valid = 0; |
| 7927 | } |
| 7928 | } else { |
| 7929 | /* |
| 7930 | * Internal checking in case the entity quest barfed |
| 7931 | */ |
| 7932 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7933 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7934 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7935 | "Internal: %%%s; is not a parameter entity\n", |
| 7936 | name, NULL); |
| 7937 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 7938 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 7939 | if (xmlPushInput(ctxt, input) < 0) |
| 7940 | return; |
| 7941 | } else { |
| 7942 | /* |
| 7943 | * TODO !!! |
| 7944 | * handle the extra spaces added before and after |
| 7945 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 7946 | */ |
| 7947 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7948 | if (xmlPushInput(ctxt, input) < 0) |
| 7949 | return; |
| 7950 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7951 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 7952 | (IS_BLANK_CH(NXT(5)))) { |
| 7953 | xmlParseTextDecl(ctxt); |
| 7954 | if (ctxt->errNo == |
| 7955 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 7956 | /* |
| 7957 | * The XML REC instructs us to stop parsing |
| 7958 | * right here |
| 7959 | */ |
| 7960 | ctxt->instate = XML_PARSER_EOF; |
| 7961 | return; |
| 7962 | } |
| 7963 | } |
| 7964 | } |
| 7965 | } |
| 7966 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7967 | } |
| 7968 | |
| 7969 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7970 | * xmlLoadEntityContent: |
| 7971 | * @ctxt: an XML parser context |
| 7972 | * @entity: an unloaded system entity |
| 7973 | * |
| 7974 | * Load the original content of the given system entity from the |
| 7975 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 7976 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 7977 | * |
| 7978 | * Returns 0 in case of success and -1 in case of failure |
| 7979 | */ |
| 7980 | static int |
| 7981 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 7982 | xmlParserInputPtr input; |
| 7983 | xmlBufferPtr buf; |
| 7984 | int l, c; |
| 7985 | int count = 0; |
| 7986 | |
| 7987 | if ((ctxt == NULL) || (entity == NULL) || |
| 7988 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7989 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 7990 | (entity->content != NULL)) { |
| 7991 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7992 | "xmlLoadEntityContent parameter error"); |
| 7993 | return(-1); |
| 7994 | } |
| 7995 | |
| 7996 | if (xmlParserDebugEntities) |
| 7997 | xmlGenericError(xmlGenericErrorContext, |
| 7998 | "Reading %s entity content input\n", entity->name); |
| 7999 | |
| 8000 | buf = xmlBufferCreate(); |
| 8001 | if (buf == NULL) { |
| 8002 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8003 | "xmlLoadEntityContent parameter error"); |
| 8004 | return(-1); |
| 8005 | } |
| 8006 | |
| 8007 | input = xmlNewEntityInputStream(ctxt, entity); |
| 8008 | if (input == NULL) { |
| 8009 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8010 | "xmlLoadEntityContent input error"); |
| 8011 | xmlBufferFree(buf); |
| 8012 | return(-1); |
| 8013 | } |
| 8014 | |
| 8015 | /* |
| 8016 | * Push the entity as the current input, read char by char |
| 8017 | * saving to the buffer until the end of the entity or an error |
| 8018 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 8019 | if (xmlPushInput(ctxt, input) < 0) { |
| 8020 | xmlBufferFree(buf); |
| 8021 | return(-1); |
| 8022 | } |
| 8023 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8024 | GROW; |
| 8025 | c = CUR_CHAR(l); |
| 8026 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 8027 | (IS_CHAR(c))) { |
| 8028 | xmlBufferAdd(buf, ctxt->input->cur, l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8029 | if (count++ > XML_PARSER_CHUNK_SIZE) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8030 | count = 0; |
| 8031 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8032 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8033 | xmlBufferFree(buf); |
| 8034 | return(-1); |
| 8035 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8036 | } |
| 8037 | NEXTL(l); |
| 8038 | c = CUR_CHAR(l); |
Daniel Veillard | 1f972e9 | 2012-08-15 10:16:37 +0800 | [diff] [blame] | 8039 | if (c == 0) { |
| 8040 | count = 0; |
| 8041 | GROW; |
| 8042 | if (ctxt->instate == XML_PARSER_EOF) { |
| 8043 | xmlBufferFree(buf); |
| 8044 | return(-1); |
| 8045 | } |
| 8046 | c = CUR_CHAR(l); |
| 8047 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
| 8050 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 8051 | xmlPopInput(ctxt); |
| 8052 | } else if (!IS_CHAR(c)) { |
| 8053 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 8054 | "xmlLoadEntityContent: invalid char value %d\n", |
| 8055 | c); |
| 8056 | xmlBufferFree(buf); |
| 8057 | return(-1); |
| 8058 | } |
| 8059 | entity->content = buf->content; |
| 8060 | buf->content = NULL; |
| 8061 | xmlBufferFree(buf); |
| 8062 | |
| 8063 | return(0); |
| 8064 | } |
| 8065 | |
| 8066 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8067 | * xmlParseStringPEReference: |
| 8068 | * @ctxt: an XML parser context |
| 8069 | * @str: a pointer to an index in the string |
| 8070 | * |
| 8071 | * parse PEReference declarations |
| 8072 | * |
| 8073 | * [69] PEReference ::= '%' Name ';' |
| 8074 | * |
| 8075 | * [ WFC: No Recursion ] |
| 8076 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 8077 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8078 | * |
| 8079 | * [ WFC: Entity Declared ] |
| 8080 | * In a document without any DTD, a document with only an internal DTD |
| 8081 | * subset which contains no parameter entity references, or a document |
| 8082 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 8083 | * entity must precede any reference to it... |
| 8084 | * |
| 8085 | * [ VC: Entity Declared ] |
| 8086 | * In a document with an external subset or external parameter entities |
| 8087 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 8088 | * must precede any reference to it... |
| 8089 | * |
| 8090 | * [ WFC: In DTD ] |
| 8091 | * Parameter-entity references may only appear in the DTD. |
| 8092 | * NOTE: misleading but this is handled. |
| 8093 | * |
| 8094 | * Returns the string of the entity content. |
| 8095 | * str is updated to the current value of the index |
| 8096 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 8097 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8098 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 8099 | const xmlChar *ptr; |
| 8100 | xmlChar cur; |
| 8101 | xmlChar *name; |
| 8102 | xmlEntityPtr entity = NULL; |
| 8103 | |
| 8104 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 8105 | ptr = *str; |
| 8106 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8107 | if (cur != '%') |
| 8108 | return(NULL); |
| 8109 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8110 | name = xmlParseStringName(ctxt, &ptr); |
| 8111 | if (name == NULL) { |
| 8112 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8113 | "xmlParseStringPEReference: no name\n"); |
| 8114 | *str = ptr; |
| 8115 | return(NULL); |
| 8116 | } |
| 8117 | cur = *ptr; |
| 8118 | if (cur != ';') { |
| 8119 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 8120 | xmlFree(name); |
| 8121 | *str = ptr; |
| 8122 | return(NULL); |
| 8123 | } |
| 8124 | ptr++; |
| 8125 | |
| 8126 | /* |
| 8127 | * Increate the number of entity references parsed |
| 8128 | */ |
| 8129 | ctxt->nbentities++; |
| 8130 | |
| 8131 | /* |
| 8132 | * Request the entity from SAX |
| 8133 | */ |
| 8134 | if ((ctxt->sax != NULL) && |
| 8135 | (ctxt->sax->getParameterEntity != NULL)) |
| 8136 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 8137 | name); |
| 8138 | if (entity == NULL) { |
| 8139 | /* |
| 8140 | * [ WFC: Entity Declared ] |
| 8141 | * In a document without any DTD, a document with only an |
| 8142 | * internal DTD subset which contains no parameter entity |
| 8143 | * references, or a document with "standalone='yes'", ... |
| 8144 | * ... The declaration of a parameter entity must precede |
| 8145 | * any reference to it... |
| 8146 | */ |
| 8147 | if ((ctxt->standalone == 1) || |
| 8148 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 8149 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 8150 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8151 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8152 | /* |
| 8153 | * [ VC: Entity Declared ] |
| 8154 | * In a document with an external subset or external |
| 8155 | * parameter entities with "standalone='no'", ... |
| 8156 | * ... The declaration of a parameter entity must |
| 8157 | * precede any reference to it... |
| 8158 | */ |
| 8159 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8160 | "PEReference: %%%s; not found\n", |
| 8161 | name, NULL); |
| 8162 | ctxt->valid = 0; |
| 8163 | } |
| 8164 | } else { |
| 8165 | /* |
| 8166 | * Internal checking in case the entity quest barfed |
| 8167 | */ |
| 8168 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 8169 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 8170 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 8171 | "%%%s; is not a parameter entity\n", |
| 8172 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8173 | } |
| 8174 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 8175 | ctxt->hasPErefs = 1; |
| 8176 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8177 | *str = ptr; |
| 8178 | return(entity); |
| 8179 | } |
| 8180 | |
| 8181 | /** |
| 8182 | * xmlParseDocTypeDecl: |
| 8183 | * @ctxt: an XML parser context |
| 8184 | * |
| 8185 | * parse a DOCTYPE declaration |
| 8186 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8187 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8188 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8189 | * |
| 8190 | * [ VC: Root Element Type ] |
| 8191 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8192 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8193 | */ |
| 8194 | |
| 8195 | void |
| 8196 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8197 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8198 | xmlChar *ExternalID = NULL; |
| 8199 | xmlChar *URI = NULL; |
| 8200 | |
| 8201 | /* |
| 8202 | * We know that '<!DOCTYPE' has been detected. |
| 8203 | */ |
| 8204 | SKIP(9); |
| 8205 | |
| 8206 | SKIP_BLANKS; |
| 8207 | |
| 8208 | /* |
| 8209 | * Parse the DOCTYPE name. |
| 8210 | */ |
| 8211 | name = xmlParseName(ctxt); |
| 8212 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8213 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8214 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8215 | } |
| 8216 | ctxt->intSubName = name; |
| 8217 | |
| 8218 | SKIP_BLANKS; |
| 8219 | |
| 8220 | /* |
| 8221 | * Check for SystemID and ExternalID |
| 8222 | */ |
| 8223 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 8224 | |
| 8225 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 8226 | ctxt->hasExternalSubset = 1; |
| 8227 | } |
| 8228 | ctxt->extSubURI = URI; |
| 8229 | ctxt->extSubSystem = ExternalID; |
| 8230 | |
| 8231 | SKIP_BLANKS; |
| 8232 | |
| 8233 | /* |
| 8234 | * Create and update the internal subset. |
| 8235 | */ |
| 8236 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 8237 | (!ctxt->disableSAX)) |
| 8238 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 8239 | |
| 8240 | /* |
| 8241 | * Is there any internal subset declarations ? |
| 8242 | * they are handled separately in xmlParseInternalSubset() |
| 8243 | */ |
| 8244 | if (RAW == '[') |
| 8245 | return; |
| 8246 | |
| 8247 | /* |
| 8248 | * We should be at the end of the DOCTYPE declaration. |
| 8249 | */ |
| 8250 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8251 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8252 | } |
| 8253 | NEXT; |
| 8254 | } |
| 8255 | |
| 8256 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8257 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8258 | * @ctxt: an XML parser context |
| 8259 | * |
| 8260 | * parse the internal subset declaration |
| 8261 | * |
| 8262 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8263 | */ |
| 8264 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8265 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8266 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 8267 | /* |
| 8268 | * Is there any DTD definition ? |
| 8269 | */ |
| 8270 | if (RAW == '[') { |
| 8271 | ctxt->instate = XML_PARSER_DTD; |
| 8272 | NEXT; |
| 8273 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8274 | * Parse the succession of Markup declarations and |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8275 | * PEReferences. |
| 8276 | * Subsequence (markupdecl | PEReference | S)* |
| 8277 | */ |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8278 | while ((RAW != ']') && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8279 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8280 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8281 | |
| 8282 | SKIP_BLANKS; |
| 8283 | xmlParseMarkupDecl(ctxt); |
| 8284 | xmlParsePEReference(ctxt); |
| 8285 | |
| 8286 | /* |
| 8287 | * Pop-up of finished entities. |
| 8288 | */ |
| 8289 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8290 | xmlPopInput(ctxt); |
| 8291 | |
| 8292 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8293 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8294 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8295 | break; |
| 8296 | } |
| 8297 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8298 | if (RAW == ']') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8299 | NEXT; |
| 8300 | SKIP_BLANKS; |
| 8301 | } |
| 8302 | } |
| 8303 | |
| 8304 | /* |
| 8305 | * We should be at the end of the DOCTYPE declaration. |
| 8306 | */ |
| 8307 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8308 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8309 | } |
| 8310 | NEXT; |
| 8311 | } |
| 8312 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8313 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8314 | /** |
| 8315 | * xmlParseAttribute: |
| 8316 | * @ctxt: an XML parser context |
| 8317 | * @value: a xmlChar ** used to store the value of the attribute |
| 8318 | * |
| 8319 | * parse an attribute |
| 8320 | * |
| 8321 | * [41] Attribute ::= Name Eq AttValue |
| 8322 | * |
| 8323 | * [ WFC: No External Entity References ] |
| 8324 | * Attribute values cannot contain direct or indirect entity references |
| 8325 | * to external entities. |
| 8326 | * |
| 8327 | * [ WFC: No < in Attribute Values ] |
| 8328 | * The replacement text of any entity referred to directly or indirectly in |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8329 | * an attribute value (other than "<") must not contain a <. |
| 8330 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8331 | * [ VC: Attribute Value Type ] |
| 8332 | * The attribute must have been declared; the value must be of the type |
| 8333 | * declared for it. |
| 8334 | * |
| 8335 | * [25] Eq ::= S? '=' S? |
| 8336 | * |
| 8337 | * With namespace: |
| 8338 | * |
| 8339 | * [NS 11] Attribute ::= QName Eq AttValue |
| 8340 | * |
| 8341 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 8342 | * definition. |
| 8343 | * |
| 8344 | * Returns the attribute name, and the value in *value. |
| 8345 | */ |
| 8346 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8347 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8348 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8349 | const xmlChar *name; |
| 8350 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8351 | |
| 8352 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 8353 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8354 | name = xmlParseName(ctxt); |
| 8355 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8356 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8357 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8358 | return(NULL); |
| 8359 | } |
| 8360 | |
| 8361 | /* |
| 8362 | * read the value |
| 8363 | */ |
| 8364 | SKIP_BLANKS; |
| 8365 | if (RAW == '=') { |
| 8366 | NEXT; |
| 8367 | SKIP_BLANKS; |
| 8368 | val = xmlParseAttValue(ctxt); |
| 8369 | ctxt->instate = XML_PARSER_CONTENT; |
| 8370 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8371 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8372 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8373 | return(NULL); |
| 8374 | } |
| 8375 | |
| 8376 | /* |
| 8377 | * Check that xml:lang conforms to the specification |
| 8378 | * No more registered as an error, just generate a warning now |
| 8379 | * since this was deprecated in XML second edition |
| 8380 | */ |
| 8381 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 8382 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8383 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8384 | "Malformed value for xml:lang : %s\n", |
| 8385 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8386 | } |
| 8387 | } |
| 8388 | |
| 8389 | /* |
| 8390 | * Check that xml:space conforms to the specification |
| 8391 | */ |
| 8392 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 8393 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 8394 | *(ctxt->space) = 0; |
| 8395 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 8396 | *(ctxt->space) = 1; |
| 8397 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8398 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 8399 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8400 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8401 | } |
| 8402 | } |
| 8403 | |
| 8404 | *value = val; |
| 8405 | return(name); |
| 8406 | } |
| 8407 | |
| 8408 | /** |
| 8409 | * xmlParseStartTag: |
| 8410 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8411 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8412 | * parse a start of tag either for rule element or |
| 8413 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8414 | * |
| 8415 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8416 | * |
| 8417 | * [ WFC: Unique Att Spec ] |
| 8418 | * 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] | 8419 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8420 | * |
| 8421 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8422 | * |
| 8423 | * [ WFC: Unique Att Spec ] |
| 8424 | * 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] | 8425 | * empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8426 | * |
| 8427 | * With namespace: |
| 8428 | * |
| 8429 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8430 | * |
| 8431 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8432 | * |
| 8433 | * Returns the element name parsed |
| 8434 | */ |
| 8435 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8436 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8437 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8438 | const xmlChar *name; |
| 8439 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8440 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8441 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8442 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8443 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8444 | int i; |
| 8445 | |
| 8446 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8447 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8448 | |
| 8449 | name = xmlParseName(ctxt); |
| 8450 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8451 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8452 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8453 | return(NULL); |
| 8454 | } |
| 8455 | |
| 8456 | /* |
| 8457 | * Now parse the attributes, it ends up with the ending |
| 8458 | * |
| 8459 | * (S Attribute)* S? |
| 8460 | */ |
| 8461 | SKIP_BLANKS; |
| 8462 | GROW; |
| 8463 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8464 | while (((RAW != '>') && |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8465 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8466 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8467 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8468 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | |
| 8470 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 8471 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8472 | /* |
| 8473 | * [ WFC: Unique Att Spec ] |
| 8474 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8475 | * start-tag or empty-element tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8476 | */ |
| 8477 | for (i = 0; i < nbatts;i += 2) { |
| 8478 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8479 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8480 | xmlFree(attvalue); |
| 8481 | goto failed; |
| 8482 | } |
| 8483 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8484 | /* |
| 8485 | * Add the pair to atts |
| 8486 | */ |
| 8487 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8488 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8489 | atts = (const xmlChar **) |
| 8490 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8491 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8492 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8493 | if (attvalue != NULL) |
| 8494 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8495 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8496 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8497 | ctxt->atts = atts; |
| 8498 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8499 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8500 | const xmlChar **n; |
| 8501 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8502 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8503 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8504 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8505 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8506 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8507 | if (attvalue != NULL) |
| 8508 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8509 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8510 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8511 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8512 | ctxt->atts = atts; |
| 8513 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8514 | } |
| 8515 | atts[nbatts++] = attname; |
| 8516 | atts[nbatts++] = attvalue; |
| 8517 | atts[nbatts] = NULL; |
| 8518 | atts[nbatts + 1] = NULL; |
| 8519 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8520 | if (attvalue != NULL) |
| 8521 | xmlFree(attvalue); |
| 8522 | } |
| 8523 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 8524 | failed: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8525 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8526 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8527 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8528 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8529 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8530 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8531 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8532 | } |
| 8533 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8534 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8535 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8536 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8537 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8538 | break; |
| 8539 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8540 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8541 | GROW; |
| 8542 | } |
| 8543 | |
| 8544 | /* |
| 8545 | * SAX: Start of Element ! |
| 8546 | */ |
| 8547 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8548 | (!ctxt->disableSAX)) { |
| 8549 | if (nbatts > 0) |
| 8550 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8551 | else |
| 8552 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8553 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8554 | |
| 8555 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8556 | /* Free only the content strings */ |
| 8557 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8558 | if (atts[i] != NULL) |
| 8559 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8560 | } |
| 8561 | return(name); |
| 8562 | } |
| 8563 | |
| 8564 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8565 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8566 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8567 | * @line: line of the start tag |
| 8568 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8569 | * |
| 8570 | * parse an end of tag |
| 8571 | * |
| 8572 | * [42] ETag ::= '</' Name S? '>' |
| 8573 | * |
| 8574 | * With namespace |
| 8575 | * |
| 8576 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8577 | */ |
| 8578 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8579 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8580 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8581 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8582 | |
| 8583 | GROW; |
| 8584 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8585 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8586 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8587 | return; |
| 8588 | } |
| 8589 | SKIP(2); |
| 8590 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8591 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8592 | |
| 8593 | /* |
| 8594 | * We should definitely be at the ending "S? '>'" part |
| 8595 | */ |
| 8596 | GROW; |
| 8597 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8598 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8599 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8600 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8601 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | |
| 8603 | /* |
| 8604 | * [ WFC: Element Type Match ] |
| 8605 | * 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] | 8606 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8607 | * |
| 8608 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8609 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8610 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8611 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8612 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8613 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | /* |
| 8617 | * SAX: End of Tag |
| 8618 | */ |
| 8619 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8620 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8621 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8622 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8623 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8624 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8625 | return; |
| 8626 | } |
| 8627 | |
| 8628 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8629 | * xmlParseEndTag: |
| 8630 | * @ctxt: an XML parser context |
| 8631 | * |
| 8632 | * parse an end of tag |
| 8633 | * |
| 8634 | * [42] ETag ::= '</' Name S? '>' |
| 8635 | * |
| 8636 | * With namespace |
| 8637 | * |
| 8638 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8639 | */ |
| 8640 | |
| 8641 | void |
| 8642 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8643 | xmlParseEndTag1(ctxt, 0); |
| 8644 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8645 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8646 | |
| 8647 | /************************************************************************ |
| 8648 | * * |
| 8649 | * SAX 2 specific operations * |
| 8650 | * * |
| 8651 | ************************************************************************/ |
| 8652 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8653 | /* |
| 8654 | * xmlGetNamespace: |
| 8655 | * @ctxt: an XML parser context |
| 8656 | * @prefix: the prefix to lookup |
| 8657 | * |
| 8658 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8659 | * The prefix must come from the @ctxt->dict dictionnary |
| 8660 | * |
| 8661 | * Returns the namespace name or NULL if not bound |
| 8662 | */ |
| 8663 | static const xmlChar * |
| 8664 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8665 | int i; |
| 8666 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8667 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8668 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8669 | if (ctxt->nsTab[i] == prefix) { |
| 8670 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8671 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8672 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8673 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8674 | return(NULL); |
| 8675 | } |
| 8676 | |
| 8677 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8678 | * xmlParseQName: |
| 8679 | * @ctxt: an XML parser context |
| 8680 | * @prefix: pointer to store the prefix part |
| 8681 | * |
| 8682 | * parse an XML Namespace QName |
| 8683 | * |
| 8684 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8685 | * [7] Prefix ::= NCName |
| 8686 | * [8] LocalPart ::= NCName |
| 8687 | * |
| 8688 | * Returns the Name parsed or NULL |
| 8689 | */ |
| 8690 | |
| 8691 | static const xmlChar * |
| 8692 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8693 | const xmlChar *l, *p; |
| 8694 | |
| 8695 | GROW; |
| 8696 | |
| 8697 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8698 | if (l == NULL) { |
| 8699 | if (CUR == ':') { |
| 8700 | l = xmlParseName(ctxt); |
| 8701 | if (l != NULL) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8702 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8703 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8704 | *prefix = NULL; |
| 8705 | return(l); |
| 8706 | } |
| 8707 | } |
| 8708 | return(NULL); |
| 8709 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8710 | if (CUR == ':') { |
| 8711 | NEXT; |
| 8712 | p = l; |
| 8713 | l = xmlParseNCName(ctxt); |
| 8714 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8715 | xmlChar *tmp; |
| 8716 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8717 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8718 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8719 | l = xmlParseNmtoken(ctxt); |
| 8720 | if (l == NULL) |
| 8721 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8722 | else { |
| 8723 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8724 | xmlFree((char *)l); |
| 8725 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8726 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8727 | if (tmp != NULL) xmlFree(tmp); |
| 8728 | *prefix = NULL; |
| 8729 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8730 | } |
| 8731 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8732 | xmlChar *tmp; |
| 8733 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8734 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8735 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8736 | NEXT; |
| 8737 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8738 | if (tmp != NULL) { |
| 8739 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8740 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8741 | if (tmp != NULL) xmlFree(tmp); |
| 8742 | *prefix = p; |
| 8743 | return(l); |
| 8744 | } |
| 8745 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8746 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8747 | if (tmp != NULL) xmlFree(tmp); |
| 8748 | *prefix = p; |
| 8749 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8750 | } |
| 8751 | *prefix = p; |
| 8752 | } else |
| 8753 | *prefix = NULL; |
| 8754 | return(l); |
| 8755 | } |
| 8756 | |
| 8757 | /** |
| 8758 | * xmlParseQNameAndCompare: |
| 8759 | * @ctxt: an XML parser context |
| 8760 | * @name: the localname |
| 8761 | * @prefix: the prefix, if any. |
| 8762 | * |
| 8763 | * parse an XML name and compares for match |
| 8764 | * (specialized for endtag parsing) |
| 8765 | * |
| 8766 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8767 | * and the name for mismatch |
| 8768 | */ |
| 8769 | |
| 8770 | static const xmlChar * |
| 8771 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8772 | xmlChar const *prefix) { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8773 | const xmlChar *cmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8774 | const xmlChar *in; |
| 8775 | const xmlChar *ret; |
| 8776 | const xmlChar *prefix2; |
| 8777 | |
| 8778 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8779 | |
| 8780 | GROW; |
| 8781 | in = ctxt->input->cur; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8782 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8783 | cmp = prefix; |
| 8784 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8785 | ++in; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8786 | ++cmp; |
| 8787 | } |
| 8788 | if ((*cmp == 0) && (*in == ':')) { |
| 8789 | in++; |
| 8790 | cmp = name; |
| 8791 | while (*in != 0 && *in == *cmp) { |
| 8792 | ++in; |
| 8793 | ++cmp; |
| 8794 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8795 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8796 | /* success */ |
| 8797 | ctxt->input->cur = in; |
| 8798 | return((const xmlChar*) 1); |
| 8799 | } |
| 8800 | } |
| 8801 | /* |
| 8802 | * all strings coms from the dictionary, equality can be done directly |
| 8803 | */ |
| 8804 | ret = xmlParseQName (ctxt, &prefix2); |
| 8805 | if ((ret == name) && (prefix == prefix2)) |
| 8806 | return((const xmlChar*) 1); |
| 8807 | return ret; |
| 8808 | } |
| 8809 | |
| 8810 | /** |
| 8811 | * xmlParseAttValueInternal: |
| 8812 | * @ctxt: an XML parser context |
| 8813 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8814 | * @alloc: whether the attribute was reallocated as a new string |
| 8815 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8816 | * |
| 8817 | * parse a value for an attribute. |
| 8818 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8819 | * directly from the data buffer. |
| 8820 | * |
| 8821 | * 3.3.3 Attribute-Value Normalization: |
| 8822 | * Before the value of an attribute is passed to the application or |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8823 | * checked for validity, the XML processor must normalize it as follows: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8824 | * - a character reference is processed by appending the referenced |
| 8825 | * character to the attribute value |
| 8826 | * - an entity reference is processed by recursively processing the |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8827 | * replacement text of the entity |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8828 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8829 | * appending #x20 to the normalized value, except that only a single |
| 8830 | * #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] | 8831 | * parsed entity or the literal entity value of an internal parsed entity |
| 8832 | * - other characters are processed by appending them to the normalized value |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8833 | * If the declared value is not CDATA, then the XML processor must further |
| 8834 | * process the normalized attribute value by discarding any leading and |
| 8835 | * trailing space (#x20) characters, and by replacing sequences of space |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8836 | * (#x20) characters by a single space (#x20) character. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8837 | * All attributes for which no declaration has been read should be treated |
| 8838 | * by a non-validating parser as if declared CDATA. |
| 8839 | * |
| 8840 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8841 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8842 | */ |
| 8843 | |
| 8844 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8845 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8846 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8847 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8848 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8849 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8850 | xmlChar *ret = NULL; |
| 8851 | |
| 8852 | GROW; |
| 8853 | in = (xmlChar *) CUR_PTR; |
| 8854 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8855 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8856 | return (NULL); |
| 8857 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8858 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8859 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8860 | /* |
| 8861 | * try to handle in this routine the most common case where no |
| 8862 | * allocation of a new string is required and where content is |
| 8863 | * pure ASCII. |
| 8864 | */ |
| 8865 | limit = *in++; |
| 8866 | end = ctxt->input->end; |
| 8867 | start = in; |
| 8868 | if (in >= end) { |
| 8869 | const xmlChar *oldbase = ctxt->input->base; |
| 8870 | GROW; |
| 8871 | if (oldbase != ctxt->input->base) { |
| 8872 | long delta = ctxt->input->base - oldbase; |
| 8873 | start = start + delta; |
| 8874 | in = in + delta; |
| 8875 | } |
| 8876 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8877 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8878 | if (normalize) { |
| 8879 | /* |
| 8880 | * Skip any leading spaces |
| 8881 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8882 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8883 | ((*in == 0x20) || (*in == 0x9) || |
| 8884 | (*in == 0xA) || (*in == 0xD))) { |
| 8885 | in++; |
| 8886 | start = in; |
| 8887 | if (in >= end) { |
| 8888 | const xmlChar *oldbase = ctxt->input->base; |
| 8889 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8890 | if (ctxt->instate == XML_PARSER_EOF) |
| 8891 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8892 | if (oldbase != ctxt->input->base) { |
| 8893 | long delta = ctxt->input->base - oldbase; |
| 8894 | start = start + delta; |
| 8895 | in = in + delta; |
| 8896 | } |
| 8897 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8898 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8899 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8900 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8901 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8902 | return(NULL); |
| 8903 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8904 | } |
| 8905 | } |
| 8906 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8907 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8908 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 8909 | if (in >= end) { |
| 8910 | const xmlChar *oldbase = ctxt->input->base; |
| 8911 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8912 | if (ctxt->instate == XML_PARSER_EOF) |
| 8913 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8914 | if (oldbase != ctxt->input->base) { |
| 8915 | long delta = ctxt->input->base - oldbase; |
| 8916 | start = start + delta; |
| 8917 | in = in + delta; |
| 8918 | } |
| 8919 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8920 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8921 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8922 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8923 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8924 | return(NULL); |
| 8925 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8926 | } |
| 8927 | } |
| 8928 | last = in; |
| 8929 | /* |
| 8930 | * skip the trailing blanks |
| 8931 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 8932 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 8933 | while ((in < end) && (*in != limit) && |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8934 | ((*in == 0x20) || (*in == 0x9) || |
| 8935 | (*in == 0xA) || (*in == 0xD))) { |
| 8936 | in++; |
| 8937 | if (in >= end) { |
| 8938 | const xmlChar *oldbase = ctxt->input->base; |
| 8939 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8940 | if (ctxt->instate == XML_PARSER_EOF) |
| 8941 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8942 | if (oldbase != ctxt->input->base) { |
| 8943 | long delta = ctxt->input->base - oldbase; |
| 8944 | start = start + delta; |
| 8945 | in = in + delta; |
| 8946 | last = last + delta; |
| 8947 | } |
| 8948 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8949 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8950 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8951 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8952 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8953 | return(NULL); |
| 8954 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8955 | } |
| 8956 | } |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8957 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8958 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8959 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8960 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8961 | return(NULL); |
| 8962 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8963 | if (*in != limit) goto need_complex; |
| 8964 | } else { |
| 8965 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8966 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8967 | in++; |
| 8968 | if (in >= end) { |
| 8969 | const xmlChar *oldbase = ctxt->input->base; |
| 8970 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 8971 | if (ctxt->instate == XML_PARSER_EOF) |
| 8972 | return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8973 | if (oldbase != ctxt->input->base) { |
| 8974 | long delta = ctxt->input->base - oldbase; |
| 8975 | start = start + delta; |
| 8976 | in = in + delta; |
| 8977 | } |
| 8978 | end = ctxt->input->end; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8979 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8980 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8981 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8982 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8983 | return(NULL); |
| 8984 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8985 | } |
| 8986 | } |
| 8987 | last = in; |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8988 | if (((in - start) > XML_MAX_TEXT_LENGTH) && |
| 8989 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 8990 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
Michael Wood | fb27e2c | 2012-09-28 08:59:33 +0200 | [diff] [blame] | 8991 | "AttValue length too long\n"); |
Daniel Veillard | e17db99 | 2012-07-19 11:25:16 +0800 | [diff] [blame] | 8992 | return(NULL); |
| 8993 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8994 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8995 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8996 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8997 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8998 | *len = last - start; |
| 8999 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9000 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9001 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9002 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9003 | } |
| 9004 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9005 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9006 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9007 | need_complex: |
| 9008 | if (alloc) *alloc = 1; |
| 9009 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9010 | } |
| 9011 | |
| 9012 | /** |
| 9013 | * xmlParseAttribute2: |
| 9014 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9015 | * @pref: the element prefix |
| 9016 | * @elem: the element name |
| 9017 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9018 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9019 | * @len: an int * to save the length of the attribute |
| 9020 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9021 | * |
| 9022 | * parse an attribute in the new SAX2 framework. |
| 9023 | * |
| 9024 | * Returns the attribute name, and the value in *value, . |
| 9025 | */ |
| 9026 | |
| 9027 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9028 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9029 | const xmlChar * pref, const xmlChar * elem, |
| 9030 | const xmlChar ** prefix, xmlChar ** value, |
| 9031 | int *len, int *alloc) |
| 9032 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9033 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 9034 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9035 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9036 | |
| 9037 | *value = NULL; |
| 9038 | GROW; |
| 9039 | name = xmlParseQName(ctxt, prefix); |
| 9040 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9041 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9042 | "error parsing attribute name\n"); |
| 9043 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9044 | } |
| 9045 | |
| 9046 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9047 | * get the type if needed |
| 9048 | */ |
| 9049 | if (ctxt->attsSpecial != NULL) { |
| 9050 | int type; |
| 9051 | |
| 9052 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9053 | pref, elem, *prefix, name); |
| 9054 | if (type != 0) |
| 9055 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9056 | } |
| 9057 | |
| 9058 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9059 | * read the value |
| 9060 | */ |
| 9061 | SKIP_BLANKS; |
| 9062 | if (RAW == '=') { |
| 9063 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9064 | SKIP_BLANKS; |
| 9065 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 9066 | if (normalize) { |
| 9067 | /* |
| 9068 | * Sometimes a second normalisation pass for spaces is needed |
| 9069 | * but that only happens if charrefs or entities refernces |
| 9070 | * have been used in the attribute value, i.e. the attribute |
| 9071 | * value have been extracted in an allocated string already. |
| 9072 | */ |
| 9073 | if (*alloc) { |
| 9074 | const xmlChar *val2; |
| 9075 | |
| 9076 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9077 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9078 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 9079 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9080 | } |
| 9081 | } |
| 9082 | } |
| 9083 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9084 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9085 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 9086 | "Specification mandate value for attribute %s\n", |
| 9087 | name); |
| 9088 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9089 | } |
| 9090 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9091 | if (*prefix == ctxt->str_xml) { |
| 9092 | /* |
| 9093 | * Check that xml:lang conforms to the specification |
| 9094 | * No more registered as an error, just generate a warning now |
| 9095 | * since this was deprecated in XML second edition |
| 9096 | */ |
| 9097 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 9098 | internal_val = xmlStrndup(val, *len); |
| 9099 | if (!xmlCheckLanguageID(internal_val)) { |
| 9100 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 9101 | "Malformed value for xml:lang : %s\n", |
| 9102 | internal_val, NULL); |
| 9103 | } |
| 9104 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9105 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9106 | /* |
| 9107 | * Check that xml:space conforms to the specification |
| 9108 | */ |
| 9109 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 9110 | internal_val = xmlStrndup(val, *len); |
| 9111 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 9112 | *(ctxt->space) = 0; |
| 9113 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 9114 | *(ctxt->space) = 1; |
| 9115 | else { |
| 9116 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 9117 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 9118 | internal_val, NULL); |
| 9119 | } |
| 9120 | } |
| 9121 | if (internal_val) { |
| 9122 | xmlFree(internal_val); |
| 9123 | } |
| 9124 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9125 | |
| 9126 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 9127 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9128 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9129 | /** |
| 9130 | * xmlParseStartTag2: |
| 9131 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9132 | * |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9133 | * parse a start of tag either for rule element or |
| 9134 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 9135 | * This routine is called when running SAX2 parsing |
| 9136 | * |
| 9137 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 9138 | * |
| 9139 | * [ WFC: Unique Att Spec ] |
| 9140 | * 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] | 9141 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9142 | * |
| 9143 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 9144 | * |
| 9145 | * [ WFC: Unique Att Spec ] |
| 9146 | * 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] | 9147 | * empty-element tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9148 | * |
| 9149 | * With namespace: |
| 9150 | * |
| 9151 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 9152 | * |
| 9153 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 9154 | * |
| 9155 | * Returns the element name parsed |
| 9156 | */ |
| 9157 | |
| 9158 | static const xmlChar * |
| 9159 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9160 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9161 | const xmlChar *localname; |
| 9162 | const xmlChar *prefix; |
| 9163 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9164 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9165 | const xmlChar *nsname; |
| 9166 | xmlChar *attvalue; |
| 9167 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9168 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9169 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9170 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9171 | const xmlChar *base; |
| 9172 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9173 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9174 | |
| 9175 | if (RAW != '<') return(NULL); |
| 9176 | NEXT1; |
| 9177 | |
| 9178 | /* |
| 9179 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 9180 | * point since the attribute values may be stored as pointers to |
| 9181 | * the buffer and calling SHRINK would destroy them ! |
| 9182 | * The Shrinking is only possible once the full set of attribute |
| 9183 | * callbacks have been done. |
| 9184 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9185 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9186 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9187 | base = ctxt->input->base; |
| 9188 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9189 | oldline = ctxt->input->line; |
| 9190 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9191 | nbatts = 0; |
| 9192 | nratts = 0; |
| 9193 | nbdef = 0; |
| 9194 | nbNs = 0; |
| 9195 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 9196 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 9197 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9198 | |
| 9199 | localname = xmlParseQName(ctxt, &prefix); |
| 9200 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9201 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 9202 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9203 | return(NULL); |
| 9204 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9205 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9206 | |
| 9207 | /* |
| 9208 | * Now parse the attributes, it ends up with the ending |
| 9209 | * |
| 9210 | * (S Attribute)* S? |
| 9211 | */ |
| 9212 | SKIP_BLANKS; |
| 9213 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9214 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9215 | |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9216 | while (((RAW != '>') && |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9217 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9218 | (IS_BYTE_CHAR(RAW))) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9219 | const xmlChar *q = CUR_PTR; |
| 9220 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9221 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9222 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9223 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 9224 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9225 | if (ctxt->input->base != base) { |
| 9226 | if ((attvalue != NULL) && (alloc != 0)) |
| 9227 | xmlFree(attvalue); |
| 9228 | attvalue = NULL; |
| 9229 | goto base_changed; |
| 9230 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9231 | if ((attname != NULL) && (attvalue != NULL)) { |
| 9232 | if (len < 0) len = xmlStrlen(attvalue); |
| 9233 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9234 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9235 | xmlURIPtr uri; |
| 9236 | |
| 9237 | if (*URL != 0) { |
| 9238 | uri = xmlParseURI((const char *) URL); |
| 9239 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9240 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9241 | "xmlns: '%s' is not a valid URI\n", |
| 9242 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9243 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 9244 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9245 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9246 | "xmlns: URI %s is not absolute\n", |
| 9247 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9248 | } |
| 9249 | xmlFreeURI(uri); |
| 9250 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9251 | if (URL == ctxt->str_xml_ns) { |
| 9252 | if (attname != ctxt->str_xml) { |
| 9253 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9254 | "xml namespace URI cannot be the default namespace\n", |
| 9255 | NULL, NULL, NULL); |
| 9256 | } |
| 9257 | goto skip_default_ns; |
| 9258 | } |
| 9259 | if ((len == 29) && |
| 9260 | (xmlStrEqual(URL, |
| 9261 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9262 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9263 | "reuse of the xmlns namespace name is forbidden\n", |
| 9264 | NULL, NULL, NULL); |
| 9265 | goto skip_default_ns; |
| 9266 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9267 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9268 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9269 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9270 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9271 | for (j = 1;j <= nbNs;j++) |
| 9272 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9273 | break; |
| 9274 | if (j <= nbNs) |
| 9275 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 9276 | else |
| 9277 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9278 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9279 | if (alloc != 0) xmlFree(attvalue); |
| 9280 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9281 | continue; |
| 9282 | } |
| 9283 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9284 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9285 | xmlURIPtr uri; |
| 9286 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9287 | if (attname == ctxt->str_xml) { |
| 9288 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9289 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9290 | "xml namespace prefix mapped to wrong URI\n", |
| 9291 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9292 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9293 | /* |
| 9294 | * Do not keep a namespace definition node |
| 9295 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9296 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9297 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9298 | if (URL == ctxt->str_xml_ns) { |
| 9299 | if (attname != ctxt->str_xml) { |
| 9300 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9301 | "xml namespace URI mapped to wrong prefix\n", |
| 9302 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9303 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9304 | goto skip_ns; |
| 9305 | } |
| 9306 | if (attname == ctxt->str_xmlns) { |
| 9307 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9308 | "redefinition of the xmlns prefix is forbidden\n", |
| 9309 | NULL, NULL, NULL); |
| 9310 | goto skip_ns; |
| 9311 | } |
| 9312 | if ((len == 29) && |
| 9313 | (xmlStrEqual(URL, |
| 9314 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9315 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9316 | "reuse of the xmlns namespace name is forbidden\n", |
| 9317 | NULL, NULL, NULL); |
| 9318 | goto skip_ns; |
| 9319 | } |
| 9320 | if ((URL == NULL) || (URL[0] == 0)) { |
| 9321 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9322 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 9323 | attname, NULL, NULL); |
| 9324 | goto skip_ns; |
| 9325 | } else { |
| 9326 | uri = xmlParseURI((const char *) URL); |
| 9327 | if (uri == NULL) { |
| 9328 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9329 | "xmlns:%s: '%s' is not a valid URI\n", |
| 9330 | attname, URL, NULL); |
| 9331 | } else { |
| 9332 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 9333 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9334 | "xmlns:%s: URI %s is not absolute\n", |
| 9335 | attname, URL, NULL); |
| 9336 | } |
| 9337 | xmlFreeURI(uri); |
| 9338 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9339 | } |
| 9340 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9341 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9342 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9343 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9344 | for (j = 1;j <= nbNs;j++) |
| 9345 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9346 | break; |
| 9347 | if (j <= nbNs) |
| 9348 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 9349 | else |
| 9350 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9351 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9352 | if (alloc != 0) xmlFree(attvalue); |
| 9353 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 9354 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9355 | continue; |
| 9356 | } |
| 9357 | |
| 9358 | /* |
| 9359 | * Add the pair to atts |
| 9360 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9361 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9362 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9363 | if (attvalue[len] == 0) |
| 9364 | xmlFree(attvalue); |
| 9365 | goto failed; |
| 9366 | } |
| 9367 | maxatts = ctxt->maxatts; |
| 9368 | atts = ctxt->atts; |
| 9369 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9370 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9371 | atts[nbatts++] = attname; |
| 9372 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9373 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9374 | atts[nbatts++] = attvalue; |
| 9375 | attvalue += len; |
| 9376 | atts[nbatts++] = attvalue; |
| 9377 | /* |
| 9378 | * tag if some deallocation is needed |
| 9379 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9380 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9381 | } else { |
| 9382 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 9383 | xmlFree(attvalue); |
| 9384 | } |
| 9385 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9386 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9387 | |
| 9388 | GROW |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9389 | if (ctxt->instate == XML_PARSER_EOF) |
| 9390 | break; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9391 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9392 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9393 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9394 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9395 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9396 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9397 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9398 | } |
| 9399 | SKIP_BLANKS; |
| 9400 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 9401 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9402 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9403 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9404 | break; |
| 9405 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9406 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9407 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9408 | } |
| 9409 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9410 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9411 | * The attributes defaulting |
| 9412 | */ |
| 9413 | if (ctxt->attsDefault != NULL) { |
| 9414 | xmlDefAttrsPtr defaults; |
| 9415 | |
| 9416 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 9417 | if (defaults != NULL) { |
| 9418 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9419 | attname = defaults->values[5 * i]; |
| 9420 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9421 | |
| 9422 | /* |
| 9423 | * special work for namespaces defaulted defs |
| 9424 | */ |
| 9425 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 9426 | /* |
| 9427 | * check that it's not a defined namespace |
| 9428 | */ |
| 9429 | for (j = 1;j <= nbNs;j++) |
| 9430 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9431 | break; |
| 9432 | if (j <= nbNs) continue; |
| 9433 | |
| 9434 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9435 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9436 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9437 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9438 | nbNs++; |
| 9439 | } |
| 9440 | } else if (aprefix == ctxt->str_xmlns) { |
| 9441 | /* |
| 9442 | * check that it's not a defined namespace |
| 9443 | */ |
| 9444 | for (j = 1;j <= nbNs;j++) |
| 9445 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9446 | break; |
| 9447 | if (j <= nbNs) continue; |
| 9448 | |
| 9449 | nsname = xmlGetNamespace(ctxt, attname); |
| 9450 | if (nsname != defaults->values[2]) { |
| 9451 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9452 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9453 | nbNs++; |
| 9454 | } |
| 9455 | } else { |
| 9456 | /* |
| 9457 | * check that it's not a defined attribute |
| 9458 | */ |
| 9459 | for (j = 0;j < nbatts;j+=5) { |
| 9460 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 9461 | break; |
| 9462 | } |
| 9463 | if (j < nbatts) continue; |
| 9464 | |
| 9465 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9466 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 9467 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9468 | } |
| 9469 | maxatts = ctxt->maxatts; |
| 9470 | atts = ctxt->atts; |
| 9471 | } |
| 9472 | atts[nbatts++] = attname; |
| 9473 | atts[nbatts++] = aprefix; |
| 9474 | if (aprefix == NULL) |
| 9475 | atts[nbatts++] = NULL; |
| 9476 | else |
| 9477 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9478 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 9479 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 9480 | if ((ctxt->standalone == 1) && |
| 9481 | (defaults->values[5 * i + 4] != NULL)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9482 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9483 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 9484 | attname, localname); |
| 9485 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9486 | nbdef++; |
| 9487 | } |
| 9488 | } |
| 9489 | } |
| 9490 | } |
| 9491 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9492 | /* |
| 9493 | * The attributes checkings |
| 9494 | */ |
| 9495 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 9496 | /* |
| 9497 | * The default namespace does not apply to attribute names. |
| 9498 | */ |
| 9499 | if (atts[i + 1] != NULL) { |
| 9500 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 9501 | if (nsname == NULL) { |
| 9502 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9503 | "Namespace prefix %s for %s on %s is not defined\n", |
| 9504 | atts[i + 1], atts[i], localname); |
| 9505 | } |
| 9506 | atts[i + 2] = nsname; |
| 9507 | } else |
| 9508 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9509 | /* |
| 9510 | * [ WFC: Unique Att Spec ] |
| 9511 | * No attribute name may appear more than once in the same |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9512 | * start-tag or empty-element tag. |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9513 | * As extended by the Namespace in XML REC. |
| 9514 | */ |
| 9515 | for (j = 0; j < i;j += 5) { |
| 9516 | if (atts[i] == atts[j]) { |
| 9517 | if (atts[i+1] == atts[j+1]) { |
| 9518 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 9519 | break; |
| 9520 | } |
| 9521 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 9522 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 9523 | "Namespaced Attribute %s in '%s' redefined\n", |
| 9524 | atts[i], nsname, NULL); |
| 9525 | break; |
| 9526 | } |
| 9527 | } |
| 9528 | } |
| 9529 | } |
| 9530 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9531 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9532 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9533 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9534 | "Namespace prefix %s on %s is not defined\n", |
| 9535 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9536 | } |
| 9537 | *pref = prefix; |
| 9538 | *URI = nsname; |
| 9539 | |
| 9540 | /* |
| 9541 | * SAX: Start of Element ! |
| 9542 | */ |
| 9543 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9544 | (!ctxt->disableSAX)) { |
| 9545 | if (nbNs > 0) |
| 9546 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9547 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9548 | nbatts / 5, nbdef, atts); |
| 9549 | else |
| 9550 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9551 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9552 | } |
| 9553 | |
| 9554 | /* |
| 9555 | * Free up attribute allocated strings if needed |
| 9556 | */ |
| 9557 | if (attval != 0) { |
| 9558 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9559 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9560 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9561 | } |
| 9562 | |
| 9563 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9564 | |
| 9565 | base_changed: |
| 9566 | /* |
| 9567 | * the attribute strings are valid iif the base didn't changed |
| 9568 | */ |
| 9569 | if (attval != 0) { |
| 9570 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9571 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9572 | xmlFree((xmlChar *) atts[i]); |
| 9573 | } |
| 9574 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9575 | ctxt->input->line = oldline; |
| 9576 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9577 | if (ctxt->wellFormed == 1) { |
| 9578 | goto reparse; |
| 9579 | } |
| 9580 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9581 | } |
| 9582 | |
| 9583 | /** |
| 9584 | * xmlParseEndTag2: |
| 9585 | * @ctxt: an XML parser context |
| 9586 | * @line: line of the start tag |
| 9587 | * @nsNr: number of namespaces on the start tag |
| 9588 | * |
| 9589 | * parse an end of tag |
| 9590 | * |
| 9591 | * [42] ETag ::= '</' Name S? '>' |
| 9592 | * |
| 9593 | * With namespace |
| 9594 | * |
| 9595 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9596 | */ |
| 9597 | |
| 9598 | static void |
| 9599 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9600 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9601 | const xmlChar *name; |
| 9602 | |
| 9603 | GROW; |
| 9604 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9605 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9606 | return; |
| 9607 | } |
| 9608 | SKIP(2); |
| 9609 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9610 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9611 | if (ctxt->input->cur[tlen] == '>') { |
| 9612 | ctxt->input->cur += tlen + 1; |
| 9613 | goto done; |
| 9614 | } |
| 9615 | ctxt->input->cur += tlen; |
| 9616 | name = (xmlChar*)1; |
| 9617 | } else { |
| 9618 | if (prefix == NULL) |
| 9619 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9620 | else |
| 9621 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9622 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9623 | |
| 9624 | /* |
| 9625 | * We should definitely be at the ending "S? '>'" part |
| 9626 | */ |
| 9627 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9628 | if (ctxt->instate == XML_PARSER_EOF) |
| 9629 | return; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9630 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9631 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9632 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9633 | } else |
| 9634 | NEXT1; |
| 9635 | |
| 9636 | /* |
| 9637 | * [ WFC: Element Type Match ] |
| 9638 | * 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] | 9639 | * start-tag. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9640 | * |
| 9641 | */ |
| 9642 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9643 | if (name == NULL) name = BAD_CAST "unparseable"; |
Daniel Veillard | 852505b | 2009-08-23 15:44:48 +0200 | [diff] [blame] | 9644 | if ((line == 0) && (ctxt->node != NULL)) |
| 9645 | line = ctxt->node->line; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9646 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9647 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9648 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9649 | } |
| 9650 | |
| 9651 | /* |
| 9652 | * SAX: End of Tag |
| 9653 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9654 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9655 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9656 | (!ctxt->disableSAX)) |
| 9657 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9658 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9659 | spacePop(ctxt); |
| 9660 | if (nsNr != 0) |
| 9661 | nsPop(ctxt, nsNr); |
| 9662 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9663 | } |
| 9664 | |
| 9665 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9666 | * xmlParseCDSect: |
| 9667 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9668 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9669 | * Parse escaped pure raw content. |
| 9670 | * |
| 9671 | * [18] CDSect ::= CDStart CData CDEnd |
| 9672 | * |
| 9673 | * [19] CDStart ::= '<![CDATA[' |
| 9674 | * |
| 9675 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9676 | * |
| 9677 | * [21] CDEnd ::= ']]>' |
| 9678 | */ |
| 9679 | void |
| 9680 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9681 | xmlChar *buf = NULL; |
| 9682 | int len = 0; |
| 9683 | int size = XML_PARSER_BUFFER_SIZE; |
| 9684 | int r, rl; |
| 9685 | int s, sl; |
| 9686 | int cur, l; |
| 9687 | int count = 0; |
| 9688 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9689 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9690 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9691 | SKIP(9); |
| 9692 | } else |
| 9693 | return; |
| 9694 | |
| 9695 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9696 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9697 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9698 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9699 | ctxt->instate = XML_PARSER_CONTENT; |
| 9700 | return; |
| 9701 | } |
| 9702 | NEXTL(rl); |
| 9703 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9704 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9705 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9706 | ctxt->instate = XML_PARSER_CONTENT; |
| 9707 | return; |
| 9708 | } |
| 9709 | NEXTL(sl); |
| 9710 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9711 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9712 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9713 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9714 | return; |
| 9715 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9716 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9717 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9718 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9719 | xmlChar *tmp; |
| 9720 | |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9721 | if ((size > XML_MAX_TEXT_LENGTH) && |
| 9722 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9723 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
| 9724 | "CData section too big found", NULL); |
| 9725 | xmlFree (buf); |
| 9726 | return; |
| 9727 | } |
| 9728 | tmp = (xmlChar *) xmlRealloc(buf, size * 2 * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9729 | if (tmp == NULL) { |
| 9730 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9731 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9732 | return; |
| 9733 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9734 | buf = tmp; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 9735 | size *= 2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9736 | } |
| 9737 | COPY_BUF(rl,buf,len,r); |
| 9738 | r = s; |
| 9739 | rl = sl; |
| 9740 | s = cur; |
| 9741 | sl = l; |
| 9742 | count++; |
| 9743 | if (count > 50) { |
| 9744 | GROW; |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 9745 | if (ctxt->instate == XML_PARSER_EOF) { |
| 9746 | xmlFree(buf); |
| 9747 | return; |
| 9748 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9749 | count = 0; |
| 9750 | } |
| 9751 | NEXTL(l); |
| 9752 | cur = CUR_CHAR(l); |
| 9753 | } |
| 9754 | buf[len] = 0; |
| 9755 | ctxt->instate = XML_PARSER_CONTENT; |
| 9756 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9757 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9758 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9759 | xmlFree(buf); |
| 9760 | return; |
| 9761 | } |
| 9762 | NEXTL(l); |
| 9763 | |
| 9764 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9765 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9766 | */ |
| 9767 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9768 | if (ctxt->sax->cdataBlock != NULL) |
| 9769 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9770 | else if (ctxt->sax->characters != NULL) |
| 9771 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9772 | } |
| 9773 | xmlFree(buf); |
| 9774 | } |
| 9775 | |
| 9776 | /** |
| 9777 | * xmlParseContent: |
| 9778 | * @ctxt: an XML parser context |
| 9779 | * |
| 9780 | * Parse a content: |
| 9781 | * |
| 9782 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9783 | */ |
| 9784 | |
| 9785 | void |
| 9786 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9787 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9788 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9789 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9790 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9791 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9792 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9793 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9794 | |
| 9795 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9796 | * First case : a Processing Instruction. |
| 9797 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9798 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9799 | xmlParsePI(ctxt); |
| 9800 | } |
| 9801 | |
| 9802 | /* |
| 9803 | * Second case : a CDSection |
| 9804 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9805 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9806 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9807 | xmlParseCDSect(ctxt); |
| 9808 | } |
| 9809 | |
| 9810 | /* |
| 9811 | * Third case : a comment |
| 9812 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9813 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9814 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9815 | xmlParseComment(ctxt); |
| 9816 | ctxt->instate = XML_PARSER_CONTENT; |
| 9817 | } |
| 9818 | |
| 9819 | /* |
| 9820 | * Fourth case : a sub-element. |
| 9821 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9822 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9823 | xmlParseElement(ctxt); |
| 9824 | } |
| 9825 | |
| 9826 | /* |
| 9827 | * Fifth case : a reference. If if has not been resolved, |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9828 | * parsing returns it's Name, create the node |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9829 | */ |
| 9830 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9831 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9832 | xmlParseReference(ctxt); |
| 9833 | } |
| 9834 | |
| 9835 | /* |
| 9836 | * Last case, text. Note that References are handled directly. |
| 9837 | */ |
| 9838 | else { |
| 9839 | xmlParseCharData(ctxt, 0); |
| 9840 | } |
| 9841 | |
| 9842 | GROW; |
| 9843 | /* |
| 9844 | * Pop-up of finished entities. |
| 9845 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9846 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9847 | xmlPopInput(ctxt); |
| 9848 | SHRINK; |
| 9849 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9850 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9851 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9852 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9853 | ctxt->instate = XML_PARSER_EOF; |
| 9854 | break; |
| 9855 | } |
| 9856 | } |
| 9857 | } |
| 9858 | |
| 9859 | /** |
| 9860 | * xmlParseElement: |
| 9861 | * @ctxt: an XML parser context |
| 9862 | * |
| 9863 | * parse an XML element, this is highly recursive |
| 9864 | * |
| 9865 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9866 | * |
| 9867 | * [ WFC: Element Type Match ] |
| 9868 | * 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] | 9869 | * start-tag. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9870 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9871 | */ |
| 9872 | |
| 9873 | void |
| 9874 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 9875 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 9876 | const xmlChar *prefix = NULL; |
| 9877 | const xmlChar *URI = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9878 | xmlParserNodeInfo node_info; |
Daniel Veillard | ed35d3d | 2012-05-11 10:52:27 +0800 | [diff] [blame] | 9879 | int line, tlen = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9880 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9881 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9882 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 9883 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 9884 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9885 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9886 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 9887 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9888 | ctxt->instate = XML_PARSER_EOF; |
| 9889 | return; |
| 9890 | } |
| 9891 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9892 | /* Capture start position */ |
| 9893 | if (ctxt->record_info) { |
| 9894 | node_info.begin_pos = ctxt->input->consumed + |
| 9895 | (CUR_PTR - ctxt->input->base); |
| 9896 | node_info.begin_line = ctxt->input->line; |
| 9897 | } |
| 9898 | |
| 9899 | if (ctxt->spaceNr == 0) |
| 9900 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 9901 | else if (*ctxt->space == -2) |
| 9902 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9903 | else |
| 9904 | spacePush(ctxt, *ctxt->space); |
| 9905 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9906 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9907 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9908 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9909 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9910 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9911 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9912 | else |
| 9913 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9914 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 9915 | if (ctxt->instate == XML_PARSER_EOF) |
| 9916 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9917 | if (name == NULL) { |
| 9918 | spacePop(ctxt); |
| 9919 | return; |
| 9920 | } |
| 9921 | namePush(ctxt, name); |
| 9922 | ret = ctxt->node; |
| 9923 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9924 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9925 | /* |
| 9926 | * [ VC: Root Element Type ] |
| 9927 | * The Name in the document type declaration must match the element |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 9928 | * type of the root element. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9929 | */ |
| 9930 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9931 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9932 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9933 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9934 | |
| 9935 | /* |
| 9936 | * Check for an Empty Element. |
| 9937 | */ |
| 9938 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9939 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9940 | if (ctxt->sax2) { |
| 9941 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9942 | (!ctxt->disableSAX)) |
| 9943 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9944 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9945 | } else { |
| 9946 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 9947 | (!ctxt->disableSAX)) |
| 9948 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9949 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9950 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9951 | namePop(ctxt); |
| 9952 | spacePop(ctxt); |
| 9953 | if (nsNr != ctxt->nsNr) |
| 9954 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9955 | if ( ret != NULL && ctxt->record_info ) { |
| 9956 | node_info.end_pos = ctxt->input->consumed + |
| 9957 | (CUR_PTR - ctxt->input->base); |
| 9958 | node_info.end_line = ctxt->input->line; |
| 9959 | node_info.node = ret; |
| 9960 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9961 | } |
| 9962 | return; |
| 9963 | } |
| 9964 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9965 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9966 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9967 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 9968 | "Couldn't find end of Start Tag %s line %d\n", |
| 9969 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9970 | |
| 9971 | /* |
| 9972 | * end of parsing of this node. |
| 9973 | */ |
| 9974 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9975 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9976 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9977 | if (nsNr != ctxt->nsNr) |
| 9978 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9979 | |
| 9980 | /* |
| 9981 | * Capture end position and add node |
| 9982 | */ |
| 9983 | if ( ret != NULL && ctxt->record_info ) { |
| 9984 | node_info.end_pos = ctxt->input->consumed + |
| 9985 | (CUR_PTR - ctxt->input->base); |
| 9986 | node_info.end_line = ctxt->input->line; |
| 9987 | node_info.node = ret; |
| 9988 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9989 | } |
| 9990 | return; |
| 9991 | } |
| 9992 | |
| 9993 | /* |
| 9994 | * Parse the content of the element: |
| 9995 | */ |
| 9996 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9997 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9998 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 9999 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10000 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10001 | |
| 10002 | /* |
| 10003 | * end of parsing of this node. |
| 10004 | */ |
| 10005 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10006 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10007 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10008 | if (nsNr != ctxt->nsNr) |
| 10009 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10010 | return; |
| 10011 | } |
| 10012 | |
| 10013 | /* |
| 10014 | * parse the end of tag: '</' should be here. |
| 10015 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10016 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10017 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10018 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10019 | } |
| 10020 | #ifdef LIBXML_SAX1_ENABLED |
| 10021 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10022 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10023 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10024 | |
| 10025 | /* |
| 10026 | * Capture end position and add node |
| 10027 | */ |
| 10028 | if ( ret != NULL && ctxt->record_info ) { |
| 10029 | node_info.end_pos = ctxt->input->consumed + |
| 10030 | (CUR_PTR - ctxt->input->base); |
| 10031 | node_info.end_line = ctxt->input->line; |
| 10032 | node_info.node = ret; |
| 10033 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 10034 | } |
| 10035 | } |
| 10036 | |
| 10037 | /** |
| 10038 | * xmlParseVersionNum: |
| 10039 | * @ctxt: an XML parser context |
| 10040 | * |
| 10041 | * parse the XML version value. |
| 10042 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10043 | * [26] VersionNum ::= '1.' [0-9]+ |
| 10044 | * |
| 10045 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10046 | * |
| 10047 | * Returns the string giving the XML version number, or NULL |
| 10048 | */ |
| 10049 | xmlChar * |
| 10050 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 10051 | xmlChar *buf = NULL; |
| 10052 | int len = 0; |
| 10053 | int size = 10; |
| 10054 | xmlChar cur; |
| 10055 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10056 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10057 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10058 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10059 | return(NULL); |
| 10060 | } |
| 10061 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10062 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10063 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10064 | return(NULL); |
| 10065 | } |
| 10066 | buf[len++] = cur; |
| 10067 | NEXT; |
| 10068 | cur=CUR; |
| 10069 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 10070 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10071 | return(NULL); |
| 10072 | } |
| 10073 | buf[len++] = cur; |
| 10074 | NEXT; |
| 10075 | cur=CUR; |
| 10076 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10077 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10078 | xmlChar *tmp; |
| 10079 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10080 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10081 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10082 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10083 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10084 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10085 | return(NULL); |
| 10086 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10087 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10088 | } |
| 10089 | buf[len++] = cur; |
| 10090 | NEXT; |
| 10091 | cur=CUR; |
| 10092 | } |
| 10093 | buf[len] = 0; |
| 10094 | return(buf); |
| 10095 | } |
| 10096 | |
| 10097 | /** |
| 10098 | * xmlParseVersionInfo: |
| 10099 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10100 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10101 | * parse the XML version. |
| 10102 | * |
| 10103 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 10104 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10105 | * [25] Eq ::= S? '=' S? |
| 10106 | * |
| 10107 | * Returns the version string, e.g. "1.0" |
| 10108 | */ |
| 10109 | |
| 10110 | xmlChar * |
| 10111 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 10112 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10113 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10114 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10115 | SKIP(7); |
| 10116 | SKIP_BLANKS; |
| 10117 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10118 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10119 | return(NULL); |
| 10120 | } |
| 10121 | NEXT; |
| 10122 | SKIP_BLANKS; |
| 10123 | if (RAW == '"') { |
| 10124 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10125 | version = xmlParseVersionNum(ctxt); |
| 10126 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10127 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10128 | } else |
| 10129 | NEXT; |
| 10130 | } else if (RAW == '\''){ |
| 10131 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10132 | version = xmlParseVersionNum(ctxt); |
| 10133 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10134 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10135 | } else |
| 10136 | NEXT; |
| 10137 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10138 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10139 | } |
| 10140 | } |
| 10141 | return(version); |
| 10142 | } |
| 10143 | |
| 10144 | /** |
| 10145 | * xmlParseEncName: |
| 10146 | * @ctxt: an XML parser context |
| 10147 | * |
| 10148 | * parse the XML encoding name |
| 10149 | * |
| 10150 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 10151 | * |
| 10152 | * Returns the encoding name value or NULL |
| 10153 | */ |
| 10154 | xmlChar * |
| 10155 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 10156 | xmlChar *buf = NULL; |
| 10157 | int len = 0; |
| 10158 | int size = 10; |
| 10159 | xmlChar cur; |
| 10160 | |
| 10161 | cur = CUR; |
| 10162 | if (((cur >= 'a') && (cur <= 'z')) || |
| 10163 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 10164 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10165 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10166 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10167 | return(NULL); |
| 10168 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10169 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10170 | buf[len++] = cur; |
| 10171 | NEXT; |
| 10172 | cur = CUR; |
| 10173 | while (((cur >= 'a') && (cur <= 'z')) || |
| 10174 | ((cur >= 'A') && (cur <= 'Z')) || |
| 10175 | ((cur >= '0') && (cur <= '9')) || |
| 10176 | (cur == '.') || (cur == '_') || |
| 10177 | (cur == '-')) { |
| 10178 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10179 | xmlChar *tmp; |
| 10180 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10181 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10182 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 10183 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10184 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10185 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10186 | return(NULL); |
| 10187 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 10188 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10189 | } |
| 10190 | buf[len++] = cur; |
| 10191 | NEXT; |
| 10192 | cur = CUR; |
| 10193 | if (cur == 0) { |
| 10194 | SHRINK; |
| 10195 | GROW; |
| 10196 | cur = CUR; |
| 10197 | } |
| 10198 | } |
| 10199 | buf[len] = 0; |
| 10200 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10201 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10202 | } |
| 10203 | return(buf); |
| 10204 | } |
| 10205 | |
| 10206 | /** |
| 10207 | * xmlParseEncodingDecl: |
| 10208 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10209 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10210 | * parse the XML encoding declaration |
| 10211 | * |
| 10212 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 10213 | * |
| 10214 | * this setups the conversion filters. |
| 10215 | * |
| 10216 | * Returns the encoding value or NULL |
| 10217 | */ |
| 10218 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10219 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10220 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 10221 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10222 | |
| 10223 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10224 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10225 | SKIP(8); |
| 10226 | SKIP_BLANKS; |
| 10227 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10228 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10229 | return(NULL); |
| 10230 | } |
| 10231 | NEXT; |
| 10232 | SKIP_BLANKS; |
| 10233 | if (RAW == '"') { |
| 10234 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10235 | encoding = xmlParseEncName(ctxt); |
| 10236 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10237 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10238 | } else |
| 10239 | NEXT; |
| 10240 | } else if (RAW == '\''){ |
| 10241 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10242 | encoding = xmlParseEncName(ctxt); |
| 10243 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10244 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10245 | } else |
| 10246 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 10247 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10248 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10249 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 10250 | |
| 10251 | /* |
| 10252 | * Non standard parsing, allowing the user to ignore encoding |
| 10253 | */ |
| 10254 | if (ctxt->options & XML_PARSE_IGNORE_ENC) |
| 10255 | return(encoding); |
| 10256 | |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10257 | /* |
| 10258 | * UTF-16 encoding stwich has already taken place at this stage, |
| 10259 | * more over the little-endian/big-endian selection is already done |
| 10260 | */ |
| 10261 | if ((encoding != NULL) && |
| 10262 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 10263 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10264 | /* |
| 10265 | * If no encoding was passed to the parser, that we are |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10266 | * using UTF-16 and no decoder is present i.e. the |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 10267 | * document is apparently UTF-8 compatible, then raise an |
| 10268 | * encoding mismatch fatal error |
| 10269 | */ |
| 10270 | if ((ctxt->encoding == NULL) && |
| 10271 | (ctxt->input->buf != NULL) && |
| 10272 | (ctxt->input->buf->encoder == NULL)) { |
| 10273 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 10274 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 10275 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10276 | if (ctxt->encoding != NULL) |
| 10277 | xmlFree((xmlChar *) ctxt->encoding); |
| 10278 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10279 | } |
| 10280 | /* |
| 10281 | * UTF-8 encoding is handled natively |
| 10282 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10283 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 10284 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 10285 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10286 | if (ctxt->encoding != NULL) |
| 10287 | xmlFree((xmlChar *) ctxt->encoding); |
| 10288 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 10289 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 10290 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10291 | xmlCharEncodingHandlerPtr handler; |
| 10292 | |
| 10293 | if (ctxt->input->encoding != NULL) |
| 10294 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 10295 | ctxt->input->encoding = encoding; |
| 10296 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10297 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 10298 | if (handler != NULL) { |
| 10299 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10300 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10301 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10302 | "Unsupported encoding %s\n", encoding); |
| 10303 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10304 | } |
| 10305 | } |
| 10306 | } |
| 10307 | return(encoding); |
| 10308 | } |
| 10309 | |
| 10310 | /** |
| 10311 | * xmlParseSDDecl: |
| 10312 | * @ctxt: an XML parser context |
| 10313 | * |
| 10314 | * parse the XML standalone declaration |
| 10315 | * |
| 10316 | * [32] SDDecl ::= S 'standalone' Eq |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10317 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10318 | * |
| 10319 | * [ VC: Standalone Document Declaration ] |
| 10320 | * TODO The standalone document declaration must have the value "no" |
| 10321 | * if any external markup declarations contain declarations of: |
| 10322 | * - attributes with default values, if elements to which these |
| 10323 | * attributes apply appear in the document without specifications |
| 10324 | * of values for these attributes, or |
| 10325 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 10326 | * to those entities appear in the document, or |
| 10327 | * - attributes with values subject to normalization, where the |
| 10328 | * attribute appears in the document with a value which will change |
| 10329 | * as a result of normalization, or |
| 10330 | * - element types with element content, if white space occurs directly |
| 10331 | * within any instance of those types. |
| 10332 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10333 | * Returns: |
| 10334 | * 1 if standalone="yes" |
| 10335 | * 0 if standalone="no" |
| 10336 | * -2 if standalone attribute is missing or invalid |
| 10337 | * (A standalone value of -2 means that the XML declaration was found, |
| 10338 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10339 | */ |
| 10340 | |
| 10341 | int |
| 10342 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10343 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10344 | |
| 10345 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10346 | 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] | 10347 | SKIP(10); |
| 10348 | SKIP_BLANKS; |
| 10349 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10350 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10351 | return(standalone); |
| 10352 | } |
| 10353 | NEXT; |
| 10354 | SKIP_BLANKS; |
| 10355 | if (RAW == '\''){ |
| 10356 | NEXT; |
| 10357 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10358 | standalone = 0; |
| 10359 | SKIP(2); |
| 10360 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10361 | (NXT(2) == 's')) { |
| 10362 | standalone = 1; |
| 10363 | SKIP(3); |
| 10364 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10365 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10366 | } |
| 10367 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10368 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10369 | } else |
| 10370 | NEXT; |
| 10371 | } else if (RAW == '"'){ |
| 10372 | NEXT; |
| 10373 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10374 | standalone = 0; |
| 10375 | SKIP(2); |
| 10376 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10377 | (NXT(2) == 's')) { |
| 10378 | standalone = 1; |
| 10379 | SKIP(3); |
| 10380 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10381 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10382 | } |
| 10383 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10384 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10385 | } else |
| 10386 | NEXT; |
| 10387 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10388 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10389 | } |
| 10390 | } |
| 10391 | return(standalone); |
| 10392 | } |
| 10393 | |
| 10394 | /** |
| 10395 | * xmlParseXMLDecl: |
| 10396 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10397 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10398 | * parse an XML declaration header |
| 10399 | * |
| 10400 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 10401 | */ |
| 10402 | |
| 10403 | void |
| 10404 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 10405 | xmlChar *version; |
| 10406 | |
| 10407 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 10408 | * This value for standalone indicates that the document has an |
| 10409 | * XML declaration but it does not have a standalone attribute. |
| 10410 | * It will be overwritten later if a standalone attribute is found. |
| 10411 | */ |
| 10412 | ctxt->input->standalone = -2; |
| 10413 | |
| 10414 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10415 | * We know that '<?xml' is here. |
| 10416 | */ |
| 10417 | SKIP(5); |
| 10418 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10419 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10420 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 10421 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10422 | } |
| 10423 | SKIP_BLANKS; |
| 10424 | |
| 10425 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10426 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10427 | */ |
| 10428 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10429 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10430 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10431 | } else { |
| 10432 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 10433 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10434 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10435 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10436 | if (ctxt->options & XML_PARSE_OLD10) { |
| 10437 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10438 | "Unsupported version '%s'\n", |
| 10439 | version); |
| 10440 | } else { |
| 10441 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 10442 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 10443 | "Unsupported version '%s'\n", |
| 10444 | version, NULL); |
| 10445 | } else { |
| 10446 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10447 | "Unsupported version '%s'\n", |
| 10448 | version); |
| 10449 | } |
| 10450 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10451 | } |
| 10452 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 10453 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10454 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 10455 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | |
| 10457 | /* |
| 10458 | * We may have the encoding declaration |
| 10459 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10460 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10461 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10462 | SKIP(2); |
| 10463 | return; |
| 10464 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10465 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10466 | } |
| 10467 | xmlParseEncodingDecl(ctxt); |
| 10468 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10469 | /* |
| 10470 | * The XML REC instructs us to stop parsing right here |
| 10471 | */ |
| 10472 | return; |
| 10473 | } |
| 10474 | |
| 10475 | /* |
| 10476 | * We may have the standalone status. |
| 10477 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10478 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10479 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10480 | SKIP(2); |
| 10481 | return; |
| 10482 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10483 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10484 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 10485 | |
| 10486 | /* |
| 10487 | * We can grow the input buffer freely at that point |
| 10488 | */ |
| 10489 | GROW; |
| 10490 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10491 | SKIP_BLANKS; |
| 10492 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 10493 | |
| 10494 | SKIP_BLANKS; |
| 10495 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10496 | SKIP(2); |
| 10497 | } else if (RAW == '>') { |
| 10498 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10499 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10500 | NEXT; |
| 10501 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10502 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10503 | MOVETO_ENDTAG(CUR_PTR); |
| 10504 | NEXT; |
| 10505 | } |
| 10506 | } |
| 10507 | |
| 10508 | /** |
| 10509 | * xmlParseMisc: |
| 10510 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10511 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10512 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10513 | * |
| 10514 | * [27] Misc ::= Comment | PI | S |
| 10515 | */ |
| 10516 | |
| 10517 | void |
| 10518 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 10519 | while ((ctxt->instate != XML_PARSER_EOF) && |
| 10520 | (((RAW == '<') && (NXT(1) == '?')) || |
| 10521 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
| 10522 | IS_BLANK_CH(CUR))) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10523 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10524 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10525 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10526 | NEXT; |
| 10527 | } else |
| 10528 | xmlParseComment(ctxt); |
| 10529 | } |
| 10530 | } |
| 10531 | |
| 10532 | /** |
| 10533 | * xmlParseDocument: |
| 10534 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10535 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10536 | * parse an XML document (and build a tree if using the standard SAX |
| 10537 | * interface). |
| 10538 | * |
| 10539 | * [1] document ::= prolog element Misc* |
| 10540 | * |
| 10541 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 10542 | * |
| 10543 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10544 | * as a result of the parsing. |
| 10545 | */ |
| 10546 | |
| 10547 | int |
| 10548 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 10549 | xmlChar start[4]; |
| 10550 | xmlCharEncoding enc; |
| 10551 | |
| 10552 | xmlInitParser(); |
| 10553 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10554 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10555 | return(-1); |
| 10556 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10557 | GROW; |
| 10558 | |
| 10559 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10560 | * SAX: detecting the level. |
| 10561 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10562 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10563 | |
| 10564 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10565 | * SAX: beginning of the document processing. |
| 10566 | */ |
| 10567 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10568 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10569 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 10570 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10571 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10572 | /* |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10573 | * Get the 4 first bytes and decode the charset |
| 10574 | * if enc != XML_CHAR_ENCODING_NONE |
| 10575 | * plug some encoding conversion routines. |
| 10576 | */ |
| 10577 | start[0] = RAW; |
| 10578 | start[1] = NXT(1); |
| 10579 | start[2] = NXT(2); |
| 10580 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10581 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10582 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10583 | xmlSwitchEncoding(ctxt, enc); |
| 10584 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10585 | } |
| 10586 | |
| 10587 | |
| 10588 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10589 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10590 | } |
| 10591 | |
| 10592 | /* |
| 10593 | * Check for the XMLDecl in the Prolog. |
Daniel Veillard | 7e385bd | 2009-08-26 11:38:49 +0200 | [diff] [blame] | 10594 | * do not GROW here to avoid the detected encoder to decode more |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10595 | * than just the first line, unless the amount of data is really |
| 10596 | * too small to hold "<?xml version="1.0" encoding="foo" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10597 | */ |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10598 | if ((ctxt->input->end - ctxt->input->cur) < 35) { |
| 10599 | GROW; |
| 10600 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10601 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10602 | |
| 10603 | /* |
| 10604 | * Note that we will switch encoding on the fly. |
| 10605 | */ |
| 10606 | xmlParseXMLDecl(ctxt); |
| 10607 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10608 | /* |
| 10609 | * The XML REC instructs us to stop parsing right here |
| 10610 | */ |
| 10611 | return(-1); |
| 10612 | } |
| 10613 | ctxt->standalone = ctxt->input->standalone; |
| 10614 | SKIP_BLANKS; |
| 10615 | } else { |
| 10616 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10617 | } |
| 10618 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10619 | ctxt->sax->startDocument(ctxt->userData); |
| 10620 | |
| 10621 | /* |
| 10622 | * The Misc part of the Prolog |
| 10623 | */ |
| 10624 | GROW; |
| 10625 | xmlParseMisc(ctxt); |
| 10626 | |
| 10627 | /* |
| 10628 | * Then possibly doc type declaration(s) and more Misc |
| 10629 | * (doctypedecl Misc*)? |
| 10630 | */ |
| 10631 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10632 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10633 | |
| 10634 | ctxt->inSubset = 1; |
| 10635 | xmlParseDocTypeDecl(ctxt); |
| 10636 | if (RAW == '[') { |
| 10637 | ctxt->instate = XML_PARSER_DTD; |
| 10638 | xmlParseInternalSubset(ctxt); |
| 10639 | } |
| 10640 | |
| 10641 | /* |
| 10642 | * Create and update the external subset. |
| 10643 | */ |
| 10644 | ctxt->inSubset = 2; |
| 10645 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10646 | (!ctxt->disableSAX)) |
| 10647 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10648 | ctxt->extSubSystem, ctxt->extSubURI); |
| 10649 | ctxt->inSubset = 0; |
| 10650 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10651 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10652 | |
| 10653 | ctxt->instate = XML_PARSER_PROLOG; |
| 10654 | xmlParseMisc(ctxt); |
| 10655 | } |
| 10656 | |
| 10657 | /* |
| 10658 | * Time to start parsing the tree itself |
| 10659 | */ |
| 10660 | GROW; |
| 10661 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10662 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10663 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10664 | } else { |
| 10665 | ctxt->instate = XML_PARSER_CONTENT; |
| 10666 | xmlParseElement(ctxt); |
| 10667 | ctxt->instate = XML_PARSER_EPILOG; |
| 10668 | |
| 10669 | |
| 10670 | /* |
| 10671 | * The Misc part at the end |
| 10672 | */ |
| 10673 | xmlParseMisc(ctxt); |
| 10674 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10675 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10676 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10677 | } |
| 10678 | ctxt->instate = XML_PARSER_EOF; |
| 10679 | } |
| 10680 | |
| 10681 | /* |
| 10682 | * SAX: end of the document processing. |
| 10683 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10684 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10685 | ctxt->sax->endDocument(ctxt->userData); |
| 10686 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10687 | /* |
| 10688 | * Remove locally kept entity definitions if the tree was not built |
| 10689 | */ |
| 10690 | if ((ctxt->myDoc != NULL) && |
| 10691 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10692 | xmlFreeDoc(ctxt->myDoc); |
| 10693 | ctxt->myDoc = NULL; |
| 10694 | } |
| 10695 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10696 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10697 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10698 | if (ctxt->valid) |
| 10699 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10700 | if (ctxt->nsWellFormed) |
| 10701 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10702 | if (ctxt->options & XML_PARSE_OLD10) |
| 10703 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10704 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10705 | if (! ctxt->wellFormed) { |
| 10706 | ctxt->valid = 0; |
| 10707 | return(-1); |
| 10708 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10709 | return(0); |
| 10710 | } |
| 10711 | |
| 10712 | /** |
| 10713 | * xmlParseExtParsedEnt: |
| 10714 | * @ctxt: an XML parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10715 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10716 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10717 | * An external general parsed entity is well-formed if it matches the |
| 10718 | * production labeled extParsedEnt. |
| 10719 | * |
| 10720 | * [78] extParsedEnt ::= TextDecl? content |
| 10721 | * |
| 10722 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10723 | * as a result of the parsing. |
| 10724 | */ |
| 10725 | |
| 10726 | int |
| 10727 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10728 | xmlChar start[4]; |
| 10729 | xmlCharEncoding enc; |
| 10730 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10731 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10732 | return(-1); |
| 10733 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10734 | xmlDefaultSAXHandlerInit(); |
| 10735 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10736 | xmlDetectSAX2(ctxt); |
| 10737 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10738 | GROW; |
| 10739 | |
| 10740 | /* |
| 10741 | * SAX: beginning of the document processing. |
| 10742 | */ |
| 10743 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10744 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10745 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10746 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10747 | * Get the 4 first bytes and decode the charset |
| 10748 | * if enc != XML_CHAR_ENCODING_NONE |
| 10749 | * plug some encoding conversion routines. |
| 10750 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10751 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10752 | start[0] = RAW; |
| 10753 | start[1] = NXT(1); |
| 10754 | start[2] = NXT(2); |
| 10755 | start[3] = NXT(3); |
| 10756 | enc = xmlDetectCharEncoding(start, 4); |
| 10757 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10758 | xmlSwitchEncoding(ctxt, enc); |
| 10759 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10760 | } |
| 10761 | |
| 10762 | |
| 10763 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10764 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10765 | } |
| 10766 | |
| 10767 | /* |
| 10768 | * Check for the XMLDecl in the Prolog. |
| 10769 | */ |
| 10770 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10771 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10772 | |
| 10773 | /* |
| 10774 | * Note that we will switch encoding on the fly. |
| 10775 | */ |
| 10776 | xmlParseXMLDecl(ctxt); |
| 10777 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10778 | /* |
| 10779 | * The XML REC instructs us to stop parsing right here |
| 10780 | */ |
| 10781 | return(-1); |
| 10782 | } |
| 10783 | SKIP_BLANKS; |
| 10784 | } else { |
| 10785 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10786 | } |
| 10787 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10788 | ctxt->sax->startDocument(ctxt->userData); |
| 10789 | |
| 10790 | /* |
| 10791 | * Doing validity checking on chunk doesn't make sense |
| 10792 | */ |
| 10793 | ctxt->instate = XML_PARSER_CONTENT; |
| 10794 | ctxt->validate = 0; |
| 10795 | ctxt->loadsubset = 0; |
| 10796 | ctxt->depth = 0; |
| 10797 | |
| 10798 | xmlParseContent(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10799 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10800 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10801 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10802 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10803 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10804 | } |
| 10805 | |
| 10806 | /* |
| 10807 | * SAX: end of the document processing. |
| 10808 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10809 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10810 | ctxt->sax->endDocument(ctxt->userData); |
| 10811 | |
| 10812 | if (! ctxt->wellFormed) return(-1); |
| 10813 | return(0); |
| 10814 | } |
| 10815 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10816 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10817 | /************************************************************************ |
| 10818 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10819 | * Progressive parsing interfaces * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10820 | * * |
| 10821 | ************************************************************************/ |
| 10822 | |
| 10823 | /** |
| 10824 | * xmlParseLookupSequence: |
| 10825 | * @ctxt: an XML parser context |
| 10826 | * @first: the first char to lookup |
| 10827 | * @next: the next char to lookup or zero |
| 10828 | * @third: the next char to lookup or zero |
| 10829 | * |
| 10830 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10831 | * (first) is available in the input stream. |
| 10832 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10833 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10834 | * parser, do not use liberally. |
| 10835 | * |
| 10836 | * Returns the index to the current parsing point if the full sequence |
| 10837 | * is available, -1 otherwise. |
| 10838 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10839 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10840 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10841 | xmlChar next, xmlChar third) { |
| 10842 | int base, len; |
| 10843 | xmlParserInputPtr in; |
| 10844 | const xmlChar *buf; |
| 10845 | |
| 10846 | in = ctxt->input; |
| 10847 | if (in == NULL) return(-1); |
| 10848 | base = in->cur - in->base; |
| 10849 | if (base < 0) return(-1); |
| 10850 | if (ctxt->checkIndex > base) |
| 10851 | base = ctxt->checkIndex; |
| 10852 | if (in->buf == NULL) { |
| 10853 | buf = in->base; |
| 10854 | len = in->length; |
| 10855 | } else { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 10856 | buf = xmlBufContent(in->buf->buffer); |
| 10857 | len = xmlBufUse(in->buf->buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10858 | } |
| 10859 | /* take into account the sequence length */ |
| 10860 | if (third) len -= 2; |
| 10861 | else if (next) len --; |
| 10862 | for (;base < len;base++) { |
| 10863 | if (buf[base] == first) { |
| 10864 | if (third != 0) { |
| 10865 | if ((buf[base + 1] != next) || |
| 10866 | (buf[base + 2] != third)) continue; |
| 10867 | } else if (next != 0) { |
| 10868 | if (buf[base + 1] != next) continue; |
| 10869 | } |
| 10870 | ctxt->checkIndex = 0; |
| 10871 | #ifdef DEBUG_PUSH |
| 10872 | if (next == 0) |
| 10873 | xmlGenericError(xmlGenericErrorContext, |
| 10874 | "PP: lookup '%c' found at %d\n", |
| 10875 | first, base); |
| 10876 | else if (third == 0) |
| 10877 | xmlGenericError(xmlGenericErrorContext, |
| 10878 | "PP: lookup '%c%c' found at %d\n", |
| 10879 | first, next, base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10880 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10881 | xmlGenericError(xmlGenericErrorContext, |
| 10882 | "PP: lookup '%c%c%c' found at %d\n", |
| 10883 | first, next, third, base); |
| 10884 | #endif |
| 10885 | return(base - (in->cur - in->base)); |
| 10886 | } |
| 10887 | } |
| 10888 | ctxt->checkIndex = base; |
| 10889 | #ifdef DEBUG_PUSH |
| 10890 | if (next == 0) |
| 10891 | xmlGenericError(xmlGenericErrorContext, |
| 10892 | "PP: lookup '%c' failed\n", first); |
| 10893 | else if (third == 0) |
| 10894 | xmlGenericError(xmlGenericErrorContext, |
| 10895 | "PP: lookup '%c%c' failed\n", first, next); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10896 | else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10897 | xmlGenericError(xmlGenericErrorContext, |
| 10898 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 10899 | #endif |
| 10900 | return(-1); |
| 10901 | } |
| 10902 | |
| 10903 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10904 | * xmlParseGetLasts: |
| 10905 | * @ctxt: an XML parser context |
| 10906 | * @lastlt: pointer to store the last '<' from the input |
| 10907 | * @lastgt: pointer to store the last '>' from the input |
| 10908 | * |
| 10909 | * Lookup the last < and > in the current chunk |
| 10910 | */ |
| 10911 | static void |
| 10912 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 10913 | const xmlChar **lastgt) { |
| 10914 | const xmlChar *tmp; |
| 10915 | |
| 10916 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 10917 | xmlGenericError(xmlGenericErrorContext, |
| 10918 | "Internal error: xmlParseGetLasts\n"); |
| 10919 | return; |
| 10920 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 10921 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10922 | tmp = ctxt->input->end; |
| 10923 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10924 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10925 | if (tmp < ctxt->input->base) { |
| 10926 | *lastlt = NULL; |
| 10927 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10928 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10929 | *lastlt = tmp; |
| 10930 | tmp++; |
| 10931 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 10932 | if (*tmp == '\'') { |
| 10933 | tmp++; |
| 10934 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 10935 | if (tmp < ctxt->input->end) tmp++; |
| 10936 | } else if (*tmp == '"') { |
| 10937 | tmp++; |
| 10938 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 10939 | if (tmp < ctxt->input->end) tmp++; |
| 10940 | } else |
| 10941 | tmp++; |
| 10942 | } |
| 10943 | if (tmp < ctxt->input->end) |
| 10944 | *lastgt = tmp; |
| 10945 | else { |
| 10946 | tmp = *lastlt; |
| 10947 | tmp--; |
| 10948 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 10949 | if (tmp >= ctxt->input->base) |
| 10950 | *lastgt = tmp; |
| 10951 | else |
| 10952 | *lastgt = NULL; |
| 10953 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10954 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10955 | } else { |
| 10956 | *lastlt = NULL; |
| 10957 | *lastgt = NULL; |
| 10958 | } |
| 10959 | } |
| 10960 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10961 | * xmlCheckCdataPush: |
| 10962 | * @cur: pointer to the bock of characters |
| 10963 | * @len: length of the block in bytes |
| 10964 | * |
| 10965 | * Check that the block of characters is okay as SCdata content [20] |
| 10966 | * |
| 10967 | * Returns the number of bytes to pass if okay, a negative index where an |
| 10968 | * UTF-8 error occured otherwise |
| 10969 | */ |
| 10970 | static int |
| 10971 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 10972 | int ix; |
| 10973 | unsigned char c; |
| 10974 | int codepoint; |
| 10975 | |
| 10976 | if ((utf == NULL) || (len <= 0)) |
| 10977 | return(0); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 10978 | |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10979 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 10980 | c = utf[ix]; |
| 10981 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 10982 | if (c >= 0x20) |
| 10983 | ix++; |
| 10984 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 10985 | ix++; |
| 10986 | else |
| 10987 | return(-ix); |
| 10988 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 10989 | if (ix + 2 > len) return(ix); |
| 10990 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 10991 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10992 | codepoint = (utf[ix] & 0x1f) << 6; |
| 10993 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10994 | if (!xmlIsCharQ(codepoint)) |
| 10995 | return(-ix); |
| 10996 | ix += 2; |
| 10997 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 10998 | if (ix + 3 > len) return(ix); |
| 10999 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11000 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 11001 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11002 | codepoint = (utf[ix] & 0xf) << 12; |
| 11003 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 11004 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11005 | if (!xmlIsCharQ(codepoint)) |
| 11006 | return(-ix); |
| 11007 | ix += 3; |
| 11008 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 11009 | if (ix + 4 > len) return(ix); |
| 11010 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 11011 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 11012 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 11013 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 11014 | codepoint = (utf[ix] & 0x7) << 18; |
| 11015 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 11016 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 11017 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11018 | if (!xmlIsCharQ(codepoint)) |
| 11019 | return(-ix); |
| 11020 | ix += 4; |
| 11021 | } else /* unknown encoding */ |
| 11022 | return(-ix); |
| 11023 | } |
| 11024 | return(ix); |
| 11025 | } |
| 11026 | |
| 11027 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11028 | * xmlParseTryOrFinish: |
| 11029 | * @ctxt: an XML parser context |
| 11030 | * @terminate: last chunk indicator |
| 11031 | * |
| 11032 | * Try to progress on parsing |
| 11033 | * |
| 11034 | * Returns zero if no parsing was possible |
| 11035 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 11036 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11037 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 11038 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11039 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11040 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11041 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11042 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11043 | if (ctxt->input == NULL) |
| 11044 | return(0); |
| 11045 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11046 | #ifdef DEBUG_PUSH |
| 11047 | switch (ctxt->instate) { |
| 11048 | case XML_PARSER_EOF: |
| 11049 | xmlGenericError(xmlGenericErrorContext, |
| 11050 | "PP: try EOF\n"); break; |
| 11051 | case XML_PARSER_START: |
| 11052 | xmlGenericError(xmlGenericErrorContext, |
| 11053 | "PP: try START\n"); break; |
| 11054 | case XML_PARSER_MISC: |
| 11055 | xmlGenericError(xmlGenericErrorContext, |
| 11056 | "PP: try MISC\n");break; |
| 11057 | case XML_PARSER_COMMENT: |
| 11058 | xmlGenericError(xmlGenericErrorContext, |
| 11059 | "PP: try COMMENT\n");break; |
| 11060 | case XML_PARSER_PROLOG: |
| 11061 | xmlGenericError(xmlGenericErrorContext, |
| 11062 | "PP: try PROLOG\n");break; |
| 11063 | case XML_PARSER_START_TAG: |
| 11064 | xmlGenericError(xmlGenericErrorContext, |
| 11065 | "PP: try START_TAG\n");break; |
| 11066 | case XML_PARSER_CONTENT: |
| 11067 | xmlGenericError(xmlGenericErrorContext, |
| 11068 | "PP: try CONTENT\n");break; |
| 11069 | case XML_PARSER_CDATA_SECTION: |
| 11070 | xmlGenericError(xmlGenericErrorContext, |
| 11071 | "PP: try CDATA_SECTION\n");break; |
| 11072 | case XML_PARSER_END_TAG: |
| 11073 | xmlGenericError(xmlGenericErrorContext, |
| 11074 | "PP: try END_TAG\n");break; |
| 11075 | case XML_PARSER_ENTITY_DECL: |
| 11076 | xmlGenericError(xmlGenericErrorContext, |
| 11077 | "PP: try ENTITY_DECL\n");break; |
| 11078 | case XML_PARSER_ENTITY_VALUE: |
| 11079 | xmlGenericError(xmlGenericErrorContext, |
| 11080 | "PP: try ENTITY_VALUE\n");break; |
| 11081 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11082 | xmlGenericError(xmlGenericErrorContext, |
| 11083 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 11084 | case XML_PARSER_DTD: |
| 11085 | xmlGenericError(xmlGenericErrorContext, |
| 11086 | "PP: try DTD\n");break; |
| 11087 | case XML_PARSER_EPILOG: |
| 11088 | xmlGenericError(xmlGenericErrorContext, |
| 11089 | "PP: try EPILOG\n");break; |
| 11090 | case XML_PARSER_PI: |
| 11091 | xmlGenericError(xmlGenericErrorContext, |
| 11092 | "PP: try PI\n");break; |
| 11093 | case XML_PARSER_IGNORE: |
| 11094 | xmlGenericError(xmlGenericErrorContext, |
| 11095 | "PP: try IGNORE\n");break; |
| 11096 | } |
| 11097 | #endif |
| 11098 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11099 | if ((ctxt->input != NULL) && |
| 11100 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11101 | xmlSHRINK(ctxt); |
| 11102 | ctxt->checkIndex = 0; |
| 11103 | } |
| 11104 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 11105 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11106 | while (1) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11107 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11108 | return(0); |
| 11109 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11110 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11111 | /* |
| 11112 | * Pop-up of finished entities. |
| 11113 | */ |
| 11114 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11115 | xmlPopInput(ctxt); |
| 11116 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 11117 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11118 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11119 | avail = ctxt->input->length - |
| 11120 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11121 | else { |
| 11122 | /* |
| 11123 | * If we are operating on converted input, try to flush |
| 11124 | * remainng chars to avoid them stalling in the non-converted |
| 11125 | * buffer. |
| 11126 | */ |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11127 | if (xmlBufIsEmpty(ctxt->input->buf->buffer) == 0) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11128 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, |
| 11129 | ctxt->input); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11130 | size_t current = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11131 | |
| 11132 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 11133 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, |
| 11134 | base, current); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11135 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11136 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 11137 | (ctxt->input->cur - ctxt->input->base); |
| 11138 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11139 | if (avail < 1) |
| 11140 | goto done; |
| 11141 | switch (ctxt->instate) { |
| 11142 | case XML_PARSER_EOF: |
| 11143 | /* |
| 11144 | * Document parsing is done ! |
| 11145 | */ |
| 11146 | goto done; |
| 11147 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11148 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 11149 | xmlChar start[4]; |
| 11150 | xmlCharEncoding enc; |
| 11151 | |
| 11152 | /* |
| 11153 | * Very first chars read from the document flow. |
| 11154 | */ |
| 11155 | if (avail < 4) |
| 11156 | goto done; |
| 11157 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11158 | /* |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11159 | * Get the 4 first bytes and decode the charset |
| 11160 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11161 | * plug some encoding conversion routines, |
| 11162 | * else xmlSwitchEncoding will set to (default) |
| 11163 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11164 | */ |
| 11165 | start[0] = RAW; |
| 11166 | start[1] = NXT(1); |
| 11167 | start[2] = NXT(2); |
| 11168 | start[3] = NXT(3); |
| 11169 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11170 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11171 | break; |
| 11172 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11173 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 11174 | if (avail < 2) |
| 11175 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11176 | cur = ctxt->input->cur[0]; |
| 11177 | next = ctxt->input->cur[1]; |
| 11178 | if (cur == 0) { |
| 11179 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11180 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11181 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11182 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11183 | ctxt->instate = XML_PARSER_EOF; |
| 11184 | #ifdef DEBUG_PUSH |
| 11185 | xmlGenericError(xmlGenericErrorContext, |
| 11186 | "PP: entering EOF\n"); |
| 11187 | #endif |
| 11188 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11189 | ctxt->sax->endDocument(ctxt->userData); |
| 11190 | goto done; |
| 11191 | } |
| 11192 | if ((cur == '<') && (next == '?')) { |
| 11193 | /* PI or XML decl */ |
| 11194 | if (avail < 5) return(ret); |
| 11195 | if ((!terminate) && |
| 11196 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11197 | return(ret); |
| 11198 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11199 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11200 | &xmlDefaultSAXLocator); |
| 11201 | if ((ctxt->input->cur[2] == 'x') && |
| 11202 | (ctxt->input->cur[3] == 'm') && |
| 11203 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 11204 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11205 | ret += 5; |
| 11206 | #ifdef DEBUG_PUSH |
| 11207 | xmlGenericError(xmlGenericErrorContext, |
| 11208 | "PP: Parsing XML Decl\n"); |
| 11209 | #endif |
| 11210 | xmlParseXMLDecl(ctxt); |
| 11211 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 11212 | /* |
| 11213 | * The XML REC instructs us to stop parsing right |
| 11214 | * here |
| 11215 | */ |
| 11216 | ctxt->instate = XML_PARSER_EOF; |
| 11217 | return(0); |
| 11218 | } |
| 11219 | ctxt->standalone = ctxt->input->standalone; |
| 11220 | if ((ctxt->encoding == NULL) && |
| 11221 | (ctxt->input->encoding != NULL)) |
| 11222 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 11223 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11224 | (!ctxt->disableSAX)) |
| 11225 | ctxt->sax->startDocument(ctxt->userData); |
| 11226 | ctxt->instate = XML_PARSER_MISC; |
| 11227 | #ifdef DEBUG_PUSH |
| 11228 | xmlGenericError(xmlGenericErrorContext, |
| 11229 | "PP: entering MISC\n"); |
| 11230 | #endif |
| 11231 | } else { |
| 11232 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 11233 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11234 | (!ctxt->disableSAX)) |
| 11235 | ctxt->sax->startDocument(ctxt->userData); |
| 11236 | ctxt->instate = XML_PARSER_MISC; |
| 11237 | #ifdef DEBUG_PUSH |
| 11238 | xmlGenericError(xmlGenericErrorContext, |
| 11239 | "PP: entering MISC\n"); |
| 11240 | #endif |
| 11241 | } |
| 11242 | } else { |
| 11243 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 11244 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 11245 | &xmlDefaultSAXLocator); |
| 11246 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11247 | if (ctxt->version == NULL) { |
| 11248 | xmlErrMemory(ctxt, NULL); |
| 11249 | break; |
| 11250 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11251 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 11252 | (!ctxt->disableSAX)) |
| 11253 | ctxt->sax->startDocument(ctxt->userData); |
| 11254 | ctxt->instate = XML_PARSER_MISC; |
| 11255 | #ifdef DEBUG_PUSH |
| 11256 | xmlGenericError(xmlGenericErrorContext, |
| 11257 | "PP: entering MISC\n"); |
| 11258 | #endif |
| 11259 | } |
| 11260 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11261 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11262 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 11263 | const xmlChar *prefix = NULL; |
| 11264 | const xmlChar *URI = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11265 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11266 | |
| 11267 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11268 | goto done; |
| 11269 | cur = ctxt->input->cur[0]; |
| 11270 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11271 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11272 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11273 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11274 | ctxt->sax->endDocument(ctxt->userData); |
| 11275 | goto done; |
| 11276 | } |
| 11277 | if (!terminate) { |
| 11278 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 11279 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11280 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11281 | goto done; |
| 11282 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11283 | goto done; |
| 11284 | } |
| 11285 | } |
| 11286 | if (ctxt->spaceNr == 0) |
| 11287 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 11288 | else if (*ctxt->space == -2) |
| 11289 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11290 | else |
| 11291 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11292 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11293 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11294 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11295 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11296 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11297 | else |
| 11298 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11299 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11300 | if (ctxt->instate == XML_PARSER_EOF) |
| 11301 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11302 | if (name == NULL) { |
| 11303 | spacePop(ctxt); |
| 11304 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11305 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11306 | ctxt->sax->endDocument(ctxt->userData); |
| 11307 | goto done; |
| 11308 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11309 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11310 | /* |
| 11311 | * [ VC: Root Element Type ] |
| 11312 | * The Name in the document type declaration must match |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11313 | * the element type of the root element. |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11314 | */ |
| 11315 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 11316 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 11317 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11318 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11319 | |
| 11320 | /* |
| 11321 | * Check for an Empty Element. |
| 11322 | */ |
| 11323 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 11324 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11325 | |
| 11326 | if (ctxt->sax2) { |
| 11327 | if ((ctxt->sax != NULL) && |
| 11328 | (ctxt->sax->endElementNs != NULL) && |
| 11329 | (!ctxt->disableSAX)) |
| 11330 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 11331 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 11332 | if (ctxt->nsNr - nsNr > 0) |
| 11333 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11334 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11335 | } else { |
| 11336 | if ((ctxt->sax != NULL) && |
| 11337 | (ctxt->sax->endElement != NULL) && |
| 11338 | (!ctxt->disableSAX)) |
| 11339 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11340 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11341 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11342 | spacePop(ctxt); |
| 11343 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11344 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11345 | } else { |
| 11346 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11347 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11348 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11349 | break; |
| 11350 | } |
| 11351 | if (RAW == '>') { |
| 11352 | NEXT; |
| 11353 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 11354 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11355 | "Couldn't find end of Start Tag %s\n", |
| 11356 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11357 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11358 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11359 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11360 | if (ctxt->sax2) |
| 11361 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11362 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11363 | else |
| 11364 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11365 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11366 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11367 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11368 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11369 | break; |
| 11370 | } |
| 11371 | case XML_PARSER_CONTENT: { |
| 11372 | const xmlChar *test; |
| 11373 | unsigned int cons; |
| 11374 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11375 | goto done; |
| 11376 | cur = ctxt->input->cur[0]; |
| 11377 | next = ctxt->input->cur[1]; |
| 11378 | |
| 11379 | test = CUR_PTR; |
| 11380 | cons = ctxt->input->consumed; |
| 11381 | if ((cur == '<') && (next == '/')) { |
| 11382 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11383 | break; |
| 11384 | } else if ((cur == '<') && (next == '?')) { |
| 11385 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11386 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11387 | ctxt->progressive = XML_PARSER_PI; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11388 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11389 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11390 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11391 | ctxt->instate = XML_PARSER_CONTENT; |
| 11392 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11393 | } else if ((cur == '<') && (next != '!')) { |
| 11394 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11395 | break; |
| 11396 | } else if ((cur == '<') && (next == '!') && |
| 11397 | (ctxt->input->cur[2] == '-') && |
| 11398 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 11399 | int term; |
| 11400 | |
| 11401 | if (avail < 4) |
| 11402 | goto done; |
| 11403 | ctxt->input->cur += 4; |
| 11404 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 11405 | ctxt->input->cur -= 4; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11406 | if ((!terminate) && (term < 0)) { |
| 11407 | ctxt->progressive = XML_PARSER_COMMENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11408 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11409 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11410 | xmlParseComment(ctxt); |
| 11411 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11412 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11413 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 11414 | (ctxt->input->cur[2] == '[') && |
| 11415 | (ctxt->input->cur[3] == 'C') && |
| 11416 | (ctxt->input->cur[4] == 'D') && |
| 11417 | (ctxt->input->cur[5] == 'A') && |
| 11418 | (ctxt->input->cur[6] == 'T') && |
| 11419 | (ctxt->input->cur[7] == 'A') && |
| 11420 | (ctxt->input->cur[8] == '[')) { |
| 11421 | SKIP(9); |
| 11422 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11423 | break; |
| 11424 | } else if ((cur == '<') && (next == '!') && |
| 11425 | (avail < 9)) { |
| 11426 | goto done; |
| 11427 | } else if (cur == '&') { |
| 11428 | if ((!terminate) && |
| 11429 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 11430 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11431 | xmlParseReference(ctxt); |
| 11432 | } else { |
| 11433 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 11434 | /* |
| 11435 | * Goal of the following test is: |
| 11436 | * - minimize calls to the SAX 'character' callback |
| 11437 | * when they are mergeable |
| 11438 | * - handle an problem for isBlank when we only parse |
| 11439 | * a sequence of blank chars and the next one is |
| 11440 | * not available to check against '<' presence. |
| 11441 | * - tries to homogenize the differences in SAX |
| 11442 | * callbacks between the push and pull versions |
| 11443 | * of the parser. |
| 11444 | */ |
| 11445 | if ((ctxt->inputNr == 1) && |
| 11446 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 11447 | if (!terminate) { |
| 11448 | if (ctxt->progressive) { |
| 11449 | if ((lastlt == NULL) || |
| 11450 | (ctxt->input->cur > lastlt)) |
| 11451 | goto done; |
| 11452 | } else if (xmlParseLookupSequence(ctxt, |
| 11453 | '<', 0, 0) < 0) { |
| 11454 | goto done; |
| 11455 | } |
| 11456 | } |
| 11457 | } |
| 11458 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11459 | xmlParseCharData(ctxt, 0); |
| 11460 | } |
| 11461 | /* |
| 11462 | * Pop-up of finished entities. |
| 11463 | */ |
| 11464 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11465 | xmlPopInput(ctxt); |
| 11466 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11467 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 11468 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11469 | ctxt->instate = XML_PARSER_EOF; |
| 11470 | break; |
| 11471 | } |
| 11472 | break; |
| 11473 | } |
| 11474 | case XML_PARSER_END_TAG: |
| 11475 | if (avail < 2) |
| 11476 | goto done; |
| 11477 | if (!terminate) { |
| 11478 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11479 | /* > can be found unescaped in attribute values */ |
| 11480 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11481 | goto done; |
| 11482 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11483 | goto done; |
| 11484 | } |
| 11485 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11486 | if (ctxt->sax2) { |
| 11487 | xmlParseEndTag2(ctxt, |
| 11488 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 11489 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11490 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11491 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11492 | } |
| 11493 | #ifdef LIBXML_SAX1_ENABLED |
| 11494 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11495 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11496 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11497 | if (ctxt->instate == XML_PARSER_EOF) { |
| 11498 | /* Nothing */ |
| 11499 | } else if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11500 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11501 | } else { |
| 11502 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11503 | } |
| 11504 | break; |
| 11505 | case XML_PARSER_CDATA_SECTION: { |
| 11506 | /* |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11507 | * The Push mode need to have the SAX callback for |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11508 | * cdataBlock merge back contiguous callbacks. |
| 11509 | */ |
| 11510 | int base; |
| 11511 | |
| 11512 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 11513 | if (base < 0) { |
| 11514 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11515 | int tmp; |
| 11516 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11517 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11518 | XML_PARSER_BIG_BUFFER_SIZE); |
| 11519 | if (tmp < 0) { |
| 11520 | tmp = -tmp; |
| 11521 | ctxt->input->cur += tmp; |
| 11522 | goto encoding_error; |
| 11523 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11524 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 11525 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11526 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11527 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11528 | else if (ctxt->sax->characters != NULL) |
| 11529 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11530 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11531 | } |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11532 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11533 | ctxt->checkIndex = 0; |
| 11534 | } |
| 11535 | goto done; |
| 11536 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11537 | int tmp; |
| 11538 | |
| 11539 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 11540 | if ((tmp < 0) || (tmp != base)) { |
| 11541 | tmp = -tmp; |
| 11542 | ctxt->input->cur += tmp; |
| 11543 | goto encoding_error; |
| 11544 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 11545 | if ((ctxt->sax != NULL) && (base == 0) && |
| 11546 | (ctxt->sax->cdataBlock != NULL) && |
| 11547 | (!ctxt->disableSAX)) { |
| 11548 | /* |
| 11549 | * Special case to provide identical behaviour |
| 11550 | * between pull and push parsers on enpty CDATA |
| 11551 | * sections |
| 11552 | */ |
| 11553 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 11554 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 11555 | "<![CDATA[", 9))) |
| 11556 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11557 | BAD_CAST "", 0); |
| 11558 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11559 | (!ctxt->disableSAX)) { |
| 11560 | if (ctxt->sax->cdataBlock != NULL) |
| 11561 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11562 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11563 | else if (ctxt->sax->characters != NULL) |
| 11564 | ctxt->sax->characters(ctxt->userData, |
| 11565 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11566 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 11567 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11568 | ctxt->checkIndex = 0; |
| 11569 | ctxt->instate = XML_PARSER_CONTENT; |
| 11570 | #ifdef DEBUG_PUSH |
| 11571 | xmlGenericError(xmlGenericErrorContext, |
| 11572 | "PP: entering CONTENT\n"); |
| 11573 | #endif |
| 11574 | } |
| 11575 | break; |
| 11576 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11577 | case XML_PARSER_MISC: |
| 11578 | SKIP_BLANKS; |
| 11579 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11580 | avail = ctxt->input->length - |
| 11581 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11582 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11583 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11584 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11585 | if (avail < 2) |
| 11586 | goto done; |
| 11587 | cur = ctxt->input->cur[0]; |
| 11588 | next = ctxt->input->cur[1]; |
| 11589 | if ((cur == '<') && (next == '?')) { |
| 11590 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11591 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11592 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11593 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11594 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11595 | #ifdef DEBUG_PUSH |
| 11596 | xmlGenericError(xmlGenericErrorContext, |
| 11597 | "PP: Parsing PI\n"); |
| 11598 | #endif |
| 11599 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11600 | ctxt->instate = XML_PARSER_MISC; |
| 11601 | ctxt->progressive = 1; |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11602 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11603 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11604 | (ctxt->input->cur[2] == '-') && |
| 11605 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11606 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11607 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11608 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11609 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11610 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11611 | #ifdef DEBUG_PUSH |
| 11612 | xmlGenericError(xmlGenericErrorContext, |
| 11613 | "PP: Parsing Comment\n"); |
| 11614 | #endif |
| 11615 | xmlParseComment(ctxt); |
| 11616 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11617 | ctxt->progressive = 1; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11618 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11619 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11620 | (ctxt->input->cur[2] == 'D') && |
| 11621 | (ctxt->input->cur[3] == 'O') && |
| 11622 | (ctxt->input->cur[4] == 'C') && |
| 11623 | (ctxt->input->cur[5] == 'T') && |
| 11624 | (ctxt->input->cur[6] == 'Y') && |
| 11625 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11626 | (ctxt->input->cur[8] == 'E')) { |
| 11627 | if ((!terminate) && |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11628 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) { |
| 11629 | ctxt->progressive = XML_PARSER_DTD; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11630 | goto done; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11631 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11632 | #ifdef DEBUG_PUSH |
| 11633 | xmlGenericError(xmlGenericErrorContext, |
| 11634 | "PP: Parsing internal subset\n"); |
| 11635 | #endif |
| 11636 | ctxt->inSubset = 1; |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 11637 | ctxt->progressive = 0; |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 11638 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11639 | xmlParseDocTypeDecl(ctxt); |
| 11640 | if (RAW == '[') { |
| 11641 | ctxt->instate = XML_PARSER_DTD; |
| 11642 | #ifdef DEBUG_PUSH |
| 11643 | xmlGenericError(xmlGenericErrorContext, |
| 11644 | "PP: entering DTD\n"); |
| 11645 | #endif |
| 11646 | } else { |
| 11647 | /* |
| 11648 | * Create and update the external subset. |
| 11649 | */ |
| 11650 | ctxt->inSubset = 2; |
| 11651 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11652 | (ctxt->sax->externalSubset != NULL)) |
| 11653 | ctxt->sax->externalSubset(ctxt->userData, |
| 11654 | ctxt->intSubName, ctxt->extSubSystem, |
| 11655 | ctxt->extSubURI); |
| 11656 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11657 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11658 | ctxt->instate = XML_PARSER_PROLOG; |
| 11659 | #ifdef DEBUG_PUSH |
| 11660 | xmlGenericError(xmlGenericErrorContext, |
| 11661 | "PP: entering PROLOG\n"); |
| 11662 | #endif |
| 11663 | } |
| 11664 | } else if ((cur == '<') && (next == '!') && |
| 11665 | (avail < 9)) { |
| 11666 | goto done; |
| 11667 | } else { |
| 11668 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11669 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11670 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11671 | #ifdef DEBUG_PUSH |
| 11672 | xmlGenericError(xmlGenericErrorContext, |
| 11673 | "PP: entering START_TAG\n"); |
| 11674 | #endif |
| 11675 | } |
| 11676 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11677 | case XML_PARSER_PROLOG: |
| 11678 | SKIP_BLANKS; |
| 11679 | if (ctxt->input->buf == NULL) |
| 11680 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11681 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11682 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11683 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11684 | if (avail < 2) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11685 | goto done; |
| 11686 | cur = ctxt->input->cur[0]; |
| 11687 | next = ctxt->input->cur[1]; |
| 11688 | if ((cur == '<') && (next == '?')) { |
| 11689 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11690 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11691 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11692 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11693 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11694 | #ifdef DEBUG_PUSH |
| 11695 | xmlGenericError(xmlGenericErrorContext, |
| 11696 | "PP: Parsing PI\n"); |
| 11697 | #endif |
| 11698 | xmlParsePI(ctxt); |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11699 | ctxt->instate = XML_PARSER_PROLOG; |
| 11700 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11701 | } else if ((cur == '<') && (next == '!') && |
| 11702 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11703 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11704 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11705 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11706 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11707 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11708 | #ifdef DEBUG_PUSH |
| 11709 | xmlGenericError(xmlGenericErrorContext, |
| 11710 | "PP: Parsing Comment\n"); |
| 11711 | #endif |
| 11712 | xmlParseComment(ctxt); |
| 11713 | ctxt->instate = XML_PARSER_PROLOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11714 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11715 | } else if ((cur == '<') && (next == '!') && |
| 11716 | (avail < 4)) { |
| 11717 | goto done; |
| 11718 | } else { |
| 11719 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11720 | if (ctxt->progressive == 0) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11721 | ctxt->progressive = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11722 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11723 | #ifdef DEBUG_PUSH |
| 11724 | xmlGenericError(xmlGenericErrorContext, |
| 11725 | "PP: entering START_TAG\n"); |
| 11726 | #endif |
| 11727 | } |
| 11728 | break; |
| 11729 | case XML_PARSER_EPILOG: |
| 11730 | SKIP_BLANKS; |
| 11731 | if (ctxt->input->buf == NULL) |
| 11732 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11733 | else |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11734 | avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 11735 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11736 | if (avail < 2) |
| 11737 | goto done; |
| 11738 | cur = ctxt->input->cur[0]; |
| 11739 | next = ctxt->input->cur[1]; |
| 11740 | if ((cur == '<') && (next == '?')) { |
| 11741 | if ((!terminate) && |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11742 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) { |
| 11743 | ctxt->progressive = XML_PARSER_PI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11744 | goto done; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11745 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11746 | #ifdef DEBUG_PUSH |
| 11747 | xmlGenericError(xmlGenericErrorContext, |
| 11748 | "PP: Parsing PI\n"); |
| 11749 | #endif |
| 11750 | xmlParsePI(ctxt); |
| 11751 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 11752 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11753 | } else if ((cur == '<') && (next == '!') && |
| 11754 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11755 | if ((!terminate) && |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11756 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) { |
| 11757 | ctxt->progressive = XML_PARSER_COMMENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11758 | goto done; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11759 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11760 | #ifdef DEBUG_PUSH |
| 11761 | xmlGenericError(xmlGenericErrorContext, |
| 11762 | "PP: Parsing Comment\n"); |
| 11763 | #endif |
| 11764 | xmlParseComment(ctxt); |
| 11765 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 11766 | ctxt->progressive = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11767 | } else if ((cur == '<') && (next == '!') && |
| 11768 | (avail < 4)) { |
| 11769 | goto done; |
| 11770 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11771 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11772 | ctxt->instate = XML_PARSER_EOF; |
| 11773 | #ifdef DEBUG_PUSH |
| 11774 | xmlGenericError(xmlGenericErrorContext, |
| 11775 | "PP: entering EOF\n"); |
| 11776 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11777 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11778 | ctxt->sax->endDocument(ctxt->userData); |
| 11779 | goto done; |
| 11780 | } |
| 11781 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11782 | case XML_PARSER_DTD: { |
| 11783 | /* |
| 11784 | * Sorry but progressive parsing of the internal subset |
| 11785 | * is not expected to be supported. We first check that |
| 11786 | * the full content of the internal subset is available and |
| 11787 | * the parsing is launched only at that point. |
| 11788 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11789 | * section and not in a ']]>' sequence which are conditional |
| 11790 | * sections (whoever argued to keep that crap in XML deserve |
| 11791 | * a place in hell !). |
| 11792 | */ |
| 11793 | int base, i; |
| 11794 | xmlChar *buf; |
| 11795 | xmlChar quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11796 | size_t use; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11797 | |
| 11798 | base = ctxt->input->cur - ctxt->input->base; |
| 11799 | if (base < 0) return(0); |
| 11800 | if (ctxt->checkIndex > base) |
| 11801 | base = ctxt->checkIndex; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11802 | buf = xmlBufContent(ctxt->input->buf->buffer); |
| 11803 | use = xmlBufUse(ctxt->input->buf->buffer); |
| 11804 | for (;(unsigned int) base < use; base++) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11805 | if (quote != 0) { |
| 11806 | if (buf[base] == quote) |
| 11807 | quote = 0; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11808 | continue; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11809 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11810 | if ((quote == 0) && (buf[base] == '<')) { |
| 11811 | int found = 0; |
| 11812 | /* special handling of comments */ |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11813 | if (((unsigned int) base + 4 < use) && |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11814 | (buf[base + 1] == '!') && |
| 11815 | (buf[base + 2] == '-') && |
| 11816 | (buf[base + 3] == '-')) { |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11817 | for (;(unsigned int) base + 3 < use; base++) { |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11818 | if ((buf[base] == '-') && |
| 11819 | (buf[base + 1] == '-') && |
| 11820 | (buf[base + 2] == '>')) { |
| 11821 | found = 1; |
| 11822 | base += 2; |
| 11823 | break; |
| 11824 | } |
| 11825 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11826 | if (!found) { |
| 11827 | #if 0 |
| 11828 | fprintf(stderr, "unfinished comment\n"); |
| 11829 | #endif |
| 11830 | break; /* for */ |
| 11831 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11832 | continue; |
| 11833 | } |
| 11834 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11835 | if (buf[base] == '"') { |
| 11836 | quote = '"'; |
| 11837 | continue; |
| 11838 | } |
| 11839 | if (buf[base] == '\'') { |
| 11840 | quote = '\''; |
| 11841 | continue; |
| 11842 | } |
| 11843 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11844 | #if 0 |
| 11845 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 11846 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 11847 | #endif |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11848 | if ((unsigned int) base +1 >= use) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11849 | break; |
| 11850 | if (buf[base + 1] == ']') { |
| 11851 | /* conditional crap, skip both ']' ! */ |
| 11852 | base++; |
| 11853 | continue; |
| 11854 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11855 | for (i = 1; (unsigned int) base + i < use; i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11856 | if (buf[base + i] == '>') { |
| 11857 | #if 0 |
| 11858 | fprintf(stderr, "found\n"); |
| 11859 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11860 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11861 | } |
| 11862 | if (!IS_BLANK_CH(buf[base + i])) { |
| 11863 | #if 0 |
| 11864 | fprintf(stderr, "not found\n"); |
| 11865 | #endif |
| 11866 | goto not_end_of_int_subset; |
| 11867 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11868 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11869 | #if 0 |
| 11870 | fprintf(stderr, "end of stream\n"); |
| 11871 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11872 | break; |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 11873 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11874 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11875 | not_end_of_int_subset: |
| 11876 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11877 | } |
| 11878 | /* |
| 11879 | * We didn't found the end of the Internal subset |
| 11880 | */ |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 11881 | if (quote == 0) |
| 11882 | ctxt->checkIndex = base; |
| 11883 | else |
| 11884 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11885 | #ifdef DEBUG_PUSH |
| 11886 | if (next == 0) |
| 11887 | xmlGenericError(xmlGenericErrorContext, |
| 11888 | "PP: lookup of int subset end filed\n"); |
| 11889 | #endif |
| 11890 | goto done; |
| 11891 | |
| 11892 | found_end_int_subset: |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 11893 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11894 | xmlParseInternalSubset(ctxt); |
| 11895 | ctxt->inSubset = 2; |
| 11896 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11897 | (ctxt->sax->externalSubset != NULL)) |
| 11898 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 11899 | ctxt->extSubSystem, ctxt->extSubURI); |
| 11900 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11901 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11902 | ctxt->instate = XML_PARSER_PROLOG; |
| 11903 | ctxt->checkIndex = 0; |
| 11904 | #ifdef DEBUG_PUSH |
| 11905 | xmlGenericError(xmlGenericErrorContext, |
| 11906 | "PP: entering PROLOG\n"); |
| 11907 | #endif |
| 11908 | break; |
| 11909 | } |
| 11910 | case XML_PARSER_COMMENT: |
| 11911 | xmlGenericError(xmlGenericErrorContext, |
| 11912 | "PP: internal error, state == COMMENT\n"); |
| 11913 | ctxt->instate = XML_PARSER_CONTENT; |
| 11914 | #ifdef DEBUG_PUSH |
| 11915 | xmlGenericError(xmlGenericErrorContext, |
| 11916 | "PP: entering CONTENT\n"); |
| 11917 | #endif |
| 11918 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11919 | case XML_PARSER_IGNORE: |
| 11920 | xmlGenericError(xmlGenericErrorContext, |
| 11921 | "PP: internal error, state == IGNORE"); |
| 11922 | ctxt->instate = XML_PARSER_DTD; |
| 11923 | #ifdef DEBUG_PUSH |
| 11924 | xmlGenericError(xmlGenericErrorContext, |
| 11925 | "PP: entering DTD\n"); |
| 11926 | #endif |
| 11927 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11928 | case XML_PARSER_PI: |
| 11929 | xmlGenericError(xmlGenericErrorContext, |
| 11930 | "PP: internal error, state == PI\n"); |
| 11931 | ctxt->instate = XML_PARSER_CONTENT; |
| 11932 | #ifdef DEBUG_PUSH |
| 11933 | xmlGenericError(xmlGenericErrorContext, |
| 11934 | "PP: entering CONTENT\n"); |
| 11935 | #endif |
| 11936 | break; |
| 11937 | case XML_PARSER_ENTITY_DECL: |
| 11938 | xmlGenericError(xmlGenericErrorContext, |
| 11939 | "PP: internal error, state == ENTITY_DECL\n"); |
| 11940 | ctxt->instate = XML_PARSER_DTD; |
| 11941 | #ifdef DEBUG_PUSH |
| 11942 | xmlGenericError(xmlGenericErrorContext, |
| 11943 | "PP: entering DTD\n"); |
| 11944 | #endif |
| 11945 | break; |
| 11946 | case XML_PARSER_ENTITY_VALUE: |
| 11947 | xmlGenericError(xmlGenericErrorContext, |
| 11948 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 11949 | ctxt->instate = XML_PARSER_CONTENT; |
| 11950 | #ifdef DEBUG_PUSH |
| 11951 | xmlGenericError(xmlGenericErrorContext, |
| 11952 | "PP: entering DTD\n"); |
| 11953 | #endif |
| 11954 | break; |
| 11955 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11956 | xmlGenericError(xmlGenericErrorContext, |
| 11957 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 11958 | ctxt->instate = XML_PARSER_START_TAG; |
| 11959 | #ifdef DEBUG_PUSH |
| 11960 | xmlGenericError(xmlGenericErrorContext, |
| 11961 | "PP: entering START_TAG\n"); |
| 11962 | #endif |
| 11963 | break; |
| 11964 | case XML_PARSER_SYSTEM_LITERAL: |
| 11965 | xmlGenericError(xmlGenericErrorContext, |
| 11966 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 11967 | ctxt->instate = XML_PARSER_START_TAG; |
| 11968 | #ifdef DEBUG_PUSH |
| 11969 | xmlGenericError(xmlGenericErrorContext, |
| 11970 | "PP: entering START_TAG\n"); |
| 11971 | #endif |
| 11972 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 11973 | case XML_PARSER_PUBLIC_LITERAL: |
| 11974 | xmlGenericError(xmlGenericErrorContext, |
| 11975 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 11976 | ctxt->instate = XML_PARSER_START_TAG; |
| 11977 | #ifdef DEBUG_PUSH |
| 11978 | xmlGenericError(xmlGenericErrorContext, |
| 11979 | "PP: entering START_TAG\n"); |
| 11980 | #endif |
| 11981 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11982 | } |
| 11983 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 11984 | done: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11985 | #ifdef DEBUG_PUSH |
| 11986 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 11987 | #endif |
| 11988 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11989 | encoding_error: |
| 11990 | { |
| 11991 | char buffer[150]; |
| 11992 | |
| 11993 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 11994 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 11995 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 11996 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 11997 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 11998 | BAD_CAST buffer, NULL); |
| 11999 | } |
| 12000 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12001 | } |
| 12002 | |
| 12003 | /** |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12004 | * xmlParseCheckTransition: |
| 12005 | * @ctxt: an XML parser context |
| 12006 | * @chunk: a char array |
| 12007 | * @size: the size in byte of the chunk |
| 12008 | * |
| 12009 | * Check depending on the current parser state if the chunk given must be |
| 12010 | * processed immediately or one need more data to advance on parsing. |
| 12011 | * |
| 12012 | * Returns -1 in case of error, 0 if the push is not needed and 1 if needed |
| 12013 | */ |
| 12014 | static int |
| 12015 | xmlParseCheckTransition(xmlParserCtxtPtr ctxt, const char *chunk, int size) { |
| 12016 | if ((ctxt == NULL) || (chunk == NULL) || (size < 0)) |
| 12017 | return(-1); |
| 12018 | if (ctxt->instate == XML_PARSER_START_TAG) { |
| 12019 | if (memchr(chunk, '>', size) != NULL) |
| 12020 | return(1); |
| 12021 | return(0); |
| 12022 | } |
| 12023 | if (ctxt->progressive == XML_PARSER_COMMENT) { |
| 12024 | if (memchr(chunk, '>', size) != NULL) |
| 12025 | return(1); |
| 12026 | return(0); |
| 12027 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12028 | if (ctxt->instate == XML_PARSER_CDATA_SECTION) { |
| 12029 | if (memchr(chunk, '>', size) != NULL) |
| 12030 | return(1); |
| 12031 | return(0); |
| 12032 | } |
Daniel Veillard | f572a78 | 2012-07-19 20:36:25 +0800 | [diff] [blame] | 12033 | if (ctxt->progressive == XML_PARSER_PI) { |
| 12034 | if (memchr(chunk, '>', size) != NULL) |
| 12035 | return(1); |
| 12036 | return(0); |
| 12037 | } |
Daniel Veillard | 5353bbf | 2012-08-03 12:03:31 +0800 | [diff] [blame] | 12038 | if (ctxt->instate == XML_PARSER_END_TAG) { |
| 12039 | if (memchr(chunk, '>', size) != NULL) |
| 12040 | return(1); |
| 12041 | return(0); |
| 12042 | } |
| 12043 | if ((ctxt->progressive == XML_PARSER_DTD) || |
| 12044 | (ctxt->instate == XML_PARSER_DTD)) { |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12045 | if (memchr(chunk, ']', size) != NULL) |
| 12046 | return(1); |
| 12047 | return(0); |
| 12048 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12049 | return(1); |
| 12050 | } |
| 12051 | |
| 12052 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12053 | * xmlParseChunk: |
| 12054 | * @ctxt: an XML parser context |
| 12055 | * @chunk: an char array |
| 12056 | * @size: the size in byte of the chunk |
| 12057 | * @terminate: last chunk indicator |
| 12058 | * |
| 12059 | * Parse a Chunk of memory |
| 12060 | * |
| 12061 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 12062 | */ |
| 12063 | int |
| 12064 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 12065 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12066 | int end_in_lf = 0; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12067 | int remain = 0; |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12068 | size_t old_avail = 0; |
| 12069 | size_t avail = 0; |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12070 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12071 | if (ctxt == NULL) |
| 12072 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 12073 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12074 | return(ctxt->errNo); |
Daniel Veillard | 48b4cdd | 2012-07-30 16:16:04 +0800 | [diff] [blame] | 12075 | if (ctxt->instate == XML_PARSER_EOF) |
| 12076 | return(-1); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 12077 | if (ctxt->instate == XML_PARSER_START) |
| 12078 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12079 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 12080 | (chunk[size - 1] == '\r')) { |
| 12081 | end_in_lf = 1; |
| 12082 | size--; |
| 12083 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12084 | |
| 12085 | xmldecl_done: |
| 12086 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12087 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12088 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12089 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12090 | size_t cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12091 | int res; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12092 | |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12093 | old_avail = xmlBufUse(ctxt->input->buf->buffer); |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12094 | /* |
| 12095 | * Specific handling if we autodetected an encoding, we should not |
| 12096 | * push more than the first line ... which depend on the encoding |
| 12097 | * And only push the rest once the final encoding was detected |
| 12098 | */ |
| 12099 | if ((ctxt->instate == XML_PARSER_START) && (ctxt->input != NULL) && |
| 12100 | (ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL)) { |
Nikolay Sivov | 7304683 | 2010-01-19 15:38:05 +0100 | [diff] [blame] | 12101 | unsigned int len = 45; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12102 | |
| 12103 | if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12104 | BAD_CAST "UTF-16")) || |
| 12105 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12106 | BAD_CAST "UTF16"))) |
| 12107 | len = 90; |
| 12108 | else if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12109 | BAD_CAST "UCS-4")) || |
| 12110 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 12111 | BAD_CAST "UCS4"))) |
| 12112 | len = 180; |
| 12113 | |
| 12114 | if (ctxt->input->buf->rawconsumed < len) |
| 12115 | len -= ctxt->input->buf->rawconsumed; |
| 12116 | |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12117 | /* |
| 12118 | * Change size for reading the initial declaration only |
| 12119 | * if size is greater than len. Otherwise, memmove in xmlBufferAdd |
| 12120 | * will blindly copy extra bytes from memory. |
| 12121 | */ |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 12122 | if ((unsigned int) size > len) { |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 12123 | remain = size - len; |
| 12124 | size = len; |
| 12125 | } else { |
| 12126 | remain = 0; |
| 12127 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12128 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12129 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 12130 | if (res < 0) { |
| 12131 | ctxt->errNo = XML_PARSER_EOF; |
| 12132 | ctxt->disableSAX = 1; |
| 12133 | return (XML_PARSER_EOF); |
| 12134 | } |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12135 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12136 | #ifdef DEBUG_PUSH |
| 12137 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12138 | #endif |
| 12139 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12140 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 12141 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 12142 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 12143 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 12144 | (in->raw != NULL)) { |
| 12145 | int nbchars; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12146 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12147 | nbchars = xmlCharEncInput(in); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12148 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12149 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12150 | xmlGenericError(xmlGenericErrorContext, |
| 12151 | "xmlParseChunk: encoder error\n"); |
| 12152 | return(XML_ERR_INVALID_ENCODING); |
| 12153 | } |
| 12154 | } |
| 12155 | } |
| 12156 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12157 | if (remain != 0) { |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12158 | xmlParseTryOrFinish(ctxt, 0); |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12159 | } else { |
| 12160 | if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) |
| 12161 | avail = xmlBufUse(ctxt->input->buf->buffer); |
| 12162 | /* |
| 12163 | * Depending on the current state it may not be such |
| 12164 | * a good idea to try parsing if there is nothing in the chunk |
| 12165 | * which would be worth doing a parser state transition and we |
| 12166 | * need to wait for more data |
| 12167 | */ |
| 12168 | if ((terminate) || (avail > XML_MAX_TEXT_LENGTH) || |
| 12169 | (old_avail == 0) || (avail == 0) || |
| 12170 | (xmlParseCheckTransition(ctxt, |
| 12171 | (const char *)&ctxt->input->base[old_avail], |
| 12172 | avail - old_avail))) |
| 12173 | xmlParseTryOrFinish(ctxt, terminate); |
| 12174 | } |
Daniel Veillard | 2b52aa0 | 2012-07-31 10:53:47 +0800 | [diff] [blame] | 12175 | if ((ctxt->input != NULL) && |
| 12176 | (((ctxt->input->end - ctxt->input->cur) > XML_MAX_LOOKUP_LIMIT) || |
| 12177 | ((ctxt->input->cur - ctxt->input->base) > XML_MAX_LOOKUP_LIMIT)) && |
| 12178 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 12179 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup"); |
| 12180 | ctxt->instate = XML_PARSER_EOF; |
| 12181 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 12182 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 12183 | return(ctxt->errNo); |
| 12184 | |
| 12185 | if (remain != 0) { |
| 12186 | chunk += size; |
| 12187 | size = remain; |
| 12188 | remain = 0; |
| 12189 | goto xmldecl_done; |
| 12190 | } |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 12191 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 12192 | (ctxt->input->buf != NULL)) { |
| 12193 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
| 12194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12195 | if (terminate) { |
| 12196 | /* |
| 12197 | * Check for termination |
| 12198 | */ |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12199 | int cur_avail = 0; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12200 | |
| 12201 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12202 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12203 | cur_avail = ctxt->input->length - |
| 12204 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12205 | else |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12206 | cur_avail = xmlBufUse(ctxt->input->buf->buffer) - |
| 12207 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12208 | } |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12209 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12210 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 12211 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12212 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12213 | } |
Daniel Veillard | 6568645 | 2012-07-19 18:25:01 +0800 | [diff] [blame] | 12214 | if ((ctxt->instate == XML_PARSER_EPILOG) && (cur_avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12215 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 12216 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12217 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 12218 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12219 | ctxt->sax->endDocument(ctxt->userData); |
| 12220 | } |
| 12221 | ctxt->instate = XML_PARSER_EOF; |
| 12222 | } |
Daniel Veillard | 6c91aa3 | 2012-10-25 15:33:59 +0800 | [diff] [blame] | 12223 | if (ctxt->wellFormed == 0) |
| 12224 | return((xmlParserErrors) ctxt->errNo); |
| 12225 | else |
| 12226 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12227 | } |
| 12228 | |
| 12229 | /************************************************************************ |
| 12230 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12231 | * I/O front end functions to the parser * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12232 | * * |
| 12233 | ************************************************************************/ |
| 12234 | |
| 12235 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12236 | * xmlCreatePushParserCtxt: |
| 12237 | * @sax: a SAX handler |
| 12238 | * @user_data: The user data returned on SAX callbacks |
| 12239 | * @chunk: a pointer to an array of chars |
| 12240 | * @size: number of chars in the array |
| 12241 | * @filename: an optional file name or URI |
| 12242 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12243 | * Create a parser context for using the XML parser in push mode. |
| 12244 | * If @buffer and @size are non-NULL, the data is used to detect |
| 12245 | * the encoding. The remaining characters will be parsed so they |
| 12246 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12247 | * To allow content encoding detection, @size should be >= 4 |
| 12248 | * The value of @filename is used for fetching external entities |
| 12249 | * and error/warning reports. |
| 12250 | * |
| 12251 | * Returns the new parser context or NULL |
| 12252 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 12253 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12254 | xmlParserCtxtPtr |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12255 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12256 | const char *chunk, int size, const char *filename) { |
| 12257 | xmlParserCtxtPtr ctxt; |
| 12258 | xmlParserInputPtr inputStream; |
| 12259 | xmlParserInputBufferPtr buf; |
| 12260 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12261 | |
| 12262 | /* |
| 12263 | * plug some encoding conversion routines |
| 12264 | */ |
| 12265 | if ((chunk != NULL) && (size >= 4)) |
| 12266 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12267 | |
| 12268 | buf = xmlAllocParserInputBuffer(enc); |
| 12269 | if (buf == NULL) return(NULL); |
| 12270 | |
| 12271 | ctxt = xmlNewParserCtxt(); |
| 12272 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 12273 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12274 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12275 | return(NULL); |
| 12276 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12277 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12278 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 12279 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12280 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12281 | xmlFreeParserInputBuffer(buf); |
| 12282 | xmlFreeParserCtxt(ctxt); |
| 12283 | return(NULL); |
| 12284 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12285 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12286 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12287 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12288 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12289 | xmlFree(ctxt->sax); |
| 12290 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12291 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12292 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12293 | xmlFreeParserInputBuffer(buf); |
| 12294 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12295 | return(NULL); |
| 12296 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12297 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12298 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12299 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12300 | else |
| 12301 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12302 | if (user_data != NULL) |
| 12303 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12304 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12305 | if (filename == NULL) { |
| 12306 | ctxt->directory = NULL; |
| 12307 | } else { |
| 12308 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12309 | } |
| 12310 | |
| 12311 | inputStream = xmlNewInputStream(ctxt); |
| 12312 | if (inputStream == NULL) { |
| 12313 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 12314 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12315 | return(NULL); |
| 12316 | } |
| 12317 | |
| 12318 | if (filename == NULL) |
| 12319 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12320 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 12321 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 12322 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 12323 | if (inputStream->filename == NULL) { |
| 12324 | xmlFreeParserCtxt(ctxt); |
| 12325 | xmlFreeParserInputBuffer(buf); |
| 12326 | return(NULL); |
| 12327 | } |
| 12328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12329 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 12330 | xmlBufResetInput(inputStream->buf->buffer, inputStream); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12331 | inputPush(ctxt, inputStream); |
| 12332 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 12333 | /* |
| 12334 | * If the caller didn't provide an initial 'chunk' for determining |
| 12335 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 12336 | * that it can be automatically determined later |
| 12337 | */ |
| 12338 | if ((size == 0) || (chunk == NULL)) { |
| 12339 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 12340 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12341 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 12342 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12343 | |
Daniel Veillard | 768eb3b | 2012-07-16 14:19:49 +0800 | [diff] [blame] | 12344 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 12345 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 12346 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12347 | #ifdef DEBUG_PUSH |
| 12348 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12349 | #endif |
| 12350 | } |
| 12351 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 12352 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12353 | xmlSwitchEncoding(ctxt, enc); |
| 12354 | } |
| 12355 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12356 | return(ctxt); |
| 12357 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 12358 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12359 | |
| 12360 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12361 | * xmlStopParser: |
| 12362 | * @ctxt: an XML parser context |
| 12363 | * |
| 12364 | * Blocks further parser processing |
| 12365 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12366 | void |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 12367 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 12368 | if (ctxt == NULL) |
| 12369 | return; |
| 12370 | ctxt->instate = XML_PARSER_EOF; |
| 12371 | ctxt->disableSAX = 1; |
| 12372 | if (ctxt->input != NULL) { |
| 12373 | ctxt->input->cur = BAD_CAST""; |
| 12374 | ctxt->input->base = ctxt->input->cur; |
| 12375 | } |
| 12376 | } |
| 12377 | |
| 12378 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12379 | * xmlCreateIOParserCtxt: |
| 12380 | * @sax: a SAX handler |
| 12381 | * @user_data: The user data returned on SAX callbacks |
| 12382 | * @ioread: an I/O read function |
| 12383 | * @ioclose: an I/O close function |
| 12384 | * @ioctx: an I/O handler |
| 12385 | * @enc: the charset encoding if known |
| 12386 | * |
| 12387 | * Create a parser context for using the XML parser with an existing |
| 12388 | * I/O stream |
| 12389 | * |
| 12390 | * Returns the new parser context or NULL |
| 12391 | */ |
| 12392 | xmlParserCtxtPtr |
| 12393 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 12394 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 12395 | void *ioctx, xmlCharEncoding enc) { |
| 12396 | xmlParserCtxtPtr ctxt; |
| 12397 | xmlParserInputPtr inputStream; |
| 12398 | xmlParserInputBufferPtr buf; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12399 | |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 12400 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12401 | |
| 12402 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12403 | if (buf == NULL) { |
| 12404 | if (ioclose != NULL) |
| 12405 | ioclose(ioctx); |
| 12406 | return (NULL); |
| 12407 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12408 | |
| 12409 | ctxt = xmlNewParserCtxt(); |
| 12410 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12411 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12412 | return(NULL); |
| 12413 | } |
| 12414 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12415 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12416 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12417 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12418 | xmlFree(ctxt->sax); |
| 12419 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12420 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12421 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12422 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12423 | return(NULL); |
| 12424 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12425 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12426 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12427 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12428 | else |
| 12429 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12430 | if (user_data != NULL) |
| 12431 | ctxt->userData = user_data; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12432 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12433 | |
| 12434 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 12435 | if (inputStream == NULL) { |
| 12436 | xmlFreeParserCtxt(ctxt); |
| 12437 | return(NULL); |
| 12438 | } |
| 12439 | inputPush(ctxt, inputStream); |
| 12440 | |
| 12441 | return(ctxt); |
| 12442 | } |
| 12443 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12444 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12445 | /************************************************************************ |
| 12446 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12447 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12448 | * * |
| 12449 | ************************************************************************/ |
| 12450 | |
| 12451 | /** |
| 12452 | * xmlIOParseDTD: |
| 12453 | * @sax: the SAX handler block or NULL |
| 12454 | * @input: an Input Buffer |
| 12455 | * @enc: the charset encoding if known |
| 12456 | * |
| 12457 | * Load and parse a DTD |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12458 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12459 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12460 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12461 | */ |
| 12462 | |
| 12463 | xmlDtdPtr |
| 12464 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 12465 | xmlCharEncoding enc) { |
| 12466 | xmlDtdPtr ret = NULL; |
| 12467 | xmlParserCtxtPtr ctxt; |
| 12468 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12469 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12470 | |
| 12471 | if (input == NULL) |
| 12472 | return(NULL); |
| 12473 | |
| 12474 | ctxt = xmlNewParserCtxt(); |
| 12475 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12476 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12477 | return(NULL); |
| 12478 | } |
| 12479 | |
| 12480 | /* |
| 12481 | * Set-up the SAX context |
| 12482 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12483 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12484 | if (ctxt->sax != NULL) |
| 12485 | xmlFree(ctxt->sax); |
| 12486 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 12487 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12488 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12489 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12490 | |
| 12491 | /* |
| 12492 | * generate a parser input from the I/O handler |
| 12493 | */ |
| 12494 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12495 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12496 | if (pinput == NULL) { |
| 12497 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12498 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12499 | xmlFreeParserCtxt(ctxt); |
| 12500 | return(NULL); |
| 12501 | } |
| 12502 | |
| 12503 | /* |
| 12504 | * plug some encoding conversion routines here. |
| 12505 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12506 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 12507 | if (sax != NULL) ctxt->sax = NULL; |
| 12508 | xmlFreeParserCtxt(ctxt); |
| 12509 | return(NULL); |
| 12510 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12511 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12512 | xmlSwitchEncoding(ctxt, enc); |
| 12513 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12514 | |
| 12515 | pinput->filename = NULL; |
| 12516 | pinput->line = 1; |
| 12517 | pinput->col = 1; |
| 12518 | pinput->base = ctxt->input->cur; |
| 12519 | pinput->cur = ctxt->input->cur; |
| 12520 | pinput->free = NULL; |
| 12521 | |
| 12522 | /* |
| 12523 | * let's parse that entity knowing it's an external subset. |
| 12524 | */ |
| 12525 | ctxt->inSubset = 2; |
| 12526 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12527 | if (ctxt->myDoc == NULL) { |
| 12528 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12529 | return(NULL); |
| 12530 | } |
| 12531 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12532 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12533 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12534 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12535 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 12536 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12537 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12538 | * Get the 4 first bytes and decode the charset |
| 12539 | * if enc != XML_CHAR_ENCODING_NONE |
| 12540 | * plug some encoding conversion routines. |
| 12541 | */ |
| 12542 | start[0] = RAW; |
| 12543 | start[1] = NXT(1); |
| 12544 | start[2] = NXT(2); |
| 12545 | start[3] = NXT(3); |
| 12546 | enc = xmlDetectCharEncoding(start, 4); |
| 12547 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12548 | xmlSwitchEncoding(ctxt, enc); |
| 12549 | } |
| 12550 | } |
| 12551 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12552 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 12553 | |
| 12554 | if (ctxt->myDoc != NULL) { |
| 12555 | if (ctxt->wellFormed) { |
| 12556 | ret = ctxt->myDoc->extSubset; |
| 12557 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 12558 | if (ret != NULL) { |
| 12559 | xmlNodePtr tmp; |
| 12560 | |
| 12561 | ret->doc = NULL; |
| 12562 | tmp = ret->children; |
| 12563 | while (tmp != NULL) { |
| 12564 | tmp->doc = NULL; |
| 12565 | tmp = tmp->next; |
| 12566 | } |
| 12567 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12568 | } else { |
| 12569 | ret = NULL; |
| 12570 | } |
| 12571 | xmlFreeDoc(ctxt->myDoc); |
| 12572 | ctxt->myDoc = NULL; |
| 12573 | } |
| 12574 | if (sax != NULL) ctxt->sax = NULL; |
| 12575 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12576 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12577 | return(ret); |
| 12578 | } |
| 12579 | |
| 12580 | /** |
| 12581 | * xmlSAXParseDTD: |
| 12582 | * @sax: the SAX handler block |
| 12583 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12584 | * @SystemID: a NAME* containing the URL to the DTD |
| 12585 | * |
| 12586 | * Load and parse an external subset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12587 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12588 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12589 | */ |
| 12590 | |
| 12591 | xmlDtdPtr |
| 12592 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 12593 | const xmlChar *SystemID) { |
| 12594 | xmlDtdPtr ret = NULL; |
| 12595 | xmlParserCtxtPtr ctxt; |
| 12596 | xmlParserInputPtr input = NULL; |
| 12597 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12598 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12599 | |
| 12600 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 12601 | |
| 12602 | ctxt = xmlNewParserCtxt(); |
| 12603 | if (ctxt == NULL) { |
| 12604 | return(NULL); |
| 12605 | } |
| 12606 | |
| 12607 | /* |
| 12608 | * Set-up the SAX context |
| 12609 | */ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12610 | if (sax != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12611 | if (ctxt->sax != NULL) |
| 12612 | xmlFree(ctxt->sax); |
| 12613 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 12614 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12615 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12616 | |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12617 | /* |
| 12618 | * Canonicalise the system ID |
| 12619 | */ |
| 12620 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 12621 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12622 | xmlFreeParserCtxt(ctxt); |
| 12623 | return(NULL); |
| 12624 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12625 | |
| 12626 | /* |
| 12627 | * Ask the Entity resolver to load the damn thing |
| 12628 | */ |
| 12629 | |
| 12630 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 12631 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 12632 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12633 | if (input == NULL) { |
| 12634 | if (sax != NULL) ctxt->sax = NULL; |
| 12635 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12636 | if (systemIdCanonic != NULL) |
| 12637 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12638 | return(NULL); |
| 12639 | } |
| 12640 | |
| 12641 | /* |
| 12642 | * plug some encoding conversion routines here. |
| 12643 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12644 | if (xmlPushInput(ctxt, input) < 0) { |
| 12645 | if (sax != NULL) ctxt->sax = NULL; |
| 12646 | xmlFreeParserCtxt(ctxt); |
| 12647 | if (systemIdCanonic != NULL) |
| 12648 | xmlFree(systemIdCanonic); |
| 12649 | return(NULL); |
| 12650 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12651 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12652 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 12653 | xmlSwitchEncoding(ctxt, enc); |
| 12654 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12655 | |
| 12656 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12657 | input->filename = (char *) systemIdCanonic; |
| 12658 | else |
| 12659 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12660 | input->line = 1; |
| 12661 | input->col = 1; |
| 12662 | input->base = ctxt->input->cur; |
| 12663 | input->cur = ctxt->input->cur; |
| 12664 | input->free = NULL; |
| 12665 | |
| 12666 | /* |
| 12667 | * let's parse that entity knowing it's an external subset. |
| 12668 | */ |
| 12669 | ctxt->inSubset = 2; |
| 12670 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12671 | if (ctxt->myDoc == NULL) { |
| 12672 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12673 | if (sax != NULL) ctxt->sax = NULL; |
| 12674 | xmlFreeParserCtxt(ctxt); |
| 12675 | return(NULL); |
| 12676 | } |
| 12677 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12678 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12679 | ExternalID, SystemID); |
| 12680 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 12681 | |
| 12682 | if (ctxt->myDoc != NULL) { |
| 12683 | if (ctxt->wellFormed) { |
| 12684 | ret = ctxt->myDoc->extSubset; |
| 12685 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 12686 | if (ret != NULL) { |
| 12687 | xmlNodePtr tmp; |
| 12688 | |
| 12689 | ret->doc = NULL; |
| 12690 | tmp = ret->children; |
| 12691 | while (tmp != NULL) { |
| 12692 | tmp->doc = NULL; |
| 12693 | tmp = tmp->next; |
| 12694 | } |
| 12695 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12696 | } else { |
| 12697 | ret = NULL; |
| 12698 | } |
| 12699 | xmlFreeDoc(ctxt->myDoc); |
| 12700 | ctxt->myDoc = NULL; |
| 12701 | } |
| 12702 | if (sax != NULL) ctxt->sax = NULL; |
| 12703 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12704 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12705 | return(ret); |
| 12706 | } |
| 12707 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12708 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12709 | /** |
| 12710 | * xmlParseDTD: |
| 12711 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12712 | * @SystemID: a NAME* containing the URL to the DTD |
| 12713 | * |
| 12714 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12715 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12716 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12717 | */ |
| 12718 | |
| 12719 | xmlDtdPtr |
| 12720 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 12721 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 12722 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12723 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12724 | |
| 12725 | /************************************************************************ |
| 12726 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12727 | * Front ends when parsing an Entity * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12728 | * * |
| 12729 | ************************************************************************/ |
| 12730 | |
| 12731 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12732 | * xmlParseCtxtExternalEntity: |
| 12733 | * @ctx: the existing parsing context |
| 12734 | * @URL: the URL for the entity to load |
| 12735 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12736 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12737 | * |
| 12738 | * Parse an external general entity within an existing parsing context |
| 12739 | * An external general parsed entity is well-formed if it matches the |
| 12740 | * production labeled extParsedEnt. |
| 12741 | * |
| 12742 | * [78] extParsedEnt ::= TextDecl? content |
| 12743 | * |
| 12744 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12745 | * the parser error code otherwise |
| 12746 | */ |
| 12747 | |
| 12748 | int |
| 12749 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12750 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12751 | xmlParserCtxtPtr ctxt; |
| 12752 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12753 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12754 | xmlSAXHandlerPtr oldsax = NULL; |
| 12755 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12756 | xmlChar start[4]; |
| 12757 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12758 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12759 | if (ctx == NULL) return(-1); |
| 12760 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12761 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12762 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12763 | return(XML_ERR_ENTITY_LOOP); |
| 12764 | } |
| 12765 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12766 | if (lst != NULL) |
| 12767 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12768 | if ((URL == NULL) && (ID == NULL)) |
| 12769 | return(-1); |
| 12770 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12771 | return(-1); |
| 12772 | |
Rob Richards | 798743a | 2009-06-19 13:54:25 -0400 | [diff] [blame] | 12773 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, ctx); |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12774 | if (ctxt == NULL) { |
| 12775 | return(-1); |
| 12776 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12777 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12778 | oldsax = ctxt->sax; |
| 12779 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12780 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12781 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12782 | if (newDoc == NULL) { |
| 12783 | xmlFreeParserCtxt(ctxt); |
| 12784 | return(-1); |
| 12785 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12786 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12787 | if (ctx->myDoc->dict) { |
| 12788 | newDoc->dict = ctx->myDoc->dict; |
| 12789 | xmlDictReference(newDoc->dict); |
| 12790 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12791 | if (ctx->myDoc != NULL) { |
| 12792 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12793 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12794 | } |
| 12795 | if (ctx->myDoc->URL != NULL) { |
| 12796 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12797 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12798 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12799 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12800 | ctxt->sax = oldsax; |
| 12801 | xmlFreeParserCtxt(ctxt); |
| 12802 | newDoc->intSubset = NULL; |
| 12803 | newDoc->extSubset = NULL; |
| 12804 | xmlFreeDoc(newDoc); |
| 12805 | return(-1); |
| 12806 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12807 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12808 | nodePush(ctxt, newDoc->children); |
| 12809 | if (ctx->myDoc == NULL) { |
| 12810 | ctxt->myDoc = newDoc; |
| 12811 | } else { |
| 12812 | ctxt->myDoc = ctx->myDoc; |
| 12813 | newDoc->children->doc = ctx->myDoc; |
| 12814 | } |
| 12815 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12816 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12817 | * Get the 4 first bytes and decode the charset |
| 12818 | * if enc != XML_CHAR_ENCODING_NONE |
| 12819 | * plug some encoding conversion routines. |
| 12820 | */ |
| 12821 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12822 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12823 | start[0] = RAW; |
| 12824 | start[1] = NXT(1); |
| 12825 | start[2] = NXT(2); |
| 12826 | start[3] = NXT(3); |
| 12827 | enc = xmlDetectCharEncoding(start, 4); |
| 12828 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12829 | xmlSwitchEncoding(ctxt, enc); |
| 12830 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12831 | } |
| 12832 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12833 | /* |
| 12834 | * Parse a possible text declaration first |
| 12835 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12836 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12837 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12838 | /* |
| 12839 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 12840 | */ |
| 12841 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 12842 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 12843 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12844 | "Version mismatch between document and entity\n"); |
| 12845 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12846 | } |
| 12847 | |
| 12848 | /* |
Daniel Veillard | 4aa68ab | 2012-04-02 17:50:54 +0800 | [diff] [blame] | 12849 | * If the user provided its own SAX callbacks then reuse the |
| 12850 | * useData callback field, otherwise the expected setup in a |
| 12851 | * DOM builder is to have userData == ctxt |
| 12852 | */ |
| 12853 | if (ctx->userData == ctx) |
| 12854 | ctxt->userData = ctxt; |
| 12855 | else |
| 12856 | ctxt->userData = ctx->userData; |
| 12857 | |
| 12858 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12859 | * Doing validity checking on chunk doesn't make sense |
| 12860 | */ |
| 12861 | ctxt->instate = XML_PARSER_CONTENT; |
| 12862 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12863 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12864 | ctxt->loadsubset = ctx->loadsubset; |
| 12865 | ctxt->depth = ctx->depth + 1; |
| 12866 | ctxt->replaceEntities = ctx->replaceEntities; |
| 12867 | if (ctxt->validate) { |
| 12868 | ctxt->vctxt.error = ctx->vctxt.error; |
| 12869 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12870 | } else { |
| 12871 | ctxt->vctxt.error = NULL; |
| 12872 | ctxt->vctxt.warning = NULL; |
| 12873 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 12874 | ctxt->vctxt.nodeTab = NULL; |
| 12875 | ctxt->vctxt.nodeNr = 0; |
| 12876 | ctxt->vctxt.nodeMax = 0; |
| 12877 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12878 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12879 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12880 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12881 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12882 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12883 | ctxt->dictNames = ctx->dictNames; |
| 12884 | ctxt->attsDefault = ctx->attsDefault; |
| 12885 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 12886 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12887 | |
| 12888 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12889 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12890 | ctx->validate = ctxt->validate; |
| 12891 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12892 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12893 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12894 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12895 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12896 | } |
| 12897 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12898 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12899 | } |
| 12900 | |
| 12901 | if (!ctxt->wellFormed) { |
| 12902 | if (ctxt->errNo == 0) |
| 12903 | ret = 1; |
| 12904 | else |
| 12905 | ret = ctxt->errNo; |
| 12906 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12907 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12908 | xmlNodePtr cur; |
| 12909 | |
| 12910 | /* |
| 12911 | * Return the newly created nodeset after unlinking it from |
| 12912 | * they pseudo parent. |
| 12913 | */ |
| 12914 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12915 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12916 | while (cur != NULL) { |
| 12917 | cur->parent = NULL; |
| 12918 | cur = cur->next; |
| 12919 | } |
| 12920 | newDoc->children->children = NULL; |
| 12921 | } |
| 12922 | ret = 0; |
| 12923 | } |
| 12924 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12925 | ctxt->dict = NULL; |
| 12926 | ctxt->attsDefault = NULL; |
| 12927 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12928 | xmlFreeParserCtxt(ctxt); |
| 12929 | newDoc->intSubset = NULL; |
| 12930 | newDoc->extSubset = NULL; |
| 12931 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12932 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12933 | return(ret); |
| 12934 | } |
| 12935 | |
| 12936 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12937 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12938 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12939 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12940 | * @sax: the SAX handler bloc (possibly NULL) |
| 12941 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12942 | * @depth: Used for loop detection, use 0 |
| 12943 | * @URL: the URL for the entity to load |
| 12944 | * @ID: the System ID for the entity to load |
| 12945 | * @list: the return value for the set of parsed nodes |
| 12946 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12947 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12948 | * |
| 12949 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12950 | * the parser error code otherwise |
| 12951 | */ |
| 12952 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12953 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12954 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 12955 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12956 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12957 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12958 | xmlParserCtxtPtr ctxt; |
| 12959 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12960 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12961 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12962 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12963 | xmlChar start[4]; |
| 12964 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12965 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12966 | if (((depth > 40) && |
| 12967 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12968 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12969 | return(XML_ERR_ENTITY_LOOP); |
| 12970 | } |
| 12971 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12972 | if (list != NULL) |
| 12973 | *list = NULL; |
| 12974 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12975 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12976 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12977 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12978 | |
| 12979 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 12980 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 12981 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12982 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12983 | if (oldctxt != NULL) { |
| 12984 | ctxt->_private = oldctxt->_private; |
| 12985 | ctxt->loadsubset = oldctxt->loadsubset; |
| 12986 | ctxt->validate = oldctxt->validate; |
| 12987 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12988 | ctxt->record_info = oldctxt->record_info; |
| 12989 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 12990 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 12991 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12992 | } else { |
| 12993 | /* |
| 12994 | * Doing validity checking on chunk without context |
| 12995 | * doesn't make sense |
| 12996 | */ |
| 12997 | ctxt->_private = NULL; |
| 12998 | ctxt->validate = 0; |
| 12999 | ctxt->external = 2; |
| 13000 | ctxt->loadsubset = 0; |
| 13001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13002 | if (sax != NULL) { |
| 13003 | oldsax = ctxt->sax; |
| 13004 | ctxt->sax = sax; |
| 13005 | if (user_data != NULL) |
| 13006 | ctxt->userData = user_data; |
| 13007 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13008 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13009 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13010 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13011 | ctxt->node_seq.maximum = 0; |
| 13012 | ctxt->node_seq.length = 0; |
| 13013 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13014 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13015 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13016 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13017 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13018 | newDoc->intSubset = doc->intSubset; |
| 13019 | newDoc->extSubset = doc->extSubset; |
| 13020 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13021 | xmlDictReference(newDoc->dict); |
| 13022 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13023 | if (doc->URL != NULL) { |
| 13024 | newDoc->URL = xmlStrdup(doc->URL); |
| 13025 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13026 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13027 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13028 | if (sax != NULL) |
| 13029 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13030 | ctxt->node_seq.maximum = 0; |
| 13031 | ctxt->node_seq.length = 0; |
| 13032 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13033 | xmlFreeParserCtxt(ctxt); |
| 13034 | newDoc->intSubset = NULL; |
| 13035 | newDoc->extSubset = NULL; |
| 13036 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13037 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13038 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13039 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13040 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 13041 | ctxt->myDoc = doc; |
| 13042 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13043 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13044 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13045 | * Get the 4 first bytes and decode the charset |
| 13046 | * if enc != XML_CHAR_ENCODING_NONE |
| 13047 | * plug some encoding conversion routines. |
| 13048 | */ |
| 13049 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 13050 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 13051 | start[0] = RAW; |
| 13052 | start[1] = NXT(1); |
| 13053 | start[2] = NXT(2); |
| 13054 | start[3] = NXT(3); |
| 13055 | enc = xmlDetectCharEncoding(start, 4); |
| 13056 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 13057 | xmlSwitchEncoding(ctxt, enc); |
| 13058 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 13059 | } |
| 13060 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13061 | /* |
| 13062 | * Parse a possible text declaration first |
| 13063 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 13064 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13065 | xmlParseTextDecl(ctxt); |
| 13066 | } |
| 13067 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13068 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13069 | ctxt->depth = depth; |
| 13070 | |
| 13071 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13072 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13073 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13074 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 13075 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13076 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13077 | } |
| 13078 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13079 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13080 | } |
| 13081 | |
| 13082 | if (!ctxt->wellFormed) { |
| 13083 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13084 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13085 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13086 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13087 | } else { |
| 13088 | if (list != NULL) { |
| 13089 | xmlNodePtr cur; |
| 13090 | |
| 13091 | /* |
| 13092 | * Return the newly created nodeset after unlinking it from |
| 13093 | * they pseudo parent. |
| 13094 | */ |
| 13095 | cur = newDoc->children->children; |
| 13096 | *list = cur; |
| 13097 | while (cur != NULL) { |
| 13098 | cur->parent = NULL; |
| 13099 | cur = cur->next; |
| 13100 | } |
| 13101 | newDoc->children->children = NULL; |
| 13102 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13103 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13104 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13105 | |
| 13106 | /* |
| 13107 | * Record in the parent context the number of entities replacement |
| 13108 | * done when parsing that reference. |
| 13109 | */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 13110 | if (oldctxt != NULL) |
| 13111 | oldctxt->nbentities += ctxt->nbentities; |
| 13112 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13113 | /* |
| 13114 | * Also record the size of the entity parsed |
| 13115 | */ |
| 13116 | if (ctxt->input != NULL) { |
| 13117 | oldctxt->sizeentities += ctxt->input->consumed; |
| 13118 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 13119 | } |
| 13120 | /* |
| 13121 | * And record the last error if any |
| 13122 | */ |
| 13123 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13124 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13125 | |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13126 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13127 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 13128 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 13129 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 13130 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 13131 | ctxt->node_seq.maximum = 0; |
| 13132 | ctxt->node_seq.length = 0; |
| 13133 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13134 | xmlFreeParserCtxt(ctxt); |
| 13135 | newDoc->intSubset = NULL; |
| 13136 | newDoc->extSubset = NULL; |
| 13137 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13138 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13139 | return(ret); |
| 13140 | } |
| 13141 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13142 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13143 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13144 | * xmlParseExternalEntity: |
| 13145 | * @doc: the document the chunk pertains to |
| 13146 | * @sax: the SAX handler bloc (possibly NULL) |
| 13147 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13148 | * @depth: Used for loop detection, use 0 |
| 13149 | * @URL: the URL for the entity to load |
| 13150 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13151 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13152 | * |
| 13153 | * Parse an external general entity |
| 13154 | * An external general parsed entity is well-formed if it matches the |
| 13155 | * production labeled extParsedEnt. |
| 13156 | * |
| 13157 | * [78] extParsedEnt ::= TextDecl? content |
| 13158 | * |
| 13159 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 13160 | * the parser error code otherwise |
| 13161 | */ |
| 13162 | |
| 13163 | int |
| 13164 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13165 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 13166 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13167 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 13168 | } |
| 13169 | |
| 13170 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 13171 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13172 | * @doc: the document the chunk pertains to |
| 13173 | * @sax: the SAX handler bloc (possibly NULL) |
| 13174 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13175 | * @depth: Used for loop detection, use 0 |
| 13176 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13177 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13178 | * |
| 13179 | * Parse a well-balanced chunk of an XML document |
| 13180 | * called by the parser |
| 13181 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13182 | * the content production in the XML grammar: |
| 13183 | * |
| 13184 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13185 | * |
| 13186 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13187 | * the parser error code otherwise |
| 13188 | */ |
| 13189 | |
| 13190 | int |
| 13191 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13192 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13193 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 13194 | depth, string, lst, 0 ); |
| 13195 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13196 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13197 | |
| 13198 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13199 | * xmlParseBalancedChunkMemoryInternal: |
| 13200 | * @oldctxt: the existing parsing context |
| 13201 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13202 | * @user_data: the user data field for the parser context |
| 13203 | * @lst: the return value for the set of parsed nodes |
| 13204 | * |
| 13205 | * |
| 13206 | * Parse a well-balanced chunk of an XML document |
| 13207 | * called by the parser |
| 13208 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13209 | * the content production in the XML grammar: |
| 13210 | * |
| 13211 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13212 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13213 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13214 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13215 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13216 | * 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] | 13217 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13218 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13219 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13220 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 13221 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 13222 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13223 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13224 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13225 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13226 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13227 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13228 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13229 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13230 | #ifdef SAX2 |
| 13231 | int i; |
| 13232 | #endif |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13233 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13234 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 13235 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13236 | return(XML_ERR_ENTITY_LOOP); |
| 13237 | } |
| 13238 | |
| 13239 | |
| 13240 | if (lst != NULL) |
| 13241 | *lst = NULL; |
| 13242 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13243 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13244 | |
| 13245 | size = xmlStrlen(string); |
| 13246 | |
| 13247 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13248 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13249 | if (user_data != NULL) |
| 13250 | ctxt->userData = user_data; |
| 13251 | else |
| 13252 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13253 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 13254 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 13255 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13256 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13257 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13258 | |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 13259 | #ifdef SAX2 |
| 13260 | /* propagate namespaces down the entity */ |
| 13261 | for (i = 0;i < oldctxt->nsNr;i += 2) { |
| 13262 | nsPush(ctxt, oldctxt->nsTab[i], oldctxt->nsTab[i+1]); |
| 13263 | } |
| 13264 | #endif |
| 13265 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13266 | oldsax = ctxt->sax; |
| 13267 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13268 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13269 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 13270 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13271 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 13272 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13273 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13274 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13275 | if (newDoc == NULL) { |
| 13276 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13277 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13278 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13279 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13280 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13281 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13282 | newDoc->dict = ctxt->dict; |
| 13283 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13284 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13285 | } else { |
| 13286 | ctxt->myDoc = oldctxt->myDoc; |
| 13287 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13288 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13289 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13290 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13291 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13292 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13293 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13294 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13295 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13296 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13297 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13298 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13299 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13300 | ctxt->myDoc->children = NULL; |
| 13301 | ctxt->myDoc->last = NULL; |
| 13302 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13303 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13304 | ctxt->instate = XML_PARSER_CONTENT; |
| 13305 | ctxt->depth = oldctxt->depth + 1; |
| 13306 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13307 | ctxt->validate = 0; |
| 13308 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 13309 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 13310 | /* |
| 13311 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13312 | */ |
| 13313 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13314 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13315 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13316 | ctxt->attsDefault = oldctxt->attsDefault; |
| 13317 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13318 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13319 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13320 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13321 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13322 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13323 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13324 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13325 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13326 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13327 | } |
| 13328 | |
| 13329 | if (!ctxt->wellFormed) { |
| 13330 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 13331 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13332 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13333 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13334 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13335 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13336 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13337 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 13338 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13339 | xmlNodePtr cur; |
| 13340 | |
| 13341 | /* |
| 13342 | * Return the newly created nodeset after unlinking it from |
| 13343 | * they pseudo parent. |
| 13344 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13345 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13346 | *lst = cur; |
| 13347 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13348 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 13349 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 13350 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 13351 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 13352 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 13353 | oldctxt->myDoc, cur); |
| 13354 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13355 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13356 | cur->parent = NULL; |
| 13357 | cur = cur->next; |
| 13358 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13359 | ctxt->myDoc->children->children = NULL; |
| 13360 | } |
| 13361 | if (ctxt->myDoc != NULL) { |
| 13362 | xmlFreeNode(ctxt->myDoc->children); |
| 13363 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 13364 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13365 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13366 | |
| 13367 | /* |
| 13368 | * Record in the parent context the number of entities replacement |
| 13369 | * done when parsing that reference. |
| 13370 | */ |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 13371 | if (oldctxt != NULL) |
| 13372 | oldctxt->nbentities += ctxt->nbentities; |
| 13373 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13374 | /* |
| 13375 | * Also record the last error if any |
| 13376 | */ |
| 13377 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13378 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 13379 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13380 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 13381 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 13382 | ctxt->attsDefault = NULL; |
| 13383 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13384 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13385 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 13386 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 13387 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13388 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13389 | return(ret); |
| 13390 | } |
| 13391 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13392 | /** |
| 13393 | * xmlParseInNodeContext: |
| 13394 | * @node: the context node |
| 13395 | * @data: the input string |
| 13396 | * @datalen: the input string length in bytes |
| 13397 | * @options: a combination of xmlParserOption |
| 13398 | * @lst: the return value for the set of parsed nodes |
| 13399 | * |
| 13400 | * Parse a well-balanced chunk of an XML document |
| 13401 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 13402 | * |
| 13403 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 13404 | * the content production in the XML grammar: |
| 13405 | * |
| 13406 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13407 | * |
| 13408 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13409 | * error code otherwise |
| 13410 | */ |
| 13411 | xmlParserErrors |
| 13412 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 13413 | int options, xmlNodePtr *lst) { |
| 13414 | #ifdef SAX2 |
| 13415 | xmlParserCtxtPtr ctxt; |
| 13416 | xmlDocPtr doc = NULL; |
| 13417 | xmlNodePtr fake, cur; |
| 13418 | int nsnr = 0; |
| 13419 | |
| 13420 | xmlParserErrors ret = XML_ERR_OK; |
| 13421 | |
| 13422 | /* |
| 13423 | * check all input parameters, grab the document |
| 13424 | */ |
| 13425 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 13426 | return(XML_ERR_INTERNAL_ERROR); |
| 13427 | switch (node->type) { |
| 13428 | case XML_ELEMENT_NODE: |
| 13429 | case XML_ATTRIBUTE_NODE: |
| 13430 | case XML_TEXT_NODE: |
| 13431 | case XML_CDATA_SECTION_NODE: |
| 13432 | case XML_ENTITY_REF_NODE: |
| 13433 | case XML_PI_NODE: |
| 13434 | case XML_COMMENT_NODE: |
| 13435 | case XML_DOCUMENT_NODE: |
| 13436 | case XML_HTML_DOCUMENT_NODE: |
| 13437 | break; |
| 13438 | default: |
| 13439 | return(XML_ERR_INTERNAL_ERROR); |
| 13440 | |
| 13441 | } |
| 13442 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 13443 | (node->type != XML_DOCUMENT_NODE) && |
| 13444 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 13445 | node = node->parent; |
| 13446 | if (node == NULL) |
| 13447 | return(XML_ERR_INTERNAL_ERROR); |
| 13448 | if (node->type == XML_ELEMENT_NODE) |
| 13449 | doc = node->doc; |
| 13450 | else |
| 13451 | doc = (xmlDocPtr) node; |
| 13452 | if (doc == NULL) |
| 13453 | return(XML_ERR_INTERNAL_ERROR); |
| 13454 | |
| 13455 | /* |
| 13456 | * allocate a context and set-up everything not related to the |
| 13457 | * node position in the tree |
| 13458 | */ |
| 13459 | if (doc->type == XML_DOCUMENT_NODE) |
| 13460 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 13461 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13462 | else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13463 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13464 | /* |
| 13465 | * When parsing in context, it makes no sense to add implied |
| 13466 | * elements like html/body/etc... |
| 13467 | */ |
| 13468 | options |= HTML_PARSE_NOIMPLIED; |
| 13469 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13470 | #endif |
| 13471 | else |
| 13472 | return(XML_ERR_INTERNAL_ERROR); |
| 13473 | |
| 13474 | if (ctxt == NULL) |
| 13475 | return(XML_ERR_NO_MEMORY); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13476 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13477 | /* |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13478 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 13479 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 13480 | * we must wait until the last moment to free the original one. |
| 13481 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13482 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13483 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13484 | xmlDictFree(ctxt->dict); |
| 13485 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13486 | } else |
| 13487 | options |= XML_PARSE_NODICT; |
| 13488 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13489 | if (doc->encoding != NULL) { |
| 13490 | xmlCharEncodingHandlerPtr hdlr; |
| 13491 | |
| 13492 | if (ctxt->encoding != NULL) |
| 13493 | xmlFree((xmlChar *) ctxt->encoding); |
| 13494 | ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); |
| 13495 | |
| 13496 | hdlr = xmlFindCharEncodingHandler(doc->encoding); |
| 13497 | if (hdlr != NULL) { |
| 13498 | xmlSwitchToEncoding(ctxt, hdlr); |
| 13499 | } else { |
| 13500 | return(XML_ERR_UNSUPPORTED_ENCODING); |
| 13501 | } |
| 13502 | } |
| 13503 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13504 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13505 | xmlDetectSAX2(ctxt); |
| 13506 | ctxt->myDoc = doc; |
| 13507 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13508 | fake = xmlNewComment(NULL); |
| 13509 | if (fake == NULL) { |
| 13510 | xmlFreeParserCtxt(ctxt); |
| 13511 | return(XML_ERR_NO_MEMORY); |
| 13512 | } |
| 13513 | xmlAddChild(node, fake); |
| 13514 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13515 | if (node->type == XML_ELEMENT_NODE) { |
| 13516 | nodePush(ctxt, node); |
| 13517 | /* |
| 13518 | * initialize the SAX2 namespaces stack |
| 13519 | */ |
| 13520 | cur = node; |
| 13521 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 13522 | xmlNsPtr ns = cur->nsDef; |
| 13523 | const xmlChar *iprefix, *ihref; |
| 13524 | |
| 13525 | while (ns != NULL) { |
| 13526 | if (ctxt->dict) { |
| 13527 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 13528 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 13529 | } else { |
| 13530 | iprefix = ns->prefix; |
| 13531 | ihref = ns->href; |
| 13532 | } |
| 13533 | |
| 13534 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 13535 | nsPush(ctxt, iprefix, ihref); |
| 13536 | nsnr++; |
| 13537 | } |
| 13538 | ns = ns->next; |
| 13539 | } |
| 13540 | cur = cur->parent; |
| 13541 | } |
| 13542 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13543 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13544 | |
| 13545 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 13546 | /* |
| 13547 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13548 | */ |
| 13549 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13550 | } |
| 13551 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 13552 | #ifdef LIBXML_HTML_ENABLED |
| 13553 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 13554 | __htmlParseContent(ctxt); |
| 13555 | else |
| 13556 | #endif |
| 13557 | xmlParseContent(ctxt); |
| 13558 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13559 | nsPop(ctxt, nsnr); |
| 13560 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 13561 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13562 | } else if (RAW != 0) { |
| 13563 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 13564 | } |
| 13565 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 13566 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13567 | ctxt->wellFormed = 0; |
| 13568 | } |
| 13569 | |
| 13570 | if (!ctxt->wellFormed) { |
| 13571 | if (ctxt->errNo == 0) |
| 13572 | ret = XML_ERR_INTERNAL_ERROR; |
| 13573 | else |
| 13574 | ret = (xmlParserErrors)ctxt->errNo; |
| 13575 | } else { |
| 13576 | ret = XML_ERR_OK; |
| 13577 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13578 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13579 | /* |
| 13580 | * Return the newly created nodeset after unlinking it from |
| 13581 | * the pseudo sibling. |
| 13582 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13583 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13584 | cur = fake->next; |
| 13585 | fake->next = NULL; |
| 13586 | node->last = fake; |
| 13587 | |
| 13588 | if (cur != NULL) { |
| 13589 | cur->prev = NULL; |
| 13590 | } |
| 13591 | |
| 13592 | *lst = cur; |
| 13593 | |
| 13594 | while (cur != NULL) { |
| 13595 | cur->parent = NULL; |
| 13596 | cur = cur->next; |
| 13597 | } |
| 13598 | |
| 13599 | xmlUnlinkNode(fake); |
| 13600 | xmlFreeNode(fake); |
| 13601 | |
| 13602 | |
| 13603 | if (ret != XML_ERR_OK) { |
| 13604 | xmlFreeNodeList(*lst); |
| 13605 | *lst = NULL; |
| 13606 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13607 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 13608 | if (doc->dict != NULL) |
| 13609 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13610 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13611 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13612 | return(ret); |
| 13613 | #else /* !SAX2 */ |
| 13614 | return(XML_ERR_INTERNAL_ERROR); |
| 13615 | #endif |
| 13616 | } |
| 13617 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13618 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13619 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13620 | * xmlParseBalancedChunkMemoryRecover: |
| 13621 | * @doc: the document the chunk pertains to |
| 13622 | * @sax: the SAX handler bloc (possibly NULL) |
| 13623 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13624 | * @depth: Used for loop detection, use 0 |
| 13625 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13626 | * @lst: the return value for the set of parsed nodes |
| 13627 | * @recover: return nodes even if the data is broken (use 0) |
| 13628 | * |
| 13629 | * |
| 13630 | * Parse a well-balanced chunk of an XML document |
| 13631 | * called by the parser |
| 13632 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13633 | * the content production in the XML grammar: |
| 13634 | * |
| 13635 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13636 | * |
| 13637 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13638 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13639 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13640 | * 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] | 13641 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 13642 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13643 | */ |
| 13644 | int |
| 13645 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13646 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13647 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13648 | xmlParserCtxtPtr ctxt; |
| 13649 | xmlDocPtr newDoc; |
| 13650 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13651 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13652 | int size; |
| 13653 | int ret = 0; |
| 13654 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13655 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13656 | return(XML_ERR_ENTITY_LOOP); |
| 13657 | } |
| 13658 | |
| 13659 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13660 | if (lst != NULL) |
| 13661 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13662 | if (string == NULL) |
| 13663 | return(-1); |
| 13664 | |
| 13665 | size = xmlStrlen(string); |
| 13666 | |
| 13667 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 13668 | if (ctxt == NULL) return(-1); |
| 13669 | ctxt->userData = ctxt; |
| 13670 | if (sax != NULL) { |
| 13671 | oldsax = ctxt->sax; |
| 13672 | ctxt->sax = sax; |
| 13673 | if (user_data != NULL) |
| 13674 | ctxt->userData = user_data; |
| 13675 | } |
| 13676 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13677 | if (newDoc == NULL) { |
| 13678 | xmlFreeParserCtxt(ctxt); |
| 13679 | return(-1); |
| 13680 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13681 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13682 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 13683 | xmlDictFree(ctxt->dict); |
| 13684 | ctxt->dict = doc->dict; |
| 13685 | xmlDictReference(ctxt->dict); |
| 13686 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13687 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13688 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 13689 | ctxt->dictNames = 1; |
| 13690 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13691 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13693 | if (doc != NULL) { |
| 13694 | newDoc->intSubset = doc->intSubset; |
| 13695 | newDoc->extSubset = doc->extSubset; |
| 13696 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13697 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13698 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13699 | if (sax != NULL) |
| 13700 | ctxt->sax = oldsax; |
| 13701 | xmlFreeParserCtxt(ctxt); |
| 13702 | newDoc->intSubset = NULL; |
| 13703 | newDoc->extSubset = NULL; |
| 13704 | xmlFreeDoc(newDoc); |
| 13705 | return(-1); |
| 13706 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13707 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 13708 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13709 | if (doc == NULL) { |
| 13710 | ctxt->myDoc = newDoc; |
| 13711 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 13712 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13713 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13714 | /* Ensure that doc has XML spec namespace */ |
| 13715 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 13716 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13717 | } |
| 13718 | ctxt->instate = XML_PARSER_CONTENT; |
| 13719 | ctxt->depth = depth; |
| 13720 | |
| 13721 | /* |
| 13722 | * Doing validity checking on chunk doesn't make sense |
| 13723 | */ |
| 13724 | ctxt->validate = 0; |
| 13725 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13726 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13727 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 13728 | if ( doc != NULL ){ |
| 13729 | content = doc->children; |
| 13730 | doc->children = NULL; |
| 13731 | xmlParseContent(ctxt); |
| 13732 | doc->children = content; |
| 13733 | } |
| 13734 | else { |
| 13735 | xmlParseContent(ctxt); |
| 13736 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13737 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13738 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13739 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13740 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13741 | } |
| 13742 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13743 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13744 | } |
| 13745 | |
| 13746 | if (!ctxt->wellFormed) { |
| 13747 | if (ctxt->errNo == 0) |
| 13748 | ret = 1; |
| 13749 | else |
| 13750 | ret = ctxt->errNo; |
| 13751 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13752 | ret = 0; |
| 13753 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13754 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13755 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 13756 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13757 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13758 | /* |
| 13759 | * Return the newly created nodeset after unlinking it from |
| 13760 | * they pseudo parent. |
| 13761 | */ |
| 13762 | cur = newDoc->children->children; |
| 13763 | *lst = cur; |
| 13764 | while (cur != NULL) { |
| 13765 | xmlSetTreeDoc(cur, doc); |
| 13766 | cur->parent = NULL; |
| 13767 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13768 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13769 | newDoc->children->children = NULL; |
| 13770 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13771 | |
| 13772 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13773 | ctxt->sax = oldsax; |
| 13774 | xmlFreeParserCtxt(ctxt); |
| 13775 | newDoc->intSubset = NULL; |
| 13776 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13777 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13778 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13779 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13780 | return(ret); |
| 13781 | } |
| 13782 | |
| 13783 | /** |
| 13784 | * xmlSAXParseEntity: |
| 13785 | * @sax: the SAX handler block |
| 13786 | * @filename: the filename |
| 13787 | * |
| 13788 | * parse an XML external entity out of context and build a tree. |
| 13789 | * It use the given SAX function block to handle the parsing callback. |
| 13790 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13791 | * |
| 13792 | * [78] extParsedEnt ::= TextDecl? content |
| 13793 | * |
| 13794 | * This correspond to a "Well Balanced" chunk |
| 13795 | * |
| 13796 | * Returns the resulting document tree |
| 13797 | */ |
| 13798 | |
| 13799 | xmlDocPtr |
| 13800 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13801 | xmlDocPtr ret; |
| 13802 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13803 | |
| 13804 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13805 | if (ctxt == NULL) { |
| 13806 | return(NULL); |
| 13807 | } |
| 13808 | if (sax != NULL) { |
| 13809 | if (ctxt->sax != NULL) |
| 13810 | xmlFree(ctxt->sax); |
| 13811 | ctxt->sax = sax; |
| 13812 | ctxt->userData = NULL; |
| 13813 | } |
| 13814 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13815 | xmlParseExtParsedEnt(ctxt); |
| 13816 | |
| 13817 | if (ctxt->wellFormed) |
| 13818 | ret = ctxt->myDoc; |
| 13819 | else { |
| 13820 | ret = NULL; |
| 13821 | xmlFreeDoc(ctxt->myDoc); |
| 13822 | ctxt->myDoc = NULL; |
| 13823 | } |
| 13824 | if (sax != NULL) |
| 13825 | ctxt->sax = NULL; |
| 13826 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13827 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13828 | return(ret); |
| 13829 | } |
| 13830 | |
| 13831 | /** |
| 13832 | * xmlParseEntity: |
| 13833 | * @filename: the filename |
| 13834 | * |
| 13835 | * parse an XML external entity out of context and build a tree. |
| 13836 | * |
| 13837 | * [78] extParsedEnt ::= TextDecl? content |
| 13838 | * |
| 13839 | * This correspond to a "Well Balanced" chunk |
| 13840 | * |
| 13841 | * Returns the resulting document tree |
| 13842 | */ |
| 13843 | |
| 13844 | xmlDocPtr |
| 13845 | xmlParseEntity(const char *filename) { |
| 13846 | return(xmlSAXParseEntity(NULL, filename)); |
| 13847 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13848 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13849 | |
| 13850 | /** |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13851 | * xmlCreateEntityParserCtxtInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13852 | * @URL: the entity URL |
| 13853 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13854 | * @base: a possible base for the target URI |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13855 | * @pctx: parser context used to set options on new context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13856 | * |
| 13857 | * Create a parser context for an external entity |
| 13858 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13859 | * by default if found at compile-time. |
| 13860 | * |
| 13861 | * Returns the new parser context or NULL |
| 13862 | */ |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13863 | static xmlParserCtxtPtr |
| 13864 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 13865 | const xmlChar *base, xmlParserCtxtPtr pctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13866 | xmlParserCtxtPtr ctxt; |
| 13867 | xmlParserInputPtr inputStream; |
| 13868 | char *directory = NULL; |
| 13869 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13870 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13871 | ctxt = xmlNewParserCtxt(); |
| 13872 | if (ctxt == NULL) { |
| 13873 | return(NULL); |
| 13874 | } |
| 13875 | |
Daniel Veillard | 48247b4 | 2009-07-10 16:12:46 +0200 | [diff] [blame] | 13876 | if (pctx != NULL) { |
| 13877 | ctxt->options = pctx->options; |
| 13878 | ctxt->_private = pctx->_private; |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13879 | } |
| 13880 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13881 | uri = xmlBuildURI(URL, base); |
| 13882 | |
| 13883 | if (uri == NULL) { |
| 13884 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 13885 | if (inputStream == NULL) { |
| 13886 | xmlFreeParserCtxt(ctxt); |
| 13887 | return(NULL); |
| 13888 | } |
| 13889 | |
| 13890 | inputPush(ctxt, inputStream); |
| 13891 | |
| 13892 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13893 | directory = xmlParserGetDirectory((char *)URL); |
| 13894 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13895 | ctxt->directory = directory; |
| 13896 | } else { |
| 13897 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 13898 | if (inputStream == NULL) { |
| 13899 | xmlFree(uri); |
| 13900 | xmlFreeParserCtxt(ctxt); |
| 13901 | return(NULL); |
| 13902 | } |
| 13903 | |
| 13904 | inputPush(ctxt, inputStream); |
| 13905 | |
| 13906 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13907 | directory = xmlParserGetDirectory((char *)uri); |
| 13908 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13909 | ctxt->directory = directory; |
| 13910 | xmlFree(uri); |
| 13911 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13912 | return(ctxt); |
| 13913 | } |
| 13914 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13915 | /** |
| 13916 | * xmlCreateEntityParserCtxt: |
| 13917 | * @URL: the entity URL |
| 13918 | * @ID: the entity PUBLIC ID |
| 13919 | * @base: a possible base for the target URI |
| 13920 | * |
| 13921 | * Create a parser context for an external entity |
| 13922 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13923 | * by default if found at compile-time. |
| 13924 | * |
| 13925 | * Returns the new parser context or NULL |
| 13926 | */ |
| 13927 | xmlParserCtxtPtr |
| 13928 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 13929 | const xmlChar *base) { |
| 13930 | return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); |
| 13931 | |
| 13932 | } |
| 13933 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13934 | /************************************************************************ |
| 13935 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13936 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13937 | * * |
| 13938 | ************************************************************************/ |
| 13939 | |
| 13940 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13941 | * xmlCreateURLParserCtxt: |
| 13942 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13943 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13944 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13945 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13946 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13947 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13948 | * |
| 13949 | * Returns the new parser context or NULL |
| 13950 | */ |
| 13951 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13952 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13953 | { |
| 13954 | xmlParserCtxtPtr ctxt; |
| 13955 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13956 | char *directory = NULL; |
| 13957 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13958 | ctxt = xmlNewParserCtxt(); |
| 13959 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13960 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13961 | return(NULL); |
| 13962 | } |
| 13963 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13964 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13965 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13966 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13967 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 13968 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13969 | if (inputStream == NULL) { |
| 13970 | xmlFreeParserCtxt(ctxt); |
| 13971 | return(NULL); |
| 13972 | } |
| 13973 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13974 | inputPush(ctxt, inputStream); |
| 13975 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 13976 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13977 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13978 | ctxt->directory = directory; |
| 13979 | |
| 13980 | return(ctxt); |
| 13981 | } |
| 13982 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13983 | /** |
| 13984 | * xmlCreateFileParserCtxt: |
| 13985 | * @filename: the filename |
| 13986 | * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 13987 | * Create a parser context for a file content. |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13988 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13989 | * by default if found at compile-time. |
| 13990 | * |
| 13991 | * Returns the new parser context or NULL |
| 13992 | */ |
| 13993 | xmlParserCtxtPtr |
| 13994 | xmlCreateFileParserCtxt(const char *filename) |
| 13995 | { |
| 13996 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 13997 | } |
| 13998 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13999 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14000 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14001 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14002 | * @sax: the SAX handler block |
| 14003 | * @filename: the filename |
| 14004 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14005 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14006 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14007 | * |
| 14008 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14009 | * compressed document is provided by default if found at compile-time. |
| 14010 | * It use the given SAX function block to handle the parsing callback. |
| 14011 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14012 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 14013 | * User data (void *) is stored within the parser context in the |
| 14014 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14015 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14016 | * Returns the resulting document tree |
| 14017 | */ |
| 14018 | |
| 14019 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14020 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 14021 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14022 | xmlDocPtr ret; |
| 14023 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14024 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 14025 | xmlInitParser(); |
| 14026 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14027 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14028 | if (ctxt == NULL) { |
| 14029 | return(NULL); |
| 14030 | } |
| 14031 | if (sax != NULL) { |
| 14032 | if (ctxt->sax != NULL) |
| 14033 | xmlFree(ctxt->sax); |
| 14034 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14035 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14036 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14037 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 14038 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14039 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14040 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 14041 | if (ctxt->directory == NULL) |
| 14042 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14043 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 14044 | ctxt->recovery = recovery; |
| 14045 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14046 | xmlParseDocument(ctxt); |
| 14047 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14048 | if ((ctxt->wellFormed) || recovery) { |
| 14049 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 14050 | if (ret != NULL) { |
| 14051 | if (ctxt->input->buf->compressed > 0) |
| 14052 | ret->compression = 9; |
| 14053 | else |
| 14054 | ret->compression = ctxt->input->buf->compressed; |
| 14055 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 14056 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14057 | else { |
| 14058 | ret = NULL; |
| 14059 | xmlFreeDoc(ctxt->myDoc); |
| 14060 | ctxt->myDoc = NULL; |
| 14061 | } |
| 14062 | if (sax != NULL) |
| 14063 | ctxt->sax = NULL; |
| 14064 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14065 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14066 | return(ret); |
| 14067 | } |
| 14068 | |
| 14069 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 14070 | * xmlSAXParseFile: |
| 14071 | * @sax: the SAX handler block |
| 14072 | * @filename: the filename |
| 14073 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14074 | * documents |
| 14075 | * |
| 14076 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14077 | * compressed document is provided by default if found at compile-time. |
| 14078 | * It use the given SAX function block to handle the parsing callback. |
| 14079 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14080 | * |
| 14081 | * Returns the resulting document tree |
| 14082 | */ |
| 14083 | |
| 14084 | xmlDocPtr |
| 14085 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 14086 | int recovery) { |
| 14087 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 14088 | } |
| 14089 | |
| 14090 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14091 | * xmlRecoverDoc: |
| 14092 | * @cur: a pointer to an array of xmlChar |
| 14093 | * |
| 14094 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14095 | * In the case the document is not Well Formed, a attempt to build a |
| 14096 | * tree is tried anyway |
| 14097 | * |
| 14098 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14099 | */ |
| 14100 | |
| 14101 | xmlDocPtr |
Daniel Veillard | f39eafa | 2009-08-20 19:15:08 +0200 | [diff] [blame] | 14102 | xmlRecoverDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14103 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 14104 | } |
| 14105 | |
| 14106 | /** |
| 14107 | * xmlParseFile: |
| 14108 | * @filename: the filename |
| 14109 | * |
| 14110 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14111 | * compressed document is provided by default if found at compile-time. |
| 14112 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 14113 | * Returns the resulting document tree if the file was wellformed, |
| 14114 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14115 | */ |
| 14116 | |
| 14117 | xmlDocPtr |
| 14118 | xmlParseFile(const char *filename) { |
| 14119 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 14120 | } |
| 14121 | |
| 14122 | /** |
| 14123 | * xmlRecoverFile: |
| 14124 | * @filename: the filename |
| 14125 | * |
| 14126 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 14127 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14128 | * In the case the document is not Well Formed, it attempts to build |
| 14129 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14130 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14131 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14132 | */ |
| 14133 | |
| 14134 | xmlDocPtr |
| 14135 | xmlRecoverFile(const char *filename) { |
| 14136 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 14137 | } |
| 14138 | |
| 14139 | |
| 14140 | /** |
| 14141 | * xmlSetupParserForBuffer: |
| 14142 | * @ctxt: an XML parser context |
| 14143 | * @buffer: a xmlChar * buffer |
| 14144 | * @filename: a file name |
| 14145 | * |
| 14146 | * Setup the parser context to parse a new buffer; Clears any prior |
| 14147 | * contents from the parser context. The buffer parameter must not be |
| 14148 | * NULL, but the filename parameter can be |
| 14149 | */ |
| 14150 | void |
| 14151 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 14152 | const char* filename) |
| 14153 | { |
| 14154 | xmlParserInputPtr input; |
| 14155 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14156 | if ((ctxt == NULL) || (buffer == NULL)) |
| 14157 | return; |
| 14158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14159 | input = xmlNewInputStream(ctxt); |
| 14160 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 14161 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14162 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14163 | return; |
| 14164 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14165 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14166 | xmlClearParserCtxt(ctxt); |
| 14167 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 14168 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14169 | input->base = buffer; |
| 14170 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 14171 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14172 | inputPush(ctxt, input); |
| 14173 | } |
| 14174 | |
| 14175 | /** |
| 14176 | * xmlSAXUserParseFile: |
| 14177 | * @sax: a SAX handler |
| 14178 | * @user_data: The user data returned on SAX callbacks |
| 14179 | * @filename: a file name |
| 14180 | * |
| 14181 | * parse an XML file and call the given SAX handler routines. |
| 14182 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14183 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14184 | * Returns 0 in case of success or a error number otherwise |
| 14185 | */ |
| 14186 | int |
| 14187 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 14188 | const char *filename) { |
| 14189 | int ret = 0; |
| 14190 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14191 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14192 | ctxt = xmlCreateFileParserCtxt(filename); |
| 14193 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 14194 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14195 | xmlFree(ctxt->sax); |
| 14196 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14197 | xmlDetectSAX2(ctxt); |
| 14198 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14199 | if (user_data != NULL) |
| 14200 | ctxt->userData = user_data; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14201 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14202 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14203 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14204 | if (ctxt->wellFormed) |
| 14205 | ret = 0; |
| 14206 | else { |
| 14207 | if (ctxt->errNo != 0) |
| 14208 | ret = ctxt->errNo; |
| 14209 | else |
| 14210 | ret = -1; |
| 14211 | } |
| 14212 | if (sax != NULL) |
| 14213 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14214 | if (ctxt->myDoc != NULL) { |
| 14215 | xmlFreeDoc(ctxt->myDoc); |
| 14216 | ctxt->myDoc = NULL; |
| 14217 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14218 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14219 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14220 | return ret; |
| 14221 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14222 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14223 | |
| 14224 | /************************************************************************ |
| 14225 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14226 | * Front ends when parsing from memory * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14227 | * * |
| 14228 | ************************************************************************/ |
| 14229 | |
| 14230 | /** |
| 14231 | * xmlCreateMemoryParserCtxt: |
| 14232 | * @buffer: a pointer to a char array |
| 14233 | * @size: the size of the array |
| 14234 | * |
| 14235 | * Create a parser context for an XML in-memory document. |
| 14236 | * |
| 14237 | * Returns the new parser context or NULL |
| 14238 | */ |
| 14239 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14240 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14241 | xmlParserCtxtPtr ctxt; |
| 14242 | xmlParserInputPtr input; |
| 14243 | xmlParserInputBufferPtr buf; |
| 14244 | |
| 14245 | if (buffer == NULL) |
| 14246 | return(NULL); |
| 14247 | if (size <= 0) |
| 14248 | return(NULL); |
| 14249 | |
| 14250 | ctxt = xmlNewParserCtxt(); |
| 14251 | if (ctxt == NULL) |
| 14252 | return(NULL); |
| 14253 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 14254 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14255 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14256 | if (buf == NULL) { |
| 14257 | xmlFreeParserCtxt(ctxt); |
| 14258 | return(NULL); |
| 14259 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14260 | |
| 14261 | input = xmlNewInputStream(ctxt); |
| 14262 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 14263 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14264 | xmlFreeParserCtxt(ctxt); |
| 14265 | return(NULL); |
| 14266 | } |
| 14267 | |
| 14268 | input->filename = NULL; |
| 14269 | input->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14270 | xmlBufResetInput(input->buf->buffer, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14271 | |
| 14272 | inputPush(ctxt, input); |
| 14273 | return(ctxt); |
| 14274 | } |
| 14275 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14276 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14277 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14278 | * xmlSAXParseMemoryWithData: |
| 14279 | * @sax: the SAX handler block |
| 14280 | * @buffer: an pointer to a char array |
| 14281 | * @size: the size of the array |
| 14282 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14283 | * documents |
| 14284 | * @data: the userdata |
| 14285 | * |
| 14286 | * parse an XML in-memory block and use the given SAX function block |
| 14287 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14288 | * DOM tree building routines. |
| 14289 | * |
| 14290 | * User data (void *) is stored within the parser context in the |
| 14291 | * context's _private member, so it is available nearly everywhere in libxml |
| 14292 | * |
| 14293 | * Returns the resulting document tree |
| 14294 | */ |
| 14295 | |
| 14296 | xmlDocPtr |
| 14297 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 14298 | int size, int recovery, void *data) { |
| 14299 | xmlDocPtr ret; |
| 14300 | xmlParserCtxtPtr ctxt; |
| 14301 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14302 | xmlInitParser(); |
| 14303 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14304 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14305 | if (ctxt == NULL) return(NULL); |
| 14306 | if (sax != NULL) { |
| 14307 | if (ctxt->sax != NULL) |
| 14308 | xmlFree(ctxt->sax); |
| 14309 | ctxt->sax = sax; |
| 14310 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14311 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14312 | if (data!=NULL) { |
| 14313 | ctxt->_private=data; |
| 14314 | } |
| 14315 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 14316 | ctxt->recovery = recovery; |
| 14317 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14318 | xmlParseDocument(ctxt); |
| 14319 | |
| 14320 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14321 | else { |
| 14322 | ret = NULL; |
| 14323 | xmlFreeDoc(ctxt->myDoc); |
| 14324 | ctxt->myDoc = NULL; |
| 14325 | } |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14326 | if (sax != NULL) |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14327 | ctxt->sax = NULL; |
| 14328 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14329 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14330 | return(ret); |
| 14331 | } |
| 14332 | |
| 14333 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14334 | * xmlSAXParseMemory: |
| 14335 | * @sax: the SAX handler block |
| 14336 | * @buffer: an pointer to a char array |
| 14337 | * @size: the size of the array |
| 14338 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 14339 | * documents |
| 14340 | * |
| 14341 | * parse an XML in-memory block and use the given SAX function block |
| 14342 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 14343 | * DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14344 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14345 | * Returns the resulting document tree |
| 14346 | */ |
| 14347 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14348 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 14349 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 14350 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14351 | } |
| 14352 | |
| 14353 | /** |
| 14354 | * xmlParseMemory: |
| 14355 | * @buffer: an pointer to a char array |
| 14356 | * @size: the size of the array |
| 14357 | * |
| 14358 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14359 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14360 | * Returns the resulting document tree |
| 14361 | */ |
| 14362 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14363 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14364 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 14365 | } |
| 14366 | |
| 14367 | /** |
| 14368 | * xmlRecoverMemory: |
| 14369 | * @buffer: an pointer to a char array |
| 14370 | * @size: the size of the array |
| 14371 | * |
| 14372 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 14373 | * In the case the document is not Well Formed, an attempt to |
| 14374 | * build a tree is tried anyway |
| 14375 | * |
| 14376 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14377 | */ |
| 14378 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 14379 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14380 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 14381 | } |
| 14382 | |
| 14383 | /** |
| 14384 | * xmlSAXUserParseMemory: |
| 14385 | * @sax: a SAX handler |
| 14386 | * @user_data: The user data returned on SAX callbacks |
| 14387 | * @buffer: an in-memory XML document input |
| 14388 | * @size: the length of the XML document in bytes |
| 14389 | * |
| 14390 | * A better SAX parsing routine. |
| 14391 | * 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] | 14392 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14393 | * Returns 0 in case of success or a error number otherwise |
| 14394 | */ |
| 14395 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14396 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14397 | int ret = 0; |
| 14398 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14399 | |
| 14400 | xmlInitParser(); |
| 14401 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14402 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14403 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14404 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 14405 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 14406 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14407 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14408 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 14409 | if (user_data != NULL) |
| 14410 | ctxt->userData = user_data; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14411 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14412 | xmlParseDocument(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14413 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14414 | if (ctxt->wellFormed) |
| 14415 | ret = 0; |
| 14416 | else { |
| 14417 | if (ctxt->errNo != 0) |
| 14418 | ret = ctxt->errNo; |
| 14419 | else |
| 14420 | ret = -1; |
| 14421 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14422 | if (sax != NULL) |
| 14423 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14424 | if (ctxt->myDoc != NULL) { |
| 14425 | xmlFreeDoc(ctxt->myDoc); |
| 14426 | ctxt->myDoc = NULL; |
| 14427 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14428 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14429 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14430 | return ret; |
| 14431 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14432 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14433 | |
| 14434 | /** |
| 14435 | * xmlCreateDocParserCtxt: |
| 14436 | * @cur: a pointer to an array of xmlChar |
| 14437 | * |
| 14438 | * Creates a parser context for an XML in-memory document. |
| 14439 | * |
| 14440 | * Returns the new parser context or NULL |
| 14441 | */ |
| 14442 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14443 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14444 | int len; |
| 14445 | |
| 14446 | if (cur == NULL) |
| 14447 | return(NULL); |
| 14448 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14449 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14450 | } |
| 14451 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14452 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14453 | /** |
| 14454 | * xmlSAXParseDoc: |
| 14455 | * @sax: the SAX handler block |
| 14456 | * @cur: a pointer to an array of xmlChar |
| 14457 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14458 | * documents |
| 14459 | * |
| 14460 | * parse an XML in-memory document and build a tree. |
| 14461 | * It use the given SAX function block to handle the parsing callback. |
| 14462 | * If sax is NULL, fallback to the default DOM tree building routines. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14463 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14464 | * Returns the resulting document tree |
| 14465 | */ |
| 14466 | |
| 14467 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14468 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14469 | xmlDocPtr ret; |
| 14470 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14471 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14472 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14473 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14474 | |
| 14475 | |
| 14476 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14477 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14478 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14479 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14480 | ctxt->sax = sax; |
| 14481 | ctxt->userData = NULL; |
| 14482 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14483 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14484 | |
| 14485 | xmlParseDocument(ctxt); |
| 14486 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14487 | else { |
| 14488 | ret = NULL; |
| 14489 | xmlFreeDoc(ctxt->myDoc); |
| 14490 | ctxt->myDoc = NULL; |
| 14491 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14492 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14493 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14494 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14495 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14496 | return(ret); |
| 14497 | } |
| 14498 | |
| 14499 | /** |
| 14500 | * xmlParseDoc: |
| 14501 | * @cur: a pointer to an array of xmlChar |
| 14502 | * |
| 14503 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14504 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14505 | * Returns the resulting document tree |
| 14506 | */ |
| 14507 | |
| 14508 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14509 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14510 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 14511 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14512 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14513 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14514 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14515 | /************************************************************************ |
| 14516 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14517 | * Specific function to keep track of entities references * |
| 14518 | * and used by the XSLT debugger * |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14519 | * * |
| 14520 | ************************************************************************/ |
| 14521 | |
| 14522 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 14523 | |
| 14524 | /** |
| 14525 | * xmlAddEntityReference: |
| 14526 | * @ent : A valid entity |
| 14527 | * @firstNode : A valid first node for children of entity |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14528 | * @lastNode : A valid last node of children entity |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14529 | * |
| 14530 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 14531 | */ |
| 14532 | static void |
| 14533 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 14534 | xmlNodePtr lastNode) |
| 14535 | { |
| 14536 | if (xmlEntityRefFunc != NULL) { |
| 14537 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 14538 | } |
| 14539 | } |
| 14540 | |
| 14541 | |
| 14542 | /** |
| 14543 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 14544 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14545 | * |
| 14546 | * Set the function to call call back when a xml reference has been made |
| 14547 | */ |
| 14548 | void |
| 14549 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 14550 | { |
| 14551 | xmlEntityRefFunc = func; |
| 14552 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14553 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14554 | |
| 14555 | /************************************************************************ |
| 14556 | * * |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14557 | * Miscellaneous * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14558 | * * |
| 14559 | ************************************************************************/ |
| 14560 | |
| 14561 | #ifdef LIBXML_XPATH_ENABLED |
| 14562 | #include <libxml/xpath.h> |
| 14563 | #endif |
| 14564 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 14565 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14566 | static int xmlParserInitialized = 0; |
| 14567 | |
| 14568 | /** |
| 14569 | * xmlInitParser: |
| 14570 | * |
| 14571 | * Initialization function for the XML parser. |
| 14572 | * This is not reentrant. Call once before processing in case of |
| 14573 | * use in multithreaded programs. |
| 14574 | */ |
| 14575 | |
| 14576 | void |
| 14577 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 14578 | if (xmlParserInitialized != 0) |
| 14579 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14580 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14581 | #ifdef LIBXML_THREAD_ENABLED |
| 14582 | __xmlGlobalInitMutexLock(); |
| 14583 | if (xmlParserInitialized == 0) { |
| 14584 | #endif |
Daniel Veillard | 7dd7080 | 2009-06-04 11:08:39 +0200 | [diff] [blame] | 14585 | xmlInitThreads(); |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 14586 | xmlInitGlobals(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14587 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 14588 | (xmlGenericError == NULL)) |
| 14589 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14590 | xmlInitMemory(); |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 14591 | xmlInitializeDict(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14592 | xmlInitCharEncodingHandlers(); |
| 14593 | xmlDefaultSAXHandlerInit(); |
| 14594 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14595 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14596 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14597 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14598 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14599 | htmlInitAutoClose(); |
| 14600 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14601 | #endif |
| 14602 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14603 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14604 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14605 | xmlParserInitialized = 1; |
| 14606 | #ifdef LIBXML_THREAD_ENABLED |
| 14607 | } |
| 14608 | __xmlGlobalInitMutexUnlock(); |
| 14609 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14610 | } |
| 14611 | |
| 14612 | /** |
| 14613 | * xmlCleanupParser: |
| 14614 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 14615 | * This function name is somewhat misleading. It does not clean up |
| 14616 | * parser state, it cleans up memory allocated by the library itself. |
| 14617 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 14618 | * related global memory allocated for the library processing. |
| 14619 | * It doesn't deallocate any document related memory. One should |
| 14620 | * call xmlCleanupParser() only when the process has finished using |
| 14621 | * the library and all XML/HTML documents built with it. |
| 14622 | * See also xmlInitParser() which has the opposite function of preparing |
| 14623 | * the library for operations. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 14624 | * |
| 14625 | * WARNING: if your application is multithreaded or has plugin support |
| 14626 | * calling this may crash the application if another thread or |
| 14627 | * a plugin is still using libxml2. It's sometimes very hard to |
| 14628 | * guess if libxml2 is in use in the application, some libraries |
| 14629 | * or plugins may use it without notice. In case of doubt abstain |
| 14630 | * from calling this function or do it just before calling exit() |
| 14631 | * to avoid leak reports from valgrind ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14632 | */ |
| 14633 | |
| 14634 | void |
| 14635 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 14636 | if (!xmlParserInitialized) |
| 14637 | return; |
| 14638 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14639 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 14640 | #ifdef LIBXML_CATALOG_ENABLED |
| 14641 | xmlCatalogCleanup(); |
| 14642 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 14643 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 14644 | xmlCleanupInputCallbacks(); |
| 14645 | #ifdef LIBXML_OUTPUT_ENABLED |
| 14646 | xmlCleanupOutputCallbacks(); |
| 14647 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14648 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14649 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 14650 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14651 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 14652 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 14653 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 14654 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14655 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 14656 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14657 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14658 | |
| 14659 | /************************************************************************ |
| 14660 | * * |
| 14661 | * New set (2.6.0) of simpler and more flexible APIs * |
| 14662 | * * |
| 14663 | ************************************************************************/ |
| 14664 | |
| 14665 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14666 | * DICT_FREE: |
| 14667 | * @str: a string |
| 14668 | * |
| 14669 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 14670 | * current scope |
| 14671 | */ |
| 14672 | #define DICT_FREE(str) \ |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14673 | if ((str) && ((!dict) || \ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14674 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 14675 | xmlFree((char *)(str)); |
| 14676 | |
| 14677 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14678 | * xmlCtxtReset: |
| 14679 | * @ctxt: an XML parser context |
| 14680 | * |
| 14681 | * Reset a parser context |
| 14682 | */ |
| 14683 | void |
| 14684 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 14685 | { |
| 14686 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14687 | xmlDictPtr dict; |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 14688 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14689 | if (ctxt == NULL) |
| 14690 | return; |
| 14691 | |
| 14692 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14693 | |
| 14694 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 14695 | xmlFreeInputStream(input); |
| 14696 | } |
| 14697 | ctxt->inputNr = 0; |
| 14698 | ctxt->input = NULL; |
| 14699 | |
| 14700 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 14701 | if (ctxt->spaceTab != NULL) { |
| 14702 | ctxt->spaceTab[0] = -1; |
| 14703 | ctxt->space = &ctxt->spaceTab[0]; |
| 14704 | } else { |
| 14705 | ctxt->space = NULL; |
| 14706 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14707 | |
| 14708 | |
| 14709 | ctxt->nodeNr = 0; |
| 14710 | ctxt->node = NULL; |
| 14711 | |
| 14712 | ctxt->nameNr = 0; |
| 14713 | ctxt->name = NULL; |
| 14714 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14715 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14716 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14717 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14718 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14719 | DICT_FREE(ctxt->directory); |
| 14720 | ctxt->directory = NULL; |
| 14721 | DICT_FREE(ctxt->extSubURI); |
| 14722 | ctxt->extSubURI = NULL; |
| 14723 | DICT_FREE(ctxt->extSubSystem); |
| 14724 | ctxt->extSubSystem = NULL; |
| 14725 | if (ctxt->myDoc != NULL) |
| 14726 | xmlFreeDoc(ctxt->myDoc); |
| 14727 | ctxt->myDoc = NULL; |
| 14728 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14729 | ctxt->standalone = -1; |
| 14730 | ctxt->hasExternalSubset = 0; |
| 14731 | ctxt->hasPErefs = 0; |
| 14732 | ctxt->html = 0; |
| 14733 | ctxt->external = 0; |
| 14734 | ctxt->instate = XML_PARSER_START; |
| 14735 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14736 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14737 | ctxt->wellFormed = 1; |
| 14738 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 14739 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14740 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14741 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14742 | ctxt->vctxt.userData = ctxt; |
| 14743 | ctxt->vctxt.error = xmlParserValidityError; |
| 14744 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14745 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14746 | ctxt->record_info = 0; |
| 14747 | ctxt->nbChars = 0; |
| 14748 | ctxt->checkIndex = 0; |
| 14749 | ctxt->inSubset = 0; |
| 14750 | ctxt->errNo = XML_ERR_OK; |
| 14751 | ctxt->depth = 0; |
| 14752 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 14753 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 14754 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14755 | ctxt->sizeentities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14756 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 14757 | |
| 14758 | if (ctxt->attsDefault != NULL) { |
| 14759 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 14760 | ctxt->attsDefault = NULL; |
| 14761 | } |
| 14762 | if (ctxt->attsSpecial != NULL) { |
| 14763 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 14764 | ctxt->attsSpecial = NULL; |
| 14765 | } |
| 14766 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14767 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14768 | if (ctxt->catalogs != NULL) |
| 14769 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14770 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 14771 | if (ctxt->lastError.code != XML_ERR_OK) |
| 14772 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14773 | } |
| 14774 | |
| 14775 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14776 | * xmlCtxtResetPush: |
| 14777 | * @ctxt: an XML parser context |
| 14778 | * @chunk: a pointer to an array of chars |
| 14779 | * @size: number of chars in the array |
| 14780 | * @filename: an optional file name or URI |
| 14781 | * @encoding: the document encoding, or NULL |
| 14782 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14783 | * Reset a push parser context |
| 14784 | * |
| 14785 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14786 | */ |
| 14787 | int |
| 14788 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 14789 | int size, const char *filename, const char *encoding) |
| 14790 | { |
| 14791 | xmlParserInputPtr inputStream; |
| 14792 | xmlParserInputBufferPtr buf; |
| 14793 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 14794 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14795 | if (ctxt == NULL) |
| 14796 | return(1); |
| 14797 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14798 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 14799 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 14800 | |
| 14801 | buf = xmlAllocParserInputBuffer(enc); |
| 14802 | if (buf == NULL) |
| 14803 | return(1); |
| 14804 | |
| 14805 | if (ctxt == NULL) { |
| 14806 | xmlFreeParserInputBuffer(buf); |
| 14807 | return(1); |
| 14808 | } |
| 14809 | |
| 14810 | xmlCtxtReset(ctxt); |
| 14811 | |
| 14812 | if (ctxt->pushTab == NULL) { |
| 14813 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 14814 | sizeof(xmlChar *)); |
| 14815 | if (ctxt->pushTab == NULL) { |
| 14816 | xmlErrMemory(ctxt, NULL); |
| 14817 | xmlFreeParserInputBuffer(buf); |
| 14818 | return(1); |
| 14819 | } |
| 14820 | } |
| 14821 | |
| 14822 | if (filename == NULL) { |
| 14823 | ctxt->directory = NULL; |
| 14824 | } else { |
| 14825 | ctxt->directory = xmlParserGetDirectory(filename); |
| 14826 | } |
| 14827 | |
| 14828 | inputStream = xmlNewInputStream(ctxt); |
| 14829 | if (inputStream == NULL) { |
| 14830 | xmlFreeParserInputBuffer(buf); |
| 14831 | return(1); |
| 14832 | } |
| 14833 | |
| 14834 | if (filename == NULL) |
| 14835 | inputStream->filename = NULL; |
| 14836 | else |
| 14837 | inputStream->filename = (char *) |
| 14838 | xmlCanonicPath((const xmlChar *) filename); |
| 14839 | inputStream->buf = buf; |
Daniel Veillard | 61551a1 | 2012-07-16 16:28:47 +0800 | [diff] [blame] | 14840 | xmlBufResetInput(buf->buffer, inputStream); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14841 | |
| 14842 | inputPush(ctxt, inputStream); |
| 14843 | |
| 14844 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 14845 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 14846 | size_t base = xmlBufGetInputBase(ctxt->input->buf->buffer, ctxt->input); |
| 14847 | size_t cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14848 | |
| 14849 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 14850 | |
Daniel Veillard | 00ac0d3 | 2012-07-16 18:03:01 +0800 | [diff] [blame] | 14851 | xmlBufSetInputBaseCur(ctxt->input->buf->buffer, ctxt->input, base, cur); |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14852 | #ifdef DEBUG_PUSH |
| 14853 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 14854 | #endif |
| 14855 | } |
| 14856 | |
| 14857 | if (encoding != NULL) { |
| 14858 | xmlCharEncodingHandlerPtr hdlr; |
| 14859 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14860 | if (ctxt->encoding != NULL) |
| 14861 | xmlFree((xmlChar *) ctxt->encoding); |
| 14862 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14863 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14864 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14865 | if (hdlr != NULL) { |
| 14866 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14867 | } else { |
| 14868 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 14869 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 14870 | } |
| 14871 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 14872 | xmlSwitchEncoding(ctxt, enc); |
| 14873 | } |
| 14874 | |
| 14875 | return(0); |
| 14876 | } |
| 14877 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14878 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14879 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14880 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14881 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14882 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14883 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14884 | * |
| 14885 | * Applies the options to the parser context |
| 14886 | * |
| 14887 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14888 | * in case of error. |
| 14889 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14890 | static int |
| 14891 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14892 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14893 | if (ctxt == NULL) |
| 14894 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14895 | if (encoding != NULL) { |
| 14896 | if (ctxt->encoding != NULL) |
| 14897 | xmlFree((xmlChar *) ctxt->encoding); |
| 14898 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14899 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14900 | if (options & XML_PARSE_RECOVER) { |
| 14901 | ctxt->recovery = 1; |
| 14902 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14903 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14904 | } else |
| 14905 | ctxt->recovery = 0; |
| 14906 | if (options & XML_PARSE_DTDLOAD) { |
| 14907 | ctxt->loadsubset = XML_DETECT_IDS; |
| 14908 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14909 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14910 | } else |
| 14911 | ctxt->loadsubset = 0; |
| 14912 | if (options & XML_PARSE_DTDATTR) { |
| 14913 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 14914 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14915 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14916 | } |
| 14917 | if (options & XML_PARSE_NOENT) { |
| 14918 | ctxt->replaceEntities = 1; |
| 14919 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 14920 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14921 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14922 | } else |
| 14923 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14924 | if (options & XML_PARSE_PEDANTIC) { |
| 14925 | ctxt->pedantic = 1; |
| 14926 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14927 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14928 | } else |
| 14929 | ctxt->pedantic = 0; |
| 14930 | if (options & XML_PARSE_NOBLANKS) { |
| 14931 | ctxt->keepBlanks = 0; |
| 14932 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 14933 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14934 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14935 | } else |
| 14936 | ctxt->keepBlanks = 1; |
| 14937 | if (options & XML_PARSE_DTDVALID) { |
| 14938 | ctxt->validate = 1; |
| 14939 | if (options & XML_PARSE_NOWARNING) |
| 14940 | ctxt->vctxt.warning = NULL; |
| 14941 | if (options & XML_PARSE_NOERROR) |
| 14942 | ctxt->vctxt.error = NULL; |
| 14943 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14944 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14945 | } else |
| 14946 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 14947 | if (options & XML_PARSE_NOWARNING) { |
| 14948 | ctxt->sax->warning = NULL; |
| 14949 | options -= XML_PARSE_NOWARNING; |
| 14950 | } |
| 14951 | if (options & XML_PARSE_NOERROR) { |
| 14952 | ctxt->sax->error = NULL; |
| 14953 | ctxt->sax->fatalError = NULL; |
| 14954 | options -= XML_PARSE_NOERROR; |
| 14955 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14956 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14957 | if (options & XML_PARSE_SAX1) { |
| 14958 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 14959 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 14960 | ctxt->sax->startElementNs = NULL; |
| 14961 | ctxt->sax->endElementNs = NULL; |
| 14962 | ctxt->sax->initialized = 1; |
| 14963 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14964 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14965 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14966 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14967 | if (options & XML_PARSE_NODICT) { |
| 14968 | ctxt->dictNames = 0; |
| 14969 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14970 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14971 | } else { |
| 14972 | ctxt->dictNames = 1; |
| 14973 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14974 | if (options & XML_PARSE_NOCDATA) { |
| 14975 | ctxt->sax->cdataBlock = NULL; |
| 14976 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14977 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14978 | } |
| 14979 | if (options & XML_PARSE_NSCLEAN) { |
| 14980 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 14981 | options -= XML_PARSE_NSCLEAN; |
| 14982 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14983 | if (options & XML_PARSE_NONET) { |
| 14984 | ctxt->options |= XML_PARSE_NONET; |
| 14985 | options -= XML_PARSE_NONET; |
| 14986 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 14987 | if (options & XML_PARSE_COMPACT) { |
| 14988 | ctxt->options |= XML_PARSE_COMPACT; |
| 14989 | options -= XML_PARSE_COMPACT; |
| 14990 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 14991 | if (options & XML_PARSE_OLD10) { |
| 14992 | ctxt->options |= XML_PARSE_OLD10; |
| 14993 | options -= XML_PARSE_OLD10; |
| 14994 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 14995 | if (options & XML_PARSE_NOBASEFIX) { |
| 14996 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 14997 | options -= XML_PARSE_NOBASEFIX; |
| 14998 | } |
| 14999 | if (options & XML_PARSE_HUGE) { |
| 15000 | ctxt->options |= XML_PARSE_HUGE; |
| 15001 | options -= XML_PARSE_HUGE; |
Daniel Veillard | 52d8ade | 2012-07-30 10:08:45 +0800 | [diff] [blame] | 15002 | if (ctxt->dict != NULL) |
| 15003 | xmlDictSetLimit(ctxt->dict, 0); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 15004 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 15005 | if (options & XML_PARSE_OLDSAX) { |
| 15006 | ctxt->options |= XML_PARSE_OLDSAX; |
| 15007 | options -= XML_PARSE_OLDSAX; |
| 15008 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 15009 | if (options & XML_PARSE_IGNORE_ENC) { |
| 15010 | ctxt->options |= XML_PARSE_IGNORE_ENC; |
| 15011 | options -= XML_PARSE_IGNORE_ENC; |
| 15012 | } |
Daniel Veillard | 968a03a | 2012-08-13 12:41:33 +0800 | [diff] [blame] | 15013 | if (options & XML_PARSE_BIG_LINES) { |
| 15014 | ctxt->options |= XML_PARSE_BIG_LINES; |
| 15015 | options -= XML_PARSE_BIG_LINES; |
| 15016 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 15017 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15018 | return (options); |
| 15019 | } |
| 15020 | |
| 15021 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15022 | * xmlCtxtUseOptions: |
| 15023 | * @ctxt: an XML parser context |
| 15024 | * @options: a combination of xmlParserOption |
| 15025 | * |
| 15026 | * Applies the options to the parser context |
| 15027 | * |
| 15028 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 15029 | * in case of error. |
| 15030 | */ |
| 15031 | int |
| 15032 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 15033 | { |
| 15034 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 15035 | } |
| 15036 | |
| 15037 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15038 | * xmlDoRead: |
| 15039 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15040 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15041 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15042 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15043 | * @reuse: keep the context for reuse |
| 15044 | * |
| 15045 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15046 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15047 | * Returns the resulting document tree or NULL |
| 15048 | */ |
| 15049 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15050 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 15051 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15052 | { |
| 15053 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 15054 | |
| 15055 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15056 | if (encoding != NULL) { |
| 15057 | xmlCharEncodingHandlerPtr hdlr; |
| 15058 | |
| 15059 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 15060 | if (hdlr != NULL) |
| 15061 | xmlSwitchToEncoding(ctxt, hdlr); |
| 15062 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15063 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 15064 | (ctxt->input->filename == NULL)) |
| 15065 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15066 | xmlParseDocument(ctxt); |
| 15067 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 15068 | ret = ctxt->myDoc; |
| 15069 | else { |
| 15070 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15071 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15072 | xmlFreeDoc(ctxt->myDoc); |
| 15073 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15074 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15075 | ctxt->myDoc = NULL; |
| 15076 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15077 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 15078 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15079 | |
| 15080 | return (ret); |
| 15081 | } |
| 15082 | |
| 15083 | /** |
| 15084 | * xmlReadDoc: |
| 15085 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15086 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15087 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15088 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15089 | * |
| 15090 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15091 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15092 | * Returns the resulting document tree |
| 15093 | */ |
| 15094 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15095 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15096 | { |
| 15097 | xmlParserCtxtPtr ctxt; |
| 15098 | |
| 15099 | if (cur == NULL) |
| 15100 | return (NULL); |
| 15101 | |
| 15102 | ctxt = xmlCreateDocParserCtxt(cur); |
| 15103 | if (ctxt == NULL) |
| 15104 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15105 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15106 | } |
| 15107 | |
| 15108 | /** |
| 15109 | * xmlReadFile: |
| 15110 | * @filename: a file or URL |
| 15111 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15112 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15113 | * |
| 15114 | * parse an XML file from the filesystem or the network. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15115 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15116 | * Returns the resulting document tree |
| 15117 | */ |
| 15118 | xmlDocPtr |
| 15119 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 15120 | { |
| 15121 | xmlParserCtxtPtr ctxt; |
| 15122 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 15123 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15124 | if (ctxt == NULL) |
| 15125 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15126 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15127 | } |
| 15128 | |
| 15129 | /** |
| 15130 | * xmlReadMemory: |
| 15131 | * @buffer: a pointer to a char array |
| 15132 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15133 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15134 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15135 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15136 | * |
| 15137 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15138 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15139 | * Returns the resulting document tree |
| 15140 | */ |
| 15141 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15142 | 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] | 15143 | { |
| 15144 | xmlParserCtxtPtr ctxt; |
| 15145 | |
| 15146 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 15147 | if (ctxt == NULL) |
| 15148 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15149 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15150 | } |
| 15151 | |
| 15152 | /** |
| 15153 | * xmlReadFd: |
| 15154 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15155 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15156 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15157 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15158 | * |
| 15159 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15160 | * NOTE that the file descriptor will not be closed when the |
| 15161 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15162 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15163 | * Returns the resulting document tree |
| 15164 | */ |
| 15165 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15166 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15167 | { |
| 15168 | xmlParserCtxtPtr ctxt; |
| 15169 | xmlParserInputBufferPtr input; |
| 15170 | xmlParserInputPtr stream; |
| 15171 | |
| 15172 | if (fd < 0) |
| 15173 | return (NULL); |
| 15174 | |
| 15175 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15176 | if (input == NULL) |
| 15177 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15178 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15179 | ctxt = xmlNewParserCtxt(); |
| 15180 | if (ctxt == NULL) { |
| 15181 | xmlFreeParserInputBuffer(input); |
| 15182 | return (NULL); |
| 15183 | } |
| 15184 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15185 | if (stream == NULL) { |
| 15186 | xmlFreeParserInputBuffer(input); |
| 15187 | xmlFreeParserCtxt(ctxt); |
| 15188 | return (NULL); |
| 15189 | } |
| 15190 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15191 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15192 | } |
| 15193 | |
| 15194 | /** |
| 15195 | * xmlReadIO: |
| 15196 | * @ioread: an I/O read function |
| 15197 | * @ioclose: an I/O close function |
| 15198 | * @ioctx: an I/O handler |
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 | * |
| 15203 | * 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] | 15204 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15205 | * Returns the resulting document tree |
| 15206 | */ |
| 15207 | xmlDocPtr |
| 15208 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15209 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15210 | { |
| 15211 | xmlParserCtxtPtr ctxt; |
| 15212 | xmlParserInputBufferPtr input; |
| 15213 | xmlParserInputPtr stream; |
| 15214 | |
| 15215 | if (ioread == NULL) |
| 15216 | return (NULL); |
| 15217 | |
| 15218 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15219 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15220 | if (input == NULL) { |
| 15221 | if (ioclose != NULL) |
| 15222 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15223 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15224 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15225 | ctxt = xmlNewParserCtxt(); |
| 15226 | if (ctxt == NULL) { |
| 15227 | xmlFreeParserInputBuffer(input); |
| 15228 | return (NULL); |
| 15229 | } |
| 15230 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15231 | if (stream == NULL) { |
| 15232 | xmlFreeParserInputBuffer(input); |
| 15233 | xmlFreeParserCtxt(ctxt); |
| 15234 | return (NULL); |
| 15235 | } |
| 15236 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15237 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15238 | } |
| 15239 | |
| 15240 | /** |
| 15241 | * xmlCtxtReadDoc: |
| 15242 | * @ctxt: an XML parser context |
| 15243 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15244 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15245 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15246 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15247 | * |
| 15248 | * parse an XML in-memory document and build a tree. |
| 15249 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +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 |
| 15254 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15255 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15256 | { |
| 15257 | xmlParserInputPtr stream; |
| 15258 | |
| 15259 | if (cur == NULL) |
| 15260 | return (NULL); |
| 15261 | if (ctxt == NULL) |
| 15262 | return (NULL); |
| 15263 | |
| 15264 | xmlCtxtReset(ctxt); |
| 15265 | |
| 15266 | stream = xmlNewStringInputStream(ctxt, cur); |
| 15267 | if (stream == NULL) { |
| 15268 | return (NULL); |
| 15269 | } |
| 15270 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15271 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15272 | } |
| 15273 | |
| 15274 | /** |
| 15275 | * xmlCtxtReadFile: |
| 15276 | * @ctxt: an XML parser context |
| 15277 | * @filename: a file or URL |
| 15278 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15279 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15280 | * |
| 15281 | * parse an XML file from the filesystem or the network. |
| 15282 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15283 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15284 | * Returns the resulting document tree |
| 15285 | */ |
| 15286 | xmlDocPtr |
| 15287 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 15288 | const char *encoding, int options) |
| 15289 | { |
| 15290 | xmlParserInputPtr stream; |
| 15291 | |
| 15292 | if (filename == NULL) |
| 15293 | return (NULL); |
| 15294 | if (ctxt == NULL) |
| 15295 | return (NULL); |
| 15296 | |
| 15297 | xmlCtxtReset(ctxt); |
| 15298 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 15299 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15300 | if (stream == NULL) { |
| 15301 | return (NULL); |
| 15302 | } |
| 15303 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15304 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15305 | } |
| 15306 | |
| 15307 | /** |
| 15308 | * xmlCtxtReadMemory: |
| 15309 | * @ctxt: an XML parser context |
| 15310 | * @buffer: a pointer to a char array |
| 15311 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15312 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15313 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15314 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15315 | * |
| 15316 | * parse an XML in-memory document and build a tree. |
| 15317 | * This reuses the existing @ctxt parser context |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15318 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15319 | * Returns the resulting document tree |
| 15320 | */ |
| 15321 | xmlDocPtr |
| 15322 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15323 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15324 | { |
| 15325 | xmlParserInputBufferPtr input; |
| 15326 | xmlParserInputPtr stream; |
| 15327 | |
| 15328 | if (ctxt == NULL) |
| 15329 | return (NULL); |
| 15330 | if (buffer == NULL) |
| 15331 | return (NULL); |
| 15332 | |
| 15333 | xmlCtxtReset(ctxt); |
| 15334 | |
| 15335 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 15336 | if (input == NULL) { |
| 15337 | return(NULL); |
| 15338 | } |
| 15339 | |
| 15340 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15341 | if (stream == NULL) { |
| 15342 | xmlFreeParserInputBuffer(input); |
| 15343 | return(NULL); |
| 15344 | } |
| 15345 | |
| 15346 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15347 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15348 | } |
| 15349 | |
| 15350 | /** |
| 15351 | * xmlCtxtReadFd: |
| 15352 | * @ctxt: an XML parser context |
| 15353 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15354 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15355 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15356 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15357 | * |
| 15358 | * parse an XML from a file descriptor and build a tree. |
| 15359 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15360 | * NOTE that the file descriptor will not be closed when the |
| 15361 | * reader is closed or reset. |
Daniel Veillard | f8e3db0 | 2012-09-11 13:26:36 +0800 | [diff] [blame] | 15362 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15363 | * Returns the resulting document tree |
| 15364 | */ |
| 15365 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15366 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 15367 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15368 | { |
| 15369 | xmlParserInputBufferPtr input; |
| 15370 | xmlParserInputPtr stream; |
| 15371 | |
| 15372 | if (fd < 0) |
| 15373 | return (NULL); |
| 15374 | if (ctxt == NULL) |
| 15375 | return (NULL); |
| 15376 | |
| 15377 | xmlCtxtReset(ctxt); |
| 15378 | |
| 15379 | |
| 15380 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 15381 | if (input == NULL) |
| 15382 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 15383 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15384 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15385 | if (stream == NULL) { |
| 15386 | xmlFreeParserInputBuffer(input); |
| 15387 | return (NULL); |
| 15388 | } |
| 15389 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15390 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15391 | } |
| 15392 | |
| 15393 | /** |
| 15394 | * xmlCtxtReadIO: |
| 15395 | * @ctxt: an XML parser context |
| 15396 | * @ioread: an I/O read function |
| 15397 | * @ioclose: an I/O close function |
| 15398 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15399 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15400 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15401 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15402 | * |
| 15403 | * parse an XML document from I/O functions and source and build a tree. |
| 15404 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15405 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15406 | * Returns the resulting document tree |
| 15407 | */ |
| 15408 | xmlDocPtr |
| 15409 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 15410 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15411 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15412 | const char *encoding, int options) |
| 15413 | { |
| 15414 | xmlParserInputBufferPtr input; |
| 15415 | xmlParserInputPtr stream; |
| 15416 | |
| 15417 | if (ioread == NULL) |
| 15418 | return (NULL); |
| 15419 | if (ctxt == NULL) |
| 15420 | return (NULL); |
| 15421 | |
| 15422 | xmlCtxtReset(ctxt); |
| 15423 | |
| 15424 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15425 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15426 | if (input == NULL) { |
| 15427 | if (ioclose != NULL) |
| 15428 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15429 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15430 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15431 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15432 | if (stream == NULL) { |
| 15433 | xmlFreeParserInputBuffer(input); |
| 15434 | return (NULL); |
| 15435 | } |
| 15436 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15437 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15438 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15439 | |
| 15440 | #define bottom_parser |
| 15441 | #include "elfgcchack.h" |