Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * parser.c : an XML 1.0 parser, namespaces and validity support are mostly |
| 3 | * implemented on top of the SAX interfaces |
| 4 | * |
| 5 | * References: |
| 6 | * The XML specification: |
| 7 | * http://www.w3.org/TR/REC-xml |
| 8 | * Original 1.0 version: |
| 9 | * http://www.w3.org/TR/1998/REC-xml-19980210 |
| 10 | * XML second edition working draft |
| 11 | * http://www.w3.org/TR/2000/WD-xml-2e-20000814 |
| 12 | * |
| 13 | * Okay this is a big file, the parser core is around 7000 lines, then it |
| 14 | * is followed by the progressive parser top routines, then the various |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 15 | * high level APIs to call the parser and a few miscellaneous functions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | * A number of helper functions and deprecated ones have been moved to |
| 17 | * parserInternals.c to reduce this file size. |
| 18 | * As much as possible the functions are associated with their relative |
| 19 | * production in the XML specification. A few productions defining the |
| 20 | * different ranges of character are actually implanted either in |
| 21 | * parserInternals.h or parserInternals.c |
| 22 | * The DOM tree build is realized from the default SAX callbacks in |
| 23 | * the module SAX.c. |
| 24 | * The routines doing the validation checks are in valid.c and called either |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 25 | * from the SAX callbacks or as standalone functions using a preparsed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 26 | * document. |
| 27 | * |
| 28 | * See Copyright for the status of this software. |
| 29 | * |
Daniel Veillard | c5d6434 | 2001-06-24 12:13:24 +0000 | [diff] [blame] | 30 | * daniel@veillard.com |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 33 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 34 | #include "libxml.h" |
| 35 | |
Daniel Veillard | 3c5ed91 | 2002-01-08 10:36:16 +0000 | [diff] [blame] | 36 | #if defined(WIN32) && !defined (__CYGWIN__) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 37 | #define XML_DIR_SEP '\\' |
| 38 | #else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 39 | #define XML_DIR_SEP '/' |
| 40 | #endif |
| 41 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 42 | #include <stdlib.h> |
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 | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 87 | static void |
| 88 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info); |
| 89 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 90 | static xmlParserCtxtPtr |
| 91 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 92 | const xmlChar *base, xmlParserCtxtPtr pctx); |
| 93 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 94 | /************************************************************************ |
| 95 | * * |
| 96 | * Arbitrary limits set in the parser. See XML_PARSE_HUGE * |
| 97 | * * |
| 98 | ************************************************************************/ |
| 99 | |
| 100 | #define XML_PARSER_BIG_ENTITY 1000 |
| 101 | #define XML_PARSER_LOT_ENTITY 5000 |
| 102 | |
| 103 | /* |
| 104 | * XML_PARSER_NON_LINEAR is the threshold where the ratio of parsed entity |
| 105 | * replacement over the size in byte of the input indicates that you have |
| 106 | * and eponential behaviour. A value of 10 correspond to at least 3 entity |
| 107 | * replacement per byte of input. |
| 108 | */ |
| 109 | #define XML_PARSER_NON_LINEAR 10 |
| 110 | |
| 111 | /* |
| 112 | * xmlParserEntityCheck |
| 113 | * |
| 114 | * Function to check non-linear entity expansion behaviour |
| 115 | * This is here to detect and stop exponential linear entity expansion |
| 116 | * This is not a limitation of the parser but a safety |
| 117 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 118 | * parser option. |
| 119 | */ |
| 120 | static int |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 121 | xmlParserEntityCheck(xmlParserCtxtPtr ctxt, size_t size, |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 122 | xmlEntityPtr ent) |
| 123 | { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 124 | size_t consumed = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 125 | |
| 126 | if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE)) |
| 127 | return (0); |
| 128 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 129 | return (1); |
| 130 | if (size != 0) { |
| 131 | /* |
| 132 | * Do the check based on the replacement size of the entity |
| 133 | */ |
| 134 | if (size < XML_PARSER_BIG_ENTITY) |
| 135 | return(0); |
| 136 | |
| 137 | /* |
| 138 | * A limit on the amount of text data reasonably used |
| 139 | */ |
| 140 | if (ctxt->input != NULL) { |
| 141 | consumed = ctxt->input->consumed + |
| 142 | (ctxt->input->cur - ctxt->input->base); |
| 143 | } |
| 144 | consumed += ctxt->sizeentities; |
| 145 | |
| 146 | if ((size < XML_PARSER_NON_LINEAR * consumed) && |
| 147 | (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed)) |
| 148 | return (0); |
| 149 | } else if (ent != NULL) { |
| 150 | /* |
| 151 | * use the number of parsed entities in the replacement |
| 152 | */ |
| 153 | size = ent->checked; |
| 154 | |
| 155 | /* |
| 156 | * The amount of data parsed counting entities size only once |
| 157 | */ |
| 158 | if (ctxt->input != NULL) { |
| 159 | consumed = ctxt->input->consumed + |
| 160 | (ctxt->input->cur - ctxt->input->base); |
| 161 | } |
| 162 | consumed += ctxt->sizeentities; |
| 163 | |
| 164 | /* |
| 165 | * Check the density of entities for the amount of data |
| 166 | * knowing an entity reference will take at least 3 bytes |
| 167 | */ |
| 168 | if (size * 3 < consumed * XML_PARSER_NON_LINEAR) |
| 169 | return (0); |
| 170 | } else { |
| 171 | /* |
| 172 | * strange we got no data for checking just return |
| 173 | */ |
| 174 | return (0); |
| 175 | } |
| 176 | |
| 177 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 178 | return (1); |
| 179 | } |
| 180 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 181 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 182 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 183 | * |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 184 | * arbitrary depth limit for the XML documents that we allow to |
| 185 | * process. This is not a limitation of the parser but a safety |
| 186 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 187 | * parser option. |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 188 | */ |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 189 | unsigned int xmlParserMaxDepth = 256; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 190 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 191 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 192 | |
| 193 | #define SAX2 1 |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 194 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 195 | #define XML_PARSER_BUFFER_SIZE 100 |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 196 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 197 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 198 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 199 | * List of XML prefixed PI allowed by W3C specs |
| 200 | */ |
| 201 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 202 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 203 | "xml-stylesheet", |
Daniel Veillard | 4c4653e | 2011-06-05 11:29:29 +0800 | [diff] [blame] | 204 | "xml-model", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 205 | NULL |
| 206 | }; |
| 207 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 208 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 209 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 210 | static xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 211 | const xmlChar **str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 212 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 213 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 214 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 215 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 216 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 217 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 218 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 219 | static int |
| 220 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, |
| 221 | const char *encoding); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 222 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 223 | static void |
| 224 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 225 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 226 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 227 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 228 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 229 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 230 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 231 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 232 | static int |
| 233 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity); |
| 234 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 235 | /************************************************************************ |
| 236 | * * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 237 | * Some factorized error routines * |
| 238 | * * |
| 239 | ************************************************************************/ |
| 240 | |
| 241 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 242 | * xmlErrAttributeDup: |
| 243 | * @ctxt: an XML parser context |
| 244 | * @prefix: the attribute prefix |
| 245 | * @localname: the attribute localname |
| 246 | * |
| 247 | * Handle a redefinition of attribute error |
| 248 | */ |
| 249 | static void |
| 250 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 251 | const xmlChar * localname) |
| 252 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 253 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 254 | (ctxt->instate == XML_PARSER_EOF)) |
| 255 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 256 | if (ctxt != NULL) |
| 257 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 258 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 259 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 260 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 261 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 262 | (const char *) localname, NULL, NULL, 0, 0, |
| 263 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 264 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 265 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 266 | XML_ERR_ATTRIBUTE_REDEFINED, XML_ERR_FATAL, NULL, 0, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 267 | (const char *) prefix, (const char *) localname, |
| 268 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 269 | localname); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 270 | if (ctxt != NULL) { |
| 271 | ctxt->wellFormed = 0; |
| 272 | if (ctxt->recovery == 0) |
| 273 | ctxt->disableSAX = 1; |
| 274 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * xmlFatalErr: |
| 279 | * @ctxt: an XML parser context |
| 280 | * @error: the error number |
| 281 | * @extra: extra information string |
| 282 | * |
| 283 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 284 | */ |
| 285 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 286 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 287 | { |
| 288 | const char *errmsg; |
| 289 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 290 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 291 | (ctxt->instate == XML_PARSER_EOF)) |
| 292 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 293 | switch (error) { |
| 294 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 295 | errmsg = "CharRef: invalid hexadecimal value\n"; |
| 296 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 297 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 298 | errmsg = "CharRef: invalid decimal value\n"; |
| 299 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 300 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 301 | errmsg = "CharRef: invalid value\n"; |
| 302 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 303 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 304 | errmsg = "internal error"; |
| 305 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 306 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 307 | errmsg = "PEReference at end of document\n"; |
| 308 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 309 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 310 | errmsg = "PEReference in prolog\n"; |
| 311 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 312 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 313 | errmsg = "PEReference in epilog\n"; |
| 314 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 315 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 316 | errmsg = "PEReference: no name\n"; |
| 317 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 318 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 319 | errmsg = "PEReference: expecting ';'\n"; |
| 320 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 321 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 322 | errmsg = "Detected an entity reference loop\n"; |
| 323 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 324 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 325 | errmsg = "EntityValue: \" or ' expected\n"; |
| 326 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 327 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 328 | errmsg = "PEReferences forbidden in internal subset\n"; |
| 329 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 330 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 331 | errmsg = "EntityValue: \" or ' expected\n"; |
| 332 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 333 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 334 | errmsg = "AttValue: \" or ' expected\n"; |
| 335 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 336 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 337 | errmsg = "Unescaped '<' not allowed in attributes values\n"; |
| 338 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 339 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 340 | errmsg = "SystemLiteral \" or ' expected\n"; |
| 341 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 342 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 343 | errmsg = "Unfinished System or Public ID \" or ' expected\n"; |
| 344 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 345 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 346 | errmsg = "Sequence ']]>' not allowed in content\n"; |
| 347 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 348 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 349 | errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; |
| 350 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 351 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 352 | errmsg = "PUBLIC, the Public Identifier is missing\n"; |
| 353 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 354 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 355 | errmsg = "Comment must not contain '--' (double-hyphen)\n"; |
| 356 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 357 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 358 | errmsg = "xmlParsePI : no target name\n"; |
| 359 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 360 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 361 | errmsg = "Invalid PI name\n"; |
| 362 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 363 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 364 | errmsg = "NOTATION: Name expected here\n"; |
| 365 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 366 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 367 | errmsg = "'>' required to close NOTATION declaration\n"; |
| 368 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 369 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 370 | errmsg = "Entity value required\n"; |
| 371 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 372 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 373 | errmsg = "Fragment not allowed"; |
| 374 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 375 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 376 | errmsg = "'(' required to start ATTLIST enumeration\n"; |
| 377 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 378 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 379 | errmsg = "NmToken expected in ATTLIST enumeration\n"; |
| 380 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 381 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 382 | errmsg = "')' required to finish ATTLIST enumeration\n"; |
| 383 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 384 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 385 | errmsg = "MixedContentDecl : '|' or ')*' expected\n"; |
| 386 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 387 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 388 | errmsg = "MixedContentDecl : '#PCDATA' expected\n"; |
| 389 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 390 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 391 | errmsg = "ContentDecl : Name or '(' expected\n"; |
| 392 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 393 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 394 | errmsg = "ContentDecl : ',' '|' or ')' expected\n"; |
| 395 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 396 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 397 | errmsg = |
| 398 | "PEReference: forbidden within markup decl in internal subset\n"; |
| 399 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 400 | case XML_ERR_GT_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 401 | errmsg = "expected '>'\n"; |
| 402 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 403 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 404 | errmsg = "XML conditional section '[' expected\n"; |
| 405 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 406 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 407 | errmsg = "Content error in the external subset\n"; |
| 408 | break; |
| 409 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 410 | errmsg = |
| 411 | "conditional section INCLUDE or IGNORE keyword expected\n"; |
| 412 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 413 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 414 | errmsg = "XML conditional section not closed\n"; |
| 415 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 416 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 417 | errmsg = "Text declaration '<?xml' required\n"; |
| 418 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 419 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 420 | errmsg = "parsing XML declaration: '?>' expected\n"; |
| 421 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 422 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 423 | errmsg = "external parsed entities cannot be standalone\n"; |
| 424 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 425 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 426 | errmsg = "EntityRef: expecting ';'\n"; |
| 427 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 428 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 429 | errmsg = "DOCTYPE improperly terminated\n"; |
| 430 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 431 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 432 | errmsg = "EndTag: '</' not found\n"; |
| 433 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 434 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 435 | errmsg = "expected '='\n"; |
| 436 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 437 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 438 | errmsg = "String not closed expecting \" or '\n"; |
| 439 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 440 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 441 | errmsg = "String not started expecting ' or \"\n"; |
| 442 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 443 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 444 | errmsg = "Invalid XML encoding name\n"; |
| 445 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 446 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 447 | errmsg = "standalone accepts only 'yes' or 'no'\n"; |
| 448 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 449 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 450 | errmsg = "Document is empty\n"; |
| 451 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 452 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 453 | errmsg = "Extra content at the end of the document\n"; |
| 454 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 455 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 456 | errmsg = "chunk is not well balanced\n"; |
| 457 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 458 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 459 | errmsg = "extra content at the end of well balanced chunk\n"; |
| 460 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 461 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 462 | errmsg = "Malformed declaration expecting version\n"; |
| 463 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 464 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 465 | case: |
| 466 | errmsg = "\n"; |
| 467 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 468 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 469 | default: |
| 470 | errmsg = "Unregistered error message\n"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 471 | } |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 472 | if (ctxt != NULL) |
| 473 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 474 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 475 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg, |
| 476 | info); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 477 | if (ctxt != NULL) { |
| 478 | ctxt->wellFormed = 0; |
| 479 | if (ctxt->recovery == 0) |
| 480 | ctxt->disableSAX = 1; |
| 481 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 484 | /** |
| 485 | * xmlFatalErrMsg: |
| 486 | * @ctxt: an XML parser context |
| 487 | * @error: the error number |
| 488 | * @msg: the error message |
| 489 | * |
| 490 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 491 | */ |
| 492 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 493 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 494 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 495 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 496 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 497 | (ctxt->instate == XML_PARSER_EOF)) |
| 498 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 499 | if (ctxt != NULL) |
| 500 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 501 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bccae2d | 2009-06-04 11:22:45 +0200 | [diff] [blame] | 502 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, "%s", msg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 503 | if (ctxt != NULL) { |
| 504 | ctxt->wellFormed = 0; |
| 505 | if (ctxt->recovery == 0) |
| 506 | ctxt->disableSAX = 1; |
| 507 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 511 | * xmlWarningMsg: |
| 512 | * @ctxt: an XML parser context |
| 513 | * @error: the error number |
| 514 | * @msg: the error message |
| 515 | * @str1: extra data |
| 516 | * @str2: extra data |
| 517 | * |
| 518 | * Handle a warning. |
| 519 | */ |
| 520 | static void |
| 521 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 522 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 523 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 524 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 525 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 526 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 527 | (ctxt->instate == XML_PARSER_EOF)) |
| 528 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 529 | if ((ctxt != NULL) && (ctxt->sax != NULL) && |
| 530 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 531 | schannel = ctxt->sax->serror; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 532 | if (ctxt != NULL) { |
| 533 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 534 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 535 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 536 | ctxt, NULL, XML_FROM_PARSER, error, |
| 537 | XML_ERR_WARNING, NULL, 0, |
| 538 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 539 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 540 | } else { |
| 541 | __xmlRaiseError(schannel, NULL, NULL, |
| 542 | ctxt, NULL, XML_FROM_PARSER, error, |
| 543 | XML_ERR_WARNING, NULL, 0, |
| 544 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 545 | msg, (const char *) str1, (const char *) str2); |
| 546 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** |
| 550 | * xmlValidityError: |
| 551 | * @ctxt: an XML parser context |
| 552 | * @error: the error number |
| 553 | * @msg: the error message |
| 554 | * @str1: extra data |
| 555 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 556 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 557 | */ |
| 558 | static void |
| 559 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 560 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 561 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 562 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 563 | |
| 564 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 565 | (ctxt->instate == XML_PARSER_EOF)) |
| 566 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 567 | if (ctxt != NULL) { |
| 568 | ctxt->errNo = error; |
| 569 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 570 | schannel = ctxt->sax->serror; |
| 571 | } |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 572 | if (ctxt != NULL) { |
| 573 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 574 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 575 | ctxt, NULL, XML_FROM_DTD, error, |
| 576 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 577 | (const char *) str2, NULL, 0, 0, |
| 578 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 579 | ctxt->valid = 0; |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 580 | } else { |
| 581 | __xmlRaiseError(schannel, NULL, NULL, |
| 582 | ctxt, NULL, XML_FROM_DTD, error, |
| 583 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 584 | (const char *) str2, NULL, 0, 0, |
| 585 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 586 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 590 | * xmlFatalErrMsgInt: |
| 591 | * @ctxt: an XML parser context |
| 592 | * @error: the error number |
| 593 | * @msg: the error message |
| 594 | * @val: an integer value |
| 595 | * |
| 596 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 597 | */ |
| 598 | static void |
| 599 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 600 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 601 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 602 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 603 | (ctxt->instate == XML_PARSER_EOF)) |
| 604 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 605 | if (ctxt != NULL) |
| 606 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 607 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 608 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 609 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 610 | if (ctxt != NULL) { |
| 611 | ctxt->wellFormed = 0; |
| 612 | if (ctxt->recovery == 0) |
| 613 | ctxt->disableSAX = 1; |
| 614 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 618 | * xmlFatalErrMsgStrIntStr: |
| 619 | * @ctxt: an XML parser context |
| 620 | * @error: the error number |
| 621 | * @msg: the error message |
| 622 | * @str1: an string info |
| 623 | * @val: an integer value |
| 624 | * @str2: an string info |
| 625 | * |
| 626 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 627 | */ |
| 628 | static void |
| 629 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 630 | const char *msg, const xmlChar *str1, int val, |
| 631 | const xmlChar *str2) |
| 632 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 633 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 634 | (ctxt->instate == XML_PARSER_EOF)) |
| 635 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 636 | if (ctxt != NULL) |
| 637 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 638 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 639 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 640 | NULL, 0, (const char *) str1, (const char *) str2, |
| 641 | NULL, val, 0, msg, str1, val, str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 642 | if (ctxt != NULL) { |
| 643 | ctxt->wellFormed = 0; |
| 644 | if (ctxt->recovery == 0) |
| 645 | ctxt->disableSAX = 1; |
| 646 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 650 | * xmlFatalErrMsgStr: |
| 651 | * @ctxt: an XML parser context |
| 652 | * @error: the error number |
| 653 | * @msg: the error message |
| 654 | * @val: a string value |
| 655 | * |
| 656 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 657 | */ |
| 658 | static void |
| 659 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 660 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 661 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 662 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 663 | (ctxt->instate == XML_PARSER_EOF)) |
| 664 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 665 | if (ctxt != NULL) |
| 666 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 667 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 668 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 669 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 670 | val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 671 | if (ctxt != NULL) { |
| 672 | ctxt->wellFormed = 0; |
| 673 | if (ctxt->recovery == 0) |
| 674 | ctxt->disableSAX = 1; |
| 675 | } |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 679 | * xmlErrMsgStr: |
| 680 | * @ctxt: an XML parser context |
| 681 | * @error: the error number |
| 682 | * @msg: the error message |
| 683 | * @val: a string value |
| 684 | * |
| 685 | * Handle a non fatal parser error |
| 686 | */ |
| 687 | static void |
| 688 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 689 | const char *msg, const xmlChar * val) |
| 690 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 691 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 692 | (ctxt->instate == XML_PARSER_EOF)) |
| 693 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 694 | if (ctxt != NULL) |
| 695 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 696 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 697 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 698 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 699 | val); |
| 700 | } |
| 701 | |
| 702 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 703 | * xmlNsErr: |
| 704 | * @ctxt: an XML parser context |
| 705 | * @error: the error number |
| 706 | * @msg: the message |
| 707 | * @info1: extra information string |
| 708 | * @info2: extra information string |
| 709 | * |
| 710 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 711 | */ |
| 712 | static void |
| 713 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 714 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 715 | const xmlChar * info1, const xmlChar * info2, |
| 716 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 717 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 718 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 719 | (ctxt->instate == XML_PARSER_EOF)) |
| 720 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 721 | if (ctxt != NULL) |
| 722 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 723 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 724 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 725 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 726 | info1, info2, info3); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 727 | if (ctxt != NULL) |
| 728 | ctxt->nsWellFormed = 0; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 731 | /** |
| 732 | * xmlNsWarn |
| 733 | * @ctxt: an XML parser context |
| 734 | * @error: the error number |
| 735 | * @msg: the message |
| 736 | * @info1: extra information string |
| 737 | * @info2: extra information string |
| 738 | * |
Daniel Veillard | 288bb62 | 2012-05-07 15:01:29 +0800 | [diff] [blame] | 739 | * Handle a namespace warning error |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 740 | */ |
| 741 | static void |
| 742 | xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 743 | const char *msg, |
| 744 | const xmlChar * info1, const xmlChar * info2, |
| 745 | const xmlChar * info3) |
| 746 | { |
| 747 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 748 | (ctxt->instate == XML_PARSER_EOF)) |
| 749 | return; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 750 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
| 751 | XML_ERR_WARNING, NULL, 0, (const char *) info1, |
| 752 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 753 | info1, info2, info3); |
| 754 | } |
| 755 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 756 | /************************************************************************ |
| 757 | * * |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 758 | * Library wide options * |
| 759 | * * |
| 760 | ************************************************************************/ |
| 761 | |
| 762 | /** |
| 763 | * xmlHasFeature: |
| 764 | * @feature: the feature to be examined |
| 765 | * |
| 766 | * Examines if the library has been compiled with a given feature. |
| 767 | * |
| 768 | * Returns a non-zero value if the feature exist, otherwise zero. |
| 769 | * Returns zero (0) if the feature does not exist or an unknown |
| 770 | * unknown feature is requested, non-zero otherwise. |
| 771 | */ |
| 772 | int |
| 773 | xmlHasFeature(xmlFeature feature) |
| 774 | { |
| 775 | switch (feature) { |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 776 | case XML_WITH_THREAD: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 777 | #ifdef LIBXML_THREAD_ENABLED |
| 778 | return(1); |
| 779 | #else |
| 780 | return(0); |
| 781 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 782 | case XML_WITH_TREE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 783 | #ifdef LIBXML_TREE_ENABLED |
| 784 | return(1); |
| 785 | #else |
| 786 | return(0); |
| 787 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 788 | case XML_WITH_OUTPUT: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 789 | #ifdef LIBXML_OUTPUT_ENABLED |
| 790 | return(1); |
| 791 | #else |
| 792 | return(0); |
| 793 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 794 | case XML_WITH_PUSH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 795 | #ifdef LIBXML_PUSH_ENABLED |
| 796 | return(1); |
| 797 | #else |
| 798 | return(0); |
| 799 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 800 | case XML_WITH_READER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 801 | #ifdef LIBXML_READER_ENABLED |
| 802 | return(1); |
| 803 | #else |
| 804 | return(0); |
| 805 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 806 | case XML_WITH_PATTERN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 807 | #ifdef LIBXML_PATTERN_ENABLED |
| 808 | return(1); |
| 809 | #else |
| 810 | return(0); |
| 811 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 812 | case XML_WITH_WRITER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 813 | #ifdef LIBXML_WRITER_ENABLED |
| 814 | return(1); |
| 815 | #else |
| 816 | return(0); |
| 817 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 818 | case XML_WITH_SAX1: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 819 | #ifdef LIBXML_SAX1_ENABLED |
| 820 | return(1); |
| 821 | #else |
| 822 | return(0); |
| 823 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 824 | case XML_WITH_FTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 825 | #ifdef LIBXML_FTP_ENABLED |
| 826 | return(1); |
| 827 | #else |
| 828 | return(0); |
| 829 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 830 | case XML_WITH_HTTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 831 | #ifdef LIBXML_HTTP_ENABLED |
| 832 | return(1); |
| 833 | #else |
| 834 | return(0); |
| 835 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 836 | case XML_WITH_VALID: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 837 | #ifdef LIBXML_VALID_ENABLED |
| 838 | return(1); |
| 839 | #else |
| 840 | return(0); |
| 841 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 842 | case XML_WITH_HTML: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 843 | #ifdef LIBXML_HTML_ENABLED |
| 844 | return(1); |
| 845 | #else |
| 846 | return(0); |
| 847 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 848 | case XML_WITH_LEGACY: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 849 | #ifdef LIBXML_LEGACY_ENABLED |
| 850 | return(1); |
| 851 | #else |
| 852 | return(0); |
| 853 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 854 | case XML_WITH_C14N: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 855 | #ifdef LIBXML_C14N_ENABLED |
| 856 | return(1); |
| 857 | #else |
| 858 | return(0); |
| 859 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 860 | case XML_WITH_CATALOG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 861 | #ifdef LIBXML_CATALOG_ENABLED |
| 862 | return(1); |
| 863 | #else |
| 864 | return(0); |
| 865 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 866 | case XML_WITH_XPATH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 867 | #ifdef LIBXML_XPATH_ENABLED |
| 868 | return(1); |
| 869 | #else |
| 870 | return(0); |
| 871 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 872 | case XML_WITH_XPTR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 873 | #ifdef LIBXML_XPTR_ENABLED |
| 874 | return(1); |
| 875 | #else |
| 876 | return(0); |
| 877 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 878 | case XML_WITH_XINCLUDE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 879 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 880 | return(1); |
| 881 | #else |
| 882 | return(0); |
| 883 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 884 | case XML_WITH_ICONV: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 885 | #ifdef LIBXML_ICONV_ENABLED |
| 886 | return(1); |
| 887 | #else |
| 888 | return(0); |
| 889 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 890 | case XML_WITH_ISO8859X: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 891 | #ifdef LIBXML_ISO8859X_ENABLED |
| 892 | return(1); |
| 893 | #else |
| 894 | return(0); |
| 895 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 896 | case XML_WITH_UNICODE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 897 | #ifdef LIBXML_UNICODE_ENABLED |
| 898 | return(1); |
| 899 | #else |
| 900 | return(0); |
| 901 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 902 | case XML_WITH_REGEXP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 903 | #ifdef LIBXML_REGEXP_ENABLED |
| 904 | return(1); |
| 905 | #else |
| 906 | return(0); |
| 907 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 908 | case XML_WITH_AUTOMATA: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 909 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 910 | return(1); |
| 911 | #else |
| 912 | return(0); |
| 913 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 914 | case XML_WITH_EXPR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 915 | #ifdef LIBXML_EXPR_ENABLED |
| 916 | return(1); |
| 917 | #else |
| 918 | return(0); |
| 919 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 920 | case XML_WITH_SCHEMAS: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 921 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 922 | return(1); |
| 923 | #else |
| 924 | return(0); |
| 925 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 926 | case XML_WITH_SCHEMATRON: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 927 | #ifdef LIBXML_SCHEMATRON_ENABLED |
| 928 | return(1); |
| 929 | #else |
| 930 | return(0); |
| 931 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 932 | case XML_WITH_MODULES: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 933 | #ifdef LIBXML_MODULES_ENABLED |
| 934 | return(1); |
| 935 | #else |
| 936 | return(0); |
| 937 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 938 | case XML_WITH_DEBUG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 939 | #ifdef LIBXML_DEBUG_ENABLED |
| 940 | return(1); |
| 941 | #else |
| 942 | return(0); |
| 943 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 944 | case XML_WITH_DEBUG_MEM: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 945 | #ifdef DEBUG_MEMORY_LOCATION |
| 946 | return(1); |
| 947 | #else |
| 948 | return(0); |
| 949 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 950 | case XML_WITH_DEBUG_RUN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 951 | #ifdef LIBXML_DEBUG_RUNTIME |
| 952 | return(1); |
| 953 | #else |
| 954 | return(0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 955 | #endif |
Daniel Veillard | 75acfee | 2006-07-13 06:29:56 +0000 | [diff] [blame] | 956 | case XML_WITH_ZLIB: |
| 957 | #ifdef LIBXML_ZLIB_ENABLED |
| 958 | return(1); |
| 959 | #else |
| 960 | return(0); |
| 961 | #endif |
Anders F Bjorklund | eae5261 | 2011-09-18 16:59:13 +0200 | [diff] [blame] | 962 | case XML_WITH_LZMA: |
| 963 | #ifdef LIBXML_LZMA_ENABLED |
| 964 | return(1); |
| 965 | #else |
| 966 | return(0); |
| 967 | #endif |
Giuseppe Iuculano | 48f7dcb | 2010-11-04 17:42:42 +0100 | [diff] [blame] | 968 | case XML_WITH_ICU: |
| 969 | #ifdef LIBXML_ICU_ENABLED |
| 970 | return(1); |
| 971 | #else |
| 972 | return(0); |
| 973 | #endif |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 974 | default: |
| 975 | break; |
| 976 | } |
| 977 | return(0); |
| 978 | } |
| 979 | |
| 980 | /************************************************************************ |
| 981 | * * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 982 | * SAX2 defaulted attributes handling * |
| 983 | * * |
| 984 | ************************************************************************/ |
| 985 | |
| 986 | /** |
| 987 | * xmlDetectSAX2: |
| 988 | * @ctxt: an XML parser context |
| 989 | * |
| 990 | * Do the SAX2 detection and specific intialization |
| 991 | */ |
| 992 | static void |
| 993 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 994 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 995 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 996 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 997 | ((ctxt->sax->startElementNs != NULL) || |
| 998 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 999 | #else |
| 1000 | ctxt->sax2 = 1; |
| 1001 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1002 | |
| 1003 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 1004 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 1005 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 1006 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 1007 | (ctxt->str_xml_ns == NULL)) { |
| 1008 | xmlErrMemory(ctxt, NULL); |
| 1009 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1012 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 1013 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 1014 | struct _xmlDefAttrs { |
| 1015 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 1016 | int maxAttrs; /* the size of the array */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1017 | const xmlChar *values[5]; /* array of localname/prefix/values/external */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1018 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1019 | |
| 1020 | /** |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1021 | * xmlAttrNormalizeSpace: |
| 1022 | * @src: the source string |
| 1023 | * @dst: the target string |
| 1024 | * |
| 1025 | * Normalize the space in non CDATA attribute values: |
| 1026 | * If the attribute type is not CDATA, then the XML processor MUST further |
| 1027 | * process the normalized attribute value by discarding any leading and |
| 1028 | * trailing space (#x20) characters, and by replacing sequences of space |
| 1029 | * (#x20) characters by a single space (#x20) character. |
| 1030 | * Note that the size of dst need to be at least src, and if one doesn't need |
| 1031 | * to preserve dst (and it doesn't come from a dictionary or read-only) then |
| 1032 | * passing src as dst is just fine. |
| 1033 | * |
| 1034 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1035 | * is needed. |
| 1036 | */ |
| 1037 | static xmlChar * |
| 1038 | xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst) |
| 1039 | { |
| 1040 | if ((src == NULL) || (dst == NULL)) |
| 1041 | return(NULL); |
| 1042 | |
| 1043 | while (*src == 0x20) src++; |
| 1044 | while (*src != 0) { |
| 1045 | if (*src == 0x20) { |
| 1046 | while (*src == 0x20) src++; |
| 1047 | if (*src != 0) |
| 1048 | *dst++ = 0x20; |
| 1049 | } else { |
| 1050 | *dst++ = *src++; |
| 1051 | } |
| 1052 | } |
| 1053 | *dst = 0; |
| 1054 | if (dst == src) |
| 1055 | return(NULL); |
| 1056 | return(dst); |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * xmlAttrNormalizeSpace2: |
| 1061 | * @src: the source string |
| 1062 | * |
| 1063 | * Normalize the space in non CDATA attribute values, a slightly more complex |
| 1064 | * front end to avoid allocation problems when running on attribute values |
| 1065 | * coming from the input. |
| 1066 | * |
| 1067 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1068 | * is needed. |
| 1069 | */ |
| 1070 | static const xmlChar * |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1071 | xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len) |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1072 | { |
| 1073 | int i; |
| 1074 | int remove_head = 0; |
| 1075 | int need_realloc = 0; |
| 1076 | const xmlChar *cur; |
| 1077 | |
| 1078 | if ((ctxt == NULL) || (src == NULL) || (len == NULL)) |
| 1079 | return(NULL); |
| 1080 | i = *len; |
| 1081 | if (i <= 0) |
| 1082 | return(NULL); |
| 1083 | |
| 1084 | cur = src; |
| 1085 | while (*cur == 0x20) { |
| 1086 | cur++; |
| 1087 | remove_head++; |
| 1088 | } |
| 1089 | while (*cur != 0) { |
| 1090 | if (*cur == 0x20) { |
| 1091 | cur++; |
| 1092 | if ((*cur == 0x20) || (*cur == 0)) { |
| 1093 | need_realloc = 1; |
| 1094 | break; |
| 1095 | } |
| 1096 | } else |
| 1097 | cur++; |
| 1098 | } |
| 1099 | if (need_realloc) { |
| 1100 | xmlChar *ret; |
| 1101 | |
| 1102 | ret = xmlStrndup(src + remove_head, i - remove_head + 1); |
| 1103 | if (ret == NULL) { |
| 1104 | xmlErrMemory(ctxt, NULL); |
| 1105 | return(NULL); |
| 1106 | } |
| 1107 | xmlAttrNormalizeSpace(ret, ret); |
| 1108 | *len = (int) strlen((const char *)ret); |
| 1109 | return(ret); |
| 1110 | } else if (remove_head) { |
| 1111 | *len -= remove_head; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1112 | memmove(src, src + remove_head, 1 + *len); |
| 1113 | return(src); |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1114 | } |
| 1115 | return(NULL); |
| 1116 | } |
| 1117 | |
| 1118 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1119 | * xmlAddDefAttrs: |
| 1120 | * @ctxt: an XML parser context |
| 1121 | * @fullname: the element fullname |
| 1122 | * @fullattr: the attribute fullname |
| 1123 | * @value: the attribute value |
| 1124 | * |
| 1125 | * Add a defaulted attribute for an element |
| 1126 | */ |
| 1127 | static void |
| 1128 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 1129 | const xmlChar *fullname, |
| 1130 | const xmlChar *fullattr, |
| 1131 | const xmlChar *value) { |
| 1132 | xmlDefAttrsPtr defaults; |
| 1133 | int len; |
| 1134 | const xmlChar *name; |
| 1135 | const xmlChar *prefix; |
| 1136 | |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 1137 | /* |
| 1138 | * Allows to detect attribute redefinitions |
| 1139 | */ |
| 1140 | if (ctxt->attsSpecial != NULL) { |
| 1141 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1142 | return; |
| 1143 | } |
| 1144 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1145 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1146 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1147 | if (ctxt->attsDefault == NULL) |
| 1148 | goto mem_error; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1152 | * split the element name into prefix:localname , the string found |
| 1153 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1154 | */ |
| 1155 | name = xmlSplitQName3(fullname, &len); |
| 1156 | if (name == NULL) { |
| 1157 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 1158 | prefix = NULL; |
| 1159 | } else { |
| 1160 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1161 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * make sure there is some storage |
| 1166 | */ |
| 1167 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 1168 | if (defaults == NULL) { |
| 1169 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1170 | (4 * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1171 | if (defaults == NULL) |
| 1172 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1173 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1174 | defaults->maxAttrs = 4; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1175 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1176 | defaults, NULL) < 0) { |
| 1177 | xmlFree(defaults); |
| 1178 | goto mem_error; |
| 1179 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1180 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1181 | xmlDefAttrsPtr temp; |
| 1182 | |
| 1183 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1184 | (2 * defaults->maxAttrs * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1185 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1186 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1187 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1188 | defaults->maxAttrs *= 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1189 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1190 | defaults, NULL) < 0) { |
| 1191 | xmlFree(defaults); |
| 1192 | goto mem_error; |
| 1193 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 1197 | * Split the element name into prefix:localname , the string found |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1198 | * are within the DTD and hen not associated to namespace names. |
| 1199 | */ |
| 1200 | name = xmlSplitQName3(fullattr, &len); |
| 1201 | if (name == NULL) { |
| 1202 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 1203 | prefix = NULL; |
| 1204 | } else { |
| 1205 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1206 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 1207 | } |
| 1208 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1209 | defaults->values[5 * defaults->nbAttrs] = name; |
| 1210 | defaults->values[5 * defaults->nbAttrs + 1] = prefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1211 | /* intern the string and precompute the end */ |
| 1212 | len = xmlStrlen(value); |
| 1213 | value = xmlDictLookup(ctxt->dict, value, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1214 | defaults->values[5 * defaults->nbAttrs + 2] = value; |
| 1215 | defaults->values[5 * defaults->nbAttrs + 3] = value + len; |
| 1216 | if (ctxt->external) |
| 1217 | defaults->values[5 * defaults->nbAttrs + 4] = BAD_CAST "external"; |
| 1218 | else |
| 1219 | defaults->values[5 * defaults->nbAttrs + 4] = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1220 | defaults->nbAttrs++; |
| 1221 | |
| 1222 | return; |
| 1223 | |
| 1224 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1225 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1229 | /** |
| 1230 | * xmlAddSpecialAttr: |
| 1231 | * @ctxt: an XML parser context |
| 1232 | * @fullname: the element fullname |
| 1233 | * @fullattr: the attribute fullname |
| 1234 | * @type: the attribute type |
| 1235 | * |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1236 | * Register this attribute type |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1237 | */ |
| 1238 | static void |
| 1239 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 1240 | const xmlChar *fullname, |
| 1241 | const xmlChar *fullattr, |
| 1242 | int type) |
| 1243 | { |
| 1244 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1245 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1246 | if (ctxt->attsSpecial == NULL) |
| 1247 | goto mem_error; |
| 1248 | } |
| 1249 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1250 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1251 | return; |
| 1252 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1253 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 1254 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1255 | return; |
| 1256 | |
| 1257 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1258 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1259 | return; |
| 1260 | } |
| 1261 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1262 | /** |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1263 | * xmlCleanSpecialAttrCallback: |
| 1264 | * |
| 1265 | * Removes CDATA attributes from the special attribute table |
| 1266 | */ |
| 1267 | static void |
| 1268 | xmlCleanSpecialAttrCallback(void *payload, void *data, |
| 1269 | const xmlChar *fullname, const xmlChar *fullattr, |
| 1270 | const xmlChar *unused ATTRIBUTE_UNUSED) { |
| 1271 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data; |
| 1272 | |
Daniel Veillard | b3edafd | 2008-01-11 08:00:57 +0000 | [diff] [blame] | 1273 | if (((long) payload) == XML_ATTRIBUTE_CDATA) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1274 | xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * xmlCleanSpecialAttr: |
| 1280 | * @ctxt: an XML parser context |
| 1281 | * |
| 1282 | * Trim the list of attributes defined to remove all those of type |
| 1283 | * CDATA as they are not special. This call should be done when finishing |
| 1284 | * to parse the DTD and before starting to parse the document root. |
| 1285 | */ |
| 1286 | static void |
| 1287 | xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt) |
| 1288 | { |
| 1289 | if (ctxt->attsSpecial == NULL) |
| 1290 | return; |
| 1291 | |
| 1292 | xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt); |
| 1293 | |
| 1294 | if (xmlHashSize(ctxt->attsSpecial) == 0) { |
| 1295 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 1296 | ctxt->attsSpecial = NULL; |
| 1297 | } |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | /** |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1302 | * xmlCheckLanguageID: |
| 1303 | * @lang: pointer to the string value |
| 1304 | * |
| 1305 | * Checks that the value conforms to the LanguageID production: |
| 1306 | * |
| 1307 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 1308 | * the XML Second edition. |
| 1309 | * |
| 1310 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 1311 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 1312 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 1313 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 1314 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 1315 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 1316 | * |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1317 | * The current REC reference the sucessors of RFC 1766, currently 5646 |
| 1318 | * |
| 1319 | * http://www.rfc-editor.org/rfc/rfc5646.txt |
| 1320 | * langtag = language |
| 1321 | * ["-" script] |
| 1322 | * ["-" region] |
| 1323 | * *("-" variant) |
| 1324 | * *("-" extension) |
| 1325 | * ["-" privateuse] |
| 1326 | * language = 2*3ALPHA ; shortest ISO 639 code |
| 1327 | * ["-" extlang] ; sometimes followed by |
| 1328 | * ; extended language subtags |
| 1329 | * / 4ALPHA ; or reserved for future use |
| 1330 | * / 5*8ALPHA ; or registered language subtag |
| 1331 | * |
| 1332 | * extlang = 3ALPHA ; selected ISO 639 codes |
| 1333 | * *2("-" 3ALPHA) ; permanently reserved |
| 1334 | * |
| 1335 | * script = 4ALPHA ; ISO 15924 code |
| 1336 | * |
| 1337 | * region = 2ALPHA ; ISO 3166-1 code |
| 1338 | * / 3DIGIT ; UN M.49 code |
| 1339 | * |
| 1340 | * variant = 5*8alphanum ; registered variants |
| 1341 | * / (DIGIT 3alphanum) |
| 1342 | * |
| 1343 | * extension = singleton 1*("-" (2*8alphanum)) |
| 1344 | * |
| 1345 | * ; Single alphanumerics |
| 1346 | * ; "x" reserved for private use |
| 1347 | * singleton = DIGIT ; 0 - 9 |
| 1348 | * / %x41-57 ; A - W |
| 1349 | * / %x59-5A ; Y - Z |
| 1350 | * / %x61-77 ; a - w |
| 1351 | * / %x79-7A ; y - z |
| 1352 | * |
| 1353 | * it sounds right to still allow Irregular i-xxx IANA and user codes too |
| 1354 | * The parser below doesn't try to cope with extension or privateuse |
| 1355 | * that could be added but that's not interoperable anyway |
| 1356 | * |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1357 | * Returns 1 if correct 0 otherwise |
| 1358 | **/ |
| 1359 | int |
| 1360 | xmlCheckLanguageID(const xmlChar * lang) |
| 1361 | { |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1362 | const xmlChar *cur = lang, *nxt; |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1363 | |
| 1364 | if (cur == NULL) |
| 1365 | return (0); |
| 1366 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1367 | ((cur[0] == 'I') && (cur[1] == '-')) || |
| 1368 | ((cur[0] == 'x') && (cur[1] == '-')) || |
| 1369 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1370 | /* |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1371 | * Still allow IANA code and user code which were coming |
| 1372 | * from the previous version of the XML-1.0 specification |
| 1373 | * it's deprecated but we should not fail |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1374 | */ |
| 1375 | cur += 2; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1376 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1377 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1378 | cur++; |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1379 | return(cur[0] == 0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1380 | } |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1381 | nxt = cur; |
| 1382 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1383 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1384 | nxt++; |
| 1385 | if (nxt - cur >= 4) { |
| 1386 | /* |
| 1387 | * Reserved |
| 1388 | */ |
| 1389 | if ((nxt - cur > 8) || (nxt[0] != 0)) |
| 1390 | return(0); |
| 1391 | return(1); |
| 1392 | } |
| 1393 | if (nxt - cur < 2) |
| 1394 | return(0); |
| 1395 | /* we got an ISO 639 code */ |
| 1396 | if (nxt[0] == 0) |
| 1397 | return(1); |
| 1398 | if (nxt[0] != '-') |
| 1399 | return(0); |
| 1400 | |
| 1401 | nxt++; |
| 1402 | cur = nxt; |
| 1403 | /* now we can have extlang or script or region or variant */ |
| 1404 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1405 | goto region_m49; |
| 1406 | |
| 1407 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1408 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1409 | nxt++; |
| 1410 | if (nxt - cur == 4) |
| 1411 | goto script; |
| 1412 | if (nxt - cur == 2) |
| 1413 | goto region; |
| 1414 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1415 | goto variant; |
| 1416 | if (nxt - cur != 3) |
| 1417 | return(0); |
| 1418 | /* we parsed an extlang */ |
| 1419 | if (nxt[0] == 0) |
| 1420 | return(1); |
| 1421 | if (nxt[0] != '-') |
| 1422 | return(0); |
| 1423 | |
| 1424 | nxt++; |
| 1425 | cur = nxt; |
| 1426 | /* now we can have script or region or variant */ |
| 1427 | if ((nxt[0] >= '0') && (nxt[0] <= '9')) |
| 1428 | goto region_m49; |
| 1429 | |
| 1430 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1431 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1432 | nxt++; |
| 1433 | if (nxt - cur == 2) |
| 1434 | goto region; |
| 1435 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1436 | goto variant; |
| 1437 | if (nxt - cur != 4) |
| 1438 | return(0); |
| 1439 | /* we parsed a script */ |
| 1440 | script: |
| 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 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 | |
| 1456 | if ((nxt - cur >= 5) && (nxt - cur <= 8)) |
| 1457 | goto variant; |
| 1458 | if (nxt - cur != 2) |
| 1459 | return(0); |
| 1460 | /* we parsed a region */ |
| 1461 | region: |
| 1462 | if (nxt[0] == 0) |
| 1463 | return(1); |
| 1464 | if (nxt[0] != '-') |
| 1465 | return(0); |
| 1466 | |
| 1467 | nxt++; |
| 1468 | cur = nxt; |
| 1469 | /* now we can just have a variant */ |
| 1470 | while (((nxt[0] >= 'A') && (nxt[0] <= 'Z')) || |
| 1471 | ((nxt[0] >= 'a') && (nxt[0] <= 'z'))) |
| 1472 | nxt++; |
| 1473 | |
| 1474 | if ((nxt - cur < 5) || (nxt - cur > 8)) |
| 1475 | return(0); |
| 1476 | |
| 1477 | /* we parsed a variant */ |
| 1478 | variant: |
| 1479 | if (nxt[0] == 0) |
| 1480 | return(1); |
| 1481 | if (nxt[0] != '-') |
| 1482 | return(0); |
| 1483 | /* extensions and private use subtags not checked */ |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1484 | return (1); |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 1485 | |
| 1486 | region_m49: |
| 1487 | if (((nxt[1] >= '0') && (nxt[1] <= '9')) && |
| 1488 | ((nxt[2] >= '0') && (nxt[2] <= '9'))) { |
| 1489 | nxt += 3; |
| 1490 | goto region; |
| 1491 | } |
| 1492 | return(0); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1495 | /************************************************************************ |
| 1496 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 1497 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1498 | * * |
| 1499 | ************************************************************************/ |
| 1500 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 1501 | static xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 1502 | const xmlChar ** str); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1503 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1504 | #ifdef SAX2 |
| 1505 | /** |
| 1506 | * nsPush: |
| 1507 | * @ctxt: an XML parser context |
| 1508 | * @prefix: the namespace prefix or NULL |
| 1509 | * @URL: the namespace name |
| 1510 | * |
| 1511 | * Pushes a new parser namespace on top of the ns stack |
| 1512 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 1513 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 1514 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1515 | */ |
| 1516 | static int |
| 1517 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 1518 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1519 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 1520 | int i; |
| 1521 | for (i = 0;i < ctxt->nsNr;i += 2) { |
| 1522 | if (ctxt->nsTab[i] == prefix) { |
| 1523 | /* in scope */ |
| 1524 | if (ctxt->nsTab[i + 1] == URL) |
| 1525 | return(-2); |
| 1526 | /* out of scope keep it */ |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
| 1530 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1531 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 1532 | ctxt->nsMax = 10; |
| 1533 | ctxt->nsNr = 0; |
| 1534 | ctxt->nsTab = (const xmlChar **) |
| 1535 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 1536 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1537 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1538 | ctxt->nsMax = 0; |
| 1539 | return (-1); |
| 1540 | } |
| 1541 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1542 | const xmlChar ** tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1543 | ctxt->nsMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1544 | tmp = (const xmlChar **) xmlRealloc((char *) ctxt->nsTab, |
| 1545 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 1546 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1547 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1548 | ctxt->nsMax /= 2; |
| 1549 | return (-1); |
| 1550 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1551 | ctxt->nsTab = tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1552 | } |
| 1553 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 1554 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 1555 | return (ctxt->nsNr); |
| 1556 | } |
| 1557 | /** |
| 1558 | * nsPop: |
| 1559 | * @ctxt: an XML parser context |
| 1560 | * @nr: the number to pop |
| 1561 | * |
| 1562 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 1563 | * |
| 1564 | * Returns the number of namespaces removed |
| 1565 | */ |
| 1566 | static int |
| 1567 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 1568 | { |
| 1569 | int i; |
| 1570 | |
| 1571 | if (ctxt->nsTab == NULL) return(0); |
| 1572 | if (ctxt->nsNr < nr) { |
| 1573 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 1574 | nr = ctxt->nsNr; |
| 1575 | } |
| 1576 | if (ctxt->nsNr <= 0) |
| 1577 | return (0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1578 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1579 | for (i = 0;i < nr;i++) { |
| 1580 | ctxt->nsNr--; |
| 1581 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 1582 | } |
| 1583 | return(nr); |
| 1584 | } |
| 1585 | #endif |
| 1586 | |
| 1587 | static int |
| 1588 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 1589 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1590 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1591 | int maxatts; |
| 1592 | |
| 1593 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1594 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1595 | atts = (const xmlChar **) |
| 1596 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1597 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1598 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1599 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 1600 | if (attallocs == NULL) goto mem_error; |
| 1601 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1602 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1603 | } else if (nr + 5 > ctxt->maxatts) { |
| 1604 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1605 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 1606 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1607 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1608 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1609 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 1610 | (maxatts / 5) * sizeof(int)); |
| 1611 | if (attallocs == NULL) goto mem_error; |
| 1612 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1613 | ctxt->maxatts = maxatts; |
| 1614 | } |
| 1615 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1616 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1617 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1618 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1621 | /** |
| 1622 | * inputPush: |
| 1623 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1624 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1625 | * |
| 1626 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1627 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1628 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1629 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1630 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1631 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 1632 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1633 | if ((ctxt == NULL) || (value == NULL)) |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1634 | return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1635 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 1636 | ctxt->inputMax *= 2; |
| 1637 | ctxt->inputTab = |
| 1638 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 1639 | ctxt->inputMax * |
| 1640 | sizeof(ctxt->inputTab[0])); |
| 1641 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1642 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1643 | xmlFreeInputStream(value); |
| 1644 | ctxt->inputMax /= 2; |
| 1645 | value = NULL; |
| 1646 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1647 | } |
| 1648 | } |
| 1649 | ctxt->inputTab[ctxt->inputNr] = value; |
| 1650 | ctxt->input = value; |
| 1651 | return (ctxt->inputNr++); |
| 1652 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1653 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1654 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1655 | * @ctxt: an XML parser context |
| 1656 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1657 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1658 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1659 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1660 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1661 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1662 | inputPop(xmlParserCtxtPtr ctxt) |
| 1663 | { |
| 1664 | xmlParserInputPtr ret; |
| 1665 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1666 | if (ctxt == NULL) |
| 1667 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1668 | if (ctxt->inputNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1669 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1670 | ctxt->inputNr--; |
| 1671 | if (ctxt->inputNr > 0) |
| 1672 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 1673 | else |
| 1674 | ctxt->input = NULL; |
| 1675 | ret = ctxt->inputTab[ctxt->inputNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1676 | ctxt->inputTab[ctxt->inputNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1677 | return (ret); |
| 1678 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1679 | /** |
| 1680 | * nodePush: |
| 1681 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1682 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1683 | * |
| 1684 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1685 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1686 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1687 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1688 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1689 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1690 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1691 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1692 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1693 | xmlNodePtr *tmp; |
| 1694 | |
| 1695 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1696 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1697 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1698 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1699 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1700 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1701 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1702 | ctxt->nodeTab = tmp; |
| 1703 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1704 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1705 | if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) && |
| 1706 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1707 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1708 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1709 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1710 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1711 | return(-1); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1712 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1713 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1714 | ctxt->node = value; |
| 1715 | return (ctxt->nodeNr++); |
| 1716 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1717 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1718 | /** |
| 1719 | * nodePop: |
| 1720 | * @ctxt: an XML parser context |
| 1721 | * |
| 1722 | * Pops the top element node from the node stack |
| 1723 | * |
| 1724 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1725 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1726 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1727 | nodePop(xmlParserCtxtPtr ctxt) |
| 1728 | { |
| 1729 | xmlNodePtr ret; |
| 1730 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1731 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1732 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1733 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1734 | ctxt->nodeNr--; |
| 1735 | if (ctxt->nodeNr > 0) |
| 1736 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1737 | else |
| 1738 | ctxt->node = NULL; |
| 1739 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1740 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1741 | return (ret); |
| 1742 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1743 | |
| 1744 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1745 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1746 | * nameNsPush: |
| 1747 | * @ctxt: an XML parser context |
| 1748 | * @value: the element name |
| 1749 | * @prefix: the element prefix |
| 1750 | * @URI: the element namespace name |
| 1751 | * |
| 1752 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1753 | * |
| 1754 | * Returns -1 in case of error, the index in the stack otherwise |
| 1755 | */ |
| 1756 | static int |
| 1757 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1758 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1759 | { |
| 1760 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1761 | const xmlChar * *tmp; |
| 1762 | void **tmp2; |
| 1763 | ctxt->nameMax *= 2; |
| 1764 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1765 | ctxt->nameMax * |
| 1766 | sizeof(ctxt->nameTab[0])); |
| 1767 | if (tmp == NULL) { |
| 1768 | ctxt->nameMax /= 2; |
| 1769 | goto mem_error; |
| 1770 | } |
| 1771 | ctxt->nameTab = tmp; |
| 1772 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1773 | ctxt->nameMax * 3 * |
| 1774 | sizeof(ctxt->pushTab[0])); |
| 1775 | if (tmp2 == NULL) { |
| 1776 | ctxt->nameMax /= 2; |
| 1777 | goto mem_error; |
| 1778 | } |
| 1779 | ctxt->pushTab = tmp2; |
| 1780 | } |
| 1781 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1782 | ctxt->name = value; |
| 1783 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1784 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1785 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1786 | return (ctxt->nameNr++); |
| 1787 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1788 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1789 | return (-1); |
| 1790 | } |
| 1791 | /** |
| 1792 | * nameNsPop: |
| 1793 | * @ctxt: an XML parser context |
| 1794 | * |
| 1795 | * Pops the top element/prefix/URI name from the name stack |
| 1796 | * |
| 1797 | * Returns the name just removed |
| 1798 | */ |
| 1799 | static const xmlChar * |
| 1800 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1801 | { |
| 1802 | const xmlChar *ret; |
| 1803 | |
| 1804 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1805 | return (NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1806 | ctxt->nameNr--; |
| 1807 | if (ctxt->nameNr > 0) |
| 1808 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1809 | else |
| 1810 | ctxt->name = NULL; |
| 1811 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1812 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1813 | return (ret); |
| 1814 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1815 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1816 | |
| 1817 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1818 | * namePush: |
| 1819 | * @ctxt: an XML parser context |
| 1820 | * @value: the element name |
| 1821 | * |
| 1822 | * Pushes a new element name on top of the name stack |
| 1823 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1824 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1825 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1826 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1827 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1828 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1829 | if (ctxt == NULL) return (-1); |
| 1830 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1831 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1832 | const xmlChar * *tmp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1833 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1834 | ctxt->nameMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1835 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1836 | if (tmp == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1837 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1838 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1839 | ctxt->nameTab = tmp; |
Xia Xinfeng | 5825ebb | 2011-11-10 13:50:22 +0800 | [diff] [blame] | 1840 | ctxt->nameMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1841 | } |
| 1842 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1843 | ctxt->name = value; |
| 1844 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1845 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1846 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1847 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1848 | } |
| 1849 | /** |
| 1850 | * namePop: |
| 1851 | * @ctxt: an XML parser context |
| 1852 | * |
| 1853 | * Pops the top element name from the name stack |
| 1854 | * |
| 1855 | * Returns the name just removed |
| 1856 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1857 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1858 | namePop(xmlParserCtxtPtr ctxt) |
| 1859 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1860 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1861 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1862 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1863 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1864 | ctxt->nameNr--; |
| 1865 | if (ctxt->nameNr > 0) |
| 1866 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1867 | else |
| 1868 | ctxt->name = NULL; |
| 1869 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1870 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1871 | return (ret); |
| 1872 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1874 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1875 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1876 | int *tmp; |
| 1877 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1878 | ctxt->spaceMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1879 | tmp = (int *) xmlRealloc(ctxt->spaceTab, |
| 1880 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1881 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1882 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1883 | ctxt->spaceMax /=2; |
| 1884 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1885 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1886 | ctxt->spaceTab = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1887 | } |
| 1888 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1889 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1890 | return(ctxt->spaceNr++); |
| 1891 | } |
| 1892 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1893 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | int ret; |
| 1895 | if (ctxt->spaceNr <= 0) return(0); |
| 1896 | ctxt->spaceNr--; |
| 1897 | if (ctxt->spaceNr > 0) |
| 1898 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1899 | else |
Daniel Veillard | dbcbbd2 | 2006-06-18 19:55:20 +0000 | [diff] [blame] | 1900 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1901 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1902 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1903 | return(ret); |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Macros for accessing the content. Those should be used only by the parser, |
| 1908 | * and not exported. |
| 1909 | * |
| 1910 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1911 | * use them |
| 1912 | * |
| 1913 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1914 | * To be used with extreme caution since operations consuming |
| 1915 | * characters may move the input buffer to a different location ! |
| 1916 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1917 | * This should be used internally by the parser |
| 1918 | * only to compare to ASCII values otherwise it would break when |
| 1919 | * running with UTF-8 encoding. |
| 1920 | * RAW same as CUR but in the input buffer, bypass any token |
| 1921 | * extraction that may have been done |
| 1922 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1923 | * to compare on ASCII based substring. |
| 1924 | * 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] | 1925 | * strings without newlines within the parser. |
| 1926 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
| 1927 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1928 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1929 | * |
| 1930 | * NEXT Skip to the next character, this does the proper decoding |
| 1931 | * 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] | 1932 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1933 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1934 | * to the number of xmlChars used for the encoding [0-5]. |
| 1935 | * CUR_SCHAR same but operate on a string instead of the context |
| 1936 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1937 | * the index |
| 1938 | * GROW, SHRINK handling of input buffers |
| 1939 | */ |
| 1940 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1941 | #define RAW (*ctxt->input->cur) |
| 1942 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1943 | #define NXT(val) ctxt->input->cur[(val)] |
| 1944 | #define CUR_PTR ctxt->input->cur |
| 1945 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1946 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1947 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1948 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1949 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1950 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1951 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1952 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1953 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1954 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1955 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1956 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1957 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1958 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1959 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1960 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1961 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 1962 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 1963 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1964 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1965 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1966 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1967 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1968 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1969 | xmlPopInput(ctxt); \ |
| 1970 | } while (0) |
| 1971 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1972 | #define SKIPL(val) do { \ |
| 1973 | int skipl; \ |
| 1974 | for(skipl=0; skipl<val; skipl++) { \ |
| 1975 | if (*(ctxt->input->cur) == '\n') { \ |
| 1976 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1977 | } else ctxt->input->col++; \ |
| 1978 | ctxt->nbChars++; \ |
| 1979 | ctxt->input->cur++; \ |
| 1980 | } \ |
| 1981 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 1982 | if ((*ctxt->input->cur == 0) && \ |
| 1983 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1984 | xmlPopInput(ctxt); \ |
| 1985 | } while (0) |
| 1986 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1987 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1988 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 1989 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1990 | xmlSHRINK (ctxt); |
| 1991 | |
| 1992 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 1993 | xmlParserInputShrink(ctxt->input); |
| 1994 | if ((*ctxt->input->cur == 0) && |
| 1995 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1996 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1997 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1998 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1999 | #define GROW if ((ctxt->progressive == 0) && \ |
| 2000 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2001 | xmlGROW (ctxt); |
| 2002 | |
| 2003 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
| 2004 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
Daniel Veillard | 59df783 | 2010-02-02 10:24:01 +0100 | [diff] [blame] | 2005 | if ((ctxt->input->cur != NULL) && (*ctxt->input->cur == 0) && |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2006 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2007 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 2008 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2009 | |
| 2010 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 2011 | |
| 2012 | #define NEXT xmlNextChar(ctxt) |
| 2013 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2014 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2015 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2016 | ctxt->input->cur++; \ |
| 2017 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2018 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2019 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 2020 | } |
| 2021 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2022 | #define NEXTL(l) do { \ |
| 2023 | if (*(ctxt->input->cur) == '\n') { \ |
| 2024 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 2025 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2026 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2028 | } while (0) |
| 2029 | |
| 2030 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 2031 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 2032 | |
| 2033 | #define COPY_BUF(l,b,i,v) \ |
| 2034 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2035 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2036 | |
| 2037 | /** |
| 2038 | * xmlSkipBlankChars: |
| 2039 | * @ctxt: the XML parser context |
| 2040 | * |
| 2041 | * skip all blanks character found at that point in the input streams. |
| 2042 | * It pops up finished entities in the process if allowable at that point. |
| 2043 | * |
| 2044 | * Returns the number of space chars skipped |
| 2045 | */ |
| 2046 | |
| 2047 | int |
| 2048 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2049 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2050 | |
| 2051 | /* |
| 2052 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 2053 | * the ASCII range. |
| 2054 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2055 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 2056 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2057 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2058 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2059 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2060 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2061 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2062 | if (*cur == '\n') { |
| 2063 | ctxt->input->line++; ctxt->input->col = 1; |
| 2064 | } |
| 2065 | cur++; |
| 2066 | res++; |
| 2067 | if (*cur == 0) { |
| 2068 | ctxt->input->cur = cur; |
| 2069 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 2070 | cur = ctxt->input->cur; |
| 2071 | } |
| 2072 | } |
| 2073 | ctxt->input->cur = cur; |
| 2074 | } else { |
| 2075 | int cur; |
| 2076 | do { |
| 2077 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 2078 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 2079 | NEXT; |
| 2080 | cur = CUR; |
| 2081 | res++; |
| 2082 | } |
| 2083 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 2084 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 2085 | xmlPopInput(ctxt); |
| 2086 | cur = CUR; |
| 2087 | } |
| 2088 | /* |
| 2089 | * Need to handle support of entities branching here |
| 2090 | */ |
| 2091 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 2092 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 2093 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2094 | return(res); |
| 2095 | } |
| 2096 | |
| 2097 | /************************************************************************ |
| 2098 | * * |
| 2099 | * Commodity functions to handle entities * |
| 2100 | * * |
| 2101 | ************************************************************************/ |
| 2102 | |
| 2103 | /** |
| 2104 | * xmlPopInput: |
| 2105 | * @ctxt: an XML parser context |
| 2106 | * |
| 2107 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 2108 | * pop it and return the next char. |
| 2109 | * |
| 2110 | * Returns the current xmlChar in the parser context |
| 2111 | */ |
| 2112 | xmlChar |
| 2113 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2114 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2115 | if (xmlParserDebugEntities) |
| 2116 | xmlGenericError(xmlGenericErrorContext, |
| 2117 | "Popping input %d\n", ctxt->inputNr); |
| 2118 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2119 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2120 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 2121 | return(xmlPopInput(ctxt)); |
| 2122 | return(CUR); |
| 2123 | } |
| 2124 | |
| 2125 | /** |
| 2126 | * xmlPushInput: |
| 2127 | * @ctxt: an XML parser context |
| 2128 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 2129 | * |
| 2130 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 2131 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2132 | * 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] | 2133 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2134 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2135 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2136 | int ret; |
| 2137 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2138 | |
| 2139 | if (xmlParserDebugEntities) { |
| 2140 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 2141 | xmlGenericError(xmlGenericErrorContext, |
| 2142 | "%s(%d): ", ctxt->input->filename, |
| 2143 | ctxt->input->line); |
| 2144 | xmlGenericError(xmlGenericErrorContext, |
| 2145 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 2146 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2147 | ret = inputPush(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2148 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2149 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /** |
| 2153 | * xmlParseCharRef: |
| 2154 | * @ctxt: an XML parser context |
| 2155 | * |
| 2156 | * parse Reference declarations |
| 2157 | * |
| 2158 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2159 | * '&#x' [0-9a-fA-F]+ ';' |
| 2160 | * |
| 2161 | * [ WFC: Legal Character ] |
| 2162 | * Characters referred to using character references must match the |
| 2163 | * production for Char. |
| 2164 | * |
| 2165 | * Returns the value parsed (as an int), 0 in case of error |
| 2166 | */ |
| 2167 | int |
| 2168 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2169 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2170 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2171 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2172 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2173 | /* |
| 2174 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2175 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2176 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2177 | (NXT(2) == 'x')) { |
| 2178 | SKIP(3); |
| 2179 | GROW; |
| 2180 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2181 | if (count++ > 20) { |
| 2182 | count = 0; |
| 2183 | GROW; |
| 2184 | } |
| 2185 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2186 | val = val * 16 + (CUR - '0'); |
| 2187 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2188 | val = val * 16 + (CUR - 'a') + 10; |
| 2189 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2190 | val = val * 16 + (CUR - 'A') + 10; |
| 2191 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2192 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2193 | val = 0; |
| 2194 | break; |
| 2195 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2196 | if (val > 0x10FFFF) |
| 2197 | outofrange = val; |
| 2198 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2199 | NEXT; |
| 2200 | count++; |
| 2201 | } |
| 2202 | if (RAW == ';') { |
| 2203 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2204 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2205 | ctxt->nbChars ++; |
| 2206 | ctxt->input->cur++; |
| 2207 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2208 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | SKIP(2); |
| 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; |
| 2215 | } |
| 2216 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2217 | val = val * 10 + (CUR - '0'); |
| 2218 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2219 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2220 | val = 0; |
| 2221 | break; |
| 2222 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2223 | if (val > 0x10FFFF) |
| 2224 | outofrange = val; |
| 2225 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2226 | NEXT; |
| 2227 | count++; |
| 2228 | } |
| 2229 | if (RAW == ';') { |
| 2230 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2231 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2232 | ctxt->nbChars ++; |
| 2233 | ctxt->input->cur++; |
| 2234 | } |
| 2235 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2236 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * [ WFC: Legal Character ] |
| 2241 | * Characters referred to using character references must match the |
| 2242 | * production for Char. |
| 2243 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2244 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2245 | return(val); |
| 2246 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2247 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2248 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2249 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2250 | } |
| 2251 | return(0); |
| 2252 | } |
| 2253 | |
| 2254 | /** |
| 2255 | * xmlParseStringCharRef: |
| 2256 | * @ctxt: an XML parser context |
| 2257 | * @str: a pointer to an index in the string |
| 2258 | * |
| 2259 | * parse Reference declarations, variant parsing from a string rather |
| 2260 | * than an an input flow. |
| 2261 | * |
| 2262 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2263 | * '&#x' [0-9a-fA-F]+ ';' |
| 2264 | * |
| 2265 | * [ WFC: Legal Character ] |
| 2266 | * Characters referred to using character references must match the |
| 2267 | * production for Char. |
| 2268 | * |
| 2269 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2270 | * updated to the current value of the index |
| 2271 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2272 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2273 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2274 | const xmlChar *ptr; |
| 2275 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2276 | unsigned int val = 0; |
| 2277 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2278 | |
| 2279 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2280 | ptr = *str; |
| 2281 | cur = *ptr; |
| 2282 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2283 | ptr += 3; |
| 2284 | cur = *ptr; |
| 2285 | while (cur != ';') { /* Non input consuming loop */ |
| 2286 | if ((cur >= '0') && (cur <= '9')) |
| 2287 | val = val * 16 + (cur - '0'); |
| 2288 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2289 | val = val * 16 + (cur - 'a') + 10; |
| 2290 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2291 | val = val * 16 + (cur - 'A') + 10; |
| 2292 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2293 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2294 | val = 0; |
| 2295 | break; |
| 2296 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2297 | if (val > 0x10FFFF) |
| 2298 | outofrange = val; |
| 2299 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2300 | ptr++; |
| 2301 | cur = *ptr; |
| 2302 | } |
| 2303 | if (cur == ';') |
| 2304 | ptr++; |
| 2305 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2306 | ptr += 2; |
| 2307 | cur = *ptr; |
| 2308 | while (cur != ';') { /* Non input consuming loops */ |
| 2309 | if ((cur >= '0') && (cur <= '9')) |
| 2310 | val = val * 10 + (cur - '0'); |
| 2311 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2312 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | val = 0; |
| 2314 | break; |
| 2315 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2316 | if (val > 0x10FFFF) |
| 2317 | outofrange = val; |
| 2318 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2319 | ptr++; |
| 2320 | cur = *ptr; |
| 2321 | } |
| 2322 | if (cur == ';') |
| 2323 | ptr++; |
| 2324 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2325 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2326 | return(0); |
| 2327 | } |
| 2328 | *str = ptr; |
| 2329 | |
| 2330 | /* |
| 2331 | * [ WFC: Legal Character ] |
| 2332 | * Characters referred to using character references must match the |
| 2333 | * production for Char. |
| 2334 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2335 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2336 | return(val); |
| 2337 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2338 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2339 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2340 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | } |
| 2342 | return(0); |
| 2343 | } |
| 2344 | |
| 2345 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2346 | * xmlNewBlanksWrapperInputStream: |
| 2347 | * @ctxt: an XML parser context |
| 2348 | * @entity: an Entity pointer |
| 2349 | * |
| 2350 | * Create a new input stream for wrapping |
| 2351 | * blanks around a PEReference |
| 2352 | * |
| 2353 | * Returns the new input stream or NULL |
| 2354 | */ |
| 2355 | |
| 2356 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
| 2357 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2358 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2359 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2360 | xmlParserInputPtr input; |
| 2361 | xmlChar *buffer; |
| 2362 | size_t length; |
| 2363 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2364 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2365 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2366 | return(NULL); |
| 2367 | } |
| 2368 | if (xmlParserDebugEntities) |
| 2369 | xmlGenericError(xmlGenericErrorContext, |
| 2370 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2371 | input = xmlNewInputStream(ctxt); |
| 2372 | if (input == NULL) { |
| 2373 | return(NULL); |
| 2374 | } |
| 2375 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2376 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2377 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2378 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2379 | xmlFree(input); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2380 | return(NULL); |
| 2381 | } |
| 2382 | buffer [0] = ' '; |
| 2383 | buffer [1] = '%'; |
| 2384 | buffer [length-3] = ';'; |
| 2385 | buffer [length-2] = ' '; |
| 2386 | buffer [length-1] = 0; |
| 2387 | memcpy(buffer + 2, entity->name, length - 5); |
| 2388 | input->free = deallocblankswrapper; |
| 2389 | input->base = buffer; |
| 2390 | input->cur = buffer; |
| 2391 | input->length = length; |
| 2392 | input->end = &buffer[length]; |
| 2393 | return(input); |
| 2394 | } |
| 2395 | |
| 2396 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2397 | * xmlParserHandlePEReference: |
| 2398 | * @ctxt: the parser context |
| 2399 | * |
| 2400 | * [69] PEReference ::= '%' Name ';' |
| 2401 | * |
| 2402 | * [ WFC: No Recursion ] |
| 2403 | * A parsed entity must not contain a recursive |
| 2404 | * reference to itself, either directly or indirectly. |
| 2405 | * |
| 2406 | * [ WFC: Entity Declared ] |
| 2407 | * In a document without any DTD, a document with only an internal DTD |
| 2408 | * subset which contains no parameter entity references, or a document |
| 2409 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2410 | * entity must precede any reference to it... |
| 2411 | * |
| 2412 | * [ VC: Entity Declared ] |
| 2413 | * In a document with an external subset or external parameter entities |
| 2414 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2415 | * must precede any reference to it... |
| 2416 | * |
| 2417 | * [ WFC: In DTD ] |
| 2418 | * Parameter-entity references may only appear in the DTD. |
| 2419 | * NOTE: misleading but this is handled. |
| 2420 | * |
| 2421 | * A PEReference may have been detected in the current input stream |
| 2422 | * the handling is done accordingly to |
| 2423 | * http://www.w3.org/TR/REC-xml#entproc |
| 2424 | * i.e. |
| 2425 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2426 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2427 | */ |
| 2428 | void |
| 2429 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2430 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2431 | xmlEntityPtr entity = NULL; |
| 2432 | xmlParserInputPtr input; |
| 2433 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2434 | if (RAW != '%') return; |
| 2435 | switch(ctxt->instate) { |
| 2436 | case XML_PARSER_CDATA_SECTION: |
| 2437 | return; |
| 2438 | case XML_PARSER_COMMENT: |
| 2439 | return; |
| 2440 | case XML_PARSER_START_TAG: |
| 2441 | return; |
| 2442 | case XML_PARSER_END_TAG: |
| 2443 | return; |
| 2444 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2445 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2446 | return; |
| 2447 | case XML_PARSER_PROLOG: |
| 2448 | case XML_PARSER_START: |
| 2449 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2450 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2451 | return; |
| 2452 | case XML_PARSER_ENTITY_DECL: |
| 2453 | case XML_PARSER_CONTENT: |
| 2454 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2455 | case XML_PARSER_PI: |
| 2456 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2457 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2458 | /* we just ignore it there */ |
| 2459 | return; |
| 2460 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2461 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | return; |
| 2463 | case XML_PARSER_ENTITY_VALUE: |
| 2464 | /* |
| 2465 | * NOTE: in the case of entity values, we don't do the |
| 2466 | * substitution here since we need the literal |
| 2467 | * entity value to be able to save the internal |
| 2468 | * subset of the document. |
| 2469 | * This will be handled by xmlStringDecodeEntities |
| 2470 | */ |
| 2471 | return; |
| 2472 | case XML_PARSER_DTD: |
| 2473 | /* |
| 2474 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2475 | * In the internal DTD subset, parameter-entity references |
| 2476 | * can occur only where markup declarations can occur, not |
| 2477 | * within markup declarations. |
| 2478 | * In that case this is handled in xmlParseMarkupDecl |
| 2479 | */ |
| 2480 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2481 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2482 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2483 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | break; |
| 2485 | case XML_PARSER_IGNORE: |
| 2486 | return; |
| 2487 | } |
| 2488 | |
| 2489 | NEXT; |
| 2490 | name = xmlParseName(ctxt); |
| 2491 | if (xmlParserDebugEntities) |
| 2492 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2493 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2494 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2495 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2496 | } else { |
| 2497 | if (RAW == ';') { |
| 2498 | NEXT; |
| 2499 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2500 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 2501 | if (entity == NULL) { |
| 2502 | |
| 2503 | /* |
| 2504 | * [ WFC: Entity Declared ] |
| 2505 | * In a document without any DTD, a document with only an |
| 2506 | * internal DTD subset which contains no parameter entity |
| 2507 | * references, or a document with "standalone='yes'", ... |
| 2508 | * ... The declaration of a parameter entity must precede |
| 2509 | * any reference to it... |
| 2510 | */ |
| 2511 | if ((ctxt->standalone == 1) || |
| 2512 | ((ctxt->hasExternalSubset == 0) && |
| 2513 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2514 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2515 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2516 | } else { |
| 2517 | /* |
| 2518 | * [ VC: Entity Declared ] |
| 2519 | * In a document with an external subset or external |
| 2520 | * parameter entities with "standalone='no'", ... |
| 2521 | * ... The declaration of a parameter entity must precede |
| 2522 | * any reference to it... |
| 2523 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2524 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2525 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2526 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2527 | name, NULL); |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2528 | } else |
| 2529 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2530 | "PEReference: %%%s; not found\n", |
| 2531 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2532 | ctxt->valid = 0; |
| 2533 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2534 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2535 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2536 | if (xmlPushInput(ctxt, input) < 0) |
| 2537 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2538 | } else { |
| 2539 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2540 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2541 | xmlChar start[4]; |
| 2542 | xmlCharEncoding enc; |
| 2543 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2544 | /* |
| 2545 | * handle the extra spaces added before and after |
| 2546 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2547 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2548 | */ |
| 2549 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2550 | if (xmlPushInput(ctxt, input) < 0) |
| 2551 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2552 | |
| 2553 | /* |
| 2554 | * Get the 4 first bytes and decode the charset |
| 2555 | * if enc != XML_CHAR_ENCODING_NONE |
| 2556 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2557 | * Note that, since we may have some non-UTF8 |
| 2558 | * encoding (like UTF16, bug 135229), the 'length' |
| 2559 | * is not known, but we can calculate based upon |
| 2560 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2561 | */ |
| 2562 | GROW |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2563 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2564 | start[0] = RAW; |
| 2565 | start[1] = NXT(1); |
| 2566 | start[2] = NXT(2); |
| 2567 | start[3] = NXT(3); |
| 2568 | enc = xmlDetectCharEncoding(start, 4); |
| 2569 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2570 | xmlSwitchEncoding(ctxt, enc); |
| 2571 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2575 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2576 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2577 | xmlParseTextDecl(ctxt); |
| 2578 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2579 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2580 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2581 | "PEReference: %s is not a parameter entity\n", |
| 2582 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2583 | } |
| 2584 | } |
| 2585 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2586 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2587 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | /* |
| 2592 | * Macro used to grow the current buffer. |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2593 | * buffer##_size is expected to be a size_t |
| 2594 | * mem_error: is expected to handle memory allocation failures |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2595 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2596 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2597 | xmlChar *tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2598 | size_t new_size = buffer##_size * 2 + n; \ |
| 2599 | if (new_size < buffer##_size) goto mem_error; \ |
| 2600 | tmp = (xmlChar *) xmlRealloc(buffer, new_size); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2601 | if (tmp == NULL) goto mem_error; \ |
| 2602 | buffer = tmp; \ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2603 | buffer##_size = new_size; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2607 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2608 | * @ctxt: the parser context |
| 2609 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2610 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2612 | * @end: an end marker xmlChar, 0 if none |
| 2613 | * @end2: an end marker xmlChar, 0 if none |
| 2614 | * @end3: an end marker xmlChar, 0 if none |
| 2615 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2616 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2617 | * |
| 2618 | * [67] Reference ::= EntityRef | CharRef |
| 2619 | * |
| 2620 | * [69] PEReference ::= '%' Name ';' |
| 2621 | * |
| 2622 | * Returns A newly allocated string with the substitution done. The caller |
| 2623 | * must deallocate it ! |
| 2624 | */ |
| 2625 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2626 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2627 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2628 | xmlChar *buffer = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2629 | size_t buffer_size = 0; |
| 2630 | size_t nbchars = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2631 | |
| 2632 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2633 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2634 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2635 | xmlEntityPtr ent; |
| 2636 | int c,l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2637 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2638 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2640 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2641 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2642 | if (((ctxt->depth > 40) && |
| 2643 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2644 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2645 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2646 | return(NULL); |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * allocate a translation buffer. |
| 2651 | */ |
| 2652 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2653 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2654 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2655 | |
| 2656 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2657 | * 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] | 2658 | * we are operating on already parsed values. |
| 2659 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2660 | if (str < last) |
| 2661 | c = CUR_SCHAR(str, l); |
| 2662 | else |
| 2663 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2664 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2665 | (c != end2) && (c != end3)) { |
| 2666 | |
| 2667 | if (c == 0) break; |
| 2668 | if ((c == '&') && (str[1] == '#')) { |
| 2669 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2670 | if (val != 0) { |
| 2671 | COPY_BUF(0,buffer,nbchars,val); |
| 2672 | } |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2673 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2674 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2675 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2676 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2677 | if (xmlParserDebugEntities) |
| 2678 | xmlGenericError(xmlGenericErrorContext, |
| 2679 | "String decoding Entity Reference: %.30s\n", |
| 2680 | str); |
| 2681 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2682 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2683 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2684 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2685 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2686 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2687 | if ((ent != NULL) && |
| 2688 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2689 | if (ent->content != NULL) { |
| 2690 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2691 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2692 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2693 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2694 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2695 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2696 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2699 | ctxt->depth++; |
| 2700 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2701 | 0, 0, 0); |
| 2702 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2703 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2704 | if (rep != NULL) { |
| 2705 | current = rep; |
| 2706 | while (*current != 0) { /* non input consuming loop */ |
| 2707 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2708 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2709 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2710 | goto int_error; |
| 2711 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2712 | } |
| 2713 | } |
| 2714 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2715 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2716 | } |
| 2717 | } else if (ent != NULL) { |
| 2718 | int i = xmlStrlen(ent->name); |
| 2719 | const xmlChar *cur = ent->name; |
| 2720 | |
| 2721 | buffer[nbchars++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2722 | if (nbchars + i + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 5bd3c06 | 2011-12-16 18:53:35 +0800 | [diff] [blame] | 2723 | growBuffer(buffer, i + XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2724 | } |
| 2725 | for (;i > 0;i--) |
| 2726 | buffer[nbchars++] = *cur++; |
| 2727 | buffer[nbchars++] = ';'; |
| 2728 | } |
| 2729 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2730 | if (xmlParserDebugEntities) |
| 2731 | xmlGenericError(xmlGenericErrorContext, |
| 2732 | "String decoding PE Reference: %.30s\n", str); |
| 2733 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2734 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2735 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2736 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2737 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2738 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2739 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2740 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2741 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2742 | ctxt->depth++; |
| 2743 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2744 | 0, 0, 0); |
| 2745 | ctxt->depth--; |
| 2746 | if (rep != NULL) { |
| 2747 | current = rep; |
| 2748 | while (*current != 0) { /* non input consuming loop */ |
| 2749 | buffer[nbchars++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2750 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2751 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2752 | goto int_error; |
| 2753 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2757 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2758 | } |
| 2759 | } |
| 2760 | } else { |
| 2761 | COPY_BUF(l,buffer,nbchars,c); |
| 2762 | str += l; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 2763 | if (nbchars + XML_PARSER_BUFFER_SIZE > buffer_size) { |
| 2764 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2765 | } |
| 2766 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2767 | if (str < last) |
| 2768 | c = CUR_SCHAR(str, l); |
| 2769 | else |
| 2770 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2771 | } |
Daniel Veillard | 13cee4e | 2009-09-05 14:52:55 +0200 | [diff] [blame] | 2772 | buffer[nbchars] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2773 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2774 | |
| 2775 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2776 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2777 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2778 | if (rep != NULL) |
| 2779 | xmlFree(rep); |
| 2780 | if (buffer != NULL) |
| 2781 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2782 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2785 | /** |
| 2786 | * xmlStringDecodeEntities: |
| 2787 | * @ctxt: the parser context |
| 2788 | * @str: the input string |
| 2789 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2790 | * @end: an end marker xmlChar, 0 if none |
| 2791 | * @end2: an end marker xmlChar, 0 if none |
| 2792 | * @end3: an end marker xmlChar, 0 if none |
| 2793 | * |
| 2794 | * Takes a entity string content and process to do the adequate substitutions. |
| 2795 | * |
| 2796 | * [67] Reference ::= EntityRef | CharRef |
| 2797 | * |
| 2798 | * [69] PEReference ::= '%' Name ';' |
| 2799 | * |
| 2800 | * Returns A newly allocated string with the substitution done. The caller |
| 2801 | * must deallocate it ! |
| 2802 | */ |
| 2803 | xmlChar * |
| 2804 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2805 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2806 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2807 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2808 | end, end2, end3)); |
| 2809 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2810 | |
| 2811 | /************************************************************************ |
| 2812 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2813 | * Commodity functions, cleanup needed ? * |
| 2814 | * * |
| 2815 | ************************************************************************/ |
| 2816 | |
| 2817 | /** |
| 2818 | * areBlanks: |
| 2819 | * @ctxt: an XML parser context |
| 2820 | * @str: a xmlChar * |
| 2821 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2822 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2823 | * |
| 2824 | * Is this a sequence of blank chars that one can ignore ? |
| 2825 | * |
| 2826 | * Returns 1 if ignorable 0 otherwise. |
| 2827 | */ |
| 2828 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2829 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2830 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2831 | int i, ret; |
| 2832 | xmlNodePtr lastChild; |
| 2833 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2834 | /* |
| 2835 | * Don't spend time trying to differentiate them, the same callback is |
| 2836 | * used ! |
| 2837 | */ |
| 2838 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2839 | return(0); |
| 2840 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2841 | /* |
| 2842 | * Check for xml:space value. |
| 2843 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2844 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2845 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2846 | return(0); |
| 2847 | |
| 2848 | /* |
| 2849 | * Check that the string is made of blanks |
| 2850 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2851 | if (blank_chars == 0) { |
| 2852 | for (i = 0;i < len;i++) |
| 2853 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2854 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2855 | |
| 2856 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2857 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2858 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2859 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2860 | if (ctxt->myDoc != NULL) { |
| 2861 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2862 | if (ret == 0) return(1); |
| 2863 | if (ret == 1) return(0); |
| 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | * Otherwise, heuristic :-\ |
| 2868 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2869 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2870 | if ((ctxt->node->children == NULL) && |
| 2871 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2872 | |
| 2873 | lastChild = xmlGetLastChild(ctxt->node); |
| 2874 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2875 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2876 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2877 | } else if (xmlNodeIsText(lastChild)) |
| 2878 | return(0); |
| 2879 | else if ((ctxt->node->children != NULL) && |
| 2880 | (xmlNodeIsText(ctxt->node->children))) |
| 2881 | return(0); |
| 2882 | return(1); |
| 2883 | } |
| 2884 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2885 | /************************************************************************ |
| 2886 | * * |
| 2887 | * Extra stuff for namespace support * |
| 2888 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2889 | * * |
| 2890 | ************************************************************************/ |
| 2891 | |
| 2892 | /** |
| 2893 | * xmlSplitQName: |
| 2894 | * @ctxt: an XML parser context |
| 2895 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2896 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2897 | * |
| 2898 | * parse an UTF8 encoded XML qualified name string |
| 2899 | * |
| 2900 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2901 | * |
| 2902 | * [NS 6] Prefix ::= NCName |
| 2903 | * |
| 2904 | * [NS 7] LocalPart ::= NCName |
| 2905 | * |
| 2906 | * Returns the local part, and prefix is updated |
| 2907 | * to get the Prefix if any. |
| 2908 | */ |
| 2909 | |
| 2910 | xmlChar * |
| 2911 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2912 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2913 | xmlChar *buffer = NULL; |
| 2914 | int len = 0; |
| 2915 | int max = XML_MAX_NAMELEN; |
| 2916 | xmlChar *ret = NULL; |
| 2917 | const xmlChar *cur = name; |
| 2918 | int c; |
| 2919 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2920 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2921 | *prefix = NULL; |
| 2922 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2923 | if (cur == NULL) return(NULL); |
| 2924 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2925 | #ifndef XML_XML_NAMESPACE |
| 2926 | /* xml: prefix is not really a namespace */ |
| 2927 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2928 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2929 | return(xmlStrdup(name)); |
| 2930 | #endif |
| 2931 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2932 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2933 | if (cur[0] == ':') |
| 2934 | return(xmlStrdup(name)); |
| 2935 | |
| 2936 | c = *cur++; |
| 2937 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2938 | buf[len++] = c; |
| 2939 | c = *cur++; |
| 2940 | } |
| 2941 | if (len >= max) { |
| 2942 | /* |
| 2943 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2944 | * for the processing speed. |
| 2945 | */ |
| 2946 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2947 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2948 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2949 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2950 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2951 | return(NULL); |
| 2952 | } |
| 2953 | memcpy(buffer, buf, len); |
| 2954 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2955 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2956 | xmlChar *tmp; |
| 2957 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2958 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2959 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2960 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2961 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2962 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2963 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2964 | return(NULL); |
| 2965 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2966 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2967 | } |
| 2968 | buffer[len++] = c; |
| 2969 | c = *cur++; |
| 2970 | } |
| 2971 | buffer[len] = 0; |
| 2972 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2973 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2974 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 2975 | if (buffer != NULL) |
| 2976 | xmlFree(buffer); |
| 2977 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2978 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 2979 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2980 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2981 | if (buffer == NULL) |
| 2982 | ret = xmlStrndup(buf, len); |
| 2983 | else { |
| 2984 | ret = buffer; |
| 2985 | buffer = NULL; |
| 2986 | max = XML_MAX_NAMELEN; |
| 2987 | } |
| 2988 | |
| 2989 | |
| 2990 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2991 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2992 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2993 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 2994 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2995 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2996 | len = 0; |
| 2997 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2998 | /* |
| 2999 | * Check that the first character is proper to start |
| 3000 | * a new name |
| 3001 | */ |
| 3002 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 3003 | ((c >= 0x41) && (c <= 0x5A)) || |
| 3004 | (c == '_') || (c == ':'))) { |
| 3005 | int l; |
| 3006 | int first = CUR_SCHAR(cur, l); |
| 3007 | |
| 3008 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3009 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3010 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3011 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 3012 | } |
| 3013 | } |
| 3014 | cur++; |
| 3015 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3016 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 3017 | buf[len++] = c; |
| 3018 | c = *cur++; |
| 3019 | } |
| 3020 | if (len >= max) { |
| 3021 | /* |
| 3022 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3023 | * for the processing speed. |
| 3024 | */ |
| 3025 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3026 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3027 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3028 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3029 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3030 | return(NULL); |
| 3031 | } |
| 3032 | memcpy(buffer, buf, len); |
| 3033 | while (c != 0) { /* tested bigname2.xml */ |
| 3034 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3035 | xmlChar *tmp; |
| 3036 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3037 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3038 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3039 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3040 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3041 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3042 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3043 | return(NULL); |
| 3044 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3045 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3046 | } |
| 3047 | buffer[len++] = c; |
| 3048 | c = *cur++; |
| 3049 | } |
| 3050 | buffer[len] = 0; |
| 3051 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3052 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3053 | if (buffer == NULL) |
| 3054 | ret = xmlStrndup(buf, len); |
| 3055 | else { |
| 3056 | ret = buffer; |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | return(ret); |
| 3061 | } |
| 3062 | |
| 3063 | /************************************************************************ |
| 3064 | * * |
| 3065 | * The parser itself * |
| 3066 | * Relates to http://www.w3.org/TR/REC-xml * |
| 3067 | * * |
| 3068 | ************************************************************************/ |
| 3069 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3070 | /************************************************************************ |
| 3071 | * * |
| 3072 | * Routines to parse Name, NCName and NmToken * |
| 3073 | * * |
| 3074 | ************************************************************************/ |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3075 | #ifdef DEBUG |
| 3076 | static unsigned long nbParseName = 0; |
| 3077 | static unsigned long nbParseNmToken = 0; |
| 3078 | static unsigned long nbParseNCName = 0; |
| 3079 | static unsigned long nbParseNCNameComplex = 0; |
| 3080 | static unsigned long nbParseNameComplex = 0; |
| 3081 | static unsigned long nbParseStringName = 0; |
| 3082 | #endif |
| 3083 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3084 | /* |
| 3085 | * The two following functions are related to the change of accepted |
| 3086 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 3087 | * They correspond to the modified production [4] and the new production [4a] |
| 3088 | * changes in that revision. Also note that the macros used for the |
| 3089 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 3090 | * anymore. |
| 3091 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 3092 | * new XML_PARSE_OLD10 option is given to the parser. |
| 3093 | */ |
| 3094 | static int |
| 3095 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 3096 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3097 | /* |
| 3098 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3099 | * Update 5 of XML-1.0 |
| 3100 | */ |
| 3101 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3102 | (((c >= 'a') && (c <= 'z')) || |
| 3103 | ((c >= 'A') && (c <= 'Z')) || |
| 3104 | (c == '_') || (c == ':') || |
| 3105 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3106 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3107 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3108 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3109 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3110 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3111 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3112 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3113 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3114 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3115 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3116 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3117 | return(1); |
| 3118 | } else { |
| 3119 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 3120 | return(1); |
| 3121 | } |
| 3122 | return(0); |
| 3123 | } |
| 3124 | |
| 3125 | static int |
| 3126 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 3127 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3128 | /* |
| 3129 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3130 | * Update 5 of XML-1.0 |
| 3131 | */ |
| 3132 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3133 | (((c >= 'a') && (c <= 'z')) || |
| 3134 | ((c >= 'A') && (c <= 'Z')) || |
| 3135 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3136 | (c == '_') || (c == ':') || |
| 3137 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3138 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3139 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3140 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3141 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3142 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3143 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3144 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3145 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3146 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3147 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3148 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3149 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3150 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3151 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 3152 | return(1); |
| 3153 | } else { |
| 3154 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3155 | (c == '.') || (c == '-') || |
| 3156 | (c == '_') || (c == ':') || |
| 3157 | (IS_COMBINING(c)) || |
| 3158 | (IS_EXTENDER(c))) |
| 3159 | return(1); |
| 3160 | } |
| 3161 | return(0); |
| 3162 | } |
| 3163 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3164 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3165 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3166 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3167 | static const xmlChar * |
| 3168 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3169 | int len = 0, l; |
| 3170 | int c; |
| 3171 | int count = 0; |
| 3172 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3173 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3174 | nbParseNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3175 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3176 | |
| 3177 | /* |
| 3178 | * Handler for more complex cases |
| 3179 | */ |
| 3180 | GROW; |
| 3181 | c = CUR_CHAR(l); |
| 3182 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3183 | /* |
| 3184 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3185 | * Update 5 of XML-1.0 |
| 3186 | */ |
| 3187 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3188 | (!(((c >= 'a') && (c <= 'z')) || |
| 3189 | ((c >= 'A') && (c <= 'Z')) || |
| 3190 | (c == '_') || (c == ':') || |
| 3191 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3192 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3193 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3194 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3195 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3196 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3197 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3198 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3199 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3200 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3201 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3202 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3203 | return(NULL); |
| 3204 | } |
| 3205 | len += l; |
| 3206 | NEXTL(l); |
| 3207 | c = CUR_CHAR(l); |
| 3208 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3209 | (((c >= 'a') && (c <= 'z')) || |
| 3210 | ((c >= 'A') && (c <= 'Z')) || |
| 3211 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3212 | (c == '_') || (c == ':') || |
| 3213 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3214 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3215 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3216 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3217 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3218 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3219 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3220 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3221 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3222 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3223 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3224 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3225 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3226 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3227 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3228 | )) { |
| 3229 | if (count++ > 100) { |
| 3230 | count = 0; |
| 3231 | GROW; |
| 3232 | } |
| 3233 | len += l; |
| 3234 | NEXTL(l); |
| 3235 | c = CUR_CHAR(l); |
| 3236 | } |
| 3237 | } else { |
| 3238 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3239 | (!IS_LETTER(c) && (c != '_') && |
| 3240 | (c != ':'))) { |
| 3241 | return(NULL); |
| 3242 | } |
| 3243 | len += l; |
| 3244 | NEXTL(l); |
| 3245 | c = CUR_CHAR(l); |
| 3246 | |
| 3247 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3248 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3249 | (c == '.') || (c == '-') || |
| 3250 | (c == '_') || (c == ':') || |
| 3251 | (IS_COMBINING(c)) || |
| 3252 | (IS_EXTENDER(c)))) { |
| 3253 | if (count++ > 100) { |
| 3254 | count = 0; |
| 3255 | GROW; |
| 3256 | } |
| 3257 | len += l; |
| 3258 | NEXTL(l); |
| 3259 | c = CUR_CHAR(l); |
| 3260 | } |
| 3261 | } |
| 3262 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3263 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3264 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3265 | } |
| 3266 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3267 | /** |
| 3268 | * xmlParseName: |
| 3269 | * @ctxt: an XML parser context |
| 3270 | * |
| 3271 | * parse an XML name. |
| 3272 | * |
| 3273 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3274 | * CombiningChar | Extender |
| 3275 | * |
| 3276 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3277 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3278 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3279 | * |
| 3280 | * Returns the Name parsed or NULL |
| 3281 | */ |
| 3282 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3283 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3284 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3285 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3286 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3287 | int count = 0; |
| 3288 | |
| 3289 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3290 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3291 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3292 | nbParseName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3293 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3294 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3295 | /* |
| 3296 | * Accelerator for simple ASCII names |
| 3297 | */ |
| 3298 | in = ctxt->input->cur; |
| 3299 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3300 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3301 | (*in == '_') || (*in == ':')) { |
| 3302 | in++; |
| 3303 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3304 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3305 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3306 | (*in == '_') || (*in == '-') || |
| 3307 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3308 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3309 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3310 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3311 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3312 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3313 | ctxt->nbChars += count; |
| 3314 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3315 | if (ret == NULL) |
| 3316 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3317 | return(ret); |
| 3318 | } |
| 3319 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3320 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3321 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3322 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3323 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3324 | static const xmlChar * |
| 3325 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3326 | int len = 0, l; |
| 3327 | int c; |
| 3328 | int count = 0; |
| 3329 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3330 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3331 | nbParseNCNameComplex++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3332 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3333 | |
| 3334 | /* |
| 3335 | * Handler for more complex cases |
| 3336 | */ |
| 3337 | GROW; |
| 3338 | c = CUR_CHAR(l); |
| 3339 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3340 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3341 | return(NULL); |
| 3342 | } |
| 3343 | |
| 3344 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3345 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
| 3346 | if (count++ > 100) { |
| 3347 | count = 0; |
| 3348 | GROW; |
| 3349 | } |
| 3350 | len += l; |
| 3351 | NEXTL(l); |
| 3352 | c = CUR_CHAR(l); |
| 3353 | } |
| 3354 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3355 | } |
| 3356 | |
| 3357 | /** |
| 3358 | * xmlParseNCName: |
| 3359 | * @ctxt: an XML parser context |
| 3360 | * @len: lenght of the string parsed |
| 3361 | * |
| 3362 | * parse an XML name. |
| 3363 | * |
| 3364 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3365 | * CombiningChar | Extender |
| 3366 | * |
| 3367 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3368 | * |
| 3369 | * Returns the Name parsed or NULL |
| 3370 | */ |
| 3371 | |
| 3372 | static const xmlChar * |
| 3373 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 3374 | const xmlChar *in; |
| 3375 | const xmlChar *ret; |
| 3376 | int count = 0; |
| 3377 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3378 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3379 | nbParseNCName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3380 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3381 | |
| 3382 | /* |
| 3383 | * Accelerator for simple ASCII names |
| 3384 | */ |
| 3385 | in = ctxt->input->cur; |
| 3386 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3387 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3388 | (*in == '_')) { |
| 3389 | in++; |
| 3390 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3391 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3392 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3393 | (*in == '_') || (*in == '-') || |
| 3394 | (*in == '.')) |
| 3395 | in++; |
| 3396 | if ((*in > 0) && (*in < 0x80)) { |
| 3397 | count = in - ctxt->input->cur; |
| 3398 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3399 | ctxt->input->cur = in; |
| 3400 | ctxt->nbChars += count; |
| 3401 | ctxt->input->col += count; |
| 3402 | if (ret == NULL) { |
| 3403 | xmlErrMemory(ctxt, NULL); |
| 3404 | } |
| 3405 | return(ret); |
| 3406 | } |
| 3407 | } |
| 3408 | return(xmlParseNCNameComplex(ctxt)); |
| 3409 | } |
| 3410 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3411 | /** |
| 3412 | * xmlParseNameAndCompare: |
| 3413 | * @ctxt: an XML parser context |
| 3414 | * |
| 3415 | * parse an XML name and compares for match |
| 3416 | * (specialized for endtag parsing) |
| 3417 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3418 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3419 | * and the name for mismatch |
| 3420 | */ |
| 3421 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3422 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3423 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3424 | register const xmlChar *cmp = other; |
| 3425 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3426 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3427 | |
| 3428 | GROW; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3429 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3430 | in = ctxt->input->cur; |
| 3431 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3432 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3433 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3434 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3435 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3436 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3437 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3438 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3439 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3440 | } |
| 3441 | /* failure (or end of input buffer), check with full function */ |
| 3442 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3443 | /* strings coming from the dictionnary direct compare possible */ |
| 3444 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3445 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3446 | } |
| 3447 | return ret; |
| 3448 | } |
| 3449 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3450 | /** |
| 3451 | * xmlParseStringName: |
| 3452 | * @ctxt: an XML parser context |
| 3453 | * @str: a pointer to the string pointer (IN/OUT) |
| 3454 | * |
| 3455 | * parse an XML name. |
| 3456 | * |
| 3457 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3458 | * CombiningChar | Extender |
| 3459 | * |
| 3460 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3461 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3462 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3463 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3464 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3465 | * is updated to the current location in the string. |
| 3466 | */ |
| 3467 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3468 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3469 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3470 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3471 | const xmlChar *cur = *str; |
| 3472 | int len = 0, l; |
| 3473 | int c; |
| 3474 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3475 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3476 | nbParseStringName++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3477 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3478 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3479 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3480 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3481 | return(NULL); |
| 3482 | } |
| 3483 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3484 | COPY_BUF(l,buf,len,c); |
| 3485 | cur += l; |
| 3486 | c = CUR_SCHAR(cur, l); |
| 3487 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3488 | COPY_BUF(l,buf,len,c); |
| 3489 | cur += l; |
| 3490 | c = CUR_SCHAR(cur, l); |
| 3491 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3492 | /* |
| 3493 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3494 | * for the processing speed. |
| 3495 | */ |
| 3496 | xmlChar *buffer; |
| 3497 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3498 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3499 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3500 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3501 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3502 | return(NULL); |
| 3503 | } |
| 3504 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3505 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3506 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3507 | xmlChar *tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3508 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3509 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3510 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3511 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3512 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3513 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3514 | return(NULL); |
| 3515 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3516 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3517 | } |
| 3518 | COPY_BUF(l,buffer,len,c); |
| 3519 | cur += l; |
| 3520 | c = CUR_SCHAR(cur, l); |
| 3521 | } |
| 3522 | buffer[len] = 0; |
| 3523 | *str = cur; |
| 3524 | return(buffer); |
| 3525 | } |
| 3526 | } |
| 3527 | *str = cur; |
| 3528 | return(xmlStrndup(buf, len)); |
| 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | * xmlParseNmtoken: |
| 3533 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3534 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3535 | * parse an XML Nmtoken. |
| 3536 | * |
| 3537 | * [7] Nmtoken ::= (NameChar)+ |
| 3538 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3539 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3540 | * |
| 3541 | * Returns the Nmtoken parsed or NULL |
| 3542 | */ |
| 3543 | |
| 3544 | xmlChar * |
| 3545 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3546 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3547 | int len = 0, l; |
| 3548 | int c; |
| 3549 | int count = 0; |
| 3550 | |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3551 | #ifdef DEBUG |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3552 | nbParseNmToken++; |
Daniel Veillard | c656146 | 2009-03-25 10:22:31 +0000 | [diff] [blame] | 3553 | #endif |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3554 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3555 | GROW; |
| 3556 | c = CUR_CHAR(l); |
| 3557 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3558 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | if (count++ > 100) { |
| 3560 | count = 0; |
| 3561 | GROW; |
| 3562 | } |
| 3563 | COPY_BUF(l,buf,len,c); |
| 3564 | NEXTL(l); |
| 3565 | c = CUR_CHAR(l); |
| 3566 | if (len >= XML_MAX_NAMELEN) { |
| 3567 | /* |
| 3568 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3569 | * for the processing speed. |
| 3570 | */ |
| 3571 | xmlChar *buffer; |
| 3572 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3573 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3574 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3575 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3576 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3577 | return(NULL); |
| 3578 | } |
| 3579 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3580 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3581 | if (count++ > 100) { |
| 3582 | count = 0; |
| 3583 | GROW; |
| 3584 | } |
| 3585 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3586 | xmlChar *tmp; |
| 3587 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3588 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3589 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3590 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3591 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3592 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3593 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3594 | return(NULL); |
| 3595 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3596 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3597 | } |
| 3598 | COPY_BUF(l,buffer,len,c); |
| 3599 | NEXTL(l); |
| 3600 | c = CUR_CHAR(l); |
| 3601 | } |
| 3602 | buffer[len] = 0; |
| 3603 | return(buffer); |
| 3604 | } |
| 3605 | } |
| 3606 | if (len == 0) |
| 3607 | return(NULL); |
| 3608 | return(xmlStrndup(buf, len)); |
| 3609 | } |
| 3610 | |
| 3611 | /** |
| 3612 | * xmlParseEntityValue: |
| 3613 | * @ctxt: an XML parser context |
| 3614 | * @orig: if non-NULL store a copy of the original entity value |
| 3615 | * |
| 3616 | * parse a value for ENTITY declarations |
| 3617 | * |
| 3618 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3619 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3620 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3621 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3622 | */ |
| 3623 | |
| 3624 | xmlChar * |
| 3625 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3626 | xmlChar *buf = NULL; |
| 3627 | int len = 0; |
| 3628 | int size = XML_PARSER_BUFFER_SIZE; |
| 3629 | int c, l; |
| 3630 | xmlChar stop; |
| 3631 | xmlChar *ret = NULL; |
| 3632 | const xmlChar *cur = NULL; |
| 3633 | xmlParserInputPtr input; |
| 3634 | |
| 3635 | if (RAW == '"') stop = '"'; |
| 3636 | else if (RAW == '\'') stop = '\''; |
| 3637 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3638 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3639 | return(NULL); |
| 3640 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3641 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3642 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3643 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3644 | return(NULL); |
| 3645 | } |
| 3646 | |
| 3647 | /* |
| 3648 | * The content of the entity definition is copied in a buffer. |
| 3649 | */ |
| 3650 | |
| 3651 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3652 | input = ctxt->input; |
| 3653 | GROW; |
| 3654 | NEXT; |
| 3655 | c = CUR_CHAR(l); |
| 3656 | /* |
| 3657 | * NOTE: 4.4.5 Included in Literal |
| 3658 | * When a parameter entity reference appears in a literal entity |
| 3659 | * value, ... a single or double quote character in the replacement |
| 3660 | * text is always treated as a normal data character and will not |
| 3661 | * terminate the literal. |
| 3662 | * In practice it means we stop the loop only when back at parsing |
| 3663 | * the initial entity and the quote is found |
| 3664 | */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3665 | while ((IS_CHAR(c)) && ((c != stop) || /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3666 | (ctxt->input != input))) { |
| 3667 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3668 | xmlChar *tmp; |
| 3669 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3670 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3671 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3672 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3673 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3674 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3675 | return(NULL); |
| 3676 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3677 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3678 | } |
| 3679 | COPY_BUF(l,buf,len,c); |
| 3680 | NEXTL(l); |
| 3681 | /* |
| 3682 | * Pop-up of finished entities. |
| 3683 | */ |
| 3684 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3685 | xmlPopInput(ctxt); |
| 3686 | |
| 3687 | GROW; |
| 3688 | c = CUR_CHAR(l); |
| 3689 | if (c == 0) { |
| 3690 | GROW; |
| 3691 | c = CUR_CHAR(l); |
| 3692 | } |
| 3693 | } |
| 3694 | buf[len] = 0; |
| 3695 | |
| 3696 | /* |
| 3697 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3698 | * reference constructs. Note Charref will be handled in |
| 3699 | * xmlStringDecodeEntities() |
| 3700 | */ |
| 3701 | cur = buf; |
| 3702 | while (*cur != 0) { /* non input consuming */ |
| 3703 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3704 | xmlChar *name; |
| 3705 | xmlChar tmp = *cur; |
| 3706 | |
| 3707 | cur++; |
| 3708 | name = xmlParseStringName(ctxt, &cur); |
| 3709 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3710 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3711 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3712 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3713 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3714 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3715 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3716 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3717 | } |
| 3718 | if (name != NULL) |
| 3719 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3720 | if (*cur == 0) |
| 3721 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3722 | } |
| 3723 | cur++; |
| 3724 | } |
| 3725 | |
| 3726 | /* |
| 3727 | * Then PEReference entities are substituted. |
| 3728 | */ |
| 3729 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3730 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | xmlFree(buf); |
| 3732 | } else { |
| 3733 | NEXT; |
| 3734 | /* |
| 3735 | * NOTE: 4.4.7 Bypassed |
| 3736 | * When a general entity reference appears in the EntityValue in |
| 3737 | * an entity declaration, it is bypassed and left as is. |
| 3738 | * so XML_SUBSTITUTE_REF is not set here. |
| 3739 | */ |
| 3740 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3741 | 0, 0, 0); |
| 3742 | if (orig != NULL) |
| 3743 | *orig = buf; |
| 3744 | else |
| 3745 | xmlFree(buf); |
| 3746 | } |
| 3747 | |
| 3748 | return(ret); |
| 3749 | } |
| 3750 | |
| 3751 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3752 | * xmlParseAttValueComplex: |
| 3753 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3754 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3755 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3756 | * |
| 3757 | * parse a value for an attribute, this is the fallback function |
| 3758 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3759 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3760 | * |
| 3761 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3762 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3763 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3764 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3765 | xmlChar limit = 0; |
| 3766 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3767 | xmlChar *rep = NULL; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3768 | size_t len = 0; |
| 3769 | size_t buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3770 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3771 | xmlChar *current = NULL; |
| 3772 | xmlEntityPtr ent; |
| 3773 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3774 | if (NXT(0) == '"') { |
| 3775 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3776 | limit = '"'; |
| 3777 | NEXT; |
| 3778 | } else if (NXT(0) == '\'') { |
| 3779 | limit = '\''; |
| 3780 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3781 | NEXT; |
| 3782 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3783 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3784 | return(NULL); |
| 3785 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3786 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3787 | /* |
| 3788 | * allocate a translation buffer. |
| 3789 | */ |
| 3790 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3791 | buf = (xmlChar *) xmlMallocAtomic(buf_size); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3792 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3793 | |
| 3794 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3795 | * 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] | 3796 | */ |
| 3797 | c = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3798 | while ((NXT(0) != limit) && /* checked */ |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 3799 | (IS_CHAR(c)) && (c != '<')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3800 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3801 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3802 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3803 | if (NXT(1) == '#') { |
| 3804 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3805 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3807 | if (ctxt->replaceEntities) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3808 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3809 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3810 | } |
| 3811 | buf[len++] = '&'; |
| 3812 | } else { |
| 3813 | /* |
| 3814 | * The reparsing will be done in xmlStringGetNodeList() |
| 3815 | * called by the attribute() function in SAX.c |
| 3816 | */ |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3817 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3818 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3819 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3820 | buf[len++] = '&'; |
| 3821 | buf[len++] = '#'; |
| 3822 | buf[len++] = '3'; |
| 3823 | buf[len++] = '8'; |
| 3824 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3825 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 3826 | } else if (val != 0) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3827 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3828 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3829 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3830 | len += xmlCopyChar(0, &buf[len], val); |
| 3831 | } |
| 3832 | } else { |
| 3833 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 3834 | ctxt->nbentities++; |
| 3835 | if (ent != NULL) |
| 3836 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3837 | if ((ent != NULL) && |
| 3838 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3839 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3840 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3841 | } |
| 3842 | if ((ctxt->replaceEntities == 0) && |
| 3843 | (ent->content[0] == '&')) { |
| 3844 | buf[len++] = '&'; |
| 3845 | buf[len++] = '#'; |
| 3846 | buf[len++] = '3'; |
| 3847 | buf[len++] = '8'; |
| 3848 | buf[len++] = ';'; |
| 3849 | } else { |
| 3850 | buf[len++] = ent->content[0]; |
| 3851 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3852 | } else if ((ent != NULL) && |
| 3853 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3854 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 3855 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3856 | XML_SUBSTITUTE_REF, |
| 3857 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3858 | if (rep != NULL) { |
| 3859 | current = rep; |
| 3860 | while (*current != 0) { /* non input consuming */ |
Daniel Veillard | 283d502 | 2009-08-25 17:18:39 +0200 | [diff] [blame] | 3861 | if ((*current == 0xD) || (*current == 0xA) || |
| 3862 | (*current == 0x9)) { |
| 3863 | buf[len++] = 0x20; |
| 3864 | current++; |
| 3865 | } else |
| 3866 | buf[len++] = *current++; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3867 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3868 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3869 | } |
| 3870 | } |
| 3871 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3872 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3873 | } |
| 3874 | } else { |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3875 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3876 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3877 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3878 | if (ent->content != NULL) |
| 3879 | buf[len++] = ent->content[0]; |
| 3880 | } |
| 3881 | } else if (ent != NULL) { |
| 3882 | int i = xmlStrlen(ent->name); |
| 3883 | const xmlChar *cur = ent->name; |
| 3884 | |
| 3885 | /* |
| 3886 | * This may look absurd but is needed to detect |
| 3887 | * entities problems |
| 3888 | */ |
| 3889 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 3890 | (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3892 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 3893 | if (rep != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3894 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3895 | rep = NULL; |
| 3896 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | /* |
| 3900 | * Just output the reference |
| 3901 | */ |
| 3902 | buf[len++] = '&'; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3903 | while (len + i + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3904 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3905 | } |
| 3906 | for (;i > 0;i--) |
| 3907 | buf[len++] = *cur++; |
| 3908 | buf[len++] = ';'; |
| 3909 | } |
| 3910 | } |
| 3911 | } else { |
| 3912 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3913 | if ((len != 0) || (!normalize)) { |
| 3914 | if ((!normalize) || (!in_space)) { |
| 3915 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3916 | while (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3917 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3921 | } |
| 3922 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3923 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3924 | COPY_BUF(l,buf,len,c); |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3925 | if (len + 10 > buf_size) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3926 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | NEXTL(l); |
| 3930 | } |
| 3931 | GROW; |
| 3932 | c = CUR_CHAR(l); |
| 3933 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3934 | if ((in_space) && (normalize)) { |
| 3935 | while (buf[len - 1] == 0x20) len--; |
| 3936 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3937 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3939 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 3941 | if ((c != 0) && (!IS_CHAR(c))) { |
| 3942 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 3943 | "invalid character in attribute value\n"); |
| 3944 | } else { |
| 3945 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 3946 | "AttValue: ' expected\n"); |
| 3947 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3948 | } else |
| 3949 | NEXT; |
Daniel Veillard | 459eeb9 | 2012-07-17 16:19:17 +0800 | [diff] [blame] | 3950 | |
| 3951 | /* |
| 3952 | * There we potentially risk an overflow, don't allow attribute value of |
| 3953 | * lenght more than INT_MAX it is a very reasonnable assumption ! |
| 3954 | */ |
| 3955 | if (len >= INT_MAX) { |
| 3956 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 3957 | "AttValue lenght too long\n"); |
| 3958 | goto mem_error; |
| 3959 | } |
| 3960 | |
| 3961 | if (attlen != NULL) *attlen = (int) len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3962 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3963 | |
| 3964 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3965 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3966 | if (buf != NULL) |
| 3967 | xmlFree(buf); |
| 3968 | if (rep != NULL) |
| 3969 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3970 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3974 | * xmlParseAttValue: |
| 3975 | * @ctxt: an XML parser context |
| 3976 | * |
| 3977 | * parse a value for an attribute |
| 3978 | * Note: the parser won't do substitution of entities here, this |
| 3979 | * will be handled later in xmlStringGetNodeList |
| 3980 | * |
| 3981 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 3982 | * "'" ([^<&'] | Reference)* "'" |
| 3983 | * |
| 3984 | * 3.3.3 Attribute-Value Normalization: |
| 3985 | * Before the value of an attribute is passed to the application or |
| 3986 | * checked for validity, the XML processor must normalize it as follows: |
| 3987 | * - a character reference is processed by appending the referenced |
| 3988 | * character to the attribute value |
| 3989 | * - an entity reference is processed by recursively processing the |
| 3990 | * replacement text of the entity |
| 3991 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 3992 | * appending #x20 to the normalized value, except that only a single |
| 3993 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 3994 | * parsed entity or the literal entity value of an internal parsed entity |
| 3995 | * - other characters are processed by appending them to the normalized value |
| 3996 | * If the declared value is not CDATA, then the XML processor must further |
| 3997 | * process the normalized attribute value by discarding any leading and |
| 3998 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3999 | * (#x20) characters by a single space (#x20) character. |
| 4000 | * All attributes for which no declaration has been read should be treated |
| 4001 | * by a non-validating parser as if declared CDATA. |
| 4002 | * |
| 4003 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 4004 | */ |
| 4005 | |
| 4006 | |
| 4007 | xmlChar * |
| 4008 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 4009 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4010 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
| 4013 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4014 | * xmlParseSystemLiteral: |
| 4015 | * @ctxt: an XML parser context |
| 4016 | * |
| 4017 | * parse an XML Literal |
| 4018 | * |
| 4019 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 4020 | * |
| 4021 | * Returns the SystemLiteral parsed or NULL |
| 4022 | */ |
| 4023 | |
| 4024 | xmlChar * |
| 4025 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 4026 | xmlChar *buf = NULL; |
| 4027 | int len = 0; |
| 4028 | int size = XML_PARSER_BUFFER_SIZE; |
| 4029 | int cur, l; |
| 4030 | xmlChar stop; |
| 4031 | int state = ctxt->instate; |
| 4032 | int count = 0; |
| 4033 | |
| 4034 | SHRINK; |
| 4035 | if (RAW == '"') { |
| 4036 | NEXT; |
| 4037 | stop = '"'; |
| 4038 | } else if (RAW == '\'') { |
| 4039 | NEXT; |
| 4040 | stop = '\''; |
| 4041 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4042 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4043 | return(NULL); |
| 4044 | } |
| 4045 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4046 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4047 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4048 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4049 | return(NULL); |
| 4050 | } |
| 4051 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 4052 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4053 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4054 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4055 | xmlChar *tmp; |
| 4056 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4057 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4058 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4059 | if (tmp == NULL) { |
| 4060 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4061 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4062 | ctxt->instate = (xmlParserInputState) state; |
| 4063 | return(NULL); |
| 4064 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4065 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4066 | } |
| 4067 | count++; |
| 4068 | if (count > 50) { |
| 4069 | GROW; |
| 4070 | count = 0; |
| 4071 | } |
| 4072 | COPY_BUF(l,buf,len,cur); |
| 4073 | NEXTL(l); |
| 4074 | cur = CUR_CHAR(l); |
| 4075 | if (cur == 0) { |
| 4076 | GROW; |
| 4077 | SHRINK; |
| 4078 | cur = CUR_CHAR(l); |
| 4079 | } |
| 4080 | } |
| 4081 | buf[len] = 0; |
| 4082 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4083 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4084 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4085 | } else { |
| 4086 | NEXT; |
| 4087 | } |
| 4088 | return(buf); |
| 4089 | } |
| 4090 | |
| 4091 | /** |
| 4092 | * xmlParsePubidLiteral: |
| 4093 | * @ctxt: an XML parser context |
| 4094 | * |
| 4095 | * parse an XML public literal |
| 4096 | * |
| 4097 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 4098 | * |
| 4099 | * Returns the PubidLiteral parsed or NULL. |
| 4100 | */ |
| 4101 | |
| 4102 | xmlChar * |
| 4103 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 4104 | xmlChar *buf = NULL; |
| 4105 | int len = 0; |
| 4106 | int size = XML_PARSER_BUFFER_SIZE; |
| 4107 | xmlChar cur; |
| 4108 | xmlChar stop; |
| 4109 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4110 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4111 | |
| 4112 | SHRINK; |
| 4113 | if (RAW == '"') { |
| 4114 | NEXT; |
| 4115 | stop = '"'; |
| 4116 | } else if (RAW == '\'') { |
| 4117 | NEXT; |
| 4118 | stop = '\''; |
| 4119 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4120 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4121 | return(NULL); |
| 4122 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4123 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4124 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4125 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4126 | return(NULL); |
| 4127 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4128 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4129 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4130 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4131 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4132 | xmlChar *tmp; |
| 4133 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4134 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4135 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4136 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4137 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4138 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4139 | return(NULL); |
| 4140 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4141 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4142 | } |
| 4143 | buf[len++] = cur; |
| 4144 | count++; |
| 4145 | if (count > 50) { |
| 4146 | GROW; |
| 4147 | count = 0; |
| 4148 | } |
| 4149 | NEXT; |
| 4150 | cur = CUR; |
| 4151 | if (cur == 0) { |
| 4152 | GROW; |
| 4153 | SHRINK; |
| 4154 | cur = CUR; |
| 4155 | } |
| 4156 | } |
| 4157 | buf[len] = 0; |
| 4158 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4159 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4160 | } else { |
| 4161 | NEXT; |
| 4162 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4163 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4164 | return(buf); |
| 4165 | } |
| 4166 | |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4167 | static void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * used for the test in the inner loop of the char data testing |
| 4171 | */ |
| 4172 | static const unsigned char test_char_data[256] = { |
| 4173 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4174 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 4175 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4176 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4177 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 4178 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 4179 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 4180 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 4181 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 4182 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 4183 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 4184 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 4185 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4186 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4187 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4188 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4189 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4190 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4191 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4192 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4193 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4194 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4195 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4196 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4197 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4198 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4199 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4200 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4201 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4202 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4203 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4204 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4205 | }; |
| 4206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4207 | /** |
| 4208 | * xmlParseCharData: |
| 4209 | * @ctxt: an XML parser context |
| 4210 | * @cdata: int indicating whether we are within a CDATA section |
| 4211 | * |
| 4212 | * parse a CharData section. |
| 4213 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4214 | * |
| 4215 | * The right angle bracket (>) may be represented using the string ">", |
| 4216 | * and must, for compatibility, be escaped using ">" or a character |
| 4217 | * reference when it appears in the string "]]>" in content, when that |
| 4218 | * string is not marking the end of a CDATA section. |
| 4219 | * |
| 4220 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4221 | */ |
| 4222 | |
| 4223 | void |
| 4224 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4225 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4226 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4227 | int line = ctxt->input->line; |
| 4228 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4229 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4230 | |
| 4231 | SHRINK; |
| 4232 | GROW; |
| 4233 | /* |
| 4234 | * Accelerated common case where input don't need to be |
| 4235 | * modified before passing it to the handler. |
| 4236 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4237 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4238 | in = ctxt->input->cur; |
| 4239 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4240 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4241 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4242 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4243 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4244 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4245 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4246 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4247 | goto get_more_space; |
| 4248 | } |
| 4249 | if (*in == '<') { |
| 4250 | nbchar = in - ctxt->input->cur; |
| 4251 | if (nbchar > 0) { |
| 4252 | const xmlChar *tmp = ctxt->input->cur; |
| 4253 | ctxt->input->cur = in; |
| 4254 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4255 | if ((ctxt->sax != NULL) && |
| 4256 | (ctxt->sax->ignorableWhitespace != |
| 4257 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4258 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4259 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4260 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4261 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4262 | } else { |
| 4263 | if (ctxt->sax->characters != NULL) |
| 4264 | ctxt->sax->characters(ctxt->userData, |
| 4265 | tmp, nbchar); |
| 4266 | if (*ctxt->space == -1) |
| 4267 | *ctxt->space = -2; |
| 4268 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4269 | } else if ((ctxt->sax != NULL) && |
| 4270 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4271 | ctxt->sax->characters(ctxt->userData, |
| 4272 | tmp, nbchar); |
| 4273 | } |
| 4274 | } |
| 4275 | return; |
| 4276 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4277 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4278 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4279 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4280 | while (test_char_data[*in]) { |
| 4281 | in++; |
| 4282 | ccol++; |
| 4283 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4284 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4285 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4286 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4287 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4288 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4289 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4290 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4291 | } |
| 4292 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4293 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4294 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4295 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4296 | return; |
| 4297 | } |
| 4298 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4299 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4300 | goto get_more; |
| 4301 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4302 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4303 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4304 | if ((ctxt->sax != NULL) && |
| 4305 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4306 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4307 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4308 | const xmlChar *tmp = ctxt->input->cur; |
| 4309 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4310 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4311 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4312 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4313 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4314 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4315 | } else { |
| 4316 | if (ctxt->sax->characters != NULL) |
| 4317 | ctxt->sax->characters(ctxt->userData, |
| 4318 | tmp, nbchar); |
| 4319 | if (*ctxt->space == -1) |
| 4320 | *ctxt->space = -2; |
| 4321 | } |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4322 | line = ctxt->input->line; |
| 4323 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4324 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4325 | if (ctxt->sax->characters != NULL) |
| 4326 | ctxt->sax->characters(ctxt->userData, |
| 4327 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4328 | line = ctxt->input->line; |
| 4329 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4330 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4331 | /* something really bad happened in the SAX callback */ |
| 4332 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4333 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4334 | } |
| 4335 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4336 | if (*in == 0xD) { |
| 4337 | in++; |
| 4338 | if (*in == 0xA) { |
| 4339 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4340 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4341 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4342 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4343 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4344 | in--; |
| 4345 | } |
| 4346 | if (*in == '<') { |
| 4347 | return; |
| 4348 | } |
| 4349 | if (*in == '&') { |
| 4350 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4351 | } |
| 4352 | SHRINK; |
| 4353 | GROW; |
| 4354 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4355 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4356 | nbchar = 0; |
| 4357 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4358 | ctxt->input->line = line; |
| 4359 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4360 | xmlParseCharDataComplex(ctxt, cdata); |
| 4361 | } |
| 4362 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4363 | /** |
| 4364 | * xmlParseCharDataComplex: |
| 4365 | * @ctxt: an XML parser context |
| 4366 | * @cdata: int indicating whether we are within a CDATA section |
| 4367 | * |
| 4368 | * parse a CharData section.this is the fallback function |
| 4369 | * of xmlParseCharData() when the parsing requires handling |
| 4370 | * of non-ASCII characters. |
| 4371 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 4372 | static void |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4373 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4374 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4375 | int nbchar = 0; |
| 4376 | int cur, l; |
| 4377 | int count = 0; |
| 4378 | |
| 4379 | SHRINK; |
| 4380 | GROW; |
| 4381 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4382 | while ((cur != '<') && /* checked */ |
| 4383 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4384 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4385 | if ((cur == ']') && (NXT(1) == ']') && |
| 4386 | (NXT(2) == '>')) { |
| 4387 | if (cdata) break; |
| 4388 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4389 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | COPY_BUF(l,buf,nbchar,cur); |
| 4393 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4394 | buf[nbchar] = 0; |
| 4395 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4396 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4397 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4398 | */ |
| 4399 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4400 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4401 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4402 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4403 | buf, nbchar); |
| 4404 | } else { |
| 4405 | if (ctxt->sax->characters != NULL) |
| 4406 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4407 | if ((ctxt->sax->characters != |
| 4408 | ctxt->sax->ignorableWhitespace) && |
| 4409 | (*ctxt->space == -1)) |
| 4410 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4411 | } |
| 4412 | } |
| 4413 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4414 | /* something really bad happened in the SAX callback */ |
| 4415 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4416 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4417 | } |
| 4418 | count++; |
| 4419 | if (count > 50) { |
| 4420 | GROW; |
| 4421 | count = 0; |
| 4422 | } |
| 4423 | NEXTL(l); |
| 4424 | cur = CUR_CHAR(l); |
| 4425 | } |
| 4426 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4427 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4428 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4429 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4430 | */ |
| 4431 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4432 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4433 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4434 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4435 | } else { |
| 4436 | if (ctxt->sax->characters != NULL) |
| 4437 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4438 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4439 | (*ctxt->space == -1)) |
| 4440 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4441 | } |
| 4442 | } |
| 4443 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4444 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4445 | /* Generate the error and skip the offending character */ |
| 4446 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4447 | "PCDATA invalid Char value %d\n", |
| 4448 | cur); |
| 4449 | NEXTL(l); |
| 4450 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | /** |
| 4454 | * xmlParseExternalID: |
| 4455 | * @ctxt: an XML parser context |
| 4456 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4457 | * @strict: indicate whether we should restrict parsing to only |
| 4458 | * production [75], see NOTE below |
| 4459 | * |
| 4460 | * Parse an External ID or a Public ID |
| 4461 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4462 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4463 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4464 | * |
| 4465 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4466 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4467 | * |
| 4468 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4469 | * |
| 4470 | * Returns the function returns SystemLiteral and in the second |
| 4471 | * case publicID receives PubidLiteral, is strict is off |
| 4472 | * it is possible to return NULL and have publicID set. |
| 4473 | */ |
| 4474 | |
| 4475 | xmlChar * |
| 4476 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4477 | xmlChar *URI = NULL; |
| 4478 | |
| 4479 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4480 | |
| 4481 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4482 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4483 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4484 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4485 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4486 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4487 | } |
| 4488 | SKIP_BLANKS; |
| 4489 | URI = xmlParseSystemLiteral(ctxt); |
| 4490 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4491 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4492 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4493 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4494 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4495 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4496 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4497 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4498 | } |
| 4499 | SKIP_BLANKS; |
| 4500 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4501 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4502 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4503 | } |
| 4504 | if (strict) { |
| 4505 | /* |
| 4506 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4507 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4508 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4509 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4510 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4511 | } |
| 4512 | } else { |
| 4513 | /* |
| 4514 | * We handle [83] so we return immediately, if |
| 4515 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4516 | * point of view that's a nice mess. |
| 4517 | */ |
| 4518 | const xmlChar *ptr; |
| 4519 | GROW; |
| 4520 | |
| 4521 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4522 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4523 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4524 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4525 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4526 | } |
| 4527 | SKIP_BLANKS; |
| 4528 | URI = xmlParseSystemLiteral(ctxt); |
| 4529 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4530 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4531 | } |
| 4532 | } |
| 4533 | return(URI); |
| 4534 | } |
| 4535 | |
| 4536 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4537 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4539 | * @buf: the already parsed part of the buffer |
| 4540 | * @len: number of bytes filles in the buffer |
| 4541 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4542 | * |
| 4543 | * Skip an XML (SGML) comment <!-- .... --> |
| 4544 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4545 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4546 | * 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] | 4547 | * |
| 4548 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4549 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4550 | static void |
| 4551 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4552 | int q, ql; |
| 4553 | int r, rl; |
| 4554 | int cur, l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4555 | int count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4556 | int inputid; |
| 4557 | |
| 4558 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4559 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4560 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4561 | len = 0; |
| 4562 | size = XML_PARSER_BUFFER_SIZE; |
| 4563 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4564 | if (buf == NULL) { |
| 4565 | xmlErrMemory(ctxt, NULL); |
| 4566 | return; |
| 4567 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4568 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4569 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4570 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4571 | if (q == 0) |
| 4572 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4573 | if (!IS_CHAR(q)) { |
| 4574 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4575 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4576 | q); |
| 4577 | xmlFree (buf); |
| 4578 | return; |
| 4579 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4580 | NEXTL(ql); |
| 4581 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4582 | if (r == 0) |
| 4583 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4584 | if (!IS_CHAR(r)) { |
| 4585 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4586 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4587 | q); |
| 4588 | xmlFree (buf); |
| 4589 | return; |
| 4590 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4591 | NEXTL(rl); |
| 4592 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4593 | if (cur == 0) |
| 4594 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4595 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4596 | ((cur != '>') || |
| 4597 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4598 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4599 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | } |
| 4601 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4602 | xmlChar *new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4603 | size *= 2; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4604 | new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4605 | if (new_buf == NULL) { |
| 4606 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4607 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4608 | return; |
| 4609 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4610 | buf = new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4611 | } |
| 4612 | COPY_BUF(ql,buf,len,q); |
| 4613 | q = r; |
| 4614 | ql = rl; |
| 4615 | r = cur; |
| 4616 | rl = l; |
| 4617 | |
| 4618 | count++; |
| 4619 | if (count > 50) { |
| 4620 | GROW; |
| 4621 | count = 0; |
| 4622 | } |
| 4623 | NEXTL(l); |
| 4624 | cur = CUR_CHAR(l); |
| 4625 | if (cur == 0) { |
| 4626 | SHRINK; |
| 4627 | GROW; |
| 4628 | cur = CUR_CHAR(l); |
| 4629 | } |
| 4630 | } |
| 4631 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4632 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4633 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4634 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4635 | } else if (!IS_CHAR(cur)) { |
| 4636 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4637 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4638 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4639 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4640 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4641 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4642 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4643 | } |
| 4644 | NEXT; |
| 4645 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4646 | (!ctxt->disableSAX)) |
| 4647 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4648 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4649 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4650 | return; |
| 4651 | not_terminated: |
| 4652 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4653 | "Comment not terminated\n", NULL); |
| 4654 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4655 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4656 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4657 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4658 | /** |
| 4659 | * xmlParseComment: |
| 4660 | * @ctxt: an XML parser context |
| 4661 | * |
| 4662 | * Skip an XML (SGML) comment <!-- .... --> |
| 4663 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4664 | * must not occur within comments. " |
| 4665 | * |
| 4666 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4667 | */ |
| 4668 | void |
| 4669 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4670 | xmlChar *buf = NULL; |
| 4671 | int size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 4672 | int len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4673 | xmlParserInputState state; |
| 4674 | const xmlChar *in; |
| 4675 | int nbchar = 0, ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4676 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4677 | |
| 4678 | /* |
| 4679 | * Check that there is a comment right here. |
| 4680 | */ |
| 4681 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4682 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4683 | state = ctxt->instate; |
| 4684 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4685 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4686 | SKIP(4); |
| 4687 | SHRINK; |
| 4688 | GROW; |
| 4689 | |
| 4690 | /* |
| 4691 | * Accelerated common case where input don't need to be |
| 4692 | * modified before passing it to the handler. |
| 4693 | */ |
| 4694 | in = ctxt->input->cur; |
| 4695 | do { |
| 4696 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4697 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4698 | ctxt->input->line++; ctxt->input->col = 1; |
| 4699 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4700 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4701 | } |
| 4702 | get_more: |
| 4703 | ccol = ctxt->input->col; |
| 4704 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4705 | ((*in >= 0x20) && (*in < '-')) || |
| 4706 | (*in == 0x09)) { |
| 4707 | in++; |
| 4708 | ccol++; |
| 4709 | } |
| 4710 | ctxt->input->col = ccol; |
| 4711 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4712 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4713 | ctxt->input->line++; ctxt->input->col = 1; |
| 4714 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4715 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4716 | goto get_more; |
| 4717 | } |
| 4718 | nbchar = in - ctxt->input->cur; |
| 4719 | /* |
| 4720 | * save current set of data |
| 4721 | */ |
| 4722 | if (nbchar > 0) { |
| 4723 | if ((ctxt->sax != NULL) && |
| 4724 | (ctxt->sax->comment != NULL)) { |
| 4725 | if (buf == NULL) { |
| 4726 | if ((*in == '-') && (in[1] == '-')) |
| 4727 | size = nbchar + 1; |
| 4728 | else |
| 4729 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4730 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4731 | if (buf == NULL) { |
| 4732 | xmlErrMemory(ctxt, NULL); |
| 4733 | ctxt->instate = state; |
| 4734 | return; |
| 4735 | } |
| 4736 | len = 0; |
| 4737 | } else if (len + nbchar + 1 >= size) { |
| 4738 | xmlChar *new_buf; |
| 4739 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4740 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4741 | size * sizeof(xmlChar)); |
| 4742 | if (new_buf == NULL) { |
| 4743 | xmlFree (buf); |
| 4744 | xmlErrMemory(ctxt, NULL); |
| 4745 | ctxt->instate = state; |
| 4746 | return; |
| 4747 | } |
| 4748 | buf = new_buf; |
| 4749 | } |
| 4750 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4751 | len += nbchar; |
| 4752 | buf[len] = 0; |
| 4753 | } |
| 4754 | } |
| 4755 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4756 | if (*in == 0xA) { |
| 4757 | in++; |
| 4758 | ctxt->input->line++; ctxt->input->col = 1; |
| 4759 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4760 | if (*in == 0xD) { |
| 4761 | in++; |
| 4762 | if (*in == 0xA) { |
| 4763 | ctxt->input->cur = in; |
| 4764 | in++; |
| 4765 | ctxt->input->line++; ctxt->input->col = 1; |
| 4766 | continue; /* while */ |
| 4767 | } |
| 4768 | in--; |
| 4769 | } |
| 4770 | SHRINK; |
| 4771 | GROW; |
| 4772 | in = ctxt->input->cur; |
| 4773 | if (*in == '-') { |
| 4774 | if (in[1] == '-') { |
| 4775 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4776 | if (ctxt->input->id != inputid) { |
| 4777 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4778 | "comment doesn't start and stop in the same entity\n"); |
| 4779 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4780 | SKIP(3); |
| 4781 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4782 | (!ctxt->disableSAX)) { |
| 4783 | if (buf != NULL) |
| 4784 | ctxt->sax->comment(ctxt->userData, buf); |
| 4785 | else |
| 4786 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 4787 | } |
| 4788 | if (buf != NULL) |
| 4789 | xmlFree(buf); |
| 4790 | ctxt->instate = state; |
| 4791 | return; |
| 4792 | } |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 4793 | if (buf != NULL) { |
| 4794 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 4795 | "Double hyphen within comment: " |
| 4796 | "<!--%.50s\n", |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4797 | buf); |
Bryan Henderson | 8658d27 | 2012-05-08 16:39:05 +0800 | [diff] [blame] | 4798 | } else |
| 4799 | xmlFatalErrMsgStr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, |
| 4800 | "Double hyphen within comment\n", NULL); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4801 | in++; |
| 4802 | ctxt->input->col++; |
| 4803 | } |
| 4804 | in++; |
| 4805 | ctxt->input->col++; |
| 4806 | goto get_more; |
| 4807 | } |
| 4808 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 4809 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 4810 | ctxt->instate = state; |
| 4811 | return; |
| 4812 | } |
| 4813 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4814 | |
| 4815 | /** |
| 4816 | * xmlParsePITarget: |
| 4817 | * @ctxt: an XML parser context |
| 4818 | * |
| 4819 | * parse the name of a PI |
| 4820 | * |
| 4821 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 4822 | * |
| 4823 | * Returns the PITarget name or NULL |
| 4824 | */ |
| 4825 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4826 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4827 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4828 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4829 | |
| 4830 | name = xmlParseName(ctxt); |
| 4831 | if ((name != NULL) && |
| 4832 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 4833 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 4834 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 4835 | int i; |
| 4836 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 4837 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4838 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4839 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4840 | return(name); |
| 4841 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4842 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4843 | return(name); |
| 4844 | } |
| 4845 | for (i = 0;;i++) { |
| 4846 | if (xmlW3CPIs[i] == NULL) break; |
| 4847 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 4848 | return(name); |
| 4849 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4850 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 4851 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 4852 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4853 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4854 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
| 4855 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4856 | "colon are forbidden from PI names '%s'\n", name, NULL, NULL); |
| 4857 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4858 | return(name); |
| 4859 | } |
| 4860 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4861 | #ifdef LIBXML_CATALOG_ENABLED |
| 4862 | /** |
| 4863 | * xmlParseCatalogPI: |
| 4864 | * @ctxt: an XML parser context |
| 4865 | * @catalog: the PI value string |
| 4866 | * |
| 4867 | * parse an XML Catalog Processing Instruction. |
| 4868 | * |
| 4869 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 4870 | * |
| 4871 | * Occurs only if allowed by the user and if happening in the Misc |
| 4872 | * part of the document before any doctype informations |
| 4873 | * This will add the given catalog to the parsing context in order |
| 4874 | * to be used if there is a resolution need further down in the document |
| 4875 | */ |
| 4876 | |
| 4877 | static void |
| 4878 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 4879 | xmlChar *URL = NULL; |
| 4880 | const xmlChar *tmp, *base; |
| 4881 | xmlChar marker; |
| 4882 | |
| 4883 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4884 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4885 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 4886 | goto error; |
| 4887 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4888 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4889 | if (*tmp != '=') { |
| 4890 | return; |
| 4891 | } |
| 4892 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4893 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4894 | marker = *tmp; |
| 4895 | if ((marker != '\'') && (marker != '"')) |
| 4896 | goto error; |
| 4897 | tmp++; |
| 4898 | base = tmp; |
| 4899 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 4900 | if (*tmp == 0) |
| 4901 | goto error; |
| 4902 | URL = xmlStrndup(base, tmp - base); |
| 4903 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4904 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4905 | if (*tmp != 0) |
| 4906 | goto error; |
| 4907 | |
| 4908 | if (URL != NULL) { |
| 4909 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 4910 | xmlFree(URL); |
| 4911 | } |
| 4912 | return; |
| 4913 | |
| 4914 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4915 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 4916 | "Catalog PI syntax error: %s\n", |
| 4917 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4918 | if (URL != NULL) |
| 4919 | xmlFree(URL); |
| 4920 | } |
| 4921 | #endif |
| 4922 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4923 | /** |
| 4924 | * xmlParsePI: |
| 4925 | * @ctxt: an XML parser context |
| 4926 | * |
| 4927 | * parse an XML Processing Instruction. |
| 4928 | * |
| 4929 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 4930 | * |
| 4931 | * The processing is transfered to SAX once parsed. |
| 4932 | */ |
| 4933 | |
| 4934 | void |
| 4935 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 4936 | xmlChar *buf = NULL; |
| 4937 | int len = 0; |
| 4938 | int size = XML_PARSER_BUFFER_SIZE; |
| 4939 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4940 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4941 | xmlParserInputState state; |
| 4942 | int count = 0; |
| 4943 | |
| 4944 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 4945 | xmlParserInputPtr input = ctxt->input; |
| 4946 | state = ctxt->instate; |
| 4947 | ctxt->instate = XML_PARSER_PI; |
| 4948 | /* |
| 4949 | * this is a Processing Instruction. |
| 4950 | */ |
| 4951 | SKIP(2); |
| 4952 | SHRINK; |
| 4953 | |
| 4954 | /* |
| 4955 | * Parse the target name and check for special support like |
| 4956 | * namespace. |
| 4957 | */ |
| 4958 | target = xmlParsePITarget(ctxt); |
| 4959 | if (target != NULL) { |
| 4960 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 4961 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4962 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4963 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4964 | } |
| 4965 | SKIP(2); |
| 4966 | |
| 4967 | /* |
| 4968 | * SAX: PI detected. |
| 4969 | */ |
| 4970 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 4971 | (ctxt->sax->processingInstruction != NULL)) |
| 4972 | ctxt->sax->processingInstruction(ctxt->userData, |
| 4973 | target, NULL); |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 4974 | if (ctxt->instate != XML_PARSER_EOF) |
| 4975 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4976 | return; |
| 4977 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4978 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4979 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4980 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4981 | ctxt->instate = state; |
| 4982 | return; |
| 4983 | } |
| 4984 | cur = CUR; |
| 4985 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4986 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4987 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4988 | } |
| 4989 | SKIP_BLANKS; |
| 4990 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4991 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4992 | ((cur != '?') || (NXT(1) != '>'))) { |
| 4993 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4994 | xmlChar *tmp; |
| 4995 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4996 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4997 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4998 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4999 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5000 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5001 | ctxt->instate = state; |
| 5002 | return; |
| 5003 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 5004 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5005 | } |
| 5006 | count++; |
| 5007 | if (count > 50) { |
| 5008 | GROW; |
| 5009 | count = 0; |
| 5010 | } |
| 5011 | COPY_BUF(l,buf,len,cur); |
| 5012 | NEXTL(l); |
| 5013 | cur = CUR_CHAR(l); |
| 5014 | if (cur == 0) { |
| 5015 | SHRINK; |
| 5016 | GROW; |
| 5017 | cur = CUR_CHAR(l); |
| 5018 | } |
| 5019 | } |
| 5020 | buf[len] = 0; |
| 5021 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5022 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 5023 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5024 | } else { |
| 5025 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5026 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5027 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5028 | } |
| 5029 | SKIP(2); |
| 5030 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 5031 | #ifdef LIBXML_CATALOG_ENABLED |
| 5032 | if (((state == XML_PARSER_MISC) || |
| 5033 | (state == XML_PARSER_START)) && |
| 5034 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 5035 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 5036 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 5037 | (allow == XML_CATA_ALLOW_ALL)) |
| 5038 | xmlParseCatalogPI(ctxt, buf); |
| 5039 | } |
| 5040 | #endif |
| 5041 | |
| 5042 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5043 | /* |
| 5044 | * SAX: PI detected. |
| 5045 | */ |
| 5046 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 5047 | (ctxt->sax->processingInstruction != NULL)) |
| 5048 | ctxt->sax->processingInstruction(ctxt->userData, |
| 5049 | target, buf); |
| 5050 | } |
| 5051 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5052 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5053 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5054 | } |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 5055 | if (ctxt->instate != XML_PARSER_EOF) |
| 5056 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | /** |
| 5061 | * xmlParseNotationDecl: |
| 5062 | * @ctxt: an XML parser context |
| 5063 | * |
| 5064 | * parse a notation declaration |
| 5065 | * |
| 5066 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 5067 | * |
| 5068 | * Hence there is actually 3 choices: |
| 5069 | * 'PUBLIC' S PubidLiteral |
| 5070 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 5071 | * and 'SYSTEM' S SystemLiteral |
| 5072 | * |
| 5073 | * See the NOTE on xmlParseExternalID(). |
| 5074 | */ |
| 5075 | |
| 5076 | void |
| 5077 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5078 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5079 | xmlChar *Pubid; |
| 5080 | xmlChar *Systemid; |
| 5081 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5082 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5083 | xmlParserInputPtr input = ctxt->input; |
| 5084 | SHRINK; |
| 5085 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5086 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5087 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5088 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5089 | return; |
| 5090 | } |
| 5091 | SKIP_BLANKS; |
| 5092 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5093 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5094 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5095 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5096 | return; |
| 5097 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5098 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5099 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5100 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5101 | return; |
| 5102 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5103 | if (xmlStrchr(name, ':') != NULL) { |
| 5104 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 5105 | "colon are forbidden from notation names '%s'\n", |
| 5106 | name, NULL, NULL); |
| 5107 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5108 | SKIP_BLANKS; |
| 5109 | |
| 5110 | /* |
| 5111 | * Parse the IDs. |
| 5112 | */ |
| 5113 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 5114 | SKIP_BLANKS; |
| 5115 | |
| 5116 | if (RAW == '>') { |
| 5117 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5118 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5119 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5120 | } |
| 5121 | NEXT; |
| 5122 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5123 | (ctxt->sax->notationDecl != NULL)) |
| 5124 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 5125 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5126 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | if (Systemid != NULL) xmlFree(Systemid); |
| 5129 | if (Pubid != NULL) xmlFree(Pubid); |
| 5130 | } |
| 5131 | } |
| 5132 | |
| 5133 | /** |
| 5134 | * xmlParseEntityDecl: |
| 5135 | * @ctxt: an XML parser context |
| 5136 | * |
| 5137 | * parse <!ENTITY declarations |
| 5138 | * |
| 5139 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 5140 | * |
| 5141 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 5142 | * |
| 5143 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 5144 | * |
| 5145 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 5146 | * |
| 5147 | * [74] PEDef ::= EntityValue | ExternalID |
| 5148 | * |
| 5149 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 5150 | * |
| 5151 | * [ VC: Notation Declared ] |
| 5152 | * The Name must match the declared name of a notation. |
| 5153 | */ |
| 5154 | |
| 5155 | void |
| 5156 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5157 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5158 | xmlChar *value = NULL; |
| 5159 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5160 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5161 | int isParameter = 0; |
| 5162 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5163 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5164 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5165 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5166 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5167 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5168 | SHRINK; |
| 5169 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5170 | skipped = SKIP_BLANKS; |
| 5171 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5172 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5173 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5175 | |
| 5176 | if (RAW == '%') { |
| 5177 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5178 | skipped = SKIP_BLANKS; |
| 5179 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5180 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5181 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5182 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5183 | isParameter = 1; |
| 5184 | } |
| 5185 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5186 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5187 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5188 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5189 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5190 | return; |
| 5191 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5192 | if (xmlStrchr(name, ':') != NULL) { |
| 5193 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 5194 | "colon are forbidden from entities names '%s'\n", |
| 5195 | name, NULL, NULL); |
| 5196 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5197 | skipped = SKIP_BLANKS; |
| 5198 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5199 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5200 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5201 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5203 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5204 | /* |
| 5205 | * handle the various case of definitions... |
| 5206 | */ |
| 5207 | if (isParameter) { |
| 5208 | if ((RAW == '"') || (RAW == '\'')) { |
| 5209 | value = xmlParseEntityValue(ctxt, &orig); |
| 5210 | if (value) { |
| 5211 | if ((ctxt->sax != NULL) && |
| 5212 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5213 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5214 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5215 | NULL, NULL, value); |
| 5216 | } |
| 5217 | } else { |
| 5218 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5219 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5220 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5221 | } |
| 5222 | if (URI) { |
| 5223 | xmlURIPtr uri; |
| 5224 | |
| 5225 | uri = xmlParseURI((const char *) URI); |
| 5226 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5227 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5228 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5229 | /* |
| 5230 | * This really ought to be a well formedness error |
| 5231 | * but the XML Core WG decided otherwise c.f. issue |
| 5232 | * E26 of the XML erratas. |
| 5233 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5234 | } else { |
| 5235 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5236 | /* |
| 5237 | * Okay this is foolish to block those but not |
| 5238 | * invalid URIs. |
| 5239 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5240 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5241 | } else { |
| 5242 | if ((ctxt->sax != NULL) && |
| 5243 | (!ctxt->disableSAX) && |
| 5244 | (ctxt->sax->entityDecl != NULL)) |
| 5245 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5246 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5247 | literal, URI, NULL); |
| 5248 | } |
| 5249 | xmlFreeURI(uri); |
| 5250 | } |
| 5251 | } |
| 5252 | } |
| 5253 | } else { |
| 5254 | if ((RAW == '"') || (RAW == '\'')) { |
| 5255 | value = xmlParseEntityValue(ctxt, &orig); |
| 5256 | if ((ctxt->sax != NULL) && |
| 5257 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5258 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5259 | XML_INTERNAL_GENERAL_ENTITY, |
| 5260 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5261 | /* |
| 5262 | * For expat compatibility in SAX mode. |
| 5263 | */ |
| 5264 | if ((ctxt->myDoc == NULL) || |
| 5265 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5266 | if (ctxt->myDoc == NULL) { |
| 5267 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5268 | if (ctxt->myDoc == NULL) { |
| 5269 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5270 | return; |
| 5271 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5272 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5273 | } |
| 5274 | if (ctxt->myDoc->intSubset == NULL) |
| 5275 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5276 | BAD_CAST "fake", NULL, NULL); |
| 5277 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5278 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5279 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5280 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5281 | } else { |
| 5282 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5283 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5284 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5285 | } |
| 5286 | if (URI) { |
| 5287 | xmlURIPtr uri; |
| 5288 | |
| 5289 | uri = xmlParseURI((const char *)URI); |
| 5290 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5291 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5292 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5293 | /* |
| 5294 | * This really ought to be a well formedness error |
| 5295 | * but the XML Core WG decided otherwise c.f. issue |
| 5296 | * E26 of the XML erratas. |
| 5297 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5298 | } else { |
| 5299 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5300 | /* |
| 5301 | * Okay this is foolish to block those but not |
| 5302 | * invalid URIs. |
| 5303 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5304 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5305 | } |
| 5306 | xmlFreeURI(uri); |
| 5307 | } |
| 5308 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5309 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5310 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5311 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5312 | } |
| 5313 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5314 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5315 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5316 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5317 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5318 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5319 | } |
| 5320 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5321 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5322 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5323 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5324 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5325 | literal, URI, ndata); |
| 5326 | } else { |
| 5327 | if ((ctxt->sax != NULL) && |
| 5328 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5329 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5330 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5331 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5332 | /* |
| 5333 | * For expat compatibility in SAX mode. |
| 5334 | * assuming the entity repalcement was asked for |
| 5335 | */ |
| 5336 | if ((ctxt->replaceEntities != 0) && |
| 5337 | ((ctxt->myDoc == NULL) || |
| 5338 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5339 | if (ctxt->myDoc == NULL) { |
| 5340 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5341 | if (ctxt->myDoc == NULL) { |
| 5342 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5343 | return; |
| 5344 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5345 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | if (ctxt->myDoc->intSubset == NULL) |
| 5349 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5350 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5351 | xmlSAX2EntityDecl(ctxt, name, |
| 5352 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5353 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5354 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5355 | } |
| 5356 | } |
| 5357 | } |
| 5358 | SKIP_BLANKS; |
| 5359 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5360 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5361 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5362 | } else { |
| 5363 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5364 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5365 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5366 | } |
| 5367 | NEXT; |
| 5368 | } |
| 5369 | if (orig != NULL) { |
| 5370 | /* |
| 5371 | * Ugly mechanism to save the raw entity value. |
| 5372 | */ |
| 5373 | xmlEntityPtr cur = NULL; |
| 5374 | |
| 5375 | if (isParameter) { |
| 5376 | if ((ctxt->sax != NULL) && |
| 5377 | (ctxt->sax->getParameterEntity != NULL)) |
| 5378 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5379 | } else { |
| 5380 | if ((ctxt->sax != NULL) && |
| 5381 | (ctxt->sax->getEntity != NULL)) |
| 5382 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5383 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5384 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5385 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5386 | } |
| 5387 | if (cur != NULL) { |
| 5388 | if (cur->orig != NULL) |
| 5389 | xmlFree(orig); |
| 5390 | else |
| 5391 | cur->orig = orig; |
| 5392 | } else |
| 5393 | xmlFree(orig); |
| 5394 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5395 | if (value != NULL) xmlFree(value); |
| 5396 | if (URI != NULL) xmlFree(URI); |
| 5397 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | |
| 5401 | /** |
| 5402 | * xmlParseDefaultDecl: |
| 5403 | * @ctxt: an XML parser context |
| 5404 | * @value: Receive a possible fixed default value for the attribute |
| 5405 | * |
| 5406 | * Parse an attribute default declaration |
| 5407 | * |
| 5408 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5409 | * |
| 5410 | * [ VC: Required Attribute ] |
| 5411 | * if the default declaration is the keyword #REQUIRED, then the |
| 5412 | * attribute must be specified for all elements of the type in the |
| 5413 | * attribute-list declaration. |
| 5414 | * |
| 5415 | * [ VC: Attribute Default Legal ] |
| 5416 | * The declared default value must meet the lexical constraints of |
| 5417 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5418 | * |
| 5419 | * [ VC: Fixed Attribute Default ] |
| 5420 | * if an attribute has a default value declared with the #FIXED |
| 5421 | * keyword, instances of that attribute must match the default value. |
| 5422 | * |
| 5423 | * [ WFC: No < in Attribute Values ] |
| 5424 | * handled in xmlParseAttValue() |
| 5425 | * |
| 5426 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 5427 | * or XML_ATTRIBUTE_FIXED. |
| 5428 | */ |
| 5429 | |
| 5430 | int |
| 5431 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5432 | int val; |
| 5433 | xmlChar *ret; |
| 5434 | |
| 5435 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5436 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5437 | SKIP(9); |
| 5438 | return(XML_ATTRIBUTE_REQUIRED); |
| 5439 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5440 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5441 | SKIP(8); |
| 5442 | return(XML_ATTRIBUTE_IMPLIED); |
| 5443 | } |
| 5444 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5445 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5446 | SKIP(6); |
| 5447 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5448 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5449 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5450 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5451 | } |
| 5452 | SKIP_BLANKS; |
| 5453 | } |
| 5454 | ret = xmlParseAttValue(ctxt); |
| 5455 | ctxt->instate = XML_PARSER_DTD; |
| 5456 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5457 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5458 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5459 | } else |
| 5460 | *value = ret; |
| 5461 | return(val); |
| 5462 | } |
| 5463 | |
| 5464 | /** |
| 5465 | * xmlParseNotationType: |
| 5466 | * @ctxt: an XML parser context |
| 5467 | * |
| 5468 | * parse an Notation attribute type. |
| 5469 | * |
| 5470 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5471 | * |
| 5472 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5473 | * |
| 5474 | * [ VC: Notation Attributes ] |
| 5475 | * Values of this type must match one of the notation names included |
| 5476 | * in the declaration; all notation names in the declaration must be declared. |
| 5477 | * |
| 5478 | * Returns: the notation attribute tree built while parsing |
| 5479 | */ |
| 5480 | |
| 5481 | xmlEnumerationPtr |
| 5482 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5483 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5484 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5485 | |
| 5486 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5487 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5488 | return(NULL); |
| 5489 | } |
| 5490 | SHRINK; |
| 5491 | do { |
| 5492 | NEXT; |
| 5493 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5494 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5495 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5496 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5497 | "Name expected in NOTATION declaration\n"); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5498 | xmlFreeEnumeration(ret); |
| 5499 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5500 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5501 | tmp = ret; |
| 5502 | while (tmp != NULL) { |
| 5503 | if (xmlStrEqual(name, tmp->name)) { |
| 5504 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5505 | "standalone: attribute notation value token %s duplicated\n", |
| 5506 | name, NULL); |
| 5507 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5508 | xmlFree((xmlChar *) name); |
| 5509 | break; |
| 5510 | } |
| 5511 | tmp = tmp->next; |
| 5512 | } |
| 5513 | if (tmp == NULL) { |
| 5514 | cur = xmlCreateEnumeration(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5515 | if (cur == NULL) { |
| 5516 | xmlFreeEnumeration(ret); |
| 5517 | return(NULL); |
| 5518 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5519 | if (last == NULL) ret = last = cur; |
| 5520 | else { |
| 5521 | last->next = cur; |
| 5522 | last = cur; |
| 5523 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5524 | } |
| 5525 | SKIP_BLANKS; |
| 5526 | } while (RAW == '|'); |
| 5527 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5528 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5529 | xmlFreeEnumeration(ret); |
| 5530 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5531 | } |
| 5532 | NEXT; |
| 5533 | return(ret); |
| 5534 | } |
| 5535 | |
| 5536 | /** |
| 5537 | * xmlParseEnumerationType: |
| 5538 | * @ctxt: an XML parser context |
| 5539 | * |
| 5540 | * parse an Enumeration attribute type. |
| 5541 | * |
| 5542 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5543 | * |
| 5544 | * [ VC: Enumeration ] |
| 5545 | * Values of this type must match one of the Nmtoken tokens in |
| 5546 | * the declaration |
| 5547 | * |
| 5548 | * Returns: the enumeration attribute tree built while parsing |
| 5549 | */ |
| 5550 | |
| 5551 | xmlEnumerationPtr |
| 5552 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5553 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5554 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5555 | |
| 5556 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5557 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5558 | return(NULL); |
| 5559 | } |
| 5560 | SHRINK; |
| 5561 | do { |
| 5562 | NEXT; |
| 5563 | SKIP_BLANKS; |
| 5564 | name = xmlParseNmtoken(ctxt); |
| 5565 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5566 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5567 | return(ret); |
| 5568 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5569 | tmp = ret; |
| 5570 | while (tmp != NULL) { |
| 5571 | if (xmlStrEqual(name, tmp->name)) { |
| 5572 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5573 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5574 | name, NULL); |
| 5575 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5576 | xmlFree(name); |
| 5577 | break; |
| 5578 | } |
| 5579 | tmp = tmp->next; |
| 5580 | } |
| 5581 | if (tmp == NULL) { |
| 5582 | cur = xmlCreateEnumeration(name); |
| 5583 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5584 | xmlFree(name); |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5585 | if (cur == NULL) { |
| 5586 | xmlFreeEnumeration(ret); |
| 5587 | return(NULL); |
| 5588 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5589 | if (last == NULL) ret = last = cur; |
| 5590 | else { |
| 5591 | last->next = cur; |
| 5592 | last = cur; |
| 5593 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5594 | } |
| 5595 | SKIP_BLANKS; |
| 5596 | } while (RAW == '|'); |
| 5597 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5598 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5599 | return(ret); |
| 5600 | } |
| 5601 | NEXT; |
| 5602 | return(ret); |
| 5603 | } |
| 5604 | |
| 5605 | /** |
| 5606 | * xmlParseEnumeratedType: |
| 5607 | * @ctxt: an XML parser context |
| 5608 | * @tree: the enumeration tree built while parsing |
| 5609 | * |
| 5610 | * parse an Enumerated attribute type. |
| 5611 | * |
| 5612 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5613 | * |
| 5614 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5615 | * |
| 5616 | * |
| 5617 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5618 | */ |
| 5619 | |
| 5620 | int |
| 5621 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5622 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5623 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5624 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5625 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5626 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5627 | return(0); |
| 5628 | } |
| 5629 | SKIP_BLANKS; |
| 5630 | *tree = xmlParseNotationType(ctxt); |
| 5631 | if (*tree == NULL) return(0); |
| 5632 | return(XML_ATTRIBUTE_NOTATION); |
| 5633 | } |
| 5634 | *tree = xmlParseEnumerationType(ctxt); |
| 5635 | if (*tree == NULL) return(0); |
| 5636 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5637 | } |
| 5638 | |
| 5639 | /** |
| 5640 | * xmlParseAttributeType: |
| 5641 | * @ctxt: an XML parser context |
| 5642 | * @tree: the enumeration tree built while parsing |
| 5643 | * |
| 5644 | * parse the Attribute list def for an element |
| 5645 | * |
| 5646 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5647 | * |
| 5648 | * [55] StringType ::= 'CDATA' |
| 5649 | * |
| 5650 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5651 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5652 | * |
| 5653 | * Validity constraints for attribute values syntax are checked in |
| 5654 | * xmlValidateAttributeValue() |
| 5655 | * |
| 5656 | * [ VC: ID ] |
| 5657 | * Values of type ID must match the Name production. A name must not |
| 5658 | * appear more than once in an XML document as a value of this type; |
| 5659 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5660 | * |
| 5661 | * [ VC: One ID per Element Type ] |
| 5662 | * No element type may have more than one ID attribute specified. |
| 5663 | * |
| 5664 | * [ VC: ID Attribute Default ] |
| 5665 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5666 | * |
| 5667 | * [ VC: IDREF ] |
| 5668 | * Values of type IDREF must match the Name production, and values |
| 5669 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5670 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5671 | * values must match the value of some ID attribute. |
| 5672 | * |
| 5673 | * [ VC: Entity Name ] |
| 5674 | * Values of type ENTITY must match the Name production, values |
| 5675 | * of type ENTITIES must match Names; each Entity Name must match the |
| 5676 | * name of an unparsed entity declared in the DTD. |
| 5677 | * |
| 5678 | * [ VC: Name Token ] |
| 5679 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 5680 | * of type NMTOKENS must match Nmtokens. |
| 5681 | * |
| 5682 | * Returns the attribute type |
| 5683 | */ |
| 5684 | int |
| 5685 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5686 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5687 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5688 | SKIP(5); |
| 5689 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5690 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5691 | SKIP(6); |
| 5692 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5693 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5694 | SKIP(5); |
| 5695 | return(XML_ATTRIBUTE_IDREF); |
| 5696 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5697 | SKIP(2); |
| 5698 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5699 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5700 | SKIP(6); |
| 5701 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5702 | } 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] | 5703 | SKIP(8); |
| 5704 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5705 | } 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] | 5706 | SKIP(8); |
| 5707 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5708 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5709 | SKIP(7); |
| 5710 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5711 | } |
| 5712 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5713 | } |
| 5714 | |
| 5715 | /** |
| 5716 | * xmlParseAttributeListDecl: |
| 5717 | * @ctxt: an XML parser context |
| 5718 | * |
| 5719 | * : parse the Attribute list def for an element |
| 5720 | * |
| 5721 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5722 | * |
| 5723 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5724 | * |
| 5725 | */ |
| 5726 | void |
| 5727 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5728 | const xmlChar *elemName; |
| 5729 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5730 | xmlEnumerationPtr tree; |
| 5731 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5732 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5733 | xmlParserInputPtr input = ctxt->input; |
| 5734 | |
| 5735 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5736 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5737 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5738 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5739 | } |
| 5740 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5741 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5742 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5743 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5744 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5745 | return; |
| 5746 | } |
| 5747 | SKIP_BLANKS; |
| 5748 | GROW; |
| 5749 | while (RAW != '>') { |
| 5750 | const xmlChar *check = CUR_PTR; |
| 5751 | int type; |
| 5752 | int def; |
| 5753 | xmlChar *defaultValue = NULL; |
| 5754 | |
| 5755 | GROW; |
| 5756 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5757 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5759 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5760 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5761 | break; |
| 5762 | } |
| 5763 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5764 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5765 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5766 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5767 | break; |
| 5768 | } |
| 5769 | SKIP_BLANKS; |
| 5770 | |
| 5771 | type = xmlParseAttributeType(ctxt, &tree); |
| 5772 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5773 | break; |
| 5774 | } |
| 5775 | |
| 5776 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5777 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5778 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5779 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5780 | if (tree != NULL) |
| 5781 | xmlFreeEnumeration(tree); |
| 5782 | break; |
| 5783 | } |
| 5784 | SKIP_BLANKS; |
| 5785 | |
| 5786 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 5787 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5788 | if (defaultValue != NULL) |
| 5789 | xmlFree(defaultValue); |
| 5790 | if (tree != NULL) |
| 5791 | xmlFreeEnumeration(tree); |
| 5792 | break; |
| 5793 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 5794 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 5795 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5796 | |
| 5797 | GROW; |
| 5798 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5799 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5800 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5801 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5802 | if (defaultValue != NULL) |
| 5803 | xmlFree(defaultValue); |
| 5804 | if (tree != NULL) |
| 5805 | xmlFreeEnumeration(tree); |
| 5806 | break; |
| 5807 | } |
| 5808 | SKIP_BLANKS; |
| 5809 | } |
| 5810 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5811 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5812 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5813 | if (defaultValue != NULL) |
| 5814 | xmlFree(defaultValue); |
| 5815 | if (tree != NULL) |
| 5816 | xmlFreeEnumeration(tree); |
| 5817 | break; |
| 5818 | } |
| 5819 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5820 | (ctxt->sax->attributeDecl != NULL)) |
| 5821 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 5822 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5823 | else if (tree != NULL) |
| 5824 | xmlFreeEnumeration(tree); |
| 5825 | |
| 5826 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 5827 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 5828 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 5829 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 5830 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 5831 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5832 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 5833 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5834 | if (defaultValue != NULL) |
| 5835 | xmlFree(defaultValue); |
| 5836 | GROW; |
| 5837 | } |
| 5838 | if (RAW == '>') { |
| 5839 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5840 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5841 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 5842 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5843 | } |
| 5844 | NEXT; |
| 5845 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | /** |
| 5850 | * xmlParseElementMixedContentDecl: |
| 5851 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5852 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5853 | * |
| 5854 | * parse the declaration for a Mixed Element content |
| 5855 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5856 | * |
| 5857 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 5858 | * '(' S? '#PCDATA' S? ')' |
| 5859 | * |
| 5860 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 5861 | * |
| 5862 | * [ VC: No Duplicate Types ] |
| 5863 | * The same name must not appear more than once in a single |
| 5864 | * mixed-content declaration. |
| 5865 | * |
| 5866 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 5867 | */ |
| 5868 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5869 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5870 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5871 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5872 | |
| 5873 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5874 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5875 | SKIP(7); |
| 5876 | SKIP_BLANKS; |
| 5877 | SHRINK; |
| 5878 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5879 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5880 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5881 | "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] | 5882 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5884 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5885 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5886 | if (ret == NULL) |
| 5887 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | if (RAW == '*') { |
| 5889 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5890 | NEXT; |
| 5891 | } |
| 5892 | return(ret); |
| 5893 | } |
| 5894 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5895 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5896 | if (ret == NULL) return(NULL); |
| 5897 | } |
| 5898 | while (RAW == '|') { |
| 5899 | NEXT; |
| 5900 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5901 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5902 | if (ret == NULL) return(NULL); |
| 5903 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5904 | if (cur != NULL) |
| 5905 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5906 | cur = ret; |
| 5907 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5908 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5910 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5911 | if (n->c1 != NULL) |
| 5912 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5913 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5914 | if (n != NULL) |
| 5915 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5916 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5917 | } |
| 5918 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5919 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5920 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5921 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5922 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5923 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5924 | return(NULL); |
| 5925 | } |
| 5926 | SKIP_BLANKS; |
| 5927 | GROW; |
| 5928 | } |
| 5929 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 5930 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5931 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5932 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5933 | if (cur->c2 != NULL) |
| 5934 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5935 | } |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 5936 | if (ret != NULL) |
| 5937 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5938 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5939 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5940 | "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] | 5941 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5942 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5943 | SKIP(2); |
| 5944 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5945 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5946 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5947 | return(NULL); |
| 5948 | } |
| 5949 | |
| 5950 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5951 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5952 | } |
| 5953 | return(ret); |
| 5954 | } |
| 5955 | |
| 5956 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5957 | * xmlParseElementChildrenContentDeclPriv: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5958 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5959 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5960 | * @depth: the level of recursion |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5961 | * |
| 5962 | * parse the declaration for a Mixed Element content |
| 5963 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5964 | * |
| 5965 | * |
| 5966 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 5967 | * |
| 5968 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 5969 | * |
| 5970 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 5971 | * |
| 5972 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 5973 | * |
| 5974 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 5975 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5976 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5977 | * opening or closing parentheses in a choice, seq, or Mixed |
| 5978 | * construct is contained in the replacement text for a parameter |
| 5979 | * entity, both must be contained in the same replacement text. For |
| 5980 | * interoperability, if a parameter-entity reference appears in a |
| 5981 | * choice, seq, or Mixed construct, its replacement text should not |
| 5982 | * be empty, and neither the first nor last non-blank character of |
| 5983 | * the replacement text should be a connector (| or ,). |
| 5984 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5985 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5986 | * hierarchy. |
| 5987 | */ |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5988 | static xmlElementContentPtr |
| 5989 | xmlParseElementChildrenContentDeclPriv(xmlParserCtxtPtr ctxt, int inputchk, |
| 5990 | int depth) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5991 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5992 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5993 | xmlChar type = 0; |
| 5994 | |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 5995 | if (((depth > 128) && ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 5996 | (depth > 2048)) { |
| 5997 | xmlFatalErrMsgInt(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, |
| 5998 | "xmlParseElementChildrenContentDecl : depth %d too deep, use XML_PARSE_HUGE\n", |
| 5999 | depth); |
| 6000 | return(NULL); |
| 6001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6002 | SKIP_BLANKS; |
| 6003 | GROW; |
| 6004 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6005 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6006 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6007 | /* Recurse on first child */ |
| 6008 | NEXT; |
| 6009 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6010 | cur = ret = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6011 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6012 | SKIP_BLANKS; |
| 6013 | GROW; |
| 6014 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6015 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6016 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6017 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6018 | return(NULL); |
| 6019 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6020 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6021 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6022 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6023 | return(NULL); |
| 6024 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6025 | GROW; |
| 6026 | if (RAW == '?') { |
| 6027 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6028 | NEXT; |
| 6029 | } else if (RAW == '*') { |
| 6030 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6031 | NEXT; |
| 6032 | } else if (RAW == '+') { |
| 6033 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6034 | NEXT; |
| 6035 | } else { |
| 6036 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6037 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6038 | GROW; |
| 6039 | } |
| 6040 | SKIP_BLANKS; |
| 6041 | SHRINK; |
| 6042 | while (RAW != ')') { |
| 6043 | /* |
| 6044 | * Each loop we parse one separator and one element. |
| 6045 | */ |
| 6046 | if (RAW == ',') { |
| 6047 | if (type == 0) type = CUR; |
| 6048 | |
| 6049 | /* |
| 6050 | * Detect "Name | Name , Name" error |
| 6051 | */ |
| 6052 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6053 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6054 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6055 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6056 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6057 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6058 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6059 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6060 | return(NULL); |
| 6061 | } |
| 6062 | NEXT; |
| 6063 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6064 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6065 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6066 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6067 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 6068 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6069 | return(NULL); |
| 6070 | } |
| 6071 | if (last == NULL) { |
| 6072 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6073 | if (ret != NULL) |
| 6074 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6075 | ret = cur = op; |
| 6076 | } else { |
| 6077 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6078 | if (op != NULL) |
| 6079 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6080 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6081 | if (last != NULL) |
| 6082 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6083 | cur =op; |
| 6084 | last = NULL; |
| 6085 | } |
| 6086 | } else if (RAW == '|') { |
| 6087 | if (type == 0) type = CUR; |
| 6088 | |
| 6089 | /* |
| 6090 | * Detect "Name , Name | Name" error |
| 6091 | */ |
| 6092 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6093 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6094 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6095 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6096 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6097 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6098 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6099 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6100 | return(NULL); |
| 6101 | } |
| 6102 | NEXT; |
| 6103 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6104 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6105 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 6106 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6107 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6108 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6109 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6110 | return(NULL); |
| 6111 | } |
| 6112 | if (last == NULL) { |
| 6113 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6114 | if (ret != NULL) |
| 6115 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6116 | ret = cur = op; |
| 6117 | } else { |
| 6118 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6119 | if (op != NULL) |
| 6120 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6121 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6122 | if (last != NULL) |
| 6123 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6124 | cur =op; |
| 6125 | last = NULL; |
| 6126 | } |
| 6127 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6128 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 6129 | if ((last != NULL) && (last != ret)) |
| 6130 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6131 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6132 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6133 | return(NULL); |
| 6134 | } |
| 6135 | GROW; |
| 6136 | SKIP_BLANKS; |
| 6137 | GROW; |
| 6138 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6139 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6140 | /* Recurse on second child */ |
| 6141 | NEXT; |
| 6142 | SKIP_BLANKS; |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6143 | last = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, |
| 6144 | depth + 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6145 | SKIP_BLANKS; |
| 6146 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6147 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6148 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6149 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6150 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6151 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | return(NULL); |
| 6153 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6154 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 6155 | if (last == NULL) { |
| 6156 | if (ret != NULL) |
| 6157 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 6158 | return(NULL); |
| 6159 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6160 | if (RAW == '?') { |
| 6161 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6162 | NEXT; |
| 6163 | } else if (RAW == '*') { |
| 6164 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6165 | NEXT; |
| 6166 | } else if (RAW == '+') { |
| 6167 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 6168 | NEXT; |
| 6169 | } else { |
| 6170 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6171 | } |
| 6172 | } |
| 6173 | SKIP_BLANKS; |
| 6174 | GROW; |
| 6175 | } |
| 6176 | if ((cur != NULL) && (last != NULL)) { |
| 6177 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 6178 | if (last != NULL) |
| 6179 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6180 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6181 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6182 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6183 | "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] | 6184 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 6185 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6186 | NEXT; |
| 6187 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6188 | if (ret != NULL) { |
| 6189 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 6190 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6191 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6192 | else |
| 6193 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 6194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6195 | NEXT; |
| 6196 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6197 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 6198 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6199 | cur = ret; |
| 6200 | /* |
| 6201 | * Some normalization: |
| 6202 | * (a | b* | c?)* == (a | b | c)* |
| 6203 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6204 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6205 | if ((cur->c1 != NULL) && |
| 6206 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6207 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6208 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6209 | if ((cur->c2 != NULL) && |
| 6210 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6211 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6212 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6213 | cur = cur->c2; |
| 6214 | } |
| 6215 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6216 | NEXT; |
| 6217 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6218 | if (ret != NULL) { |
| 6219 | int found = 0; |
| 6220 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6221 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6222 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6223 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6224 | else |
| 6225 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6226 | /* |
| 6227 | * Some normalization: |
| 6228 | * (a | b*)+ == (a | b)* |
| 6229 | * (a | b?)+ == (a | b)* |
| 6230 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6231 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6232 | if ((cur->c1 != NULL) && |
| 6233 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6234 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6235 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6236 | found = 1; |
| 6237 | } |
| 6238 | if ((cur->c2 != NULL) && |
| 6239 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6240 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6241 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6242 | found = 1; |
| 6243 | } |
| 6244 | cur = cur->c2; |
| 6245 | } |
| 6246 | if (found) |
| 6247 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6249 | NEXT; |
| 6250 | } |
| 6251 | return(ret); |
| 6252 | } |
| 6253 | |
| 6254 | /** |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6255 | * xmlParseElementChildrenContentDecl: |
| 6256 | * @ctxt: an XML parser context |
| 6257 | * @inputchk: the input used for the current entity, needed for boundary checks |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6258 | * |
| 6259 | * parse the declaration for a Mixed Element content |
| 6260 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 6261 | * |
| 6262 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 6263 | * |
| 6264 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 6265 | * |
| 6266 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 6267 | * |
| 6268 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 6269 | * |
| 6270 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 6271 | * TODO Parameter-entity replacement text must be properly nested |
| 6272 | * with parenthesized groups. That is to say, if either of the |
| 6273 | * opening or closing parentheses in a choice, seq, or Mixed |
| 6274 | * construct is contained in the replacement text for a parameter |
| 6275 | * entity, both must be contained in the same replacement text. For |
| 6276 | * interoperability, if a parameter-entity reference appears in a |
| 6277 | * choice, seq, or Mixed construct, its replacement text should not |
| 6278 | * be empty, and neither the first nor last non-blank character of |
| 6279 | * the replacement text should be a connector (| or ,). |
| 6280 | * |
| 6281 | * Returns the tree of xmlElementContentPtr describing the element |
| 6282 | * hierarchy. |
| 6283 | */ |
| 6284 | xmlElementContentPtr |
| 6285 | xmlParseElementChildrenContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
| 6286 | /* stub left for API/ABI compat */ |
| 6287 | return(xmlParseElementChildrenContentDeclPriv(ctxt, inputchk, 1)); |
| 6288 | } |
| 6289 | |
| 6290 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6291 | * xmlParseElementContentDecl: |
| 6292 | * @ctxt: an XML parser context |
| 6293 | * @name: the name of the element being defined. |
| 6294 | * @result: the Element Content pointer will be stored here if any |
| 6295 | * |
| 6296 | * parse the declaration for an Element content either Mixed or Children, |
| 6297 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 6298 | * |
| 6299 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6300 | * |
| 6301 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6302 | */ |
| 6303 | |
| 6304 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6305 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6306 | xmlElementContentPtr *result) { |
| 6307 | |
| 6308 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6309 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | int res; |
| 6311 | |
| 6312 | *result = NULL; |
| 6313 | |
| 6314 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6315 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6316 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6317 | return(-1); |
| 6318 | } |
| 6319 | NEXT; |
| 6320 | GROW; |
| 6321 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6322 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6323 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6324 | res = XML_ELEMENT_TYPE_MIXED; |
| 6325 | } else { |
Daniel Veillard | 489f967 | 2009-08-10 16:49:30 +0200 | [diff] [blame] | 6326 | tree = xmlParseElementChildrenContentDeclPriv(ctxt, inputid, 1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6327 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6328 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6329 | SKIP_BLANKS; |
| 6330 | *result = tree; |
| 6331 | return(res); |
| 6332 | } |
| 6333 | |
| 6334 | /** |
| 6335 | * xmlParseElementDecl: |
| 6336 | * @ctxt: an XML parser context |
| 6337 | * |
| 6338 | * parse an Element declaration. |
| 6339 | * |
| 6340 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6341 | * |
| 6342 | * [ VC: Unique Element Type Declaration ] |
| 6343 | * No element type may be declared more than once |
| 6344 | * |
| 6345 | * Returns the type of the element, or -1 in case of error |
| 6346 | */ |
| 6347 | int |
| 6348 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6349 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6350 | int ret = -1; |
| 6351 | xmlElementContentPtr content = NULL; |
| 6352 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6353 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6354 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6355 | xmlParserInputPtr input = ctxt->input; |
| 6356 | |
| 6357 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6358 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6359 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6360 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6361 | } |
| 6362 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6363 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6364 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6365 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6366 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6367 | return(-1); |
| 6368 | } |
| 6369 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6370 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6371 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6372 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6373 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6374 | } |
| 6375 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6376 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6377 | SKIP(5); |
| 6378 | /* |
| 6379 | * Element must always be empty. |
| 6380 | */ |
| 6381 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6382 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6383 | (NXT(2) == 'Y')) { |
| 6384 | SKIP(3); |
| 6385 | /* |
| 6386 | * Element is a generic container. |
| 6387 | */ |
| 6388 | ret = XML_ELEMENT_TYPE_ANY; |
| 6389 | } else if (RAW == '(') { |
| 6390 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6391 | } else { |
| 6392 | /* |
| 6393 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6394 | */ |
| 6395 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6396 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6397 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6398 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6399 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6400 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6401 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6402 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6403 | return(-1); |
| 6404 | } |
| 6405 | |
| 6406 | SKIP_BLANKS; |
| 6407 | /* |
| 6408 | * Pop-up of finished entities. |
| 6409 | */ |
| 6410 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6411 | xmlPopInput(ctxt); |
| 6412 | SKIP_BLANKS; |
| 6413 | |
| 6414 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6415 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6416 | if (content != NULL) { |
| 6417 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6418 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6419 | } else { |
| 6420 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6421 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6422 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | NEXT; |
| 6426 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6427 | (ctxt->sax->elementDecl != NULL)) { |
| 6428 | if (content != NULL) |
| 6429 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6430 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6431 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6432 | if ((content != NULL) && (content->parent == NULL)) { |
| 6433 | /* |
| 6434 | * this is a trick: if xmlAddElementDecl is called, |
| 6435 | * instead of copying the full tree it is plugged directly |
| 6436 | * if called from the parser. Avoid duplicating the |
| 6437 | * interfaces or change the API/ABI |
| 6438 | */ |
| 6439 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6440 | } |
| 6441 | } else if (content != NULL) { |
| 6442 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6443 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6444 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6445 | } |
| 6446 | return(ret); |
| 6447 | } |
| 6448 | |
| 6449 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6450 | * xmlParseConditionalSections |
| 6451 | * @ctxt: an XML parser context |
| 6452 | * |
| 6453 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6454 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 6455 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6456 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6457 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6458 | */ |
| 6459 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6460 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6461 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6462 | int id = ctxt->input->id; |
| 6463 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6464 | SKIP(3); |
| 6465 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6466 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6467 | SKIP(7); |
| 6468 | SKIP_BLANKS; |
| 6469 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6470 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6471 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6472 | if (ctxt->input->id != id) { |
| 6473 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6474 | "All markup of the conditional section is not in the same entity\n", |
| 6475 | NULL, NULL); |
| 6476 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6477 | NEXT; |
| 6478 | } |
| 6479 | if (xmlParserDebugEntities) { |
| 6480 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6481 | xmlGenericError(xmlGenericErrorContext, |
| 6482 | "%s(%d): ", ctxt->input->filename, |
| 6483 | ctxt->input->line); |
| 6484 | xmlGenericError(xmlGenericErrorContext, |
| 6485 | "Entering INCLUDE Conditional Section\n"); |
| 6486 | } |
| 6487 | |
| 6488 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6489 | (NXT(2) != '>'))) { |
| 6490 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6491 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6492 | |
| 6493 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6494 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6495 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6496 | NEXT; |
| 6497 | } else if (RAW == '%') { |
| 6498 | xmlParsePEReference(ctxt); |
| 6499 | } else |
| 6500 | xmlParseMarkupDecl(ctxt); |
| 6501 | |
| 6502 | /* |
| 6503 | * Pop-up of finished entities. |
| 6504 | */ |
| 6505 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6506 | xmlPopInput(ctxt); |
| 6507 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6508 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6509 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6510 | break; |
| 6511 | } |
| 6512 | } |
| 6513 | if (xmlParserDebugEntities) { |
| 6514 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6515 | xmlGenericError(xmlGenericErrorContext, |
| 6516 | "%s(%d): ", ctxt->input->filename, |
| 6517 | ctxt->input->line); |
| 6518 | xmlGenericError(xmlGenericErrorContext, |
| 6519 | "Leaving INCLUDE Conditional Section\n"); |
| 6520 | } |
| 6521 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6522 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6523 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6524 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6525 | int depth = 0; |
| 6526 | |
| 6527 | SKIP(6); |
| 6528 | SKIP_BLANKS; |
| 6529 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6530 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6531 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6532 | if (ctxt->input->id != id) { |
| 6533 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6534 | "All markup of the conditional section is not in the same entity\n", |
| 6535 | NULL, NULL); |
| 6536 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | NEXT; |
| 6538 | } |
| 6539 | if (xmlParserDebugEntities) { |
| 6540 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6541 | xmlGenericError(xmlGenericErrorContext, |
| 6542 | "%s(%d): ", ctxt->input->filename, |
| 6543 | ctxt->input->line); |
| 6544 | xmlGenericError(xmlGenericErrorContext, |
| 6545 | "Entering IGNORE Conditional Section\n"); |
| 6546 | } |
| 6547 | |
| 6548 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6549 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6550 | * But disable SAX event generating DTD building in the meantime |
| 6551 | */ |
| 6552 | state = ctxt->disableSAX; |
| 6553 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6554 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6555 | ctxt->instate = XML_PARSER_IGNORE; |
| 6556 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 6557 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6558 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6559 | depth++; |
| 6560 | SKIP(3); |
| 6561 | continue; |
| 6562 | } |
| 6563 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6564 | if (--depth >= 0) SKIP(3); |
| 6565 | continue; |
| 6566 | } |
| 6567 | NEXT; |
| 6568 | continue; |
| 6569 | } |
| 6570 | |
| 6571 | ctxt->disableSAX = state; |
| 6572 | ctxt->instate = instate; |
| 6573 | |
| 6574 | if (xmlParserDebugEntities) { |
| 6575 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6576 | xmlGenericError(xmlGenericErrorContext, |
| 6577 | "%s(%d): ", ctxt->input->filename, |
| 6578 | ctxt->input->line); |
| 6579 | xmlGenericError(xmlGenericErrorContext, |
| 6580 | "Leaving IGNORE Conditional Section\n"); |
| 6581 | } |
| 6582 | |
| 6583 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6584 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | if (RAW == 0) |
| 6588 | SHRINK; |
| 6589 | |
| 6590 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6591 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6592 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6593 | if (ctxt->input->id != id) { |
| 6594 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6595 | "All markup of the conditional section is not in the same entity\n", |
| 6596 | NULL, NULL); |
| 6597 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6598 | SKIP(3); |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6603 | * xmlParseMarkupDecl: |
| 6604 | * @ctxt: an XML parser context |
| 6605 | * |
| 6606 | * parse Markup declarations |
| 6607 | * |
| 6608 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6609 | * NotationDecl | PI | Comment |
| 6610 | * |
| 6611 | * [ VC: Proper Declaration/PE Nesting ] |
| 6612 | * Parameter-entity replacement text must be properly nested with |
| 6613 | * markup declarations. That is to say, if either the first character |
| 6614 | * or the last character of a markup declaration (markupdecl above) is |
| 6615 | * contained in the replacement text for a parameter-entity reference, |
| 6616 | * both must be contained in the same replacement text. |
| 6617 | * |
| 6618 | * [ WFC: PEs in Internal Subset ] |
| 6619 | * In the internal DTD subset, parameter-entity references can occur |
| 6620 | * only where markup declarations can occur, not within markup declarations. |
| 6621 | * (This does not apply to references that occur in external parameter |
| 6622 | * entities or to the external subset.) |
| 6623 | */ |
| 6624 | void |
| 6625 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6626 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6627 | if (CUR == '<') { |
| 6628 | if (NXT(1) == '!') { |
| 6629 | switch (NXT(2)) { |
| 6630 | case 'E': |
| 6631 | if (NXT(3) == 'L') |
| 6632 | xmlParseElementDecl(ctxt); |
| 6633 | else if (NXT(3) == 'N') |
| 6634 | xmlParseEntityDecl(ctxt); |
| 6635 | break; |
| 6636 | case 'A': |
| 6637 | xmlParseAttributeListDecl(ctxt); |
| 6638 | break; |
| 6639 | case 'N': |
| 6640 | xmlParseNotationDecl(ctxt); |
| 6641 | break; |
| 6642 | case '-': |
| 6643 | xmlParseComment(ctxt); |
| 6644 | break; |
| 6645 | default: |
| 6646 | /* there is an error but it will be detected later */ |
| 6647 | break; |
| 6648 | } |
| 6649 | } else if (NXT(1) == '?') { |
| 6650 | xmlParsePI(ctxt); |
| 6651 | } |
| 6652 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6653 | /* |
| 6654 | * This is only for internal subset. On external entities, |
| 6655 | * the replacement is done before parsing stage |
| 6656 | */ |
| 6657 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6658 | xmlParsePEReference(ctxt); |
| 6659 | |
| 6660 | /* |
| 6661 | * Conditional sections are allowed from entities included |
| 6662 | * by PE References in the internal subset. |
| 6663 | */ |
| 6664 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6665 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6666 | xmlParseConditionalSections(ctxt); |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | ctxt->instate = XML_PARSER_DTD; |
| 6671 | } |
| 6672 | |
| 6673 | /** |
| 6674 | * xmlParseTextDecl: |
| 6675 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6676 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6677 | * parse an XML declaration header for external entities |
| 6678 | * |
| 6679 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6680 | */ |
| 6681 | |
| 6682 | void |
| 6683 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6684 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6685 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6686 | |
| 6687 | /* |
| 6688 | * We know that '<?xml' is here. |
| 6689 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6690 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6691 | SKIP(5); |
| 6692 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6693 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6694 | return; |
| 6695 | } |
| 6696 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6697 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6698 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6699 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6700 | } |
| 6701 | SKIP_BLANKS; |
| 6702 | |
| 6703 | /* |
| 6704 | * We may have the VersionInfo here. |
| 6705 | */ |
| 6706 | version = xmlParseVersionInfo(ctxt); |
| 6707 | if (version == NULL) |
| 6708 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6709 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6710 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6711 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6712 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6713 | } |
| 6714 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6715 | ctxt->input->version = version; |
| 6716 | |
| 6717 | /* |
| 6718 | * We must have the encoding declaration |
| 6719 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6720 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6721 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6722 | /* |
| 6723 | * The XML REC instructs us to stop parsing right here |
| 6724 | */ |
| 6725 | return; |
| 6726 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6727 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 6728 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 6729 | "Missing encoding in text declaration\n"); |
| 6730 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6731 | |
| 6732 | SKIP_BLANKS; |
| 6733 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 6734 | SKIP(2); |
| 6735 | } else if (RAW == '>') { |
| 6736 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6737 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6738 | NEXT; |
| 6739 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6740 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6741 | MOVETO_ENDTAG(CUR_PTR); |
| 6742 | NEXT; |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6747 | * xmlParseExternalSubset: |
| 6748 | * @ctxt: an XML parser context |
| 6749 | * @ExternalID: the external identifier |
| 6750 | * @SystemID: the system identifier (or URL) |
| 6751 | * |
| 6752 | * parse Markup declarations from an external subset |
| 6753 | * |
| 6754 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 6755 | * |
| 6756 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 6757 | */ |
| 6758 | void |
| 6759 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 6760 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 6761 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6762 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6763 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 6764 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6765 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 6766 | xmlChar start[4]; |
| 6767 | xmlCharEncoding enc; |
| 6768 | |
| 6769 | start[0] = RAW; |
| 6770 | start[1] = NXT(1); |
| 6771 | start[2] = NXT(2); |
| 6772 | start[3] = NXT(3); |
| 6773 | enc = xmlDetectCharEncoding(start, 4); |
| 6774 | if (enc != XML_CHAR_ENCODING_NONE) |
| 6775 | xmlSwitchEncoding(ctxt, enc); |
| 6776 | } |
| 6777 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6778 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6779 | xmlParseTextDecl(ctxt); |
| 6780 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6781 | /* |
| 6782 | * The XML REC instructs us to stop parsing right here |
| 6783 | */ |
| 6784 | ctxt->instate = XML_PARSER_EOF; |
| 6785 | return; |
| 6786 | } |
| 6787 | } |
| 6788 | if (ctxt->myDoc == NULL) { |
| 6789 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6790 | if (ctxt->myDoc == NULL) { |
| 6791 | xmlErrMemory(ctxt, "New Doc failed"); |
| 6792 | return; |
| 6793 | } |
| 6794 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6795 | } |
| 6796 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 6797 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 6798 | |
| 6799 | ctxt->instate = XML_PARSER_DTD; |
| 6800 | ctxt->external = 1; |
| 6801 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 6802 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6803 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6804 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6805 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6806 | |
| 6807 | GROW; |
| 6808 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6809 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6810 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6811 | NEXT; |
| 6812 | } else if (RAW == '%') { |
| 6813 | xmlParsePEReference(ctxt); |
| 6814 | } else |
| 6815 | xmlParseMarkupDecl(ctxt); |
| 6816 | |
| 6817 | /* |
| 6818 | * Pop-up of finished entities. |
| 6819 | */ |
| 6820 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6821 | xmlPopInput(ctxt); |
| 6822 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6823 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6824 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6825 | break; |
| 6826 | } |
| 6827 | } |
| 6828 | |
| 6829 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6830 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | } |
| 6834 | |
| 6835 | /** |
| 6836 | * xmlParseReference: |
| 6837 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6838 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6839 | * parse and handle entity references in content, depending on the SAX |
| 6840 | * interface, this may end-up in a call to character() if this is a |
| 6841 | * CharRef, a predefined entity, if there is no reference() callback. |
| 6842 | * or if the parser was asked to switch to that mode. |
| 6843 | * |
| 6844 | * [67] Reference ::= EntityRef | CharRef |
| 6845 | */ |
| 6846 | void |
| 6847 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 6848 | xmlEntityPtr ent; |
| 6849 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6850 | int was_checked; |
| 6851 | xmlNodePtr list = NULL; |
| 6852 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6853 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6854 | |
| 6855 | if (RAW != '&') |
| 6856 | return; |
| 6857 | |
| 6858 | /* |
| 6859 | * Simple case of a CharRef |
| 6860 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6861 | if (NXT(1) == '#') { |
| 6862 | int i = 0; |
| 6863 | xmlChar out[10]; |
| 6864 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6865 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6866 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 6867 | if (value == 0) |
| 6868 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6869 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 6870 | /* |
| 6871 | * So we are using non-UTF-8 buffers |
| 6872 | * Check that the char fit on 8bits, if not |
| 6873 | * generate a CharRef. |
| 6874 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6875 | if (value <= 0xFF) { |
| 6876 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6877 | out[1] = 0; |
| 6878 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6879 | (!ctxt->disableSAX)) |
| 6880 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 6881 | } else { |
| 6882 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6883 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6884 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6885 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6886 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6887 | (!ctxt->disableSAX)) |
| 6888 | ctxt->sax->reference(ctxt->userData, out); |
| 6889 | } |
| 6890 | } else { |
| 6891 | /* |
| 6892 | * Just encode the value in UTF-8 |
| 6893 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6894 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6895 | out[i] = 0; |
| 6896 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6897 | (!ctxt->disableSAX)) |
| 6898 | ctxt->sax->characters(ctxt->userData, out, i); |
| 6899 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6900 | return; |
| 6901 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6902 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6903 | /* |
| 6904 | * We are seeing an entity reference |
| 6905 | */ |
| 6906 | ent = xmlParseEntityRef(ctxt); |
| 6907 | if (ent == NULL) return; |
| 6908 | if (!ctxt->wellFormed) |
| 6909 | return; |
| 6910 | was_checked = ent->checked; |
| 6911 | |
| 6912 | /* special case of predefined entities */ |
| 6913 | if ((ent->name == NULL) || |
| 6914 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 6915 | val = ent->content; |
| 6916 | if (val == NULL) return; |
| 6917 | /* |
| 6918 | * inline the entity. |
| 6919 | */ |
| 6920 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6921 | (!ctxt->disableSAX)) |
| 6922 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 6923 | return; |
| 6924 | } |
| 6925 | |
| 6926 | /* |
| 6927 | * The first reference to the entity trigger a parsing phase |
| 6928 | * where the ent->children is filled with the result from |
| 6929 | * the parsing. |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame^] | 6930 | * Note: external parsed entities will not be loaded, it is not |
| 6931 | * required for a non-validating parser, unless the parsing option |
| 6932 | * of validating, or substituting entities were given. Doing so is |
| 6933 | * far more secure as the parser will only process data coming from |
| 6934 | * the document entity by default. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6935 | */ |
Daniel Veillard | 4629ee0 | 2012-07-23 14:15:40 +0800 | [diff] [blame^] | 6936 | if ((ent->checked == 0) && |
| 6937 | ((ent->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY) || |
| 6938 | (ctxt->options & (XML_PARSE_NOENT | XML_PARSE_DTDVALID)))) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6939 | unsigned long oldnbent = ctxt->nbentities; |
| 6940 | |
| 6941 | /* |
| 6942 | * This is a bit hackish but this seems the best |
| 6943 | * way to make sure both SAX and DOM entity support |
| 6944 | * behaves okay. |
| 6945 | */ |
| 6946 | void *user_data; |
| 6947 | if (ctxt->userData == ctxt) |
| 6948 | user_data = NULL; |
| 6949 | else |
| 6950 | user_data = ctxt->userData; |
| 6951 | |
| 6952 | /* |
| 6953 | * Check that this entity is well formed |
| 6954 | * 4.3.2: An internal general parsed entity is well-formed |
| 6955 | * if its replacement text matches the production labeled |
| 6956 | * content. |
| 6957 | */ |
| 6958 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 6959 | ctxt->depth++; |
| 6960 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 6961 | user_data, &list); |
| 6962 | ctxt->depth--; |
| 6963 | |
| 6964 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 6965 | ctxt->depth++; |
| 6966 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 6967 | user_data, ctxt->depth, ent->URI, |
| 6968 | ent->ExternalID, &list); |
| 6969 | ctxt->depth--; |
| 6970 | } else { |
| 6971 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 6972 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6973 | "invalid entity type found\n", NULL); |
| 6974 | } |
| 6975 | |
| 6976 | /* |
| 6977 | * Store the number of entities needing parsing for this entity |
| 6978 | * content and do checkings |
| 6979 | */ |
| 6980 | ent->checked = ctxt->nbentities - oldnbent; |
| 6981 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6982 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6983 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6984 | return; |
| 6985 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6986 | if (xmlParserEntityCheck(ctxt, 0, ent)) { |
| 6987 | xmlFreeNodeList(list); |
| 6988 | return; |
| 6989 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6990 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6991 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 6992 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 6993 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 6994 | (ent->children == NULL)) { |
| 6995 | ent->children = list; |
| 6996 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6997 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6998 | * Prune it directly in the generated document |
| 6999 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7000 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7001 | if (((list->type == XML_TEXT_NODE) && |
| 7002 | (list->next == NULL)) || |
| 7003 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7004 | list->parent = (xmlNodePtr) ent; |
| 7005 | list = NULL; |
| 7006 | ent->owner = 1; |
| 7007 | } else { |
| 7008 | ent->owner = 0; |
| 7009 | while (list != NULL) { |
| 7010 | list->parent = (xmlNodePtr) ctxt->node; |
| 7011 | list->doc = ctxt->myDoc; |
| 7012 | if (list->next == NULL) |
| 7013 | ent->last = list; |
| 7014 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7015 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7016 | list = ent->children; |
| 7017 | #ifdef LIBXML_LEGACY_ENABLED |
| 7018 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7019 | xmlAddEntityReference(ent, list, NULL); |
| 7020 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7021 | } |
| 7022 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7023 | ent->owner = 1; |
| 7024 | while (list != NULL) { |
| 7025 | list->parent = (xmlNodePtr) ent; |
Rob Richards | c794eb5 | 2011-02-18 12:17:17 -0500 | [diff] [blame] | 7026 | xmlSetTreeDoc(list, ent->doc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7027 | if (list->next == NULL) |
| 7028 | ent->last = list; |
| 7029 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7030 | } |
| 7031 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7032 | } else { |
| 7033 | xmlFreeNodeList(list); |
| 7034 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7035 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7036 | } else if ((ret != XML_ERR_OK) && |
| 7037 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 7038 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7039 | "Entity '%s' failed to parse\n", ent->name); |
| 7040 | } else if (list != NULL) { |
| 7041 | xmlFreeNodeList(list); |
| 7042 | list = NULL; |
| 7043 | } |
| 7044 | if (ent->checked == 0) |
| 7045 | ent->checked = 1; |
| 7046 | } else if (ent->checked != 1) { |
| 7047 | ctxt->nbentities += ent->checked; |
| 7048 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 7049 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7050 | /* |
| 7051 | * Now that the entity content has been gathered |
| 7052 | * provide it to the application, this can take different forms based |
| 7053 | * on the parsing modes. |
| 7054 | */ |
| 7055 | if (ent->children == NULL) { |
| 7056 | /* |
| 7057 | * Probably running in SAX mode and the callbacks don't |
| 7058 | * build the entity content. So unless we already went |
| 7059 | * though parsing for first checking go though the entity |
| 7060 | * content to generate callbacks associated to the entity |
| 7061 | */ |
| 7062 | if (was_checked != 0) { |
| 7063 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7064 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7065 | * This is a bit hackish but this seems the best |
| 7066 | * way to make sure both SAX and DOM entity support |
| 7067 | * behaves okay. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7068 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7069 | if (ctxt->userData == ctxt) |
| 7070 | user_data = NULL; |
| 7071 | else |
| 7072 | user_data = ctxt->userData; |
| 7073 | |
| 7074 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 7075 | ctxt->depth++; |
| 7076 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 7077 | ent->content, user_data, NULL); |
| 7078 | ctxt->depth--; |
| 7079 | } else if (ent->etype == |
| 7080 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 7081 | ctxt->depth++; |
| 7082 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 7083 | ctxt->sax, user_data, ctxt->depth, |
| 7084 | ent->URI, ent->ExternalID, NULL); |
| 7085 | ctxt->depth--; |
| 7086 | } else { |
| 7087 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 7088 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7089 | "invalid entity type found\n", NULL); |
| 7090 | } |
| 7091 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 7092 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 7093 | return; |
| 7094 | } |
| 7095 | } |
| 7096 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7097 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7098 | /* |
| 7099 | * Entity reference callback comes second, it's somewhat |
| 7100 | * superfluous but a compatibility to historical behaviour |
| 7101 | */ |
| 7102 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7103 | } |
| 7104 | return; |
| 7105 | } |
| 7106 | |
| 7107 | /* |
| 7108 | * If we didn't get any children for the entity being built |
| 7109 | */ |
| 7110 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 7111 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 7112 | /* |
| 7113 | * Create a node. |
| 7114 | */ |
| 7115 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 7116 | return; |
| 7117 | } |
| 7118 | |
| 7119 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 7120 | /* |
| 7121 | * There is a problem on the handling of _private for entities |
| 7122 | * (bug 155816): Should we copy the content of the field from |
| 7123 | * the entity (possibly overwriting some value set by the user |
| 7124 | * when a copy is created), should we leave it alone, or should |
| 7125 | * we try to take care of different situations? The problem |
| 7126 | * is exacerbated by the usage of this field by the xmlReader. |
| 7127 | * To fix this bug, we look at _private on the created node |
| 7128 | * and, if it's NULL, we copy in whatever was in the entity. |
| 7129 | * If it's not NULL we leave it alone. This is somewhat of a |
| 7130 | * hack - maybe we should have further tests to determine |
| 7131 | * what to do. |
| 7132 | */ |
| 7133 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 7134 | /* |
| 7135 | * Seems we are generating the DOM content, do |
| 7136 | * a simple tree copy for all references except the first |
| 7137 | * In the first occurrence list contains the replacement. |
| 7138 | * progressive == 2 means we are operating on the Reader |
| 7139 | * and since nodes are discarded we must copy all the time. |
| 7140 | */ |
| 7141 | if (((list == NULL) && (ent->owner == 0)) || |
| 7142 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 7143 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 7144 | |
| 7145 | /* |
| 7146 | * when operating on a reader, the entities definitions |
| 7147 | * are always owning the entities subtree. |
| 7148 | if (ctxt->parseMode == XML_PARSE_READER) |
| 7149 | ent->owner = 1; |
| 7150 | */ |
| 7151 | |
| 7152 | cur = ent->children; |
| 7153 | while (cur != NULL) { |
| 7154 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7155 | if (nw != NULL) { |
| 7156 | if (nw->_private == NULL) |
| 7157 | nw->_private = cur->_private; |
| 7158 | if (firstChild == NULL){ |
| 7159 | firstChild = nw; |
| 7160 | } |
| 7161 | nw = xmlAddChild(ctxt->node, nw); |
| 7162 | } |
| 7163 | if (cur == ent->last) { |
| 7164 | /* |
| 7165 | * needed to detect some strange empty |
| 7166 | * node cases in the reader tests |
| 7167 | */ |
| 7168 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 7169 | (nw != NULL) && |
| 7170 | (nw->type == XML_ELEMENT_NODE) && |
| 7171 | (nw->children == NULL)) |
| 7172 | nw->extra = 1; |
| 7173 | |
| 7174 | break; |
| 7175 | } |
| 7176 | cur = cur->next; |
| 7177 | } |
| 7178 | #ifdef LIBXML_LEGACY_ENABLED |
| 7179 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7180 | xmlAddEntityReference(ent, firstChild, nw); |
| 7181 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7182 | } else if (list == NULL) { |
| 7183 | xmlNodePtr nw = NULL, cur, next, last, |
| 7184 | firstChild = NULL; |
| 7185 | /* |
| 7186 | * Copy the entity child list and make it the new |
| 7187 | * entity child list. The goal is to make sure any |
| 7188 | * ID or REF referenced will be the one from the |
| 7189 | * document content and not the entity copy. |
| 7190 | */ |
| 7191 | cur = ent->children; |
| 7192 | ent->children = NULL; |
| 7193 | last = ent->last; |
| 7194 | ent->last = NULL; |
| 7195 | while (cur != NULL) { |
| 7196 | next = cur->next; |
| 7197 | cur->next = NULL; |
| 7198 | cur->parent = NULL; |
| 7199 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 7200 | if (nw != NULL) { |
| 7201 | if (nw->_private == NULL) |
| 7202 | nw->_private = cur->_private; |
| 7203 | if (firstChild == NULL){ |
| 7204 | firstChild = cur; |
| 7205 | } |
| 7206 | xmlAddChild((xmlNodePtr) ent, nw); |
| 7207 | xmlAddChild(ctxt->node, cur); |
| 7208 | } |
| 7209 | if (cur == last) |
| 7210 | break; |
| 7211 | cur = next; |
| 7212 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 7213 | if (ent->owner == 0) |
| 7214 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7215 | #ifdef LIBXML_LEGACY_ENABLED |
| 7216 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 7217 | xmlAddEntityReference(ent, firstChild, nw); |
| 7218 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 7219 | } else { |
| 7220 | const xmlChar *nbktext; |
| 7221 | |
| 7222 | /* |
| 7223 | * the name change is to avoid coalescing of the |
| 7224 | * node with a possible previous text one which |
| 7225 | * would make ent->children a dangling pointer |
| 7226 | */ |
| 7227 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 7228 | -1); |
| 7229 | if (ent->children->type == XML_TEXT_NODE) |
| 7230 | ent->children->name = nbktext; |
| 7231 | if ((ent->last != ent->children) && |
| 7232 | (ent->last->type == XML_TEXT_NODE)) |
| 7233 | ent->last->name = nbktext; |
| 7234 | xmlAddChildList(ctxt->node, ent->children); |
| 7235 | } |
| 7236 | |
| 7237 | /* |
| 7238 | * This is to avoid a nasty side effect, see |
| 7239 | * characters() in SAX.c |
| 7240 | */ |
| 7241 | ctxt->nodemem = 0; |
| 7242 | ctxt->nodelen = 0; |
| 7243 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7244 | } |
| 7245 | } |
| 7246 | } |
| 7247 | |
| 7248 | /** |
| 7249 | * xmlParseEntityRef: |
| 7250 | * @ctxt: an XML parser context |
| 7251 | * |
| 7252 | * parse ENTITY references declarations |
| 7253 | * |
| 7254 | * [68] EntityRef ::= '&' Name ';' |
| 7255 | * |
| 7256 | * [ WFC: Entity Declared ] |
| 7257 | * In a document without any DTD, a document with only an internal DTD |
| 7258 | * subset which contains no parameter entity references, or a document |
| 7259 | * with "standalone='yes'", the Name given in the entity reference |
| 7260 | * must match that in an entity declaration, except that well-formed |
| 7261 | * documents need not declare any of the following entities: amp, lt, |
| 7262 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7263 | * any reference to it. Similarly, the declaration of a general entity |
| 7264 | * must precede any reference to it which appears in a default value in an |
| 7265 | * attribute-list declaration. Note that if entities are declared in the |
| 7266 | * external subset or in external parameter entities, a non-validating |
| 7267 | * processor is not obligated to read and process their declarations; |
| 7268 | * for such documents, the rule that an entity must be declared is a |
| 7269 | * well-formedness constraint only if standalone='yes'. |
| 7270 | * |
| 7271 | * [ WFC: Parsed Entity ] |
| 7272 | * An entity reference must not contain the name of an unparsed entity |
| 7273 | * |
| 7274 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7275 | */ |
| 7276 | xmlEntityPtr |
| 7277 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7278 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7279 | xmlEntityPtr ent = NULL; |
| 7280 | |
| 7281 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7282 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7283 | if (RAW != '&') |
| 7284 | return(NULL); |
| 7285 | NEXT; |
| 7286 | name = xmlParseName(ctxt); |
| 7287 | if (name == NULL) { |
| 7288 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7289 | "xmlParseEntityRef: no name\n"); |
| 7290 | return(NULL); |
| 7291 | } |
| 7292 | if (RAW != ';') { |
| 7293 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7294 | return(NULL); |
| 7295 | } |
| 7296 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7297 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7298 | /* |
| 7299 | * Predefined entites override any extra definition |
| 7300 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7301 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7302 | ent = xmlGetPredefinedEntity(name); |
| 7303 | if (ent != NULL) |
| 7304 | return(ent); |
| 7305 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7306 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7307 | /* |
| 7308 | * Increate the number of entity references parsed |
| 7309 | */ |
| 7310 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7311 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7312 | /* |
| 7313 | * Ask first SAX for entity resolution, otherwise try the |
| 7314 | * entities which may have stored in the parser context. |
| 7315 | */ |
| 7316 | if (ctxt->sax != NULL) { |
| 7317 | if (ctxt->sax->getEntity != NULL) |
| 7318 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7319 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7320 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7321 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7322 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7323 | (ctxt->userData==ctxt)) { |
| 7324 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7325 | } |
| 7326 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7327 | /* |
| 7328 | * [ WFC: Entity Declared ] |
| 7329 | * In a document without any DTD, a document with only an |
| 7330 | * internal DTD subset which contains no parameter entity |
| 7331 | * references, or a document with "standalone='yes'", the |
| 7332 | * Name given in the entity reference must match that in an |
| 7333 | * entity declaration, except that well-formed documents |
| 7334 | * need not declare any of the following entities: amp, lt, |
| 7335 | * gt, apos, quot. |
| 7336 | * The declaration of a parameter entity must precede any |
| 7337 | * reference to it. |
| 7338 | * Similarly, the declaration of a general entity must |
| 7339 | * precede any reference to it which appears in a default |
| 7340 | * value in an attribute-list declaration. Note that if |
| 7341 | * entities are declared in the external subset or in |
| 7342 | * external parameter entities, a non-validating processor |
| 7343 | * is not obligated to read and process their declarations; |
| 7344 | * for such documents, the rule that an entity must be |
| 7345 | * declared is a well-formedness constraint only if |
| 7346 | * standalone='yes'. |
| 7347 | */ |
| 7348 | if (ent == NULL) { |
| 7349 | if ((ctxt->standalone == 1) || |
| 7350 | ((ctxt->hasExternalSubset == 0) && |
| 7351 | (ctxt->hasPErefs == 0))) { |
| 7352 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7353 | "Entity '%s' not defined\n", name); |
| 7354 | } else { |
| 7355 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7356 | "Entity '%s' not defined\n", name); |
| 7357 | if ((ctxt->inSubset == 0) && |
| 7358 | (ctxt->sax != NULL) && |
| 7359 | (ctxt->sax->reference != NULL)) { |
| 7360 | ctxt->sax->reference(ctxt->userData, name); |
| 7361 | } |
| 7362 | } |
| 7363 | ctxt->valid = 0; |
| 7364 | } |
| 7365 | |
| 7366 | /* |
| 7367 | * [ WFC: Parsed Entity ] |
| 7368 | * An entity reference must not contain the name of an |
| 7369 | * unparsed entity |
| 7370 | */ |
| 7371 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7372 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7373 | "Entity reference to unparsed entity %s\n", name); |
| 7374 | } |
| 7375 | |
| 7376 | /* |
| 7377 | * [ WFC: No External Entity References ] |
| 7378 | * Attribute values cannot contain direct or indirect |
| 7379 | * entity references to external entities. |
| 7380 | */ |
| 7381 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7382 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7383 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7384 | "Attribute references external entity '%s'\n", name); |
| 7385 | } |
| 7386 | /* |
| 7387 | * [ WFC: No < in Attribute Values ] |
| 7388 | * The replacement text of any entity referred to directly or |
| 7389 | * indirectly in an attribute value (other than "<") must |
| 7390 | * not contain a <. |
| 7391 | */ |
| 7392 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7393 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7394 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7395 | (xmlStrchr(ent->content, '<'))) { |
| 7396 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7397 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7398 | } |
| 7399 | |
| 7400 | /* |
| 7401 | * Internal check, no parameter entities here ... |
| 7402 | */ |
| 7403 | else { |
| 7404 | switch (ent->etype) { |
| 7405 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7406 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7407 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7408 | "Attempt to reference the parameter entity '%s'\n", |
| 7409 | name); |
| 7410 | break; |
| 7411 | default: |
| 7412 | break; |
| 7413 | } |
| 7414 | } |
| 7415 | |
| 7416 | /* |
| 7417 | * [ WFC: No Recursion ] |
| 7418 | * A parsed entity must not contain a recursive reference |
| 7419 | * to itself, either directly or indirectly. |
| 7420 | * Done somewhere else |
| 7421 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7422 | return(ent); |
| 7423 | } |
| 7424 | |
| 7425 | /** |
| 7426 | * xmlParseStringEntityRef: |
| 7427 | * @ctxt: an XML parser context |
| 7428 | * @str: a pointer to an index in the string |
| 7429 | * |
| 7430 | * parse ENTITY references declarations, but this version parses it from |
| 7431 | * a string value. |
| 7432 | * |
| 7433 | * [68] EntityRef ::= '&' Name ';' |
| 7434 | * |
| 7435 | * [ WFC: Entity Declared ] |
| 7436 | * In a document without any DTD, a document with only an internal DTD |
| 7437 | * subset which contains no parameter entity references, or a document |
| 7438 | * with "standalone='yes'", the Name given in the entity reference |
| 7439 | * must match that in an entity declaration, except that well-formed |
| 7440 | * documents need not declare any of the following entities: amp, lt, |
| 7441 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7442 | * any reference to it. Similarly, the declaration of a general entity |
| 7443 | * must precede any reference to it which appears in a default value in an |
| 7444 | * attribute-list declaration. Note that if entities are declared in the |
| 7445 | * external subset or in external parameter entities, a non-validating |
| 7446 | * processor is not obligated to read and process their declarations; |
| 7447 | * for such documents, the rule that an entity must be declared is a |
| 7448 | * well-formedness constraint only if standalone='yes'. |
| 7449 | * |
| 7450 | * [ WFC: Parsed Entity ] |
| 7451 | * An entity reference must not contain the name of an unparsed entity |
| 7452 | * |
| 7453 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7454 | * is updated to the current location in the string. |
| 7455 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7456 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7457 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7458 | xmlChar *name; |
| 7459 | const xmlChar *ptr; |
| 7460 | xmlChar cur; |
| 7461 | xmlEntityPtr ent = NULL; |
| 7462 | |
| 7463 | if ((str == NULL) || (*str == NULL)) |
| 7464 | return(NULL); |
| 7465 | ptr = *str; |
| 7466 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7467 | if (cur != '&') |
| 7468 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7469 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7470 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7471 | name = xmlParseStringName(ctxt, &ptr); |
| 7472 | if (name == NULL) { |
| 7473 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7474 | "xmlParseStringEntityRef: no name\n"); |
| 7475 | *str = ptr; |
| 7476 | return(NULL); |
| 7477 | } |
| 7478 | if (*ptr != ';') { |
| 7479 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7480 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7481 | *str = ptr; |
| 7482 | return(NULL); |
| 7483 | } |
| 7484 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7485 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7486 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7487 | /* |
| 7488 | * Predefined entites override any extra definition |
| 7489 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7490 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7491 | ent = xmlGetPredefinedEntity(name); |
| 7492 | if (ent != NULL) { |
| 7493 | xmlFree(name); |
| 7494 | *str = ptr; |
| 7495 | return(ent); |
| 7496 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7497 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7498 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7499 | /* |
| 7500 | * Increate the number of entity references parsed |
| 7501 | */ |
| 7502 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7503 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7504 | /* |
| 7505 | * Ask first SAX for entity resolution, otherwise try the |
| 7506 | * entities which may have stored in the parser context. |
| 7507 | */ |
| 7508 | if (ctxt->sax != NULL) { |
| 7509 | if (ctxt->sax->getEntity != NULL) |
| 7510 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7511 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7512 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7513 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7514 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7515 | } |
| 7516 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7517 | |
| 7518 | /* |
| 7519 | * [ WFC: Entity Declared ] |
| 7520 | * In a document without any DTD, a document with only an |
| 7521 | * internal DTD subset which contains no parameter entity |
| 7522 | * references, or a document with "standalone='yes'", the |
| 7523 | * Name given in the entity reference must match that in an |
| 7524 | * entity declaration, except that well-formed documents |
| 7525 | * need not declare any of the following entities: amp, lt, |
| 7526 | * gt, apos, quot. |
| 7527 | * The declaration of a parameter entity must precede any |
| 7528 | * reference to it. |
| 7529 | * Similarly, the declaration of a general entity must |
| 7530 | * precede any reference to it which appears in a default |
| 7531 | * value in an attribute-list declaration. Note that if |
| 7532 | * entities are declared in the external subset or in |
| 7533 | * external parameter entities, a non-validating processor |
| 7534 | * is not obligated to read and process their declarations; |
| 7535 | * for such documents, the rule that an entity must be |
| 7536 | * declared is a well-formedness constraint only if |
| 7537 | * standalone='yes'. |
| 7538 | */ |
| 7539 | if (ent == NULL) { |
| 7540 | if ((ctxt->standalone == 1) || |
| 7541 | ((ctxt->hasExternalSubset == 0) && |
| 7542 | (ctxt->hasPErefs == 0))) { |
| 7543 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7544 | "Entity '%s' not defined\n", name); |
| 7545 | } else { |
| 7546 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7547 | "Entity '%s' not defined\n", |
| 7548 | name); |
| 7549 | } |
| 7550 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7551 | } |
| 7552 | |
| 7553 | /* |
| 7554 | * [ WFC: Parsed Entity ] |
| 7555 | * An entity reference must not contain the name of an |
| 7556 | * unparsed entity |
| 7557 | */ |
| 7558 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7559 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7560 | "Entity reference to unparsed entity %s\n", name); |
| 7561 | } |
| 7562 | |
| 7563 | /* |
| 7564 | * [ WFC: No External Entity References ] |
| 7565 | * Attribute values cannot contain direct or indirect |
| 7566 | * entity references to external entities. |
| 7567 | */ |
| 7568 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7569 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7570 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7571 | "Attribute references external entity '%s'\n", name); |
| 7572 | } |
| 7573 | /* |
| 7574 | * [ WFC: No < in Attribute Values ] |
| 7575 | * The replacement text of any entity referred to directly or |
| 7576 | * indirectly in an attribute value (other than "<") must |
| 7577 | * not contain a <. |
| 7578 | */ |
| 7579 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7580 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 7581 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7582 | (xmlStrchr(ent->content, '<'))) { |
| 7583 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7584 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7585 | name); |
| 7586 | } |
| 7587 | |
| 7588 | /* |
| 7589 | * Internal check, no parameter entities here ... |
| 7590 | */ |
| 7591 | else { |
| 7592 | switch (ent->etype) { |
| 7593 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7594 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7595 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7596 | "Attempt to reference the parameter entity '%s'\n", |
| 7597 | name); |
| 7598 | break; |
| 7599 | default: |
| 7600 | break; |
| 7601 | } |
| 7602 | } |
| 7603 | |
| 7604 | /* |
| 7605 | * [ WFC: No Recursion ] |
| 7606 | * A parsed entity must not contain a recursive reference |
| 7607 | * to itself, either directly or indirectly. |
| 7608 | * Done somewhere else |
| 7609 | */ |
| 7610 | |
| 7611 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7612 | *str = ptr; |
| 7613 | return(ent); |
| 7614 | } |
| 7615 | |
| 7616 | /** |
| 7617 | * xmlParsePEReference: |
| 7618 | * @ctxt: an XML parser context |
| 7619 | * |
| 7620 | * parse PEReference declarations |
| 7621 | * The entity content is handled directly by pushing it's content as |
| 7622 | * a new input stream. |
| 7623 | * |
| 7624 | * [69] PEReference ::= '%' Name ';' |
| 7625 | * |
| 7626 | * [ WFC: No Recursion ] |
| 7627 | * A parsed entity must not contain a recursive |
| 7628 | * reference to itself, either directly or indirectly. |
| 7629 | * |
| 7630 | * [ WFC: Entity Declared ] |
| 7631 | * In a document without any DTD, a document with only an internal DTD |
| 7632 | * subset which contains no parameter entity references, or a document |
| 7633 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7634 | * entity must precede any reference to it... |
| 7635 | * |
| 7636 | * [ VC: Entity Declared ] |
| 7637 | * In a document with an external subset or external parameter entities |
| 7638 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7639 | * must precede any reference to it... |
| 7640 | * |
| 7641 | * [ WFC: In DTD ] |
| 7642 | * Parameter-entity references may only appear in the DTD. |
| 7643 | * NOTE: misleading but this is handled. |
| 7644 | */ |
| 7645 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7646 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7647 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7648 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7649 | xmlEntityPtr entity = NULL; |
| 7650 | xmlParserInputPtr input; |
| 7651 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7652 | if (RAW != '%') |
| 7653 | return; |
| 7654 | NEXT; |
| 7655 | name = xmlParseName(ctxt); |
| 7656 | if (name == NULL) { |
| 7657 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7658 | "xmlParsePEReference: no name\n"); |
| 7659 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7660 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7661 | if (RAW != ';') { |
| 7662 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7663 | return; |
| 7664 | } |
| 7665 | |
| 7666 | NEXT; |
| 7667 | |
| 7668 | /* |
| 7669 | * Increate the number of entity references parsed |
| 7670 | */ |
| 7671 | ctxt->nbentities++; |
| 7672 | |
| 7673 | /* |
| 7674 | * Request the entity from SAX |
| 7675 | */ |
| 7676 | if ((ctxt->sax != NULL) && |
| 7677 | (ctxt->sax->getParameterEntity != NULL)) |
| 7678 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7679 | name); |
| 7680 | if (entity == NULL) { |
| 7681 | /* |
| 7682 | * [ WFC: Entity Declared ] |
| 7683 | * In a document without any DTD, a document with only an |
| 7684 | * internal DTD subset which contains no parameter entity |
| 7685 | * references, or a document with "standalone='yes'", ... |
| 7686 | * ... The declaration of a parameter entity must precede |
| 7687 | * any reference to it... |
| 7688 | */ |
| 7689 | if ((ctxt->standalone == 1) || |
| 7690 | ((ctxt->hasExternalSubset == 0) && |
| 7691 | (ctxt->hasPErefs == 0))) { |
| 7692 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7693 | "PEReference: %%%s; not found\n", |
| 7694 | name); |
| 7695 | } else { |
| 7696 | /* |
| 7697 | * [ VC: Entity Declared ] |
| 7698 | * In a document with an external subset or external |
| 7699 | * parameter entities with "standalone='no'", ... |
| 7700 | * ... The declaration of a parameter entity must |
| 7701 | * precede any reference to it... |
| 7702 | */ |
| 7703 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7704 | "PEReference: %%%s; not found\n", |
| 7705 | name, NULL); |
| 7706 | ctxt->valid = 0; |
| 7707 | } |
| 7708 | } else { |
| 7709 | /* |
| 7710 | * Internal checking in case the entity quest barfed |
| 7711 | */ |
| 7712 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7713 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7714 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7715 | "Internal: %%%s; is not a parameter entity\n", |
| 7716 | name, NULL); |
| 7717 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 7718 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 7719 | if (xmlPushInput(ctxt, input) < 0) |
| 7720 | return; |
| 7721 | } else { |
| 7722 | /* |
| 7723 | * TODO !!! |
| 7724 | * handle the extra spaces added before and after |
| 7725 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 7726 | */ |
| 7727 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7728 | if (xmlPushInput(ctxt, input) < 0) |
| 7729 | return; |
| 7730 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7731 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 7732 | (IS_BLANK_CH(NXT(5)))) { |
| 7733 | xmlParseTextDecl(ctxt); |
| 7734 | if (ctxt->errNo == |
| 7735 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 7736 | /* |
| 7737 | * The XML REC instructs us to stop parsing |
| 7738 | * right here |
| 7739 | */ |
| 7740 | ctxt->instate = XML_PARSER_EOF; |
| 7741 | return; |
| 7742 | } |
| 7743 | } |
| 7744 | } |
| 7745 | } |
| 7746 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7747 | } |
| 7748 | |
| 7749 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7750 | * xmlLoadEntityContent: |
| 7751 | * @ctxt: an XML parser context |
| 7752 | * @entity: an unloaded system entity |
| 7753 | * |
| 7754 | * Load the original content of the given system entity from the |
| 7755 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 7756 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 7757 | * |
| 7758 | * Returns 0 in case of success and -1 in case of failure |
| 7759 | */ |
| 7760 | static int |
| 7761 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 7762 | xmlParserInputPtr input; |
| 7763 | xmlBufferPtr buf; |
| 7764 | int l, c; |
| 7765 | int count = 0; |
| 7766 | |
| 7767 | if ((ctxt == NULL) || (entity == NULL) || |
| 7768 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7769 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 7770 | (entity->content != NULL)) { |
| 7771 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7772 | "xmlLoadEntityContent parameter error"); |
| 7773 | return(-1); |
| 7774 | } |
| 7775 | |
| 7776 | if (xmlParserDebugEntities) |
| 7777 | xmlGenericError(xmlGenericErrorContext, |
| 7778 | "Reading %s entity content input\n", entity->name); |
| 7779 | |
| 7780 | buf = xmlBufferCreate(); |
| 7781 | if (buf == NULL) { |
| 7782 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7783 | "xmlLoadEntityContent parameter error"); |
| 7784 | return(-1); |
| 7785 | } |
| 7786 | |
| 7787 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7788 | if (input == NULL) { |
| 7789 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7790 | "xmlLoadEntityContent input error"); |
| 7791 | xmlBufferFree(buf); |
| 7792 | return(-1); |
| 7793 | } |
| 7794 | |
| 7795 | /* |
| 7796 | * Push the entity as the current input, read char by char |
| 7797 | * saving to the buffer until the end of the entity or an error |
| 7798 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 7799 | if (xmlPushInput(ctxt, input) < 0) { |
| 7800 | xmlBufferFree(buf); |
| 7801 | return(-1); |
| 7802 | } |
| 7803 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7804 | GROW; |
| 7805 | c = CUR_CHAR(l); |
| 7806 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 7807 | (IS_CHAR(c))) { |
| 7808 | xmlBufferAdd(buf, ctxt->input->cur, l); |
| 7809 | if (count++ > 100) { |
| 7810 | count = 0; |
| 7811 | GROW; |
| 7812 | } |
| 7813 | NEXTL(l); |
| 7814 | c = CUR_CHAR(l); |
| 7815 | } |
| 7816 | |
| 7817 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 7818 | xmlPopInput(ctxt); |
| 7819 | } else if (!IS_CHAR(c)) { |
| 7820 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 7821 | "xmlLoadEntityContent: invalid char value %d\n", |
| 7822 | c); |
| 7823 | xmlBufferFree(buf); |
| 7824 | return(-1); |
| 7825 | } |
| 7826 | entity->content = buf->content; |
| 7827 | buf->content = NULL; |
| 7828 | xmlBufferFree(buf); |
| 7829 | |
| 7830 | return(0); |
| 7831 | } |
| 7832 | |
| 7833 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7834 | * xmlParseStringPEReference: |
| 7835 | * @ctxt: an XML parser context |
| 7836 | * @str: a pointer to an index in the string |
| 7837 | * |
| 7838 | * parse PEReference declarations |
| 7839 | * |
| 7840 | * [69] PEReference ::= '%' Name ';' |
| 7841 | * |
| 7842 | * [ WFC: No Recursion ] |
| 7843 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7844 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7845 | * |
| 7846 | * [ WFC: Entity Declared ] |
| 7847 | * In a document without any DTD, a document with only an internal DTD |
| 7848 | * subset which contains no parameter entity references, or a document |
| 7849 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7850 | * entity must precede any reference to it... |
| 7851 | * |
| 7852 | * [ VC: Entity Declared ] |
| 7853 | * In a document with an external subset or external parameter entities |
| 7854 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7855 | * must precede any reference to it... |
| 7856 | * |
| 7857 | * [ WFC: In DTD ] |
| 7858 | * Parameter-entity references may only appear in the DTD. |
| 7859 | * NOTE: misleading but this is handled. |
| 7860 | * |
| 7861 | * Returns the string of the entity content. |
| 7862 | * str is updated to the current value of the index |
| 7863 | */ |
Daniel Veillard | 8ed1072 | 2009-08-20 19:17:36 +0200 | [diff] [blame] | 7864 | static xmlEntityPtr |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7865 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 7866 | const xmlChar *ptr; |
| 7867 | xmlChar cur; |
| 7868 | xmlChar *name; |
| 7869 | xmlEntityPtr entity = NULL; |
| 7870 | |
| 7871 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 7872 | ptr = *str; |
| 7873 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7874 | if (cur != '%') |
| 7875 | return(NULL); |
| 7876 | ptr++; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7877 | name = xmlParseStringName(ctxt, &ptr); |
| 7878 | if (name == NULL) { |
| 7879 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7880 | "xmlParseStringPEReference: no name\n"); |
| 7881 | *str = ptr; |
| 7882 | return(NULL); |
| 7883 | } |
| 7884 | cur = *ptr; |
| 7885 | if (cur != ';') { |
| 7886 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7887 | xmlFree(name); |
| 7888 | *str = ptr; |
| 7889 | return(NULL); |
| 7890 | } |
| 7891 | ptr++; |
| 7892 | |
| 7893 | /* |
| 7894 | * Increate the number of entity references parsed |
| 7895 | */ |
| 7896 | ctxt->nbentities++; |
| 7897 | |
| 7898 | /* |
| 7899 | * Request the entity from SAX |
| 7900 | */ |
| 7901 | if ((ctxt->sax != NULL) && |
| 7902 | (ctxt->sax->getParameterEntity != NULL)) |
| 7903 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7904 | name); |
| 7905 | if (entity == NULL) { |
| 7906 | /* |
| 7907 | * [ WFC: Entity Declared ] |
| 7908 | * In a document without any DTD, a document with only an |
| 7909 | * internal DTD subset which contains no parameter entity |
| 7910 | * references, or a document with "standalone='yes'", ... |
| 7911 | * ... The declaration of a parameter entity must precede |
| 7912 | * any reference to it... |
| 7913 | */ |
| 7914 | if ((ctxt->standalone == 1) || |
| 7915 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 7916 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7917 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7918 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7919 | /* |
| 7920 | * [ VC: Entity Declared ] |
| 7921 | * In a document with an external subset or external |
| 7922 | * parameter entities with "standalone='no'", ... |
| 7923 | * ... The declaration of a parameter entity must |
| 7924 | * precede any reference to it... |
| 7925 | */ |
| 7926 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7927 | "PEReference: %%%s; not found\n", |
| 7928 | name, NULL); |
| 7929 | ctxt->valid = 0; |
| 7930 | } |
| 7931 | } else { |
| 7932 | /* |
| 7933 | * Internal checking in case the entity quest barfed |
| 7934 | */ |
| 7935 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7936 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7937 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7938 | "%%%s; is not a parameter entity\n", |
| 7939 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7940 | } |
| 7941 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7942 | ctxt->hasPErefs = 1; |
| 7943 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7944 | *str = ptr; |
| 7945 | return(entity); |
| 7946 | } |
| 7947 | |
| 7948 | /** |
| 7949 | * xmlParseDocTypeDecl: |
| 7950 | * @ctxt: an XML parser context |
| 7951 | * |
| 7952 | * parse a DOCTYPE declaration |
| 7953 | * |
| 7954 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 7955 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 7956 | * |
| 7957 | * [ VC: Root Element Type ] |
| 7958 | * The Name in the document type declaration must match the element |
| 7959 | * type of the root element. |
| 7960 | */ |
| 7961 | |
| 7962 | void |
| 7963 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7964 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7965 | xmlChar *ExternalID = NULL; |
| 7966 | xmlChar *URI = NULL; |
| 7967 | |
| 7968 | /* |
| 7969 | * We know that '<!DOCTYPE' has been detected. |
| 7970 | */ |
| 7971 | SKIP(9); |
| 7972 | |
| 7973 | SKIP_BLANKS; |
| 7974 | |
| 7975 | /* |
| 7976 | * Parse the DOCTYPE name. |
| 7977 | */ |
| 7978 | name = xmlParseName(ctxt); |
| 7979 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7980 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7981 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7982 | } |
| 7983 | ctxt->intSubName = name; |
| 7984 | |
| 7985 | SKIP_BLANKS; |
| 7986 | |
| 7987 | /* |
| 7988 | * Check for SystemID and ExternalID |
| 7989 | */ |
| 7990 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 7991 | |
| 7992 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 7993 | ctxt->hasExternalSubset = 1; |
| 7994 | } |
| 7995 | ctxt->extSubURI = URI; |
| 7996 | ctxt->extSubSystem = ExternalID; |
| 7997 | |
| 7998 | SKIP_BLANKS; |
| 7999 | |
| 8000 | /* |
| 8001 | * Create and update the internal subset. |
| 8002 | */ |
| 8003 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 8004 | (!ctxt->disableSAX)) |
| 8005 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 8006 | |
| 8007 | /* |
| 8008 | * Is there any internal subset declarations ? |
| 8009 | * they are handled separately in xmlParseInternalSubset() |
| 8010 | */ |
| 8011 | if (RAW == '[') |
| 8012 | return; |
| 8013 | |
| 8014 | /* |
| 8015 | * We should be at the end of the DOCTYPE declaration. |
| 8016 | */ |
| 8017 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8018 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8019 | } |
| 8020 | NEXT; |
| 8021 | } |
| 8022 | |
| 8023 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8024 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8025 | * @ctxt: an XML parser context |
| 8026 | * |
| 8027 | * parse the internal subset declaration |
| 8028 | * |
| 8029 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 8030 | */ |
| 8031 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8032 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8033 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 8034 | /* |
| 8035 | * Is there any DTD definition ? |
| 8036 | */ |
| 8037 | if (RAW == '[') { |
| 8038 | ctxt->instate = XML_PARSER_DTD; |
| 8039 | NEXT; |
| 8040 | /* |
| 8041 | * Parse the succession of Markup declarations and |
| 8042 | * PEReferences. |
| 8043 | * Subsequence (markupdecl | PEReference | S)* |
| 8044 | */ |
| 8045 | while (RAW != ']') { |
| 8046 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8047 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8048 | |
| 8049 | SKIP_BLANKS; |
| 8050 | xmlParseMarkupDecl(ctxt); |
| 8051 | xmlParsePEReference(ctxt); |
| 8052 | |
| 8053 | /* |
| 8054 | * Pop-up of finished entities. |
| 8055 | */ |
| 8056 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8057 | xmlPopInput(ctxt); |
| 8058 | |
| 8059 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8060 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8061 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8062 | break; |
| 8063 | } |
| 8064 | } |
| 8065 | if (RAW == ']') { |
| 8066 | NEXT; |
| 8067 | SKIP_BLANKS; |
| 8068 | } |
| 8069 | } |
| 8070 | |
| 8071 | /* |
| 8072 | * We should be at the end of the DOCTYPE declaration. |
| 8073 | */ |
| 8074 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8075 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8076 | } |
| 8077 | NEXT; |
| 8078 | } |
| 8079 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8080 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8081 | /** |
| 8082 | * xmlParseAttribute: |
| 8083 | * @ctxt: an XML parser context |
| 8084 | * @value: a xmlChar ** used to store the value of the attribute |
| 8085 | * |
| 8086 | * parse an attribute |
| 8087 | * |
| 8088 | * [41] Attribute ::= Name Eq AttValue |
| 8089 | * |
| 8090 | * [ WFC: No External Entity References ] |
| 8091 | * Attribute values cannot contain direct or indirect entity references |
| 8092 | * to external entities. |
| 8093 | * |
| 8094 | * [ WFC: No < in Attribute Values ] |
| 8095 | * The replacement text of any entity referred to directly or indirectly in |
| 8096 | * an attribute value (other than "<") must not contain a <. |
| 8097 | * |
| 8098 | * [ VC: Attribute Value Type ] |
| 8099 | * The attribute must have been declared; the value must be of the type |
| 8100 | * declared for it. |
| 8101 | * |
| 8102 | * [25] Eq ::= S? '=' S? |
| 8103 | * |
| 8104 | * With namespace: |
| 8105 | * |
| 8106 | * [NS 11] Attribute ::= QName Eq AttValue |
| 8107 | * |
| 8108 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 8109 | * definition. |
| 8110 | * |
| 8111 | * Returns the attribute name, and the value in *value. |
| 8112 | */ |
| 8113 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8114 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8115 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8116 | const xmlChar *name; |
| 8117 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8118 | |
| 8119 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 8120 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8121 | name = xmlParseName(ctxt); |
| 8122 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8123 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8124 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8125 | return(NULL); |
| 8126 | } |
| 8127 | |
| 8128 | /* |
| 8129 | * read the value |
| 8130 | */ |
| 8131 | SKIP_BLANKS; |
| 8132 | if (RAW == '=') { |
| 8133 | NEXT; |
| 8134 | SKIP_BLANKS; |
| 8135 | val = xmlParseAttValue(ctxt); |
| 8136 | ctxt->instate = XML_PARSER_CONTENT; |
| 8137 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8138 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8139 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8140 | return(NULL); |
| 8141 | } |
| 8142 | |
| 8143 | /* |
| 8144 | * Check that xml:lang conforms to the specification |
| 8145 | * No more registered as an error, just generate a warning now |
| 8146 | * since this was deprecated in XML second edition |
| 8147 | */ |
| 8148 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 8149 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8150 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8151 | "Malformed value for xml:lang : %s\n", |
| 8152 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8153 | } |
| 8154 | } |
| 8155 | |
| 8156 | /* |
| 8157 | * Check that xml:space conforms to the specification |
| 8158 | */ |
| 8159 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 8160 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 8161 | *(ctxt->space) = 0; |
| 8162 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 8163 | *(ctxt->space) = 1; |
| 8164 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8165 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 8166 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8167 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | *value = val; |
| 8172 | return(name); |
| 8173 | } |
| 8174 | |
| 8175 | /** |
| 8176 | * xmlParseStartTag: |
| 8177 | * @ctxt: an XML parser context |
| 8178 | * |
| 8179 | * parse a start of tag either for rule element or |
| 8180 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8181 | * |
| 8182 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8183 | * |
| 8184 | * [ WFC: Unique Att Spec ] |
| 8185 | * No attribute name may appear more than once in the same start-tag or |
| 8186 | * empty-element tag. |
| 8187 | * |
| 8188 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8189 | * |
| 8190 | * [ WFC: Unique Att Spec ] |
| 8191 | * No attribute name may appear more than once in the same start-tag or |
| 8192 | * empty-element tag. |
| 8193 | * |
| 8194 | * With namespace: |
| 8195 | * |
| 8196 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8197 | * |
| 8198 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8199 | * |
| 8200 | * Returns the element name parsed |
| 8201 | */ |
| 8202 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8203 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8204 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8205 | const xmlChar *name; |
| 8206 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8207 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8208 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8209 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8210 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8211 | int i; |
| 8212 | |
| 8213 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8214 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8215 | |
| 8216 | name = xmlParseName(ctxt); |
| 8217 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8218 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8219 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | return(NULL); |
| 8221 | } |
| 8222 | |
| 8223 | /* |
| 8224 | * Now parse the attributes, it ends up with the ending |
| 8225 | * |
| 8226 | * (S Attribute)* S? |
| 8227 | */ |
| 8228 | SKIP_BLANKS; |
| 8229 | GROW; |
| 8230 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8231 | while ((RAW != '>') && |
| 8232 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8233 | (IS_BYTE_CHAR(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8234 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8235 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8236 | |
| 8237 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 8238 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8239 | /* |
| 8240 | * [ WFC: Unique Att Spec ] |
| 8241 | * No attribute name may appear more than once in the same |
| 8242 | * start-tag or empty-element tag. |
| 8243 | */ |
| 8244 | for (i = 0; i < nbatts;i += 2) { |
| 8245 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8246 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8247 | xmlFree(attvalue); |
| 8248 | goto failed; |
| 8249 | } |
| 8250 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8251 | /* |
| 8252 | * Add the pair to atts |
| 8253 | */ |
| 8254 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8255 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8256 | atts = (const xmlChar **) |
| 8257 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8258 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8259 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8260 | if (attvalue != NULL) |
| 8261 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8262 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8263 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8264 | ctxt->atts = atts; |
| 8265 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8266 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8267 | const xmlChar **n; |
| 8268 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8269 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8270 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8271 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8272 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8273 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8274 | if (attvalue != NULL) |
| 8275 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8276 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8277 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8278 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8279 | ctxt->atts = atts; |
| 8280 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8281 | } |
| 8282 | atts[nbatts++] = attname; |
| 8283 | atts[nbatts++] = attvalue; |
| 8284 | atts[nbatts] = NULL; |
| 8285 | atts[nbatts + 1] = NULL; |
| 8286 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8287 | if (attvalue != NULL) |
| 8288 | xmlFree(attvalue); |
| 8289 | } |
| 8290 | |
| 8291 | failed: |
| 8292 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8293 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8294 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8295 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8296 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8297 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8298 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8299 | } |
| 8300 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8301 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8302 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8303 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8304 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8305 | break; |
| 8306 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8307 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8308 | GROW; |
| 8309 | } |
| 8310 | |
| 8311 | /* |
| 8312 | * SAX: Start of Element ! |
| 8313 | */ |
| 8314 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8315 | (!ctxt->disableSAX)) { |
| 8316 | if (nbatts > 0) |
| 8317 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8318 | else |
| 8319 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8320 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8321 | |
| 8322 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8323 | /* Free only the content strings */ |
| 8324 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8325 | if (atts[i] != NULL) |
| 8326 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8327 | } |
| 8328 | return(name); |
| 8329 | } |
| 8330 | |
| 8331 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8332 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8334 | * @line: line of the start tag |
| 8335 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8336 | * |
| 8337 | * parse an end of tag |
| 8338 | * |
| 8339 | * [42] ETag ::= '</' Name S? '>' |
| 8340 | * |
| 8341 | * With namespace |
| 8342 | * |
| 8343 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8344 | */ |
| 8345 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8346 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8347 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8348 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8349 | |
| 8350 | GROW; |
| 8351 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8352 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8353 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8354 | return; |
| 8355 | } |
| 8356 | SKIP(2); |
| 8357 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8358 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | |
| 8360 | /* |
| 8361 | * We should definitely be at the ending "S? '>'" part |
| 8362 | */ |
| 8363 | GROW; |
| 8364 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8365 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8366 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8367 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8368 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8369 | |
| 8370 | /* |
| 8371 | * [ WFC: Element Type Match ] |
| 8372 | * The Name in an element's end-tag must match the element type in the |
| 8373 | * start-tag. |
| 8374 | * |
| 8375 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8376 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8377 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8378 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8379 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8380 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8381 | } |
| 8382 | |
| 8383 | /* |
| 8384 | * SAX: End of Tag |
| 8385 | */ |
| 8386 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8387 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8388 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8389 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8390 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8391 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8392 | return; |
| 8393 | } |
| 8394 | |
| 8395 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8396 | * xmlParseEndTag: |
| 8397 | * @ctxt: an XML parser context |
| 8398 | * |
| 8399 | * parse an end of tag |
| 8400 | * |
| 8401 | * [42] ETag ::= '</' Name S? '>' |
| 8402 | * |
| 8403 | * With namespace |
| 8404 | * |
| 8405 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8406 | */ |
| 8407 | |
| 8408 | void |
| 8409 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8410 | xmlParseEndTag1(ctxt, 0); |
| 8411 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8412 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8413 | |
| 8414 | /************************************************************************ |
| 8415 | * * |
| 8416 | * SAX 2 specific operations * |
| 8417 | * * |
| 8418 | ************************************************************************/ |
| 8419 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8420 | /* |
| 8421 | * xmlGetNamespace: |
| 8422 | * @ctxt: an XML parser context |
| 8423 | * @prefix: the prefix to lookup |
| 8424 | * |
| 8425 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8426 | * The prefix must come from the @ctxt->dict dictionnary |
| 8427 | * |
| 8428 | * Returns the namespace name or NULL if not bound |
| 8429 | */ |
| 8430 | static const xmlChar * |
| 8431 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8432 | int i; |
| 8433 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8434 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8435 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8436 | if (ctxt->nsTab[i] == prefix) { |
| 8437 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8438 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8439 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8440 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8441 | return(NULL); |
| 8442 | } |
| 8443 | |
| 8444 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8445 | * xmlParseQName: |
| 8446 | * @ctxt: an XML parser context |
| 8447 | * @prefix: pointer to store the prefix part |
| 8448 | * |
| 8449 | * parse an XML Namespace QName |
| 8450 | * |
| 8451 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8452 | * [7] Prefix ::= NCName |
| 8453 | * [8] LocalPart ::= NCName |
| 8454 | * |
| 8455 | * Returns the Name parsed or NULL |
| 8456 | */ |
| 8457 | |
| 8458 | static const xmlChar * |
| 8459 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8460 | const xmlChar *l, *p; |
| 8461 | |
| 8462 | GROW; |
| 8463 | |
| 8464 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8465 | if (l == NULL) { |
| 8466 | if (CUR == ':') { |
| 8467 | l = xmlParseName(ctxt); |
| 8468 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8469 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8470 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8471 | *prefix = NULL; |
| 8472 | return(l); |
| 8473 | } |
| 8474 | } |
| 8475 | return(NULL); |
| 8476 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8477 | if (CUR == ':') { |
| 8478 | NEXT; |
| 8479 | p = l; |
| 8480 | l = xmlParseNCName(ctxt); |
| 8481 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8482 | xmlChar *tmp; |
| 8483 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8484 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8485 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8486 | l = xmlParseNmtoken(ctxt); |
| 8487 | if (l == NULL) |
| 8488 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8489 | else { |
| 8490 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8491 | xmlFree((char *)l); |
| 8492 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8493 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8494 | if (tmp != NULL) xmlFree(tmp); |
| 8495 | *prefix = NULL; |
| 8496 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8497 | } |
| 8498 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8499 | xmlChar *tmp; |
| 8500 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8501 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8502 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8503 | NEXT; |
| 8504 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8505 | if (tmp != NULL) { |
| 8506 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8507 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8508 | if (tmp != NULL) xmlFree(tmp); |
| 8509 | *prefix = p; |
| 8510 | return(l); |
| 8511 | } |
| 8512 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8513 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8514 | if (tmp != NULL) xmlFree(tmp); |
| 8515 | *prefix = p; |
| 8516 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8517 | } |
| 8518 | *prefix = p; |
| 8519 | } else |
| 8520 | *prefix = NULL; |
| 8521 | return(l); |
| 8522 | } |
| 8523 | |
| 8524 | /** |
| 8525 | * xmlParseQNameAndCompare: |
| 8526 | * @ctxt: an XML parser context |
| 8527 | * @name: the localname |
| 8528 | * @prefix: the prefix, if any. |
| 8529 | * |
| 8530 | * parse an XML name and compares for match |
| 8531 | * (specialized for endtag parsing) |
| 8532 | * |
| 8533 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8534 | * and the name for mismatch |
| 8535 | */ |
| 8536 | |
| 8537 | static const xmlChar * |
| 8538 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8539 | xmlChar const *prefix) { |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8540 | const xmlChar *cmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8541 | const xmlChar *in; |
| 8542 | const xmlChar *ret; |
| 8543 | const xmlChar *prefix2; |
| 8544 | |
| 8545 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8546 | |
| 8547 | GROW; |
| 8548 | in = ctxt->input->cur; |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 8549 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8550 | cmp = prefix; |
| 8551 | while (*in != 0 && *in == *cmp) { |
| 8552 | ++in; |
| 8553 | ++cmp; |
| 8554 | } |
| 8555 | if ((*cmp == 0) && (*in == ':')) { |
| 8556 | in++; |
| 8557 | cmp = name; |
| 8558 | while (*in != 0 && *in == *cmp) { |
| 8559 | ++in; |
| 8560 | ++cmp; |
| 8561 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8562 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8563 | /* success */ |
| 8564 | ctxt->input->cur = in; |
| 8565 | return((const xmlChar*) 1); |
| 8566 | } |
| 8567 | } |
| 8568 | /* |
| 8569 | * all strings coms from the dictionary, equality can be done directly |
| 8570 | */ |
| 8571 | ret = xmlParseQName (ctxt, &prefix2); |
| 8572 | if ((ret == name) && (prefix == prefix2)) |
| 8573 | return((const xmlChar*) 1); |
| 8574 | return ret; |
| 8575 | } |
| 8576 | |
| 8577 | /** |
| 8578 | * xmlParseAttValueInternal: |
| 8579 | * @ctxt: an XML parser context |
| 8580 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8581 | * @alloc: whether the attribute was reallocated as a new string |
| 8582 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8583 | * |
| 8584 | * parse a value for an attribute. |
| 8585 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8586 | * directly from the data buffer. |
| 8587 | * |
| 8588 | * 3.3.3 Attribute-Value Normalization: |
| 8589 | * Before the value of an attribute is passed to the application or |
| 8590 | * checked for validity, the XML processor must normalize it as follows: |
| 8591 | * - a character reference is processed by appending the referenced |
| 8592 | * character to the attribute value |
| 8593 | * - an entity reference is processed by recursively processing the |
| 8594 | * replacement text of the entity |
| 8595 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8596 | * appending #x20 to the normalized value, except that only a single |
| 8597 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 8598 | * parsed entity or the literal entity value of an internal parsed entity |
| 8599 | * - other characters are processed by appending them to the normalized value |
| 8600 | * If the declared value is not CDATA, then the XML processor must further |
| 8601 | * process the normalized attribute value by discarding any leading and |
| 8602 | * trailing space (#x20) characters, and by replacing sequences of space |
| 8603 | * (#x20) characters by a single space (#x20) character. |
| 8604 | * All attributes for which no declaration has been read should be treated |
| 8605 | * by a non-validating parser as if declared CDATA. |
| 8606 | * |
| 8607 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8608 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8609 | */ |
| 8610 | |
| 8611 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8612 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8613 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8614 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8615 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8616 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8617 | xmlChar *ret = NULL; |
| 8618 | |
| 8619 | GROW; |
| 8620 | in = (xmlChar *) CUR_PTR; |
| 8621 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8622 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8623 | return (NULL); |
| 8624 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8625 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8626 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8627 | /* |
| 8628 | * try to handle in this routine the most common case where no |
| 8629 | * allocation of a new string is required and where content is |
| 8630 | * pure ASCII. |
| 8631 | */ |
| 8632 | limit = *in++; |
| 8633 | end = ctxt->input->end; |
| 8634 | start = in; |
| 8635 | if (in >= end) { |
| 8636 | const xmlChar *oldbase = ctxt->input->base; |
| 8637 | GROW; |
| 8638 | if (oldbase != ctxt->input->base) { |
| 8639 | long delta = ctxt->input->base - oldbase; |
| 8640 | start = start + delta; |
| 8641 | in = in + delta; |
| 8642 | } |
| 8643 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8644 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8645 | if (normalize) { |
| 8646 | /* |
| 8647 | * Skip any leading spaces |
| 8648 | */ |
| 8649 | while ((in < end) && (*in != limit) && |
| 8650 | ((*in == 0x20) || (*in == 0x9) || |
| 8651 | (*in == 0xA) || (*in == 0xD))) { |
| 8652 | in++; |
| 8653 | start = in; |
| 8654 | if (in >= end) { |
| 8655 | const xmlChar *oldbase = ctxt->input->base; |
| 8656 | GROW; |
| 8657 | if (oldbase != ctxt->input->base) { |
| 8658 | long delta = ctxt->input->base - oldbase; |
| 8659 | start = start + delta; |
| 8660 | in = in + delta; |
| 8661 | } |
| 8662 | end = ctxt->input->end; |
| 8663 | } |
| 8664 | } |
| 8665 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8666 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8667 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 8668 | if (in >= end) { |
| 8669 | const xmlChar *oldbase = ctxt->input->base; |
| 8670 | GROW; |
| 8671 | if (oldbase != ctxt->input->base) { |
| 8672 | long delta = ctxt->input->base - oldbase; |
| 8673 | start = start + delta; |
| 8674 | in = in + delta; |
| 8675 | } |
| 8676 | end = ctxt->input->end; |
| 8677 | } |
| 8678 | } |
| 8679 | last = in; |
| 8680 | /* |
| 8681 | * skip the trailing blanks |
| 8682 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 8683 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8684 | while ((in < end) && (*in != limit) && |
| 8685 | ((*in == 0x20) || (*in == 0x9) || |
| 8686 | (*in == 0xA) || (*in == 0xD))) { |
| 8687 | in++; |
| 8688 | if (in >= end) { |
| 8689 | const xmlChar *oldbase = ctxt->input->base; |
| 8690 | GROW; |
| 8691 | if (oldbase != ctxt->input->base) { |
| 8692 | long delta = ctxt->input->base - oldbase; |
| 8693 | start = start + delta; |
| 8694 | in = in + delta; |
| 8695 | last = last + delta; |
| 8696 | } |
| 8697 | end = ctxt->input->end; |
| 8698 | } |
| 8699 | } |
| 8700 | if (*in != limit) goto need_complex; |
| 8701 | } else { |
| 8702 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8703 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8704 | in++; |
| 8705 | if (in >= end) { |
| 8706 | const xmlChar *oldbase = ctxt->input->base; |
| 8707 | GROW; |
| 8708 | if (oldbase != ctxt->input->base) { |
| 8709 | long delta = ctxt->input->base - oldbase; |
| 8710 | start = start + delta; |
| 8711 | in = in + delta; |
| 8712 | } |
| 8713 | end = ctxt->input->end; |
| 8714 | } |
| 8715 | } |
| 8716 | last = in; |
| 8717 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8718 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8719 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8720 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8721 | *len = last - start; |
| 8722 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8723 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8724 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8725 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8726 | } |
| 8727 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8728 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8729 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8730 | need_complex: |
| 8731 | if (alloc) *alloc = 1; |
| 8732 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8733 | } |
| 8734 | |
| 8735 | /** |
| 8736 | * xmlParseAttribute2: |
| 8737 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8738 | * @pref: the element prefix |
| 8739 | * @elem: the element name |
| 8740 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8741 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8742 | * @len: an int * to save the length of the attribute |
| 8743 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8744 | * |
| 8745 | * parse an attribute in the new SAX2 framework. |
| 8746 | * |
| 8747 | * Returns the attribute name, and the value in *value, . |
| 8748 | */ |
| 8749 | |
| 8750 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8751 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8752 | const xmlChar * pref, const xmlChar * elem, |
| 8753 | const xmlChar ** prefix, xmlChar ** value, |
| 8754 | int *len, int *alloc) |
| 8755 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8756 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8757 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8758 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8759 | |
| 8760 | *value = NULL; |
| 8761 | GROW; |
| 8762 | name = xmlParseQName(ctxt, prefix); |
| 8763 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8764 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8765 | "error parsing attribute name\n"); |
| 8766 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8767 | } |
| 8768 | |
| 8769 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8770 | * get the type if needed |
| 8771 | */ |
| 8772 | if (ctxt->attsSpecial != NULL) { |
| 8773 | int type; |
| 8774 | |
| 8775 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8776 | pref, elem, *prefix, name); |
| 8777 | if (type != 0) |
| 8778 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8782 | * read the value |
| 8783 | */ |
| 8784 | SKIP_BLANKS; |
| 8785 | if (RAW == '=') { |
| 8786 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8787 | SKIP_BLANKS; |
| 8788 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 8789 | if (normalize) { |
| 8790 | /* |
| 8791 | * Sometimes a second normalisation pass for spaces is needed |
| 8792 | * but that only happens if charrefs or entities refernces |
| 8793 | * have been used in the attribute value, i.e. the attribute |
| 8794 | * value have been extracted in an allocated string already. |
| 8795 | */ |
| 8796 | if (*alloc) { |
| 8797 | const xmlChar *val2; |
| 8798 | |
| 8799 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8800 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8801 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 8802 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8803 | } |
| 8804 | } |
| 8805 | } |
| 8806 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8807 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8808 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 8809 | "Specification mandate value for attribute %s\n", |
| 8810 | name); |
| 8811 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8812 | } |
| 8813 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8814 | if (*prefix == ctxt->str_xml) { |
| 8815 | /* |
| 8816 | * Check that xml:lang conforms to the specification |
| 8817 | * No more registered as an error, just generate a warning now |
| 8818 | * since this was deprecated in XML second edition |
| 8819 | */ |
| 8820 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 8821 | internal_val = xmlStrndup(val, *len); |
| 8822 | if (!xmlCheckLanguageID(internal_val)) { |
| 8823 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8824 | "Malformed value for xml:lang : %s\n", |
| 8825 | internal_val, NULL); |
| 8826 | } |
| 8827 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8828 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8829 | /* |
| 8830 | * Check that xml:space conforms to the specification |
| 8831 | */ |
| 8832 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 8833 | internal_val = xmlStrndup(val, *len); |
| 8834 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 8835 | *(ctxt->space) = 0; |
| 8836 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 8837 | *(ctxt->space) = 1; |
| 8838 | else { |
| 8839 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 8840 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 8841 | internal_val, NULL); |
| 8842 | } |
| 8843 | } |
| 8844 | if (internal_val) { |
| 8845 | xmlFree(internal_val); |
| 8846 | } |
| 8847 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8848 | |
| 8849 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8850 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8851 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8852 | /** |
| 8853 | * xmlParseStartTag2: |
| 8854 | * @ctxt: an XML parser context |
| 8855 | * |
| 8856 | * parse a start of tag either for rule element or |
| 8857 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8858 | * This routine is called when running SAX2 parsing |
| 8859 | * |
| 8860 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8861 | * |
| 8862 | * [ WFC: Unique Att Spec ] |
| 8863 | * No attribute name may appear more than once in the same start-tag or |
| 8864 | * empty-element tag. |
| 8865 | * |
| 8866 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8867 | * |
| 8868 | * [ WFC: Unique Att Spec ] |
| 8869 | * No attribute name may appear more than once in the same start-tag or |
| 8870 | * empty-element tag. |
| 8871 | * |
| 8872 | * With namespace: |
| 8873 | * |
| 8874 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8875 | * |
| 8876 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8877 | * |
| 8878 | * Returns the element name parsed |
| 8879 | */ |
| 8880 | |
| 8881 | static const xmlChar * |
| 8882 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8883 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8884 | const xmlChar *localname; |
| 8885 | const xmlChar *prefix; |
| 8886 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8887 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8888 | const xmlChar *nsname; |
| 8889 | xmlChar *attvalue; |
| 8890 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8891 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8892 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8893 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8894 | const xmlChar *base; |
| 8895 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8896 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8897 | |
| 8898 | if (RAW != '<') return(NULL); |
| 8899 | NEXT1; |
| 8900 | |
| 8901 | /* |
| 8902 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 8903 | * point since the attribute values may be stored as pointers to |
| 8904 | * the buffer and calling SHRINK would destroy them ! |
| 8905 | * The Shrinking is only possible once the full set of attribute |
| 8906 | * callbacks have been done. |
| 8907 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8908 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8909 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8910 | base = ctxt->input->base; |
| 8911 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8912 | oldline = ctxt->input->line; |
| 8913 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8914 | nbatts = 0; |
| 8915 | nratts = 0; |
| 8916 | nbdef = 0; |
| 8917 | nbNs = 0; |
| 8918 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8919 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 8920 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8921 | |
| 8922 | localname = xmlParseQName(ctxt, &prefix); |
| 8923 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8924 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8925 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8926 | return(NULL); |
| 8927 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8928 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8929 | |
| 8930 | /* |
| 8931 | * Now parse the attributes, it ends up with the ending |
| 8932 | * |
| 8933 | * (S Attribute)* S? |
| 8934 | */ |
| 8935 | SKIP_BLANKS; |
| 8936 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8937 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8938 | |
| 8939 | while ((RAW != '>') && |
| 8940 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8941 | (IS_BYTE_CHAR(RAW))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8942 | const xmlChar *q = CUR_PTR; |
| 8943 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8944 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8945 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8946 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 8947 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8948 | if (ctxt->input->base != base) { |
| 8949 | if ((attvalue != NULL) && (alloc != 0)) |
| 8950 | xmlFree(attvalue); |
| 8951 | attvalue = NULL; |
| 8952 | goto base_changed; |
| 8953 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8954 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8955 | if (len < 0) len = xmlStrlen(attvalue); |
| 8956 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8957 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 8958 | xmlURIPtr uri; |
| 8959 | |
| 8960 | if (*URL != 0) { |
| 8961 | uri = xmlParseURI((const char *) URL); |
| 8962 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8963 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 8964 | "xmlns: '%s' is not a valid URI\n", |
| 8965 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8966 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 8967 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8968 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 8969 | "xmlns: URI %s is not absolute\n", |
| 8970 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8971 | } |
| 8972 | xmlFreeURI(uri); |
| 8973 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8974 | if (URL == ctxt->str_xml_ns) { |
| 8975 | if (attname != ctxt->str_xml) { |
| 8976 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8977 | "xml namespace URI cannot be the default namespace\n", |
| 8978 | NULL, NULL, NULL); |
| 8979 | } |
| 8980 | goto skip_default_ns; |
| 8981 | } |
| 8982 | if ((len == 29) && |
| 8983 | (xmlStrEqual(URL, |
| 8984 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 8985 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8986 | "reuse of the xmlns namespace name is forbidden\n", |
| 8987 | NULL, NULL, NULL); |
| 8988 | goto skip_default_ns; |
| 8989 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8990 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8991 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8992 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8993 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8994 | for (j = 1;j <= nbNs;j++) |
| 8995 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 8996 | break; |
| 8997 | if (j <= nbNs) |
| 8998 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 8999 | else |
| 9000 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9001 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9002 | if (alloc != 0) xmlFree(attvalue); |
| 9003 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9004 | continue; |
| 9005 | } |
| 9006 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9007 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 9008 | xmlURIPtr uri; |
| 9009 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9010 | if (attname == ctxt->str_xml) { |
| 9011 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9012 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9013 | "xml namespace prefix mapped to wrong URI\n", |
| 9014 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9015 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9016 | /* |
| 9017 | * Do not keep a namespace definition node |
| 9018 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9019 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 9020 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9021 | if (URL == ctxt->str_xml_ns) { |
| 9022 | if (attname != ctxt->str_xml) { |
| 9023 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9024 | "xml namespace URI mapped to wrong prefix\n", |
| 9025 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9026 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9027 | goto skip_ns; |
| 9028 | } |
| 9029 | if (attname == ctxt->str_xmlns) { |
| 9030 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9031 | "redefinition of the xmlns prefix is forbidden\n", |
| 9032 | NULL, NULL, NULL); |
| 9033 | goto skip_ns; |
| 9034 | } |
| 9035 | if ((len == 29) && |
| 9036 | (xmlStrEqual(URL, |
| 9037 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 9038 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9039 | "reuse of the xmlns namespace name is forbidden\n", |
| 9040 | NULL, NULL, NULL); |
| 9041 | goto skip_ns; |
| 9042 | } |
| 9043 | if ((URL == NULL) || (URL[0] == 0)) { |
| 9044 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 9045 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 9046 | attname, NULL, NULL); |
| 9047 | goto skip_ns; |
| 9048 | } else { |
| 9049 | uri = xmlParseURI((const char *) URL); |
| 9050 | if (uri == NULL) { |
| 9051 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 9052 | "xmlns:%s: '%s' is not a valid URI\n", |
| 9053 | attname, URL, NULL); |
| 9054 | } else { |
| 9055 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 9056 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 9057 | "xmlns:%s: URI %s is not absolute\n", |
| 9058 | attname, URL, NULL); |
| 9059 | } |
| 9060 | xmlFreeURI(uri); |
| 9061 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9062 | } |
| 9063 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9064 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9065 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9066 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9067 | for (j = 1;j <= nbNs;j++) |
| 9068 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9069 | break; |
| 9070 | if (j <= nbNs) |
| 9071 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 9072 | else |
| 9073 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9074 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9075 | if (alloc != 0) xmlFree(attvalue); |
| 9076 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 9077 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9078 | continue; |
| 9079 | } |
| 9080 | |
| 9081 | /* |
| 9082 | * Add the pair to atts |
| 9083 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9084 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9085 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9086 | if (attvalue[len] == 0) |
| 9087 | xmlFree(attvalue); |
| 9088 | goto failed; |
| 9089 | } |
| 9090 | maxatts = ctxt->maxatts; |
| 9091 | atts = ctxt->atts; |
| 9092 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9093 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9094 | atts[nbatts++] = attname; |
| 9095 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9096 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9097 | atts[nbatts++] = attvalue; |
| 9098 | attvalue += len; |
| 9099 | atts[nbatts++] = attvalue; |
| 9100 | /* |
| 9101 | * tag if some deallocation is needed |
| 9102 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9103 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9104 | } else { |
| 9105 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 9106 | xmlFree(attvalue); |
| 9107 | } |
| 9108 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9109 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9110 | |
| 9111 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9112 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9113 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 9114 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9115 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9116 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9117 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9118 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9119 | } |
| 9120 | SKIP_BLANKS; |
| 9121 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 9122 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9123 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9124 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9125 | break; |
| 9126 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9127 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9128 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9129 | } |
| 9130 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9131 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9132 | * The attributes defaulting |
| 9133 | */ |
| 9134 | if (ctxt->attsDefault != NULL) { |
| 9135 | xmlDefAttrsPtr defaults; |
| 9136 | |
| 9137 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 9138 | if (defaults != NULL) { |
| 9139 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9140 | attname = defaults->values[5 * i]; |
| 9141 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9142 | |
| 9143 | /* |
| 9144 | * special work for namespaces defaulted defs |
| 9145 | */ |
| 9146 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 9147 | /* |
| 9148 | * check that it's not a defined namespace |
| 9149 | */ |
| 9150 | for (j = 1;j <= nbNs;j++) |
| 9151 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 9152 | break; |
| 9153 | if (j <= nbNs) continue; |
| 9154 | |
| 9155 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9156 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9157 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9158 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9159 | nbNs++; |
| 9160 | } |
| 9161 | } else if (aprefix == ctxt->str_xmlns) { |
| 9162 | /* |
| 9163 | * check that it's not a defined namespace |
| 9164 | */ |
| 9165 | for (j = 1;j <= nbNs;j++) |
| 9166 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 9167 | break; |
| 9168 | if (j <= nbNs) continue; |
| 9169 | |
| 9170 | nsname = xmlGetNamespace(ctxt, attname); |
| 9171 | if (nsname != defaults->values[2]) { |
| 9172 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9173 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9174 | nbNs++; |
| 9175 | } |
| 9176 | } else { |
| 9177 | /* |
| 9178 | * check that it's not a defined attribute |
| 9179 | */ |
| 9180 | for (j = 0;j < nbatts;j+=5) { |
| 9181 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 9182 | break; |
| 9183 | } |
| 9184 | if (j < nbatts) continue; |
| 9185 | |
| 9186 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 9187 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 9188 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9189 | } |
| 9190 | maxatts = ctxt->maxatts; |
| 9191 | atts = ctxt->atts; |
| 9192 | } |
| 9193 | atts[nbatts++] = attname; |
| 9194 | atts[nbatts++] = aprefix; |
| 9195 | if (aprefix == NULL) |
| 9196 | atts[nbatts++] = NULL; |
| 9197 | else |
| 9198 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 9199 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 9200 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 9201 | if ((ctxt->standalone == 1) && |
| 9202 | (defaults->values[5 * i + 4] != NULL)) { |
| 9203 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
| 9204 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 9205 | attname, localname); |
| 9206 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9207 | nbdef++; |
| 9208 | } |
| 9209 | } |
| 9210 | } |
| 9211 | } |
| 9212 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9213 | /* |
| 9214 | * The attributes checkings |
| 9215 | */ |
| 9216 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 9217 | /* |
| 9218 | * The default namespace does not apply to attribute names. |
| 9219 | */ |
| 9220 | if (atts[i + 1] != NULL) { |
| 9221 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 9222 | if (nsname == NULL) { |
| 9223 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9224 | "Namespace prefix %s for %s on %s is not defined\n", |
| 9225 | atts[i + 1], atts[i], localname); |
| 9226 | } |
| 9227 | atts[i + 2] = nsname; |
| 9228 | } else |
| 9229 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 9230 | /* |
| 9231 | * [ WFC: Unique Att Spec ] |
| 9232 | * No attribute name may appear more than once in the same |
| 9233 | * start-tag or empty-element tag. |
| 9234 | * As extended by the Namespace in XML REC. |
| 9235 | */ |
| 9236 | for (j = 0; j < i;j += 5) { |
| 9237 | if (atts[i] == atts[j]) { |
| 9238 | if (atts[i+1] == atts[j+1]) { |
| 9239 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 9240 | break; |
| 9241 | } |
| 9242 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 9243 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 9244 | "Namespaced Attribute %s in '%s' redefined\n", |
| 9245 | atts[i], nsname, NULL); |
| 9246 | break; |
| 9247 | } |
| 9248 | } |
| 9249 | } |
| 9250 | } |
| 9251 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9252 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9253 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9254 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9255 | "Namespace prefix %s on %s is not defined\n", |
| 9256 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9257 | } |
| 9258 | *pref = prefix; |
| 9259 | *URI = nsname; |
| 9260 | |
| 9261 | /* |
| 9262 | * SAX: Start of Element ! |
| 9263 | */ |
| 9264 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9265 | (!ctxt->disableSAX)) { |
| 9266 | if (nbNs > 0) |
| 9267 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9268 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9269 | nbatts / 5, nbdef, atts); |
| 9270 | else |
| 9271 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9272 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9273 | } |
| 9274 | |
| 9275 | /* |
| 9276 | * Free up attribute allocated strings if needed |
| 9277 | */ |
| 9278 | if (attval != 0) { |
| 9279 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9280 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9281 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9282 | } |
| 9283 | |
| 9284 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9285 | |
| 9286 | base_changed: |
| 9287 | /* |
| 9288 | * the attribute strings are valid iif the base didn't changed |
| 9289 | */ |
| 9290 | if (attval != 0) { |
| 9291 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9292 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9293 | xmlFree((xmlChar *) atts[i]); |
| 9294 | } |
| 9295 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9296 | ctxt->input->line = oldline; |
| 9297 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9298 | if (ctxt->wellFormed == 1) { |
| 9299 | goto reparse; |
| 9300 | } |
| 9301 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9302 | } |
| 9303 | |
| 9304 | /** |
| 9305 | * xmlParseEndTag2: |
| 9306 | * @ctxt: an XML parser context |
| 9307 | * @line: line of the start tag |
| 9308 | * @nsNr: number of namespaces on the start tag |
| 9309 | * |
| 9310 | * parse an end of tag |
| 9311 | * |
| 9312 | * [42] ETag ::= '</' Name S? '>' |
| 9313 | * |
| 9314 | * With namespace |
| 9315 | * |
| 9316 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9317 | */ |
| 9318 | |
| 9319 | static void |
| 9320 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9321 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9322 | const xmlChar *name; |
| 9323 | |
| 9324 | GROW; |
| 9325 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9326 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9327 | return; |
| 9328 | } |
| 9329 | SKIP(2); |
| 9330 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9331 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9332 | if (ctxt->input->cur[tlen] == '>') { |
| 9333 | ctxt->input->cur += tlen + 1; |
| 9334 | goto done; |
| 9335 | } |
| 9336 | ctxt->input->cur += tlen; |
| 9337 | name = (xmlChar*)1; |
| 9338 | } else { |
| 9339 | if (prefix == NULL) |
| 9340 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9341 | else |
| 9342 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9343 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9344 | |
| 9345 | /* |
| 9346 | * We should definitely be at the ending "S? '>'" part |
| 9347 | */ |
| 9348 | GROW; |
| 9349 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9350 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9351 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9352 | } else |
| 9353 | NEXT1; |
| 9354 | |
| 9355 | /* |
| 9356 | * [ WFC: Element Type Match ] |
| 9357 | * The Name in an element's end-tag must match the element type in the |
| 9358 | * start-tag. |
| 9359 | * |
| 9360 | */ |
| 9361 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9362 | if (name == NULL) name = BAD_CAST "unparseable"; |
Daniel Veillard | 852505b | 2009-08-23 15:44:48 +0200 | [diff] [blame] | 9363 | if ((line == 0) && (ctxt->node != NULL)) |
| 9364 | line = ctxt->node->line; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9365 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9366 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9367 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | /* |
| 9371 | * SAX: End of Tag |
| 9372 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9373 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9374 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9375 | (!ctxt->disableSAX)) |
| 9376 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9377 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9378 | spacePop(ctxt); |
| 9379 | if (nsNr != 0) |
| 9380 | nsPop(ctxt, nsNr); |
| 9381 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
| 9384 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9385 | * xmlParseCDSect: |
| 9386 | * @ctxt: an XML parser context |
| 9387 | * |
| 9388 | * Parse escaped pure raw content. |
| 9389 | * |
| 9390 | * [18] CDSect ::= CDStart CData CDEnd |
| 9391 | * |
| 9392 | * [19] CDStart ::= '<![CDATA[' |
| 9393 | * |
| 9394 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9395 | * |
| 9396 | * [21] CDEnd ::= ']]>' |
| 9397 | */ |
| 9398 | void |
| 9399 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9400 | xmlChar *buf = NULL; |
| 9401 | int len = 0; |
| 9402 | int size = XML_PARSER_BUFFER_SIZE; |
| 9403 | int r, rl; |
| 9404 | int s, sl; |
| 9405 | int cur, l; |
| 9406 | int count = 0; |
| 9407 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9408 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9409 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9410 | SKIP(9); |
| 9411 | } else |
| 9412 | return; |
| 9413 | |
| 9414 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9415 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9416 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9417 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9418 | ctxt->instate = XML_PARSER_CONTENT; |
| 9419 | return; |
| 9420 | } |
| 9421 | NEXTL(rl); |
| 9422 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9423 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9424 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9425 | ctxt->instate = XML_PARSER_CONTENT; |
| 9426 | return; |
| 9427 | } |
| 9428 | NEXTL(sl); |
| 9429 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9430 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9431 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9432 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9433 | return; |
| 9434 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9435 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9436 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9437 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9438 | xmlChar *tmp; |
| 9439 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9440 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9441 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9442 | if (tmp == NULL) { |
| 9443 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9444 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9445 | return; |
| 9446 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9447 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9448 | } |
| 9449 | COPY_BUF(rl,buf,len,r); |
| 9450 | r = s; |
| 9451 | rl = sl; |
| 9452 | s = cur; |
| 9453 | sl = l; |
| 9454 | count++; |
| 9455 | if (count > 50) { |
| 9456 | GROW; |
| 9457 | count = 0; |
| 9458 | } |
| 9459 | NEXTL(l); |
| 9460 | cur = CUR_CHAR(l); |
| 9461 | } |
| 9462 | buf[len] = 0; |
| 9463 | ctxt->instate = XML_PARSER_CONTENT; |
| 9464 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9465 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9466 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9467 | xmlFree(buf); |
| 9468 | return; |
| 9469 | } |
| 9470 | NEXTL(l); |
| 9471 | |
| 9472 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9473 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9474 | */ |
| 9475 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9476 | if (ctxt->sax->cdataBlock != NULL) |
| 9477 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9478 | else if (ctxt->sax->characters != NULL) |
| 9479 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9480 | } |
| 9481 | xmlFree(buf); |
| 9482 | } |
| 9483 | |
| 9484 | /** |
| 9485 | * xmlParseContent: |
| 9486 | * @ctxt: an XML parser context |
| 9487 | * |
| 9488 | * Parse a content: |
| 9489 | * |
| 9490 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9491 | */ |
| 9492 | |
| 9493 | void |
| 9494 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9495 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9496 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9497 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9498 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9499 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9500 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9501 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9502 | |
| 9503 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9504 | * First case : a Processing Instruction. |
| 9505 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9506 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9507 | xmlParsePI(ctxt); |
| 9508 | } |
| 9509 | |
| 9510 | /* |
| 9511 | * Second case : a CDSection |
| 9512 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9513 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9514 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9515 | xmlParseCDSect(ctxt); |
| 9516 | } |
| 9517 | |
| 9518 | /* |
| 9519 | * Third case : a comment |
| 9520 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9521 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9522 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9523 | xmlParseComment(ctxt); |
| 9524 | ctxt->instate = XML_PARSER_CONTENT; |
| 9525 | } |
| 9526 | |
| 9527 | /* |
| 9528 | * Fourth case : a sub-element. |
| 9529 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9530 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9531 | xmlParseElement(ctxt); |
| 9532 | } |
| 9533 | |
| 9534 | /* |
| 9535 | * Fifth case : a reference. If if has not been resolved, |
| 9536 | * parsing returns it's Name, create the node |
| 9537 | */ |
| 9538 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9539 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9540 | xmlParseReference(ctxt); |
| 9541 | } |
| 9542 | |
| 9543 | /* |
| 9544 | * Last case, text. Note that References are handled directly. |
| 9545 | */ |
| 9546 | else { |
| 9547 | xmlParseCharData(ctxt, 0); |
| 9548 | } |
| 9549 | |
| 9550 | GROW; |
| 9551 | /* |
| 9552 | * Pop-up of finished entities. |
| 9553 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9554 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9555 | xmlPopInput(ctxt); |
| 9556 | SHRINK; |
| 9557 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9558 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9559 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9560 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9561 | ctxt->instate = XML_PARSER_EOF; |
| 9562 | break; |
| 9563 | } |
| 9564 | } |
| 9565 | } |
| 9566 | |
| 9567 | /** |
| 9568 | * xmlParseElement: |
| 9569 | * @ctxt: an XML parser context |
| 9570 | * |
| 9571 | * parse an XML element, this is highly recursive |
| 9572 | * |
| 9573 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9574 | * |
| 9575 | * [ WFC: Element Type Match ] |
| 9576 | * The Name in an element's end-tag must match the element type in the |
| 9577 | * start-tag. |
| 9578 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9579 | */ |
| 9580 | |
| 9581 | void |
| 9582 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 9583 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 9584 | const xmlChar *prefix = NULL; |
| 9585 | const xmlChar *URI = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9586 | xmlParserNodeInfo node_info; |
Daniel Veillard | ed35d3d | 2012-05-11 10:52:27 +0800 | [diff] [blame] | 9587 | int line, tlen = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9588 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9589 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9590 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 9591 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 9592 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9593 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9594 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 9595 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9596 | ctxt->instate = XML_PARSER_EOF; |
| 9597 | return; |
| 9598 | } |
| 9599 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9600 | /* Capture start position */ |
| 9601 | if (ctxt->record_info) { |
| 9602 | node_info.begin_pos = ctxt->input->consumed + |
| 9603 | (CUR_PTR - ctxt->input->base); |
| 9604 | node_info.begin_line = ctxt->input->line; |
| 9605 | } |
| 9606 | |
| 9607 | if (ctxt->spaceNr == 0) |
| 9608 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 9609 | else if (*ctxt->space == -2) |
| 9610 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9611 | else |
| 9612 | spacePush(ctxt, *ctxt->space); |
| 9613 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9614 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9615 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9616 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9617 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9618 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9619 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9620 | else |
| 9621 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9622 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 9623 | if (ctxt->instate == XML_PARSER_EOF) |
| 9624 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9625 | if (name == NULL) { |
| 9626 | spacePop(ctxt); |
| 9627 | return; |
| 9628 | } |
| 9629 | namePush(ctxt, name); |
| 9630 | ret = ctxt->node; |
| 9631 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9632 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9633 | /* |
| 9634 | * [ VC: Root Element Type ] |
| 9635 | * The Name in the document type declaration must match the element |
| 9636 | * type of the root element. |
| 9637 | */ |
| 9638 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9639 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9640 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9641 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9642 | |
| 9643 | /* |
| 9644 | * Check for an Empty Element. |
| 9645 | */ |
| 9646 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9647 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9648 | if (ctxt->sax2) { |
| 9649 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9650 | (!ctxt->disableSAX)) |
| 9651 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9652 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9653 | } else { |
| 9654 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 9655 | (!ctxt->disableSAX)) |
| 9656 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9657 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9658 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9659 | namePop(ctxt); |
| 9660 | spacePop(ctxt); |
| 9661 | if (nsNr != ctxt->nsNr) |
| 9662 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9663 | if ( ret != NULL && ctxt->record_info ) { |
| 9664 | node_info.end_pos = ctxt->input->consumed + |
| 9665 | (CUR_PTR - ctxt->input->base); |
| 9666 | node_info.end_line = ctxt->input->line; |
| 9667 | node_info.node = ret; |
| 9668 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9669 | } |
| 9670 | return; |
| 9671 | } |
| 9672 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9673 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9674 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9675 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 9676 | "Couldn't find end of Start Tag %s line %d\n", |
| 9677 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9678 | |
| 9679 | /* |
| 9680 | * end of parsing of this node. |
| 9681 | */ |
| 9682 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9683 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9684 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9685 | if (nsNr != ctxt->nsNr) |
| 9686 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9687 | |
| 9688 | /* |
| 9689 | * Capture end position and add node |
| 9690 | */ |
| 9691 | if ( ret != NULL && ctxt->record_info ) { |
| 9692 | node_info.end_pos = ctxt->input->consumed + |
| 9693 | (CUR_PTR - ctxt->input->base); |
| 9694 | node_info.end_line = ctxt->input->line; |
| 9695 | node_info.node = ret; |
| 9696 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9697 | } |
| 9698 | return; |
| 9699 | } |
| 9700 | |
| 9701 | /* |
| 9702 | * Parse the content of the element: |
| 9703 | */ |
| 9704 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9705 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9706 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 9707 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9708 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9709 | |
| 9710 | /* |
| 9711 | * end of parsing of this node. |
| 9712 | */ |
| 9713 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9714 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9715 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9716 | if (nsNr != ctxt->nsNr) |
| 9717 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9718 | return; |
| 9719 | } |
| 9720 | |
| 9721 | /* |
| 9722 | * parse the end of tag: '</' should be here. |
| 9723 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9724 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9725 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9726 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9727 | } |
| 9728 | #ifdef LIBXML_SAX1_ENABLED |
| 9729 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9730 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9731 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9732 | |
| 9733 | /* |
| 9734 | * Capture end position and add node |
| 9735 | */ |
| 9736 | if ( ret != NULL && ctxt->record_info ) { |
| 9737 | node_info.end_pos = ctxt->input->consumed + |
| 9738 | (CUR_PTR - ctxt->input->base); |
| 9739 | node_info.end_line = ctxt->input->line; |
| 9740 | node_info.node = ret; |
| 9741 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9742 | } |
| 9743 | } |
| 9744 | |
| 9745 | /** |
| 9746 | * xmlParseVersionNum: |
| 9747 | * @ctxt: an XML parser context |
| 9748 | * |
| 9749 | * parse the XML version value. |
| 9750 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9751 | * [26] VersionNum ::= '1.' [0-9]+ |
| 9752 | * |
| 9753 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9754 | * |
| 9755 | * Returns the string giving the XML version number, or NULL |
| 9756 | */ |
| 9757 | xmlChar * |
| 9758 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 9759 | xmlChar *buf = NULL; |
| 9760 | int len = 0; |
| 9761 | int size = 10; |
| 9762 | xmlChar cur; |
| 9763 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9764 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9765 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9766 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9767 | return(NULL); |
| 9768 | } |
| 9769 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9770 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9771 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9772 | return(NULL); |
| 9773 | } |
| 9774 | buf[len++] = cur; |
| 9775 | NEXT; |
| 9776 | cur=CUR; |
| 9777 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9778 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9779 | return(NULL); |
| 9780 | } |
| 9781 | buf[len++] = cur; |
| 9782 | NEXT; |
| 9783 | cur=CUR; |
| 9784 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9785 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9786 | xmlChar *tmp; |
| 9787 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9788 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9789 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9790 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9791 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9792 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9793 | return(NULL); |
| 9794 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9795 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9796 | } |
| 9797 | buf[len++] = cur; |
| 9798 | NEXT; |
| 9799 | cur=CUR; |
| 9800 | } |
| 9801 | buf[len] = 0; |
| 9802 | return(buf); |
| 9803 | } |
| 9804 | |
| 9805 | /** |
| 9806 | * xmlParseVersionInfo: |
| 9807 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9808 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9809 | * parse the XML version. |
| 9810 | * |
| 9811 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9812 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9813 | * [25] Eq ::= S? '=' S? |
| 9814 | * |
| 9815 | * Returns the version string, e.g. "1.0" |
| 9816 | */ |
| 9817 | |
| 9818 | xmlChar * |
| 9819 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 9820 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9821 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9822 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9823 | SKIP(7); |
| 9824 | SKIP_BLANKS; |
| 9825 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9826 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9827 | return(NULL); |
| 9828 | } |
| 9829 | NEXT; |
| 9830 | SKIP_BLANKS; |
| 9831 | if (RAW == '"') { |
| 9832 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9833 | version = xmlParseVersionNum(ctxt); |
| 9834 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9835 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9836 | } else |
| 9837 | NEXT; |
| 9838 | } else if (RAW == '\''){ |
| 9839 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9840 | version = xmlParseVersionNum(ctxt); |
| 9841 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9842 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9843 | } else |
| 9844 | NEXT; |
| 9845 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9846 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9847 | } |
| 9848 | } |
| 9849 | return(version); |
| 9850 | } |
| 9851 | |
| 9852 | /** |
| 9853 | * xmlParseEncName: |
| 9854 | * @ctxt: an XML parser context |
| 9855 | * |
| 9856 | * parse the XML encoding name |
| 9857 | * |
| 9858 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 9859 | * |
| 9860 | * Returns the encoding name value or NULL |
| 9861 | */ |
| 9862 | xmlChar * |
| 9863 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 9864 | xmlChar *buf = NULL; |
| 9865 | int len = 0; |
| 9866 | int size = 10; |
| 9867 | xmlChar cur; |
| 9868 | |
| 9869 | cur = CUR; |
| 9870 | if (((cur >= 'a') && (cur <= 'z')) || |
| 9871 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9872 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9873 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9874 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9875 | return(NULL); |
| 9876 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9877 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9878 | buf[len++] = cur; |
| 9879 | NEXT; |
| 9880 | cur = CUR; |
| 9881 | while (((cur >= 'a') && (cur <= 'z')) || |
| 9882 | ((cur >= 'A') && (cur <= 'Z')) || |
| 9883 | ((cur >= '0') && (cur <= '9')) || |
| 9884 | (cur == '.') || (cur == '_') || |
| 9885 | (cur == '-')) { |
| 9886 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9887 | xmlChar *tmp; |
| 9888 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9889 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9890 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9891 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9892 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9893 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9894 | return(NULL); |
| 9895 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9896 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9897 | } |
| 9898 | buf[len++] = cur; |
| 9899 | NEXT; |
| 9900 | cur = CUR; |
| 9901 | if (cur == 0) { |
| 9902 | SHRINK; |
| 9903 | GROW; |
| 9904 | cur = CUR; |
| 9905 | } |
| 9906 | } |
| 9907 | buf[len] = 0; |
| 9908 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9909 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9910 | } |
| 9911 | return(buf); |
| 9912 | } |
| 9913 | |
| 9914 | /** |
| 9915 | * xmlParseEncodingDecl: |
| 9916 | * @ctxt: an XML parser context |
| 9917 | * |
| 9918 | * parse the XML encoding declaration |
| 9919 | * |
| 9920 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 9921 | * |
| 9922 | * this setups the conversion filters. |
| 9923 | * |
| 9924 | * Returns the encoding value or NULL |
| 9925 | */ |
| 9926 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9927 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9928 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 9929 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9930 | |
| 9931 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9932 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9933 | SKIP(8); |
| 9934 | SKIP_BLANKS; |
| 9935 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9936 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9937 | return(NULL); |
| 9938 | } |
| 9939 | NEXT; |
| 9940 | SKIP_BLANKS; |
| 9941 | if (RAW == '"') { |
| 9942 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9943 | encoding = xmlParseEncName(ctxt); |
| 9944 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9945 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9946 | } else |
| 9947 | NEXT; |
| 9948 | } else if (RAW == '\''){ |
| 9949 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9950 | encoding = xmlParseEncName(ctxt); |
| 9951 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9952 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9953 | } else |
| 9954 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 9955 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9956 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9957 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 9958 | |
| 9959 | /* |
| 9960 | * Non standard parsing, allowing the user to ignore encoding |
| 9961 | */ |
| 9962 | if (ctxt->options & XML_PARSE_IGNORE_ENC) |
| 9963 | return(encoding); |
| 9964 | |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9965 | /* |
| 9966 | * UTF-16 encoding stwich has already taken place at this stage, |
| 9967 | * more over the little-endian/big-endian selection is already done |
| 9968 | */ |
| 9969 | if ((encoding != NULL) && |
| 9970 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 9971 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9972 | /* |
| 9973 | * If no encoding was passed to the parser, that we are |
| 9974 | * using UTF-16 and no decoder is present i.e. the |
| 9975 | * document is apparently UTF-8 compatible, then raise an |
| 9976 | * encoding mismatch fatal error |
| 9977 | */ |
| 9978 | if ((ctxt->encoding == NULL) && |
| 9979 | (ctxt->input->buf != NULL) && |
| 9980 | (ctxt->input->buf->encoder == NULL)) { |
| 9981 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 9982 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 9983 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9984 | if (ctxt->encoding != NULL) |
| 9985 | xmlFree((xmlChar *) ctxt->encoding); |
| 9986 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9987 | } |
| 9988 | /* |
| 9989 | * UTF-8 encoding is handled natively |
| 9990 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9991 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9992 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 9993 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9994 | if (ctxt->encoding != NULL) |
| 9995 | xmlFree((xmlChar *) ctxt->encoding); |
| 9996 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9997 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9998 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9999 | xmlCharEncodingHandlerPtr handler; |
| 10000 | |
| 10001 | if (ctxt->input->encoding != NULL) |
| 10002 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 10003 | ctxt->input->encoding = encoding; |
| 10004 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10005 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 10006 | if (handler != NULL) { |
| 10007 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10008 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 10009 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 10010 | "Unsupported encoding %s\n", encoding); |
| 10011 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10012 | } |
| 10013 | } |
| 10014 | } |
| 10015 | return(encoding); |
| 10016 | } |
| 10017 | |
| 10018 | /** |
| 10019 | * xmlParseSDDecl: |
| 10020 | * @ctxt: an XML parser context |
| 10021 | * |
| 10022 | * parse the XML standalone declaration |
| 10023 | * |
| 10024 | * [32] SDDecl ::= S 'standalone' Eq |
| 10025 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 10026 | * |
| 10027 | * [ VC: Standalone Document Declaration ] |
| 10028 | * TODO The standalone document declaration must have the value "no" |
| 10029 | * if any external markup declarations contain declarations of: |
| 10030 | * - attributes with default values, if elements to which these |
| 10031 | * attributes apply appear in the document without specifications |
| 10032 | * of values for these attributes, or |
| 10033 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 10034 | * to those entities appear in the document, or |
| 10035 | * - attributes with values subject to normalization, where the |
| 10036 | * attribute appears in the document with a value which will change |
| 10037 | * as a result of normalization, or |
| 10038 | * - element types with element content, if white space occurs directly |
| 10039 | * within any instance of those types. |
| 10040 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10041 | * Returns: |
| 10042 | * 1 if standalone="yes" |
| 10043 | * 0 if standalone="no" |
| 10044 | * -2 if standalone attribute is missing or invalid |
| 10045 | * (A standalone value of -2 means that the XML declaration was found, |
| 10046 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10047 | */ |
| 10048 | |
| 10049 | int |
| 10050 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 10051 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10052 | |
| 10053 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10054 | 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] | 10055 | SKIP(10); |
| 10056 | SKIP_BLANKS; |
| 10057 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10058 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10059 | return(standalone); |
| 10060 | } |
| 10061 | NEXT; |
| 10062 | SKIP_BLANKS; |
| 10063 | if (RAW == '\''){ |
| 10064 | NEXT; |
| 10065 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10066 | standalone = 0; |
| 10067 | SKIP(2); |
| 10068 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10069 | (NXT(2) == 's')) { |
| 10070 | standalone = 1; |
| 10071 | SKIP(3); |
| 10072 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10073 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10074 | } |
| 10075 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10076 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10077 | } else |
| 10078 | NEXT; |
| 10079 | } else if (RAW == '"'){ |
| 10080 | NEXT; |
| 10081 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 10082 | standalone = 0; |
| 10083 | SKIP(2); |
| 10084 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 10085 | (NXT(2) == 's')) { |
| 10086 | standalone = 1; |
| 10087 | SKIP(3); |
| 10088 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10089 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10090 | } |
| 10091 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10092 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10093 | } else |
| 10094 | NEXT; |
| 10095 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10096 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10097 | } |
| 10098 | } |
| 10099 | return(standalone); |
| 10100 | } |
| 10101 | |
| 10102 | /** |
| 10103 | * xmlParseXMLDecl: |
| 10104 | * @ctxt: an XML parser context |
| 10105 | * |
| 10106 | * parse an XML declaration header |
| 10107 | * |
| 10108 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 10109 | */ |
| 10110 | |
| 10111 | void |
| 10112 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 10113 | xmlChar *version; |
| 10114 | |
| 10115 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 10116 | * This value for standalone indicates that the document has an |
| 10117 | * XML declaration but it does not have a standalone attribute. |
| 10118 | * It will be overwritten later if a standalone attribute is found. |
| 10119 | */ |
| 10120 | ctxt->input->standalone = -2; |
| 10121 | |
| 10122 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10123 | * We know that '<?xml' is here. |
| 10124 | */ |
| 10125 | SKIP(5); |
| 10126 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10127 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10128 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 10129 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10130 | } |
| 10131 | SKIP_BLANKS; |
| 10132 | |
| 10133 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10134 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10135 | */ |
| 10136 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10137 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10138 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10139 | } else { |
| 10140 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 10141 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10142 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10143 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 10144 | if (ctxt->options & XML_PARSE_OLD10) { |
| 10145 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10146 | "Unsupported version '%s'\n", |
| 10147 | version); |
| 10148 | } else { |
| 10149 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 10150 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 10151 | "Unsupported version '%s'\n", |
| 10152 | version, NULL); |
| 10153 | } else { |
| 10154 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 10155 | "Unsupported version '%s'\n", |
| 10156 | version); |
| 10157 | } |
| 10158 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10159 | } |
| 10160 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 10161 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 10162 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 10163 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10164 | |
| 10165 | /* |
| 10166 | * We may have the encoding declaration |
| 10167 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10168 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10169 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10170 | SKIP(2); |
| 10171 | return; |
| 10172 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10173 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10174 | } |
| 10175 | xmlParseEncodingDecl(ctxt); |
| 10176 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10177 | /* |
| 10178 | * The XML REC instructs us to stop parsing right here |
| 10179 | */ |
| 10180 | return; |
| 10181 | } |
| 10182 | |
| 10183 | /* |
| 10184 | * We may have the standalone status. |
| 10185 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10186 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10187 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10188 | SKIP(2); |
| 10189 | return; |
| 10190 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10191 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10192 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 10193 | |
| 10194 | /* |
| 10195 | * We can grow the input buffer freely at that point |
| 10196 | */ |
| 10197 | GROW; |
| 10198 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10199 | SKIP_BLANKS; |
| 10200 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 10201 | |
| 10202 | SKIP_BLANKS; |
| 10203 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 10204 | SKIP(2); |
| 10205 | } else if (RAW == '>') { |
| 10206 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10207 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10208 | NEXT; |
| 10209 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10210 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10211 | MOVETO_ENDTAG(CUR_PTR); |
| 10212 | NEXT; |
| 10213 | } |
| 10214 | } |
| 10215 | |
| 10216 | /** |
| 10217 | * xmlParseMisc: |
| 10218 | * @ctxt: an XML parser context |
| 10219 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10220 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10221 | * |
| 10222 | * [27] Misc ::= Comment | PI | S |
| 10223 | */ |
| 10224 | |
| 10225 | void |
| 10226 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10227 | while (((RAW == '<') && (NXT(1) == '?')) || |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10228 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10229 | IS_BLANK_CH(CUR)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10230 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10231 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10232 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10233 | NEXT; |
| 10234 | } else |
| 10235 | xmlParseComment(ctxt); |
| 10236 | } |
| 10237 | } |
| 10238 | |
| 10239 | /** |
| 10240 | * xmlParseDocument: |
| 10241 | * @ctxt: an XML parser context |
| 10242 | * |
| 10243 | * parse an XML document (and build a tree if using the standard SAX |
| 10244 | * interface). |
| 10245 | * |
| 10246 | * [1] document ::= prolog element Misc* |
| 10247 | * |
| 10248 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 10249 | * |
| 10250 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10251 | * as a result of the parsing. |
| 10252 | */ |
| 10253 | |
| 10254 | int |
| 10255 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 10256 | xmlChar start[4]; |
| 10257 | xmlCharEncoding enc; |
| 10258 | |
| 10259 | xmlInitParser(); |
| 10260 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10261 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10262 | return(-1); |
| 10263 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10264 | GROW; |
| 10265 | |
| 10266 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10267 | * SAX: detecting the level. |
| 10268 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10269 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10270 | |
| 10271 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10272 | * SAX: beginning of the document processing. |
| 10273 | */ |
| 10274 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10275 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10276 | |
Nikolay Sivov | e6ad10a | 2010-11-01 11:35:14 +0100 | [diff] [blame] | 10277 | if ((ctxt->encoding == NULL) && |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10278 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10279 | /* |
| 10280 | * Get the 4 first bytes and decode the charset |
| 10281 | * if enc != XML_CHAR_ENCODING_NONE |
| 10282 | * plug some encoding conversion routines. |
| 10283 | */ |
| 10284 | start[0] = RAW; |
| 10285 | start[1] = NXT(1); |
| 10286 | start[2] = NXT(2); |
| 10287 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10288 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10289 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10290 | xmlSwitchEncoding(ctxt, enc); |
| 10291 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10292 | } |
| 10293 | |
| 10294 | |
| 10295 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10296 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10297 | } |
| 10298 | |
| 10299 | /* |
| 10300 | * Check for the XMLDecl in the Prolog. |
Daniel Veillard | 7e385bd | 2009-08-26 11:38:49 +0200 | [diff] [blame] | 10301 | * do not GROW here to avoid the detected encoder to decode more |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10302 | * than just the first line, unless the amount of data is really |
| 10303 | * too small to hold "<?xml version="1.0" encoding="foo" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10304 | */ |
Daniel Veillard | 9d3d141 | 2009-09-15 18:41:30 +0200 | [diff] [blame] | 10305 | if ((ctxt->input->end - ctxt->input->cur) < 35) { |
| 10306 | GROW; |
| 10307 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10308 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10309 | |
| 10310 | /* |
| 10311 | * Note that we will switch encoding on the fly. |
| 10312 | */ |
| 10313 | xmlParseXMLDecl(ctxt); |
| 10314 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10315 | /* |
| 10316 | * The XML REC instructs us to stop parsing right here |
| 10317 | */ |
| 10318 | return(-1); |
| 10319 | } |
| 10320 | ctxt->standalone = ctxt->input->standalone; |
| 10321 | SKIP_BLANKS; |
| 10322 | } else { |
| 10323 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10324 | } |
| 10325 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10326 | ctxt->sax->startDocument(ctxt->userData); |
| 10327 | |
| 10328 | /* |
| 10329 | * The Misc part of the Prolog |
| 10330 | */ |
| 10331 | GROW; |
| 10332 | xmlParseMisc(ctxt); |
| 10333 | |
| 10334 | /* |
| 10335 | * Then possibly doc type declaration(s) and more Misc |
| 10336 | * (doctypedecl Misc*)? |
| 10337 | */ |
| 10338 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10339 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10340 | |
| 10341 | ctxt->inSubset = 1; |
| 10342 | xmlParseDocTypeDecl(ctxt); |
| 10343 | if (RAW == '[') { |
| 10344 | ctxt->instate = XML_PARSER_DTD; |
| 10345 | xmlParseInternalSubset(ctxt); |
| 10346 | } |
| 10347 | |
| 10348 | /* |
| 10349 | * Create and update the external subset. |
| 10350 | */ |
| 10351 | ctxt->inSubset = 2; |
| 10352 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10353 | (!ctxt->disableSAX)) |
| 10354 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10355 | ctxt->extSubSystem, ctxt->extSubURI); |
| 10356 | ctxt->inSubset = 0; |
| 10357 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10358 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10359 | |
| 10360 | ctxt->instate = XML_PARSER_PROLOG; |
| 10361 | xmlParseMisc(ctxt); |
| 10362 | } |
| 10363 | |
| 10364 | /* |
| 10365 | * Time to start parsing the tree itself |
| 10366 | */ |
| 10367 | GROW; |
| 10368 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10369 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10370 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10371 | } else { |
| 10372 | ctxt->instate = XML_PARSER_CONTENT; |
| 10373 | xmlParseElement(ctxt); |
| 10374 | ctxt->instate = XML_PARSER_EPILOG; |
| 10375 | |
| 10376 | |
| 10377 | /* |
| 10378 | * The Misc part at the end |
| 10379 | */ |
| 10380 | xmlParseMisc(ctxt); |
| 10381 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10382 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10383 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10384 | } |
| 10385 | ctxt->instate = XML_PARSER_EOF; |
| 10386 | } |
| 10387 | |
| 10388 | /* |
| 10389 | * SAX: end of the document processing. |
| 10390 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10391 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10392 | ctxt->sax->endDocument(ctxt->userData); |
| 10393 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10394 | /* |
| 10395 | * Remove locally kept entity definitions if the tree was not built |
| 10396 | */ |
| 10397 | if ((ctxt->myDoc != NULL) && |
| 10398 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10399 | xmlFreeDoc(ctxt->myDoc); |
| 10400 | ctxt->myDoc = NULL; |
| 10401 | } |
| 10402 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10403 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10404 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10405 | if (ctxt->valid) |
| 10406 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10407 | if (ctxt->nsWellFormed) |
| 10408 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10409 | if (ctxt->options & XML_PARSE_OLD10) |
| 10410 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10411 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10412 | if (! ctxt->wellFormed) { |
| 10413 | ctxt->valid = 0; |
| 10414 | return(-1); |
| 10415 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10416 | return(0); |
| 10417 | } |
| 10418 | |
| 10419 | /** |
| 10420 | * xmlParseExtParsedEnt: |
| 10421 | * @ctxt: an XML parser context |
| 10422 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10423 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10424 | * An external general parsed entity is well-formed if it matches the |
| 10425 | * production labeled extParsedEnt. |
| 10426 | * |
| 10427 | * [78] extParsedEnt ::= TextDecl? content |
| 10428 | * |
| 10429 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10430 | * as a result of the parsing. |
| 10431 | */ |
| 10432 | |
| 10433 | int |
| 10434 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10435 | xmlChar start[4]; |
| 10436 | xmlCharEncoding enc; |
| 10437 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10438 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10439 | return(-1); |
| 10440 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10441 | xmlDefaultSAXHandlerInit(); |
| 10442 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10443 | xmlDetectSAX2(ctxt); |
| 10444 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10445 | GROW; |
| 10446 | |
| 10447 | /* |
| 10448 | * SAX: beginning of the document processing. |
| 10449 | */ |
| 10450 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10451 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10452 | |
| 10453 | /* |
| 10454 | * Get the 4 first bytes and decode the charset |
| 10455 | * if enc != XML_CHAR_ENCODING_NONE |
| 10456 | * plug some encoding conversion routines. |
| 10457 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10458 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10459 | start[0] = RAW; |
| 10460 | start[1] = NXT(1); |
| 10461 | start[2] = NXT(2); |
| 10462 | start[3] = NXT(3); |
| 10463 | enc = xmlDetectCharEncoding(start, 4); |
| 10464 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10465 | xmlSwitchEncoding(ctxt, enc); |
| 10466 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10467 | } |
| 10468 | |
| 10469 | |
| 10470 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10471 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10472 | } |
| 10473 | |
| 10474 | /* |
| 10475 | * Check for the XMLDecl in the Prolog. |
| 10476 | */ |
| 10477 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10478 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10479 | |
| 10480 | /* |
| 10481 | * Note that we will switch encoding on the fly. |
| 10482 | */ |
| 10483 | xmlParseXMLDecl(ctxt); |
| 10484 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10485 | /* |
| 10486 | * The XML REC instructs us to stop parsing right here |
| 10487 | */ |
| 10488 | return(-1); |
| 10489 | } |
| 10490 | SKIP_BLANKS; |
| 10491 | } else { |
| 10492 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10493 | } |
| 10494 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10495 | ctxt->sax->startDocument(ctxt->userData); |
| 10496 | |
| 10497 | /* |
| 10498 | * Doing validity checking on chunk doesn't make sense |
| 10499 | */ |
| 10500 | ctxt->instate = XML_PARSER_CONTENT; |
| 10501 | ctxt->validate = 0; |
| 10502 | ctxt->loadsubset = 0; |
| 10503 | ctxt->depth = 0; |
| 10504 | |
| 10505 | xmlParseContent(ctxt); |
| 10506 | |
| 10507 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10508 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10509 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10510 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10511 | } |
| 10512 | |
| 10513 | /* |
| 10514 | * SAX: end of the document processing. |
| 10515 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10516 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10517 | ctxt->sax->endDocument(ctxt->userData); |
| 10518 | |
| 10519 | if (! ctxt->wellFormed) return(-1); |
| 10520 | return(0); |
| 10521 | } |
| 10522 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10523 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10524 | /************************************************************************ |
| 10525 | * * |
| 10526 | * Progressive parsing interfaces * |
| 10527 | * * |
| 10528 | ************************************************************************/ |
| 10529 | |
| 10530 | /** |
| 10531 | * xmlParseLookupSequence: |
| 10532 | * @ctxt: an XML parser context |
| 10533 | * @first: the first char to lookup |
| 10534 | * @next: the next char to lookup or zero |
| 10535 | * @third: the next char to lookup or zero |
| 10536 | * |
| 10537 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10538 | * (first) is available in the input stream. |
| 10539 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10540 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10541 | * parser, do not use liberally. |
| 10542 | * |
| 10543 | * Returns the index to the current parsing point if the full sequence |
| 10544 | * is available, -1 otherwise. |
| 10545 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10546 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10547 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10548 | xmlChar next, xmlChar third) { |
| 10549 | int base, len; |
| 10550 | xmlParserInputPtr in; |
| 10551 | const xmlChar *buf; |
| 10552 | |
| 10553 | in = ctxt->input; |
| 10554 | if (in == NULL) return(-1); |
| 10555 | base = in->cur - in->base; |
| 10556 | if (base < 0) return(-1); |
| 10557 | if (ctxt->checkIndex > base) |
| 10558 | base = ctxt->checkIndex; |
| 10559 | if (in->buf == NULL) { |
| 10560 | buf = in->base; |
| 10561 | len = in->length; |
| 10562 | } else { |
| 10563 | buf = in->buf->buffer->content; |
| 10564 | len = in->buf->buffer->use; |
| 10565 | } |
| 10566 | /* take into account the sequence length */ |
| 10567 | if (third) len -= 2; |
| 10568 | else if (next) len --; |
| 10569 | for (;base < len;base++) { |
| 10570 | if (buf[base] == first) { |
| 10571 | if (third != 0) { |
| 10572 | if ((buf[base + 1] != next) || |
| 10573 | (buf[base + 2] != third)) continue; |
| 10574 | } else if (next != 0) { |
| 10575 | if (buf[base + 1] != next) continue; |
| 10576 | } |
| 10577 | ctxt->checkIndex = 0; |
| 10578 | #ifdef DEBUG_PUSH |
| 10579 | if (next == 0) |
| 10580 | xmlGenericError(xmlGenericErrorContext, |
| 10581 | "PP: lookup '%c' found at %d\n", |
| 10582 | first, base); |
| 10583 | else if (third == 0) |
| 10584 | xmlGenericError(xmlGenericErrorContext, |
| 10585 | "PP: lookup '%c%c' found at %d\n", |
| 10586 | first, next, base); |
| 10587 | else |
| 10588 | xmlGenericError(xmlGenericErrorContext, |
| 10589 | "PP: lookup '%c%c%c' found at %d\n", |
| 10590 | first, next, third, base); |
| 10591 | #endif |
| 10592 | return(base - (in->cur - in->base)); |
| 10593 | } |
| 10594 | } |
| 10595 | ctxt->checkIndex = base; |
| 10596 | #ifdef DEBUG_PUSH |
| 10597 | if (next == 0) |
| 10598 | xmlGenericError(xmlGenericErrorContext, |
| 10599 | "PP: lookup '%c' failed\n", first); |
| 10600 | else if (third == 0) |
| 10601 | xmlGenericError(xmlGenericErrorContext, |
| 10602 | "PP: lookup '%c%c' failed\n", first, next); |
| 10603 | else |
| 10604 | xmlGenericError(xmlGenericErrorContext, |
| 10605 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 10606 | #endif |
| 10607 | return(-1); |
| 10608 | } |
| 10609 | |
| 10610 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10611 | * xmlParseGetLasts: |
| 10612 | * @ctxt: an XML parser context |
| 10613 | * @lastlt: pointer to store the last '<' from the input |
| 10614 | * @lastgt: pointer to store the last '>' from the input |
| 10615 | * |
| 10616 | * Lookup the last < and > in the current chunk |
| 10617 | */ |
| 10618 | static void |
| 10619 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 10620 | const xmlChar **lastgt) { |
| 10621 | const xmlChar *tmp; |
| 10622 | |
| 10623 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 10624 | xmlGenericError(xmlGenericErrorContext, |
| 10625 | "Internal error: xmlParseGetLasts\n"); |
| 10626 | return; |
| 10627 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 10628 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10629 | tmp = ctxt->input->end; |
| 10630 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10631 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10632 | if (tmp < ctxt->input->base) { |
| 10633 | *lastlt = NULL; |
| 10634 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10635 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10636 | *lastlt = tmp; |
| 10637 | tmp++; |
| 10638 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 10639 | if (*tmp == '\'') { |
| 10640 | tmp++; |
| 10641 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 10642 | if (tmp < ctxt->input->end) tmp++; |
| 10643 | } else if (*tmp == '"') { |
| 10644 | tmp++; |
| 10645 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 10646 | if (tmp < ctxt->input->end) tmp++; |
| 10647 | } else |
| 10648 | tmp++; |
| 10649 | } |
| 10650 | if (tmp < ctxt->input->end) |
| 10651 | *lastgt = tmp; |
| 10652 | else { |
| 10653 | tmp = *lastlt; |
| 10654 | tmp--; |
| 10655 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 10656 | if (tmp >= ctxt->input->base) |
| 10657 | *lastgt = tmp; |
| 10658 | else |
| 10659 | *lastgt = NULL; |
| 10660 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10661 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10662 | } else { |
| 10663 | *lastlt = NULL; |
| 10664 | *lastgt = NULL; |
| 10665 | } |
| 10666 | } |
| 10667 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10668 | * xmlCheckCdataPush: |
| 10669 | * @cur: pointer to the bock of characters |
| 10670 | * @len: length of the block in bytes |
| 10671 | * |
| 10672 | * Check that the block of characters is okay as SCdata content [20] |
| 10673 | * |
| 10674 | * Returns the number of bytes to pass if okay, a negative index where an |
| 10675 | * UTF-8 error occured otherwise |
| 10676 | */ |
| 10677 | static int |
| 10678 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 10679 | int ix; |
| 10680 | unsigned char c; |
| 10681 | int codepoint; |
| 10682 | |
| 10683 | if ((utf == NULL) || (len <= 0)) |
| 10684 | return(0); |
| 10685 | |
| 10686 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 10687 | c = utf[ix]; |
| 10688 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 10689 | if (c >= 0x20) |
| 10690 | ix++; |
| 10691 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 10692 | ix++; |
| 10693 | else |
| 10694 | return(-ix); |
| 10695 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 10696 | if (ix + 2 > len) return(ix); |
| 10697 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 10698 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10699 | codepoint = (utf[ix] & 0x1f) << 6; |
| 10700 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10701 | if (!xmlIsCharQ(codepoint)) |
| 10702 | return(-ix); |
| 10703 | ix += 2; |
| 10704 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 10705 | if (ix + 3 > len) return(ix); |
| 10706 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10707 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 10708 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10709 | codepoint = (utf[ix] & 0xf) << 12; |
| 10710 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 10711 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10712 | if (!xmlIsCharQ(codepoint)) |
| 10713 | return(-ix); |
| 10714 | ix += 3; |
| 10715 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 10716 | if (ix + 4 > len) return(ix); |
| 10717 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10718 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 10719 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 10720 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10721 | codepoint = (utf[ix] & 0x7) << 18; |
| 10722 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 10723 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 10724 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10725 | if (!xmlIsCharQ(codepoint)) |
| 10726 | return(-ix); |
| 10727 | ix += 4; |
| 10728 | } else /* unknown encoding */ |
| 10729 | return(-ix); |
| 10730 | } |
| 10731 | return(ix); |
| 10732 | } |
| 10733 | |
| 10734 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10735 | * xmlParseTryOrFinish: |
| 10736 | * @ctxt: an XML parser context |
| 10737 | * @terminate: last chunk indicator |
| 10738 | * |
| 10739 | * Try to progress on parsing |
| 10740 | * |
| 10741 | * Returns zero if no parsing was possible |
| 10742 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10743 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10744 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 10745 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10746 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10747 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10748 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10749 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10750 | if (ctxt->input == NULL) |
| 10751 | return(0); |
| 10752 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10753 | #ifdef DEBUG_PUSH |
| 10754 | switch (ctxt->instate) { |
| 10755 | case XML_PARSER_EOF: |
| 10756 | xmlGenericError(xmlGenericErrorContext, |
| 10757 | "PP: try EOF\n"); break; |
| 10758 | case XML_PARSER_START: |
| 10759 | xmlGenericError(xmlGenericErrorContext, |
| 10760 | "PP: try START\n"); break; |
| 10761 | case XML_PARSER_MISC: |
| 10762 | xmlGenericError(xmlGenericErrorContext, |
| 10763 | "PP: try MISC\n");break; |
| 10764 | case XML_PARSER_COMMENT: |
| 10765 | xmlGenericError(xmlGenericErrorContext, |
| 10766 | "PP: try COMMENT\n");break; |
| 10767 | case XML_PARSER_PROLOG: |
| 10768 | xmlGenericError(xmlGenericErrorContext, |
| 10769 | "PP: try PROLOG\n");break; |
| 10770 | case XML_PARSER_START_TAG: |
| 10771 | xmlGenericError(xmlGenericErrorContext, |
| 10772 | "PP: try START_TAG\n");break; |
| 10773 | case XML_PARSER_CONTENT: |
| 10774 | xmlGenericError(xmlGenericErrorContext, |
| 10775 | "PP: try CONTENT\n");break; |
| 10776 | case XML_PARSER_CDATA_SECTION: |
| 10777 | xmlGenericError(xmlGenericErrorContext, |
| 10778 | "PP: try CDATA_SECTION\n");break; |
| 10779 | case XML_PARSER_END_TAG: |
| 10780 | xmlGenericError(xmlGenericErrorContext, |
| 10781 | "PP: try END_TAG\n");break; |
| 10782 | case XML_PARSER_ENTITY_DECL: |
| 10783 | xmlGenericError(xmlGenericErrorContext, |
| 10784 | "PP: try ENTITY_DECL\n");break; |
| 10785 | case XML_PARSER_ENTITY_VALUE: |
| 10786 | xmlGenericError(xmlGenericErrorContext, |
| 10787 | "PP: try ENTITY_VALUE\n");break; |
| 10788 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 10789 | xmlGenericError(xmlGenericErrorContext, |
| 10790 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 10791 | case XML_PARSER_DTD: |
| 10792 | xmlGenericError(xmlGenericErrorContext, |
| 10793 | "PP: try DTD\n");break; |
| 10794 | case XML_PARSER_EPILOG: |
| 10795 | xmlGenericError(xmlGenericErrorContext, |
| 10796 | "PP: try EPILOG\n");break; |
| 10797 | case XML_PARSER_PI: |
| 10798 | xmlGenericError(xmlGenericErrorContext, |
| 10799 | "PP: try PI\n");break; |
| 10800 | case XML_PARSER_IGNORE: |
| 10801 | xmlGenericError(xmlGenericErrorContext, |
| 10802 | "PP: try IGNORE\n");break; |
| 10803 | } |
| 10804 | #endif |
| 10805 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10806 | if ((ctxt->input != NULL) && |
| 10807 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10808 | xmlSHRINK(ctxt); |
| 10809 | ctxt->checkIndex = 0; |
| 10810 | } |
| 10811 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 10812 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10813 | while (1) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 10814 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10815 | return(0); |
| 10816 | |
| 10817 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10818 | /* |
| 10819 | * Pop-up of finished entities. |
| 10820 | */ |
| 10821 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 10822 | xmlPopInput(ctxt); |
| 10823 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10824 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10825 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10826 | avail = ctxt->input->length - |
| 10827 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 10828 | else { |
| 10829 | /* |
| 10830 | * If we are operating on converted input, try to flush |
| 10831 | * remainng chars to avoid them stalling in the non-converted |
| 10832 | * buffer. |
| 10833 | */ |
| 10834 | if ((ctxt->input->buf->raw != NULL) && |
| 10835 | (ctxt->input->buf->raw->use > 0)) { |
| 10836 | int base = ctxt->input->base - |
| 10837 | ctxt->input->buf->buffer->content; |
| 10838 | int current = ctxt->input->cur - ctxt->input->base; |
| 10839 | |
| 10840 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 10841 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10842 | ctxt->input->cur = ctxt->input->base + current; |
| 10843 | ctxt->input->end = |
| 10844 | &ctxt->input->buf->buffer->content[ |
| 10845 | ctxt->input->buf->buffer->use]; |
| 10846 | } |
| 10847 | avail = ctxt->input->buf->buffer->use - |
| 10848 | (ctxt->input->cur - ctxt->input->base); |
| 10849 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10850 | if (avail < 1) |
| 10851 | goto done; |
| 10852 | switch (ctxt->instate) { |
| 10853 | case XML_PARSER_EOF: |
| 10854 | /* |
| 10855 | * Document parsing is done ! |
| 10856 | */ |
| 10857 | goto done; |
| 10858 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10859 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 10860 | xmlChar start[4]; |
| 10861 | xmlCharEncoding enc; |
| 10862 | |
| 10863 | /* |
| 10864 | * Very first chars read from the document flow. |
| 10865 | */ |
| 10866 | if (avail < 4) |
| 10867 | goto done; |
| 10868 | |
| 10869 | /* |
| 10870 | * Get the 4 first bytes and decode the charset |
| 10871 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10872 | * plug some encoding conversion routines, |
| 10873 | * else xmlSwitchEncoding will set to (default) |
| 10874 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10875 | */ |
| 10876 | start[0] = RAW; |
| 10877 | start[1] = NXT(1); |
| 10878 | start[2] = NXT(2); |
| 10879 | start[3] = NXT(3); |
| 10880 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10881 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10882 | break; |
| 10883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10884 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 10885 | if (avail < 2) |
| 10886 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10887 | cur = ctxt->input->cur[0]; |
| 10888 | next = ctxt->input->cur[1]; |
| 10889 | if (cur == 0) { |
| 10890 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10891 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10892 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10893 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10894 | ctxt->instate = XML_PARSER_EOF; |
| 10895 | #ifdef DEBUG_PUSH |
| 10896 | xmlGenericError(xmlGenericErrorContext, |
| 10897 | "PP: entering EOF\n"); |
| 10898 | #endif |
| 10899 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10900 | ctxt->sax->endDocument(ctxt->userData); |
| 10901 | goto done; |
| 10902 | } |
| 10903 | if ((cur == '<') && (next == '?')) { |
| 10904 | /* PI or XML decl */ |
| 10905 | if (avail < 5) return(ret); |
| 10906 | if ((!terminate) && |
| 10907 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10908 | return(ret); |
| 10909 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10910 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10911 | &xmlDefaultSAXLocator); |
| 10912 | if ((ctxt->input->cur[2] == 'x') && |
| 10913 | (ctxt->input->cur[3] == 'm') && |
| 10914 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10915 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10916 | ret += 5; |
| 10917 | #ifdef DEBUG_PUSH |
| 10918 | xmlGenericError(xmlGenericErrorContext, |
| 10919 | "PP: Parsing XML Decl\n"); |
| 10920 | #endif |
| 10921 | xmlParseXMLDecl(ctxt); |
| 10922 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10923 | /* |
| 10924 | * The XML REC instructs us to stop parsing right |
| 10925 | * here |
| 10926 | */ |
| 10927 | ctxt->instate = XML_PARSER_EOF; |
| 10928 | return(0); |
| 10929 | } |
| 10930 | ctxt->standalone = ctxt->input->standalone; |
| 10931 | if ((ctxt->encoding == NULL) && |
| 10932 | (ctxt->input->encoding != NULL)) |
| 10933 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 10934 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10935 | (!ctxt->disableSAX)) |
| 10936 | ctxt->sax->startDocument(ctxt->userData); |
| 10937 | ctxt->instate = XML_PARSER_MISC; |
| 10938 | #ifdef DEBUG_PUSH |
| 10939 | xmlGenericError(xmlGenericErrorContext, |
| 10940 | "PP: entering MISC\n"); |
| 10941 | #endif |
| 10942 | } else { |
| 10943 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10944 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10945 | (!ctxt->disableSAX)) |
| 10946 | ctxt->sax->startDocument(ctxt->userData); |
| 10947 | ctxt->instate = XML_PARSER_MISC; |
| 10948 | #ifdef DEBUG_PUSH |
| 10949 | xmlGenericError(xmlGenericErrorContext, |
| 10950 | "PP: entering MISC\n"); |
| 10951 | #endif |
| 10952 | } |
| 10953 | } else { |
| 10954 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10955 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10956 | &xmlDefaultSAXLocator); |
| 10957 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10958 | if (ctxt->version == NULL) { |
| 10959 | xmlErrMemory(ctxt, NULL); |
| 10960 | break; |
| 10961 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10962 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10963 | (!ctxt->disableSAX)) |
| 10964 | ctxt->sax->startDocument(ctxt->userData); |
| 10965 | ctxt->instate = XML_PARSER_MISC; |
| 10966 | #ifdef DEBUG_PUSH |
| 10967 | xmlGenericError(xmlGenericErrorContext, |
| 10968 | "PP: entering MISC\n"); |
| 10969 | #endif |
| 10970 | } |
| 10971 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10972 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10973 | const xmlChar *name; |
Daniel Veillard | 1549561 | 2009-09-05 15:04:41 +0200 | [diff] [blame] | 10974 | const xmlChar *prefix = NULL; |
| 10975 | const xmlChar *URI = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10976 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10977 | |
| 10978 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 10979 | goto done; |
| 10980 | cur = ctxt->input->cur[0]; |
| 10981 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10982 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10983 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10984 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10985 | ctxt->sax->endDocument(ctxt->userData); |
| 10986 | goto done; |
| 10987 | } |
| 10988 | if (!terminate) { |
| 10989 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 10990 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10991 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10992 | goto done; |
| 10993 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 10994 | goto done; |
| 10995 | } |
| 10996 | } |
| 10997 | if (ctxt->spaceNr == 0) |
| 10998 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 10999 | else if (*ctxt->space == -2) |
| 11000 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11001 | else |
| 11002 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11003 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11004 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11005 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11006 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11007 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11008 | else |
| 11009 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11010 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11011 | if (ctxt->instate == XML_PARSER_EOF) |
| 11012 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11013 | if (name == NULL) { |
| 11014 | spacePop(ctxt); |
| 11015 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11016 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 11017 | ctxt->sax->endDocument(ctxt->userData); |
| 11018 | goto done; |
| 11019 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11020 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11021 | /* |
| 11022 | * [ VC: Root Element Type ] |
| 11023 | * The Name in the document type declaration must match |
| 11024 | * the element type of the root element. |
| 11025 | */ |
| 11026 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 11027 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 11028 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11029 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11030 | |
| 11031 | /* |
| 11032 | * Check for an Empty Element. |
| 11033 | */ |
| 11034 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 11035 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11036 | |
| 11037 | if (ctxt->sax2) { |
| 11038 | if ((ctxt->sax != NULL) && |
| 11039 | (ctxt->sax->endElementNs != NULL) && |
| 11040 | (!ctxt->disableSAX)) |
| 11041 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 11042 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 11043 | if (ctxt->nsNr - nsNr > 0) |
| 11044 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11045 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11046 | } else { |
| 11047 | if ((ctxt->sax != NULL) && |
| 11048 | (ctxt->sax->endElement != NULL) && |
| 11049 | (!ctxt->disableSAX)) |
| 11050 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11051 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11052 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11053 | spacePop(ctxt); |
| 11054 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11055 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11056 | } else { |
| 11057 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11058 | } |
| 11059 | break; |
| 11060 | } |
| 11061 | if (RAW == '>') { |
| 11062 | NEXT; |
| 11063 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 11064 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11065 | "Couldn't find end of Start Tag %s\n", |
| 11066 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11067 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11068 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11069 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11070 | if (ctxt->sax2) |
| 11071 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11072 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11073 | else |
| 11074 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11075 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11076 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11077 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11078 | break; |
| 11079 | } |
| 11080 | case XML_PARSER_CONTENT: { |
| 11081 | const xmlChar *test; |
| 11082 | unsigned int cons; |
| 11083 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 11084 | goto done; |
| 11085 | cur = ctxt->input->cur[0]; |
| 11086 | next = ctxt->input->cur[1]; |
| 11087 | |
| 11088 | test = CUR_PTR; |
| 11089 | cons = ctxt->input->consumed; |
| 11090 | if ((cur == '<') && (next == '/')) { |
| 11091 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11092 | break; |
| 11093 | } else if ((cur == '<') && (next == '?')) { |
| 11094 | if ((!terminate) && |
| 11095 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11096 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11097 | xmlParsePI(ctxt); |
| 11098 | } else if ((cur == '<') && (next != '!')) { |
| 11099 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11100 | break; |
| 11101 | } else if ((cur == '<') && (next == '!') && |
| 11102 | (ctxt->input->cur[2] == '-') && |
| 11103 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 11104 | int term; |
| 11105 | |
| 11106 | if (avail < 4) |
| 11107 | goto done; |
| 11108 | ctxt->input->cur += 4; |
| 11109 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 11110 | ctxt->input->cur -= 4; |
| 11111 | if ((!terminate) && (term < 0)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11112 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11113 | xmlParseComment(ctxt); |
| 11114 | ctxt->instate = XML_PARSER_CONTENT; |
| 11115 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 11116 | (ctxt->input->cur[2] == '[') && |
| 11117 | (ctxt->input->cur[3] == 'C') && |
| 11118 | (ctxt->input->cur[4] == 'D') && |
| 11119 | (ctxt->input->cur[5] == 'A') && |
| 11120 | (ctxt->input->cur[6] == 'T') && |
| 11121 | (ctxt->input->cur[7] == 'A') && |
| 11122 | (ctxt->input->cur[8] == '[')) { |
| 11123 | SKIP(9); |
| 11124 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11125 | break; |
| 11126 | } else if ((cur == '<') && (next == '!') && |
| 11127 | (avail < 9)) { |
| 11128 | goto done; |
| 11129 | } else if (cur == '&') { |
| 11130 | if ((!terminate) && |
| 11131 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 11132 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11133 | xmlParseReference(ctxt); |
| 11134 | } else { |
| 11135 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 11136 | /* |
| 11137 | * Goal of the following test is: |
| 11138 | * - minimize calls to the SAX 'character' callback |
| 11139 | * when they are mergeable |
| 11140 | * - handle an problem for isBlank when we only parse |
| 11141 | * a sequence of blank chars and the next one is |
| 11142 | * not available to check against '<' presence. |
| 11143 | * - tries to homogenize the differences in SAX |
| 11144 | * callbacks between the push and pull versions |
| 11145 | * of the parser. |
| 11146 | */ |
| 11147 | if ((ctxt->inputNr == 1) && |
| 11148 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 11149 | if (!terminate) { |
| 11150 | if (ctxt->progressive) { |
| 11151 | if ((lastlt == NULL) || |
| 11152 | (ctxt->input->cur > lastlt)) |
| 11153 | goto done; |
| 11154 | } else if (xmlParseLookupSequence(ctxt, |
| 11155 | '<', 0, 0) < 0) { |
| 11156 | goto done; |
| 11157 | } |
| 11158 | } |
| 11159 | } |
| 11160 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11161 | xmlParseCharData(ctxt, 0); |
| 11162 | } |
| 11163 | /* |
| 11164 | * Pop-up of finished entities. |
| 11165 | */ |
| 11166 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 11167 | xmlPopInput(ctxt); |
| 11168 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11169 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 11170 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11171 | ctxt->instate = XML_PARSER_EOF; |
| 11172 | break; |
| 11173 | } |
| 11174 | break; |
| 11175 | } |
| 11176 | case XML_PARSER_END_TAG: |
| 11177 | if (avail < 2) |
| 11178 | goto done; |
| 11179 | if (!terminate) { |
| 11180 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 11181 | /* > can be found unescaped in attribute values */ |
| 11182 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11183 | goto done; |
| 11184 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 11185 | goto done; |
| 11186 | } |
| 11187 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11188 | if (ctxt->sax2) { |
| 11189 | xmlParseEndTag2(ctxt, |
| 11190 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 11191 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 11192 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11193 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11194 | } |
| 11195 | #ifdef LIBXML_SAX1_ENABLED |
| 11196 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11197 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11198 | #endif /* LIBXML_SAX1_ENABLED */ |
Chris Evans | 77404b8 | 2011-12-14 16:18:25 +0800 | [diff] [blame] | 11199 | if (ctxt->instate == XML_PARSER_EOF) { |
| 11200 | /* Nothing */ |
| 11201 | } else if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11202 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11203 | } else { |
| 11204 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11205 | } |
| 11206 | break; |
| 11207 | case XML_PARSER_CDATA_SECTION: { |
| 11208 | /* |
| 11209 | * The Push mode need to have the SAX callback for |
| 11210 | * cdataBlock merge back contiguous callbacks. |
| 11211 | */ |
| 11212 | int base; |
| 11213 | |
| 11214 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 11215 | if (base < 0) { |
| 11216 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11217 | int tmp; |
| 11218 | |
| 11219 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
| 11220 | XML_PARSER_BIG_BUFFER_SIZE); |
| 11221 | if (tmp < 0) { |
| 11222 | tmp = -tmp; |
| 11223 | ctxt->input->cur += tmp; |
| 11224 | goto encoding_error; |
| 11225 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11226 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 11227 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11228 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11229 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11230 | else if (ctxt->sax->characters != NULL) |
| 11231 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11232 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11233 | } |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11234 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11235 | ctxt->checkIndex = 0; |
| 11236 | } |
| 11237 | goto done; |
| 11238 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11239 | int tmp; |
| 11240 | |
| 11241 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 11242 | if ((tmp < 0) || (tmp != base)) { |
| 11243 | tmp = -tmp; |
| 11244 | ctxt->input->cur += tmp; |
| 11245 | goto encoding_error; |
| 11246 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 11247 | if ((ctxt->sax != NULL) && (base == 0) && |
| 11248 | (ctxt->sax->cdataBlock != NULL) && |
| 11249 | (!ctxt->disableSAX)) { |
| 11250 | /* |
| 11251 | * Special case to provide identical behaviour |
| 11252 | * between pull and push parsers on enpty CDATA |
| 11253 | * sections |
| 11254 | */ |
| 11255 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 11256 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 11257 | "<![CDATA[", 9))) |
| 11258 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11259 | BAD_CAST "", 0); |
| 11260 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11261 | (!ctxt->disableSAX)) { |
| 11262 | if (ctxt->sax->cdataBlock != NULL) |
| 11263 | ctxt->sax->cdataBlock(ctxt->userData, |
| 11264 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 11265 | else if (ctxt->sax->characters != NULL) |
| 11266 | ctxt->sax->characters(ctxt->userData, |
| 11267 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11268 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 11269 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11270 | ctxt->checkIndex = 0; |
| 11271 | ctxt->instate = XML_PARSER_CONTENT; |
| 11272 | #ifdef DEBUG_PUSH |
| 11273 | xmlGenericError(xmlGenericErrorContext, |
| 11274 | "PP: entering CONTENT\n"); |
| 11275 | #endif |
| 11276 | } |
| 11277 | break; |
| 11278 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11279 | case XML_PARSER_MISC: |
| 11280 | SKIP_BLANKS; |
| 11281 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11282 | avail = ctxt->input->length - |
| 11283 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11284 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11285 | avail = ctxt->input->buf->buffer->use - |
| 11286 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11287 | if (avail < 2) |
| 11288 | goto done; |
| 11289 | cur = ctxt->input->cur[0]; |
| 11290 | next = ctxt->input->cur[1]; |
| 11291 | if ((cur == '<') && (next == '?')) { |
| 11292 | if ((!terminate) && |
| 11293 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11294 | goto done; |
| 11295 | #ifdef DEBUG_PUSH |
| 11296 | xmlGenericError(xmlGenericErrorContext, |
| 11297 | "PP: Parsing PI\n"); |
| 11298 | #endif |
| 11299 | xmlParsePI(ctxt); |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11300 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11301 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11302 | (ctxt->input->cur[2] == '-') && |
| 11303 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11304 | if ((!terminate) && |
| 11305 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11306 | goto done; |
| 11307 | #ifdef DEBUG_PUSH |
| 11308 | xmlGenericError(xmlGenericErrorContext, |
| 11309 | "PP: Parsing Comment\n"); |
| 11310 | #endif |
| 11311 | xmlParseComment(ctxt); |
| 11312 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11313 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11314 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11315 | (ctxt->input->cur[2] == 'D') && |
| 11316 | (ctxt->input->cur[3] == 'O') && |
| 11317 | (ctxt->input->cur[4] == 'C') && |
| 11318 | (ctxt->input->cur[5] == 'T') && |
| 11319 | (ctxt->input->cur[6] == 'Y') && |
| 11320 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11321 | (ctxt->input->cur[8] == 'E')) { |
| 11322 | if ((!terminate) && |
| 11323 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 11324 | goto done; |
| 11325 | #ifdef DEBUG_PUSH |
| 11326 | xmlGenericError(xmlGenericErrorContext, |
| 11327 | "PP: Parsing internal subset\n"); |
| 11328 | #endif |
| 11329 | ctxt->inSubset = 1; |
| 11330 | xmlParseDocTypeDecl(ctxt); |
| 11331 | if (RAW == '[') { |
| 11332 | ctxt->instate = XML_PARSER_DTD; |
| 11333 | #ifdef DEBUG_PUSH |
| 11334 | xmlGenericError(xmlGenericErrorContext, |
| 11335 | "PP: entering DTD\n"); |
| 11336 | #endif |
| 11337 | } else { |
| 11338 | /* |
| 11339 | * Create and update the external subset. |
| 11340 | */ |
| 11341 | ctxt->inSubset = 2; |
| 11342 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11343 | (ctxt->sax->externalSubset != NULL)) |
| 11344 | ctxt->sax->externalSubset(ctxt->userData, |
| 11345 | ctxt->intSubName, ctxt->extSubSystem, |
| 11346 | ctxt->extSubURI); |
| 11347 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11348 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11349 | ctxt->instate = XML_PARSER_PROLOG; |
| 11350 | #ifdef DEBUG_PUSH |
| 11351 | xmlGenericError(xmlGenericErrorContext, |
| 11352 | "PP: entering PROLOG\n"); |
| 11353 | #endif |
| 11354 | } |
| 11355 | } else if ((cur == '<') && (next == '!') && |
| 11356 | (avail < 9)) { |
| 11357 | goto done; |
| 11358 | } else { |
| 11359 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11360 | ctxt->progressive = 1; |
| 11361 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11362 | #ifdef DEBUG_PUSH |
| 11363 | xmlGenericError(xmlGenericErrorContext, |
| 11364 | "PP: entering START_TAG\n"); |
| 11365 | #endif |
| 11366 | } |
| 11367 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11368 | case XML_PARSER_PROLOG: |
| 11369 | SKIP_BLANKS; |
| 11370 | if (ctxt->input->buf == NULL) |
| 11371 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11372 | else |
| 11373 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11374 | if (avail < 2) |
| 11375 | goto done; |
| 11376 | cur = ctxt->input->cur[0]; |
| 11377 | next = ctxt->input->cur[1]; |
| 11378 | if ((cur == '<') && (next == '?')) { |
| 11379 | if ((!terminate) && |
| 11380 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11381 | goto done; |
| 11382 | #ifdef DEBUG_PUSH |
| 11383 | xmlGenericError(xmlGenericErrorContext, |
| 11384 | "PP: Parsing PI\n"); |
| 11385 | #endif |
| 11386 | xmlParsePI(ctxt); |
| 11387 | } else if ((cur == '<') && (next == '!') && |
| 11388 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11389 | if ((!terminate) && |
| 11390 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11391 | goto done; |
| 11392 | #ifdef DEBUG_PUSH |
| 11393 | xmlGenericError(xmlGenericErrorContext, |
| 11394 | "PP: Parsing Comment\n"); |
| 11395 | #endif |
| 11396 | xmlParseComment(ctxt); |
| 11397 | ctxt->instate = XML_PARSER_PROLOG; |
| 11398 | } else if ((cur == '<') && (next == '!') && |
| 11399 | (avail < 4)) { |
| 11400 | goto done; |
| 11401 | } else { |
| 11402 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11403 | if (ctxt->progressive == 0) |
| 11404 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11405 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11406 | #ifdef DEBUG_PUSH |
| 11407 | xmlGenericError(xmlGenericErrorContext, |
| 11408 | "PP: entering START_TAG\n"); |
| 11409 | #endif |
| 11410 | } |
| 11411 | break; |
| 11412 | case XML_PARSER_EPILOG: |
| 11413 | SKIP_BLANKS; |
| 11414 | if (ctxt->input->buf == NULL) |
| 11415 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11416 | else |
| 11417 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11418 | if (avail < 2) |
| 11419 | goto done; |
| 11420 | cur = ctxt->input->cur[0]; |
| 11421 | next = ctxt->input->cur[1]; |
| 11422 | if ((cur == '<') && (next == '?')) { |
| 11423 | if ((!terminate) && |
| 11424 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11425 | goto done; |
| 11426 | #ifdef DEBUG_PUSH |
| 11427 | xmlGenericError(xmlGenericErrorContext, |
| 11428 | "PP: Parsing PI\n"); |
| 11429 | #endif |
| 11430 | xmlParsePI(ctxt); |
| 11431 | ctxt->instate = XML_PARSER_EPILOG; |
| 11432 | } else if ((cur == '<') && (next == '!') && |
| 11433 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11434 | if ((!terminate) && |
| 11435 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11436 | goto done; |
| 11437 | #ifdef DEBUG_PUSH |
| 11438 | xmlGenericError(xmlGenericErrorContext, |
| 11439 | "PP: Parsing Comment\n"); |
| 11440 | #endif |
| 11441 | xmlParseComment(ctxt); |
| 11442 | ctxt->instate = XML_PARSER_EPILOG; |
| 11443 | } else if ((cur == '<') && (next == '!') && |
| 11444 | (avail < 4)) { |
| 11445 | goto done; |
| 11446 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11447 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11448 | ctxt->instate = XML_PARSER_EOF; |
| 11449 | #ifdef DEBUG_PUSH |
| 11450 | xmlGenericError(xmlGenericErrorContext, |
| 11451 | "PP: entering EOF\n"); |
| 11452 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11453 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11454 | ctxt->sax->endDocument(ctxt->userData); |
| 11455 | goto done; |
| 11456 | } |
| 11457 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11458 | case XML_PARSER_DTD: { |
| 11459 | /* |
| 11460 | * Sorry but progressive parsing of the internal subset |
| 11461 | * is not expected to be supported. We first check that |
| 11462 | * the full content of the internal subset is available and |
| 11463 | * the parsing is launched only at that point. |
| 11464 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11465 | * section and not in a ']]>' sequence which are conditional |
| 11466 | * sections (whoever argued to keep that crap in XML deserve |
| 11467 | * a place in hell !). |
| 11468 | */ |
| 11469 | int base, i; |
| 11470 | xmlChar *buf; |
| 11471 | xmlChar quote = 0; |
| 11472 | |
| 11473 | base = ctxt->input->cur - ctxt->input->base; |
| 11474 | if (base < 0) return(0); |
| 11475 | if (ctxt->checkIndex > base) |
| 11476 | base = ctxt->checkIndex; |
| 11477 | buf = ctxt->input->buf->buffer->content; |
| 11478 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 11479 | base++) { |
| 11480 | if (quote != 0) { |
| 11481 | if (buf[base] == quote) |
| 11482 | quote = 0; |
| 11483 | continue; |
| 11484 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11485 | if ((quote == 0) && (buf[base] == '<')) { |
| 11486 | int found = 0; |
| 11487 | /* special handling of comments */ |
| 11488 | if (((unsigned int) base + 4 < |
| 11489 | ctxt->input->buf->buffer->use) && |
| 11490 | (buf[base + 1] == '!') && |
| 11491 | (buf[base + 2] == '-') && |
| 11492 | (buf[base + 3] == '-')) { |
| 11493 | for (;(unsigned int) base + 3 < |
| 11494 | ctxt->input->buf->buffer->use; base++) { |
| 11495 | if ((buf[base] == '-') && |
| 11496 | (buf[base + 1] == '-') && |
| 11497 | (buf[base + 2] == '>')) { |
| 11498 | found = 1; |
| 11499 | base += 2; |
| 11500 | break; |
| 11501 | } |
| 11502 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11503 | if (!found) { |
| 11504 | #if 0 |
| 11505 | fprintf(stderr, "unfinished comment\n"); |
| 11506 | #endif |
| 11507 | break; /* for */ |
| 11508 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11509 | continue; |
| 11510 | } |
| 11511 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11512 | if (buf[base] == '"') { |
| 11513 | quote = '"'; |
| 11514 | continue; |
| 11515 | } |
| 11516 | if (buf[base] == '\'') { |
| 11517 | quote = '\''; |
| 11518 | continue; |
| 11519 | } |
| 11520 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11521 | #if 0 |
| 11522 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 11523 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 11524 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11525 | if ((unsigned int) base +1 >= |
| 11526 | ctxt->input->buf->buffer->use) |
| 11527 | break; |
| 11528 | if (buf[base + 1] == ']') { |
| 11529 | /* conditional crap, skip both ']' ! */ |
| 11530 | base++; |
| 11531 | continue; |
| 11532 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11533 | for (i = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11534 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 11535 | i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11536 | if (buf[base + i] == '>') { |
| 11537 | #if 0 |
| 11538 | fprintf(stderr, "found\n"); |
| 11539 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11540 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11541 | } |
| 11542 | if (!IS_BLANK_CH(buf[base + i])) { |
| 11543 | #if 0 |
| 11544 | fprintf(stderr, "not found\n"); |
| 11545 | #endif |
| 11546 | goto not_end_of_int_subset; |
| 11547 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11548 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11549 | #if 0 |
| 11550 | fprintf(stderr, "end of stream\n"); |
| 11551 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11552 | break; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11553 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11554 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11555 | not_end_of_int_subset: |
| 11556 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11557 | } |
| 11558 | /* |
| 11559 | * We didn't found the end of the Internal subset |
| 11560 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11561 | #ifdef DEBUG_PUSH |
| 11562 | if (next == 0) |
| 11563 | xmlGenericError(xmlGenericErrorContext, |
| 11564 | "PP: lookup of int subset end filed\n"); |
| 11565 | #endif |
| 11566 | goto done; |
| 11567 | |
| 11568 | found_end_int_subset: |
| 11569 | xmlParseInternalSubset(ctxt); |
| 11570 | ctxt->inSubset = 2; |
| 11571 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11572 | (ctxt->sax->externalSubset != NULL)) |
| 11573 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 11574 | ctxt->extSubSystem, ctxt->extSubURI); |
| 11575 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11576 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11577 | ctxt->instate = XML_PARSER_PROLOG; |
| 11578 | ctxt->checkIndex = 0; |
| 11579 | #ifdef DEBUG_PUSH |
| 11580 | xmlGenericError(xmlGenericErrorContext, |
| 11581 | "PP: entering PROLOG\n"); |
| 11582 | #endif |
| 11583 | break; |
| 11584 | } |
| 11585 | case XML_PARSER_COMMENT: |
| 11586 | xmlGenericError(xmlGenericErrorContext, |
| 11587 | "PP: internal error, state == COMMENT\n"); |
| 11588 | ctxt->instate = XML_PARSER_CONTENT; |
| 11589 | #ifdef DEBUG_PUSH |
| 11590 | xmlGenericError(xmlGenericErrorContext, |
| 11591 | "PP: entering CONTENT\n"); |
| 11592 | #endif |
| 11593 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11594 | case XML_PARSER_IGNORE: |
| 11595 | xmlGenericError(xmlGenericErrorContext, |
| 11596 | "PP: internal error, state == IGNORE"); |
| 11597 | ctxt->instate = XML_PARSER_DTD; |
| 11598 | #ifdef DEBUG_PUSH |
| 11599 | xmlGenericError(xmlGenericErrorContext, |
| 11600 | "PP: entering DTD\n"); |
| 11601 | #endif |
| 11602 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11603 | case XML_PARSER_PI: |
| 11604 | xmlGenericError(xmlGenericErrorContext, |
| 11605 | "PP: internal error, state == PI\n"); |
| 11606 | ctxt->instate = XML_PARSER_CONTENT; |
| 11607 | #ifdef DEBUG_PUSH |
| 11608 | xmlGenericError(xmlGenericErrorContext, |
| 11609 | "PP: entering CONTENT\n"); |
| 11610 | #endif |
| 11611 | break; |
| 11612 | case XML_PARSER_ENTITY_DECL: |
| 11613 | xmlGenericError(xmlGenericErrorContext, |
| 11614 | "PP: internal error, state == ENTITY_DECL\n"); |
| 11615 | ctxt->instate = XML_PARSER_DTD; |
| 11616 | #ifdef DEBUG_PUSH |
| 11617 | xmlGenericError(xmlGenericErrorContext, |
| 11618 | "PP: entering DTD\n"); |
| 11619 | #endif |
| 11620 | break; |
| 11621 | case XML_PARSER_ENTITY_VALUE: |
| 11622 | xmlGenericError(xmlGenericErrorContext, |
| 11623 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 11624 | ctxt->instate = XML_PARSER_CONTENT; |
| 11625 | #ifdef DEBUG_PUSH |
| 11626 | xmlGenericError(xmlGenericErrorContext, |
| 11627 | "PP: entering DTD\n"); |
| 11628 | #endif |
| 11629 | break; |
| 11630 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11631 | xmlGenericError(xmlGenericErrorContext, |
| 11632 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 11633 | ctxt->instate = XML_PARSER_START_TAG; |
| 11634 | #ifdef DEBUG_PUSH |
| 11635 | xmlGenericError(xmlGenericErrorContext, |
| 11636 | "PP: entering START_TAG\n"); |
| 11637 | #endif |
| 11638 | break; |
| 11639 | case XML_PARSER_SYSTEM_LITERAL: |
| 11640 | xmlGenericError(xmlGenericErrorContext, |
| 11641 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 11642 | ctxt->instate = XML_PARSER_START_TAG; |
| 11643 | #ifdef DEBUG_PUSH |
| 11644 | xmlGenericError(xmlGenericErrorContext, |
| 11645 | "PP: entering START_TAG\n"); |
| 11646 | #endif |
| 11647 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 11648 | case XML_PARSER_PUBLIC_LITERAL: |
| 11649 | xmlGenericError(xmlGenericErrorContext, |
| 11650 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 11651 | ctxt->instate = XML_PARSER_START_TAG; |
| 11652 | #ifdef DEBUG_PUSH |
| 11653 | xmlGenericError(xmlGenericErrorContext, |
| 11654 | "PP: entering START_TAG\n"); |
| 11655 | #endif |
| 11656 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11657 | } |
| 11658 | } |
| 11659 | done: |
| 11660 | #ifdef DEBUG_PUSH |
| 11661 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 11662 | #endif |
| 11663 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11664 | encoding_error: |
| 11665 | { |
| 11666 | char buffer[150]; |
| 11667 | |
| 11668 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 11669 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 11670 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 11671 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 11672 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 11673 | BAD_CAST buffer, NULL); |
| 11674 | } |
| 11675 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11676 | } |
| 11677 | |
| 11678 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11679 | * xmlParseChunk: |
| 11680 | * @ctxt: an XML parser context |
| 11681 | * @chunk: an char array |
| 11682 | * @size: the size in byte of the chunk |
| 11683 | * @terminate: last chunk indicator |
| 11684 | * |
| 11685 | * Parse a Chunk of memory |
| 11686 | * |
| 11687 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 11688 | */ |
| 11689 | int |
| 11690 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 11691 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11692 | int end_in_lf = 0; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11693 | int remain = 0; |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11694 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11695 | if (ctxt == NULL) |
| 11696 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11697 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11698 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 11699 | if (ctxt->instate == XML_PARSER_START) |
| 11700 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11701 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 11702 | (chunk[size - 1] == '\r')) { |
| 11703 | end_in_lf = 1; |
| 11704 | size--; |
| 11705 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11706 | |
| 11707 | xmldecl_done: |
| 11708 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11709 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 11710 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 11711 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11712 | int cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11713 | int res; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11714 | |
| 11715 | /* |
| 11716 | * Specific handling if we autodetected an encoding, we should not |
| 11717 | * push more than the first line ... which depend on the encoding |
| 11718 | * And only push the rest once the final encoding was detected |
| 11719 | */ |
| 11720 | if ((ctxt->instate == XML_PARSER_START) && (ctxt->input != NULL) && |
| 11721 | (ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL)) { |
Nikolay Sivov | 7304683 | 2010-01-19 15:38:05 +0100 | [diff] [blame] | 11722 | unsigned int len = 45; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11723 | |
| 11724 | if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 11725 | BAD_CAST "UTF-16")) || |
| 11726 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 11727 | BAD_CAST "UTF16"))) |
| 11728 | len = 90; |
| 11729 | else if ((xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 11730 | BAD_CAST "UCS-4")) || |
| 11731 | (xmlStrcasestr(BAD_CAST ctxt->input->buf->encoder->name, |
| 11732 | BAD_CAST "UCS4"))) |
| 11733 | len = 180; |
| 11734 | |
| 11735 | if (ctxt->input->buf->rawconsumed < len) |
| 11736 | len -= ctxt->input->buf->rawconsumed; |
| 11737 | |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 11738 | /* |
| 11739 | * Change size for reading the initial declaration only |
| 11740 | * if size is greater than len. Otherwise, memmove in xmlBufferAdd |
| 11741 | * will blindly copy extra bytes from memory. |
| 11742 | */ |
Daniel Veillard | 60587d6 | 2010-11-04 15:16:27 +0100 | [diff] [blame] | 11743 | if ((unsigned int) size > len) { |
Raul Hudea | ba9716a | 2010-03-15 10:13:29 +0100 | [diff] [blame] | 11744 | remain = size - len; |
| 11745 | size = len; |
| 11746 | } else { |
| 11747 | remain = 0; |
| 11748 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11749 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11750 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 11751 | if (res < 0) { |
| 11752 | ctxt->errNo = XML_PARSER_EOF; |
| 11753 | ctxt->disableSAX = 1; |
| 11754 | return (XML_PARSER_EOF); |
| 11755 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11756 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11757 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11758 | ctxt->input->end = |
| 11759 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11760 | #ifdef DEBUG_PUSH |
| 11761 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11762 | #endif |
| 11763 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11764 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 11765 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 11766 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 11767 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 11768 | (in->raw != NULL)) { |
| 11769 | int nbchars; |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11770 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11771 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 11772 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11773 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11774 | xmlGenericError(xmlGenericErrorContext, |
| 11775 | "xmlParseChunk: encoder error\n"); |
| 11776 | return(XML_ERR_INVALID_ENCODING); |
| 11777 | } |
| 11778 | } |
| 11779 | } |
| 11780 | } |
Daniel Veillard | a6c76a2 | 2009-08-26 14:37:00 +0200 | [diff] [blame] | 11781 | if (remain != 0) |
| 11782 | xmlParseTryOrFinish(ctxt, 0); |
| 11783 | else |
| 11784 | xmlParseTryOrFinish(ctxt, terminate); |
| 11785 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 11786 | return(ctxt->errNo); |
| 11787 | |
| 11788 | if (remain != 0) { |
| 11789 | chunk += size; |
| 11790 | size = remain; |
| 11791 | remain = 0; |
| 11792 | goto xmldecl_done; |
| 11793 | } |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11794 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 11795 | (ctxt->input->buf != NULL)) { |
| 11796 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
| 11797 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11798 | if (terminate) { |
| 11799 | /* |
| 11800 | * Check for termination |
| 11801 | */ |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11802 | int avail = 0; |
| 11803 | |
| 11804 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11805 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11806 | avail = ctxt->input->length - |
| 11807 | (ctxt->input->cur - ctxt->input->base); |
| 11808 | else |
| 11809 | avail = ctxt->input->buf->buffer->use - |
| 11810 | (ctxt->input->cur - ctxt->input->base); |
| 11811 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11812 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11813 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 11814 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11815 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11816 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11817 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11818 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11819 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11820 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11821 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11822 | ctxt->sax->endDocument(ctxt->userData); |
| 11823 | } |
| 11824 | ctxt->instate = XML_PARSER_EOF; |
| 11825 | } |
| 11826 | return((xmlParserErrors) ctxt->errNo); |
| 11827 | } |
| 11828 | |
| 11829 | /************************************************************************ |
| 11830 | * * |
| 11831 | * I/O front end functions to the parser * |
| 11832 | * * |
| 11833 | ************************************************************************/ |
| 11834 | |
| 11835 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11836 | * xmlCreatePushParserCtxt: |
| 11837 | * @sax: a SAX handler |
| 11838 | * @user_data: The user data returned on SAX callbacks |
| 11839 | * @chunk: a pointer to an array of chars |
| 11840 | * @size: number of chars in the array |
| 11841 | * @filename: an optional file name or URI |
| 11842 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11843 | * Create a parser context for using the XML parser in push mode. |
| 11844 | * If @buffer and @size are non-NULL, the data is used to detect |
| 11845 | * the encoding. The remaining characters will be parsed so they |
| 11846 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11847 | * To allow content encoding detection, @size should be >= 4 |
| 11848 | * The value of @filename is used for fetching external entities |
| 11849 | * and error/warning reports. |
| 11850 | * |
| 11851 | * Returns the new parser context or NULL |
| 11852 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11854 | xmlParserCtxtPtr |
| 11855 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 11856 | const char *chunk, int size, const char *filename) { |
| 11857 | xmlParserCtxtPtr ctxt; |
| 11858 | xmlParserInputPtr inputStream; |
| 11859 | xmlParserInputBufferPtr buf; |
| 11860 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 11861 | |
| 11862 | /* |
| 11863 | * plug some encoding conversion routines |
| 11864 | */ |
| 11865 | if ((chunk != NULL) && (size >= 4)) |
| 11866 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 11867 | |
| 11868 | buf = xmlAllocParserInputBuffer(enc); |
| 11869 | if (buf == NULL) return(NULL); |
| 11870 | |
| 11871 | ctxt = xmlNewParserCtxt(); |
| 11872 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11873 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11874 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11875 | return(NULL); |
| 11876 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11877 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11878 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 11879 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11880 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11881 | xmlFreeParserInputBuffer(buf); |
| 11882 | xmlFreeParserCtxt(ctxt); |
| 11883 | return(NULL); |
| 11884 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11885 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11886 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11887 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11888 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11889 | xmlFree(ctxt->sax); |
| 11890 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 11891 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11892 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11893 | xmlFreeParserInputBuffer(buf); |
| 11894 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11895 | return(NULL); |
| 11896 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 11897 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 11898 | if (sax->initialized == XML_SAX2_MAGIC) |
| 11899 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 11900 | else |
| 11901 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11902 | if (user_data != NULL) |
| 11903 | ctxt->userData = user_data; |
| 11904 | } |
| 11905 | if (filename == NULL) { |
| 11906 | ctxt->directory = NULL; |
| 11907 | } else { |
| 11908 | ctxt->directory = xmlParserGetDirectory(filename); |
| 11909 | } |
| 11910 | |
| 11911 | inputStream = xmlNewInputStream(ctxt); |
| 11912 | if (inputStream == NULL) { |
| 11913 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11914 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11915 | return(NULL); |
| 11916 | } |
| 11917 | |
| 11918 | if (filename == NULL) |
| 11919 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11920 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 11921 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11922 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11923 | if (inputStream->filename == NULL) { |
| 11924 | xmlFreeParserCtxt(ctxt); |
| 11925 | xmlFreeParserInputBuffer(buf); |
| 11926 | return(NULL); |
| 11927 | } |
| 11928 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11929 | inputStream->buf = buf; |
| 11930 | inputStream->base = inputStream->buf->buffer->content; |
| 11931 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11932 | inputStream->end = |
| 11933 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11934 | |
| 11935 | inputPush(ctxt, inputStream); |
| 11936 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11937 | /* |
| 11938 | * If the caller didn't provide an initial 'chunk' for determining |
| 11939 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 11940 | * that it can be automatically determined later |
| 11941 | */ |
| 11942 | if ((size == 0) || (chunk == NULL)) { |
| 11943 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 11944 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11945 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11946 | int cur = ctxt->input->cur - ctxt->input->base; |
| 11947 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11948 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11949 | |
| 11950 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11951 | ctxt->input->cur = ctxt->input->base + cur; |
| 11952 | ctxt->input->end = |
| 11953 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11954 | #ifdef DEBUG_PUSH |
| 11955 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11956 | #endif |
| 11957 | } |
| 11958 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11959 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11960 | xmlSwitchEncoding(ctxt, enc); |
| 11961 | } |
| 11962 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11963 | return(ctxt); |
| 11964 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 11965 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11966 | |
| 11967 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 11968 | * xmlStopParser: |
| 11969 | * @ctxt: an XML parser context |
| 11970 | * |
| 11971 | * Blocks further parser processing |
| 11972 | */ |
| 11973 | void |
| 11974 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 11975 | if (ctxt == NULL) |
| 11976 | return; |
| 11977 | ctxt->instate = XML_PARSER_EOF; |
| 11978 | ctxt->disableSAX = 1; |
| 11979 | if (ctxt->input != NULL) { |
| 11980 | ctxt->input->cur = BAD_CAST""; |
| 11981 | ctxt->input->base = ctxt->input->cur; |
| 11982 | } |
| 11983 | } |
| 11984 | |
| 11985 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11986 | * xmlCreateIOParserCtxt: |
| 11987 | * @sax: a SAX handler |
| 11988 | * @user_data: The user data returned on SAX callbacks |
| 11989 | * @ioread: an I/O read function |
| 11990 | * @ioclose: an I/O close function |
| 11991 | * @ioctx: an I/O handler |
| 11992 | * @enc: the charset encoding if known |
| 11993 | * |
| 11994 | * Create a parser context for using the XML parser with an existing |
| 11995 | * I/O stream |
| 11996 | * |
| 11997 | * Returns the new parser context or NULL |
| 11998 | */ |
| 11999 | xmlParserCtxtPtr |
| 12000 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 12001 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 12002 | void *ioctx, xmlCharEncoding enc) { |
| 12003 | xmlParserCtxtPtr ctxt; |
| 12004 | xmlParserInputPtr inputStream; |
| 12005 | xmlParserInputBufferPtr buf; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12006 | |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 12007 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12008 | |
| 12009 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12010 | if (buf == NULL) { |
| 12011 | if (ioclose != NULL) |
| 12012 | ioclose(ioctx); |
| 12013 | return (NULL); |
| 12014 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12015 | |
| 12016 | ctxt = xmlNewParserCtxt(); |
| 12017 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12018 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12019 | return(NULL); |
| 12020 | } |
| 12021 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12022 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 12023 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12024 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12025 | xmlFree(ctxt->sax); |
| 12026 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 12027 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12028 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 12029 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12030 | return(NULL); |
| 12031 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 12032 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 12033 | if (sax->initialized == XML_SAX2_MAGIC) |
| 12034 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 12035 | else |
| 12036 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12037 | if (user_data != NULL) |
| 12038 | ctxt->userData = user_data; |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 12039 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12040 | |
| 12041 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 12042 | if (inputStream == NULL) { |
| 12043 | xmlFreeParserCtxt(ctxt); |
| 12044 | return(NULL); |
| 12045 | } |
| 12046 | inputPush(ctxt, inputStream); |
| 12047 | |
| 12048 | return(ctxt); |
| 12049 | } |
| 12050 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12051 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12052 | /************************************************************************ |
| 12053 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 12054 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12055 | * * |
| 12056 | ************************************************************************/ |
| 12057 | |
| 12058 | /** |
| 12059 | * xmlIOParseDTD: |
| 12060 | * @sax: the SAX handler block or NULL |
| 12061 | * @input: an Input Buffer |
| 12062 | * @enc: the charset encoding if known |
| 12063 | * |
| 12064 | * Load and parse a DTD |
| 12065 | * |
| 12066 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12067 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12068 | */ |
| 12069 | |
| 12070 | xmlDtdPtr |
| 12071 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 12072 | xmlCharEncoding enc) { |
| 12073 | xmlDtdPtr ret = NULL; |
| 12074 | xmlParserCtxtPtr ctxt; |
| 12075 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12076 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12077 | |
| 12078 | if (input == NULL) |
| 12079 | return(NULL); |
| 12080 | |
| 12081 | ctxt = xmlNewParserCtxt(); |
| 12082 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12083 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12084 | return(NULL); |
| 12085 | } |
| 12086 | |
| 12087 | /* |
| 12088 | * Set-up the SAX context |
| 12089 | */ |
| 12090 | if (sax != NULL) { |
| 12091 | if (ctxt->sax != NULL) |
| 12092 | xmlFree(ctxt->sax); |
| 12093 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 12094 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12095 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12096 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12097 | |
| 12098 | /* |
| 12099 | * generate a parser input from the I/O handler |
| 12100 | */ |
| 12101 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12102 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12103 | if (pinput == NULL) { |
| 12104 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 12105 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12106 | xmlFreeParserCtxt(ctxt); |
| 12107 | return(NULL); |
| 12108 | } |
| 12109 | |
| 12110 | /* |
| 12111 | * plug some encoding conversion routines here. |
| 12112 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12113 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 12114 | if (sax != NULL) ctxt->sax = NULL; |
| 12115 | xmlFreeParserCtxt(ctxt); |
| 12116 | return(NULL); |
| 12117 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 12118 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12119 | xmlSwitchEncoding(ctxt, enc); |
| 12120 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12121 | |
| 12122 | pinput->filename = NULL; |
| 12123 | pinput->line = 1; |
| 12124 | pinput->col = 1; |
| 12125 | pinput->base = ctxt->input->cur; |
| 12126 | pinput->cur = ctxt->input->cur; |
| 12127 | pinput->free = NULL; |
| 12128 | |
| 12129 | /* |
| 12130 | * let's parse that entity knowing it's an external subset. |
| 12131 | */ |
| 12132 | ctxt->inSubset = 2; |
| 12133 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12134 | if (ctxt->myDoc == NULL) { |
| 12135 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12136 | return(NULL); |
| 12137 | } |
| 12138 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12139 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12140 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12141 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12142 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 12143 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12144 | /* |
| 12145 | * Get the 4 first bytes and decode the charset |
| 12146 | * if enc != XML_CHAR_ENCODING_NONE |
| 12147 | * plug some encoding conversion routines. |
| 12148 | */ |
| 12149 | start[0] = RAW; |
| 12150 | start[1] = NXT(1); |
| 12151 | start[2] = NXT(2); |
| 12152 | start[3] = NXT(3); |
| 12153 | enc = xmlDetectCharEncoding(start, 4); |
| 12154 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12155 | xmlSwitchEncoding(ctxt, enc); |
| 12156 | } |
| 12157 | } |
| 12158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12159 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 12160 | |
| 12161 | if (ctxt->myDoc != NULL) { |
| 12162 | if (ctxt->wellFormed) { |
| 12163 | ret = ctxt->myDoc->extSubset; |
| 12164 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 12165 | if (ret != NULL) { |
| 12166 | xmlNodePtr tmp; |
| 12167 | |
| 12168 | ret->doc = NULL; |
| 12169 | tmp = ret->children; |
| 12170 | while (tmp != NULL) { |
| 12171 | tmp->doc = NULL; |
| 12172 | tmp = tmp->next; |
| 12173 | } |
| 12174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12175 | } else { |
| 12176 | ret = NULL; |
| 12177 | } |
| 12178 | xmlFreeDoc(ctxt->myDoc); |
| 12179 | ctxt->myDoc = NULL; |
| 12180 | } |
| 12181 | if (sax != NULL) ctxt->sax = NULL; |
| 12182 | xmlFreeParserCtxt(ctxt); |
| 12183 | |
| 12184 | return(ret); |
| 12185 | } |
| 12186 | |
| 12187 | /** |
| 12188 | * xmlSAXParseDTD: |
| 12189 | * @sax: the SAX handler block |
| 12190 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12191 | * @SystemID: a NAME* containing the URL to the DTD |
| 12192 | * |
| 12193 | * Load and parse an external subset. |
| 12194 | * |
| 12195 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12196 | */ |
| 12197 | |
| 12198 | xmlDtdPtr |
| 12199 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 12200 | const xmlChar *SystemID) { |
| 12201 | xmlDtdPtr ret = NULL; |
| 12202 | xmlParserCtxtPtr ctxt; |
| 12203 | xmlParserInputPtr input = NULL; |
| 12204 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12205 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12206 | |
| 12207 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 12208 | |
| 12209 | ctxt = xmlNewParserCtxt(); |
| 12210 | if (ctxt == NULL) { |
| 12211 | return(NULL); |
| 12212 | } |
| 12213 | |
| 12214 | /* |
| 12215 | * Set-up the SAX context |
| 12216 | */ |
| 12217 | if (sax != NULL) { |
| 12218 | if (ctxt->sax != NULL) |
| 12219 | xmlFree(ctxt->sax); |
| 12220 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 12221 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12222 | } |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12223 | |
| 12224 | /* |
| 12225 | * Canonicalise the system ID |
| 12226 | */ |
| 12227 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 12228 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12229 | xmlFreeParserCtxt(ctxt); |
| 12230 | return(NULL); |
| 12231 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12232 | |
| 12233 | /* |
| 12234 | * Ask the Entity resolver to load the damn thing |
| 12235 | */ |
| 12236 | |
| 12237 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 12238 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 12239 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12240 | if (input == NULL) { |
| 12241 | if (sax != NULL) ctxt->sax = NULL; |
| 12242 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12243 | if (systemIdCanonic != NULL) |
| 12244 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12245 | return(NULL); |
| 12246 | } |
| 12247 | |
| 12248 | /* |
| 12249 | * plug some encoding conversion routines here. |
| 12250 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 12251 | if (xmlPushInput(ctxt, input) < 0) { |
| 12252 | if (sax != NULL) ctxt->sax = NULL; |
| 12253 | xmlFreeParserCtxt(ctxt); |
| 12254 | if (systemIdCanonic != NULL) |
| 12255 | xmlFree(systemIdCanonic); |
| 12256 | return(NULL); |
| 12257 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12258 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12259 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 12260 | xmlSwitchEncoding(ctxt, enc); |
| 12261 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12262 | |
| 12263 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 12264 | input->filename = (char *) systemIdCanonic; |
| 12265 | else |
| 12266 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12267 | input->line = 1; |
| 12268 | input->col = 1; |
| 12269 | input->base = ctxt->input->cur; |
| 12270 | input->cur = ctxt->input->cur; |
| 12271 | input->free = NULL; |
| 12272 | |
| 12273 | /* |
| 12274 | * let's parse that entity knowing it's an external subset. |
| 12275 | */ |
| 12276 | ctxt->inSubset = 2; |
| 12277 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12278 | if (ctxt->myDoc == NULL) { |
| 12279 | xmlErrMemory(ctxt, "New Doc failed"); |
| 12280 | if (sax != NULL) ctxt->sax = NULL; |
| 12281 | xmlFreeParserCtxt(ctxt); |
| 12282 | return(NULL); |
| 12283 | } |
| 12284 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12285 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 12286 | ExternalID, SystemID); |
| 12287 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 12288 | |
| 12289 | if (ctxt->myDoc != NULL) { |
| 12290 | if (ctxt->wellFormed) { |
| 12291 | ret = ctxt->myDoc->extSubset; |
| 12292 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 12293 | if (ret != NULL) { |
| 12294 | xmlNodePtr tmp; |
| 12295 | |
| 12296 | ret->doc = NULL; |
| 12297 | tmp = ret->children; |
| 12298 | while (tmp != NULL) { |
| 12299 | tmp->doc = NULL; |
| 12300 | tmp = tmp->next; |
| 12301 | } |
| 12302 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12303 | } else { |
| 12304 | ret = NULL; |
| 12305 | } |
| 12306 | xmlFreeDoc(ctxt->myDoc); |
| 12307 | ctxt->myDoc = NULL; |
| 12308 | } |
| 12309 | if (sax != NULL) ctxt->sax = NULL; |
| 12310 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12311 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12312 | return(ret); |
| 12313 | } |
| 12314 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12315 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12316 | /** |
| 12317 | * xmlParseDTD: |
| 12318 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 12319 | * @SystemID: a NAME* containing the URL to the DTD |
| 12320 | * |
| 12321 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12322 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12323 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 12324 | */ |
| 12325 | |
| 12326 | xmlDtdPtr |
| 12327 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 12328 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 12329 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12330 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12331 | |
| 12332 | /************************************************************************ |
| 12333 | * * |
| 12334 | * Front ends when parsing an Entity * |
| 12335 | * * |
| 12336 | ************************************************************************/ |
| 12337 | |
| 12338 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12339 | * xmlParseCtxtExternalEntity: |
| 12340 | * @ctx: the existing parsing context |
| 12341 | * @URL: the URL for the entity to load |
| 12342 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12343 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12344 | * |
| 12345 | * Parse an external general entity within an existing parsing context |
| 12346 | * An external general parsed entity is well-formed if it matches the |
| 12347 | * production labeled extParsedEnt. |
| 12348 | * |
| 12349 | * [78] extParsedEnt ::= TextDecl? content |
| 12350 | * |
| 12351 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12352 | * the parser error code otherwise |
| 12353 | */ |
| 12354 | |
| 12355 | int |
| 12356 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12357 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12358 | xmlParserCtxtPtr ctxt; |
| 12359 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12360 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12361 | xmlSAXHandlerPtr oldsax = NULL; |
| 12362 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12363 | xmlChar start[4]; |
| 12364 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12365 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12366 | if (ctx == NULL) return(-1); |
| 12367 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12368 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12369 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12370 | return(XML_ERR_ENTITY_LOOP); |
| 12371 | } |
| 12372 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12373 | if (lst != NULL) |
| 12374 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12375 | if ((URL == NULL) && (ID == NULL)) |
| 12376 | return(-1); |
| 12377 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12378 | return(-1); |
| 12379 | |
Rob Richards | 798743a | 2009-06-19 13:54:25 -0400 | [diff] [blame] | 12380 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, ctx); |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12381 | if (ctxt == NULL) { |
| 12382 | return(-1); |
| 12383 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12384 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12385 | oldsax = ctxt->sax; |
| 12386 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12387 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12388 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12389 | if (newDoc == NULL) { |
| 12390 | xmlFreeParserCtxt(ctxt); |
| 12391 | return(-1); |
| 12392 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12393 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12394 | if (ctx->myDoc->dict) { |
| 12395 | newDoc->dict = ctx->myDoc->dict; |
| 12396 | xmlDictReference(newDoc->dict); |
| 12397 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12398 | if (ctx->myDoc != NULL) { |
| 12399 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12400 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12401 | } |
| 12402 | if (ctx->myDoc->URL != NULL) { |
| 12403 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12404 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12405 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12406 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12407 | ctxt->sax = oldsax; |
| 12408 | xmlFreeParserCtxt(ctxt); |
| 12409 | newDoc->intSubset = NULL; |
| 12410 | newDoc->extSubset = NULL; |
| 12411 | xmlFreeDoc(newDoc); |
| 12412 | return(-1); |
| 12413 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12414 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12415 | nodePush(ctxt, newDoc->children); |
| 12416 | if (ctx->myDoc == NULL) { |
| 12417 | ctxt->myDoc = newDoc; |
| 12418 | } else { |
| 12419 | ctxt->myDoc = ctx->myDoc; |
| 12420 | newDoc->children->doc = ctx->myDoc; |
| 12421 | } |
| 12422 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12423 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12424 | * Get the 4 first bytes and decode the charset |
| 12425 | * if enc != XML_CHAR_ENCODING_NONE |
| 12426 | * plug some encoding conversion routines. |
| 12427 | */ |
| 12428 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12429 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12430 | start[0] = RAW; |
| 12431 | start[1] = NXT(1); |
| 12432 | start[2] = NXT(2); |
| 12433 | start[3] = NXT(3); |
| 12434 | enc = xmlDetectCharEncoding(start, 4); |
| 12435 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12436 | xmlSwitchEncoding(ctxt, enc); |
| 12437 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12438 | } |
| 12439 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12440 | /* |
| 12441 | * Parse a possible text declaration first |
| 12442 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12443 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12444 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12445 | /* |
| 12446 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 12447 | */ |
| 12448 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 12449 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
| 12450 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
| 12451 | "Version mismatch between document and entity\n"); |
| 12452 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12453 | } |
| 12454 | |
| 12455 | /* |
Daniel Veillard | 4aa68ab | 2012-04-02 17:50:54 +0800 | [diff] [blame] | 12456 | * If the user provided its own SAX callbacks then reuse the |
| 12457 | * useData callback field, otherwise the expected setup in a |
| 12458 | * DOM builder is to have userData == ctxt |
| 12459 | */ |
| 12460 | if (ctx->userData == ctx) |
| 12461 | ctxt->userData = ctxt; |
| 12462 | else |
| 12463 | ctxt->userData = ctx->userData; |
| 12464 | |
| 12465 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12466 | * Doing validity checking on chunk doesn't make sense |
| 12467 | */ |
| 12468 | ctxt->instate = XML_PARSER_CONTENT; |
| 12469 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12470 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12471 | ctxt->loadsubset = ctx->loadsubset; |
| 12472 | ctxt->depth = ctx->depth + 1; |
| 12473 | ctxt->replaceEntities = ctx->replaceEntities; |
| 12474 | if (ctxt->validate) { |
| 12475 | ctxt->vctxt.error = ctx->vctxt.error; |
| 12476 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12477 | } else { |
| 12478 | ctxt->vctxt.error = NULL; |
| 12479 | ctxt->vctxt.warning = NULL; |
| 12480 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 12481 | ctxt->vctxt.nodeTab = NULL; |
| 12482 | ctxt->vctxt.nodeNr = 0; |
| 12483 | ctxt->vctxt.nodeMax = 0; |
| 12484 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12485 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12486 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12487 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12488 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12489 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12490 | ctxt->dictNames = ctx->dictNames; |
| 12491 | ctxt->attsDefault = ctx->attsDefault; |
| 12492 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 12493 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12494 | |
| 12495 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12496 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12497 | ctx->validate = ctxt->validate; |
| 12498 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12499 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12500 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12501 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12502 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12503 | } |
| 12504 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12505 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12506 | } |
| 12507 | |
| 12508 | if (!ctxt->wellFormed) { |
| 12509 | if (ctxt->errNo == 0) |
| 12510 | ret = 1; |
| 12511 | else |
| 12512 | ret = ctxt->errNo; |
| 12513 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12514 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12515 | xmlNodePtr cur; |
| 12516 | |
| 12517 | /* |
| 12518 | * Return the newly created nodeset after unlinking it from |
| 12519 | * they pseudo parent. |
| 12520 | */ |
| 12521 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12522 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12523 | while (cur != NULL) { |
| 12524 | cur->parent = NULL; |
| 12525 | cur = cur->next; |
| 12526 | } |
| 12527 | newDoc->children->children = NULL; |
| 12528 | } |
| 12529 | ret = 0; |
| 12530 | } |
| 12531 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12532 | ctxt->dict = NULL; |
| 12533 | ctxt->attsDefault = NULL; |
| 12534 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12535 | xmlFreeParserCtxt(ctxt); |
| 12536 | newDoc->intSubset = NULL; |
| 12537 | newDoc->extSubset = NULL; |
| 12538 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12539 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12540 | return(ret); |
| 12541 | } |
| 12542 | |
| 12543 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12544 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12545 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12546 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12547 | * @sax: the SAX handler bloc (possibly NULL) |
| 12548 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12549 | * @depth: Used for loop detection, use 0 |
| 12550 | * @URL: the URL for the entity to load |
| 12551 | * @ID: the System ID for the entity to load |
| 12552 | * @list: the return value for the set of parsed nodes |
| 12553 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12554 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12555 | * |
| 12556 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12557 | * the parser error code otherwise |
| 12558 | */ |
| 12559 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12560 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12561 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 12562 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12563 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12564 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12565 | xmlParserCtxtPtr ctxt; |
| 12566 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12567 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12568 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12569 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12570 | xmlChar start[4]; |
| 12571 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12572 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12573 | if (((depth > 40) && |
| 12574 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12575 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12576 | return(XML_ERR_ENTITY_LOOP); |
| 12577 | } |
| 12578 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12579 | if (list != NULL) |
| 12580 | *list = NULL; |
| 12581 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12582 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12583 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12584 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12585 | |
| 12586 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 12587 | ctxt = xmlCreateEntityParserCtxtInternal(URL, ID, NULL, oldctxt); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 12588 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12589 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12590 | if (oldctxt != NULL) { |
| 12591 | ctxt->_private = oldctxt->_private; |
| 12592 | ctxt->loadsubset = oldctxt->loadsubset; |
| 12593 | ctxt->validate = oldctxt->validate; |
| 12594 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12595 | ctxt->record_info = oldctxt->record_info; |
| 12596 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 12597 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 12598 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12599 | } else { |
| 12600 | /* |
| 12601 | * Doing validity checking on chunk without context |
| 12602 | * doesn't make sense |
| 12603 | */ |
| 12604 | ctxt->_private = NULL; |
| 12605 | ctxt->validate = 0; |
| 12606 | ctxt->external = 2; |
| 12607 | ctxt->loadsubset = 0; |
| 12608 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12609 | if (sax != NULL) { |
| 12610 | oldsax = ctxt->sax; |
| 12611 | ctxt->sax = sax; |
| 12612 | if (user_data != NULL) |
| 12613 | ctxt->userData = user_data; |
| 12614 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12615 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12616 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12617 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12618 | ctxt->node_seq.maximum = 0; |
| 12619 | ctxt->node_seq.length = 0; |
| 12620 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12621 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12622 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12623 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12624 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12625 | newDoc->intSubset = doc->intSubset; |
| 12626 | newDoc->extSubset = doc->extSubset; |
| 12627 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12628 | xmlDictReference(newDoc->dict); |
| 12629 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12630 | if (doc->URL != NULL) { |
| 12631 | newDoc->URL = xmlStrdup(doc->URL); |
| 12632 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12633 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12634 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12635 | if (sax != NULL) |
| 12636 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12637 | ctxt->node_seq.maximum = 0; |
| 12638 | ctxt->node_seq.length = 0; |
| 12639 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12640 | xmlFreeParserCtxt(ctxt); |
| 12641 | newDoc->intSubset = NULL; |
| 12642 | newDoc->extSubset = NULL; |
| 12643 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12644 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12645 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12646 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12647 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12648 | ctxt->myDoc = doc; |
| 12649 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12650 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12651 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12652 | * Get the 4 first bytes and decode the charset |
| 12653 | * if enc != XML_CHAR_ENCODING_NONE |
| 12654 | * plug some encoding conversion routines. |
| 12655 | */ |
| 12656 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12657 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12658 | start[0] = RAW; |
| 12659 | start[1] = NXT(1); |
| 12660 | start[2] = NXT(2); |
| 12661 | start[3] = NXT(3); |
| 12662 | enc = xmlDetectCharEncoding(start, 4); |
| 12663 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12664 | xmlSwitchEncoding(ctxt, enc); |
| 12665 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12666 | } |
| 12667 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12668 | /* |
| 12669 | * Parse a possible text declaration first |
| 12670 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12671 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12672 | xmlParseTextDecl(ctxt); |
| 12673 | } |
| 12674 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12675 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12676 | ctxt->depth = depth; |
| 12677 | |
| 12678 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12679 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12680 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12681 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12682 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12683 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12684 | } |
| 12685 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12686 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12687 | } |
| 12688 | |
| 12689 | if (!ctxt->wellFormed) { |
| 12690 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12691 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12692 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12693 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12694 | } else { |
| 12695 | if (list != NULL) { |
| 12696 | xmlNodePtr cur; |
| 12697 | |
| 12698 | /* |
| 12699 | * Return the newly created nodeset after unlinking it from |
| 12700 | * they pseudo parent. |
| 12701 | */ |
| 12702 | cur = newDoc->children->children; |
| 12703 | *list = cur; |
| 12704 | while (cur != NULL) { |
| 12705 | cur->parent = NULL; |
| 12706 | cur = cur->next; |
| 12707 | } |
| 12708 | newDoc->children->children = NULL; |
| 12709 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12710 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12711 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12712 | |
| 12713 | /* |
| 12714 | * Record in the parent context the number of entities replacement |
| 12715 | * done when parsing that reference. |
| 12716 | */ |
Daniel Veillard | 76d3645 | 2009-09-07 11:19:33 +0200 | [diff] [blame] | 12717 | if (oldctxt != NULL) |
| 12718 | oldctxt->nbentities += ctxt->nbentities; |
| 12719 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12720 | /* |
| 12721 | * Also record the size of the entity parsed |
| 12722 | */ |
| 12723 | if (ctxt->input != NULL) { |
| 12724 | oldctxt->sizeentities += ctxt->input->consumed; |
| 12725 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 12726 | } |
| 12727 | /* |
| 12728 | * And record the last error if any |
| 12729 | */ |
| 12730 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12731 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12732 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12733 | if (sax != NULL) |
| 12734 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 12735 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 12736 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 12737 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12738 | ctxt->node_seq.maximum = 0; |
| 12739 | ctxt->node_seq.length = 0; |
| 12740 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12741 | xmlFreeParserCtxt(ctxt); |
| 12742 | newDoc->intSubset = NULL; |
| 12743 | newDoc->extSubset = NULL; |
| 12744 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12745 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12746 | return(ret); |
| 12747 | } |
| 12748 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12749 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12750 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12751 | * xmlParseExternalEntity: |
| 12752 | * @doc: the document the chunk pertains to |
| 12753 | * @sax: the SAX handler bloc (possibly NULL) |
| 12754 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12755 | * @depth: Used for loop detection, use 0 |
| 12756 | * @URL: the URL for the entity to load |
| 12757 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12758 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12759 | * |
| 12760 | * Parse an external general entity |
| 12761 | * An external general parsed entity is well-formed if it matches the |
| 12762 | * production labeled extParsedEnt. |
| 12763 | * |
| 12764 | * [78] extParsedEnt ::= TextDecl? content |
| 12765 | * |
| 12766 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12767 | * the parser error code otherwise |
| 12768 | */ |
| 12769 | |
| 12770 | int |
| 12771 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12772 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12773 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12774 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12775 | } |
| 12776 | |
| 12777 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 12778 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12779 | * @doc: the document the chunk pertains to |
| 12780 | * @sax: the SAX handler bloc (possibly NULL) |
| 12781 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12782 | * @depth: Used for loop detection, use 0 |
| 12783 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12784 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12785 | * |
| 12786 | * Parse a well-balanced chunk of an XML document |
| 12787 | * called by the parser |
| 12788 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12789 | * the content production in the XML grammar: |
| 12790 | * |
| 12791 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12792 | * |
| 12793 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 12794 | * the parser error code otherwise |
| 12795 | */ |
| 12796 | |
| 12797 | int |
| 12798 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12799 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12800 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 12801 | depth, string, lst, 0 ); |
| 12802 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12803 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12804 | |
| 12805 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12806 | * xmlParseBalancedChunkMemoryInternal: |
| 12807 | * @oldctxt: the existing parsing context |
| 12808 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 12809 | * @user_data: the user data field for the parser context |
| 12810 | * @lst: the return value for the set of parsed nodes |
| 12811 | * |
| 12812 | * |
| 12813 | * Parse a well-balanced chunk of an XML document |
| 12814 | * called by the parser |
| 12815 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12816 | * the content production in the XML grammar: |
| 12817 | * |
| 12818 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12819 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12820 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 12821 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12822 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12823 | * 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] | 12824 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12825 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12826 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12827 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 12828 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 12829 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12830 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12831 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12832 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12833 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12834 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12835 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12836 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 12837 | #ifdef SAX2 |
| 12838 | int i; |
| 12839 | #endif |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12840 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12841 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12842 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12843 | return(XML_ERR_ENTITY_LOOP); |
| 12844 | } |
| 12845 | |
| 12846 | |
| 12847 | if (lst != NULL) |
| 12848 | *lst = NULL; |
| 12849 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12850 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12851 | |
| 12852 | size = xmlStrlen(string); |
| 12853 | |
| 12854 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12855 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12856 | if (user_data != NULL) |
| 12857 | ctxt->userData = user_data; |
| 12858 | else |
| 12859 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12860 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12861 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12862 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12863 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12864 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12865 | |
Daniel Veillard | 74eaec1 | 2009-08-26 15:57:20 +0200 | [diff] [blame] | 12866 | #ifdef SAX2 |
| 12867 | /* propagate namespaces down the entity */ |
| 12868 | for (i = 0;i < oldctxt->nsNr;i += 2) { |
| 12869 | nsPush(ctxt, oldctxt->nsTab[i], oldctxt->nsTab[i+1]); |
| 12870 | } |
| 12871 | #endif |
| 12872 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12873 | oldsax = ctxt->sax; |
| 12874 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12875 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12876 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 12877 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12878 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 12879 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12880 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12881 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12882 | if (newDoc == NULL) { |
| 12883 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12884 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12885 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12886 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12887 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12888 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12889 | newDoc->dict = ctxt->dict; |
| 12890 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12891 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12892 | } else { |
| 12893 | ctxt->myDoc = oldctxt->myDoc; |
| 12894 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12895 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12896 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12897 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12898 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12899 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12900 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12901 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12902 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12903 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12904 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12905 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12906 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12907 | ctxt->myDoc->children = NULL; |
| 12908 | ctxt->myDoc->last = NULL; |
| 12909 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12910 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12911 | ctxt->instate = XML_PARSER_CONTENT; |
| 12912 | ctxt->depth = oldctxt->depth + 1; |
| 12913 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12914 | ctxt->validate = 0; |
| 12915 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 12916 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 12917 | /* |
| 12918 | * ID/IDREF registration will be done in xmlValidateElement below |
| 12919 | */ |
| 12920 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 12921 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12922 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12923 | ctxt->attsDefault = oldctxt->attsDefault; |
| 12924 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12925 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12926 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12927 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12928 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12929 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12930 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12931 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12932 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12933 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12934 | } |
| 12935 | |
| 12936 | if (!ctxt->wellFormed) { |
| 12937 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12938 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12939 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12940 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12941 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12942 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12943 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12944 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12945 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12946 | xmlNodePtr cur; |
| 12947 | |
| 12948 | /* |
| 12949 | * Return the newly created nodeset after unlinking it from |
| 12950 | * they pseudo parent. |
| 12951 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12952 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12953 | *lst = cur; |
| 12954 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12955 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 12956 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 12957 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 12958 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 12959 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 12960 | oldctxt->myDoc, cur); |
| 12961 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12962 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12963 | cur->parent = NULL; |
| 12964 | cur = cur->next; |
| 12965 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12966 | ctxt->myDoc->children->children = NULL; |
| 12967 | } |
| 12968 | if (ctxt->myDoc != NULL) { |
| 12969 | xmlFreeNode(ctxt->myDoc->children); |
| 12970 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12971 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12972 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12973 | |
| 12974 | /* |
| 12975 | * Record in the parent context the number of entities replacement |
| 12976 | * done when parsing that reference. |
| 12977 | */ |
Daniel Veillard | d44b936 | 2009-09-07 12:15:08 +0200 | [diff] [blame] | 12978 | if (oldctxt != NULL) |
| 12979 | oldctxt->nbentities += ctxt->nbentities; |
| 12980 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12981 | /* |
| 12982 | * Also record the last error if any |
| 12983 | */ |
| 12984 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12985 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12986 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12987 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12988 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12989 | ctxt->attsDefault = NULL; |
| 12990 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12991 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12992 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12993 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12994 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12995 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12996 | return(ret); |
| 12997 | } |
| 12998 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12999 | /** |
| 13000 | * xmlParseInNodeContext: |
| 13001 | * @node: the context node |
| 13002 | * @data: the input string |
| 13003 | * @datalen: the input string length in bytes |
| 13004 | * @options: a combination of xmlParserOption |
| 13005 | * @lst: the return value for the set of parsed nodes |
| 13006 | * |
| 13007 | * Parse a well-balanced chunk of an XML document |
| 13008 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 13009 | * |
| 13010 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 13011 | * the content production in the XML grammar: |
| 13012 | * |
| 13013 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13014 | * |
| 13015 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 13016 | * error code otherwise |
| 13017 | */ |
| 13018 | xmlParserErrors |
| 13019 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 13020 | int options, xmlNodePtr *lst) { |
| 13021 | #ifdef SAX2 |
| 13022 | xmlParserCtxtPtr ctxt; |
| 13023 | xmlDocPtr doc = NULL; |
| 13024 | xmlNodePtr fake, cur; |
| 13025 | int nsnr = 0; |
| 13026 | |
| 13027 | xmlParserErrors ret = XML_ERR_OK; |
| 13028 | |
| 13029 | /* |
| 13030 | * check all input parameters, grab the document |
| 13031 | */ |
| 13032 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 13033 | return(XML_ERR_INTERNAL_ERROR); |
| 13034 | switch (node->type) { |
| 13035 | case XML_ELEMENT_NODE: |
| 13036 | case XML_ATTRIBUTE_NODE: |
| 13037 | case XML_TEXT_NODE: |
| 13038 | case XML_CDATA_SECTION_NODE: |
| 13039 | case XML_ENTITY_REF_NODE: |
| 13040 | case XML_PI_NODE: |
| 13041 | case XML_COMMENT_NODE: |
| 13042 | case XML_DOCUMENT_NODE: |
| 13043 | case XML_HTML_DOCUMENT_NODE: |
| 13044 | break; |
| 13045 | default: |
| 13046 | return(XML_ERR_INTERNAL_ERROR); |
| 13047 | |
| 13048 | } |
| 13049 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 13050 | (node->type != XML_DOCUMENT_NODE) && |
| 13051 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 13052 | node = node->parent; |
| 13053 | if (node == NULL) |
| 13054 | return(XML_ERR_INTERNAL_ERROR); |
| 13055 | if (node->type == XML_ELEMENT_NODE) |
| 13056 | doc = node->doc; |
| 13057 | else |
| 13058 | doc = (xmlDocPtr) node; |
| 13059 | if (doc == NULL) |
| 13060 | return(XML_ERR_INTERNAL_ERROR); |
| 13061 | |
| 13062 | /* |
| 13063 | * allocate a context and set-up everything not related to the |
| 13064 | * node position in the tree |
| 13065 | */ |
| 13066 | if (doc->type == XML_DOCUMENT_NODE) |
| 13067 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 13068 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13069 | else if (doc->type == XML_HTML_DOCUMENT_NODE) { |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13070 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
Daniel Veillard | e20fb5a | 2010-01-29 20:47:08 +0100 | [diff] [blame] | 13071 | /* |
| 13072 | * When parsing in context, it makes no sense to add implied |
| 13073 | * elements like html/body/etc... |
| 13074 | */ |
| 13075 | options |= HTML_PARSE_NOIMPLIED; |
| 13076 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13077 | #endif |
| 13078 | else |
| 13079 | return(XML_ERR_INTERNAL_ERROR); |
| 13080 | |
| 13081 | if (ctxt == NULL) |
| 13082 | return(XML_ERR_NO_MEMORY); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13083 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13084 | /* |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13085 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 13086 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 13087 | * we must wait until the last moment to free the original one. |
| 13088 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13089 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13090 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13091 | xmlDictFree(ctxt->dict); |
| 13092 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13093 | } else |
| 13094 | options |= XML_PARSE_NODICT; |
| 13095 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13096 | if (doc->encoding != NULL) { |
| 13097 | xmlCharEncodingHandlerPtr hdlr; |
| 13098 | |
| 13099 | if (ctxt->encoding != NULL) |
| 13100 | xmlFree((xmlChar *) ctxt->encoding); |
| 13101 | ctxt->encoding = xmlStrdup((const xmlChar *) doc->encoding); |
| 13102 | |
| 13103 | hdlr = xmlFindCharEncodingHandler(doc->encoding); |
| 13104 | if (hdlr != NULL) { |
| 13105 | xmlSwitchToEncoding(ctxt, hdlr); |
| 13106 | } else { |
| 13107 | return(XML_ERR_UNSUPPORTED_ENCODING); |
| 13108 | } |
| 13109 | } |
| 13110 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13111 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13112 | xmlDetectSAX2(ctxt); |
| 13113 | ctxt->myDoc = doc; |
| 13114 | |
Daniel Veillard | 47cd14e | 2010-02-04 18:49:01 +0100 | [diff] [blame] | 13115 | fake = xmlNewComment(NULL); |
| 13116 | if (fake == NULL) { |
| 13117 | xmlFreeParserCtxt(ctxt); |
| 13118 | return(XML_ERR_NO_MEMORY); |
| 13119 | } |
| 13120 | xmlAddChild(node, fake); |
| 13121 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13122 | if (node->type == XML_ELEMENT_NODE) { |
| 13123 | nodePush(ctxt, node); |
| 13124 | /* |
| 13125 | * initialize the SAX2 namespaces stack |
| 13126 | */ |
| 13127 | cur = node; |
| 13128 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 13129 | xmlNsPtr ns = cur->nsDef; |
| 13130 | const xmlChar *iprefix, *ihref; |
| 13131 | |
| 13132 | while (ns != NULL) { |
| 13133 | if (ctxt->dict) { |
| 13134 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 13135 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 13136 | } else { |
| 13137 | iprefix = ns->prefix; |
| 13138 | ihref = ns->href; |
| 13139 | } |
| 13140 | |
| 13141 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 13142 | nsPush(ctxt, iprefix, ihref); |
| 13143 | nsnr++; |
| 13144 | } |
| 13145 | ns = ns->next; |
| 13146 | } |
| 13147 | cur = cur->parent; |
| 13148 | } |
| 13149 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13150 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13151 | |
| 13152 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 13153 | /* |
| 13154 | * ID/IDREF registration will be done in xmlValidateElement below |
| 13155 | */ |
| 13156 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 13157 | } |
| 13158 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 13159 | #ifdef LIBXML_HTML_ENABLED |
| 13160 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 13161 | __htmlParseContent(ctxt); |
| 13162 | else |
| 13163 | #endif |
| 13164 | xmlParseContent(ctxt); |
| 13165 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13166 | nsPop(ctxt, nsnr); |
| 13167 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 13168 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13169 | } else if (RAW != 0) { |
| 13170 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 13171 | } |
| 13172 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 13173 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 13174 | ctxt->wellFormed = 0; |
| 13175 | } |
| 13176 | |
| 13177 | if (!ctxt->wellFormed) { |
| 13178 | if (ctxt->errNo == 0) |
| 13179 | ret = XML_ERR_INTERNAL_ERROR; |
| 13180 | else |
| 13181 | ret = (xmlParserErrors)ctxt->errNo; |
| 13182 | } else { |
| 13183 | ret = XML_ERR_OK; |
| 13184 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13185 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13186 | /* |
| 13187 | * Return the newly created nodeset after unlinking it from |
| 13188 | * the pseudo sibling. |
| 13189 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13190 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13191 | cur = fake->next; |
| 13192 | fake->next = NULL; |
| 13193 | node->last = fake; |
| 13194 | |
| 13195 | if (cur != NULL) { |
| 13196 | cur->prev = NULL; |
| 13197 | } |
| 13198 | |
| 13199 | *lst = cur; |
| 13200 | |
| 13201 | while (cur != NULL) { |
| 13202 | cur->parent = NULL; |
| 13203 | cur = cur->next; |
| 13204 | } |
| 13205 | |
| 13206 | xmlUnlinkNode(fake); |
| 13207 | xmlFreeNode(fake); |
| 13208 | |
| 13209 | |
| 13210 | if (ret != XML_ERR_OK) { |
| 13211 | xmlFreeNodeList(*lst); |
| 13212 | *lst = NULL; |
| 13213 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 13214 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 13215 | if (doc->dict != NULL) |
| 13216 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13217 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13218 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 13219 | return(ret); |
| 13220 | #else /* !SAX2 */ |
| 13221 | return(XML_ERR_INTERNAL_ERROR); |
| 13222 | #endif |
| 13223 | } |
| 13224 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13225 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 13226 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13227 | * xmlParseBalancedChunkMemoryRecover: |
| 13228 | * @doc: the document the chunk pertains to |
| 13229 | * @sax: the SAX handler bloc (possibly NULL) |
| 13230 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 13231 | * @depth: Used for loop detection, use 0 |
| 13232 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 13233 | * @lst: the return value for the set of parsed nodes |
| 13234 | * @recover: return nodes even if the data is broken (use 0) |
| 13235 | * |
| 13236 | * |
| 13237 | * Parse a well-balanced chunk of an XML document |
| 13238 | * called by the parser |
| 13239 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 13240 | * the content production in the XML grammar: |
| 13241 | * |
| 13242 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 13243 | * |
| 13244 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 13245 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13246 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13247 | * 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] | 13248 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 13249 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13250 | */ |
| 13251 | int |
| 13252 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13253 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13254 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13255 | xmlParserCtxtPtr ctxt; |
| 13256 | xmlDocPtr newDoc; |
| 13257 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13258 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13259 | int size; |
| 13260 | int ret = 0; |
| 13261 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13262 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13263 | return(XML_ERR_ENTITY_LOOP); |
| 13264 | } |
| 13265 | |
| 13266 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 13267 | if (lst != NULL) |
| 13268 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13269 | if (string == NULL) |
| 13270 | return(-1); |
| 13271 | |
| 13272 | size = xmlStrlen(string); |
| 13273 | |
| 13274 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 13275 | if (ctxt == NULL) return(-1); |
| 13276 | ctxt->userData = ctxt; |
| 13277 | if (sax != NULL) { |
| 13278 | oldsax = ctxt->sax; |
| 13279 | ctxt->sax = sax; |
| 13280 | if (user_data != NULL) |
| 13281 | ctxt->userData = user_data; |
| 13282 | } |
| 13283 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 13284 | if (newDoc == NULL) { |
| 13285 | xmlFreeParserCtxt(ctxt); |
| 13286 | return(-1); |
| 13287 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 13288 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13289 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 13290 | xmlDictFree(ctxt->dict); |
| 13291 | ctxt->dict = doc->dict; |
| 13292 | xmlDictReference(ctxt->dict); |
| 13293 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 13294 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 13295 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 13296 | ctxt->dictNames = 1; |
| 13297 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13298 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13299 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13300 | if (doc != NULL) { |
| 13301 | newDoc->intSubset = doc->intSubset; |
| 13302 | newDoc->extSubset = doc->extSubset; |
| 13303 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13304 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 13305 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13306 | if (sax != NULL) |
| 13307 | ctxt->sax = oldsax; |
| 13308 | xmlFreeParserCtxt(ctxt); |
| 13309 | newDoc->intSubset = NULL; |
| 13310 | newDoc->extSubset = NULL; |
| 13311 | xmlFreeDoc(newDoc); |
| 13312 | return(-1); |
| 13313 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13314 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 13315 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13316 | if (doc == NULL) { |
| 13317 | ctxt->myDoc = newDoc; |
| 13318 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 13319 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13320 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13321 | /* Ensure that doc has XML spec namespace */ |
| 13322 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 13323 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13324 | } |
| 13325 | ctxt->instate = XML_PARSER_CONTENT; |
| 13326 | ctxt->depth = depth; |
| 13327 | |
| 13328 | /* |
| 13329 | * Doing validity checking on chunk doesn't make sense |
| 13330 | */ |
| 13331 | ctxt->validate = 0; |
| 13332 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13333 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13334 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 13335 | if ( doc != NULL ){ |
| 13336 | content = doc->children; |
| 13337 | doc->children = NULL; |
| 13338 | xmlParseContent(ctxt); |
| 13339 | doc->children = content; |
| 13340 | } |
| 13341 | else { |
| 13342 | xmlParseContent(ctxt); |
| 13343 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13344 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13345 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13346 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13347 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13348 | } |
| 13349 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 13350 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13351 | } |
| 13352 | |
| 13353 | if (!ctxt->wellFormed) { |
| 13354 | if (ctxt->errNo == 0) |
| 13355 | ret = 1; |
| 13356 | else |
| 13357 | ret = ctxt->errNo; |
| 13358 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13359 | ret = 0; |
| 13360 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13361 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13362 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 13363 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13364 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13365 | /* |
| 13366 | * Return the newly created nodeset after unlinking it from |
| 13367 | * they pseudo parent. |
| 13368 | */ |
| 13369 | cur = newDoc->children->children; |
| 13370 | *lst = cur; |
| 13371 | while (cur != NULL) { |
| 13372 | xmlSetTreeDoc(cur, doc); |
| 13373 | cur->parent = NULL; |
| 13374 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13375 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13376 | newDoc->children->children = NULL; |
| 13377 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13378 | |
| 13379 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13380 | ctxt->sax = oldsax; |
| 13381 | xmlFreeParserCtxt(ctxt); |
| 13382 | newDoc->intSubset = NULL; |
| 13383 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13384 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13385 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13386 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13387 | return(ret); |
| 13388 | } |
| 13389 | |
| 13390 | /** |
| 13391 | * xmlSAXParseEntity: |
| 13392 | * @sax: the SAX handler block |
| 13393 | * @filename: the filename |
| 13394 | * |
| 13395 | * parse an XML external entity out of context and build a tree. |
| 13396 | * It use the given SAX function block to handle the parsing callback. |
| 13397 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13398 | * |
| 13399 | * [78] extParsedEnt ::= TextDecl? content |
| 13400 | * |
| 13401 | * This correspond to a "Well Balanced" chunk |
| 13402 | * |
| 13403 | * Returns the resulting document tree |
| 13404 | */ |
| 13405 | |
| 13406 | xmlDocPtr |
| 13407 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13408 | xmlDocPtr ret; |
| 13409 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13410 | |
| 13411 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13412 | if (ctxt == NULL) { |
| 13413 | return(NULL); |
| 13414 | } |
| 13415 | if (sax != NULL) { |
| 13416 | if (ctxt->sax != NULL) |
| 13417 | xmlFree(ctxt->sax); |
| 13418 | ctxt->sax = sax; |
| 13419 | ctxt->userData = NULL; |
| 13420 | } |
| 13421 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13422 | xmlParseExtParsedEnt(ctxt); |
| 13423 | |
| 13424 | if (ctxt->wellFormed) |
| 13425 | ret = ctxt->myDoc; |
| 13426 | else { |
| 13427 | ret = NULL; |
| 13428 | xmlFreeDoc(ctxt->myDoc); |
| 13429 | ctxt->myDoc = NULL; |
| 13430 | } |
| 13431 | if (sax != NULL) |
| 13432 | ctxt->sax = NULL; |
| 13433 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13434 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13435 | return(ret); |
| 13436 | } |
| 13437 | |
| 13438 | /** |
| 13439 | * xmlParseEntity: |
| 13440 | * @filename: the filename |
| 13441 | * |
| 13442 | * parse an XML external entity out of context and build a tree. |
| 13443 | * |
| 13444 | * [78] extParsedEnt ::= TextDecl? content |
| 13445 | * |
| 13446 | * This correspond to a "Well Balanced" chunk |
| 13447 | * |
| 13448 | * Returns the resulting document tree |
| 13449 | */ |
| 13450 | |
| 13451 | xmlDocPtr |
| 13452 | xmlParseEntity(const char *filename) { |
| 13453 | return(xmlSAXParseEntity(NULL, filename)); |
| 13454 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13455 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13456 | |
| 13457 | /** |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13458 | * xmlCreateEntityParserCtxtInternal: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13459 | * @URL: the entity URL |
| 13460 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13461 | * @base: a possible base for the target URI |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13462 | * @pctx: parser context used to set options on new context |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13463 | * |
| 13464 | * Create a parser context for an external entity |
| 13465 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13466 | * by default if found at compile-time. |
| 13467 | * |
| 13468 | * Returns the new parser context or NULL |
| 13469 | */ |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13470 | static xmlParserCtxtPtr |
| 13471 | xmlCreateEntityParserCtxtInternal(const xmlChar *URL, const xmlChar *ID, |
| 13472 | const xmlChar *base, xmlParserCtxtPtr pctx) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13473 | xmlParserCtxtPtr ctxt; |
| 13474 | xmlParserInputPtr inputStream; |
| 13475 | char *directory = NULL; |
| 13476 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13477 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13478 | ctxt = xmlNewParserCtxt(); |
| 13479 | if (ctxt == NULL) { |
| 13480 | return(NULL); |
| 13481 | } |
| 13482 | |
Daniel Veillard | 48247b4 | 2009-07-10 16:12:46 +0200 | [diff] [blame] | 13483 | if (pctx != NULL) { |
| 13484 | ctxt->options = pctx->options; |
| 13485 | ctxt->_private = pctx->_private; |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13486 | } |
| 13487 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13488 | uri = xmlBuildURI(URL, base); |
| 13489 | |
| 13490 | if (uri == NULL) { |
| 13491 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 13492 | if (inputStream == NULL) { |
| 13493 | xmlFreeParserCtxt(ctxt); |
| 13494 | return(NULL); |
| 13495 | } |
| 13496 | |
| 13497 | inputPush(ctxt, inputStream); |
| 13498 | |
| 13499 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13500 | directory = xmlParserGetDirectory((char *)URL); |
| 13501 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13502 | ctxt->directory = directory; |
| 13503 | } else { |
| 13504 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 13505 | if (inputStream == NULL) { |
| 13506 | xmlFree(uri); |
| 13507 | xmlFreeParserCtxt(ctxt); |
| 13508 | return(NULL); |
| 13509 | } |
| 13510 | |
| 13511 | inputPush(ctxt, inputStream); |
| 13512 | |
| 13513 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13514 | directory = xmlParserGetDirectory((char *)uri); |
| 13515 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13516 | ctxt->directory = directory; |
| 13517 | xmlFree(uri); |
| 13518 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13519 | return(ctxt); |
| 13520 | } |
| 13521 | |
Rob Richards | 9c0aa47 | 2009-03-26 18:10:19 +0000 | [diff] [blame] | 13522 | /** |
| 13523 | * xmlCreateEntityParserCtxt: |
| 13524 | * @URL: the entity URL |
| 13525 | * @ID: the entity PUBLIC ID |
| 13526 | * @base: a possible base for the target URI |
| 13527 | * |
| 13528 | * Create a parser context for an external entity |
| 13529 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13530 | * by default if found at compile-time. |
| 13531 | * |
| 13532 | * Returns the new parser context or NULL |
| 13533 | */ |
| 13534 | xmlParserCtxtPtr |
| 13535 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 13536 | const xmlChar *base) { |
| 13537 | return xmlCreateEntityParserCtxtInternal(URL, ID, base, NULL); |
| 13538 | |
| 13539 | } |
| 13540 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13541 | /************************************************************************ |
| 13542 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13543 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13544 | * * |
| 13545 | ************************************************************************/ |
| 13546 | |
| 13547 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13548 | * xmlCreateURLParserCtxt: |
| 13549 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13550 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13551 | * |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13552 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13553 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13554 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13555 | * |
| 13556 | * Returns the new parser context or NULL |
| 13557 | */ |
| 13558 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13559 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13560 | { |
| 13561 | xmlParserCtxtPtr ctxt; |
| 13562 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13563 | char *directory = NULL; |
| 13564 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13565 | ctxt = xmlNewParserCtxt(); |
| 13566 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13567 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13568 | return(NULL); |
| 13569 | } |
| 13570 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13571 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13572 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13573 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13574 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 13575 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13576 | if (inputStream == NULL) { |
| 13577 | xmlFreeParserCtxt(ctxt); |
| 13578 | return(NULL); |
| 13579 | } |
| 13580 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13581 | inputPush(ctxt, inputStream); |
| 13582 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 13583 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13584 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13585 | ctxt->directory = directory; |
| 13586 | |
| 13587 | return(ctxt); |
| 13588 | } |
| 13589 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13590 | /** |
| 13591 | * xmlCreateFileParserCtxt: |
| 13592 | * @filename: the filename |
| 13593 | * |
| 13594 | * Create a parser context for a file content. |
| 13595 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13596 | * by default if found at compile-time. |
| 13597 | * |
| 13598 | * Returns the new parser context or NULL |
| 13599 | */ |
| 13600 | xmlParserCtxtPtr |
| 13601 | xmlCreateFileParserCtxt(const char *filename) |
| 13602 | { |
| 13603 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 13604 | } |
| 13605 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13606 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13607 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13608 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13609 | * @sax: the SAX handler block |
| 13610 | * @filename: the filename |
| 13611 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13612 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13613 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13614 | * |
| 13615 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13616 | * compressed document is provided by default if found at compile-time. |
| 13617 | * It use the given SAX function block to handle the parsing callback. |
| 13618 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13619 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 13620 | * User data (void *) is stored within the parser context in the |
| 13621 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13622 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13623 | * Returns the resulting document tree |
| 13624 | */ |
| 13625 | |
| 13626 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13627 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 13628 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13629 | xmlDocPtr ret; |
| 13630 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13631 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 13632 | xmlInitParser(); |
| 13633 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13634 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13635 | if (ctxt == NULL) { |
| 13636 | return(NULL); |
| 13637 | } |
| 13638 | if (sax != NULL) { |
| 13639 | if (ctxt->sax != NULL) |
| 13640 | xmlFree(ctxt->sax); |
| 13641 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13642 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13643 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13644 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 13645 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13646 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13647 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 13648 | if (ctxt->directory == NULL) |
| 13649 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13650 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 13651 | ctxt->recovery = recovery; |
| 13652 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13653 | xmlParseDocument(ctxt); |
| 13654 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13655 | if ((ctxt->wellFormed) || recovery) { |
| 13656 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 13657 | if (ret != NULL) { |
| 13658 | if (ctxt->input->buf->compressed > 0) |
| 13659 | ret->compression = 9; |
| 13660 | else |
| 13661 | ret->compression = ctxt->input->buf->compressed; |
| 13662 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13663 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13664 | else { |
| 13665 | ret = NULL; |
| 13666 | xmlFreeDoc(ctxt->myDoc); |
| 13667 | ctxt->myDoc = NULL; |
| 13668 | } |
| 13669 | if (sax != NULL) |
| 13670 | ctxt->sax = NULL; |
| 13671 | xmlFreeParserCtxt(ctxt); |
| 13672 | |
| 13673 | return(ret); |
| 13674 | } |
| 13675 | |
| 13676 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13677 | * xmlSAXParseFile: |
| 13678 | * @sax: the SAX handler block |
| 13679 | * @filename: the filename |
| 13680 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13681 | * documents |
| 13682 | * |
| 13683 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13684 | * compressed document is provided by default if found at compile-time. |
| 13685 | * It use the given SAX function block to handle the parsing callback. |
| 13686 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13687 | * |
| 13688 | * Returns the resulting document tree |
| 13689 | */ |
| 13690 | |
| 13691 | xmlDocPtr |
| 13692 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 13693 | int recovery) { |
| 13694 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 13695 | } |
| 13696 | |
| 13697 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13698 | * xmlRecoverDoc: |
| 13699 | * @cur: a pointer to an array of xmlChar |
| 13700 | * |
| 13701 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13702 | * In the case the document is not Well Formed, a attempt to build a |
| 13703 | * tree is tried anyway |
| 13704 | * |
| 13705 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13706 | */ |
| 13707 | |
| 13708 | xmlDocPtr |
Daniel Veillard | f39eafa | 2009-08-20 19:15:08 +0200 | [diff] [blame] | 13709 | xmlRecoverDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13710 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 13711 | } |
| 13712 | |
| 13713 | /** |
| 13714 | * xmlParseFile: |
| 13715 | * @filename: the filename |
| 13716 | * |
| 13717 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13718 | * compressed document is provided by default if found at compile-time. |
| 13719 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 13720 | * Returns the resulting document tree if the file was wellformed, |
| 13721 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13722 | */ |
| 13723 | |
| 13724 | xmlDocPtr |
| 13725 | xmlParseFile(const char *filename) { |
| 13726 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 13727 | } |
| 13728 | |
| 13729 | /** |
| 13730 | * xmlRecoverFile: |
| 13731 | * @filename: the filename |
| 13732 | * |
| 13733 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13734 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13735 | * In the case the document is not Well Formed, it attempts to build |
| 13736 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13737 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13738 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13739 | */ |
| 13740 | |
| 13741 | xmlDocPtr |
| 13742 | xmlRecoverFile(const char *filename) { |
| 13743 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 13744 | } |
| 13745 | |
| 13746 | |
| 13747 | /** |
| 13748 | * xmlSetupParserForBuffer: |
| 13749 | * @ctxt: an XML parser context |
| 13750 | * @buffer: a xmlChar * buffer |
| 13751 | * @filename: a file name |
| 13752 | * |
| 13753 | * Setup the parser context to parse a new buffer; Clears any prior |
| 13754 | * contents from the parser context. The buffer parameter must not be |
| 13755 | * NULL, but the filename parameter can be |
| 13756 | */ |
| 13757 | void |
| 13758 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 13759 | const char* filename) |
| 13760 | { |
| 13761 | xmlParserInputPtr input; |
| 13762 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13763 | if ((ctxt == NULL) || (buffer == NULL)) |
| 13764 | return; |
| 13765 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13766 | input = xmlNewInputStream(ctxt); |
| 13767 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 13768 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13769 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13770 | return; |
| 13771 | } |
| 13772 | |
| 13773 | xmlClearParserCtxt(ctxt); |
| 13774 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 13775 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13776 | input->base = buffer; |
| 13777 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13778 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13779 | inputPush(ctxt, input); |
| 13780 | } |
| 13781 | |
| 13782 | /** |
| 13783 | * xmlSAXUserParseFile: |
| 13784 | * @sax: a SAX handler |
| 13785 | * @user_data: The user data returned on SAX callbacks |
| 13786 | * @filename: a file name |
| 13787 | * |
| 13788 | * parse an XML file and call the given SAX handler routines. |
| 13789 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13790 | * |
| 13791 | * Returns 0 in case of success or a error number otherwise |
| 13792 | */ |
| 13793 | int |
| 13794 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 13795 | const char *filename) { |
| 13796 | int ret = 0; |
| 13797 | xmlParserCtxtPtr ctxt; |
| 13798 | |
| 13799 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13800 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 13801 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13802 | xmlFree(ctxt->sax); |
| 13803 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13804 | xmlDetectSAX2(ctxt); |
| 13805 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13806 | if (user_data != NULL) |
| 13807 | ctxt->userData = user_data; |
| 13808 | |
| 13809 | xmlParseDocument(ctxt); |
| 13810 | |
| 13811 | if (ctxt->wellFormed) |
| 13812 | ret = 0; |
| 13813 | else { |
| 13814 | if (ctxt->errNo != 0) |
| 13815 | ret = ctxt->errNo; |
| 13816 | else |
| 13817 | ret = -1; |
| 13818 | } |
| 13819 | if (sax != NULL) |
| 13820 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13821 | if (ctxt->myDoc != NULL) { |
| 13822 | xmlFreeDoc(ctxt->myDoc); |
| 13823 | ctxt->myDoc = NULL; |
| 13824 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13825 | xmlFreeParserCtxt(ctxt); |
| 13826 | |
| 13827 | return ret; |
| 13828 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13829 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13830 | |
| 13831 | /************************************************************************ |
| 13832 | * * |
| 13833 | * Front ends when parsing from memory * |
| 13834 | * * |
| 13835 | ************************************************************************/ |
| 13836 | |
| 13837 | /** |
| 13838 | * xmlCreateMemoryParserCtxt: |
| 13839 | * @buffer: a pointer to a char array |
| 13840 | * @size: the size of the array |
| 13841 | * |
| 13842 | * Create a parser context for an XML in-memory document. |
| 13843 | * |
| 13844 | * Returns the new parser context or NULL |
| 13845 | */ |
| 13846 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 13847 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13848 | xmlParserCtxtPtr ctxt; |
| 13849 | xmlParserInputPtr input; |
| 13850 | xmlParserInputBufferPtr buf; |
| 13851 | |
| 13852 | if (buffer == NULL) |
| 13853 | return(NULL); |
| 13854 | if (size <= 0) |
| 13855 | return(NULL); |
| 13856 | |
| 13857 | ctxt = xmlNewParserCtxt(); |
| 13858 | if (ctxt == NULL) |
| 13859 | return(NULL); |
| 13860 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 13861 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13862 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13863 | if (buf == NULL) { |
| 13864 | xmlFreeParserCtxt(ctxt); |
| 13865 | return(NULL); |
| 13866 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13867 | |
| 13868 | input = xmlNewInputStream(ctxt); |
| 13869 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13870 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13871 | xmlFreeParserCtxt(ctxt); |
| 13872 | return(NULL); |
| 13873 | } |
| 13874 | |
| 13875 | input->filename = NULL; |
| 13876 | input->buf = buf; |
| 13877 | input->base = input->buf->buffer->content; |
| 13878 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13879 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13880 | |
| 13881 | inputPush(ctxt, input); |
| 13882 | return(ctxt); |
| 13883 | } |
| 13884 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13885 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13886 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13887 | * xmlSAXParseMemoryWithData: |
| 13888 | * @sax: the SAX handler block |
| 13889 | * @buffer: an pointer to a char array |
| 13890 | * @size: the size of the array |
| 13891 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13892 | * documents |
| 13893 | * @data: the userdata |
| 13894 | * |
| 13895 | * parse an XML in-memory block and use the given SAX function block |
| 13896 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13897 | * DOM tree building routines. |
| 13898 | * |
| 13899 | * User data (void *) is stored within the parser context in the |
| 13900 | * context's _private member, so it is available nearly everywhere in libxml |
| 13901 | * |
| 13902 | * Returns the resulting document tree |
| 13903 | */ |
| 13904 | |
| 13905 | xmlDocPtr |
| 13906 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 13907 | int size, int recovery, void *data) { |
| 13908 | xmlDocPtr ret; |
| 13909 | xmlParserCtxtPtr ctxt; |
| 13910 | |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 13911 | xmlInitParser(); |
| 13912 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13913 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 13914 | if (ctxt == NULL) return(NULL); |
| 13915 | if (sax != NULL) { |
| 13916 | if (ctxt->sax != NULL) |
| 13917 | xmlFree(ctxt->sax); |
| 13918 | ctxt->sax = sax; |
| 13919 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13920 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13921 | if (data!=NULL) { |
| 13922 | ctxt->_private=data; |
| 13923 | } |
| 13924 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 13925 | ctxt->recovery = recovery; |
| 13926 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13927 | xmlParseDocument(ctxt); |
| 13928 | |
| 13929 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 13930 | else { |
| 13931 | ret = NULL; |
| 13932 | xmlFreeDoc(ctxt->myDoc); |
| 13933 | ctxt->myDoc = NULL; |
| 13934 | } |
| 13935 | if (sax != NULL) |
| 13936 | ctxt->sax = NULL; |
| 13937 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 13938 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13939 | return(ret); |
| 13940 | } |
| 13941 | |
| 13942 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13943 | * xmlSAXParseMemory: |
| 13944 | * @sax: the SAX handler block |
| 13945 | * @buffer: an pointer to a char array |
| 13946 | * @size: the size of the array |
| 13947 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 13948 | * documents |
| 13949 | * |
| 13950 | * parse an XML in-memory block and use the given SAX function block |
| 13951 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13952 | * DOM tree building routines. |
| 13953 | * |
| 13954 | * Returns the resulting document tree |
| 13955 | */ |
| 13956 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13957 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 13958 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13959 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13960 | } |
| 13961 | |
| 13962 | /** |
| 13963 | * xmlParseMemory: |
| 13964 | * @buffer: an pointer to a char array |
| 13965 | * @size: the size of the array |
| 13966 | * |
| 13967 | * parse an XML in-memory block and build a tree. |
| 13968 | * |
| 13969 | * Returns the resulting document tree |
| 13970 | */ |
| 13971 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13972 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13973 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 13974 | } |
| 13975 | |
| 13976 | /** |
| 13977 | * xmlRecoverMemory: |
| 13978 | * @buffer: an pointer to a char array |
| 13979 | * @size: the size of the array |
| 13980 | * |
| 13981 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13982 | * In the case the document is not Well Formed, an attempt to |
| 13983 | * build a tree is tried anyway |
| 13984 | * |
| 13985 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13986 | */ |
| 13987 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13988 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13989 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 13990 | } |
| 13991 | |
| 13992 | /** |
| 13993 | * xmlSAXUserParseMemory: |
| 13994 | * @sax: a SAX handler |
| 13995 | * @user_data: The user data returned on SAX callbacks |
| 13996 | * @buffer: an in-memory XML document input |
| 13997 | * @size: the length of the XML document in bytes |
| 13998 | * |
| 13999 | * A better SAX parsing routine. |
| 14000 | * 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] | 14001 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14002 | * Returns 0 in case of success or a error number otherwise |
| 14003 | */ |
| 14004 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 14005 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14006 | int ret = 0; |
| 14007 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14008 | |
| 14009 | xmlInitParser(); |
| 14010 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14011 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14012 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14013 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 14014 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 14015 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14016 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14017 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 14018 | if (user_data != NULL) |
| 14019 | ctxt->userData = user_data; |
Daniel Veillard | ab2a763 | 2009-07-09 08:45:03 +0200 | [diff] [blame] | 14020 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14021 | xmlParseDocument(ctxt); |
| 14022 | |
| 14023 | if (ctxt->wellFormed) |
| 14024 | ret = 0; |
| 14025 | else { |
| 14026 | if (ctxt->errNo != 0) |
| 14027 | ret = ctxt->errNo; |
| 14028 | else |
| 14029 | ret = -1; |
| 14030 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 14031 | if (sax != NULL) |
| 14032 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14033 | if (ctxt->myDoc != NULL) { |
| 14034 | xmlFreeDoc(ctxt->myDoc); |
| 14035 | ctxt->myDoc = NULL; |
| 14036 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14037 | xmlFreeParserCtxt(ctxt); |
| 14038 | |
| 14039 | return ret; |
| 14040 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14041 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14042 | |
| 14043 | /** |
| 14044 | * xmlCreateDocParserCtxt: |
| 14045 | * @cur: a pointer to an array of xmlChar |
| 14046 | * |
| 14047 | * Creates a parser context for an XML in-memory document. |
| 14048 | * |
| 14049 | * Returns the new parser context or NULL |
| 14050 | */ |
| 14051 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14052 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14053 | int len; |
| 14054 | |
| 14055 | if (cur == NULL) |
| 14056 | return(NULL); |
| 14057 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14058 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14059 | } |
| 14060 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14061 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14062 | /** |
| 14063 | * xmlSAXParseDoc: |
| 14064 | * @sax: the SAX handler block |
| 14065 | * @cur: a pointer to an array of xmlChar |
| 14066 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 14067 | * documents |
| 14068 | * |
| 14069 | * parse an XML in-memory document and build a tree. |
| 14070 | * It use the given SAX function block to handle the parsing callback. |
| 14071 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 14072 | * |
| 14073 | * Returns the resulting document tree |
| 14074 | */ |
| 14075 | |
| 14076 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14077 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14078 | xmlDocPtr ret; |
| 14079 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14080 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14081 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14082 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14083 | |
| 14084 | |
| 14085 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14086 | if (ctxt == NULL) return(NULL); |
| 14087 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14088 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14089 | ctxt->sax = sax; |
| 14090 | ctxt->userData = NULL; |
| 14091 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 14092 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14093 | |
| 14094 | xmlParseDocument(ctxt); |
| 14095 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 14096 | else { |
| 14097 | ret = NULL; |
| 14098 | xmlFreeDoc(ctxt->myDoc); |
| 14099 | ctxt->myDoc = NULL; |
| 14100 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 14101 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 14102 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14103 | xmlFreeParserCtxt(ctxt); |
| 14104 | |
| 14105 | return(ret); |
| 14106 | } |
| 14107 | |
| 14108 | /** |
| 14109 | * xmlParseDoc: |
| 14110 | * @cur: a pointer to an array of xmlChar |
| 14111 | * |
| 14112 | * parse an XML in-memory document and build a tree. |
| 14113 | * |
| 14114 | * Returns the resulting document tree |
| 14115 | */ |
| 14116 | |
| 14117 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 14118 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14119 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 14120 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14121 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14122 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14123 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14124 | /************************************************************************ |
| 14125 | * * |
| 14126 | * Specific function to keep track of entities references * |
| 14127 | * and used by the XSLT debugger * |
| 14128 | * * |
| 14129 | ************************************************************************/ |
| 14130 | |
| 14131 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 14132 | |
| 14133 | /** |
| 14134 | * xmlAddEntityReference: |
| 14135 | * @ent : A valid entity |
| 14136 | * @firstNode : A valid first node for children of entity |
| 14137 | * @lastNode : A valid last node of children entity |
| 14138 | * |
| 14139 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 14140 | */ |
| 14141 | static void |
| 14142 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 14143 | xmlNodePtr lastNode) |
| 14144 | { |
| 14145 | if (xmlEntityRefFunc != NULL) { |
| 14146 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 14147 | } |
| 14148 | } |
| 14149 | |
| 14150 | |
| 14151 | /** |
| 14152 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 14153 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 14154 | * |
| 14155 | * Set the function to call call back when a xml reference has been made |
| 14156 | */ |
| 14157 | void |
| 14158 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 14159 | { |
| 14160 | xmlEntityRefFunc = func; |
| 14161 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14162 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14163 | |
| 14164 | /************************************************************************ |
| 14165 | * * |
| 14166 | * Miscellaneous * |
| 14167 | * * |
| 14168 | ************************************************************************/ |
| 14169 | |
| 14170 | #ifdef LIBXML_XPATH_ENABLED |
| 14171 | #include <libxml/xpath.h> |
| 14172 | #endif |
| 14173 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 14174 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14175 | static int xmlParserInitialized = 0; |
| 14176 | |
| 14177 | /** |
| 14178 | * xmlInitParser: |
| 14179 | * |
| 14180 | * Initialization function for the XML parser. |
| 14181 | * This is not reentrant. Call once before processing in case of |
| 14182 | * use in multithreaded programs. |
| 14183 | */ |
| 14184 | |
| 14185 | void |
| 14186 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 14187 | if (xmlParserInitialized != 0) |
| 14188 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14189 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14190 | #ifdef LIBXML_THREAD_ENABLED |
| 14191 | __xmlGlobalInitMutexLock(); |
| 14192 | if (xmlParserInitialized == 0) { |
| 14193 | #endif |
Daniel Veillard | 7dd7080 | 2009-06-04 11:08:39 +0200 | [diff] [blame] | 14194 | xmlInitThreads(); |
Mike Hommey | e6f0509 | 2010-10-15 19:50:03 +0200 | [diff] [blame] | 14195 | xmlInitGlobals(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14196 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 14197 | (xmlGenericError == NULL)) |
| 14198 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14199 | xmlInitMemory(); |
Daniel Veillard | 379ebc1 | 2012-05-18 15:41:31 +0800 | [diff] [blame] | 14200 | xmlInitializeDict(); |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14201 | xmlInitCharEncodingHandlers(); |
| 14202 | xmlDefaultSAXHandlerInit(); |
| 14203 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14204 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14205 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 14206 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14207 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14208 | htmlInitAutoClose(); |
| 14209 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14210 | #endif |
| 14211 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14212 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14213 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 14214 | xmlParserInitialized = 1; |
| 14215 | #ifdef LIBXML_THREAD_ENABLED |
| 14216 | } |
| 14217 | __xmlGlobalInitMutexUnlock(); |
| 14218 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14219 | } |
| 14220 | |
| 14221 | /** |
| 14222 | * xmlCleanupParser: |
| 14223 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 14224 | * This function name is somewhat misleading. It does not clean up |
| 14225 | * parser state, it cleans up memory allocated by the library itself. |
| 14226 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 14227 | * related global memory allocated for the library processing. |
| 14228 | * It doesn't deallocate any document related memory. One should |
| 14229 | * call xmlCleanupParser() only when the process has finished using |
| 14230 | * the library and all XML/HTML documents built with it. |
| 14231 | * See also xmlInitParser() which has the opposite function of preparing |
| 14232 | * the library for operations. |
Daniel Veillard | 0110120 | 2009-02-21 09:22:04 +0000 | [diff] [blame] | 14233 | * |
| 14234 | * WARNING: if your application is multithreaded or has plugin support |
| 14235 | * calling this may crash the application if another thread or |
| 14236 | * a plugin is still using libxml2. It's sometimes very hard to |
| 14237 | * guess if libxml2 is in use in the application, some libraries |
| 14238 | * or plugins may use it without notice. In case of doubt abstain |
| 14239 | * from calling this function or do it just before calling exit() |
| 14240 | * to avoid leak reports from valgrind ! |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14241 | */ |
| 14242 | |
| 14243 | void |
| 14244 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 14245 | if (!xmlParserInitialized) |
| 14246 | return; |
| 14247 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14248 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 14249 | #ifdef LIBXML_CATALOG_ENABLED |
| 14250 | xmlCatalogCleanup(); |
| 14251 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 14252 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 14253 | xmlCleanupInputCallbacks(); |
| 14254 | #ifdef LIBXML_OUTPUT_ENABLED |
| 14255 | xmlCleanupOutputCallbacks(); |
| 14256 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14257 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14258 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 14259 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14260 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 14261 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 14262 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 14263 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 14264 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 14265 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14266 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14267 | |
| 14268 | /************************************************************************ |
| 14269 | * * |
| 14270 | * New set (2.6.0) of simpler and more flexible APIs * |
| 14271 | * * |
| 14272 | ************************************************************************/ |
| 14273 | |
| 14274 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14275 | * DICT_FREE: |
| 14276 | * @str: a string |
| 14277 | * |
| 14278 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 14279 | * current scope |
| 14280 | */ |
| 14281 | #define DICT_FREE(str) \ |
| 14282 | if ((str) && ((!dict) || \ |
| 14283 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 14284 | xmlFree((char *)(str)); |
| 14285 | |
| 14286 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14287 | * xmlCtxtReset: |
| 14288 | * @ctxt: an XML parser context |
| 14289 | * |
| 14290 | * Reset a parser context |
| 14291 | */ |
| 14292 | void |
| 14293 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 14294 | { |
| 14295 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14296 | xmlDictPtr dict; |
| 14297 | |
| 14298 | if (ctxt == NULL) |
| 14299 | return; |
| 14300 | |
| 14301 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14302 | |
| 14303 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 14304 | xmlFreeInputStream(input); |
| 14305 | } |
| 14306 | ctxt->inputNr = 0; |
| 14307 | ctxt->input = NULL; |
| 14308 | |
| 14309 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 14310 | if (ctxt->spaceTab != NULL) { |
| 14311 | ctxt->spaceTab[0] = -1; |
| 14312 | ctxt->space = &ctxt->spaceTab[0]; |
| 14313 | } else { |
| 14314 | ctxt->space = NULL; |
| 14315 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14316 | |
| 14317 | |
| 14318 | ctxt->nodeNr = 0; |
| 14319 | ctxt->node = NULL; |
| 14320 | |
| 14321 | ctxt->nameNr = 0; |
| 14322 | ctxt->name = NULL; |
| 14323 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14324 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14325 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14326 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14327 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14328 | DICT_FREE(ctxt->directory); |
| 14329 | ctxt->directory = NULL; |
| 14330 | DICT_FREE(ctxt->extSubURI); |
| 14331 | ctxt->extSubURI = NULL; |
| 14332 | DICT_FREE(ctxt->extSubSystem); |
| 14333 | ctxt->extSubSystem = NULL; |
| 14334 | if (ctxt->myDoc != NULL) |
| 14335 | xmlFreeDoc(ctxt->myDoc); |
| 14336 | ctxt->myDoc = NULL; |
| 14337 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14338 | ctxt->standalone = -1; |
| 14339 | ctxt->hasExternalSubset = 0; |
| 14340 | ctxt->hasPErefs = 0; |
| 14341 | ctxt->html = 0; |
| 14342 | ctxt->external = 0; |
| 14343 | ctxt->instate = XML_PARSER_START; |
| 14344 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14345 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14346 | ctxt->wellFormed = 1; |
| 14347 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 14348 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14349 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14350 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14351 | ctxt->vctxt.userData = ctxt; |
| 14352 | ctxt->vctxt.error = xmlParserValidityError; |
| 14353 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 14354 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14355 | ctxt->record_info = 0; |
| 14356 | ctxt->nbChars = 0; |
| 14357 | ctxt->checkIndex = 0; |
| 14358 | ctxt->inSubset = 0; |
| 14359 | ctxt->errNo = XML_ERR_OK; |
| 14360 | ctxt->depth = 0; |
| 14361 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 14362 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 14363 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 14364 | ctxt->sizeentities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14365 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 14366 | |
| 14367 | if (ctxt->attsDefault != NULL) { |
| 14368 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 14369 | ctxt->attsDefault = NULL; |
| 14370 | } |
| 14371 | if (ctxt->attsSpecial != NULL) { |
| 14372 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 14373 | ctxt->attsSpecial = NULL; |
| 14374 | } |
| 14375 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14376 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14377 | if (ctxt->catalogs != NULL) |
| 14378 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 14379 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 14380 | if (ctxt->lastError.code != XML_ERR_OK) |
| 14381 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14382 | } |
| 14383 | |
| 14384 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14385 | * xmlCtxtResetPush: |
| 14386 | * @ctxt: an XML parser context |
| 14387 | * @chunk: a pointer to an array of chars |
| 14388 | * @size: number of chars in the array |
| 14389 | * @filename: an optional file name or URI |
| 14390 | * @encoding: the document encoding, or NULL |
| 14391 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14392 | * Reset a push parser context |
| 14393 | * |
| 14394 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14395 | */ |
| 14396 | int |
| 14397 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 14398 | int size, const char *filename, const char *encoding) |
| 14399 | { |
| 14400 | xmlParserInputPtr inputStream; |
| 14401 | xmlParserInputBufferPtr buf; |
| 14402 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 14403 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14404 | if (ctxt == NULL) |
| 14405 | return(1); |
| 14406 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14407 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 14408 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 14409 | |
| 14410 | buf = xmlAllocParserInputBuffer(enc); |
| 14411 | if (buf == NULL) |
| 14412 | return(1); |
| 14413 | |
| 14414 | if (ctxt == NULL) { |
| 14415 | xmlFreeParserInputBuffer(buf); |
| 14416 | return(1); |
| 14417 | } |
| 14418 | |
| 14419 | xmlCtxtReset(ctxt); |
| 14420 | |
| 14421 | if (ctxt->pushTab == NULL) { |
| 14422 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 14423 | sizeof(xmlChar *)); |
| 14424 | if (ctxt->pushTab == NULL) { |
| 14425 | xmlErrMemory(ctxt, NULL); |
| 14426 | xmlFreeParserInputBuffer(buf); |
| 14427 | return(1); |
| 14428 | } |
| 14429 | } |
| 14430 | |
| 14431 | if (filename == NULL) { |
| 14432 | ctxt->directory = NULL; |
| 14433 | } else { |
| 14434 | ctxt->directory = xmlParserGetDirectory(filename); |
| 14435 | } |
| 14436 | |
| 14437 | inputStream = xmlNewInputStream(ctxt); |
| 14438 | if (inputStream == NULL) { |
| 14439 | xmlFreeParserInputBuffer(buf); |
| 14440 | return(1); |
| 14441 | } |
| 14442 | |
| 14443 | if (filename == NULL) |
| 14444 | inputStream->filename = NULL; |
| 14445 | else |
| 14446 | inputStream->filename = (char *) |
| 14447 | xmlCanonicPath((const xmlChar *) filename); |
| 14448 | inputStream->buf = buf; |
| 14449 | inputStream->base = inputStream->buf->buffer->content; |
| 14450 | inputStream->cur = inputStream->buf->buffer->content; |
| 14451 | inputStream->end = |
| 14452 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 14453 | |
| 14454 | inputPush(ctxt, inputStream); |
| 14455 | |
| 14456 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 14457 | (ctxt->input->buf != NULL)) { |
| 14458 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 14459 | int cur = ctxt->input->cur - ctxt->input->base; |
| 14460 | |
| 14461 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 14462 | |
| 14463 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 14464 | ctxt->input->cur = ctxt->input->base + cur; |
| 14465 | ctxt->input->end = |
| 14466 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer-> |
| 14467 | use]; |
| 14468 | #ifdef DEBUG_PUSH |
| 14469 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 14470 | #endif |
| 14471 | } |
| 14472 | |
| 14473 | if (encoding != NULL) { |
| 14474 | xmlCharEncodingHandlerPtr hdlr; |
| 14475 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14476 | if (ctxt->encoding != NULL) |
| 14477 | xmlFree((xmlChar *) ctxt->encoding); |
| 14478 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14479 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14480 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14481 | if (hdlr != NULL) { |
| 14482 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14483 | } else { |
| 14484 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 14485 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 14486 | } |
| 14487 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 14488 | xmlSwitchEncoding(ctxt, enc); |
| 14489 | } |
| 14490 | |
| 14491 | return(0); |
| 14492 | } |
| 14493 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14494 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14495 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14496 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14497 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14498 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14499 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14500 | * |
| 14501 | * Applies the options to the parser context |
| 14502 | * |
| 14503 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14504 | * in case of error. |
| 14505 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14506 | static int |
| 14507 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14508 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14509 | if (ctxt == NULL) |
| 14510 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14511 | if (encoding != NULL) { |
| 14512 | if (ctxt->encoding != NULL) |
| 14513 | xmlFree((xmlChar *) ctxt->encoding); |
| 14514 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14515 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14516 | if (options & XML_PARSE_RECOVER) { |
| 14517 | ctxt->recovery = 1; |
| 14518 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14519 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14520 | } else |
| 14521 | ctxt->recovery = 0; |
| 14522 | if (options & XML_PARSE_DTDLOAD) { |
| 14523 | ctxt->loadsubset = XML_DETECT_IDS; |
| 14524 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14525 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14526 | } else |
| 14527 | ctxt->loadsubset = 0; |
| 14528 | if (options & XML_PARSE_DTDATTR) { |
| 14529 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 14530 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14531 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14532 | } |
| 14533 | if (options & XML_PARSE_NOENT) { |
| 14534 | ctxt->replaceEntities = 1; |
| 14535 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 14536 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14537 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14538 | } else |
| 14539 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14540 | if (options & XML_PARSE_PEDANTIC) { |
| 14541 | ctxt->pedantic = 1; |
| 14542 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14543 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14544 | } else |
| 14545 | ctxt->pedantic = 0; |
| 14546 | if (options & XML_PARSE_NOBLANKS) { |
| 14547 | ctxt->keepBlanks = 0; |
| 14548 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 14549 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14550 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14551 | } else |
| 14552 | ctxt->keepBlanks = 1; |
| 14553 | if (options & XML_PARSE_DTDVALID) { |
| 14554 | ctxt->validate = 1; |
| 14555 | if (options & XML_PARSE_NOWARNING) |
| 14556 | ctxt->vctxt.warning = NULL; |
| 14557 | if (options & XML_PARSE_NOERROR) |
| 14558 | ctxt->vctxt.error = NULL; |
| 14559 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14560 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14561 | } else |
| 14562 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 14563 | if (options & XML_PARSE_NOWARNING) { |
| 14564 | ctxt->sax->warning = NULL; |
| 14565 | options -= XML_PARSE_NOWARNING; |
| 14566 | } |
| 14567 | if (options & XML_PARSE_NOERROR) { |
| 14568 | ctxt->sax->error = NULL; |
| 14569 | ctxt->sax->fatalError = NULL; |
| 14570 | options -= XML_PARSE_NOERROR; |
| 14571 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14572 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14573 | if (options & XML_PARSE_SAX1) { |
| 14574 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 14575 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 14576 | ctxt->sax->startElementNs = NULL; |
| 14577 | ctxt->sax->endElementNs = NULL; |
| 14578 | ctxt->sax->initialized = 1; |
| 14579 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14580 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14581 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14582 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14583 | if (options & XML_PARSE_NODICT) { |
| 14584 | ctxt->dictNames = 0; |
| 14585 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14586 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14587 | } else { |
| 14588 | ctxt->dictNames = 1; |
| 14589 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14590 | if (options & XML_PARSE_NOCDATA) { |
| 14591 | ctxt->sax->cdataBlock = NULL; |
| 14592 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14593 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14594 | } |
| 14595 | if (options & XML_PARSE_NSCLEAN) { |
| 14596 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 14597 | options -= XML_PARSE_NSCLEAN; |
| 14598 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14599 | if (options & XML_PARSE_NONET) { |
| 14600 | ctxt->options |= XML_PARSE_NONET; |
| 14601 | options -= XML_PARSE_NONET; |
| 14602 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 14603 | if (options & XML_PARSE_COMPACT) { |
| 14604 | ctxt->options |= XML_PARSE_COMPACT; |
| 14605 | options -= XML_PARSE_COMPACT; |
| 14606 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 14607 | if (options & XML_PARSE_OLD10) { |
| 14608 | ctxt->options |= XML_PARSE_OLD10; |
| 14609 | options -= XML_PARSE_OLD10; |
| 14610 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 14611 | if (options & XML_PARSE_NOBASEFIX) { |
| 14612 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 14613 | options -= XML_PARSE_NOBASEFIX; |
| 14614 | } |
| 14615 | if (options & XML_PARSE_HUGE) { |
| 14616 | ctxt->options |= XML_PARSE_HUGE; |
| 14617 | options -= XML_PARSE_HUGE; |
| 14618 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame] | 14619 | if (options & XML_PARSE_OLDSAX) { |
| 14620 | ctxt->options |= XML_PARSE_OLDSAX; |
| 14621 | options -= XML_PARSE_OLDSAX; |
| 14622 | } |
Daniel Veillard | c62efc8 | 2011-05-16 16:03:50 +0800 | [diff] [blame] | 14623 | if (options & XML_PARSE_IGNORE_ENC) { |
| 14624 | ctxt->options |= XML_PARSE_IGNORE_ENC; |
| 14625 | options -= XML_PARSE_IGNORE_ENC; |
| 14626 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 14627 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14628 | return (options); |
| 14629 | } |
| 14630 | |
| 14631 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14632 | * xmlCtxtUseOptions: |
| 14633 | * @ctxt: an XML parser context |
| 14634 | * @options: a combination of xmlParserOption |
| 14635 | * |
| 14636 | * Applies the options to the parser context |
| 14637 | * |
| 14638 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14639 | * in case of error. |
| 14640 | */ |
| 14641 | int |
| 14642 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 14643 | { |
| 14644 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 14645 | } |
| 14646 | |
| 14647 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14648 | * xmlDoRead: |
| 14649 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14650 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14651 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14652 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14653 | * @reuse: keep the context for reuse |
| 14654 | * |
| 14655 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14656 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14657 | * Returns the resulting document tree or NULL |
| 14658 | */ |
| 14659 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14660 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 14661 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14662 | { |
| 14663 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14664 | |
| 14665 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14666 | if (encoding != NULL) { |
| 14667 | xmlCharEncodingHandlerPtr hdlr; |
| 14668 | |
| 14669 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14670 | if (hdlr != NULL) |
| 14671 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14672 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14673 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 14674 | (ctxt->input->filename == NULL)) |
| 14675 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14676 | xmlParseDocument(ctxt); |
| 14677 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 14678 | ret = ctxt->myDoc; |
| 14679 | else { |
| 14680 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14681 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14682 | xmlFreeDoc(ctxt->myDoc); |
| 14683 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14684 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14685 | ctxt->myDoc = NULL; |
| 14686 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14687 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14688 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14689 | |
| 14690 | return (ret); |
| 14691 | } |
| 14692 | |
| 14693 | /** |
| 14694 | * xmlReadDoc: |
| 14695 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14696 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14697 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14698 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14699 | * |
| 14700 | * parse an XML in-memory document and build a tree. |
| 14701 | * |
| 14702 | * Returns the resulting document tree |
| 14703 | */ |
| 14704 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14705 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14706 | { |
| 14707 | xmlParserCtxtPtr ctxt; |
| 14708 | |
| 14709 | if (cur == NULL) |
| 14710 | return (NULL); |
| 14711 | |
| 14712 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14713 | if (ctxt == NULL) |
| 14714 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14715 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14716 | } |
| 14717 | |
| 14718 | /** |
| 14719 | * xmlReadFile: |
| 14720 | * @filename: a file or URL |
| 14721 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14722 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14723 | * |
| 14724 | * parse an XML file from the filesystem or the network. |
| 14725 | * |
| 14726 | * Returns the resulting document tree |
| 14727 | */ |
| 14728 | xmlDocPtr |
| 14729 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 14730 | { |
| 14731 | xmlParserCtxtPtr ctxt; |
| 14732 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14733 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14734 | if (ctxt == NULL) |
| 14735 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14736 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14737 | } |
| 14738 | |
| 14739 | /** |
| 14740 | * xmlReadMemory: |
| 14741 | * @buffer: a pointer to a char array |
| 14742 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14743 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14744 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14745 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14746 | * |
| 14747 | * parse an XML in-memory document and build a tree. |
| 14748 | * |
| 14749 | * Returns the resulting document tree |
| 14750 | */ |
| 14751 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14752 | 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] | 14753 | { |
| 14754 | xmlParserCtxtPtr ctxt; |
| 14755 | |
| 14756 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14757 | if (ctxt == NULL) |
| 14758 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14759 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14760 | } |
| 14761 | |
| 14762 | /** |
| 14763 | * xmlReadFd: |
| 14764 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14765 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14766 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14767 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14768 | * |
| 14769 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14770 | * NOTE that the file descriptor will not be closed when the |
| 14771 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14772 | * |
| 14773 | * Returns the resulting document tree |
| 14774 | */ |
| 14775 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14776 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14777 | { |
| 14778 | xmlParserCtxtPtr ctxt; |
| 14779 | xmlParserInputBufferPtr input; |
| 14780 | xmlParserInputPtr stream; |
| 14781 | |
| 14782 | if (fd < 0) |
| 14783 | return (NULL); |
| 14784 | |
| 14785 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14786 | if (input == NULL) |
| 14787 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14788 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14789 | ctxt = xmlNewParserCtxt(); |
| 14790 | if (ctxt == NULL) { |
| 14791 | xmlFreeParserInputBuffer(input); |
| 14792 | return (NULL); |
| 14793 | } |
| 14794 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14795 | if (stream == NULL) { |
| 14796 | xmlFreeParserInputBuffer(input); |
| 14797 | xmlFreeParserCtxt(ctxt); |
| 14798 | return (NULL); |
| 14799 | } |
| 14800 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14801 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14802 | } |
| 14803 | |
| 14804 | /** |
| 14805 | * xmlReadIO: |
| 14806 | * @ioread: an I/O read function |
| 14807 | * @ioclose: an I/O close function |
| 14808 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14809 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14810 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14811 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14812 | * |
| 14813 | * 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] | 14814 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14815 | * Returns the resulting document tree |
| 14816 | */ |
| 14817 | xmlDocPtr |
| 14818 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14819 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14820 | { |
| 14821 | xmlParserCtxtPtr ctxt; |
| 14822 | xmlParserInputBufferPtr input; |
| 14823 | xmlParserInputPtr stream; |
| 14824 | |
| 14825 | if (ioread == NULL) |
| 14826 | return (NULL); |
| 14827 | |
| 14828 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 14829 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 14830 | if (input == NULL) { |
| 14831 | if (ioclose != NULL) |
| 14832 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14833 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 14834 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14835 | ctxt = xmlNewParserCtxt(); |
| 14836 | if (ctxt == NULL) { |
| 14837 | xmlFreeParserInputBuffer(input); |
| 14838 | return (NULL); |
| 14839 | } |
| 14840 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14841 | if (stream == NULL) { |
| 14842 | xmlFreeParserInputBuffer(input); |
| 14843 | xmlFreeParserCtxt(ctxt); |
| 14844 | return (NULL); |
| 14845 | } |
| 14846 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14847 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14848 | } |
| 14849 | |
| 14850 | /** |
| 14851 | * xmlCtxtReadDoc: |
| 14852 | * @ctxt: an XML parser context |
| 14853 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14854 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14855 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14856 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14857 | * |
| 14858 | * parse an XML in-memory document and build a tree. |
| 14859 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 14860 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14861 | * Returns the resulting document tree |
| 14862 | */ |
| 14863 | xmlDocPtr |
| 14864 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14865 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14866 | { |
| 14867 | xmlParserInputPtr stream; |
| 14868 | |
| 14869 | if (cur == NULL) |
| 14870 | return (NULL); |
| 14871 | if (ctxt == NULL) |
| 14872 | return (NULL); |
| 14873 | |
| 14874 | xmlCtxtReset(ctxt); |
| 14875 | |
| 14876 | stream = xmlNewStringInputStream(ctxt, cur); |
| 14877 | if (stream == NULL) { |
| 14878 | return (NULL); |
| 14879 | } |
| 14880 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14881 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14882 | } |
| 14883 | |
| 14884 | /** |
| 14885 | * xmlCtxtReadFile: |
| 14886 | * @ctxt: an XML parser context |
| 14887 | * @filename: a file or URL |
| 14888 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14889 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14890 | * |
| 14891 | * parse an XML file from the filesystem or the network. |
| 14892 | * This reuses the existing @ctxt parser context |
| 14893 | * |
| 14894 | * Returns the resulting document tree |
| 14895 | */ |
| 14896 | xmlDocPtr |
| 14897 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 14898 | const char *encoding, int options) |
| 14899 | { |
| 14900 | xmlParserInputPtr stream; |
| 14901 | |
| 14902 | if (filename == NULL) |
| 14903 | return (NULL); |
| 14904 | if (ctxt == NULL) |
| 14905 | return (NULL); |
| 14906 | |
| 14907 | xmlCtxtReset(ctxt); |
| 14908 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 14909 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14910 | if (stream == NULL) { |
| 14911 | return (NULL); |
| 14912 | } |
| 14913 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14914 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14915 | } |
| 14916 | |
| 14917 | /** |
| 14918 | * xmlCtxtReadMemory: |
| 14919 | * @ctxt: an XML parser context |
| 14920 | * @buffer: a pointer to a char array |
| 14921 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14922 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14923 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14924 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14925 | * |
| 14926 | * parse an XML in-memory document and build a tree. |
| 14927 | * This reuses the existing @ctxt parser context |
| 14928 | * |
| 14929 | * Returns the resulting document tree |
| 14930 | */ |
| 14931 | xmlDocPtr |
| 14932 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14933 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14934 | { |
| 14935 | xmlParserInputBufferPtr input; |
| 14936 | xmlParserInputPtr stream; |
| 14937 | |
| 14938 | if (ctxt == NULL) |
| 14939 | return (NULL); |
| 14940 | if (buffer == NULL) |
| 14941 | return (NULL); |
| 14942 | |
| 14943 | xmlCtxtReset(ctxt); |
| 14944 | |
| 14945 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 14946 | if (input == NULL) { |
| 14947 | return(NULL); |
| 14948 | } |
| 14949 | |
| 14950 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14951 | if (stream == NULL) { |
| 14952 | xmlFreeParserInputBuffer(input); |
| 14953 | return(NULL); |
| 14954 | } |
| 14955 | |
| 14956 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14957 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14958 | } |
| 14959 | |
| 14960 | /** |
| 14961 | * xmlCtxtReadFd: |
| 14962 | * @ctxt: an XML parser context |
| 14963 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14964 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14965 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14966 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14967 | * |
| 14968 | * parse an XML from a file descriptor and build a tree. |
| 14969 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14970 | * NOTE that the file descriptor will not be closed when the |
| 14971 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14972 | * |
| 14973 | * Returns the resulting document tree |
| 14974 | */ |
| 14975 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14976 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 14977 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14978 | { |
| 14979 | xmlParserInputBufferPtr input; |
| 14980 | xmlParserInputPtr stream; |
| 14981 | |
| 14982 | if (fd < 0) |
| 14983 | return (NULL); |
| 14984 | if (ctxt == NULL) |
| 14985 | return (NULL); |
| 14986 | |
| 14987 | xmlCtxtReset(ctxt); |
| 14988 | |
| 14989 | |
| 14990 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14991 | if (input == NULL) |
| 14992 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14993 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14994 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14995 | if (stream == NULL) { |
| 14996 | xmlFreeParserInputBuffer(input); |
| 14997 | return (NULL); |
| 14998 | } |
| 14999 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15000 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15001 | } |
| 15002 | |
| 15003 | /** |
| 15004 | * xmlCtxtReadIO: |
| 15005 | * @ctxt: an XML parser context |
| 15006 | * @ioread: an I/O read function |
| 15007 | * @ioclose: an I/O close function |
| 15008 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15009 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15010 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 15011 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15012 | * |
| 15013 | * parse an XML document from I/O functions and source and build a tree. |
| 15014 | * This reuses the existing @ctxt parser context |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15015 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15016 | * Returns the resulting document tree |
| 15017 | */ |
| 15018 | xmlDocPtr |
| 15019 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 15020 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15021 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15022 | const char *encoding, int options) |
| 15023 | { |
| 15024 | xmlParserInputBufferPtr input; |
| 15025 | xmlParserInputPtr stream; |
| 15026 | |
| 15027 | if (ioread == NULL) |
| 15028 | return (NULL); |
| 15029 | if (ctxt == NULL) |
| 15030 | return (NULL); |
| 15031 | |
| 15032 | xmlCtxtReset(ctxt); |
| 15033 | |
| 15034 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 15035 | XML_CHAR_ENCODING_NONE); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15036 | if (input == NULL) { |
| 15037 | if (ioclose != NULL) |
| 15038 | ioclose(ioctx); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15039 | return (NULL); |
Lin Yi-Li | 24464be | 2012-05-10 16:14:55 +0800 | [diff] [blame] | 15040 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15041 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 15042 | if (stream == NULL) { |
| 15043 | xmlFreeParserInputBuffer(input); |
| 15044 | return (NULL); |
| 15045 | } |
| 15046 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 15047 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 15048 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 15049 | |
| 15050 | #define bottom_parser |
| 15051 | #include "elfgcchack.h" |