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> |
| 43 | #include <string.h> |
Aleksey Sanin | e7acf43 | 2003-10-02 20:05:27 +0000 | [diff] [blame] | 44 | #include <stdarg.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 45 | #include <libxml/xmlmemory.h> |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 46 | #include <libxml/threads.h> |
| 47 | #include <libxml/globals.h> |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 48 | #include <libxml/tree.h> |
| 49 | #include <libxml/parser.h> |
| 50 | #include <libxml/parserInternals.h> |
| 51 | #include <libxml/valid.h> |
| 52 | #include <libxml/entities.h> |
| 53 | #include <libxml/xmlerror.h> |
| 54 | #include <libxml/encoding.h> |
| 55 | #include <libxml/xmlIO.h> |
| 56 | #include <libxml/uri.h> |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 57 | #ifdef LIBXML_CATALOG_ENABLED |
| 58 | #include <libxml/catalog.h> |
| 59 | #endif |
William M. Brack | 1d8c9b2 | 2004-12-25 10:14:57 +0000 | [diff] [blame] | 60 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 61 | #include <libxml/xmlschemastypes.h> |
| 62 | #include <libxml/relaxng.h> |
| 63 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | #ifdef HAVE_CTYPE_H |
| 65 | #include <ctype.h> |
| 66 | #endif |
| 67 | #ifdef HAVE_STDLIB_H |
| 68 | #include <stdlib.h> |
| 69 | #endif |
| 70 | #ifdef HAVE_SYS_STAT_H |
| 71 | #include <sys/stat.h> |
| 72 | #endif |
| 73 | #ifdef HAVE_FCNTL_H |
| 74 | #include <fcntl.h> |
| 75 | #endif |
| 76 | #ifdef HAVE_UNISTD_H |
| 77 | #include <unistd.h> |
| 78 | #endif |
| 79 | #ifdef HAVE_ZLIB_H |
| 80 | #include <zlib.h> |
| 81 | #endif |
| 82 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 83 | static void |
| 84 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info); |
| 85 | |
| 86 | /************************************************************************ |
| 87 | * * |
| 88 | * Arbitrary limits set in the parser. See XML_PARSE_HUGE * |
| 89 | * * |
| 90 | ************************************************************************/ |
| 91 | |
| 92 | #define XML_PARSER_BIG_ENTITY 1000 |
| 93 | #define XML_PARSER_LOT_ENTITY 5000 |
| 94 | |
| 95 | /* |
| 96 | * XML_PARSER_NON_LINEAR is the threshold where the ratio of parsed entity |
| 97 | * replacement over the size in byte of the input indicates that you have |
| 98 | * and eponential behaviour. A value of 10 correspond to at least 3 entity |
| 99 | * replacement per byte of input. |
| 100 | */ |
| 101 | #define XML_PARSER_NON_LINEAR 10 |
| 102 | |
| 103 | /* |
| 104 | * xmlParserEntityCheck |
| 105 | * |
| 106 | * Function to check non-linear entity expansion behaviour |
| 107 | * This is here to detect and stop exponential linear entity expansion |
| 108 | * This is not a limitation of the parser but a safety |
| 109 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 110 | * parser option. |
| 111 | */ |
| 112 | static int |
| 113 | xmlParserEntityCheck(xmlParserCtxtPtr ctxt, unsigned long size, |
| 114 | xmlEntityPtr ent) |
| 115 | { |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 116 | unsigned long consumed = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 117 | |
| 118 | if ((ctxt == NULL) || (ctxt->options & XML_PARSE_HUGE)) |
| 119 | return (0); |
| 120 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 121 | return (1); |
| 122 | if (size != 0) { |
| 123 | /* |
| 124 | * Do the check based on the replacement size of the entity |
| 125 | */ |
| 126 | if (size < XML_PARSER_BIG_ENTITY) |
| 127 | return(0); |
| 128 | |
| 129 | /* |
| 130 | * A limit on the amount of text data reasonably used |
| 131 | */ |
| 132 | if (ctxt->input != NULL) { |
| 133 | consumed = ctxt->input->consumed + |
| 134 | (ctxt->input->cur - ctxt->input->base); |
| 135 | } |
| 136 | consumed += ctxt->sizeentities; |
| 137 | |
| 138 | if ((size < XML_PARSER_NON_LINEAR * consumed) && |
| 139 | (ctxt->nbentities * 3 < XML_PARSER_NON_LINEAR * consumed)) |
| 140 | return (0); |
| 141 | } else if (ent != NULL) { |
| 142 | /* |
| 143 | * use the number of parsed entities in the replacement |
| 144 | */ |
| 145 | size = ent->checked; |
| 146 | |
| 147 | /* |
| 148 | * The amount of data parsed counting entities size only once |
| 149 | */ |
| 150 | if (ctxt->input != NULL) { |
| 151 | consumed = ctxt->input->consumed + |
| 152 | (ctxt->input->cur - ctxt->input->base); |
| 153 | } |
| 154 | consumed += ctxt->sizeentities; |
| 155 | |
| 156 | /* |
| 157 | * Check the density of entities for the amount of data |
| 158 | * knowing an entity reference will take at least 3 bytes |
| 159 | */ |
| 160 | if (size * 3 < consumed * XML_PARSER_NON_LINEAR) |
| 161 | return (0); |
| 162 | } else { |
| 163 | /* |
| 164 | * strange we got no data for checking just return |
| 165 | */ |
| 166 | return (0); |
| 167 | } |
| 168 | |
| 169 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 170 | return (1); |
| 171 | } |
| 172 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 173 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 174 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 175 | * |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 176 | * arbitrary depth limit for the XML documents that we allow to |
| 177 | * process. This is not a limitation of the parser but a safety |
| 178 | * boundary feature. It can be disabled with the XML_PARSE_HUGE |
| 179 | * parser option. |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 180 | */ |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 181 | unsigned int xmlParserMaxDepth = 256; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 182 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 183 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 184 | |
| 185 | #define SAX2 1 |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 186 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 187 | #define XML_PARSER_BUFFER_SIZE 100 |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 188 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 189 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 190 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 191 | * List of XML prefixed PI allowed by W3C specs |
| 192 | */ |
| 193 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 194 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 195 | "xml-stylesheet", |
| 196 | NULL |
| 197 | }; |
| 198 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 200 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 201 | xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 202 | const xmlChar **str); |
| 203 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 204 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 205 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 206 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 207 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 208 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 209 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 210 | static int |
| 211 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, |
| 212 | const char *encoding); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 213 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 214 | static void |
| 215 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 216 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 217 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 218 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 219 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 220 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 221 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 222 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 223 | static int |
| 224 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity); |
| 225 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 226 | /************************************************************************ |
| 227 | * * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 228 | * Some factorized error routines * |
| 229 | * * |
| 230 | ************************************************************************/ |
| 231 | |
| 232 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 233 | * xmlErrAttributeDup: |
| 234 | * @ctxt: an XML parser context |
| 235 | * @prefix: the attribute prefix |
| 236 | * @localname: the attribute localname |
| 237 | * |
| 238 | * Handle a redefinition of attribute error |
| 239 | */ |
| 240 | static void |
| 241 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 242 | const xmlChar * localname) |
| 243 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 244 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 245 | (ctxt->instate == XML_PARSER_EOF)) |
| 246 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 247 | if (ctxt != NULL) |
| 248 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 249 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 250 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 251 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 252 | (const char *) localname, NULL, NULL, 0, 0, |
| 253 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 254 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 255 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 256 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 257 | (const char *) prefix, (const char *) localname, |
| 258 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 259 | localname); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 260 | if (ctxt != NULL) { |
| 261 | ctxt->wellFormed = 0; |
| 262 | if (ctxt->recovery == 0) |
| 263 | ctxt->disableSAX = 1; |
| 264 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * xmlFatalErr: |
| 269 | * @ctxt: an XML parser context |
| 270 | * @error: the error number |
| 271 | * @extra: extra information string |
| 272 | * |
| 273 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 274 | */ |
| 275 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 276 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 277 | { |
| 278 | const char *errmsg; |
| 279 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 280 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 281 | (ctxt->instate == XML_PARSER_EOF)) |
| 282 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 283 | switch (error) { |
| 284 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 285 | errmsg = "CharRef: invalid hexadecimal value\n"; |
| 286 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 287 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 288 | errmsg = "CharRef: invalid decimal value\n"; |
| 289 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 290 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 291 | errmsg = "CharRef: invalid value\n"; |
| 292 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 293 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 294 | errmsg = "internal error"; |
| 295 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 296 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 297 | errmsg = "PEReference at end of document\n"; |
| 298 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 299 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 300 | errmsg = "PEReference in prolog\n"; |
| 301 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 302 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 303 | errmsg = "PEReference in epilog\n"; |
| 304 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 305 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 306 | errmsg = "PEReference: no name\n"; |
| 307 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 308 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 309 | errmsg = "PEReference: expecting ';'\n"; |
| 310 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 311 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 312 | errmsg = "Detected an entity reference loop\n"; |
| 313 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 314 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 315 | errmsg = "EntityValue: \" or ' expected\n"; |
| 316 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 317 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 318 | errmsg = "PEReferences forbidden in internal subset\n"; |
| 319 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 320 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 321 | errmsg = "EntityValue: \" or ' expected\n"; |
| 322 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 323 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 324 | errmsg = "AttValue: \" or ' expected\n"; |
| 325 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 326 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 327 | errmsg = "Unescaped '<' not allowed in attributes values\n"; |
| 328 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 329 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 330 | errmsg = "SystemLiteral \" or ' expected\n"; |
| 331 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 332 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 333 | errmsg = "Unfinished System or Public ID \" or ' expected\n"; |
| 334 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 335 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 336 | errmsg = "Sequence ']]>' not allowed in content\n"; |
| 337 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 338 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 339 | errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; |
| 340 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 341 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 342 | errmsg = "PUBLIC, the Public Identifier is missing\n"; |
| 343 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 344 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 345 | errmsg = "Comment must not contain '--' (double-hyphen)\n"; |
| 346 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 347 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 348 | errmsg = "xmlParsePI : no target name\n"; |
| 349 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 350 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 351 | errmsg = "Invalid PI name\n"; |
| 352 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 353 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 354 | errmsg = "NOTATION: Name expected here\n"; |
| 355 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 356 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 357 | errmsg = "'>' required to close NOTATION declaration\n"; |
| 358 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 359 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 360 | errmsg = "Entity value required\n"; |
| 361 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 362 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 363 | errmsg = "Fragment not allowed"; |
| 364 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 365 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 366 | errmsg = "'(' required to start ATTLIST enumeration\n"; |
| 367 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 368 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 369 | errmsg = "NmToken expected in ATTLIST enumeration\n"; |
| 370 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 371 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 372 | errmsg = "')' required to finish ATTLIST enumeration\n"; |
| 373 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 374 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 375 | errmsg = "MixedContentDecl : '|' or ')*' expected\n"; |
| 376 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 377 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 378 | errmsg = "MixedContentDecl : '#PCDATA' expected\n"; |
| 379 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 380 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 381 | errmsg = "ContentDecl : Name or '(' expected\n"; |
| 382 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 383 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 384 | errmsg = "ContentDecl : ',' '|' or ')' expected\n"; |
| 385 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 386 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 387 | errmsg = |
| 388 | "PEReference: forbidden within markup decl in internal subset\n"; |
| 389 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 390 | case XML_ERR_GT_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 391 | errmsg = "expected '>'\n"; |
| 392 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 393 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 394 | errmsg = "XML conditional section '[' expected\n"; |
| 395 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 396 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 397 | errmsg = "Content error in the external subset\n"; |
| 398 | break; |
| 399 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 400 | errmsg = |
| 401 | "conditional section INCLUDE or IGNORE keyword expected\n"; |
| 402 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 403 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 404 | errmsg = "XML conditional section not closed\n"; |
| 405 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 406 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 407 | errmsg = "Text declaration '<?xml' required\n"; |
| 408 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 409 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 410 | errmsg = "parsing XML declaration: '?>' expected\n"; |
| 411 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 412 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 413 | errmsg = "external parsed entities cannot be standalone\n"; |
| 414 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 415 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 416 | errmsg = "EntityRef: expecting ';'\n"; |
| 417 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 418 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 419 | errmsg = "DOCTYPE improperly terminated\n"; |
| 420 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 421 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 422 | errmsg = "EndTag: '</' not found\n"; |
| 423 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 424 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 425 | errmsg = "expected '='\n"; |
| 426 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 427 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 428 | errmsg = "String not closed expecting \" or '\n"; |
| 429 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 430 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 431 | errmsg = "String not started expecting ' or \"\n"; |
| 432 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 433 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 434 | errmsg = "Invalid XML encoding name\n"; |
| 435 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 436 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 437 | errmsg = "standalone accepts only 'yes' or 'no'\n"; |
| 438 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 439 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 440 | errmsg = "Document is empty\n"; |
| 441 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 442 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 443 | errmsg = "Extra content at the end of the document\n"; |
| 444 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 445 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 446 | errmsg = "chunk is not well balanced\n"; |
| 447 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 448 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 449 | errmsg = "extra content at the end of well balanced chunk\n"; |
| 450 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 451 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 452 | errmsg = "Malformed declaration expecting version\n"; |
| 453 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 454 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 455 | case: |
| 456 | errmsg = "\n"; |
| 457 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 458 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 459 | default: |
| 460 | errmsg = "Unregistered error message\n"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 461 | } |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 462 | if (ctxt != NULL) |
| 463 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 464 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 465 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg, |
| 466 | info); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 467 | if (ctxt != NULL) { |
| 468 | ctxt->wellFormed = 0; |
| 469 | if (ctxt->recovery == 0) |
| 470 | ctxt->disableSAX = 1; |
| 471 | } |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 474 | /** |
| 475 | * xmlFatalErrMsg: |
| 476 | * @ctxt: an XML parser context |
| 477 | * @error: the error number |
| 478 | * @msg: the error message |
| 479 | * |
| 480 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 481 | */ |
| 482 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 483 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 484 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 485 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 486 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 487 | (ctxt->instate == XML_PARSER_EOF)) |
| 488 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 489 | if (ctxt != NULL) |
| 490 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 491 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 492 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 493 | if (ctxt != NULL) { |
| 494 | ctxt->wellFormed = 0; |
| 495 | if (ctxt->recovery == 0) |
| 496 | ctxt->disableSAX = 1; |
| 497 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 501 | * xmlWarningMsg: |
| 502 | * @ctxt: an XML parser context |
| 503 | * @error: the error number |
| 504 | * @msg: the error message |
| 505 | * @str1: extra data |
| 506 | * @str2: extra data |
| 507 | * |
| 508 | * Handle a warning. |
| 509 | */ |
| 510 | static void |
| 511 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 512 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 513 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 514 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 515 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 516 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 517 | (ctxt->instate == XML_PARSER_EOF)) |
| 518 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 519 | if ((ctxt != NULL) && (ctxt->sax != NULL) && |
| 520 | (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 521 | schannel = ctxt->sax->serror; |
| 522 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 523 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 524 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 525 | ctxt, NULL, XML_FROM_PARSER, error, |
| 526 | XML_ERR_WARNING, NULL, 0, |
| 527 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 528 | msg, (const char *) str1, (const char *) str2); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * xmlValidityError: |
| 533 | * @ctxt: an XML parser context |
| 534 | * @error: the error number |
| 535 | * @msg: the error message |
| 536 | * @str1: extra data |
| 537 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 538 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 539 | */ |
| 540 | static void |
| 541 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 542 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 543 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 544 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 545 | |
| 546 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 547 | (ctxt->instate == XML_PARSER_EOF)) |
| 548 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 549 | if (ctxt != NULL) { |
| 550 | ctxt->errNo = error; |
| 551 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
| 552 | schannel = ctxt->sax->serror; |
| 553 | } |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 554 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 555 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 556 | ctxt, NULL, XML_FROM_DTD, error, |
| 557 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 558 | (const char *) str2, NULL, 0, 0, |
| 559 | msg, (const char *) str1, (const char *) str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 560 | if (ctxt != NULL) { |
| 561 | ctxt->valid = 0; |
| 562 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 566 | * xmlFatalErrMsgInt: |
| 567 | * @ctxt: an XML parser context |
| 568 | * @error: the error number |
| 569 | * @msg: the error message |
| 570 | * @val: an integer value |
| 571 | * |
| 572 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 573 | */ |
| 574 | static void |
| 575 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 576 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 577 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 578 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 579 | (ctxt->instate == XML_PARSER_EOF)) |
| 580 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 581 | if (ctxt != NULL) |
| 582 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 583 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 584 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 585 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 586 | if (ctxt != NULL) { |
| 587 | ctxt->wellFormed = 0; |
| 588 | if (ctxt->recovery == 0) |
| 589 | ctxt->disableSAX = 1; |
| 590 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 594 | * xmlFatalErrMsgStrIntStr: |
| 595 | * @ctxt: an XML parser context |
| 596 | * @error: the error number |
| 597 | * @msg: the error message |
| 598 | * @str1: an string info |
| 599 | * @val: an integer value |
| 600 | * @str2: an string info |
| 601 | * |
| 602 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 603 | */ |
| 604 | static void |
| 605 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 606 | const char *msg, const xmlChar *str1, int val, |
| 607 | const xmlChar *str2) |
| 608 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 609 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 610 | (ctxt->instate == XML_PARSER_EOF)) |
| 611 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 612 | if (ctxt != NULL) |
| 613 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 614 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 615 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 616 | NULL, 0, (const char *) str1, (const char *) str2, |
| 617 | NULL, val, 0, msg, str1, val, str2); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 618 | if (ctxt != NULL) { |
| 619 | ctxt->wellFormed = 0; |
| 620 | if (ctxt->recovery == 0) |
| 621 | ctxt->disableSAX = 1; |
| 622 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 626 | * xmlFatalErrMsgStr: |
| 627 | * @ctxt: an XML parser context |
| 628 | * @error: the error number |
| 629 | * @msg: the error message |
| 630 | * @val: a string value |
| 631 | * |
| 632 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 633 | */ |
| 634 | static void |
| 635 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 636 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 637 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 638 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 639 | (ctxt->instate == XML_PARSER_EOF)) |
| 640 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 641 | if (ctxt != NULL) |
| 642 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 643 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 644 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 645 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 646 | val); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 647 | if (ctxt != NULL) { |
| 648 | ctxt->wellFormed = 0; |
| 649 | if (ctxt->recovery == 0) |
| 650 | ctxt->disableSAX = 1; |
| 651 | } |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 655 | * xmlErrMsgStr: |
| 656 | * @ctxt: an XML parser context |
| 657 | * @error: the error number |
| 658 | * @msg: the error message |
| 659 | * @val: a string value |
| 660 | * |
| 661 | * Handle a non fatal parser error |
| 662 | */ |
| 663 | static void |
| 664 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 665 | const char *msg, const xmlChar * val) |
| 666 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 667 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 668 | (ctxt->instate == XML_PARSER_EOF)) |
| 669 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 670 | if (ctxt != NULL) |
| 671 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 672 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 673 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 674 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 675 | val); |
| 676 | } |
| 677 | |
| 678 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 679 | * xmlNsErr: |
| 680 | * @ctxt: an XML parser context |
| 681 | * @error: the error number |
| 682 | * @msg: the message |
| 683 | * @info1: extra information string |
| 684 | * @info2: extra information string |
| 685 | * |
| 686 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 687 | */ |
| 688 | static void |
| 689 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 690 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 691 | const xmlChar * info1, const xmlChar * info2, |
| 692 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 693 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 694 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 695 | (ctxt->instate == XML_PARSER_EOF)) |
| 696 | return; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 697 | if (ctxt != NULL) |
| 698 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 699 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 700 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 701 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 702 | info1, info2, info3); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 703 | if (ctxt != NULL) |
| 704 | ctxt->nsWellFormed = 0; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 707 | /** |
| 708 | * xmlNsWarn |
| 709 | * @ctxt: an XML parser context |
| 710 | * @error: the error number |
| 711 | * @msg: the message |
| 712 | * @info1: extra information string |
| 713 | * @info2: extra information string |
| 714 | * |
| 715 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 716 | */ |
| 717 | static void |
| 718 | xmlNsWarn(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 719 | const char *msg, |
| 720 | const xmlChar * info1, const xmlChar * info2, |
| 721 | const xmlChar * info3) |
| 722 | { |
| 723 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 724 | (ctxt->instate == XML_PARSER_EOF)) |
| 725 | return; |
| 726 | if (ctxt != NULL) |
| 727 | ctxt->errNo = error; |
| 728 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
| 729 | XML_ERR_WARNING, NULL, 0, (const char *) info1, |
| 730 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 731 | info1, info2, info3); |
| 732 | } |
| 733 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 734 | /************************************************************************ |
| 735 | * * |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 736 | * Library wide options * |
| 737 | * * |
| 738 | ************************************************************************/ |
| 739 | |
| 740 | /** |
| 741 | * xmlHasFeature: |
| 742 | * @feature: the feature to be examined |
| 743 | * |
| 744 | * Examines if the library has been compiled with a given feature. |
| 745 | * |
| 746 | * Returns a non-zero value if the feature exist, otherwise zero. |
| 747 | * Returns zero (0) if the feature does not exist or an unknown |
| 748 | * unknown feature is requested, non-zero otherwise. |
| 749 | */ |
| 750 | int |
| 751 | xmlHasFeature(xmlFeature feature) |
| 752 | { |
| 753 | switch (feature) { |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 754 | case XML_WITH_THREAD: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 755 | #ifdef LIBXML_THREAD_ENABLED |
| 756 | return(1); |
| 757 | #else |
| 758 | return(0); |
| 759 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 760 | case XML_WITH_TREE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 761 | #ifdef LIBXML_TREE_ENABLED |
| 762 | return(1); |
| 763 | #else |
| 764 | return(0); |
| 765 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 766 | case XML_WITH_OUTPUT: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 767 | #ifdef LIBXML_OUTPUT_ENABLED |
| 768 | return(1); |
| 769 | #else |
| 770 | return(0); |
| 771 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 772 | case XML_WITH_PUSH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 773 | #ifdef LIBXML_PUSH_ENABLED |
| 774 | return(1); |
| 775 | #else |
| 776 | return(0); |
| 777 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 778 | case XML_WITH_READER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 779 | #ifdef LIBXML_READER_ENABLED |
| 780 | return(1); |
| 781 | #else |
| 782 | return(0); |
| 783 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 784 | case XML_WITH_PATTERN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 785 | #ifdef LIBXML_PATTERN_ENABLED |
| 786 | return(1); |
| 787 | #else |
| 788 | return(0); |
| 789 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 790 | case XML_WITH_WRITER: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 791 | #ifdef LIBXML_WRITER_ENABLED |
| 792 | return(1); |
| 793 | #else |
| 794 | return(0); |
| 795 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 796 | case XML_WITH_SAX1: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 797 | #ifdef LIBXML_SAX1_ENABLED |
| 798 | return(1); |
| 799 | #else |
| 800 | return(0); |
| 801 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 802 | case XML_WITH_FTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 803 | #ifdef LIBXML_FTP_ENABLED |
| 804 | return(1); |
| 805 | #else |
| 806 | return(0); |
| 807 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 808 | case XML_WITH_HTTP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 809 | #ifdef LIBXML_HTTP_ENABLED |
| 810 | return(1); |
| 811 | #else |
| 812 | return(0); |
| 813 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 814 | case XML_WITH_VALID: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 815 | #ifdef LIBXML_VALID_ENABLED |
| 816 | return(1); |
| 817 | #else |
| 818 | return(0); |
| 819 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 820 | case XML_WITH_HTML: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 821 | #ifdef LIBXML_HTML_ENABLED |
| 822 | return(1); |
| 823 | #else |
| 824 | return(0); |
| 825 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 826 | case XML_WITH_LEGACY: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 827 | #ifdef LIBXML_LEGACY_ENABLED |
| 828 | return(1); |
| 829 | #else |
| 830 | return(0); |
| 831 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 832 | case XML_WITH_C14N: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 833 | #ifdef LIBXML_C14N_ENABLED |
| 834 | return(1); |
| 835 | #else |
| 836 | return(0); |
| 837 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 838 | case XML_WITH_CATALOG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 839 | #ifdef LIBXML_CATALOG_ENABLED |
| 840 | return(1); |
| 841 | #else |
| 842 | return(0); |
| 843 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 844 | case XML_WITH_XPATH: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 845 | #ifdef LIBXML_XPATH_ENABLED |
| 846 | return(1); |
| 847 | #else |
| 848 | return(0); |
| 849 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 850 | case XML_WITH_XPTR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 851 | #ifdef LIBXML_XPTR_ENABLED |
| 852 | return(1); |
| 853 | #else |
| 854 | return(0); |
| 855 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 856 | case XML_WITH_XINCLUDE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 857 | #ifdef LIBXML_XINCLUDE_ENABLED |
| 858 | return(1); |
| 859 | #else |
| 860 | return(0); |
| 861 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 862 | case XML_WITH_ICONV: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 863 | #ifdef LIBXML_ICONV_ENABLED |
| 864 | return(1); |
| 865 | #else |
| 866 | return(0); |
| 867 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 868 | case XML_WITH_ISO8859X: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 869 | #ifdef LIBXML_ISO8859X_ENABLED |
| 870 | return(1); |
| 871 | #else |
| 872 | return(0); |
| 873 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 874 | case XML_WITH_UNICODE: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 875 | #ifdef LIBXML_UNICODE_ENABLED |
| 876 | return(1); |
| 877 | #else |
| 878 | return(0); |
| 879 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 880 | case XML_WITH_REGEXP: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 881 | #ifdef LIBXML_REGEXP_ENABLED |
| 882 | return(1); |
| 883 | #else |
| 884 | return(0); |
| 885 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 886 | case XML_WITH_AUTOMATA: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 887 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 888 | return(1); |
| 889 | #else |
| 890 | return(0); |
| 891 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 892 | case XML_WITH_EXPR: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 893 | #ifdef LIBXML_EXPR_ENABLED |
| 894 | return(1); |
| 895 | #else |
| 896 | return(0); |
| 897 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 898 | case XML_WITH_SCHEMAS: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 899 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 900 | return(1); |
| 901 | #else |
| 902 | return(0); |
| 903 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 904 | case XML_WITH_SCHEMATRON: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 905 | #ifdef LIBXML_SCHEMATRON_ENABLED |
| 906 | return(1); |
| 907 | #else |
| 908 | return(0); |
| 909 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 910 | case XML_WITH_MODULES: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 911 | #ifdef LIBXML_MODULES_ENABLED |
| 912 | return(1); |
| 913 | #else |
| 914 | return(0); |
| 915 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 916 | case XML_WITH_DEBUG: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 917 | #ifdef LIBXML_DEBUG_ENABLED |
| 918 | return(1); |
| 919 | #else |
| 920 | return(0); |
| 921 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 922 | case XML_WITH_DEBUG_MEM: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 923 | #ifdef DEBUG_MEMORY_LOCATION |
| 924 | return(1); |
| 925 | #else |
| 926 | return(0); |
| 927 | #endif |
Daniel Veillard | 602434d | 2005-09-12 09:20:31 +0000 | [diff] [blame] | 928 | case XML_WITH_DEBUG_RUN: |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 929 | #ifdef LIBXML_DEBUG_RUNTIME |
| 930 | return(1); |
| 931 | #else |
| 932 | return(0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 933 | #endif |
Daniel Veillard | 75acfee | 2006-07-13 06:29:56 +0000 | [diff] [blame] | 934 | case XML_WITH_ZLIB: |
| 935 | #ifdef LIBXML_ZLIB_ENABLED |
| 936 | return(1); |
| 937 | #else |
| 938 | return(0); |
| 939 | #endif |
Daniel Veillard | 0bcc7f6 | 2005-09-04 21:39:03 +0000 | [diff] [blame] | 940 | default: |
| 941 | break; |
| 942 | } |
| 943 | return(0); |
| 944 | } |
| 945 | |
| 946 | /************************************************************************ |
| 947 | * * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 948 | * SAX2 defaulted attributes handling * |
| 949 | * * |
| 950 | ************************************************************************/ |
| 951 | |
| 952 | /** |
| 953 | * xmlDetectSAX2: |
| 954 | * @ctxt: an XML parser context |
| 955 | * |
| 956 | * Do the SAX2 detection and specific intialization |
| 957 | */ |
| 958 | static void |
| 959 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 960 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 961 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 962 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 963 | ((ctxt->sax->startElementNs != NULL) || |
| 964 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 965 | #else |
| 966 | ctxt->sax2 = 1; |
| 967 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 968 | |
| 969 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 970 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 971 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 972 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 973 | (ctxt->str_xml_ns == NULL)) { |
| 974 | xmlErrMemory(ctxt, NULL); |
| 975 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 978 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 979 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 980 | struct _xmlDefAttrs { |
| 981 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 982 | int maxAttrs; /* the size of the array */ |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 983 | const xmlChar *values[5]; /* array of localname/prefix/values/external */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 984 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 985 | |
| 986 | /** |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 987 | * xmlAttrNormalizeSpace: |
| 988 | * @src: the source string |
| 989 | * @dst: the target string |
| 990 | * |
| 991 | * Normalize the space in non CDATA attribute values: |
| 992 | * If the attribute type is not CDATA, then the XML processor MUST further |
| 993 | * process the normalized attribute value by discarding any leading and |
| 994 | * trailing space (#x20) characters, and by replacing sequences of space |
| 995 | * (#x20) characters by a single space (#x20) character. |
| 996 | * Note that the size of dst need to be at least src, and if one doesn't need |
| 997 | * to preserve dst (and it doesn't come from a dictionary or read-only) then |
| 998 | * passing src as dst is just fine. |
| 999 | * |
| 1000 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1001 | * is needed. |
| 1002 | */ |
| 1003 | static xmlChar * |
| 1004 | xmlAttrNormalizeSpace(const xmlChar *src, xmlChar *dst) |
| 1005 | { |
| 1006 | if ((src == NULL) || (dst == NULL)) |
| 1007 | return(NULL); |
| 1008 | |
| 1009 | while (*src == 0x20) src++; |
| 1010 | while (*src != 0) { |
| 1011 | if (*src == 0x20) { |
| 1012 | while (*src == 0x20) src++; |
| 1013 | if (*src != 0) |
| 1014 | *dst++ = 0x20; |
| 1015 | } else { |
| 1016 | *dst++ = *src++; |
| 1017 | } |
| 1018 | } |
| 1019 | *dst = 0; |
| 1020 | if (dst == src) |
| 1021 | return(NULL); |
| 1022 | return(dst); |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * xmlAttrNormalizeSpace2: |
| 1027 | * @src: the source string |
| 1028 | * |
| 1029 | * Normalize the space in non CDATA attribute values, a slightly more complex |
| 1030 | * front end to avoid allocation problems when running on attribute values |
| 1031 | * coming from the input. |
| 1032 | * |
| 1033 | * Returns a pointer to the normalized value (dst) or NULL if no conversion |
| 1034 | * is needed. |
| 1035 | */ |
| 1036 | static const xmlChar * |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1037 | xmlAttrNormalizeSpace2(xmlParserCtxtPtr ctxt, xmlChar *src, int *len) |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1038 | { |
| 1039 | int i; |
| 1040 | int remove_head = 0; |
| 1041 | int need_realloc = 0; |
| 1042 | const xmlChar *cur; |
| 1043 | |
| 1044 | if ((ctxt == NULL) || (src == NULL) || (len == NULL)) |
| 1045 | return(NULL); |
| 1046 | i = *len; |
| 1047 | if (i <= 0) |
| 1048 | return(NULL); |
| 1049 | |
| 1050 | cur = src; |
| 1051 | while (*cur == 0x20) { |
| 1052 | cur++; |
| 1053 | remove_head++; |
| 1054 | } |
| 1055 | while (*cur != 0) { |
| 1056 | if (*cur == 0x20) { |
| 1057 | cur++; |
| 1058 | if ((*cur == 0x20) || (*cur == 0)) { |
| 1059 | need_realloc = 1; |
| 1060 | break; |
| 1061 | } |
| 1062 | } else |
| 1063 | cur++; |
| 1064 | } |
| 1065 | if (need_realloc) { |
| 1066 | xmlChar *ret; |
| 1067 | |
| 1068 | ret = xmlStrndup(src + remove_head, i - remove_head + 1); |
| 1069 | if (ret == NULL) { |
| 1070 | xmlErrMemory(ctxt, NULL); |
| 1071 | return(NULL); |
| 1072 | } |
| 1073 | xmlAttrNormalizeSpace(ret, ret); |
| 1074 | *len = (int) strlen((const char *)ret); |
| 1075 | return(ret); |
| 1076 | } else if (remove_head) { |
| 1077 | *len -= remove_head; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1078 | memmove(src, src + remove_head, 1 + *len); |
| 1079 | return(src); |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 1080 | } |
| 1081 | return(NULL); |
| 1082 | } |
| 1083 | |
| 1084 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1085 | * xmlAddDefAttrs: |
| 1086 | * @ctxt: an XML parser context |
| 1087 | * @fullname: the element fullname |
| 1088 | * @fullattr: the attribute fullname |
| 1089 | * @value: the attribute value |
| 1090 | * |
| 1091 | * Add a defaulted attribute for an element |
| 1092 | */ |
| 1093 | static void |
| 1094 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 1095 | const xmlChar *fullname, |
| 1096 | const xmlChar *fullattr, |
| 1097 | const xmlChar *value) { |
| 1098 | xmlDefAttrsPtr defaults; |
| 1099 | int len; |
| 1100 | const xmlChar *name; |
| 1101 | const xmlChar *prefix; |
| 1102 | |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 1103 | /* |
| 1104 | * Allows to detect attribute redefinitions |
| 1105 | */ |
| 1106 | if (ctxt->attsSpecial != NULL) { |
| 1107 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1108 | return; |
| 1109 | } |
| 1110 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1111 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1112 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1113 | if (ctxt->attsDefault == NULL) |
| 1114 | goto mem_error; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1118 | * split the element name into prefix:localname , the string found |
| 1119 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1120 | */ |
| 1121 | name = xmlSplitQName3(fullname, &len); |
| 1122 | if (name == NULL) { |
| 1123 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 1124 | prefix = NULL; |
| 1125 | } else { |
| 1126 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1127 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * make sure there is some storage |
| 1132 | */ |
| 1133 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 1134 | if (defaults == NULL) { |
| 1135 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1136 | (4 * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1137 | if (defaults == NULL) |
| 1138 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1139 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1140 | defaults->maxAttrs = 4; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1141 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1142 | defaults, NULL) < 0) { |
| 1143 | xmlFree(defaults); |
| 1144 | goto mem_error; |
| 1145 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1146 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1147 | xmlDefAttrsPtr temp; |
| 1148 | |
| 1149 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1150 | (2 * defaults->maxAttrs * 5) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1151 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1152 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 1153 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1154 | defaults->maxAttrs *= 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 1155 | if (xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, |
| 1156 | defaults, NULL) < 0) { |
| 1157 | xmlFree(defaults); |
| 1158 | goto mem_error; |
| 1159 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 1163 | * Split the element name into prefix:localname , the string found |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1164 | * are within the DTD and hen not associated to namespace names. |
| 1165 | */ |
| 1166 | name = xmlSplitQName3(fullattr, &len); |
| 1167 | if (name == NULL) { |
| 1168 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 1169 | prefix = NULL; |
| 1170 | } else { |
| 1171 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 1172 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 1173 | } |
| 1174 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1175 | defaults->values[5 * defaults->nbAttrs] = name; |
| 1176 | defaults->values[5 * defaults->nbAttrs + 1] = prefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1177 | /* intern the string and precompute the end */ |
| 1178 | len = xmlStrlen(value); |
| 1179 | value = xmlDictLookup(ctxt->dict, value, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 1180 | defaults->values[5 * defaults->nbAttrs + 2] = value; |
| 1181 | defaults->values[5 * defaults->nbAttrs + 3] = value + len; |
| 1182 | if (ctxt->external) |
| 1183 | defaults->values[5 * defaults->nbAttrs + 4] = BAD_CAST "external"; |
| 1184 | else |
| 1185 | defaults->values[5 * defaults->nbAttrs + 4] = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1186 | defaults->nbAttrs++; |
| 1187 | |
| 1188 | return; |
| 1189 | |
| 1190 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1191 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
| 1194 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1195 | /** |
| 1196 | * xmlAddSpecialAttr: |
| 1197 | * @ctxt: an XML parser context |
| 1198 | * @fullname: the element fullname |
| 1199 | * @fullattr: the attribute fullname |
| 1200 | * @type: the attribute type |
| 1201 | * |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1202 | * Register this attribute type |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1203 | */ |
| 1204 | static void |
| 1205 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 1206 | const xmlChar *fullname, |
| 1207 | const xmlChar *fullattr, |
| 1208 | int type) |
| 1209 | { |
| 1210 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 1211 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1212 | if (ctxt->attsSpecial == NULL) |
| 1213 | goto mem_error; |
| 1214 | } |
| 1215 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1216 | if (xmlHashLookup2(ctxt->attsSpecial, fullname, fullattr) != NULL) |
| 1217 | return; |
| 1218 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1219 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 1220 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1221 | return; |
| 1222 | |
| 1223 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1224 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1225 | return; |
| 1226 | } |
| 1227 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1228 | /** |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1229 | * xmlCleanSpecialAttrCallback: |
| 1230 | * |
| 1231 | * Removes CDATA attributes from the special attribute table |
| 1232 | */ |
| 1233 | static void |
| 1234 | xmlCleanSpecialAttrCallback(void *payload, void *data, |
| 1235 | const xmlChar *fullname, const xmlChar *fullattr, |
| 1236 | const xmlChar *unused ATTRIBUTE_UNUSED) { |
| 1237 | xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) data; |
| 1238 | |
Daniel Veillard | b3edafd | 2008-01-11 08:00:57 +0000 | [diff] [blame] | 1239 | if (((long) payload) == XML_ATTRIBUTE_CDATA) { |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 1240 | xmlHashRemoveEntry2(ctxt->attsSpecial, fullname, fullattr, NULL); |
| 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | /** |
| 1245 | * xmlCleanSpecialAttr: |
| 1246 | * @ctxt: an XML parser context |
| 1247 | * |
| 1248 | * Trim the list of attributes defined to remove all those of type |
| 1249 | * CDATA as they are not special. This call should be done when finishing |
| 1250 | * to parse the DTD and before starting to parse the document root. |
| 1251 | */ |
| 1252 | static void |
| 1253 | xmlCleanSpecialAttr(xmlParserCtxtPtr ctxt) |
| 1254 | { |
| 1255 | if (ctxt->attsSpecial == NULL) |
| 1256 | return; |
| 1257 | |
| 1258 | xmlHashScanFull(ctxt->attsSpecial, xmlCleanSpecialAttrCallback, ctxt); |
| 1259 | |
| 1260 | if (xmlHashSize(ctxt->attsSpecial) == 0) { |
| 1261 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 1262 | ctxt->attsSpecial = NULL; |
| 1263 | } |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | /** |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 1268 | * xmlCheckLanguageID: |
| 1269 | * @lang: pointer to the string value |
| 1270 | * |
| 1271 | * Checks that the value conforms to the LanguageID production: |
| 1272 | * |
| 1273 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 1274 | * the XML Second edition. |
| 1275 | * |
| 1276 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 1277 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 1278 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 1279 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 1280 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 1281 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 1282 | * |
| 1283 | * Returns 1 if correct 0 otherwise |
| 1284 | **/ |
| 1285 | int |
| 1286 | xmlCheckLanguageID(const xmlChar * lang) |
| 1287 | { |
| 1288 | const xmlChar *cur = lang; |
| 1289 | |
| 1290 | if (cur == NULL) |
| 1291 | return (0); |
| 1292 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
| 1293 | ((cur[0] == 'I') && (cur[1] == '-'))) { |
| 1294 | /* |
| 1295 | * IANA code |
| 1296 | */ |
| 1297 | cur += 2; |
| 1298 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 1299 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1300 | cur++; |
| 1301 | } else if (((cur[0] == 'x') && (cur[1] == '-')) || |
| 1302 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
| 1303 | /* |
| 1304 | * User code |
| 1305 | */ |
| 1306 | cur += 2; |
| 1307 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 1308 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1309 | cur++; |
| 1310 | } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 1311 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) { |
| 1312 | /* |
| 1313 | * ISO639 |
| 1314 | */ |
| 1315 | cur++; |
| 1316 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 1317 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1318 | cur++; |
| 1319 | else |
| 1320 | return (0); |
| 1321 | } else |
| 1322 | return (0); |
| 1323 | while (cur[0] != 0) { /* non input consuming */ |
| 1324 | if (cur[0] != '-') |
| 1325 | return (0); |
| 1326 | cur++; |
| 1327 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 1328 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1329 | cur++; |
| 1330 | else |
| 1331 | return (0); |
| 1332 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 1333 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 1334 | cur++; |
| 1335 | } |
| 1336 | return (1); |
| 1337 | } |
| 1338 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1339 | /************************************************************************ |
| 1340 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 1341 | * Parser stacks related functions and macros * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1342 | * * |
| 1343 | ************************************************************************/ |
| 1344 | |
| 1345 | xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 1346 | const xmlChar ** str); |
| 1347 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1348 | #ifdef SAX2 |
| 1349 | /** |
| 1350 | * nsPush: |
| 1351 | * @ctxt: an XML parser context |
| 1352 | * @prefix: the namespace prefix or NULL |
| 1353 | * @URL: the namespace name |
| 1354 | * |
| 1355 | * Pushes a new parser namespace on top of the ns stack |
| 1356 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 1357 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 1358 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1359 | */ |
| 1360 | static int |
| 1361 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 1362 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 1363 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 1364 | int i; |
| 1365 | for (i = 0;i < ctxt->nsNr;i += 2) { |
| 1366 | if (ctxt->nsTab[i] == prefix) { |
| 1367 | /* in scope */ |
| 1368 | if (ctxt->nsTab[i + 1] == URL) |
| 1369 | return(-2); |
| 1370 | /* out of scope keep it */ |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1375 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 1376 | ctxt->nsMax = 10; |
| 1377 | ctxt->nsNr = 0; |
| 1378 | ctxt->nsTab = (const xmlChar **) |
| 1379 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 1380 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1381 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1382 | ctxt->nsMax = 0; |
| 1383 | return (-1); |
| 1384 | } |
| 1385 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1386 | const xmlChar ** tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1387 | ctxt->nsMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1388 | tmp = (const xmlChar **) xmlRealloc((char *) ctxt->nsTab, |
| 1389 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 1390 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1391 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1392 | ctxt->nsMax /= 2; |
| 1393 | return (-1); |
| 1394 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1395 | ctxt->nsTab = tmp; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1396 | } |
| 1397 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 1398 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 1399 | return (ctxt->nsNr); |
| 1400 | } |
| 1401 | /** |
| 1402 | * nsPop: |
| 1403 | * @ctxt: an XML parser context |
| 1404 | * @nr: the number to pop |
| 1405 | * |
| 1406 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 1407 | * |
| 1408 | * Returns the number of namespaces removed |
| 1409 | */ |
| 1410 | static int |
| 1411 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 1412 | { |
| 1413 | int i; |
| 1414 | |
| 1415 | if (ctxt->nsTab == NULL) return(0); |
| 1416 | if (ctxt->nsNr < nr) { |
| 1417 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 1418 | nr = ctxt->nsNr; |
| 1419 | } |
| 1420 | if (ctxt->nsNr <= 0) |
| 1421 | return (0); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 1422 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1423 | for (i = 0;i < nr;i++) { |
| 1424 | ctxt->nsNr--; |
| 1425 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 1426 | } |
| 1427 | return(nr); |
| 1428 | } |
| 1429 | #endif |
| 1430 | |
| 1431 | static int |
| 1432 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 1433 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1434 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1435 | int maxatts; |
| 1436 | |
| 1437 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1438 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1439 | atts = (const xmlChar **) |
| 1440 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1441 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1442 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1443 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 1444 | if (attallocs == NULL) goto mem_error; |
| 1445 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1446 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1447 | } else if (nr + 5 > ctxt->maxatts) { |
| 1448 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1449 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 1450 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1451 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1452 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1453 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 1454 | (maxatts / 5) * sizeof(int)); |
| 1455 | if (attallocs == NULL) goto mem_error; |
| 1456 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1457 | ctxt->maxatts = maxatts; |
| 1458 | } |
| 1459 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1460 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1461 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1462 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1465 | /** |
| 1466 | * inputPush: |
| 1467 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1468 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1469 | * |
| 1470 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1471 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1472 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1473 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1474 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1475 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 1476 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1477 | if ((ctxt == NULL) || (value == NULL)) |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1478 | return(-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1479 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 1480 | ctxt->inputMax *= 2; |
| 1481 | ctxt->inputTab = |
| 1482 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 1483 | ctxt->inputMax * |
| 1484 | sizeof(ctxt->inputTab[0])); |
| 1485 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1486 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1487 | xmlFreeInputStream(value); |
| 1488 | ctxt->inputMax /= 2; |
| 1489 | value = NULL; |
| 1490 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | ctxt->inputTab[ctxt->inputNr] = value; |
| 1494 | ctxt->input = value; |
| 1495 | return (ctxt->inputNr++); |
| 1496 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1497 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1498 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1499 | * @ctxt: an XML parser context |
| 1500 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1501 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1502 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1503 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1504 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1505 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1506 | inputPop(xmlParserCtxtPtr ctxt) |
| 1507 | { |
| 1508 | xmlParserInputPtr ret; |
| 1509 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 1510 | if (ctxt == NULL) |
| 1511 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1512 | if (ctxt->inputNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1513 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1514 | ctxt->inputNr--; |
| 1515 | if (ctxt->inputNr > 0) |
| 1516 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 1517 | else |
| 1518 | ctxt->input = NULL; |
| 1519 | ret = ctxt->inputTab[ctxt->inputNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1520 | ctxt->inputTab[ctxt->inputNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1521 | return (ret); |
| 1522 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1523 | /** |
| 1524 | * nodePush: |
| 1525 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1526 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1527 | * |
| 1528 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1529 | * |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1530 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1531 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1532 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1533 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1534 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1535 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1536 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1537 | xmlNodePtr *tmp; |
| 1538 | |
| 1539 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1540 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1541 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1542 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1543 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1544 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1545 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1546 | ctxt->nodeTab = tmp; |
| 1547 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1548 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1549 | if ((((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) && |
| 1550 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1551 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1552 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1553 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1554 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1555 | return(-1); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1556 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1557 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1558 | ctxt->node = value; |
| 1559 | return (ctxt->nodeNr++); |
| 1560 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 1561 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1562 | /** |
| 1563 | * nodePop: |
| 1564 | * @ctxt: an XML parser context |
| 1565 | * |
| 1566 | * Pops the top element node from the node stack |
| 1567 | * |
| 1568 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1570 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1571 | nodePop(xmlParserCtxtPtr ctxt) |
| 1572 | { |
| 1573 | xmlNodePtr ret; |
| 1574 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1575 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1576 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1577 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1578 | ctxt->nodeNr--; |
| 1579 | if (ctxt->nodeNr > 0) |
| 1580 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1581 | else |
| 1582 | ctxt->node = NULL; |
| 1583 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1584 | ctxt->nodeTab[ctxt->nodeNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1585 | return (ret); |
| 1586 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1587 | |
| 1588 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1589 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1590 | * nameNsPush: |
| 1591 | * @ctxt: an XML parser context |
| 1592 | * @value: the element name |
| 1593 | * @prefix: the element prefix |
| 1594 | * @URI: the element namespace name |
| 1595 | * |
| 1596 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1597 | * |
| 1598 | * Returns -1 in case of error, the index in the stack otherwise |
| 1599 | */ |
| 1600 | static int |
| 1601 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1602 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1603 | { |
| 1604 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1605 | const xmlChar * *tmp; |
| 1606 | void **tmp2; |
| 1607 | ctxt->nameMax *= 2; |
| 1608 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1609 | ctxt->nameMax * |
| 1610 | sizeof(ctxt->nameTab[0])); |
| 1611 | if (tmp == NULL) { |
| 1612 | ctxt->nameMax /= 2; |
| 1613 | goto mem_error; |
| 1614 | } |
| 1615 | ctxt->nameTab = tmp; |
| 1616 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1617 | ctxt->nameMax * 3 * |
| 1618 | sizeof(ctxt->pushTab[0])); |
| 1619 | if (tmp2 == NULL) { |
| 1620 | ctxt->nameMax /= 2; |
| 1621 | goto mem_error; |
| 1622 | } |
| 1623 | ctxt->pushTab = tmp2; |
| 1624 | } |
| 1625 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1626 | ctxt->name = value; |
| 1627 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1628 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1629 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1630 | return (ctxt->nameNr++); |
| 1631 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1632 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1633 | return (-1); |
| 1634 | } |
| 1635 | /** |
| 1636 | * nameNsPop: |
| 1637 | * @ctxt: an XML parser context |
| 1638 | * |
| 1639 | * Pops the top element/prefix/URI name from the name stack |
| 1640 | * |
| 1641 | * Returns the name just removed |
| 1642 | */ |
| 1643 | static const xmlChar * |
| 1644 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1645 | { |
| 1646 | const xmlChar *ret; |
| 1647 | |
| 1648 | if (ctxt->nameNr <= 0) |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1649 | return (NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1650 | ctxt->nameNr--; |
| 1651 | if (ctxt->nameNr > 0) |
| 1652 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1653 | else |
| 1654 | ctxt->name = NULL; |
| 1655 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1656 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1657 | return (ret); |
| 1658 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1659 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1660 | |
| 1661 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1662 | * namePush: |
| 1663 | * @ctxt: an XML parser context |
| 1664 | * @value: the element name |
| 1665 | * |
| 1666 | * Pushes a new element name on top of the name stack |
| 1667 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1668 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1669 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1670 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1671 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1672 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1673 | if (ctxt == NULL) return (-1); |
| 1674 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1675 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1676 | const xmlChar * *tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1677 | ctxt->nameMax *= 2; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1678 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1679 | ctxt->nameMax * |
| 1680 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1681 | if (tmp == NULL) { |
| 1682 | ctxt->nameMax /= 2; |
| 1683 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1684 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1685 | ctxt->nameTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1686 | } |
| 1687 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1688 | ctxt->name = value; |
| 1689 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1690 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1691 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1692 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1693 | } |
| 1694 | /** |
| 1695 | * namePop: |
| 1696 | * @ctxt: an XML parser context |
| 1697 | * |
| 1698 | * Pops the top element name from the name stack |
| 1699 | * |
| 1700 | * Returns the name just removed |
| 1701 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1702 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1703 | namePop(xmlParserCtxtPtr ctxt) |
| 1704 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1705 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1706 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1707 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1708 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1709 | ctxt->nameNr--; |
| 1710 | if (ctxt->nameNr > 0) |
| 1711 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1712 | else |
| 1713 | ctxt->name = NULL; |
| 1714 | ret = ctxt->nameTab[ctxt->nameNr]; |
Daniel Veillard | 24505b0 | 2005-07-28 23:49:35 +0000 | [diff] [blame] | 1715 | ctxt->nameTab[ctxt->nameNr] = NULL; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1716 | return (ret); |
| 1717 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1718 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1719 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1720 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1721 | int *tmp; |
| 1722 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1723 | ctxt->spaceMax *= 2; |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1724 | tmp = (int *) xmlRealloc(ctxt->spaceTab, |
| 1725 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1726 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1727 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1728 | ctxt->spaceMax /=2; |
| 1729 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1730 | } |
Daniel Veillard | a4ba09d | 2008-04-03 06:24:04 +0000 | [diff] [blame] | 1731 | ctxt->spaceTab = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1732 | } |
| 1733 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1734 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1735 | return(ctxt->spaceNr++); |
| 1736 | } |
| 1737 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1738 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1739 | int ret; |
| 1740 | if (ctxt->spaceNr <= 0) return(0); |
| 1741 | ctxt->spaceNr--; |
| 1742 | if (ctxt->spaceNr > 0) |
| 1743 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1744 | else |
Daniel Veillard | dbcbbd2 | 2006-06-18 19:55:20 +0000 | [diff] [blame] | 1745 | ctxt->space = &ctxt->spaceTab[0]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1746 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1747 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1748 | return(ret); |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Macros for accessing the content. Those should be used only by the parser, |
| 1753 | * and not exported. |
| 1754 | * |
| 1755 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1756 | * use them |
| 1757 | * |
| 1758 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1759 | * To be used with extreme caution since operations consuming |
| 1760 | * characters may move the input buffer to a different location ! |
| 1761 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1762 | * This should be used internally by the parser |
| 1763 | * only to compare to ASCII values otherwise it would break when |
| 1764 | * running with UTF-8 encoding. |
| 1765 | * RAW same as CUR but in the input buffer, bypass any token |
| 1766 | * extraction that may have been done |
| 1767 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1768 | * to compare on ASCII based substring. |
| 1769 | * 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] | 1770 | * strings without newlines within the parser. |
| 1771 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
| 1772 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1773 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1774 | * |
| 1775 | * NEXT Skip to the next character, this does the proper decoding |
| 1776 | * 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] | 1777 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1778 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1779 | * to the number of xmlChars used for the encoding [0-5]. |
| 1780 | * CUR_SCHAR same but operate on a string instead of the context |
| 1781 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1782 | * the index |
| 1783 | * GROW, SHRINK handling of input buffers |
| 1784 | */ |
| 1785 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1786 | #define RAW (*ctxt->input->cur) |
| 1787 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1788 | #define NXT(val) ctxt->input->cur[(val)] |
| 1789 | #define CUR_PTR ctxt->input->cur |
| 1790 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1791 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1792 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1793 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1794 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1795 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1796 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1797 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1798 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1799 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1800 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1801 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1802 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1803 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1804 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1805 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1806 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 1807 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 1808 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1809 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1810 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1811 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1812 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1813 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1814 | xmlPopInput(ctxt); \ |
| 1815 | } while (0) |
| 1816 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1817 | #define SKIPL(val) do { \ |
| 1818 | int skipl; \ |
| 1819 | for(skipl=0; skipl<val; skipl++) { \ |
| 1820 | if (*(ctxt->input->cur) == '\n') { \ |
| 1821 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1822 | } else ctxt->input->col++; \ |
| 1823 | ctxt->nbChars++; \ |
| 1824 | ctxt->input->cur++; \ |
| 1825 | } \ |
| 1826 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 1827 | if ((*ctxt->input->cur == 0) && \ |
| 1828 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1829 | xmlPopInput(ctxt); \ |
| 1830 | } while (0) |
| 1831 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1832 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1833 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 1834 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1835 | xmlSHRINK (ctxt); |
| 1836 | |
| 1837 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 1838 | xmlParserInputShrink(ctxt->input); |
| 1839 | if ((*ctxt->input->cur == 0) && |
| 1840 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1841 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1842 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1843 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1844 | #define GROW if ((ctxt->progressive == 0) && \ |
| 1845 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1846 | xmlGROW (ctxt); |
| 1847 | |
| 1848 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
| 1849 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1850 | if ((*ctxt->input->cur == 0) && |
| 1851 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1852 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1853 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1854 | |
| 1855 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 1856 | |
| 1857 | #define NEXT xmlNextChar(ctxt) |
| 1858 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1859 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1860 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1861 | ctxt->input->cur++; \ |
| 1862 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1863 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1864 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 1865 | } |
| 1866 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1867 | #define NEXTL(l) do { \ |
| 1868 | if (*(ctxt->input->cur) == '\n') { \ |
| 1869 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1870 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1871 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1872 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1873 | } while (0) |
| 1874 | |
| 1875 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 1876 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 1877 | |
| 1878 | #define COPY_BUF(l,b,i,v) \ |
| 1879 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1880 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1881 | |
| 1882 | /** |
| 1883 | * xmlSkipBlankChars: |
| 1884 | * @ctxt: the XML parser context |
| 1885 | * |
| 1886 | * skip all blanks character found at that point in the input streams. |
| 1887 | * It pops up finished entities in the process if allowable at that point. |
| 1888 | * |
| 1889 | * Returns the number of space chars skipped |
| 1890 | */ |
| 1891 | |
| 1892 | int |
| 1893 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1894 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | |
| 1896 | /* |
| 1897 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 1898 | * the ASCII range. |
| 1899 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1900 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 1901 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1902 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1903 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1904 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1905 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1906 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1907 | if (*cur == '\n') { |
| 1908 | ctxt->input->line++; ctxt->input->col = 1; |
| 1909 | } |
| 1910 | cur++; |
| 1911 | res++; |
| 1912 | if (*cur == 0) { |
| 1913 | ctxt->input->cur = cur; |
| 1914 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1915 | cur = ctxt->input->cur; |
| 1916 | } |
| 1917 | } |
| 1918 | ctxt->input->cur = cur; |
| 1919 | } else { |
| 1920 | int cur; |
| 1921 | do { |
| 1922 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 1923 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1924 | NEXT; |
| 1925 | cur = CUR; |
| 1926 | res++; |
| 1927 | } |
| 1928 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 1929 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 1930 | xmlPopInput(ctxt); |
| 1931 | cur = CUR; |
| 1932 | } |
| 1933 | /* |
| 1934 | * Need to handle support of entities branching here |
| 1935 | */ |
| 1936 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 1937 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 1938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1939 | return(res); |
| 1940 | } |
| 1941 | |
| 1942 | /************************************************************************ |
| 1943 | * * |
| 1944 | * Commodity functions to handle entities * |
| 1945 | * * |
| 1946 | ************************************************************************/ |
| 1947 | |
| 1948 | /** |
| 1949 | * xmlPopInput: |
| 1950 | * @ctxt: an XML parser context |
| 1951 | * |
| 1952 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 1953 | * pop it and return the next char. |
| 1954 | * |
| 1955 | * Returns the current xmlChar in the parser context |
| 1956 | */ |
| 1957 | xmlChar |
| 1958 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1959 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1960 | if (xmlParserDebugEntities) |
| 1961 | xmlGenericError(xmlGenericErrorContext, |
| 1962 | "Popping input %d\n", ctxt->inputNr); |
| 1963 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1964 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1965 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1966 | return(xmlPopInput(ctxt)); |
| 1967 | return(CUR); |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * xmlPushInput: |
| 1972 | * @ctxt: an XML parser context |
| 1973 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 1974 | * |
| 1975 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 1976 | * of the previous one(s). |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1977 | * 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] | 1978 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1979 | int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1980 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1981 | int ret; |
| 1982 | if (input == NULL) return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1983 | |
| 1984 | if (xmlParserDebugEntities) { |
| 1985 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 1986 | xmlGenericError(xmlGenericErrorContext, |
| 1987 | "%s(%d): ", ctxt->input->filename, |
| 1988 | ctxt->input->line); |
| 1989 | xmlGenericError(xmlGenericErrorContext, |
| 1990 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 1991 | } |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1992 | ret = inputPush(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1993 | GROW; |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 1994 | return(ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | /** |
| 1998 | * xmlParseCharRef: |
| 1999 | * @ctxt: an XML parser context |
| 2000 | * |
| 2001 | * parse Reference declarations |
| 2002 | * |
| 2003 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2004 | * '&#x' [0-9a-fA-F]+ ';' |
| 2005 | * |
| 2006 | * [ WFC: Legal Character ] |
| 2007 | * Characters referred to using character references must match the |
| 2008 | * production for Char. |
| 2009 | * |
| 2010 | * Returns the value parsed (as an int), 0 in case of error |
| 2011 | */ |
| 2012 | int |
| 2013 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 2014 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2015 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2016 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2017 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2018 | /* |
| 2019 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 2020 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2021 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2022 | (NXT(2) == 'x')) { |
| 2023 | SKIP(3); |
| 2024 | GROW; |
| 2025 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2026 | if (count++ > 20) { |
| 2027 | count = 0; |
| 2028 | GROW; |
| 2029 | } |
| 2030 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2031 | val = val * 16 + (CUR - '0'); |
| 2032 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 2033 | val = val * 16 + (CUR - 'a') + 10; |
| 2034 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 2035 | val = val * 16 + (CUR - 'A') + 10; |
| 2036 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2037 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2038 | val = 0; |
| 2039 | break; |
| 2040 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2041 | if (val > 0x10FFFF) |
| 2042 | outofrange = val; |
| 2043 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2044 | NEXT; |
| 2045 | count++; |
| 2046 | } |
| 2047 | if (RAW == ';') { |
| 2048 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2049 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2050 | ctxt->nbChars ++; |
| 2051 | ctxt->input->cur++; |
| 2052 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2053 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2054 | SKIP(2); |
| 2055 | GROW; |
| 2056 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 2057 | if (count++ > 20) { |
| 2058 | count = 0; |
| 2059 | GROW; |
| 2060 | } |
| 2061 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2062 | val = val * 10 + (CUR - '0'); |
| 2063 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2064 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2065 | val = 0; |
| 2066 | break; |
| 2067 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2068 | if (val > 0x10FFFF) |
| 2069 | outofrange = val; |
| 2070 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2071 | NEXT; |
| 2072 | count++; |
| 2073 | } |
| 2074 | if (RAW == ';') { |
| 2075 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2076 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2077 | ctxt->nbChars ++; |
| 2078 | ctxt->input->cur++; |
| 2079 | } |
| 2080 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2081 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | /* |
| 2085 | * [ WFC: Legal Character ] |
| 2086 | * Characters referred to using character references must match the |
| 2087 | * production for Char. |
| 2088 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2089 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2090 | return(val); |
| 2091 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2092 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2093 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 2094 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2095 | } |
| 2096 | return(0); |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * xmlParseStringCharRef: |
| 2101 | * @ctxt: an XML parser context |
| 2102 | * @str: a pointer to an index in the string |
| 2103 | * |
| 2104 | * parse Reference declarations, variant parsing from a string rather |
| 2105 | * than an an input flow. |
| 2106 | * |
| 2107 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 2108 | * '&#x' [0-9a-fA-F]+ ';' |
| 2109 | * |
| 2110 | * [ WFC: Legal Character ] |
| 2111 | * Characters referred to using character references must match the |
| 2112 | * production for Char. |
| 2113 | * |
| 2114 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 2115 | * updated to the current value of the index |
| 2116 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2117 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2118 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 2119 | const xmlChar *ptr; |
| 2120 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2121 | unsigned int val = 0; |
| 2122 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2123 | |
| 2124 | if ((str == NULL) || (*str == NULL)) return(0); |
| 2125 | ptr = *str; |
| 2126 | cur = *ptr; |
| 2127 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 2128 | ptr += 3; |
| 2129 | cur = *ptr; |
| 2130 | while (cur != ';') { /* Non input consuming loop */ |
| 2131 | if ((cur >= '0') && (cur <= '9')) |
| 2132 | val = val * 16 + (cur - '0'); |
| 2133 | else if ((cur >= 'a') && (cur <= 'f')) |
| 2134 | val = val * 16 + (cur - 'a') + 10; |
| 2135 | else if ((cur >= 'A') && (cur <= 'F')) |
| 2136 | val = val * 16 + (cur - 'A') + 10; |
| 2137 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2138 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2139 | val = 0; |
| 2140 | break; |
| 2141 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2142 | if (val > 0x10FFFF) |
| 2143 | outofrange = val; |
| 2144 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2145 | ptr++; |
| 2146 | cur = *ptr; |
| 2147 | } |
| 2148 | if (cur == ';') |
| 2149 | ptr++; |
| 2150 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 2151 | ptr += 2; |
| 2152 | cur = *ptr; |
| 2153 | while (cur != ';') { /* Non input consuming loops */ |
| 2154 | if ((cur >= '0') && (cur <= '9')) |
| 2155 | val = val * 10 + (cur - '0'); |
| 2156 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2157 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2158 | val = 0; |
| 2159 | break; |
| 2160 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2161 | if (val > 0x10FFFF) |
| 2162 | outofrange = val; |
| 2163 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2164 | ptr++; |
| 2165 | cur = *ptr; |
| 2166 | } |
| 2167 | if (cur == ';') |
| 2168 | ptr++; |
| 2169 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2170 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2171 | return(0); |
| 2172 | } |
| 2173 | *str = ptr; |
| 2174 | |
| 2175 | /* |
| 2176 | * [ WFC: Legal Character ] |
| 2177 | * Characters referred to using character references must match the |
| 2178 | * production for Char. |
| 2179 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 2180 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2181 | return(val); |
| 2182 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2183 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 2184 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 2185 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2186 | } |
| 2187 | return(0); |
| 2188 | } |
| 2189 | |
| 2190 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2191 | * xmlNewBlanksWrapperInputStream: |
| 2192 | * @ctxt: an XML parser context |
| 2193 | * @entity: an Entity pointer |
| 2194 | * |
| 2195 | * Create a new input stream for wrapping |
| 2196 | * blanks around a PEReference |
| 2197 | * |
| 2198 | * Returns the new input stream or NULL |
| 2199 | */ |
| 2200 | |
| 2201 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
| 2202 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 2203 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2204 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 2205 | xmlParserInputPtr input; |
| 2206 | xmlChar *buffer; |
| 2207 | size_t length; |
| 2208 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2209 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2210 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2211 | return(NULL); |
| 2212 | } |
| 2213 | if (xmlParserDebugEntities) |
| 2214 | xmlGenericError(xmlGenericErrorContext, |
| 2215 | "new blanks wrapper for entity: %s\n", entity->name); |
| 2216 | input = xmlNewInputStream(ctxt); |
| 2217 | if (input == NULL) { |
| 2218 | return(NULL); |
| 2219 | } |
| 2220 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2221 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2222 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2223 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2224 | xmlFree(input); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2225 | return(NULL); |
| 2226 | } |
| 2227 | buffer [0] = ' '; |
| 2228 | buffer [1] = '%'; |
| 2229 | buffer [length-3] = ';'; |
| 2230 | buffer [length-2] = ' '; |
| 2231 | buffer [length-1] = 0; |
| 2232 | memcpy(buffer + 2, entity->name, length - 5); |
| 2233 | input->free = deallocblankswrapper; |
| 2234 | input->base = buffer; |
| 2235 | input->cur = buffer; |
| 2236 | input->length = length; |
| 2237 | input->end = &buffer[length]; |
| 2238 | return(input); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2242 | * xmlParserHandlePEReference: |
| 2243 | * @ctxt: the parser context |
| 2244 | * |
| 2245 | * [69] PEReference ::= '%' Name ';' |
| 2246 | * |
| 2247 | * [ WFC: No Recursion ] |
| 2248 | * A parsed entity must not contain a recursive |
| 2249 | * reference to itself, either directly or indirectly. |
| 2250 | * |
| 2251 | * [ WFC: Entity Declared ] |
| 2252 | * In a document without any DTD, a document with only an internal DTD |
| 2253 | * subset which contains no parameter entity references, or a document |
| 2254 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 2255 | * entity must precede any reference to it... |
| 2256 | * |
| 2257 | * [ VC: Entity Declared ] |
| 2258 | * In a document with an external subset or external parameter entities |
| 2259 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 2260 | * must precede any reference to it... |
| 2261 | * |
| 2262 | * [ WFC: In DTD ] |
| 2263 | * Parameter-entity references may only appear in the DTD. |
| 2264 | * NOTE: misleading but this is handled. |
| 2265 | * |
| 2266 | * A PEReference may have been detected in the current input stream |
| 2267 | * the handling is done accordingly to |
| 2268 | * http://www.w3.org/TR/REC-xml#entproc |
| 2269 | * i.e. |
| 2270 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2271 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2272 | */ |
| 2273 | void |
| 2274 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2275 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2276 | xmlEntityPtr entity = NULL; |
| 2277 | xmlParserInputPtr input; |
| 2278 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2279 | if (RAW != '%') return; |
| 2280 | switch(ctxt->instate) { |
| 2281 | case XML_PARSER_CDATA_SECTION: |
| 2282 | return; |
| 2283 | case XML_PARSER_COMMENT: |
| 2284 | return; |
| 2285 | case XML_PARSER_START_TAG: |
| 2286 | return; |
| 2287 | case XML_PARSER_END_TAG: |
| 2288 | return; |
| 2289 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2290 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2291 | return; |
| 2292 | case XML_PARSER_PROLOG: |
| 2293 | case XML_PARSER_START: |
| 2294 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2295 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2296 | return; |
| 2297 | case XML_PARSER_ENTITY_DECL: |
| 2298 | case XML_PARSER_CONTENT: |
| 2299 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 2300 | case XML_PARSER_PI: |
| 2301 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 2302 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2303 | /* we just ignore it there */ |
| 2304 | return; |
| 2305 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2306 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2307 | return; |
| 2308 | case XML_PARSER_ENTITY_VALUE: |
| 2309 | /* |
| 2310 | * NOTE: in the case of entity values, we don't do the |
| 2311 | * substitution here since we need the literal |
| 2312 | * entity value to be able to save the internal |
| 2313 | * subset of the document. |
| 2314 | * This will be handled by xmlStringDecodeEntities |
| 2315 | */ |
| 2316 | return; |
| 2317 | case XML_PARSER_DTD: |
| 2318 | /* |
| 2319 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 2320 | * In the internal DTD subset, parameter-entity references |
| 2321 | * can occur only where markup declarations can occur, not |
| 2322 | * within markup declarations. |
| 2323 | * In that case this is handled in xmlParseMarkupDecl |
| 2324 | */ |
| 2325 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 2326 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2327 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2328 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2329 | break; |
| 2330 | case XML_PARSER_IGNORE: |
| 2331 | return; |
| 2332 | } |
| 2333 | |
| 2334 | NEXT; |
| 2335 | name = xmlParseName(ctxt); |
| 2336 | if (xmlParserDebugEntities) |
| 2337 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2338 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2339 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2340 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2341 | } else { |
| 2342 | if (RAW == ';') { |
| 2343 | NEXT; |
| 2344 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 2345 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 2346 | if (entity == NULL) { |
| 2347 | |
| 2348 | /* |
| 2349 | * [ WFC: Entity Declared ] |
| 2350 | * In a document without any DTD, a document with only an |
| 2351 | * internal DTD subset which contains no parameter entity |
| 2352 | * references, or a document with "standalone='yes'", ... |
| 2353 | * ... The declaration of a parameter entity must precede |
| 2354 | * any reference to it... |
| 2355 | */ |
| 2356 | if ((ctxt->standalone == 1) || |
| 2357 | ((ctxt->hasExternalSubset == 0) && |
| 2358 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2359 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2361 | } else { |
| 2362 | /* |
| 2363 | * [ VC: Entity Declared ] |
| 2364 | * In a document with an external subset or external |
| 2365 | * parameter entities with "standalone='no'", ... |
| 2366 | * ... The declaration of a parameter entity must precede |
| 2367 | * any reference to it... |
| 2368 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2369 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 2370 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2371 | "PEReference: %%%s; not found\n", |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 2372 | name, NULL); |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 2373 | } else |
| 2374 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 2375 | "PEReference: %%%s; not found\n", |
| 2376 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2377 | ctxt->valid = 0; |
| 2378 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 2379 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 2380 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2381 | if (xmlPushInput(ctxt, input) < 0) |
| 2382 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2383 | } else { |
| 2384 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 2385 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2386 | xmlChar start[4]; |
| 2387 | xmlCharEncoding enc; |
| 2388 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2389 | /* |
| 2390 | * handle the extra spaces added before and after |
| 2391 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2392 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2393 | */ |
| 2394 | input = xmlNewEntityInputStream(ctxt, entity); |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2395 | if (xmlPushInput(ctxt, input) < 0) |
| 2396 | return; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2397 | |
| 2398 | /* |
| 2399 | * Get the 4 first bytes and decode the charset |
| 2400 | * if enc != XML_CHAR_ENCODING_NONE |
| 2401 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2402 | * Note that, since we may have some non-UTF8 |
| 2403 | * encoding (like UTF16, bug 135229), the 'length' |
| 2404 | * is not known, but we can calculate based upon |
| 2405 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2406 | */ |
| 2407 | GROW |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 2408 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 2409 | start[0] = RAW; |
| 2410 | start[1] = NXT(1); |
| 2411 | start[2] = NXT(2); |
| 2412 | start[3] = NXT(3); |
| 2413 | enc = xmlDetectCharEncoding(start, 4); |
| 2414 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 2415 | xmlSwitchEncoding(ctxt, enc); |
| 2416 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2419 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 2420 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 2421 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2422 | xmlParseTextDecl(ctxt); |
| 2423 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2424 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2425 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 2426 | "PEReference: %s is not a parameter entity\n", |
| 2427 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2431 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | /* |
| 2437 | * Macro used to grow the current buffer. |
| 2438 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2439 | #define growBuffer(buffer, n) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2440 | xmlChar *tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2441 | buffer##_size *= 2; \ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2442 | buffer##_size += n; \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2443 | tmp = (xmlChar *) \ |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2444 | xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 2445 | if (tmp == NULL) goto mem_error; \ |
| 2446 | buffer = tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 2450 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2451 | * @ctxt: the parser context |
| 2452 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2453 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2454 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2455 | * @end: an end marker xmlChar, 0 if none |
| 2456 | * @end2: an end marker xmlChar, 0 if none |
| 2457 | * @end3: an end marker xmlChar, 0 if none |
| 2458 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2459 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2460 | * |
| 2461 | * [67] Reference ::= EntityRef | CharRef |
| 2462 | * |
| 2463 | * [69] PEReference ::= '%' Name ';' |
| 2464 | * |
| 2465 | * Returns A newly allocated string with the substitution done. The caller |
| 2466 | * must deallocate it ! |
| 2467 | */ |
| 2468 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2469 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2470 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2471 | xmlChar *buffer = NULL; |
| 2472 | int buffer_size = 0; |
| 2473 | |
| 2474 | xmlChar *current = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2475 | xmlChar *rep = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2476 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2477 | xmlEntityPtr ent; |
| 2478 | int c,l; |
| 2479 | int nbchars = 0; |
| 2480 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2481 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2482 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2483 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2484 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2485 | if (((ctxt->depth > 40) && |
| 2486 | ((ctxt->options & XML_PARSE_HUGE) == 0)) || |
| 2487 | (ctxt->depth > 1024)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2488 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2489 | return(NULL); |
| 2490 | } |
| 2491 | |
| 2492 | /* |
| 2493 | * allocate a translation buffer. |
| 2494 | */ |
| 2495 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2496 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2497 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2498 | |
| 2499 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2500 | * 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] | 2501 | * we are operating on already parsed values. |
| 2502 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2503 | if (str < last) |
| 2504 | c = CUR_SCHAR(str, l); |
| 2505 | else |
| 2506 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2507 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 2508 | (c != end2) && (c != end3)) { |
| 2509 | |
| 2510 | if (c == 0) break; |
| 2511 | if ((c == '&') && (str[1] == '#')) { |
| 2512 | int val = xmlParseStringCharRef(ctxt, &str); |
| 2513 | if (val != 0) { |
| 2514 | COPY_BUF(0,buffer,nbchars,val); |
| 2515 | } |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2516 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2517 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2518 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2519 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 2520 | if (xmlParserDebugEntities) |
| 2521 | xmlGenericError(xmlGenericErrorContext, |
| 2522 | "String decoding Entity Reference: %.30s\n", |
| 2523 | str); |
| 2524 | ent = xmlParseStringEntityRef(ctxt, &str); |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 2525 | if ((ctxt->lastError.code == XML_ERR_ENTITY_LOOP) || |
| 2526 | (ctxt->lastError.code == XML_ERR_INTERNAL_ERROR)) |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2527 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2528 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2529 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2530 | if ((ent != NULL) && |
| 2531 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2532 | if (ent->content != NULL) { |
| 2533 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2534 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2535 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Daniel Veillard | bedc977 | 2005-09-28 21:42:15 +0000 | [diff] [blame] | 2536 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2537 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 2538 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 2539 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } else if ((ent != NULL) && (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2542 | ctxt->depth++; |
| 2543 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2544 | 0, 0, 0); |
| 2545 | ctxt->depth--; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2546 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2547 | if (rep != NULL) { |
| 2548 | current = rep; |
| 2549 | while (*current != 0) { /* non input consuming loop */ |
| 2550 | buffer[nbchars++] = *current++; |
| 2551 | if (nbchars > |
| 2552 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2553 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2554 | goto int_error; |
| 2555 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2559 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } else if (ent != NULL) { |
| 2562 | int i = xmlStrlen(ent->name); |
| 2563 | const xmlChar *cur = ent->name; |
| 2564 | |
| 2565 | buffer[nbchars++] = '&'; |
| 2566 | if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2567 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2568 | } |
| 2569 | for (;i > 0;i--) |
| 2570 | buffer[nbchars++] = *cur++; |
| 2571 | buffer[nbchars++] = ';'; |
| 2572 | } |
| 2573 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2574 | if (xmlParserDebugEntities) |
| 2575 | xmlGenericError(xmlGenericErrorContext, |
| 2576 | "String decoding PE Reference: %.30s\n", str); |
| 2577 | ent = xmlParseStringPEReference(ctxt, &str); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2578 | if (ctxt->lastError.code == XML_ERR_ENTITY_LOOP) |
| 2579 | goto int_error; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2580 | if (ent != NULL) |
Daniel Veillard | f4f4e48 | 2008-08-25 08:57:48 +0000 | [diff] [blame] | 2581 | ctxt->nbentities += ent->checked; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2582 | if (ent != NULL) { |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2583 | if (ent->content == NULL) { |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 2584 | xmlLoadEntityContent(ctxt, ent); |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2585 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2586 | ctxt->depth++; |
| 2587 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2588 | 0, 0, 0); |
| 2589 | ctxt->depth--; |
| 2590 | if (rep != NULL) { |
| 2591 | current = rep; |
| 2592 | while (*current != 0) { /* non input consuming loop */ |
| 2593 | buffer[nbchars++] = *current++; |
| 2594 | if (nbchars > |
| 2595 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2596 | if (xmlParserEntityCheck(ctxt, nbchars, ent)) |
| 2597 | goto int_error; |
| 2598 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2602 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | } else { |
| 2606 | COPY_BUF(l,buffer,nbchars,c); |
| 2607 | str += l; |
| 2608 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 2609 | growBuffer(buffer, XML_PARSER_BUFFER_SIZE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2610 | } |
| 2611 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2612 | if (str < last) |
| 2613 | c = CUR_SCHAR(str, l); |
| 2614 | else |
| 2615 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2616 | } |
| 2617 | buffer[nbchars++] = 0; |
| 2618 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2619 | |
| 2620 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2621 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 2622 | int_error: |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2623 | if (rep != NULL) |
| 2624 | xmlFree(rep); |
| 2625 | if (buffer != NULL) |
| 2626 | xmlFree(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2627 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2628 | } |
| 2629 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2630 | /** |
| 2631 | * xmlStringDecodeEntities: |
| 2632 | * @ctxt: the parser context |
| 2633 | * @str: the input string |
| 2634 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2635 | * @end: an end marker xmlChar, 0 if none |
| 2636 | * @end2: an end marker xmlChar, 0 if none |
| 2637 | * @end3: an end marker xmlChar, 0 if none |
| 2638 | * |
| 2639 | * Takes a entity string content and process to do the adequate substitutions. |
| 2640 | * |
| 2641 | * [67] Reference ::= EntityRef | CharRef |
| 2642 | * |
| 2643 | * [69] PEReference ::= '%' Name ';' |
| 2644 | * |
| 2645 | * Returns A newly allocated string with the substitution done. The caller |
| 2646 | * must deallocate it ! |
| 2647 | */ |
| 2648 | xmlChar * |
| 2649 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2650 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2651 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2652 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2653 | end, end2, end3)); |
| 2654 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2655 | |
| 2656 | /************************************************************************ |
| 2657 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2658 | * Commodity functions, cleanup needed ? * |
| 2659 | * * |
| 2660 | ************************************************************************/ |
| 2661 | |
| 2662 | /** |
| 2663 | * areBlanks: |
| 2664 | * @ctxt: an XML parser context |
| 2665 | * @str: a xmlChar * |
| 2666 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2667 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2668 | * |
| 2669 | * Is this a sequence of blank chars that one can ignore ? |
| 2670 | * |
| 2671 | * Returns 1 if ignorable 0 otherwise. |
| 2672 | */ |
| 2673 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2674 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2675 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2676 | int i, ret; |
| 2677 | xmlNodePtr lastChild; |
| 2678 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2679 | /* |
| 2680 | * Don't spend time trying to differentiate them, the same callback is |
| 2681 | * used ! |
| 2682 | */ |
| 2683 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2684 | return(0); |
| 2685 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2686 | /* |
| 2687 | * Check for xml:space value. |
| 2688 | */ |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 2689 | if ((ctxt->space == NULL) || (*(ctxt->space) == 1) || |
| 2690 | (*(ctxt->space) == -2)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2691 | return(0); |
| 2692 | |
| 2693 | /* |
| 2694 | * Check that the string is made of blanks |
| 2695 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2696 | if (blank_chars == 0) { |
| 2697 | for (i = 0;i < len;i++) |
| 2698 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2699 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 | |
| 2701 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2702 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2703 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2704 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | if (ctxt->myDoc != NULL) { |
| 2706 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2707 | if (ret == 0) return(1); |
| 2708 | if (ret == 1) return(0); |
| 2709 | } |
| 2710 | |
| 2711 | /* |
| 2712 | * Otherwise, heuristic :-\ |
| 2713 | */ |
Daniel Veillard | abac41e | 2005-07-06 15:17:38 +0000 | [diff] [blame] | 2714 | if ((RAW != '<') && (RAW != 0xD)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2715 | if ((ctxt->node->children == NULL) && |
| 2716 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2717 | |
| 2718 | lastChild = xmlGetLastChild(ctxt->node); |
| 2719 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2720 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2721 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2722 | } else if (xmlNodeIsText(lastChild)) |
| 2723 | return(0); |
| 2724 | else if ((ctxt->node->children != NULL) && |
| 2725 | (xmlNodeIsText(ctxt->node->children))) |
| 2726 | return(0); |
| 2727 | return(1); |
| 2728 | } |
| 2729 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2730 | /************************************************************************ |
| 2731 | * * |
| 2732 | * Extra stuff for namespace support * |
| 2733 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2734 | * * |
| 2735 | ************************************************************************/ |
| 2736 | |
| 2737 | /** |
| 2738 | * xmlSplitQName: |
| 2739 | * @ctxt: an XML parser context |
| 2740 | * @name: an XML parser context |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2741 | * @prefix: a xmlChar ** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2742 | * |
| 2743 | * parse an UTF8 encoded XML qualified name string |
| 2744 | * |
| 2745 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2746 | * |
| 2747 | * [NS 6] Prefix ::= NCName |
| 2748 | * |
| 2749 | * [NS 7] LocalPart ::= NCName |
| 2750 | * |
| 2751 | * Returns the local part, and prefix is updated |
| 2752 | * to get the Prefix if any. |
| 2753 | */ |
| 2754 | |
| 2755 | xmlChar * |
| 2756 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2757 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2758 | xmlChar *buffer = NULL; |
| 2759 | int len = 0; |
| 2760 | int max = XML_MAX_NAMELEN; |
| 2761 | xmlChar *ret = NULL; |
| 2762 | const xmlChar *cur = name; |
| 2763 | int c; |
| 2764 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2765 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2766 | *prefix = NULL; |
| 2767 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2768 | if (cur == NULL) return(NULL); |
| 2769 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2770 | #ifndef XML_XML_NAMESPACE |
| 2771 | /* xml: prefix is not really a namespace */ |
| 2772 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2773 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2774 | return(xmlStrdup(name)); |
| 2775 | #endif |
| 2776 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2777 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2778 | if (cur[0] == ':') |
| 2779 | return(xmlStrdup(name)); |
| 2780 | |
| 2781 | c = *cur++; |
| 2782 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2783 | buf[len++] = c; |
| 2784 | c = *cur++; |
| 2785 | } |
| 2786 | if (len >= max) { |
| 2787 | /* |
| 2788 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2789 | * for the processing speed. |
| 2790 | */ |
| 2791 | max = len * 2; |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2792 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2793 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2794 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2795 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2796 | return(NULL); |
| 2797 | } |
| 2798 | memcpy(buffer, buf, len); |
| 2799 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2800 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2801 | xmlChar *tmp; |
| 2802 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2803 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2804 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2805 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2806 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2807 | xmlFree(buffer); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2808 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2809 | return(NULL); |
| 2810 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2811 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2812 | } |
| 2813 | buffer[len++] = c; |
| 2814 | c = *cur++; |
| 2815 | } |
| 2816 | buffer[len] = 0; |
| 2817 | } |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 2818 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2819 | if ((c == ':') && (*cur == 0)) { |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 2820 | if (buffer != NULL) |
| 2821 | xmlFree(buffer); |
| 2822 | *prefix = NULL; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2823 | return(xmlStrdup(name)); |
Daniel Veillard | 02a4963 | 2006-10-13 12:42:31 +0000 | [diff] [blame] | 2824 | } |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2825 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2826 | if (buffer == NULL) |
| 2827 | ret = xmlStrndup(buf, len); |
| 2828 | else { |
| 2829 | ret = buffer; |
| 2830 | buffer = NULL; |
| 2831 | max = XML_MAX_NAMELEN; |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2836 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2837 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2838 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 2839 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2840 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2841 | len = 0; |
| 2842 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2843 | /* |
| 2844 | * Check that the first character is proper to start |
| 2845 | * a new name |
| 2846 | */ |
| 2847 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 2848 | ((c >= 0x41) && (c <= 0x5A)) || |
| 2849 | (c == '_') || (c == ':'))) { |
| 2850 | int l; |
| 2851 | int first = CUR_SCHAR(cur, l); |
| 2852 | |
| 2853 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2854 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2855 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2856 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | cur++; |
| 2860 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2861 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 2862 | buf[len++] = c; |
| 2863 | c = *cur++; |
| 2864 | } |
| 2865 | if (len >= max) { |
| 2866 | /* |
| 2867 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2868 | * for the processing speed. |
| 2869 | */ |
| 2870 | max = len * 2; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2871 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2872 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2873 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2874 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2875 | return(NULL); |
| 2876 | } |
| 2877 | memcpy(buffer, buf, len); |
| 2878 | while (c != 0) { /* tested bigname2.xml */ |
| 2879 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2880 | xmlChar *tmp; |
| 2881 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2882 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2883 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2884 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2885 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2886 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2887 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2888 | return(NULL); |
| 2889 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2890 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2891 | } |
| 2892 | buffer[len++] = c; |
| 2893 | c = *cur++; |
| 2894 | } |
| 2895 | buffer[len] = 0; |
| 2896 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 2897 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2898 | if (buffer == NULL) |
| 2899 | ret = xmlStrndup(buf, len); |
| 2900 | else { |
| 2901 | ret = buffer; |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | return(ret); |
| 2906 | } |
| 2907 | |
| 2908 | /************************************************************************ |
| 2909 | * * |
| 2910 | * The parser itself * |
| 2911 | * Relates to http://www.w3.org/TR/REC-xml * |
| 2912 | * * |
| 2913 | ************************************************************************/ |
| 2914 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 2915 | /************************************************************************ |
| 2916 | * * |
| 2917 | * Routines to parse Name, NCName and NmToken * |
| 2918 | * * |
| 2919 | ************************************************************************/ |
| 2920 | unsigned long nbParseName = 0; |
| 2921 | unsigned long nbParseNmToken = 0; |
| 2922 | unsigned long nbParseNCName = 0; |
| 2923 | unsigned long nbParseNCNameComplex = 0; |
| 2924 | unsigned long nbParseNameComplex = 0; |
| 2925 | unsigned long nbParseStringName = 0; |
| 2926 | /* |
| 2927 | * The two following functions are related to the change of accepted |
| 2928 | * characters for Name and NmToken in the Revision 5 of XML-1.0 |
| 2929 | * They correspond to the modified production [4] and the new production [4a] |
| 2930 | * changes in that revision. Also note that the macros used for the |
| 2931 | * productions Letter, Digit, CombiningChar and Extender are not needed |
| 2932 | * anymore. |
| 2933 | * We still keep compatibility to pre-revision5 parsing semantic if the |
| 2934 | * new XML_PARSE_OLD10 option is given to the parser. |
| 2935 | */ |
| 2936 | static int |
| 2937 | xmlIsNameStartChar(xmlParserCtxtPtr ctxt, int c) { |
| 2938 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 2939 | /* |
| 2940 | * Use the new checks of production [4] [4a] amd [5] of the |
| 2941 | * Update 5 of XML-1.0 |
| 2942 | */ |
| 2943 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 2944 | (((c >= 'a') && (c <= 'z')) || |
| 2945 | ((c >= 'A') && (c <= 'Z')) || |
| 2946 | (c == '_') || (c == ':') || |
| 2947 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 2948 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 2949 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 2950 | ((c >= 0x370) && (c <= 0x37D)) || |
| 2951 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 2952 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 2953 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 2954 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 2955 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 2956 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 2957 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 2958 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 2959 | return(1); |
| 2960 | } else { |
| 2961 | if (IS_LETTER(c) || (c == '_') || (c == ':')) |
| 2962 | return(1); |
| 2963 | } |
| 2964 | return(0); |
| 2965 | } |
| 2966 | |
| 2967 | static int |
| 2968 | xmlIsNameChar(xmlParserCtxtPtr ctxt, int c) { |
| 2969 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 2970 | /* |
| 2971 | * Use the new checks of production [4] [4a] amd [5] of the |
| 2972 | * Update 5 of XML-1.0 |
| 2973 | */ |
| 2974 | if ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 2975 | (((c >= 'a') && (c <= 'z')) || |
| 2976 | ((c >= 'A') && (c <= 'Z')) || |
| 2977 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 2978 | (c == '_') || (c == ':') || |
| 2979 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 2980 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 2981 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 2982 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 2983 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 2984 | ((c >= 0x370) && (c <= 0x37D)) || |
| 2985 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 2986 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 2987 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 2988 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 2989 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 2990 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 2991 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 2992 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 2993 | ((c >= 0x10000) && (c <= 0xEFFFF)))) |
| 2994 | return(1); |
| 2995 | } else { |
| 2996 | if ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 2997 | (c == '.') || (c == '-') || |
| 2998 | (c == '_') || (c == ':') || |
| 2999 | (IS_COMBINING(c)) || |
| 3000 | (IS_EXTENDER(c))) |
| 3001 | return(1); |
| 3002 | } |
| 3003 | return(0); |
| 3004 | } |
| 3005 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3006 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3007 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3008 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3009 | static const xmlChar * |
| 3010 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
| 3011 | int len = 0, l; |
| 3012 | int c; |
| 3013 | int count = 0; |
| 3014 | |
| 3015 | nbParseNameComplex++; |
| 3016 | |
| 3017 | /* |
| 3018 | * Handler for more complex cases |
| 3019 | */ |
| 3020 | GROW; |
| 3021 | c = CUR_CHAR(l); |
| 3022 | if ((ctxt->options & XML_PARSE_OLD10) == 0) { |
| 3023 | /* |
| 3024 | * Use the new checks of production [4] [4a] amd [5] of the |
| 3025 | * Update 5 of XML-1.0 |
| 3026 | */ |
| 3027 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3028 | (!(((c >= 'a') && (c <= 'z')) || |
| 3029 | ((c >= 'A') && (c <= 'Z')) || |
| 3030 | (c == '_') || (c == ':') || |
| 3031 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3032 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3033 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3034 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3035 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3036 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3037 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3038 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3039 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3040 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3041 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3042 | ((c >= 0x10000) && (c <= 0xEFFFF))))) { |
| 3043 | return(NULL); |
| 3044 | } |
| 3045 | len += l; |
| 3046 | NEXTL(l); |
| 3047 | c = CUR_CHAR(l); |
| 3048 | while ((c != ' ') && (c != '>') && (c != '/') && /* accelerators */ |
| 3049 | (((c >= 'a') && (c <= 'z')) || |
| 3050 | ((c >= 'A') && (c <= 'Z')) || |
| 3051 | ((c >= '0') && (c <= '9')) || /* !start */ |
| 3052 | (c == '_') || (c == ':') || |
| 3053 | (c == '-') || (c == '.') || (c == 0xB7) || /* !start */ |
| 3054 | ((c >= 0xC0) && (c <= 0xD6)) || |
| 3055 | ((c >= 0xD8) && (c <= 0xF6)) || |
| 3056 | ((c >= 0xF8) && (c <= 0x2FF)) || |
| 3057 | ((c >= 0x300) && (c <= 0x36F)) || /* !start */ |
| 3058 | ((c >= 0x370) && (c <= 0x37D)) || |
| 3059 | ((c >= 0x37F) && (c <= 0x1FFF)) || |
| 3060 | ((c >= 0x200C) && (c <= 0x200D)) || |
| 3061 | ((c >= 0x203F) && (c <= 0x2040)) || /* !start */ |
| 3062 | ((c >= 0x2070) && (c <= 0x218F)) || |
| 3063 | ((c >= 0x2C00) && (c <= 0x2FEF)) || |
| 3064 | ((c >= 0x3001) && (c <= 0xD7FF)) || |
| 3065 | ((c >= 0xF900) && (c <= 0xFDCF)) || |
| 3066 | ((c >= 0xFDF0) && (c <= 0xFFFD)) || |
| 3067 | ((c >= 0x10000) && (c <= 0xEFFFF)) |
| 3068 | )) { |
| 3069 | if (count++ > 100) { |
| 3070 | count = 0; |
| 3071 | GROW; |
| 3072 | } |
| 3073 | len += l; |
| 3074 | NEXTL(l); |
| 3075 | c = CUR_CHAR(l); |
| 3076 | } |
| 3077 | } else { |
| 3078 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3079 | (!IS_LETTER(c) && (c != '_') && |
| 3080 | (c != ':'))) { |
| 3081 | return(NULL); |
| 3082 | } |
| 3083 | len += l; |
| 3084 | NEXTL(l); |
| 3085 | c = CUR_CHAR(l); |
| 3086 | |
| 3087 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3088 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
| 3089 | (c == '.') || (c == '-') || |
| 3090 | (c == '_') || (c == ':') || |
| 3091 | (IS_COMBINING(c)) || |
| 3092 | (IS_EXTENDER(c)))) { |
| 3093 | if (count++ > 100) { |
| 3094 | count = 0; |
| 3095 | GROW; |
| 3096 | } |
| 3097 | len += l; |
| 3098 | NEXTL(l); |
| 3099 | c = CUR_CHAR(l); |
| 3100 | } |
| 3101 | } |
| 3102 | if ((*ctxt->input->cur == '\n') && (ctxt->input->cur[-1] == '\r')) |
| 3103 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - (len + 1), len)); |
| 3104 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3105 | } |
| 3106 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3107 | /** |
| 3108 | * xmlParseName: |
| 3109 | * @ctxt: an XML parser context |
| 3110 | * |
| 3111 | * parse an XML name. |
| 3112 | * |
| 3113 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3114 | * CombiningChar | Extender |
| 3115 | * |
| 3116 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3117 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3118 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3119 | * |
| 3120 | * Returns the Name parsed or NULL |
| 3121 | */ |
| 3122 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3123 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3124 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3125 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3126 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3127 | int count = 0; |
| 3128 | |
| 3129 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3130 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3131 | nbParseName++; |
| 3132 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3133 | /* |
| 3134 | * Accelerator for simple ASCII names |
| 3135 | */ |
| 3136 | in = ctxt->input->cur; |
| 3137 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3138 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3139 | (*in == '_') || (*in == ':')) { |
| 3140 | in++; |
| 3141 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3142 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3143 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3144 | (*in == '_') || (*in == '-') || |
| 3145 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3146 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3147 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3148 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3149 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3150 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 3151 | ctxt->nbChars += count; |
| 3152 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3153 | if (ret == NULL) |
| 3154 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3155 | return(ret); |
| 3156 | } |
| 3157 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3158 | /* accelerator for special cases */ |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 3159 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 3160 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3161 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3162 | static const xmlChar * |
| 3163 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 3164 | int len = 0, l; |
| 3165 | int c; |
| 3166 | int count = 0; |
| 3167 | |
| 3168 | nbParseNCNameComplex++; |
| 3169 | |
| 3170 | /* |
| 3171 | * Handler for more complex cases |
| 3172 | */ |
| 3173 | GROW; |
| 3174 | c = CUR_CHAR(l); |
| 3175 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 3176 | (!xmlIsNameStartChar(ctxt, c) || (c == ':'))) { |
| 3177 | return(NULL); |
| 3178 | } |
| 3179 | |
| 3180 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
| 3181 | (xmlIsNameChar(ctxt, c) && (c != ':'))) { |
| 3182 | if (count++ > 100) { |
| 3183 | count = 0; |
| 3184 | GROW; |
| 3185 | } |
| 3186 | len += l; |
| 3187 | NEXTL(l); |
| 3188 | c = CUR_CHAR(l); |
| 3189 | } |
| 3190 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 3191 | } |
| 3192 | |
| 3193 | /** |
| 3194 | * xmlParseNCName: |
| 3195 | * @ctxt: an XML parser context |
| 3196 | * @len: lenght of the string parsed |
| 3197 | * |
| 3198 | * parse an XML name. |
| 3199 | * |
| 3200 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 3201 | * CombiningChar | Extender |
| 3202 | * |
| 3203 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 3204 | * |
| 3205 | * Returns the Name parsed or NULL |
| 3206 | */ |
| 3207 | |
| 3208 | static const xmlChar * |
| 3209 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 3210 | const xmlChar *in; |
| 3211 | const xmlChar *ret; |
| 3212 | int count = 0; |
| 3213 | |
| 3214 | nbParseNCName++; |
| 3215 | |
| 3216 | /* |
| 3217 | * Accelerator for simple ASCII names |
| 3218 | */ |
| 3219 | in = ctxt->input->cur; |
| 3220 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3221 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3222 | (*in == '_')) { |
| 3223 | in++; |
| 3224 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 3225 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 3226 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 3227 | (*in == '_') || (*in == '-') || |
| 3228 | (*in == '.')) |
| 3229 | in++; |
| 3230 | if ((*in > 0) && (*in < 0x80)) { |
| 3231 | count = in - ctxt->input->cur; |
| 3232 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 3233 | ctxt->input->cur = in; |
| 3234 | ctxt->nbChars += count; |
| 3235 | ctxt->input->col += count; |
| 3236 | if (ret == NULL) { |
| 3237 | xmlErrMemory(ctxt, NULL); |
| 3238 | } |
| 3239 | return(ret); |
| 3240 | } |
| 3241 | } |
| 3242 | return(xmlParseNCNameComplex(ctxt)); |
| 3243 | } |
| 3244 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3245 | /** |
| 3246 | * xmlParseNameAndCompare: |
| 3247 | * @ctxt: an XML parser context |
| 3248 | * |
| 3249 | * parse an XML name and compares for match |
| 3250 | * (specialized for endtag parsing) |
| 3251 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3252 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 3253 | * and the name for mismatch |
| 3254 | */ |
| 3255 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3256 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3257 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3258 | register const xmlChar *cmp = other; |
| 3259 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3260 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3261 | |
| 3262 | GROW; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3263 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3264 | in = ctxt->input->cur; |
| 3265 | while (*in != 0 && *in == *cmp) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3266 | ++in; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3267 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3268 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3269 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3270 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3271 | /* success */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3272 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3273 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3274 | } |
| 3275 | /* failure (or end of input buffer), check with full function */ |
| 3276 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3277 | /* strings coming from the dictionnary direct compare possible */ |
| 3278 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3279 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 3280 | } |
| 3281 | return ret; |
| 3282 | } |
| 3283 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3284 | /** |
| 3285 | * xmlParseStringName: |
| 3286 | * @ctxt: an XML parser context |
| 3287 | * @str: a pointer to the string pointer (IN/OUT) |
| 3288 | * |
| 3289 | * parse an XML name. |
| 3290 | * |
| 3291 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 3292 | * CombiningChar | Extender |
| 3293 | * |
| 3294 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 3295 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3296 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3297 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3298 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3299 | * is updated to the current location in the string. |
| 3300 | */ |
| 3301 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 3302 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3303 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 3304 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3305 | const xmlChar *cur = *str; |
| 3306 | int len = 0, l; |
| 3307 | int c; |
| 3308 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3309 | nbParseStringName++; |
| 3310 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3311 | c = CUR_SCHAR(cur, l); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3312 | if (!xmlIsNameStartChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3313 | return(NULL); |
| 3314 | } |
| 3315 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3316 | COPY_BUF(l,buf,len,c); |
| 3317 | cur += l; |
| 3318 | c = CUR_SCHAR(cur, l); |
| 3319 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3320 | COPY_BUF(l,buf,len,c); |
| 3321 | cur += l; |
| 3322 | c = CUR_SCHAR(cur, l); |
| 3323 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 3324 | /* |
| 3325 | * Okay someone managed to make a huge name, so he's ready to pay |
| 3326 | * for the processing speed. |
| 3327 | */ |
| 3328 | xmlChar *buffer; |
| 3329 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3330 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3331 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3332 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3333 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3334 | return(NULL); |
| 3335 | } |
| 3336 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3337 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3338 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3339 | xmlChar *tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3340 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3341 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3342 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3343 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3344 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3345 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3346 | return(NULL); |
| 3347 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3348 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3349 | } |
| 3350 | COPY_BUF(l,buffer,len,c); |
| 3351 | cur += l; |
| 3352 | c = CUR_SCHAR(cur, l); |
| 3353 | } |
| 3354 | buffer[len] = 0; |
| 3355 | *str = cur; |
| 3356 | return(buffer); |
| 3357 | } |
| 3358 | } |
| 3359 | *str = cur; |
| 3360 | return(xmlStrndup(buf, len)); |
| 3361 | } |
| 3362 | |
| 3363 | /** |
| 3364 | * xmlParseNmtoken: |
| 3365 | * @ctxt: an XML parser context |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3366 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3367 | * parse an XML Nmtoken. |
| 3368 | * |
| 3369 | * [7] Nmtoken ::= (NameChar)+ |
| 3370 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 3371 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3372 | * |
| 3373 | * Returns the Nmtoken parsed or NULL |
| 3374 | */ |
| 3375 | |
| 3376 | xmlChar * |
| 3377 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 3378 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 3379 | int len = 0, l; |
| 3380 | int c; |
| 3381 | int count = 0; |
| 3382 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3383 | nbParseNmToken++; |
| 3384 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3385 | GROW; |
| 3386 | c = CUR_CHAR(l); |
| 3387 | |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3388 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3389 | if (count++ > 100) { |
| 3390 | count = 0; |
| 3391 | GROW; |
| 3392 | } |
| 3393 | COPY_BUF(l,buf,len,c); |
| 3394 | NEXTL(l); |
| 3395 | c = CUR_CHAR(l); |
| 3396 | if (len >= XML_MAX_NAMELEN) { |
| 3397 | /* |
| 3398 | * Okay someone managed to make a huge token, so he's ready to pay |
| 3399 | * for the processing speed. |
| 3400 | */ |
| 3401 | xmlChar *buffer; |
| 3402 | int max = len * 2; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3403 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3404 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3405 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3406 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3407 | return(NULL); |
| 3408 | } |
| 3409 | memcpy(buffer, buf, len); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 3410 | while (xmlIsNameChar(ctxt, c)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3411 | if (count++ > 100) { |
| 3412 | count = 0; |
| 3413 | GROW; |
| 3414 | } |
| 3415 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3416 | xmlChar *tmp; |
| 3417 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3418 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3419 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3420 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3421 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3422 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3423 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3424 | return(NULL); |
| 3425 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3426 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3427 | } |
| 3428 | COPY_BUF(l,buffer,len,c); |
| 3429 | NEXTL(l); |
| 3430 | c = CUR_CHAR(l); |
| 3431 | } |
| 3432 | buffer[len] = 0; |
| 3433 | return(buffer); |
| 3434 | } |
| 3435 | } |
| 3436 | if (len == 0) |
| 3437 | return(NULL); |
| 3438 | return(xmlStrndup(buf, len)); |
| 3439 | } |
| 3440 | |
| 3441 | /** |
| 3442 | * xmlParseEntityValue: |
| 3443 | * @ctxt: an XML parser context |
| 3444 | * @orig: if non-NULL store a copy of the original entity value |
| 3445 | * |
| 3446 | * parse a value for ENTITY declarations |
| 3447 | * |
| 3448 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 3449 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 3450 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3451 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3452 | */ |
| 3453 | |
| 3454 | xmlChar * |
| 3455 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 3456 | xmlChar *buf = NULL; |
| 3457 | int len = 0; |
| 3458 | int size = XML_PARSER_BUFFER_SIZE; |
| 3459 | int c, l; |
| 3460 | xmlChar stop; |
| 3461 | xmlChar *ret = NULL; |
| 3462 | const xmlChar *cur = NULL; |
| 3463 | xmlParserInputPtr input; |
| 3464 | |
| 3465 | if (RAW == '"') stop = '"'; |
| 3466 | else if (RAW == '\'') stop = '\''; |
| 3467 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3468 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3469 | return(NULL); |
| 3470 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3471 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3472 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3473 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3474 | return(NULL); |
| 3475 | } |
| 3476 | |
| 3477 | /* |
| 3478 | * The content of the entity definition is copied in a buffer. |
| 3479 | */ |
| 3480 | |
| 3481 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 3482 | input = ctxt->input; |
| 3483 | GROW; |
| 3484 | NEXT; |
| 3485 | c = CUR_CHAR(l); |
| 3486 | /* |
| 3487 | * NOTE: 4.4.5 Included in Literal |
| 3488 | * When a parameter entity reference appears in a literal entity |
| 3489 | * value, ... a single or double quote character in the replacement |
| 3490 | * text is always treated as a normal data character and will not |
| 3491 | * terminate the literal. |
| 3492 | * In practice it means we stop the loop only when back at parsing |
| 3493 | * the initial entity and the quote is found |
| 3494 | */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3495 | while ((IS_CHAR(c)) && ((c != stop) || /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3496 | (ctxt->input != input))) { |
| 3497 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3498 | xmlChar *tmp; |
| 3499 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3500 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3501 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3502 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3503 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3504 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3505 | return(NULL); |
| 3506 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3507 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3508 | } |
| 3509 | COPY_BUF(l,buf,len,c); |
| 3510 | NEXTL(l); |
| 3511 | /* |
| 3512 | * Pop-up of finished entities. |
| 3513 | */ |
| 3514 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 3515 | xmlPopInput(ctxt); |
| 3516 | |
| 3517 | GROW; |
| 3518 | c = CUR_CHAR(l); |
| 3519 | if (c == 0) { |
| 3520 | GROW; |
| 3521 | c = CUR_CHAR(l); |
| 3522 | } |
| 3523 | } |
| 3524 | buf[len] = 0; |
| 3525 | |
| 3526 | /* |
| 3527 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 3528 | * reference constructs. Note Charref will be handled in |
| 3529 | * xmlStringDecodeEntities() |
| 3530 | */ |
| 3531 | cur = buf; |
| 3532 | while (*cur != 0) { /* non input consuming */ |
| 3533 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 3534 | xmlChar *name; |
| 3535 | xmlChar tmp = *cur; |
| 3536 | |
| 3537 | cur++; |
| 3538 | name = xmlParseStringName(ctxt, &cur); |
| 3539 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3540 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3541 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3542 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3543 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 3544 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 3545 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3546 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3547 | } |
| 3548 | if (name != NULL) |
| 3549 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3550 | if (*cur == 0) |
| 3551 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3552 | } |
| 3553 | cur++; |
| 3554 | } |
| 3555 | |
| 3556 | /* |
| 3557 | * Then PEReference entities are substituted. |
| 3558 | */ |
| 3559 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3560 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3561 | xmlFree(buf); |
| 3562 | } else { |
| 3563 | NEXT; |
| 3564 | /* |
| 3565 | * NOTE: 4.4.7 Bypassed |
| 3566 | * When a general entity reference appears in the EntityValue in |
| 3567 | * an entity declaration, it is bypassed and left as is. |
| 3568 | * so XML_SUBSTITUTE_REF is not set here. |
| 3569 | */ |
| 3570 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 3571 | 0, 0, 0); |
| 3572 | if (orig != NULL) |
| 3573 | *orig = buf; |
| 3574 | else |
| 3575 | xmlFree(buf); |
| 3576 | } |
| 3577 | |
| 3578 | return(ret); |
| 3579 | } |
| 3580 | |
| 3581 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3582 | * xmlParseAttValueComplex: |
| 3583 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3584 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3585 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3586 | * |
| 3587 | * parse a value for an attribute, this is the fallback function |
| 3588 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3589 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3590 | * |
| 3591 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3592 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3593 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3594 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 3595 | xmlChar limit = 0; |
| 3596 | xmlChar *buf = NULL; |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3597 | xmlChar *rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3598 | int len = 0; |
| 3599 | int buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3600 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3601 | xmlChar *current = NULL; |
| 3602 | xmlEntityPtr ent; |
| 3603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3604 | if (NXT(0) == '"') { |
| 3605 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3606 | limit = '"'; |
| 3607 | NEXT; |
| 3608 | } else if (NXT(0) == '\'') { |
| 3609 | limit = '\''; |
| 3610 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 3611 | NEXT; |
| 3612 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3613 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3614 | return(NULL); |
| 3615 | } |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3616 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3617 | /* |
| 3618 | * allocate a translation buffer. |
| 3619 | */ |
| 3620 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3621 | buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3622 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3623 | |
| 3624 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3625 | * 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] | 3626 | */ |
| 3627 | c = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3628 | while ((NXT(0) != limit) && /* checked */ |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 3629 | (IS_CHAR(c)) && (c != '<')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3630 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3631 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 3632 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3633 | if (NXT(1) == '#') { |
| 3634 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3635 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3636 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3637 | if (ctxt->replaceEntities) { |
| 3638 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3639 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3640 | } |
| 3641 | buf[len++] = '&'; |
| 3642 | } else { |
| 3643 | /* |
| 3644 | * The reparsing will be done in xmlStringGetNodeList() |
| 3645 | * called by the attribute() function in SAX.c |
| 3646 | */ |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3647 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3648 | growBuffer(buf, 10); |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 3649 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3650 | buf[len++] = '&'; |
| 3651 | buf[len++] = '#'; |
| 3652 | buf[len++] = '3'; |
| 3653 | buf[len++] = '8'; |
| 3654 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3655 | } |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 3656 | } else if (val != 0) { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3657 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3658 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3659 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3660 | len += xmlCopyChar(0, &buf[len], val); |
| 3661 | } |
| 3662 | } else { |
| 3663 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 3664 | ctxt->nbentities++; |
| 3665 | if (ent != NULL) |
| 3666 | ctxt->nbentities += ent->owner; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3667 | if ((ent != NULL) && |
| 3668 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 3669 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3670 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3671 | } |
| 3672 | if ((ctxt->replaceEntities == 0) && |
| 3673 | (ent->content[0] == '&')) { |
| 3674 | buf[len++] = '&'; |
| 3675 | buf[len++] = '#'; |
| 3676 | buf[len++] = '3'; |
| 3677 | buf[len++] = '8'; |
| 3678 | buf[len++] = ';'; |
| 3679 | } else { |
| 3680 | buf[len++] = ent->content[0]; |
| 3681 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3682 | } else if ((ent != NULL) && |
| 3683 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3684 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 3685 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3686 | XML_SUBSTITUTE_REF, |
| 3687 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3688 | if (rep != NULL) { |
| 3689 | current = rep; |
| 3690 | while (*current != 0) { /* non input consuming */ |
| 3691 | buf[len++] = *current++; |
| 3692 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3693 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3694 | } |
| 3695 | } |
| 3696 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3697 | rep = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3698 | } |
| 3699 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3700 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3701 | growBuffer(buf, 10); |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 3702 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3703 | if (ent->content != NULL) |
| 3704 | buf[len++] = ent->content[0]; |
| 3705 | } |
| 3706 | } else if (ent != NULL) { |
| 3707 | int i = xmlStrlen(ent->name); |
| 3708 | const xmlChar *cur = ent->name; |
| 3709 | |
| 3710 | /* |
| 3711 | * This may look absurd but is needed to detect |
| 3712 | * entities problems |
| 3713 | */ |
| 3714 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 3715 | (ent->content != NULL)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3716 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3717 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 3718 | if (rep != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3719 | xmlFree(rep); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3720 | rep = NULL; |
| 3721 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | * Just output the reference |
| 3726 | */ |
| 3727 | buf[len++] = '&'; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3728 | while (len > buf_size - i - 10) { |
| 3729 | growBuffer(buf, i + 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3730 | } |
| 3731 | for (;i > 0;i--) |
| 3732 | buf[len++] = *cur++; |
| 3733 | buf[len++] = ';'; |
| 3734 | } |
| 3735 | } |
| 3736 | } else { |
| 3737 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3738 | if ((len != 0) || (!normalize)) { |
| 3739 | if ((!normalize) || (!in_space)) { |
| 3740 | COPY_BUF(l,buf,len,0x20); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3741 | while (len > buf_size - 10) { |
| 3742 | growBuffer(buf, 10); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3743 | } |
| 3744 | } |
| 3745 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3746 | } |
| 3747 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3748 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3749 | COPY_BUF(l,buf,len,c); |
| 3750 | if (len > buf_size - 10) { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 3751 | growBuffer(buf, 10); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3752 | } |
| 3753 | } |
| 3754 | NEXTL(l); |
| 3755 | } |
| 3756 | GROW; |
| 3757 | c = CUR_CHAR(l); |
| 3758 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3759 | if ((in_space) && (normalize)) { |
| 3760 | while (buf[len - 1] == 0x20) len--; |
| 3761 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3762 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3763 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3764 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3765 | } else if (RAW != limit) { |
Daniel Veillard | b9e5acc | 2007-06-12 13:43:00 +0000 | [diff] [blame] | 3766 | if ((c != 0) && (!IS_CHAR(c))) { |
| 3767 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_CHAR, |
| 3768 | "invalid character in attribute value\n"); |
| 3769 | } else { |
| 3770 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 3771 | "AttValue: ' expected\n"); |
| 3772 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3773 | } else |
| 3774 | NEXT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3775 | if (attlen != NULL) *attlen = len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3776 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3777 | |
| 3778 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3779 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 3780 | if (buf != NULL) |
| 3781 | xmlFree(buf); |
| 3782 | if (rep != NULL) |
| 3783 | xmlFree(rep); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3784 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3788 | * xmlParseAttValue: |
| 3789 | * @ctxt: an XML parser context |
| 3790 | * |
| 3791 | * parse a value for an attribute |
| 3792 | * Note: the parser won't do substitution of entities here, this |
| 3793 | * will be handled later in xmlStringGetNodeList |
| 3794 | * |
| 3795 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 3796 | * "'" ([^<&'] | Reference)* "'" |
| 3797 | * |
| 3798 | * 3.3.3 Attribute-Value Normalization: |
| 3799 | * Before the value of an attribute is passed to the application or |
| 3800 | * checked for validity, the XML processor must normalize it as follows: |
| 3801 | * - a character reference is processed by appending the referenced |
| 3802 | * character to the attribute value |
| 3803 | * - an entity reference is processed by recursively processing the |
| 3804 | * replacement text of the entity |
| 3805 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 3806 | * appending #x20 to the normalized value, except that only a single |
| 3807 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 3808 | * parsed entity or the literal entity value of an internal parsed entity |
| 3809 | * - other characters are processed by appending them to the normalized value |
| 3810 | * If the declared value is not CDATA, then the XML processor must further |
| 3811 | * process the normalized attribute value by discarding any leading and |
| 3812 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3813 | * (#x20) characters by a single space (#x20) character. |
| 3814 | * All attributes for which no declaration has been read should be treated |
| 3815 | * by a non-validating parser as if declared CDATA. |
| 3816 | * |
| 3817 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3818 | */ |
| 3819 | |
| 3820 | |
| 3821 | xmlChar * |
| 3822 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 3823 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3824 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3828 | * xmlParseSystemLiteral: |
| 3829 | * @ctxt: an XML parser context |
| 3830 | * |
| 3831 | * parse an XML Literal |
| 3832 | * |
| 3833 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 3834 | * |
| 3835 | * Returns the SystemLiteral parsed or NULL |
| 3836 | */ |
| 3837 | |
| 3838 | xmlChar * |
| 3839 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 3840 | xmlChar *buf = NULL; |
| 3841 | int len = 0; |
| 3842 | int size = XML_PARSER_BUFFER_SIZE; |
| 3843 | int cur, l; |
| 3844 | xmlChar stop; |
| 3845 | int state = ctxt->instate; |
| 3846 | int count = 0; |
| 3847 | |
| 3848 | SHRINK; |
| 3849 | if (RAW == '"') { |
| 3850 | NEXT; |
| 3851 | stop = '"'; |
| 3852 | } else if (RAW == '\'') { |
| 3853 | NEXT; |
| 3854 | stop = '\''; |
| 3855 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3856 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3857 | return(NULL); |
| 3858 | } |
| 3859 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3860 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3861 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3862 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3863 | return(NULL); |
| 3864 | } |
| 3865 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 3866 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3867 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3868 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3869 | xmlChar *tmp; |
| 3870 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3871 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3872 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3873 | if (tmp == NULL) { |
| 3874 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3875 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | ctxt->instate = (xmlParserInputState) state; |
| 3877 | return(NULL); |
| 3878 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3879 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3880 | } |
| 3881 | count++; |
| 3882 | if (count > 50) { |
| 3883 | GROW; |
| 3884 | count = 0; |
| 3885 | } |
| 3886 | COPY_BUF(l,buf,len,cur); |
| 3887 | NEXTL(l); |
| 3888 | cur = CUR_CHAR(l); |
| 3889 | if (cur == 0) { |
| 3890 | GROW; |
| 3891 | SHRINK; |
| 3892 | cur = CUR_CHAR(l); |
| 3893 | } |
| 3894 | } |
| 3895 | buf[len] = 0; |
| 3896 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3897 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3898 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3899 | } else { |
| 3900 | NEXT; |
| 3901 | } |
| 3902 | return(buf); |
| 3903 | } |
| 3904 | |
| 3905 | /** |
| 3906 | * xmlParsePubidLiteral: |
| 3907 | * @ctxt: an XML parser context |
| 3908 | * |
| 3909 | * parse an XML public literal |
| 3910 | * |
| 3911 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 3912 | * |
| 3913 | * Returns the PubidLiteral parsed or NULL. |
| 3914 | */ |
| 3915 | |
| 3916 | xmlChar * |
| 3917 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 3918 | xmlChar *buf = NULL; |
| 3919 | int len = 0; |
| 3920 | int size = XML_PARSER_BUFFER_SIZE; |
| 3921 | xmlChar cur; |
| 3922 | xmlChar stop; |
| 3923 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3924 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3925 | |
| 3926 | SHRINK; |
| 3927 | if (RAW == '"') { |
| 3928 | NEXT; |
| 3929 | stop = '"'; |
| 3930 | } else if (RAW == '\'') { |
| 3931 | NEXT; |
| 3932 | stop = '\''; |
| 3933 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3934 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3935 | return(NULL); |
| 3936 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3937 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3938 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3939 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3940 | return(NULL); |
| 3941 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3942 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3943 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3944 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3945 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3946 | xmlChar *tmp; |
| 3947 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3948 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3949 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3950 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3951 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3952 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3953 | return(NULL); |
| 3954 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3955 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3956 | } |
| 3957 | buf[len++] = cur; |
| 3958 | count++; |
| 3959 | if (count > 50) { |
| 3960 | GROW; |
| 3961 | count = 0; |
| 3962 | } |
| 3963 | NEXT; |
| 3964 | cur = CUR; |
| 3965 | if (cur == 0) { |
| 3966 | GROW; |
| 3967 | SHRINK; |
| 3968 | cur = CUR; |
| 3969 | } |
| 3970 | } |
| 3971 | buf[len] = 0; |
| 3972 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3973 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3974 | } else { |
| 3975 | NEXT; |
| 3976 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3977 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3978 | return(buf); |
| 3979 | } |
| 3980 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3981 | void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 3982 | |
| 3983 | /* |
| 3984 | * used for the test in the inner loop of the char data testing |
| 3985 | */ |
| 3986 | static const unsigned char test_char_data[256] = { |
| 3987 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3988 | 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x9, CR/LF separated */ |
| 3989 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3990 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 3991 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x00, 0x27, /* & */ |
| 3992 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, |
| 3993 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 3994 | 0x38, 0x39, 0x3A, 0x3B, 0x00, 0x3D, 0x3E, 0x3F, /* < */ |
| 3995 | 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 3996 | 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, |
| 3997 | 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, |
| 3998 | 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x00, 0x5E, 0x5F, /* ] */ |
| 3999 | 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 4000 | 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, |
| 4001 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 4002 | 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, |
| 4003 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* non-ascii */ |
| 4004 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4005 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4006 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4007 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4008 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4009 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4010 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4011 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4012 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4013 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4014 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4015 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4016 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4017 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 4018 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 4019 | }; |
| 4020 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4021 | /** |
| 4022 | * xmlParseCharData: |
| 4023 | * @ctxt: an XML parser context |
| 4024 | * @cdata: int indicating whether we are within a CDATA section |
| 4025 | * |
| 4026 | * parse a CharData section. |
| 4027 | * if we are within a CDATA section ']]>' marks an end of section. |
| 4028 | * |
| 4029 | * The right angle bracket (>) may be represented using the string ">", |
| 4030 | * and must, for compatibility, be escaped using ">" or a character |
| 4031 | * reference when it appears in the string "]]>" in content, when that |
| 4032 | * string is not marking the end of a CDATA section. |
| 4033 | * |
| 4034 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 4035 | */ |
| 4036 | |
| 4037 | void |
| 4038 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4039 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4040 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4041 | int line = ctxt->input->line; |
| 4042 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4043 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4044 | |
| 4045 | SHRINK; |
| 4046 | GROW; |
| 4047 | /* |
| 4048 | * Accelerated common case where input don't need to be |
| 4049 | * modified before passing it to the handler. |
| 4050 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4051 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4052 | in = ctxt->input->cur; |
| 4053 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4054 | get_more_space: |
Daniel Veillard | 9e264ad | 2008-01-11 06:10:16 +0000 | [diff] [blame] | 4055 | while (*in == 0x20) { in++; ctxt->input->col++; } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4056 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4057 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4058 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4059 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4060 | } while (*in == 0xA); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4061 | goto get_more_space; |
| 4062 | } |
| 4063 | if (*in == '<') { |
| 4064 | nbchar = in - ctxt->input->cur; |
| 4065 | if (nbchar > 0) { |
| 4066 | const xmlChar *tmp = ctxt->input->cur; |
| 4067 | ctxt->input->cur = in; |
| 4068 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4069 | if ((ctxt->sax != NULL) && |
| 4070 | (ctxt->sax->ignorableWhitespace != |
| 4071 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4072 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4073 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4074 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4075 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4076 | } else { |
| 4077 | if (ctxt->sax->characters != NULL) |
| 4078 | ctxt->sax->characters(ctxt->userData, |
| 4079 | tmp, nbchar); |
| 4080 | if (*ctxt->space == -1) |
| 4081 | *ctxt->space = -2; |
| 4082 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4083 | } else if ((ctxt->sax != NULL) && |
| 4084 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4085 | ctxt->sax->characters(ctxt->userData, |
| 4086 | tmp, nbchar); |
| 4087 | } |
| 4088 | } |
| 4089 | return; |
| 4090 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4091 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4092 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4093 | ccol = ctxt->input->col; |
Daniel Veillard | 0a119eb | 2005-07-20 13:46:00 +0000 | [diff] [blame] | 4094 | while (test_char_data[*in]) { |
| 4095 | in++; |
| 4096 | ccol++; |
| 4097 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 4098 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4099 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4100 | do { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4101 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4102 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4103 | } while (*in == 0xA); |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 4104 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4105 | } |
| 4106 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4107 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4108 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4109 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4110 | return; |
| 4111 | } |
| 4112 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4113 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4114 | goto get_more; |
| 4115 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4116 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4117 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4118 | if ((ctxt->sax != NULL) && |
| 4119 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4120 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4121 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 4122 | const xmlChar *tmp = ctxt->input->cur; |
| 4123 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 4124 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4125 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 4126 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4127 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4128 | tmp, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4129 | } else { |
| 4130 | if (ctxt->sax->characters != NULL) |
| 4131 | ctxt->sax->characters(ctxt->userData, |
| 4132 | tmp, nbchar); |
| 4133 | if (*ctxt->space == -1) |
| 4134 | *ctxt->space = -2; |
| 4135 | } |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4136 | line = ctxt->input->line; |
| 4137 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 4138 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4139 | if (ctxt->sax->characters != NULL) |
| 4140 | ctxt->sax->characters(ctxt->userData, |
| 4141 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 4142 | line = ctxt->input->line; |
| 4143 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 4144 | } |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4145 | /* something really bad happened in the SAX callback */ |
| 4146 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4147 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4148 | } |
| 4149 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4150 | if (*in == 0xD) { |
| 4151 | in++; |
| 4152 | if (*in == 0xA) { |
| 4153 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4154 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4155 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4156 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4157 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4158 | in--; |
| 4159 | } |
| 4160 | if (*in == '<') { |
| 4161 | return; |
| 4162 | } |
| 4163 | if (*in == '&') { |
| 4164 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4165 | } |
| 4166 | SHRINK; |
| 4167 | GROW; |
| 4168 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4169 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4170 | nbchar = 0; |
| 4171 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4172 | ctxt->input->line = line; |
| 4173 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4174 | xmlParseCharDataComplex(ctxt, cdata); |
| 4175 | } |
| 4176 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4177 | /** |
| 4178 | * xmlParseCharDataComplex: |
| 4179 | * @ctxt: an XML parser context |
| 4180 | * @cdata: int indicating whether we are within a CDATA section |
| 4181 | * |
| 4182 | * parse a CharData section.this is the fallback function |
| 4183 | * of xmlParseCharData() when the parsing requires handling |
| 4184 | * of non-ASCII characters. |
| 4185 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4186 | void |
| 4187 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4188 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4189 | int nbchar = 0; |
| 4190 | int cur, l; |
| 4191 | int count = 0; |
| 4192 | |
| 4193 | SHRINK; |
| 4194 | GROW; |
| 4195 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4196 | while ((cur != '<') && /* checked */ |
| 4197 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4198 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4199 | if ((cur == ']') && (NXT(1) == ']') && |
| 4200 | (NXT(2) == '>')) { |
| 4201 | if (cdata) break; |
| 4202 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4203 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4204 | } |
| 4205 | } |
| 4206 | COPY_BUF(l,buf,nbchar,cur); |
| 4207 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4208 | buf[nbchar] = 0; |
| 4209 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4211 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4212 | */ |
| 4213 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4214 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4215 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4216 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4217 | buf, nbchar); |
| 4218 | } else { |
| 4219 | if (ctxt->sax->characters != NULL) |
| 4220 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4221 | if ((ctxt->sax->characters != |
| 4222 | ctxt->sax->ignorableWhitespace) && |
| 4223 | (*ctxt->space == -1)) |
| 4224 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4225 | } |
| 4226 | } |
| 4227 | nbchar = 0; |
Daniel Veillard | 1dc9feb | 2008-11-17 15:59:21 +0000 | [diff] [blame] | 4228 | /* something really bad happened in the SAX callback */ |
| 4229 | if (ctxt->instate != XML_PARSER_CONTENT) |
| 4230 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4231 | } |
| 4232 | count++; |
| 4233 | if (count > 50) { |
| 4234 | GROW; |
| 4235 | count = 0; |
| 4236 | } |
| 4237 | NEXTL(l); |
| 4238 | cur = CUR_CHAR(l); |
| 4239 | } |
| 4240 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4241 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4242 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4243 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4244 | */ |
| 4245 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4246 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4247 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4248 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4249 | } else { |
| 4250 | if (ctxt->sax->characters != NULL) |
| 4251 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4252 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4253 | (*ctxt->space == -1)) |
| 4254 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4255 | } |
| 4256 | } |
| 4257 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4258 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4259 | /* Generate the error and skip the offending character */ |
| 4260 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4261 | "PCDATA invalid Char value %d\n", |
| 4262 | cur); |
| 4263 | NEXTL(l); |
| 4264 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | /** |
| 4268 | * xmlParseExternalID: |
| 4269 | * @ctxt: an XML parser context |
| 4270 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4271 | * @strict: indicate whether we should restrict parsing to only |
| 4272 | * production [75], see NOTE below |
| 4273 | * |
| 4274 | * Parse an External ID or a Public ID |
| 4275 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4276 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4277 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4278 | * |
| 4279 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4280 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4281 | * |
| 4282 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4283 | * |
| 4284 | * Returns the function returns SystemLiteral and in the second |
| 4285 | * case publicID receives PubidLiteral, is strict is off |
| 4286 | * it is possible to return NULL and have publicID set. |
| 4287 | */ |
| 4288 | |
| 4289 | xmlChar * |
| 4290 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4291 | xmlChar *URI = NULL; |
| 4292 | |
| 4293 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4294 | |
| 4295 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4296 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4297 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4298 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4299 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4300 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4301 | } |
| 4302 | SKIP_BLANKS; |
| 4303 | URI = xmlParseSystemLiteral(ctxt); |
| 4304 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4305 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4306 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4307 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4308 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4309 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4310 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4311 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4312 | } |
| 4313 | SKIP_BLANKS; |
| 4314 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4315 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4316 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4317 | } |
| 4318 | if (strict) { |
| 4319 | /* |
| 4320 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4321 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4322 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4323 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4324 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4325 | } |
| 4326 | } else { |
| 4327 | /* |
| 4328 | * We handle [83] so we return immediately, if |
| 4329 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4330 | * point of view that's a nice mess. |
| 4331 | */ |
| 4332 | const xmlChar *ptr; |
| 4333 | GROW; |
| 4334 | |
| 4335 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4336 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4337 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4338 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4340 | } |
| 4341 | SKIP_BLANKS; |
| 4342 | URI = xmlParseSystemLiteral(ctxt); |
| 4343 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4344 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4345 | } |
| 4346 | } |
| 4347 | return(URI); |
| 4348 | } |
| 4349 | |
| 4350 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4351 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4352 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4353 | * @buf: the already parsed part of the buffer |
| 4354 | * @len: number of bytes filles in the buffer |
| 4355 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4356 | * |
| 4357 | * Skip an XML (SGML) comment <!-- .... --> |
| 4358 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4359 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4360 | * 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] | 4361 | * |
| 4362 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4363 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4364 | static void |
| 4365 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4366 | int q, ql; |
| 4367 | int r, rl; |
| 4368 | int cur, l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4369 | int count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4370 | int inputid; |
| 4371 | |
| 4372 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4373 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4374 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4375 | len = 0; |
| 4376 | size = XML_PARSER_BUFFER_SIZE; |
| 4377 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4378 | if (buf == NULL) { |
| 4379 | xmlErrMemory(ctxt, NULL); |
| 4380 | return; |
| 4381 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4382 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4383 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4384 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4385 | if (q == 0) |
| 4386 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4387 | if (!IS_CHAR(q)) { |
| 4388 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4389 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4390 | q); |
| 4391 | xmlFree (buf); |
| 4392 | return; |
| 4393 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4394 | NEXTL(ql); |
| 4395 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4396 | if (r == 0) |
| 4397 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4398 | if (!IS_CHAR(r)) { |
| 4399 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4400 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4401 | q); |
| 4402 | xmlFree (buf); |
| 4403 | return; |
| 4404 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4405 | NEXTL(rl); |
| 4406 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4407 | if (cur == 0) |
| 4408 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4409 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4410 | ((cur != '>') || |
| 4411 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4412 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4413 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4414 | } |
| 4415 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4416 | xmlChar *new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4417 | size *= 2; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4418 | new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4419 | if (new_buf == NULL) { |
| 4420 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4421 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4422 | return; |
| 4423 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4424 | buf = new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4425 | } |
| 4426 | COPY_BUF(ql,buf,len,q); |
| 4427 | q = r; |
| 4428 | ql = rl; |
| 4429 | r = cur; |
| 4430 | rl = l; |
| 4431 | |
| 4432 | count++; |
| 4433 | if (count > 50) { |
| 4434 | GROW; |
| 4435 | count = 0; |
| 4436 | } |
| 4437 | NEXTL(l); |
| 4438 | cur = CUR_CHAR(l); |
| 4439 | if (cur == 0) { |
| 4440 | SHRINK; |
| 4441 | GROW; |
| 4442 | cur = CUR_CHAR(l); |
| 4443 | } |
| 4444 | } |
| 4445 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4446 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4447 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4448 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4449 | } else if (!IS_CHAR(cur)) { |
| 4450 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4451 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4452 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4453 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4454 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4455 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4456 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4457 | } |
| 4458 | NEXT; |
| 4459 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4460 | (!ctxt->disableSAX)) |
| 4461 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4462 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4463 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4464 | return; |
| 4465 | not_terminated: |
| 4466 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4467 | "Comment not terminated\n", NULL); |
| 4468 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4469 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4470 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4471 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4472 | /** |
| 4473 | * xmlParseComment: |
| 4474 | * @ctxt: an XML parser context |
| 4475 | * |
| 4476 | * Skip an XML (SGML) comment <!-- .... --> |
| 4477 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4478 | * must not occur within comments. " |
| 4479 | * |
| 4480 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4481 | */ |
| 4482 | void |
| 4483 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4484 | xmlChar *buf = NULL; |
| 4485 | int size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 4486 | int len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4487 | xmlParserInputState state; |
| 4488 | const xmlChar *in; |
| 4489 | int nbchar = 0, ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4490 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4491 | |
| 4492 | /* |
| 4493 | * Check that there is a comment right here. |
| 4494 | */ |
| 4495 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4496 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4497 | state = ctxt->instate; |
| 4498 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4499 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4500 | SKIP(4); |
| 4501 | SHRINK; |
| 4502 | GROW; |
| 4503 | |
| 4504 | /* |
| 4505 | * Accelerated common case where input don't need to be |
| 4506 | * modified before passing it to the handler. |
| 4507 | */ |
| 4508 | in = ctxt->input->cur; |
| 4509 | do { |
| 4510 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4511 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4512 | ctxt->input->line++; ctxt->input->col = 1; |
| 4513 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4514 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4515 | } |
| 4516 | get_more: |
| 4517 | ccol = ctxt->input->col; |
| 4518 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4519 | ((*in >= 0x20) && (*in < '-')) || |
| 4520 | (*in == 0x09)) { |
| 4521 | in++; |
| 4522 | ccol++; |
| 4523 | } |
| 4524 | ctxt->input->col = ccol; |
| 4525 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4526 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4527 | ctxt->input->line++; ctxt->input->col = 1; |
| 4528 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4529 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4530 | goto get_more; |
| 4531 | } |
| 4532 | nbchar = in - ctxt->input->cur; |
| 4533 | /* |
| 4534 | * save current set of data |
| 4535 | */ |
| 4536 | if (nbchar > 0) { |
| 4537 | if ((ctxt->sax != NULL) && |
| 4538 | (ctxt->sax->comment != NULL)) { |
| 4539 | if (buf == NULL) { |
| 4540 | if ((*in == '-') && (in[1] == '-')) |
| 4541 | size = nbchar + 1; |
| 4542 | else |
| 4543 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4544 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4545 | if (buf == NULL) { |
| 4546 | xmlErrMemory(ctxt, NULL); |
| 4547 | ctxt->instate = state; |
| 4548 | return; |
| 4549 | } |
| 4550 | len = 0; |
| 4551 | } else if (len + nbchar + 1 >= size) { |
| 4552 | xmlChar *new_buf; |
| 4553 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4554 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4555 | size * sizeof(xmlChar)); |
| 4556 | if (new_buf == NULL) { |
| 4557 | xmlFree (buf); |
| 4558 | xmlErrMemory(ctxt, NULL); |
| 4559 | ctxt->instate = state; |
| 4560 | return; |
| 4561 | } |
| 4562 | buf = new_buf; |
| 4563 | } |
| 4564 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4565 | len += nbchar; |
| 4566 | buf[len] = 0; |
| 4567 | } |
| 4568 | } |
| 4569 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4570 | if (*in == 0xA) { |
| 4571 | in++; |
| 4572 | ctxt->input->line++; ctxt->input->col = 1; |
| 4573 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4574 | if (*in == 0xD) { |
| 4575 | in++; |
| 4576 | if (*in == 0xA) { |
| 4577 | ctxt->input->cur = in; |
| 4578 | in++; |
| 4579 | ctxt->input->line++; ctxt->input->col = 1; |
| 4580 | continue; /* while */ |
| 4581 | } |
| 4582 | in--; |
| 4583 | } |
| 4584 | SHRINK; |
| 4585 | GROW; |
| 4586 | in = ctxt->input->cur; |
| 4587 | if (*in == '-') { |
| 4588 | if (in[1] == '-') { |
| 4589 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4590 | if (ctxt->input->id != inputid) { |
| 4591 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4592 | "comment doesn't start and stop in the same entity\n"); |
| 4593 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4594 | SKIP(3); |
| 4595 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4596 | (!ctxt->disableSAX)) { |
| 4597 | if (buf != NULL) |
| 4598 | ctxt->sax->comment(ctxt->userData, buf); |
| 4599 | else |
| 4600 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 4601 | } |
| 4602 | if (buf != NULL) |
| 4603 | xmlFree(buf); |
| 4604 | ctxt->instate = state; |
| 4605 | return; |
| 4606 | } |
| 4607 | if (buf != NULL) |
| 4608 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4609 | "Comment not terminated \n<!--%.50s\n", |
| 4610 | buf); |
| 4611 | else |
| 4612 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4613 | "Comment not terminated \n", NULL); |
| 4614 | in++; |
| 4615 | ctxt->input->col++; |
| 4616 | } |
| 4617 | in++; |
| 4618 | ctxt->input->col++; |
| 4619 | goto get_more; |
| 4620 | } |
| 4621 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 4622 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 4623 | ctxt->instate = state; |
| 4624 | return; |
| 4625 | } |
| 4626 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4627 | |
| 4628 | /** |
| 4629 | * xmlParsePITarget: |
| 4630 | * @ctxt: an XML parser context |
| 4631 | * |
| 4632 | * parse the name of a PI |
| 4633 | * |
| 4634 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 4635 | * |
| 4636 | * Returns the PITarget name or NULL |
| 4637 | */ |
| 4638 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4639 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4641 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4642 | |
| 4643 | name = xmlParseName(ctxt); |
| 4644 | if ((name != NULL) && |
| 4645 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 4646 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 4647 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 4648 | int i; |
| 4649 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 4650 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4651 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4652 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4653 | return(name); |
| 4654 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4655 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4656 | return(name); |
| 4657 | } |
| 4658 | for (i = 0;;i++) { |
| 4659 | if (xmlW3CPIs[i] == NULL) break; |
| 4660 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 4661 | return(name); |
| 4662 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4663 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 4664 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 4665 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4666 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4667 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
| 4668 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4669 | "colon are forbidden from PI names '%s'\n", name, NULL, NULL); |
| 4670 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4671 | return(name); |
| 4672 | } |
| 4673 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4674 | #ifdef LIBXML_CATALOG_ENABLED |
| 4675 | /** |
| 4676 | * xmlParseCatalogPI: |
| 4677 | * @ctxt: an XML parser context |
| 4678 | * @catalog: the PI value string |
| 4679 | * |
| 4680 | * parse an XML Catalog Processing Instruction. |
| 4681 | * |
| 4682 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 4683 | * |
| 4684 | * Occurs only if allowed by the user and if happening in the Misc |
| 4685 | * part of the document before any doctype informations |
| 4686 | * This will add the given catalog to the parsing context in order |
| 4687 | * to be used if there is a resolution need further down in the document |
| 4688 | */ |
| 4689 | |
| 4690 | static void |
| 4691 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 4692 | xmlChar *URL = NULL; |
| 4693 | const xmlChar *tmp, *base; |
| 4694 | xmlChar marker; |
| 4695 | |
| 4696 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4697 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4698 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 4699 | goto error; |
| 4700 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4701 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4702 | if (*tmp != '=') { |
| 4703 | return; |
| 4704 | } |
| 4705 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4706 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4707 | marker = *tmp; |
| 4708 | if ((marker != '\'') && (marker != '"')) |
| 4709 | goto error; |
| 4710 | tmp++; |
| 4711 | base = tmp; |
| 4712 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 4713 | if (*tmp == 0) |
| 4714 | goto error; |
| 4715 | URL = xmlStrndup(base, tmp - base); |
| 4716 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4717 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4718 | if (*tmp != 0) |
| 4719 | goto error; |
| 4720 | |
| 4721 | if (URL != NULL) { |
| 4722 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 4723 | xmlFree(URL); |
| 4724 | } |
| 4725 | return; |
| 4726 | |
| 4727 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4728 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 4729 | "Catalog PI syntax error: %s\n", |
| 4730 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4731 | if (URL != NULL) |
| 4732 | xmlFree(URL); |
| 4733 | } |
| 4734 | #endif |
| 4735 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4736 | /** |
| 4737 | * xmlParsePI: |
| 4738 | * @ctxt: an XML parser context |
| 4739 | * |
| 4740 | * parse an XML Processing Instruction. |
| 4741 | * |
| 4742 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 4743 | * |
| 4744 | * The processing is transfered to SAX once parsed. |
| 4745 | */ |
| 4746 | |
| 4747 | void |
| 4748 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 4749 | xmlChar *buf = NULL; |
| 4750 | int len = 0; |
| 4751 | int size = XML_PARSER_BUFFER_SIZE; |
| 4752 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4753 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4754 | xmlParserInputState state; |
| 4755 | int count = 0; |
| 4756 | |
| 4757 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 4758 | xmlParserInputPtr input = ctxt->input; |
| 4759 | state = ctxt->instate; |
| 4760 | ctxt->instate = XML_PARSER_PI; |
| 4761 | /* |
| 4762 | * this is a Processing Instruction. |
| 4763 | */ |
| 4764 | SKIP(2); |
| 4765 | SHRINK; |
| 4766 | |
| 4767 | /* |
| 4768 | * Parse the target name and check for special support like |
| 4769 | * namespace. |
| 4770 | */ |
| 4771 | target = xmlParsePITarget(ctxt); |
| 4772 | if (target != NULL) { |
| 4773 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 4774 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4775 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4776 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4777 | } |
| 4778 | SKIP(2); |
| 4779 | |
| 4780 | /* |
| 4781 | * SAX: PI detected. |
| 4782 | */ |
| 4783 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 4784 | (ctxt->sax->processingInstruction != NULL)) |
| 4785 | ctxt->sax->processingInstruction(ctxt->userData, |
| 4786 | target, NULL); |
| 4787 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | return; |
| 4789 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4790 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4791 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4792 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4793 | ctxt->instate = state; |
| 4794 | return; |
| 4795 | } |
| 4796 | cur = CUR; |
| 4797 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4798 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4799 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4800 | } |
| 4801 | SKIP_BLANKS; |
| 4802 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4803 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4804 | ((cur != '?') || (NXT(1) != '>'))) { |
| 4805 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4806 | xmlChar *tmp; |
| 4807 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4808 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4809 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4810 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4811 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4812 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4813 | ctxt->instate = state; |
| 4814 | return; |
| 4815 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4816 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4817 | } |
| 4818 | count++; |
| 4819 | if (count > 50) { |
| 4820 | GROW; |
| 4821 | count = 0; |
| 4822 | } |
| 4823 | COPY_BUF(l,buf,len,cur); |
| 4824 | NEXTL(l); |
| 4825 | cur = CUR_CHAR(l); |
| 4826 | if (cur == 0) { |
| 4827 | SHRINK; |
| 4828 | GROW; |
| 4829 | cur = CUR_CHAR(l); |
| 4830 | } |
| 4831 | } |
| 4832 | buf[len] = 0; |
| 4833 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4834 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 4835 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4836 | } else { |
| 4837 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4838 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4839 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4840 | } |
| 4841 | SKIP(2); |
| 4842 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4843 | #ifdef LIBXML_CATALOG_ENABLED |
| 4844 | if (((state == XML_PARSER_MISC) || |
| 4845 | (state == XML_PARSER_START)) && |
| 4846 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 4847 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 4848 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 4849 | (allow == XML_CATA_ALLOW_ALL)) |
| 4850 | xmlParseCatalogPI(ctxt, buf); |
| 4851 | } |
| 4852 | #endif |
| 4853 | |
| 4854 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4855 | /* |
| 4856 | * SAX: PI detected. |
| 4857 | */ |
| 4858 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 4859 | (ctxt->sax->processingInstruction != NULL)) |
| 4860 | ctxt->sax->processingInstruction(ctxt->userData, |
| 4861 | target, buf); |
| 4862 | } |
| 4863 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4864 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4865 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4866 | } |
| 4867 | ctxt->instate = state; |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | /** |
| 4872 | * xmlParseNotationDecl: |
| 4873 | * @ctxt: an XML parser context |
| 4874 | * |
| 4875 | * parse a notation declaration |
| 4876 | * |
| 4877 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 4878 | * |
| 4879 | * Hence there is actually 3 choices: |
| 4880 | * 'PUBLIC' S PubidLiteral |
| 4881 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4882 | * and 'SYSTEM' S SystemLiteral |
| 4883 | * |
| 4884 | * See the NOTE on xmlParseExternalID(). |
| 4885 | */ |
| 4886 | |
| 4887 | void |
| 4888 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4889 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4890 | xmlChar *Pubid; |
| 4891 | xmlChar *Systemid; |
| 4892 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4893 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4894 | xmlParserInputPtr input = ctxt->input; |
| 4895 | SHRINK; |
| 4896 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4897 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4898 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4899 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4900 | return; |
| 4901 | } |
| 4902 | SKIP_BLANKS; |
| 4903 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4904 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4905 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4906 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4907 | return; |
| 4908 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4909 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4910 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4911 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4912 | return; |
| 4913 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4914 | if (xmlStrchr(name, ':') != NULL) { |
| 4915 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4916 | "colon are forbidden from notation names '%s'\n", |
| 4917 | name, NULL, NULL); |
| 4918 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4919 | SKIP_BLANKS; |
| 4920 | |
| 4921 | /* |
| 4922 | * Parse the IDs. |
| 4923 | */ |
| 4924 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 4925 | SKIP_BLANKS; |
| 4926 | |
| 4927 | if (RAW == '>') { |
| 4928 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4929 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4930 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4931 | } |
| 4932 | NEXT; |
| 4933 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4934 | (ctxt->sax->notationDecl != NULL)) |
| 4935 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 4936 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4937 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | if (Systemid != NULL) xmlFree(Systemid); |
| 4940 | if (Pubid != NULL) xmlFree(Pubid); |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | /** |
| 4945 | * xmlParseEntityDecl: |
| 4946 | * @ctxt: an XML parser context |
| 4947 | * |
| 4948 | * parse <!ENTITY declarations |
| 4949 | * |
| 4950 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 4951 | * |
| 4952 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 4953 | * |
| 4954 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 4955 | * |
| 4956 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 4957 | * |
| 4958 | * [74] PEDef ::= EntityValue | ExternalID |
| 4959 | * |
| 4960 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 4961 | * |
| 4962 | * [ VC: Notation Declared ] |
| 4963 | * The Name must match the declared name of a notation. |
| 4964 | */ |
| 4965 | |
| 4966 | void |
| 4967 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4968 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4969 | xmlChar *value = NULL; |
| 4970 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4971 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4972 | int isParameter = 0; |
| 4973 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4974 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4975 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4976 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4977 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4978 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4979 | SHRINK; |
| 4980 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4981 | skipped = SKIP_BLANKS; |
| 4982 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4983 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4984 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4985 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4986 | |
| 4987 | if (RAW == '%') { |
| 4988 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4989 | skipped = SKIP_BLANKS; |
| 4990 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4991 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4992 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4993 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4994 | isParameter = 1; |
| 4995 | } |
| 4996 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4997 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4998 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4999 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5000 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5001 | return; |
| 5002 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 5003 | if (xmlStrchr(name, ':') != NULL) { |
| 5004 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 5005 | "colon are forbidden from entities names '%s'\n", |
| 5006 | name, NULL, NULL); |
| 5007 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5008 | skipped = SKIP_BLANKS; |
| 5009 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5010 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5011 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5012 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5013 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5014 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5015 | /* |
| 5016 | * handle the various case of definitions... |
| 5017 | */ |
| 5018 | if (isParameter) { |
| 5019 | if ((RAW == '"') || (RAW == '\'')) { |
| 5020 | value = xmlParseEntityValue(ctxt, &orig); |
| 5021 | if (value) { |
| 5022 | if ((ctxt->sax != NULL) && |
| 5023 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5024 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5025 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5026 | NULL, NULL, value); |
| 5027 | } |
| 5028 | } else { |
| 5029 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5030 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5031 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5032 | } |
| 5033 | if (URI) { |
| 5034 | xmlURIPtr uri; |
| 5035 | |
| 5036 | uri = xmlParseURI((const char *) URI); |
| 5037 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5038 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5039 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5040 | /* |
| 5041 | * This really ought to be a well formedness error |
| 5042 | * but the XML Core WG decided otherwise c.f. issue |
| 5043 | * E26 of the XML erratas. |
| 5044 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5045 | } else { |
| 5046 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5047 | /* |
| 5048 | * Okay this is foolish to block those but not |
| 5049 | * invalid URIs. |
| 5050 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5051 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5052 | } else { |
| 5053 | if ((ctxt->sax != NULL) && |
| 5054 | (!ctxt->disableSAX) && |
| 5055 | (ctxt->sax->entityDecl != NULL)) |
| 5056 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5057 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5058 | literal, URI, NULL); |
| 5059 | } |
| 5060 | xmlFreeURI(uri); |
| 5061 | } |
| 5062 | } |
| 5063 | } |
| 5064 | } else { |
| 5065 | if ((RAW == '"') || (RAW == '\'')) { |
| 5066 | value = xmlParseEntityValue(ctxt, &orig); |
| 5067 | if ((ctxt->sax != NULL) && |
| 5068 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5069 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5070 | XML_INTERNAL_GENERAL_ENTITY, |
| 5071 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5072 | /* |
| 5073 | * For expat compatibility in SAX mode. |
| 5074 | */ |
| 5075 | if ((ctxt->myDoc == NULL) || |
| 5076 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5077 | if (ctxt->myDoc == NULL) { |
| 5078 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5079 | if (ctxt->myDoc == NULL) { |
| 5080 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5081 | return; |
| 5082 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5083 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5084 | } |
| 5085 | if (ctxt->myDoc->intSubset == NULL) |
| 5086 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5087 | BAD_CAST "fake", NULL, NULL); |
| 5088 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5089 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5090 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5091 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5092 | } else { |
| 5093 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5094 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5095 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5096 | } |
| 5097 | if (URI) { |
| 5098 | xmlURIPtr uri; |
| 5099 | |
| 5100 | uri = xmlParseURI((const char *)URI); |
| 5101 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5102 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5103 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5104 | /* |
| 5105 | * This really ought to be a well formedness error |
| 5106 | * but the XML Core WG decided otherwise c.f. issue |
| 5107 | * E26 of the XML erratas. |
| 5108 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5109 | } else { |
| 5110 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5111 | /* |
| 5112 | * Okay this is foolish to block those but not |
| 5113 | * invalid URIs. |
| 5114 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5115 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5116 | } |
| 5117 | xmlFreeURI(uri); |
| 5118 | } |
| 5119 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5120 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5121 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5122 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5123 | } |
| 5124 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5125 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5126 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5127 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5128 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5129 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | } |
| 5131 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5132 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5133 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5134 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5135 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5136 | literal, URI, ndata); |
| 5137 | } else { |
| 5138 | if ((ctxt->sax != NULL) && |
| 5139 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5140 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5141 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5142 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5143 | /* |
| 5144 | * For expat compatibility in SAX mode. |
| 5145 | * assuming the entity repalcement was asked for |
| 5146 | */ |
| 5147 | if ((ctxt->replaceEntities != 0) && |
| 5148 | ((ctxt->myDoc == NULL) || |
| 5149 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5150 | if (ctxt->myDoc == NULL) { |
| 5151 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5152 | if (ctxt->myDoc == NULL) { |
| 5153 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5154 | return; |
| 5155 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5156 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | if (ctxt->myDoc->intSubset == NULL) |
| 5160 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5161 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5162 | xmlSAX2EntityDecl(ctxt, name, |
| 5163 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5164 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5165 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | } |
| 5169 | SKIP_BLANKS; |
| 5170 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5171 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5172 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5173 | } else { |
| 5174 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5175 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5176 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5177 | } |
| 5178 | NEXT; |
| 5179 | } |
| 5180 | if (orig != NULL) { |
| 5181 | /* |
| 5182 | * Ugly mechanism to save the raw entity value. |
| 5183 | */ |
| 5184 | xmlEntityPtr cur = NULL; |
| 5185 | |
| 5186 | if (isParameter) { |
| 5187 | if ((ctxt->sax != NULL) && |
| 5188 | (ctxt->sax->getParameterEntity != NULL)) |
| 5189 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5190 | } else { |
| 5191 | if ((ctxt->sax != NULL) && |
| 5192 | (ctxt->sax->getEntity != NULL)) |
| 5193 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5194 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5195 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5196 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5197 | } |
| 5198 | if (cur != NULL) { |
| 5199 | if (cur->orig != NULL) |
| 5200 | xmlFree(orig); |
| 5201 | else |
| 5202 | cur->orig = orig; |
| 5203 | } else |
| 5204 | xmlFree(orig); |
| 5205 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5206 | if (value != NULL) xmlFree(value); |
| 5207 | if (URI != NULL) xmlFree(URI); |
| 5208 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | |
| 5212 | /** |
| 5213 | * xmlParseDefaultDecl: |
| 5214 | * @ctxt: an XML parser context |
| 5215 | * @value: Receive a possible fixed default value for the attribute |
| 5216 | * |
| 5217 | * Parse an attribute default declaration |
| 5218 | * |
| 5219 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5220 | * |
| 5221 | * [ VC: Required Attribute ] |
| 5222 | * if the default declaration is the keyword #REQUIRED, then the |
| 5223 | * attribute must be specified for all elements of the type in the |
| 5224 | * attribute-list declaration. |
| 5225 | * |
| 5226 | * [ VC: Attribute Default Legal ] |
| 5227 | * The declared default value must meet the lexical constraints of |
| 5228 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5229 | * |
| 5230 | * [ VC: Fixed Attribute Default ] |
| 5231 | * if an attribute has a default value declared with the #FIXED |
| 5232 | * keyword, instances of that attribute must match the default value. |
| 5233 | * |
| 5234 | * [ WFC: No < in Attribute Values ] |
| 5235 | * handled in xmlParseAttValue() |
| 5236 | * |
| 5237 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 5238 | * or XML_ATTRIBUTE_FIXED. |
| 5239 | */ |
| 5240 | |
| 5241 | int |
| 5242 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5243 | int val; |
| 5244 | xmlChar *ret; |
| 5245 | |
| 5246 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5247 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5248 | SKIP(9); |
| 5249 | return(XML_ATTRIBUTE_REQUIRED); |
| 5250 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5251 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5252 | SKIP(8); |
| 5253 | return(XML_ATTRIBUTE_IMPLIED); |
| 5254 | } |
| 5255 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5256 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5257 | SKIP(6); |
| 5258 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5259 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5260 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5261 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | } |
| 5263 | SKIP_BLANKS; |
| 5264 | } |
| 5265 | ret = xmlParseAttValue(ctxt); |
| 5266 | ctxt->instate = XML_PARSER_DTD; |
| 5267 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5268 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5269 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5270 | } else |
| 5271 | *value = ret; |
| 5272 | return(val); |
| 5273 | } |
| 5274 | |
| 5275 | /** |
| 5276 | * xmlParseNotationType: |
| 5277 | * @ctxt: an XML parser context |
| 5278 | * |
| 5279 | * parse an Notation attribute type. |
| 5280 | * |
| 5281 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5282 | * |
| 5283 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5284 | * |
| 5285 | * [ VC: Notation Attributes ] |
| 5286 | * Values of this type must match one of the notation names included |
| 5287 | * in the declaration; all notation names in the declaration must be declared. |
| 5288 | * |
| 5289 | * Returns: the notation attribute tree built while parsing |
| 5290 | */ |
| 5291 | |
| 5292 | xmlEnumerationPtr |
| 5293 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5294 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5295 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5296 | |
| 5297 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5298 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5299 | return(NULL); |
| 5300 | } |
| 5301 | SHRINK; |
| 5302 | do { |
| 5303 | NEXT; |
| 5304 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5305 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5306 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5307 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5308 | "Name expected in NOTATION declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5309 | return(ret); |
| 5310 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5311 | tmp = ret; |
| 5312 | while (tmp != NULL) { |
| 5313 | if (xmlStrEqual(name, tmp->name)) { |
| 5314 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5315 | "standalone: attribute notation value token %s duplicated\n", |
| 5316 | name, NULL); |
| 5317 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5318 | xmlFree((xmlChar *) name); |
| 5319 | break; |
| 5320 | } |
| 5321 | tmp = tmp->next; |
| 5322 | } |
| 5323 | if (tmp == NULL) { |
| 5324 | cur = xmlCreateEnumeration(name); |
| 5325 | if (cur == NULL) return(ret); |
| 5326 | if (last == NULL) ret = last = cur; |
| 5327 | else { |
| 5328 | last->next = cur; |
| 5329 | last = cur; |
| 5330 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5331 | } |
| 5332 | SKIP_BLANKS; |
| 5333 | } while (RAW == '|'); |
| 5334 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5335 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5336 | if ((last != NULL) && (last != ret)) |
| 5337 | xmlFreeEnumeration(last); |
| 5338 | return(ret); |
| 5339 | } |
| 5340 | NEXT; |
| 5341 | return(ret); |
| 5342 | } |
| 5343 | |
| 5344 | /** |
| 5345 | * xmlParseEnumerationType: |
| 5346 | * @ctxt: an XML parser context |
| 5347 | * |
| 5348 | * parse an Enumeration attribute type. |
| 5349 | * |
| 5350 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5351 | * |
| 5352 | * [ VC: Enumeration ] |
| 5353 | * Values of this type must match one of the Nmtoken tokens in |
| 5354 | * the declaration |
| 5355 | * |
| 5356 | * Returns: the enumeration attribute tree built while parsing |
| 5357 | */ |
| 5358 | |
| 5359 | xmlEnumerationPtr |
| 5360 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5361 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5362 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5363 | |
| 5364 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5365 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5366 | return(NULL); |
| 5367 | } |
| 5368 | SHRINK; |
| 5369 | do { |
| 5370 | NEXT; |
| 5371 | SKIP_BLANKS; |
| 5372 | name = xmlParseNmtoken(ctxt); |
| 5373 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5374 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5375 | return(ret); |
| 5376 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5377 | tmp = ret; |
| 5378 | while (tmp != NULL) { |
| 5379 | if (xmlStrEqual(name, tmp->name)) { |
| 5380 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5381 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5382 | name, NULL); |
| 5383 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5384 | xmlFree(name); |
| 5385 | break; |
| 5386 | } |
| 5387 | tmp = tmp->next; |
| 5388 | } |
| 5389 | if (tmp == NULL) { |
| 5390 | cur = xmlCreateEnumeration(name); |
| 5391 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5392 | xmlFree(name); |
| 5393 | if (cur == NULL) return(ret); |
| 5394 | if (last == NULL) ret = last = cur; |
| 5395 | else { |
| 5396 | last->next = cur; |
| 5397 | last = cur; |
| 5398 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5399 | } |
| 5400 | SKIP_BLANKS; |
| 5401 | } while (RAW == '|'); |
| 5402 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5403 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5404 | return(ret); |
| 5405 | } |
| 5406 | NEXT; |
| 5407 | return(ret); |
| 5408 | } |
| 5409 | |
| 5410 | /** |
| 5411 | * xmlParseEnumeratedType: |
| 5412 | * @ctxt: an XML parser context |
| 5413 | * @tree: the enumeration tree built while parsing |
| 5414 | * |
| 5415 | * parse an Enumerated attribute type. |
| 5416 | * |
| 5417 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5418 | * |
| 5419 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5420 | * |
| 5421 | * |
| 5422 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5423 | */ |
| 5424 | |
| 5425 | int |
| 5426 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5427 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5428 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5429 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5430 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5431 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5432 | return(0); |
| 5433 | } |
| 5434 | SKIP_BLANKS; |
| 5435 | *tree = xmlParseNotationType(ctxt); |
| 5436 | if (*tree == NULL) return(0); |
| 5437 | return(XML_ATTRIBUTE_NOTATION); |
| 5438 | } |
| 5439 | *tree = xmlParseEnumerationType(ctxt); |
| 5440 | if (*tree == NULL) return(0); |
| 5441 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5442 | } |
| 5443 | |
| 5444 | /** |
| 5445 | * xmlParseAttributeType: |
| 5446 | * @ctxt: an XML parser context |
| 5447 | * @tree: the enumeration tree built while parsing |
| 5448 | * |
| 5449 | * parse the Attribute list def for an element |
| 5450 | * |
| 5451 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5452 | * |
| 5453 | * [55] StringType ::= 'CDATA' |
| 5454 | * |
| 5455 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5456 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5457 | * |
| 5458 | * Validity constraints for attribute values syntax are checked in |
| 5459 | * xmlValidateAttributeValue() |
| 5460 | * |
| 5461 | * [ VC: ID ] |
| 5462 | * Values of type ID must match the Name production. A name must not |
| 5463 | * appear more than once in an XML document as a value of this type; |
| 5464 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5465 | * |
| 5466 | * [ VC: One ID per Element Type ] |
| 5467 | * No element type may have more than one ID attribute specified. |
| 5468 | * |
| 5469 | * [ VC: ID Attribute Default ] |
| 5470 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5471 | * |
| 5472 | * [ VC: IDREF ] |
| 5473 | * Values of type IDREF must match the Name production, and values |
| 5474 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5475 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5476 | * values must match the value of some ID attribute. |
| 5477 | * |
| 5478 | * [ VC: Entity Name ] |
| 5479 | * Values of type ENTITY must match the Name production, values |
| 5480 | * of type ENTITIES must match Names; each Entity Name must match the |
| 5481 | * name of an unparsed entity declared in the DTD. |
| 5482 | * |
| 5483 | * [ VC: Name Token ] |
| 5484 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 5485 | * of type NMTOKENS must match Nmtokens. |
| 5486 | * |
| 5487 | * Returns the attribute type |
| 5488 | */ |
| 5489 | int |
| 5490 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5491 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5492 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | SKIP(5); |
| 5494 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5495 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5496 | SKIP(6); |
| 5497 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5498 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5499 | SKIP(5); |
| 5500 | return(XML_ATTRIBUTE_IDREF); |
| 5501 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5502 | SKIP(2); |
| 5503 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5504 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5505 | SKIP(6); |
| 5506 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5507 | } 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] | 5508 | SKIP(8); |
| 5509 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5510 | } 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] | 5511 | SKIP(8); |
| 5512 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5513 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5514 | SKIP(7); |
| 5515 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5516 | } |
| 5517 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5518 | } |
| 5519 | |
| 5520 | /** |
| 5521 | * xmlParseAttributeListDecl: |
| 5522 | * @ctxt: an XML parser context |
| 5523 | * |
| 5524 | * : parse the Attribute list def for an element |
| 5525 | * |
| 5526 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5527 | * |
| 5528 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5529 | * |
| 5530 | */ |
| 5531 | void |
| 5532 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5533 | const xmlChar *elemName; |
| 5534 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5535 | xmlEnumerationPtr tree; |
| 5536 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5537 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5538 | xmlParserInputPtr input = ctxt->input; |
| 5539 | |
| 5540 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5541 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5542 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5543 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5544 | } |
| 5545 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5546 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5547 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5548 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5549 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5550 | return; |
| 5551 | } |
| 5552 | SKIP_BLANKS; |
| 5553 | GROW; |
| 5554 | while (RAW != '>') { |
| 5555 | const xmlChar *check = CUR_PTR; |
| 5556 | int type; |
| 5557 | int def; |
| 5558 | xmlChar *defaultValue = NULL; |
| 5559 | |
| 5560 | GROW; |
| 5561 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5562 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5563 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5564 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5565 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5566 | break; |
| 5567 | } |
| 5568 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5569 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5570 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5571 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | break; |
| 5573 | } |
| 5574 | SKIP_BLANKS; |
| 5575 | |
| 5576 | type = xmlParseAttributeType(ctxt, &tree); |
| 5577 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5578 | break; |
| 5579 | } |
| 5580 | |
| 5581 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5582 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5583 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5584 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5585 | if (tree != NULL) |
| 5586 | xmlFreeEnumeration(tree); |
| 5587 | break; |
| 5588 | } |
| 5589 | SKIP_BLANKS; |
| 5590 | |
| 5591 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 5592 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5593 | if (defaultValue != NULL) |
| 5594 | xmlFree(defaultValue); |
| 5595 | if (tree != NULL) |
| 5596 | xmlFreeEnumeration(tree); |
| 5597 | break; |
| 5598 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 5599 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 5600 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5601 | |
| 5602 | GROW; |
| 5603 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5604 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5605 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5606 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5607 | if (defaultValue != NULL) |
| 5608 | xmlFree(defaultValue); |
| 5609 | if (tree != NULL) |
| 5610 | xmlFreeEnumeration(tree); |
| 5611 | break; |
| 5612 | } |
| 5613 | SKIP_BLANKS; |
| 5614 | } |
| 5615 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5616 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5617 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5618 | if (defaultValue != NULL) |
| 5619 | xmlFree(defaultValue); |
| 5620 | if (tree != NULL) |
| 5621 | xmlFreeEnumeration(tree); |
| 5622 | break; |
| 5623 | } |
| 5624 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5625 | (ctxt->sax->attributeDecl != NULL)) |
| 5626 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 5627 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5628 | else if (tree != NULL) |
| 5629 | xmlFreeEnumeration(tree); |
| 5630 | |
| 5631 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 5632 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 5633 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 5634 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 5635 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 5636 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5637 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 5638 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5639 | if (defaultValue != NULL) |
| 5640 | xmlFree(defaultValue); |
| 5641 | GROW; |
| 5642 | } |
| 5643 | if (RAW == '>') { |
| 5644 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5645 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5646 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 5647 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5648 | } |
| 5649 | NEXT; |
| 5650 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5651 | } |
| 5652 | } |
| 5653 | |
| 5654 | /** |
| 5655 | * xmlParseElementMixedContentDecl: |
| 5656 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5657 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5658 | * |
| 5659 | * parse the declaration for a Mixed Element content |
| 5660 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5661 | * |
| 5662 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 5663 | * '(' S? '#PCDATA' S? ')' |
| 5664 | * |
| 5665 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 5666 | * |
| 5667 | * [ VC: No Duplicate Types ] |
| 5668 | * The same name must not appear more than once in a single |
| 5669 | * mixed-content declaration. |
| 5670 | * |
| 5671 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 5672 | */ |
| 5673 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5674 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5675 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5676 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5677 | |
| 5678 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5679 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5680 | SKIP(7); |
| 5681 | SKIP_BLANKS; |
| 5682 | SHRINK; |
| 5683 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5684 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5685 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5686 | "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] | 5687 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5688 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5689 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5690 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5691 | if (ret == NULL) |
| 5692 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5693 | if (RAW == '*') { |
| 5694 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5695 | NEXT; |
| 5696 | } |
| 5697 | return(ret); |
| 5698 | } |
| 5699 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5700 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5701 | if (ret == NULL) return(NULL); |
| 5702 | } |
| 5703 | while (RAW == '|') { |
| 5704 | NEXT; |
| 5705 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5706 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5707 | if (ret == NULL) return(NULL); |
| 5708 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5709 | if (cur != NULL) |
| 5710 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5711 | cur = ret; |
| 5712 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5713 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5714 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5715 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5716 | if (n->c1 != NULL) |
| 5717 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5718 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5719 | if (n != NULL) |
| 5720 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5721 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5722 | } |
| 5723 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5724 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5725 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5726 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5727 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5728 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5729 | return(NULL); |
| 5730 | } |
| 5731 | SKIP_BLANKS; |
| 5732 | GROW; |
| 5733 | } |
| 5734 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 5735 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5736 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5737 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5738 | if (cur->c2 != NULL) |
| 5739 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5740 | } |
| 5741 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5742 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5743 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5744 | "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] | 5745 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5746 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5747 | SKIP(2); |
| 5748 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5749 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5750 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5751 | return(NULL); |
| 5752 | } |
| 5753 | |
| 5754 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5755 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5756 | } |
| 5757 | return(ret); |
| 5758 | } |
| 5759 | |
| 5760 | /** |
| 5761 | * xmlParseElementChildrenContentDecl: |
| 5762 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5763 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5764 | * |
| 5765 | * parse the declaration for a Mixed Element content |
| 5766 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5767 | * |
| 5768 | * |
| 5769 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 5770 | * |
| 5771 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 5772 | * |
| 5773 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 5774 | * |
| 5775 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 5776 | * |
| 5777 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 5778 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5779 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5780 | * opening or closing parentheses in a choice, seq, or Mixed |
| 5781 | * construct is contained in the replacement text for a parameter |
| 5782 | * entity, both must be contained in the same replacement text. For |
| 5783 | * interoperability, if a parameter-entity reference appears in a |
| 5784 | * choice, seq, or Mixed construct, its replacement text should not |
| 5785 | * be empty, and neither the first nor last non-blank character of |
| 5786 | * the replacement text should be a connector (| or ,). |
| 5787 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5788 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | * hierarchy. |
| 5790 | */ |
| 5791 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5792 | xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5793 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5794 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5795 | xmlChar type = 0; |
| 5796 | |
| 5797 | SKIP_BLANKS; |
| 5798 | GROW; |
| 5799 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5800 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5801 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5802 | /* Recurse on first child */ |
| 5803 | NEXT; |
| 5804 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5805 | cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5806 | SKIP_BLANKS; |
| 5807 | GROW; |
| 5808 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5809 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5810 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5811 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5812 | return(NULL); |
| 5813 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5814 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5815 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5816 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5817 | return(NULL); |
| 5818 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5819 | GROW; |
| 5820 | if (RAW == '?') { |
| 5821 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5822 | NEXT; |
| 5823 | } else if (RAW == '*') { |
| 5824 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5825 | NEXT; |
| 5826 | } else if (RAW == '+') { |
| 5827 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5828 | NEXT; |
| 5829 | } else { |
| 5830 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5831 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5832 | GROW; |
| 5833 | } |
| 5834 | SKIP_BLANKS; |
| 5835 | SHRINK; |
| 5836 | while (RAW != ')') { |
| 5837 | /* |
| 5838 | * Each loop we parse one separator and one element. |
| 5839 | */ |
| 5840 | if (RAW == ',') { |
| 5841 | if (type == 0) type = CUR; |
| 5842 | |
| 5843 | /* |
| 5844 | * Detect "Name | Name , Name" error |
| 5845 | */ |
| 5846 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5847 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5848 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5849 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5850 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5851 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5852 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5853 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5854 | return(NULL); |
| 5855 | } |
| 5856 | NEXT; |
| 5857 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5858 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5859 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5860 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5861 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 5862 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | return(NULL); |
| 5864 | } |
| 5865 | if (last == NULL) { |
| 5866 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5867 | if (ret != NULL) |
| 5868 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5869 | ret = cur = op; |
| 5870 | } else { |
| 5871 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5872 | if (op != NULL) |
| 5873 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5875 | if (last != NULL) |
| 5876 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5877 | cur =op; |
| 5878 | last = NULL; |
| 5879 | } |
| 5880 | } else if (RAW == '|') { |
| 5881 | if (type == 0) type = CUR; |
| 5882 | |
| 5883 | /* |
| 5884 | * Detect "Name , Name | Name" error |
| 5885 | */ |
| 5886 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5887 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5889 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5890 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5891 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5892 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5893 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5894 | return(NULL); |
| 5895 | } |
| 5896 | NEXT; |
| 5897 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5898 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5899 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5900 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5901 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5902 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5903 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5904 | return(NULL); |
| 5905 | } |
| 5906 | if (last == NULL) { |
| 5907 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5908 | if (ret != NULL) |
| 5909 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5910 | ret = cur = op; |
| 5911 | } else { |
| 5912 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5913 | if (op != NULL) |
| 5914 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5915 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5916 | if (last != NULL) |
| 5917 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5918 | cur =op; |
| 5919 | last = NULL; |
| 5920 | } |
| 5921 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5922 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 5923 | if ((last != NULL) && (last != ret)) |
| 5924 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5925 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5926 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5927 | return(NULL); |
| 5928 | } |
| 5929 | GROW; |
| 5930 | SKIP_BLANKS; |
| 5931 | GROW; |
| 5932 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5933 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5934 | /* Recurse on second child */ |
| 5935 | NEXT; |
| 5936 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5937 | last = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5938 | SKIP_BLANKS; |
| 5939 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5940 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5941 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5942 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5943 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5944 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5945 | return(NULL); |
| 5946 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5947 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5948 | if (last == NULL) { |
| 5949 | if (ret != NULL) |
| 5950 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 5951 | return(NULL); |
| 5952 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5953 | if (RAW == '?') { |
| 5954 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5955 | NEXT; |
| 5956 | } else if (RAW == '*') { |
| 5957 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5958 | NEXT; |
| 5959 | } else if (RAW == '+') { |
| 5960 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5961 | NEXT; |
| 5962 | } else { |
| 5963 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5964 | } |
| 5965 | } |
| 5966 | SKIP_BLANKS; |
| 5967 | GROW; |
| 5968 | } |
| 5969 | if ((cur != NULL) && (last != NULL)) { |
| 5970 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5971 | if (last != NULL) |
| 5972 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5974 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5975 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5976 | "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] | 5977 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5978 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5979 | NEXT; |
| 5980 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 5981 | if (ret != NULL) { |
| 5982 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 5983 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 5984 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5985 | else |
| 5986 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5987 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5988 | NEXT; |
| 5989 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5990 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5991 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5992 | cur = ret; |
| 5993 | /* |
| 5994 | * Some normalization: |
| 5995 | * (a | b* | c?)* == (a | b | c)* |
| 5996 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 5997 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5998 | if ((cur->c1 != NULL) && |
| 5999 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6000 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6001 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6002 | if ((cur->c2 != NULL) && |
| 6003 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6004 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 6005 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6006 | cur = cur->c2; |
| 6007 | } |
| 6008 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6009 | NEXT; |
| 6010 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6011 | if (ret != NULL) { |
| 6012 | int found = 0; |
| 6013 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6014 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6015 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6016 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6017 | else |
| 6018 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6019 | /* |
| 6020 | * Some normalization: |
| 6021 | * (a | b*)+ == (a | b)* |
| 6022 | * (a | b?)+ == (a | b)* |
| 6023 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6024 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6025 | if ((cur->c1 != NULL) && |
| 6026 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6027 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6028 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6029 | found = 1; |
| 6030 | } |
| 6031 | if ((cur->c2 != NULL) && |
| 6032 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6033 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6034 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6035 | found = 1; |
| 6036 | } |
| 6037 | cur = cur->c2; |
| 6038 | } |
| 6039 | if (found) |
| 6040 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6041 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6042 | NEXT; |
| 6043 | } |
| 6044 | return(ret); |
| 6045 | } |
| 6046 | |
| 6047 | /** |
| 6048 | * xmlParseElementContentDecl: |
| 6049 | * @ctxt: an XML parser context |
| 6050 | * @name: the name of the element being defined. |
| 6051 | * @result: the Element Content pointer will be stored here if any |
| 6052 | * |
| 6053 | * parse the declaration for an Element content either Mixed or Children, |
| 6054 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 6055 | * |
| 6056 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6057 | * |
| 6058 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6059 | */ |
| 6060 | |
| 6061 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6062 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6063 | xmlElementContentPtr *result) { |
| 6064 | |
| 6065 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6066 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6067 | int res; |
| 6068 | |
| 6069 | *result = NULL; |
| 6070 | |
| 6071 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6072 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6073 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6074 | return(-1); |
| 6075 | } |
| 6076 | NEXT; |
| 6077 | GROW; |
| 6078 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6079 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6080 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6081 | res = XML_ELEMENT_TYPE_MIXED; |
| 6082 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6083 | tree = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6084 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6086 | SKIP_BLANKS; |
| 6087 | *result = tree; |
| 6088 | return(res); |
| 6089 | } |
| 6090 | |
| 6091 | /** |
| 6092 | * xmlParseElementDecl: |
| 6093 | * @ctxt: an XML parser context |
| 6094 | * |
| 6095 | * parse an Element declaration. |
| 6096 | * |
| 6097 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6098 | * |
| 6099 | * [ VC: Unique Element Type Declaration ] |
| 6100 | * No element type may be declared more than once |
| 6101 | * |
| 6102 | * Returns the type of the element, or -1 in case of error |
| 6103 | */ |
| 6104 | int |
| 6105 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6106 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6107 | int ret = -1; |
| 6108 | xmlElementContentPtr content = NULL; |
| 6109 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6110 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6111 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6112 | xmlParserInputPtr input = ctxt->input; |
| 6113 | |
| 6114 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6115 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6116 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6117 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6118 | } |
| 6119 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6120 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6121 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6122 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6123 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6124 | return(-1); |
| 6125 | } |
| 6126 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6127 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6128 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6129 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6130 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6131 | } |
| 6132 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6133 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6134 | SKIP(5); |
| 6135 | /* |
| 6136 | * Element must always be empty. |
| 6137 | */ |
| 6138 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6139 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6140 | (NXT(2) == 'Y')) { |
| 6141 | SKIP(3); |
| 6142 | /* |
| 6143 | * Element is a generic container. |
| 6144 | */ |
| 6145 | ret = XML_ELEMENT_TYPE_ANY; |
| 6146 | } else if (RAW == '(') { |
| 6147 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6148 | } else { |
| 6149 | /* |
| 6150 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6151 | */ |
| 6152 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6153 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6154 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6155 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6156 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6157 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6158 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6159 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6160 | return(-1); |
| 6161 | } |
| 6162 | |
| 6163 | SKIP_BLANKS; |
| 6164 | /* |
| 6165 | * Pop-up of finished entities. |
| 6166 | */ |
| 6167 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6168 | xmlPopInput(ctxt); |
| 6169 | SKIP_BLANKS; |
| 6170 | |
| 6171 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6172 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6173 | if (content != NULL) { |
| 6174 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6175 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6176 | } else { |
| 6177 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6178 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6179 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
| 6182 | NEXT; |
| 6183 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6184 | (ctxt->sax->elementDecl != NULL)) { |
| 6185 | if (content != NULL) |
| 6186 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6187 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6188 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6189 | if ((content != NULL) && (content->parent == NULL)) { |
| 6190 | /* |
| 6191 | * this is a trick: if xmlAddElementDecl is called, |
| 6192 | * instead of copying the full tree it is plugged directly |
| 6193 | * if called from the parser. Avoid duplicating the |
| 6194 | * interfaces or change the API/ABI |
| 6195 | */ |
| 6196 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6197 | } |
| 6198 | } else if (content != NULL) { |
| 6199 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6200 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6201 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6202 | } |
| 6203 | return(ret); |
| 6204 | } |
| 6205 | |
| 6206 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6207 | * xmlParseConditionalSections |
| 6208 | * @ctxt: an XML parser context |
| 6209 | * |
| 6210 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6211 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 6212 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6213 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6214 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6215 | */ |
| 6216 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6217 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6219 | int id = ctxt->input->id; |
| 6220 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6221 | SKIP(3); |
| 6222 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6223 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6224 | SKIP(7); |
| 6225 | SKIP_BLANKS; |
| 6226 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6227 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6229 | if (ctxt->input->id != id) { |
| 6230 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6231 | "All markup of the conditional section is not in the same entity\n", |
| 6232 | NULL, NULL); |
| 6233 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6234 | NEXT; |
| 6235 | } |
| 6236 | if (xmlParserDebugEntities) { |
| 6237 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6238 | xmlGenericError(xmlGenericErrorContext, |
| 6239 | "%s(%d): ", ctxt->input->filename, |
| 6240 | ctxt->input->line); |
| 6241 | xmlGenericError(xmlGenericErrorContext, |
| 6242 | "Entering INCLUDE Conditional Section\n"); |
| 6243 | } |
| 6244 | |
| 6245 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6246 | (NXT(2) != '>'))) { |
| 6247 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6248 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6249 | |
| 6250 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6251 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6252 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6253 | NEXT; |
| 6254 | } else if (RAW == '%') { |
| 6255 | xmlParsePEReference(ctxt); |
| 6256 | } else |
| 6257 | xmlParseMarkupDecl(ctxt); |
| 6258 | |
| 6259 | /* |
| 6260 | * Pop-up of finished entities. |
| 6261 | */ |
| 6262 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6263 | xmlPopInput(ctxt); |
| 6264 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6265 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6266 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6267 | break; |
| 6268 | } |
| 6269 | } |
| 6270 | if (xmlParserDebugEntities) { |
| 6271 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6272 | xmlGenericError(xmlGenericErrorContext, |
| 6273 | "%s(%d): ", ctxt->input->filename, |
| 6274 | ctxt->input->line); |
| 6275 | xmlGenericError(xmlGenericErrorContext, |
| 6276 | "Leaving INCLUDE Conditional Section\n"); |
| 6277 | } |
| 6278 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6279 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6280 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6281 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | int depth = 0; |
| 6283 | |
| 6284 | SKIP(6); |
| 6285 | SKIP_BLANKS; |
| 6286 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6287 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6288 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6289 | if (ctxt->input->id != id) { |
| 6290 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6291 | "All markup of the conditional section is not in the same entity\n", |
| 6292 | NULL, NULL); |
| 6293 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6294 | NEXT; |
| 6295 | } |
| 6296 | if (xmlParserDebugEntities) { |
| 6297 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6298 | xmlGenericError(xmlGenericErrorContext, |
| 6299 | "%s(%d): ", ctxt->input->filename, |
| 6300 | ctxt->input->line); |
| 6301 | xmlGenericError(xmlGenericErrorContext, |
| 6302 | "Entering IGNORE Conditional Section\n"); |
| 6303 | } |
| 6304 | |
| 6305 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6306 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6307 | * But disable SAX event generating DTD building in the meantime |
| 6308 | */ |
| 6309 | state = ctxt->disableSAX; |
| 6310 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6311 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6312 | ctxt->instate = XML_PARSER_IGNORE; |
| 6313 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 6314 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6315 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6316 | depth++; |
| 6317 | SKIP(3); |
| 6318 | continue; |
| 6319 | } |
| 6320 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6321 | if (--depth >= 0) SKIP(3); |
| 6322 | continue; |
| 6323 | } |
| 6324 | NEXT; |
| 6325 | continue; |
| 6326 | } |
| 6327 | |
| 6328 | ctxt->disableSAX = state; |
| 6329 | ctxt->instate = instate; |
| 6330 | |
| 6331 | if (xmlParserDebugEntities) { |
| 6332 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6333 | xmlGenericError(xmlGenericErrorContext, |
| 6334 | "%s(%d): ", ctxt->input->filename, |
| 6335 | ctxt->input->line); |
| 6336 | xmlGenericError(xmlGenericErrorContext, |
| 6337 | "Leaving IGNORE Conditional Section\n"); |
| 6338 | } |
| 6339 | |
| 6340 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6341 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
| 6344 | if (RAW == 0) |
| 6345 | SHRINK; |
| 6346 | |
| 6347 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6348 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6350 | if (ctxt->input->id != id) { |
| 6351 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6352 | "All markup of the conditional section is not in the same entity\n", |
| 6353 | NULL, NULL); |
| 6354 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6355 | SKIP(3); |
| 6356 | } |
| 6357 | } |
| 6358 | |
| 6359 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6360 | * xmlParseMarkupDecl: |
| 6361 | * @ctxt: an XML parser context |
| 6362 | * |
| 6363 | * parse Markup declarations |
| 6364 | * |
| 6365 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6366 | * NotationDecl | PI | Comment |
| 6367 | * |
| 6368 | * [ VC: Proper Declaration/PE Nesting ] |
| 6369 | * Parameter-entity replacement text must be properly nested with |
| 6370 | * markup declarations. That is to say, if either the first character |
| 6371 | * or the last character of a markup declaration (markupdecl above) is |
| 6372 | * contained in the replacement text for a parameter-entity reference, |
| 6373 | * both must be contained in the same replacement text. |
| 6374 | * |
| 6375 | * [ WFC: PEs in Internal Subset ] |
| 6376 | * In the internal DTD subset, parameter-entity references can occur |
| 6377 | * only where markup declarations can occur, not within markup declarations. |
| 6378 | * (This does not apply to references that occur in external parameter |
| 6379 | * entities or to the external subset.) |
| 6380 | */ |
| 6381 | void |
| 6382 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6383 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6384 | if (CUR == '<') { |
| 6385 | if (NXT(1) == '!') { |
| 6386 | switch (NXT(2)) { |
| 6387 | case 'E': |
| 6388 | if (NXT(3) == 'L') |
| 6389 | xmlParseElementDecl(ctxt); |
| 6390 | else if (NXT(3) == 'N') |
| 6391 | xmlParseEntityDecl(ctxt); |
| 6392 | break; |
| 6393 | case 'A': |
| 6394 | xmlParseAttributeListDecl(ctxt); |
| 6395 | break; |
| 6396 | case 'N': |
| 6397 | xmlParseNotationDecl(ctxt); |
| 6398 | break; |
| 6399 | case '-': |
| 6400 | xmlParseComment(ctxt); |
| 6401 | break; |
| 6402 | default: |
| 6403 | /* there is an error but it will be detected later */ |
| 6404 | break; |
| 6405 | } |
| 6406 | } else if (NXT(1) == '?') { |
| 6407 | xmlParsePI(ctxt); |
| 6408 | } |
| 6409 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6410 | /* |
| 6411 | * This is only for internal subset. On external entities, |
| 6412 | * the replacement is done before parsing stage |
| 6413 | */ |
| 6414 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6415 | xmlParsePEReference(ctxt); |
| 6416 | |
| 6417 | /* |
| 6418 | * Conditional sections are allowed from entities included |
| 6419 | * by PE References in the internal subset. |
| 6420 | */ |
| 6421 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6422 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6423 | xmlParseConditionalSections(ctxt); |
| 6424 | } |
| 6425 | } |
| 6426 | |
| 6427 | ctxt->instate = XML_PARSER_DTD; |
| 6428 | } |
| 6429 | |
| 6430 | /** |
| 6431 | * xmlParseTextDecl: |
| 6432 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6433 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6434 | * parse an XML declaration header for external entities |
| 6435 | * |
| 6436 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6437 | */ |
| 6438 | |
| 6439 | void |
| 6440 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6441 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6442 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6443 | |
| 6444 | /* |
| 6445 | * We know that '<?xml' is here. |
| 6446 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6447 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6448 | SKIP(5); |
| 6449 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6450 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6451 | return; |
| 6452 | } |
| 6453 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6454 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6455 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6456 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6457 | } |
| 6458 | SKIP_BLANKS; |
| 6459 | |
| 6460 | /* |
| 6461 | * We may have the VersionInfo here. |
| 6462 | */ |
| 6463 | version = xmlParseVersionInfo(ctxt); |
| 6464 | if (version == NULL) |
| 6465 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6466 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6467 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6468 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6469 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6470 | } |
| 6471 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6472 | ctxt->input->version = version; |
| 6473 | |
| 6474 | /* |
| 6475 | * We must have the encoding declaration |
| 6476 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6477 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6478 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6479 | /* |
| 6480 | * The XML REC instructs us to stop parsing right here |
| 6481 | */ |
| 6482 | return; |
| 6483 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6484 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 6485 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 6486 | "Missing encoding in text declaration\n"); |
| 6487 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6488 | |
| 6489 | SKIP_BLANKS; |
| 6490 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 6491 | SKIP(2); |
| 6492 | } else if (RAW == '>') { |
| 6493 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6494 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6495 | NEXT; |
| 6496 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6497 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6498 | MOVETO_ENDTAG(CUR_PTR); |
| 6499 | NEXT; |
| 6500 | } |
| 6501 | } |
| 6502 | |
| 6503 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6504 | * xmlParseExternalSubset: |
| 6505 | * @ctxt: an XML parser context |
| 6506 | * @ExternalID: the external identifier |
| 6507 | * @SystemID: the system identifier (or URL) |
| 6508 | * |
| 6509 | * parse Markup declarations from an external subset |
| 6510 | * |
| 6511 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 6512 | * |
| 6513 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 6514 | */ |
| 6515 | void |
| 6516 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 6517 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 6518 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6519 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6520 | |
| 6521 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 6522 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 6523 | xmlChar start[4]; |
| 6524 | xmlCharEncoding enc; |
| 6525 | |
| 6526 | start[0] = RAW; |
| 6527 | start[1] = NXT(1); |
| 6528 | start[2] = NXT(2); |
| 6529 | start[3] = NXT(3); |
| 6530 | enc = xmlDetectCharEncoding(start, 4); |
| 6531 | if (enc != XML_CHAR_ENCODING_NONE) |
| 6532 | xmlSwitchEncoding(ctxt, enc); |
| 6533 | } |
| 6534 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6535 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6536 | xmlParseTextDecl(ctxt); |
| 6537 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6538 | /* |
| 6539 | * The XML REC instructs us to stop parsing right here |
| 6540 | */ |
| 6541 | ctxt->instate = XML_PARSER_EOF; |
| 6542 | return; |
| 6543 | } |
| 6544 | } |
| 6545 | if (ctxt->myDoc == NULL) { |
| 6546 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6547 | if (ctxt->myDoc == NULL) { |
| 6548 | xmlErrMemory(ctxt, "New Doc failed"); |
| 6549 | return; |
| 6550 | } |
| 6551 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6552 | } |
| 6553 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 6554 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 6555 | |
| 6556 | ctxt->instate = XML_PARSER_DTD; |
| 6557 | ctxt->external = 1; |
| 6558 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 6559 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6560 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6561 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6562 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6563 | |
| 6564 | GROW; |
| 6565 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6566 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6567 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6568 | NEXT; |
| 6569 | } else if (RAW == '%') { |
| 6570 | xmlParsePEReference(ctxt); |
| 6571 | } else |
| 6572 | xmlParseMarkupDecl(ctxt); |
| 6573 | |
| 6574 | /* |
| 6575 | * Pop-up of finished entities. |
| 6576 | */ |
| 6577 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6578 | xmlPopInput(ctxt); |
| 6579 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6580 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6581 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6582 | break; |
| 6583 | } |
| 6584 | } |
| 6585 | |
| 6586 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6587 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | } |
| 6591 | |
| 6592 | /** |
| 6593 | * xmlParseReference: |
| 6594 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6595 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6596 | * parse and handle entity references in content, depending on the SAX |
| 6597 | * interface, this may end-up in a call to character() if this is a |
| 6598 | * CharRef, a predefined entity, if there is no reference() callback. |
| 6599 | * or if the parser was asked to switch to that mode. |
| 6600 | * |
| 6601 | * [67] Reference ::= EntityRef | CharRef |
| 6602 | */ |
| 6603 | void |
| 6604 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 6605 | xmlEntityPtr ent; |
| 6606 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6607 | int was_checked; |
| 6608 | xmlNodePtr list = NULL; |
| 6609 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6610 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6611 | |
| 6612 | if (RAW != '&') |
| 6613 | return; |
| 6614 | |
| 6615 | /* |
| 6616 | * Simple case of a CharRef |
| 6617 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | if (NXT(1) == '#') { |
| 6619 | int i = 0; |
| 6620 | xmlChar out[10]; |
| 6621 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6622 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6623 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 6624 | if (value == 0) |
| 6625 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6626 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 6627 | /* |
| 6628 | * So we are using non-UTF-8 buffers |
| 6629 | * Check that the char fit on 8bits, if not |
| 6630 | * generate a CharRef. |
| 6631 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6632 | if (value <= 0xFF) { |
| 6633 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6634 | out[1] = 0; |
| 6635 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6636 | (!ctxt->disableSAX)) |
| 6637 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 6638 | } else { |
| 6639 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6640 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6641 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6642 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6643 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6644 | (!ctxt->disableSAX)) |
| 6645 | ctxt->sax->reference(ctxt->userData, out); |
| 6646 | } |
| 6647 | } else { |
| 6648 | /* |
| 6649 | * Just encode the value in UTF-8 |
| 6650 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6651 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6652 | out[i] = 0; |
| 6653 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6654 | (!ctxt->disableSAX)) |
| 6655 | ctxt->sax->characters(ctxt->userData, out, i); |
| 6656 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6657 | return; |
| 6658 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6659 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6660 | /* |
| 6661 | * We are seeing an entity reference |
| 6662 | */ |
| 6663 | ent = xmlParseEntityRef(ctxt); |
| 6664 | if (ent == NULL) return; |
| 6665 | if (!ctxt->wellFormed) |
| 6666 | return; |
| 6667 | was_checked = ent->checked; |
| 6668 | |
| 6669 | /* special case of predefined entities */ |
| 6670 | if ((ent->name == NULL) || |
| 6671 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 6672 | val = ent->content; |
| 6673 | if (val == NULL) return; |
| 6674 | /* |
| 6675 | * inline the entity. |
| 6676 | */ |
| 6677 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6678 | (!ctxt->disableSAX)) |
| 6679 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 6680 | return; |
| 6681 | } |
| 6682 | |
| 6683 | /* |
| 6684 | * The first reference to the entity trigger a parsing phase |
| 6685 | * where the ent->children is filled with the result from |
| 6686 | * the parsing. |
| 6687 | */ |
| 6688 | if (ent->checked == 0) { |
| 6689 | unsigned long oldnbent = ctxt->nbentities; |
| 6690 | |
| 6691 | /* |
| 6692 | * This is a bit hackish but this seems the best |
| 6693 | * way to make sure both SAX and DOM entity support |
| 6694 | * behaves okay. |
| 6695 | */ |
| 6696 | void *user_data; |
| 6697 | if (ctxt->userData == ctxt) |
| 6698 | user_data = NULL; |
| 6699 | else |
| 6700 | user_data = ctxt->userData; |
| 6701 | |
| 6702 | /* |
| 6703 | * Check that this entity is well formed |
| 6704 | * 4.3.2: An internal general parsed entity is well-formed |
| 6705 | * if its replacement text matches the production labeled |
| 6706 | * content. |
| 6707 | */ |
| 6708 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 6709 | ctxt->depth++; |
| 6710 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 6711 | user_data, &list); |
| 6712 | ctxt->depth--; |
| 6713 | |
| 6714 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 6715 | ctxt->depth++; |
| 6716 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 6717 | user_data, ctxt->depth, ent->URI, |
| 6718 | ent->ExternalID, &list); |
| 6719 | ctxt->depth--; |
| 6720 | } else { |
| 6721 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 6722 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6723 | "invalid entity type found\n", NULL); |
| 6724 | } |
| 6725 | |
| 6726 | /* |
| 6727 | * Store the number of entities needing parsing for this entity |
| 6728 | * content and do checkings |
| 6729 | */ |
| 6730 | ent->checked = ctxt->nbentities - oldnbent; |
| 6731 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6732 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6733 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6734 | return; |
| 6735 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6736 | if (xmlParserEntityCheck(ctxt, 0, ent)) { |
| 6737 | xmlFreeNodeList(list); |
| 6738 | return; |
| 6739 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6740 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6741 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 6742 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 6743 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 6744 | (ent->children == NULL)) { |
| 6745 | ent->children = list; |
| 6746 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6747 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6748 | * Prune it directly in the generated document |
| 6749 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6750 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6751 | if (((list->type == XML_TEXT_NODE) && |
| 6752 | (list->next == NULL)) || |
| 6753 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 6754 | list->parent = (xmlNodePtr) ent; |
| 6755 | list = NULL; |
| 6756 | ent->owner = 1; |
| 6757 | } else { |
| 6758 | ent->owner = 0; |
| 6759 | while (list != NULL) { |
| 6760 | list->parent = (xmlNodePtr) ctxt->node; |
| 6761 | list->doc = ctxt->myDoc; |
| 6762 | if (list->next == NULL) |
| 6763 | ent->last = list; |
| 6764 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6765 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6766 | list = ent->children; |
| 6767 | #ifdef LIBXML_LEGACY_ENABLED |
| 6768 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6769 | xmlAddEntityReference(ent, list, NULL); |
| 6770 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6771 | } |
| 6772 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6773 | ent->owner = 1; |
| 6774 | while (list != NULL) { |
| 6775 | list->parent = (xmlNodePtr) ent; |
| 6776 | if (list->next == NULL) |
| 6777 | ent->last = list; |
| 6778 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6779 | } |
| 6780 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6781 | } else { |
| 6782 | xmlFreeNodeList(list); |
| 6783 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6784 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6785 | } else if ((ret != XML_ERR_OK) && |
| 6786 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 6787 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 6788 | "Entity '%s' failed to parse\n", ent->name); |
| 6789 | } else if (list != NULL) { |
| 6790 | xmlFreeNodeList(list); |
| 6791 | list = NULL; |
| 6792 | } |
| 6793 | if (ent->checked == 0) |
| 6794 | ent->checked = 1; |
| 6795 | } else if (ent->checked != 1) { |
| 6796 | ctxt->nbentities += ent->checked; |
| 6797 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6798 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6799 | /* |
| 6800 | * Now that the entity content has been gathered |
| 6801 | * provide it to the application, this can take different forms based |
| 6802 | * on the parsing modes. |
| 6803 | */ |
| 6804 | if (ent->children == NULL) { |
| 6805 | /* |
| 6806 | * Probably running in SAX mode and the callbacks don't |
| 6807 | * build the entity content. So unless we already went |
| 6808 | * though parsing for first checking go though the entity |
| 6809 | * content to generate callbacks associated to the entity |
| 6810 | */ |
| 6811 | if (was_checked != 0) { |
| 6812 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6813 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6814 | * This is a bit hackish but this seems the best |
| 6815 | * way to make sure both SAX and DOM entity support |
| 6816 | * behaves okay. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6817 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6818 | if (ctxt->userData == ctxt) |
| 6819 | user_data = NULL; |
| 6820 | else |
| 6821 | user_data = ctxt->userData; |
| 6822 | |
| 6823 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 6824 | ctxt->depth++; |
| 6825 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 6826 | ent->content, user_data, NULL); |
| 6827 | ctxt->depth--; |
| 6828 | } else if (ent->etype == |
| 6829 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 6830 | ctxt->depth++; |
| 6831 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 6832 | ctxt->sax, user_data, ctxt->depth, |
| 6833 | ent->URI, ent->ExternalID, NULL); |
| 6834 | ctxt->depth--; |
| 6835 | } else { |
| 6836 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 6837 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6838 | "invalid entity type found\n", NULL); |
| 6839 | } |
| 6840 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 6841 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 6842 | return; |
| 6843 | } |
| 6844 | } |
| 6845 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6846 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 6847 | /* |
| 6848 | * Entity reference callback comes second, it's somewhat |
| 6849 | * superfluous but a compatibility to historical behaviour |
| 6850 | */ |
| 6851 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 6852 | } |
| 6853 | return; |
| 6854 | } |
| 6855 | |
| 6856 | /* |
| 6857 | * If we didn't get any children for the entity being built |
| 6858 | */ |
| 6859 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6860 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 6861 | /* |
| 6862 | * Create a node. |
| 6863 | */ |
| 6864 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 6865 | return; |
| 6866 | } |
| 6867 | |
| 6868 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 6869 | /* |
| 6870 | * There is a problem on the handling of _private for entities |
| 6871 | * (bug 155816): Should we copy the content of the field from |
| 6872 | * the entity (possibly overwriting some value set by the user |
| 6873 | * when a copy is created), should we leave it alone, or should |
| 6874 | * we try to take care of different situations? The problem |
| 6875 | * is exacerbated by the usage of this field by the xmlReader. |
| 6876 | * To fix this bug, we look at _private on the created node |
| 6877 | * and, if it's NULL, we copy in whatever was in the entity. |
| 6878 | * If it's not NULL we leave it alone. This is somewhat of a |
| 6879 | * hack - maybe we should have further tests to determine |
| 6880 | * what to do. |
| 6881 | */ |
| 6882 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 6883 | /* |
| 6884 | * Seems we are generating the DOM content, do |
| 6885 | * a simple tree copy for all references except the first |
| 6886 | * In the first occurrence list contains the replacement. |
| 6887 | * progressive == 2 means we are operating on the Reader |
| 6888 | * and since nodes are discarded we must copy all the time. |
| 6889 | */ |
| 6890 | if (((list == NULL) && (ent->owner == 0)) || |
| 6891 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 6892 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 6893 | |
| 6894 | /* |
| 6895 | * when operating on a reader, the entities definitions |
| 6896 | * are always owning the entities subtree. |
| 6897 | if (ctxt->parseMode == XML_PARSE_READER) |
| 6898 | ent->owner = 1; |
| 6899 | */ |
| 6900 | |
| 6901 | cur = ent->children; |
| 6902 | while (cur != NULL) { |
| 6903 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 6904 | if (nw != NULL) { |
| 6905 | if (nw->_private == NULL) |
| 6906 | nw->_private = cur->_private; |
| 6907 | if (firstChild == NULL){ |
| 6908 | firstChild = nw; |
| 6909 | } |
| 6910 | nw = xmlAddChild(ctxt->node, nw); |
| 6911 | } |
| 6912 | if (cur == ent->last) { |
| 6913 | /* |
| 6914 | * needed to detect some strange empty |
| 6915 | * node cases in the reader tests |
| 6916 | */ |
| 6917 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 6918 | (nw != NULL) && |
| 6919 | (nw->type == XML_ELEMENT_NODE) && |
| 6920 | (nw->children == NULL)) |
| 6921 | nw->extra = 1; |
| 6922 | |
| 6923 | break; |
| 6924 | } |
| 6925 | cur = cur->next; |
| 6926 | } |
| 6927 | #ifdef LIBXML_LEGACY_ENABLED |
| 6928 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6929 | xmlAddEntityReference(ent, firstChild, nw); |
| 6930 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 6931 | } else if (list == NULL) { |
| 6932 | xmlNodePtr nw = NULL, cur, next, last, |
| 6933 | firstChild = NULL; |
| 6934 | /* |
| 6935 | * Copy the entity child list and make it the new |
| 6936 | * entity child list. The goal is to make sure any |
| 6937 | * ID or REF referenced will be the one from the |
| 6938 | * document content and not the entity copy. |
| 6939 | */ |
| 6940 | cur = ent->children; |
| 6941 | ent->children = NULL; |
| 6942 | last = ent->last; |
| 6943 | ent->last = NULL; |
| 6944 | while (cur != NULL) { |
| 6945 | next = cur->next; |
| 6946 | cur->next = NULL; |
| 6947 | cur->parent = NULL; |
| 6948 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 6949 | if (nw != NULL) { |
| 6950 | if (nw->_private == NULL) |
| 6951 | nw->_private = cur->_private; |
| 6952 | if (firstChild == NULL){ |
| 6953 | firstChild = cur; |
| 6954 | } |
| 6955 | xmlAddChild((xmlNodePtr) ent, nw); |
| 6956 | xmlAddChild(ctxt->node, cur); |
| 6957 | } |
| 6958 | if (cur == last) |
| 6959 | break; |
| 6960 | cur = next; |
| 6961 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 6962 | if (ent->owner == 0) |
| 6963 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6964 | #ifdef LIBXML_LEGACY_ENABLED |
| 6965 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6966 | xmlAddEntityReference(ent, firstChild, nw); |
| 6967 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 6968 | } else { |
| 6969 | const xmlChar *nbktext; |
| 6970 | |
| 6971 | /* |
| 6972 | * the name change is to avoid coalescing of the |
| 6973 | * node with a possible previous text one which |
| 6974 | * would make ent->children a dangling pointer |
| 6975 | */ |
| 6976 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 6977 | -1); |
| 6978 | if (ent->children->type == XML_TEXT_NODE) |
| 6979 | ent->children->name = nbktext; |
| 6980 | if ((ent->last != ent->children) && |
| 6981 | (ent->last->type == XML_TEXT_NODE)) |
| 6982 | ent->last->name = nbktext; |
| 6983 | xmlAddChildList(ctxt->node, ent->children); |
| 6984 | } |
| 6985 | |
| 6986 | /* |
| 6987 | * This is to avoid a nasty side effect, see |
| 6988 | * characters() in SAX.c |
| 6989 | */ |
| 6990 | ctxt->nodemem = 0; |
| 6991 | ctxt->nodelen = 0; |
| 6992 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6993 | } |
| 6994 | } |
| 6995 | } |
| 6996 | |
| 6997 | /** |
| 6998 | * xmlParseEntityRef: |
| 6999 | * @ctxt: an XML parser context |
| 7000 | * |
| 7001 | * parse ENTITY references declarations |
| 7002 | * |
| 7003 | * [68] EntityRef ::= '&' Name ';' |
| 7004 | * |
| 7005 | * [ WFC: Entity Declared ] |
| 7006 | * In a document without any DTD, a document with only an internal DTD |
| 7007 | * subset which contains no parameter entity references, or a document |
| 7008 | * with "standalone='yes'", the Name given in the entity reference |
| 7009 | * must match that in an entity declaration, except that well-formed |
| 7010 | * documents need not declare any of the following entities: amp, lt, |
| 7011 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7012 | * any reference to it. Similarly, the declaration of a general entity |
| 7013 | * must precede any reference to it which appears in a default value in an |
| 7014 | * attribute-list declaration. Note that if entities are declared in the |
| 7015 | * external subset or in external parameter entities, a non-validating |
| 7016 | * processor is not obligated to read and process their declarations; |
| 7017 | * for such documents, the rule that an entity must be declared is a |
| 7018 | * well-formedness constraint only if standalone='yes'. |
| 7019 | * |
| 7020 | * [ WFC: Parsed Entity ] |
| 7021 | * An entity reference must not contain the name of an unparsed entity |
| 7022 | * |
| 7023 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7024 | */ |
| 7025 | xmlEntityPtr |
| 7026 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7027 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7028 | xmlEntityPtr ent = NULL; |
| 7029 | |
| 7030 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7031 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7032 | if (RAW != '&') |
| 7033 | return(NULL); |
| 7034 | NEXT; |
| 7035 | name = xmlParseName(ctxt); |
| 7036 | if (name == NULL) { |
| 7037 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7038 | "xmlParseEntityRef: no name\n"); |
| 7039 | return(NULL); |
| 7040 | } |
| 7041 | if (RAW != ';') { |
| 7042 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7043 | return(NULL); |
| 7044 | } |
| 7045 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7046 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7047 | /* |
| 7048 | * Predefined entites override any extra definition |
| 7049 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7050 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7051 | ent = xmlGetPredefinedEntity(name); |
| 7052 | if (ent != NULL) |
| 7053 | return(ent); |
| 7054 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7055 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7056 | /* |
| 7057 | * Increate the number of entity references parsed |
| 7058 | */ |
| 7059 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7060 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7061 | /* |
| 7062 | * Ask first SAX for entity resolution, otherwise try the |
| 7063 | * entities which may have stored in the parser context. |
| 7064 | */ |
| 7065 | if (ctxt->sax != NULL) { |
| 7066 | if (ctxt->sax->getEntity != NULL) |
| 7067 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7068 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7069 | (ctxt->options & XML_PARSE_OLDSAX)) |
| 7070 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7071 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7072 | (ctxt->userData==ctxt)) { |
| 7073 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7074 | } |
| 7075 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7076 | /* |
| 7077 | * [ WFC: Entity Declared ] |
| 7078 | * In a document without any DTD, a document with only an |
| 7079 | * internal DTD subset which contains no parameter entity |
| 7080 | * references, or a document with "standalone='yes'", the |
| 7081 | * Name given in the entity reference must match that in an |
| 7082 | * entity declaration, except that well-formed documents |
| 7083 | * need not declare any of the following entities: amp, lt, |
| 7084 | * gt, apos, quot. |
| 7085 | * The declaration of a parameter entity must precede any |
| 7086 | * reference to it. |
| 7087 | * Similarly, the declaration of a general entity must |
| 7088 | * precede any reference to it which appears in a default |
| 7089 | * value in an attribute-list declaration. Note that if |
| 7090 | * entities are declared in the external subset or in |
| 7091 | * external parameter entities, a non-validating processor |
| 7092 | * is not obligated to read and process their declarations; |
| 7093 | * for such documents, the rule that an entity must be |
| 7094 | * declared is a well-formedness constraint only if |
| 7095 | * standalone='yes'. |
| 7096 | */ |
| 7097 | if (ent == NULL) { |
| 7098 | if ((ctxt->standalone == 1) || |
| 7099 | ((ctxt->hasExternalSubset == 0) && |
| 7100 | (ctxt->hasPErefs == 0))) { |
| 7101 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7102 | "Entity '%s' not defined\n", name); |
| 7103 | } else { |
| 7104 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7105 | "Entity '%s' not defined\n", name); |
| 7106 | if ((ctxt->inSubset == 0) && |
| 7107 | (ctxt->sax != NULL) && |
| 7108 | (ctxt->sax->reference != NULL)) { |
| 7109 | ctxt->sax->reference(ctxt->userData, name); |
| 7110 | } |
| 7111 | } |
| 7112 | ctxt->valid = 0; |
| 7113 | } |
| 7114 | |
| 7115 | /* |
| 7116 | * [ WFC: Parsed Entity ] |
| 7117 | * An entity reference must not contain the name of an |
| 7118 | * unparsed entity |
| 7119 | */ |
| 7120 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7121 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7122 | "Entity reference to unparsed entity %s\n", name); |
| 7123 | } |
| 7124 | |
| 7125 | /* |
| 7126 | * [ WFC: No External Entity References ] |
| 7127 | * Attribute values cannot contain direct or indirect |
| 7128 | * entity references to external entities. |
| 7129 | */ |
| 7130 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7131 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7132 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7133 | "Attribute references external entity '%s'\n", name); |
| 7134 | } |
| 7135 | /* |
| 7136 | * [ WFC: No < in Attribute Values ] |
| 7137 | * The replacement text of any entity referred to directly or |
| 7138 | * indirectly in an attribute value (other than "<") must |
| 7139 | * not contain a <. |
| 7140 | */ |
| 7141 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7142 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7143 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7144 | (xmlStrchr(ent->content, '<'))) { |
| 7145 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7146 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7147 | } |
| 7148 | |
| 7149 | /* |
| 7150 | * Internal check, no parameter entities here ... |
| 7151 | */ |
| 7152 | else { |
| 7153 | switch (ent->etype) { |
| 7154 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7155 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7156 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7157 | "Attempt to reference the parameter entity '%s'\n", |
| 7158 | name); |
| 7159 | break; |
| 7160 | default: |
| 7161 | break; |
| 7162 | } |
| 7163 | } |
| 7164 | |
| 7165 | /* |
| 7166 | * [ WFC: No Recursion ] |
| 7167 | * A parsed entity must not contain a recursive reference |
| 7168 | * to itself, either directly or indirectly. |
| 7169 | * Done somewhere else |
| 7170 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7171 | return(ent); |
| 7172 | } |
| 7173 | |
| 7174 | /** |
| 7175 | * xmlParseStringEntityRef: |
| 7176 | * @ctxt: an XML parser context |
| 7177 | * @str: a pointer to an index in the string |
| 7178 | * |
| 7179 | * parse ENTITY references declarations, but this version parses it from |
| 7180 | * a string value. |
| 7181 | * |
| 7182 | * [68] EntityRef ::= '&' Name ';' |
| 7183 | * |
| 7184 | * [ WFC: Entity Declared ] |
| 7185 | * In a document without any DTD, a document with only an internal DTD |
| 7186 | * subset which contains no parameter entity references, or a document |
| 7187 | * with "standalone='yes'", the Name given in the entity reference |
| 7188 | * must match that in an entity declaration, except that well-formed |
| 7189 | * documents need not declare any of the following entities: amp, lt, |
| 7190 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7191 | * any reference to it. Similarly, the declaration of a general entity |
| 7192 | * must precede any reference to it which appears in a default value in an |
| 7193 | * attribute-list declaration. Note that if entities are declared in the |
| 7194 | * external subset or in external parameter entities, a non-validating |
| 7195 | * processor is not obligated to read and process their declarations; |
| 7196 | * for such documents, the rule that an entity must be declared is a |
| 7197 | * well-formedness constraint only if standalone='yes'. |
| 7198 | * |
| 7199 | * [ WFC: Parsed Entity ] |
| 7200 | * An entity reference must not contain the name of an unparsed entity |
| 7201 | * |
| 7202 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7203 | * is updated to the current location in the string. |
| 7204 | */ |
| 7205 | xmlEntityPtr |
| 7206 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7207 | xmlChar *name; |
| 7208 | const xmlChar *ptr; |
| 7209 | xmlChar cur; |
| 7210 | xmlEntityPtr ent = NULL; |
| 7211 | |
| 7212 | if ((str == NULL) || (*str == NULL)) |
| 7213 | return(NULL); |
| 7214 | ptr = *str; |
| 7215 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7216 | if (cur != '&') |
| 7217 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7218 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7219 | ptr++; |
| 7220 | cur = *ptr; |
| 7221 | name = xmlParseStringName(ctxt, &ptr); |
| 7222 | if (name == NULL) { |
| 7223 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7224 | "xmlParseStringEntityRef: no name\n"); |
| 7225 | *str = ptr; |
| 7226 | return(NULL); |
| 7227 | } |
| 7228 | if (*ptr != ';') { |
| 7229 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Daniel Veillard | 7f4547c | 2008-10-03 07:58:23 +0000 | [diff] [blame] | 7230 | xmlFree(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7231 | *str = ptr; |
| 7232 | return(NULL); |
| 7233 | } |
| 7234 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7235 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7236 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7237 | /* |
| 7238 | * Predefined entites override any extra definition |
| 7239 | */ |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7240 | if ((ctxt->options & XML_PARSE_OLDSAX) == 0) { |
| 7241 | ent = xmlGetPredefinedEntity(name); |
| 7242 | if (ent != NULL) { |
| 7243 | xmlFree(name); |
| 7244 | *str = ptr; |
| 7245 | return(ent); |
| 7246 | } |
Daniel Veillard | 34a7fc3 | 2008-10-02 20:55:10 +0000 | [diff] [blame] | 7247 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7248 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7249 | /* |
| 7250 | * Increate the number of entity references parsed |
| 7251 | */ |
| 7252 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7253 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7254 | /* |
| 7255 | * Ask first SAX for entity resolution, otherwise try the |
| 7256 | * entities which may have stored in the parser context. |
| 7257 | */ |
| 7258 | if (ctxt->sax != NULL) { |
| 7259 | if (ctxt->sax->getEntity != NULL) |
| 7260 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7261 | if ((ent == NULL) && (ctxt->options & XML_PARSE_OLDSAX)) |
| 7262 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7263 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7264 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7265 | } |
| 7266 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7267 | |
| 7268 | /* |
| 7269 | * [ WFC: Entity Declared ] |
| 7270 | * In a document without any DTD, a document with only an |
| 7271 | * internal DTD subset which contains no parameter entity |
| 7272 | * references, or a document with "standalone='yes'", the |
| 7273 | * Name given in the entity reference must match that in an |
| 7274 | * entity declaration, except that well-formed documents |
| 7275 | * need not declare any of the following entities: amp, lt, |
| 7276 | * gt, apos, quot. |
| 7277 | * The declaration of a parameter entity must precede any |
| 7278 | * reference to it. |
| 7279 | * Similarly, the declaration of a general entity must |
| 7280 | * precede any reference to it which appears in a default |
| 7281 | * value in an attribute-list declaration. Note that if |
| 7282 | * entities are declared in the external subset or in |
| 7283 | * external parameter entities, a non-validating processor |
| 7284 | * is not obligated to read and process their declarations; |
| 7285 | * for such documents, the rule that an entity must be |
| 7286 | * declared is a well-formedness constraint only if |
| 7287 | * standalone='yes'. |
| 7288 | */ |
| 7289 | if (ent == NULL) { |
| 7290 | if ((ctxt->standalone == 1) || |
| 7291 | ((ctxt->hasExternalSubset == 0) && |
| 7292 | (ctxt->hasPErefs == 0))) { |
| 7293 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7294 | "Entity '%s' not defined\n", name); |
| 7295 | } else { |
| 7296 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7297 | "Entity '%s' not defined\n", |
| 7298 | name); |
| 7299 | } |
| 7300 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7301 | } |
| 7302 | |
| 7303 | /* |
| 7304 | * [ WFC: Parsed Entity ] |
| 7305 | * An entity reference must not contain the name of an |
| 7306 | * unparsed entity |
| 7307 | */ |
| 7308 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7309 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7310 | "Entity reference to unparsed entity %s\n", name); |
| 7311 | } |
| 7312 | |
| 7313 | /* |
| 7314 | * [ WFC: No External Entity References ] |
| 7315 | * Attribute values cannot contain direct or indirect |
| 7316 | * entity references to external entities. |
| 7317 | */ |
| 7318 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7319 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7320 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7321 | "Attribute references external entity '%s'\n", name); |
| 7322 | } |
| 7323 | /* |
| 7324 | * [ WFC: No < in Attribute Values ] |
| 7325 | * The replacement text of any entity referred to directly or |
| 7326 | * indirectly in an attribute value (other than "<") must |
| 7327 | * not contain a <. |
| 7328 | */ |
| 7329 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7330 | (ent != NULL) && (ent->content != NULL) && |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 7331 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7332 | (xmlStrchr(ent->content, '<'))) { |
| 7333 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7334 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7335 | name); |
| 7336 | } |
| 7337 | |
| 7338 | /* |
| 7339 | * Internal check, no parameter entities here ... |
| 7340 | */ |
| 7341 | else { |
| 7342 | switch (ent->etype) { |
| 7343 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7344 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7345 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7346 | "Attempt to reference the parameter entity '%s'\n", |
| 7347 | name); |
| 7348 | break; |
| 7349 | default: |
| 7350 | break; |
| 7351 | } |
| 7352 | } |
| 7353 | |
| 7354 | /* |
| 7355 | * [ WFC: No Recursion ] |
| 7356 | * A parsed entity must not contain a recursive reference |
| 7357 | * to itself, either directly or indirectly. |
| 7358 | * Done somewhere else |
| 7359 | */ |
| 7360 | |
| 7361 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7362 | *str = ptr; |
| 7363 | return(ent); |
| 7364 | } |
| 7365 | |
| 7366 | /** |
| 7367 | * xmlParsePEReference: |
| 7368 | * @ctxt: an XML parser context |
| 7369 | * |
| 7370 | * parse PEReference declarations |
| 7371 | * The entity content is handled directly by pushing it's content as |
| 7372 | * a new input stream. |
| 7373 | * |
| 7374 | * [69] PEReference ::= '%' Name ';' |
| 7375 | * |
| 7376 | * [ WFC: No Recursion ] |
| 7377 | * A parsed entity must not contain a recursive |
| 7378 | * reference to itself, either directly or indirectly. |
| 7379 | * |
| 7380 | * [ WFC: Entity Declared ] |
| 7381 | * In a document without any DTD, a document with only an internal DTD |
| 7382 | * subset which contains no parameter entity references, or a document |
| 7383 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7384 | * entity must precede any reference to it... |
| 7385 | * |
| 7386 | * [ VC: Entity Declared ] |
| 7387 | * In a document with an external subset or external parameter entities |
| 7388 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7389 | * must precede any reference to it... |
| 7390 | * |
| 7391 | * [ WFC: In DTD ] |
| 7392 | * Parameter-entity references may only appear in the DTD. |
| 7393 | * NOTE: misleading but this is handled. |
| 7394 | */ |
| 7395 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7396 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7397 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7398 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7399 | xmlEntityPtr entity = NULL; |
| 7400 | xmlParserInputPtr input; |
| 7401 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7402 | if (RAW != '%') |
| 7403 | return; |
| 7404 | NEXT; |
| 7405 | name = xmlParseName(ctxt); |
| 7406 | if (name == NULL) { |
| 7407 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7408 | "xmlParsePEReference: no name\n"); |
| 7409 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7410 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7411 | if (RAW != ';') { |
| 7412 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7413 | return; |
| 7414 | } |
| 7415 | |
| 7416 | NEXT; |
| 7417 | |
| 7418 | /* |
| 7419 | * Increate the number of entity references parsed |
| 7420 | */ |
| 7421 | ctxt->nbentities++; |
| 7422 | |
| 7423 | /* |
| 7424 | * Request the entity from SAX |
| 7425 | */ |
| 7426 | if ((ctxt->sax != NULL) && |
| 7427 | (ctxt->sax->getParameterEntity != NULL)) |
| 7428 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7429 | name); |
| 7430 | if (entity == NULL) { |
| 7431 | /* |
| 7432 | * [ WFC: Entity Declared ] |
| 7433 | * In a document without any DTD, a document with only an |
| 7434 | * internal DTD subset which contains no parameter entity |
| 7435 | * references, or a document with "standalone='yes'", ... |
| 7436 | * ... The declaration of a parameter entity must precede |
| 7437 | * any reference to it... |
| 7438 | */ |
| 7439 | if ((ctxt->standalone == 1) || |
| 7440 | ((ctxt->hasExternalSubset == 0) && |
| 7441 | (ctxt->hasPErefs == 0))) { |
| 7442 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7443 | "PEReference: %%%s; not found\n", |
| 7444 | name); |
| 7445 | } else { |
| 7446 | /* |
| 7447 | * [ VC: Entity Declared ] |
| 7448 | * In a document with an external subset or external |
| 7449 | * parameter entities with "standalone='no'", ... |
| 7450 | * ... The declaration of a parameter entity must |
| 7451 | * precede any reference to it... |
| 7452 | */ |
| 7453 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7454 | "PEReference: %%%s; not found\n", |
| 7455 | name, NULL); |
| 7456 | ctxt->valid = 0; |
| 7457 | } |
| 7458 | } else { |
| 7459 | /* |
| 7460 | * Internal checking in case the entity quest barfed |
| 7461 | */ |
| 7462 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7463 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7464 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7465 | "Internal: %%%s; is not a parameter entity\n", |
| 7466 | name, NULL); |
| 7467 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 7468 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 7469 | if (xmlPushInput(ctxt, input) < 0) |
| 7470 | return; |
| 7471 | } else { |
| 7472 | /* |
| 7473 | * TODO !!! |
| 7474 | * handle the extra spaces added before and after |
| 7475 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 7476 | */ |
| 7477 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7478 | if (xmlPushInput(ctxt, input) < 0) |
| 7479 | return; |
| 7480 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7481 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 7482 | (IS_BLANK_CH(NXT(5)))) { |
| 7483 | xmlParseTextDecl(ctxt); |
| 7484 | if (ctxt->errNo == |
| 7485 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 7486 | /* |
| 7487 | * The XML REC instructs us to stop parsing |
| 7488 | * right here |
| 7489 | */ |
| 7490 | ctxt->instate = XML_PARSER_EOF; |
| 7491 | return; |
| 7492 | } |
| 7493 | } |
| 7494 | } |
| 7495 | } |
| 7496 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7497 | } |
| 7498 | |
| 7499 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7500 | * xmlLoadEntityContent: |
| 7501 | * @ctxt: an XML parser context |
| 7502 | * @entity: an unloaded system entity |
| 7503 | * |
| 7504 | * Load the original content of the given system entity from the |
| 7505 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 7506 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 7507 | * |
| 7508 | * Returns 0 in case of success and -1 in case of failure |
| 7509 | */ |
| 7510 | static int |
| 7511 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 7512 | xmlParserInputPtr input; |
| 7513 | xmlBufferPtr buf; |
| 7514 | int l, c; |
| 7515 | int count = 0; |
| 7516 | |
| 7517 | if ((ctxt == NULL) || (entity == NULL) || |
| 7518 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7519 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 7520 | (entity->content != NULL)) { |
| 7521 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7522 | "xmlLoadEntityContent parameter error"); |
| 7523 | return(-1); |
| 7524 | } |
| 7525 | |
| 7526 | if (xmlParserDebugEntities) |
| 7527 | xmlGenericError(xmlGenericErrorContext, |
| 7528 | "Reading %s entity content input\n", entity->name); |
| 7529 | |
| 7530 | buf = xmlBufferCreate(); |
| 7531 | if (buf == NULL) { |
| 7532 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7533 | "xmlLoadEntityContent parameter error"); |
| 7534 | return(-1); |
| 7535 | } |
| 7536 | |
| 7537 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7538 | if (input == NULL) { |
| 7539 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7540 | "xmlLoadEntityContent input error"); |
| 7541 | xmlBufferFree(buf); |
| 7542 | return(-1); |
| 7543 | } |
| 7544 | |
| 7545 | /* |
| 7546 | * Push the entity as the current input, read char by char |
| 7547 | * saving to the buffer until the end of the entity or an error |
| 7548 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 7549 | if (xmlPushInput(ctxt, input) < 0) { |
| 7550 | xmlBufferFree(buf); |
| 7551 | return(-1); |
| 7552 | } |
| 7553 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7554 | GROW; |
| 7555 | c = CUR_CHAR(l); |
| 7556 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 7557 | (IS_CHAR(c))) { |
| 7558 | xmlBufferAdd(buf, ctxt->input->cur, l); |
| 7559 | if (count++ > 100) { |
| 7560 | count = 0; |
| 7561 | GROW; |
| 7562 | } |
| 7563 | NEXTL(l); |
| 7564 | c = CUR_CHAR(l); |
| 7565 | } |
| 7566 | |
| 7567 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 7568 | xmlPopInput(ctxt); |
| 7569 | } else if (!IS_CHAR(c)) { |
| 7570 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 7571 | "xmlLoadEntityContent: invalid char value %d\n", |
| 7572 | c); |
| 7573 | xmlBufferFree(buf); |
| 7574 | return(-1); |
| 7575 | } |
| 7576 | entity->content = buf->content; |
| 7577 | buf->content = NULL; |
| 7578 | xmlBufferFree(buf); |
| 7579 | |
| 7580 | return(0); |
| 7581 | } |
| 7582 | |
| 7583 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7584 | * xmlParseStringPEReference: |
| 7585 | * @ctxt: an XML parser context |
| 7586 | * @str: a pointer to an index in the string |
| 7587 | * |
| 7588 | * parse PEReference declarations |
| 7589 | * |
| 7590 | * [69] PEReference ::= '%' Name ';' |
| 7591 | * |
| 7592 | * [ WFC: No Recursion ] |
| 7593 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7594 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7595 | * |
| 7596 | * [ WFC: Entity Declared ] |
| 7597 | * In a document without any DTD, a document with only an internal DTD |
| 7598 | * subset which contains no parameter entity references, or a document |
| 7599 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7600 | * entity must precede any reference to it... |
| 7601 | * |
| 7602 | * [ VC: Entity Declared ] |
| 7603 | * In a document with an external subset or external parameter entities |
| 7604 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7605 | * must precede any reference to it... |
| 7606 | * |
| 7607 | * [ WFC: In DTD ] |
| 7608 | * Parameter-entity references may only appear in the DTD. |
| 7609 | * NOTE: misleading but this is handled. |
| 7610 | * |
| 7611 | * Returns the string of the entity content. |
| 7612 | * str is updated to the current value of the index |
| 7613 | */ |
| 7614 | xmlEntityPtr |
| 7615 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 7616 | const xmlChar *ptr; |
| 7617 | xmlChar cur; |
| 7618 | xmlChar *name; |
| 7619 | xmlEntityPtr entity = NULL; |
| 7620 | |
| 7621 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 7622 | ptr = *str; |
| 7623 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7624 | if (cur != '%') |
| 7625 | return(NULL); |
| 7626 | ptr++; |
| 7627 | cur = *ptr; |
| 7628 | name = xmlParseStringName(ctxt, &ptr); |
| 7629 | if (name == NULL) { |
| 7630 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7631 | "xmlParseStringPEReference: no name\n"); |
| 7632 | *str = ptr; |
| 7633 | return(NULL); |
| 7634 | } |
| 7635 | cur = *ptr; |
| 7636 | if (cur != ';') { |
| 7637 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7638 | xmlFree(name); |
| 7639 | *str = ptr; |
| 7640 | return(NULL); |
| 7641 | } |
| 7642 | ptr++; |
| 7643 | |
| 7644 | /* |
| 7645 | * Increate the number of entity references parsed |
| 7646 | */ |
| 7647 | ctxt->nbentities++; |
| 7648 | |
| 7649 | /* |
| 7650 | * Request the entity from SAX |
| 7651 | */ |
| 7652 | if ((ctxt->sax != NULL) && |
| 7653 | (ctxt->sax->getParameterEntity != NULL)) |
| 7654 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7655 | name); |
| 7656 | if (entity == NULL) { |
| 7657 | /* |
| 7658 | * [ WFC: Entity Declared ] |
| 7659 | * In a document without any DTD, a document with only an |
| 7660 | * internal DTD subset which contains no parameter entity |
| 7661 | * references, or a document with "standalone='yes'", ... |
| 7662 | * ... The declaration of a parameter entity must precede |
| 7663 | * any reference to it... |
| 7664 | */ |
| 7665 | if ((ctxt->standalone == 1) || |
| 7666 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 7667 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7668 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7669 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7670 | /* |
| 7671 | * [ VC: Entity Declared ] |
| 7672 | * In a document with an external subset or external |
| 7673 | * parameter entities with "standalone='no'", ... |
| 7674 | * ... The declaration of a parameter entity must |
| 7675 | * precede any reference to it... |
| 7676 | */ |
| 7677 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7678 | "PEReference: %%%s; not found\n", |
| 7679 | name, NULL); |
| 7680 | ctxt->valid = 0; |
| 7681 | } |
| 7682 | } else { |
| 7683 | /* |
| 7684 | * Internal checking in case the entity quest barfed |
| 7685 | */ |
| 7686 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7687 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7688 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7689 | "%%%s; is not a parameter entity\n", |
| 7690 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7691 | } |
| 7692 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7693 | ctxt->hasPErefs = 1; |
| 7694 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7695 | *str = ptr; |
| 7696 | return(entity); |
| 7697 | } |
| 7698 | |
| 7699 | /** |
| 7700 | * xmlParseDocTypeDecl: |
| 7701 | * @ctxt: an XML parser context |
| 7702 | * |
| 7703 | * parse a DOCTYPE declaration |
| 7704 | * |
| 7705 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 7706 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 7707 | * |
| 7708 | * [ VC: Root Element Type ] |
| 7709 | * The Name in the document type declaration must match the element |
| 7710 | * type of the root element. |
| 7711 | */ |
| 7712 | |
| 7713 | void |
| 7714 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7715 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7716 | xmlChar *ExternalID = NULL; |
| 7717 | xmlChar *URI = NULL; |
| 7718 | |
| 7719 | /* |
| 7720 | * We know that '<!DOCTYPE' has been detected. |
| 7721 | */ |
| 7722 | SKIP(9); |
| 7723 | |
| 7724 | SKIP_BLANKS; |
| 7725 | |
| 7726 | /* |
| 7727 | * Parse the DOCTYPE name. |
| 7728 | */ |
| 7729 | name = xmlParseName(ctxt); |
| 7730 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7731 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7732 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7733 | } |
| 7734 | ctxt->intSubName = name; |
| 7735 | |
| 7736 | SKIP_BLANKS; |
| 7737 | |
| 7738 | /* |
| 7739 | * Check for SystemID and ExternalID |
| 7740 | */ |
| 7741 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 7742 | |
| 7743 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 7744 | ctxt->hasExternalSubset = 1; |
| 7745 | } |
| 7746 | ctxt->extSubURI = URI; |
| 7747 | ctxt->extSubSystem = ExternalID; |
| 7748 | |
| 7749 | SKIP_BLANKS; |
| 7750 | |
| 7751 | /* |
| 7752 | * Create and update the internal subset. |
| 7753 | */ |
| 7754 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 7755 | (!ctxt->disableSAX)) |
| 7756 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 7757 | |
| 7758 | /* |
| 7759 | * Is there any internal subset declarations ? |
| 7760 | * they are handled separately in xmlParseInternalSubset() |
| 7761 | */ |
| 7762 | if (RAW == '[') |
| 7763 | return; |
| 7764 | |
| 7765 | /* |
| 7766 | * We should be at the end of the DOCTYPE declaration. |
| 7767 | */ |
| 7768 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7769 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7770 | } |
| 7771 | NEXT; |
| 7772 | } |
| 7773 | |
| 7774 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7775 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7776 | * @ctxt: an XML parser context |
| 7777 | * |
| 7778 | * parse the internal subset declaration |
| 7779 | * |
| 7780 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 7781 | */ |
| 7782 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7783 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7784 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 7785 | /* |
| 7786 | * Is there any DTD definition ? |
| 7787 | */ |
| 7788 | if (RAW == '[') { |
| 7789 | ctxt->instate = XML_PARSER_DTD; |
| 7790 | NEXT; |
| 7791 | /* |
| 7792 | * Parse the succession of Markup declarations and |
| 7793 | * PEReferences. |
| 7794 | * Subsequence (markupdecl | PEReference | S)* |
| 7795 | */ |
| 7796 | while (RAW != ']') { |
| 7797 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7798 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7799 | |
| 7800 | SKIP_BLANKS; |
| 7801 | xmlParseMarkupDecl(ctxt); |
| 7802 | xmlParsePEReference(ctxt); |
| 7803 | |
| 7804 | /* |
| 7805 | * Pop-up of finished entities. |
| 7806 | */ |
| 7807 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7808 | xmlPopInput(ctxt); |
| 7809 | |
| 7810 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7811 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7812 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7813 | break; |
| 7814 | } |
| 7815 | } |
| 7816 | if (RAW == ']') { |
| 7817 | NEXT; |
| 7818 | SKIP_BLANKS; |
| 7819 | } |
| 7820 | } |
| 7821 | |
| 7822 | /* |
| 7823 | * We should be at the end of the DOCTYPE declaration. |
| 7824 | */ |
| 7825 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7826 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7827 | } |
| 7828 | NEXT; |
| 7829 | } |
| 7830 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7831 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7832 | /** |
| 7833 | * xmlParseAttribute: |
| 7834 | * @ctxt: an XML parser context |
| 7835 | * @value: a xmlChar ** used to store the value of the attribute |
| 7836 | * |
| 7837 | * parse an attribute |
| 7838 | * |
| 7839 | * [41] Attribute ::= Name Eq AttValue |
| 7840 | * |
| 7841 | * [ WFC: No External Entity References ] |
| 7842 | * Attribute values cannot contain direct or indirect entity references |
| 7843 | * to external entities. |
| 7844 | * |
| 7845 | * [ WFC: No < in Attribute Values ] |
| 7846 | * The replacement text of any entity referred to directly or indirectly in |
| 7847 | * an attribute value (other than "<") must not contain a <. |
| 7848 | * |
| 7849 | * [ VC: Attribute Value Type ] |
| 7850 | * The attribute must have been declared; the value must be of the type |
| 7851 | * declared for it. |
| 7852 | * |
| 7853 | * [25] Eq ::= S? '=' S? |
| 7854 | * |
| 7855 | * With namespace: |
| 7856 | * |
| 7857 | * [NS 11] Attribute ::= QName Eq AttValue |
| 7858 | * |
| 7859 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 7860 | * definition. |
| 7861 | * |
| 7862 | * Returns the attribute name, and the value in *value. |
| 7863 | */ |
| 7864 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7865 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7866 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7867 | const xmlChar *name; |
| 7868 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7869 | |
| 7870 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 7871 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7872 | name = xmlParseName(ctxt); |
| 7873 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7874 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7875 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7876 | return(NULL); |
| 7877 | } |
| 7878 | |
| 7879 | /* |
| 7880 | * read the value |
| 7881 | */ |
| 7882 | SKIP_BLANKS; |
| 7883 | if (RAW == '=') { |
| 7884 | NEXT; |
| 7885 | SKIP_BLANKS; |
| 7886 | val = xmlParseAttValue(ctxt); |
| 7887 | ctxt->instate = XML_PARSER_CONTENT; |
| 7888 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7889 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7890 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7891 | return(NULL); |
| 7892 | } |
| 7893 | |
| 7894 | /* |
| 7895 | * Check that xml:lang conforms to the specification |
| 7896 | * No more registered as an error, just generate a warning now |
| 7897 | * since this was deprecated in XML second edition |
| 7898 | */ |
| 7899 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 7900 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7901 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 7902 | "Malformed value for xml:lang : %s\n", |
| 7903 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7904 | } |
| 7905 | } |
| 7906 | |
| 7907 | /* |
| 7908 | * Check that xml:space conforms to the specification |
| 7909 | */ |
| 7910 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 7911 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 7912 | *(ctxt->space) = 0; |
| 7913 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 7914 | *(ctxt->space) = 1; |
| 7915 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 7916 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 7917 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 7918 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7919 | } |
| 7920 | } |
| 7921 | |
| 7922 | *value = val; |
| 7923 | return(name); |
| 7924 | } |
| 7925 | |
| 7926 | /** |
| 7927 | * xmlParseStartTag: |
| 7928 | * @ctxt: an XML parser context |
| 7929 | * |
| 7930 | * parse a start of tag either for rule element or |
| 7931 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 7932 | * |
| 7933 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 7934 | * |
| 7935 | * [ WFC: Unique Att Spec ] |
| 7936 | * No attribute name may appear more than once in the same start-tag or |
| 7937 | * empty-element tag. |
| 7938 | * |
| 7939 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 7940 | * |
| 7941 | * [ WFC: Unique Att Spec ] |
| 7942 | * No attribute name may appear more than once in the same start-tag or |
| 7943 | * empty-element tag. |
| 7944 | * |
| 7945 | * With namespace: |
| 7946 | * |
| 7947 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 7948 | * |
| 7949 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 7950 | * |
| 7951 | * Returns the element name parsed |
| 7952 | */ |
| 7953 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7954 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7955 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7956 | const xmlChar *name; |
| 7957 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7958 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7959 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7960 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7961 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7962 | int i; |
| 7963 | |
| 7964 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7965 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7966 | |
| 7967 | name = xmlParseName(ctxt); |
| 7968 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7969 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7970 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7971 | return(NULL); |
| 7972 | } |
| 7973 | |
| 7974 | /* |
| 7975 | * Now parse the attributes, it ends up with the ending |
| 7976 | * |
| 7977 | * (S Attribute)* S? |
| 7978 | */ |
| 7979 | SKIP_BLANKS; |
| 7980 | GROW; |
| 7981 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7982 | while ((RAW != '>') && |
| 7983 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7984 | (IS_BYTE_CHAR(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7985 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7986 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7987 | |
| 7988 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 7989 | if ((attname != NULL) && (attvalue != NULL)) { |
| 7990 | /* |
| 7991 | * [ WFC: Unique Att Spec ] |
| 7992 | * No attribute name may appear more than once in the same |
| 7993 | * start-tag or empty-element tag. |
| 7994 | */ |
| 7995 | for (i = 0; i < nbatts;i += 2) { |
| 7996 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7997 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7998 | xmlFree(attvalue); |
| 7999 | goto failed; |
| 8000 | } |
| 8001 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8002 | /* |
| 8003 | * Add the pair to atts |
| 8004 | */ |
| 8005 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8006 | maxatts = 22; /* allow for 10 attrs by default */ |
| 8007 | atts = (const xmlChar **) |
| 8008 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8009 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8010 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8011 | if (attvalue != NULL) |
| 8012 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8013 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8014 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8015 | ctxt->atts = atts; |
| 8016 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8017 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8018 | const xmlChar **n; |
| 8019 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8020 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8021 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8022 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8023 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8024 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8025 | if (attvalue != NULL) |
| 8026 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8027 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8028 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8029 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8030 | ctxt->atts = atts; |
| 8031 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8032 | } |
| 8033 | atts[nbatts++] = attname; |
| 8034 | atts[nbatts++] = attvalue; |
| 8035 | atts[nbatts] = NULL; |
| 8036 | atts[nbatts + 1] = NULL; |
| 8037 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8038 | if (attvalue != NULL) |
| 8039 | xmlFree(attvalue); |
| 8040 | } |
| 8041 | |
| 8042 | failed: |
| 8043 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8044 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8045 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8046 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8047 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8048 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8049 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8050 | } |
| 8051 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8052 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8053 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8054 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8055 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8056 | break; |
| 8057 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8058 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8059 | GROW; |
| 8060 | } |
| 8061 | |
| 8062 | /* |
| 8063 | * SAX: Start of Element ! |
| 8064 | */ |
| 8065 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8066 | (!ctxt->disableSAX)) { |
| 8067 | if (nbatts > 0) |
| 8068 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8069 | else |
| 8070 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8071 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8072 | |
| 8073 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8074 | /* Free only the content strings */ |
| 8075 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8076 | if (atts[i] != NULL) |
| 8077 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8078 | } |
| 8079 | return(name); |
| 8080 | } |
| 8081 | |
| 8082 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8083 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8084 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8085 | * @line: line of the start tag |
| 8086 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8087 | * |
| 8088 | * parse an end of tag |
| 8089 | * |
| 8090 | * [42] ETag ::= '</' Name S? '>' |
| 8091 | * |
| 8092 | * With namespace |
| 8093 | * |
| 8094 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8095 | */ |
| 8096 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8097 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8098 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8099 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8100 | |
| 8101 | GROW; |
| 8102 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8103 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8104 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8105 | return; |
| 8106 | } |
| 8107 | SKIP(2); |
| 8108 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8109 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8110 | |
| 8111 | /* |
| 8112 | * We should definitely be at the ending "S? '>'" part |
| 8113 | */ |
| 8114 | GROW; |
| 8115 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8116 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8117 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8118 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8119 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8120 | |
| 8121 | /* |
| 8122 | * [ WFC: Element Type Match ] |
| 8123 | * The Name in an element's end-tag must match the element type in the |
| 8124 | * start-tag. |
| 8125 | * |
| 8126 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8127 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8128 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8129 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8130 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8131 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8132 | } |
| 8133 | |
| 8134 | /* |
| 8135 | * SAX: End of Tag |
| 8136 | */ |
| 8137 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8138 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8139 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8140 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8141 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8142 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8143 | return; |
| 8144 | } |
| 8145 | |
| 8146 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8147 | * xmlParseEndTag: |
| 8148 | * @ctxt: an XML parser context |
| 8149 | * |
| 8150 | * parse an end of tag |
| 8151 | * |
| 8152 | * [42] ETag ::= '</' Name S? '>' |
| 8153 | * |
| 8154 | * With namespace |
| 8155 | * |
| 8156 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8157 | */ |
| 8158 | |
| 8159 | void |
| 8160 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8161 | xmlParseEndTag1(ctxt, 0); |
| 8162 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8163 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8164 | |
| 8165 | /************************************************************************ |
| 8166 | * * |
| 8167 | * SAX 2 specific operations * |
| 8168 | * * |
| 8169 | ************************************************************************/ |
| 8170 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8171 | /* |
| 8172 | * xmlGetNamespace: |
| 8173 | * @ctxt: an XML parser context |
| 8174 | * @prefix: the prefix to lookup |
| 8175 | * |
| 8176 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8177 | * The prefix must come from the @ctxt->dict dictionnary |
| 8178 | * |
| 8179 | * Returns the namespace name or NULL if not bound |
| 8180 | */ |
| 8181 | static const xmlChar * |
| 8182 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8183 | int i; |
| 8184 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8185 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8186 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8187 | if (ctxt->nsTab[i] == prefix) { |
| 8188 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8189 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8190 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8191 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8192 | return(NULL); |
| 8193 | } |
| 8194 | |
| 8195 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8196 | * xmlParseQName: |
| 8197 | * @ctxt: an XML parser context |
| 8198 | * @prefix: pointer to store the prefix part |
| 8199 | * |
| 8200 | * parse an XML Namespace QName |
| 8201 | * |
| 8202 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8203 | * [7] Prefix ::= NCName |
| 8204 | * [8] LocalPart ::= NCName |
| 8205 | * |
| 8206 | * Returns the Name parsed or NULL |
| 8207 | */ |
| 8208 | |
| 8209 | static const xmlChar * |
| 8210 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8211 | const xmlChar *l, *p; |
| 8212 | |
| 8213 | GROW; |
| 8214 | |
| 8215 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8216 | if (l == NULL) { |
| 8217 | if (CUR == ':') { |
| 8218 | l = xmlParseName(ctxt); |
| 8219 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8220 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8221 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8222 | *prefix = NULL; |
| 8223 | return(l); |
| 8224 | } |
| 8225 | } |
| 8226 | return(NULL); |
| 8227 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8228 | if (CUR == ':') { |
| 8229 | NEXT; |
| 8230 | p = l; |
| 8231 | l = xmlParseNCName(ctxt); |
| 8232 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8233 | xmlChar *tmp; |
| 8234 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8235 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8236 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8237 | l = xmlParseNmtoken(ctxt); |
| 8238 | if (l == NULL) |
| 8239 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8240 | else { |
| 8241 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8242 | xmlFree((char *)l); |
| 8243 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8244 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8245 | if (tmp != NULL) xmlFree(tmp); |
| 8246 | *prefix = NULL; |
| 8247 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8248 | } |
| 8249 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8250 | xmlChar *tmp; |
| 8251 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8252 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8253 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8254 | NEXT; |
| 8255 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8256 | if (tmp != NULL) { |
| 8257 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8258 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8259 | if (tmp != NULL) xmlFree(tmp); |
| 8260 | *prefix = p; |
| 8261 | return(l); |
| 8262 | } |
| 8263 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8264 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8265 | if (tmp != NULL) xmlFree(tmp); |
| 8266 | *prefix = p; |
| 8267 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8268 | } |
| 8269 | *prefix = p; |
| 8270 | } else |
| 8271 | *prefix = NULL; |
| 8272 | return(l); |
| 8273 | } |
| 8274 | |
| 8275 | /** |
| 8276 | * xmlParseQNameAndCompare: |
| 8277 | * @ctxt: an XML parser context |
| 8278 | * @name: the localname |
| 8279 | * @prefix: the prefix, if any. |
| 8280 | * |
| 8281 | * parse an XML name and compares for match |
| 8282 | * (specialized for endtag parsing) |
| 8283 | * |
| 8284 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8285 | * and the name for mismatch |
| 8286 | */ |
| 8287 | |
| 8288 | static const xmlChar * |
| 8289 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8290 | xmlChar const *prefix) { |
| 8291 | const xmlChar *cmp = name; |
| 8292 | const xmlChar *in; |
| 8293 | const xmlChar *ret; |
| 8294 | const xmlChar *prefix2; |
| 8295 | |
| 8296 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8297 | |
| 8298 | GROW; |
| 8299 | in = ctxt->input->cur; |
| 8300 | |
| 8301 | cmp = prefix; |
| 8302 | while (*in != 0 && *in == *cmp) { |
| 8303 | ++in; |
| 8304 | ++cmp; |
| 8305 | } |
| 8306 | if ((*cmp == 0) && (*in == ':')) { |
| 8307 | in++; |
| 8308 | cmp = name; |
| 8309 | while (*in != 0 && *in == *cmp) { |
| 8310 | ++in; |
| 8311 | ++cmp; |
| 8312 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8313 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8314 | /* success */ |
| 8315 | ctxt->input->cur = in; |
| 8316 | return((const xmlChar*) 1); |
| 8317 | } |
| 8318 | } |
| 8319 | /* |
| 8320 | * all strings coms from the dictionary, equality can be done directly |
| 8321 | */ |
| 8322 | ret = xmlParseQName (ctxt, &prefix2); |
| 8323 | if ((ret == name) && (prefix == prefix2)) |
| 8324 | return((const xmlChar*) 1); |
| 8325 | return ret; |
| 8326 | } |
| 8327 | |
| 8328 | /** |
| 8329 | * xmlParseAttValueInternal: |
| 8330 | * @ctxt: an XML parser context |
| 8331 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8332 | * @alloc: whether the attribute was reallocated as a new string |
| 8333 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8334 | * |
| 8335 | * parse a value for an attribute. |
| 8336 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8337 | * directly from the data buffer. |
| 8338 | * |
| 8339 | * 3.3.3 Attribute-Value Normalization: |
| 8340 | * Before the value of an attribute is passed to the application or |
| 8341 | * checked for validity, the XML processor must normalize it as follows: |
| 8342 | * - a character reference is processed by appending the referenced |
| 8343 | * character to the attribute value |
| 8344 | * - an entity reference is processed by recursively processing the |
| 8345 | * replacement text of the entity |
| 8346 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8347 | * appending #x20 to the normalized value, except that only a single |
| 8348 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 8349 | * parsed entity or the literal entity value of an internal parsed entity |
| 8350 | * - other characters are processed by appending them to the normalized value |
| 8351 | * If the declared value is not CDATA, then the XML processor must further |
| 8352 | * process the normalized attribute value by discarding any leading and |
| 8353 | * trailing space (#x20) characters, and by replacing sequences of space |
| 8354 | * (#x20) characters by a single space (#x20) character. |
| 8355 | * All attributes for which no declaration has been read should be treated |
| 8356 | * by a non-validating parser as if declared CDATA. |
| 8357 | * |
| 8358 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8359 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8360 | */ |
| 8361 | |
| 8362 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8363 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8364 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8365 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8366 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8367 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8368 | xmlChar *ret = NULL; |
| 8369 | |
| 8370 | GROW; |
| 8371 | in = (xmlChar *) CUR_PTR; |
| 8372 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8373 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8374 | return (NULL); |
| 8375 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8376 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8377 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8378 | /* |
| 8379 | * try to handle in this routine the most common case where no |
| 8380 | * allocation of a new string is required and where content is |
| 8381 | * pure ASCII. |
| 8382 | */ |
| 8383 | limit = *in++; |
| 8384 | end = ctxt->input->end; |
| 8385 | start = in; |
| 8386 | if (in >= end) { |
| 8387 | const xmlChar *oldbase = ctxt->input->base; |
| 8388 | GROW; |
| 8389 | if (oldbase != ctxt->input->base) { |
| 8390 | long delta = ctxt->input->base - oldbase; |
| 8391 | start = start + delta; |
| 8392 | in = in + delta; |
| 8393 | } |
| 8394 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8395 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8396 | if (normalize) { |
| 8397 | /* |
| 8398 | * Skip any leading spaces |
| 8399 | */ |
| 8400 | while ((in < end) && (*in != limit) && |
| 8401 | ((*in == 0x20) || (*in == 0x9) || |
| 8402 | (*in == 0xA) || (*in == 0xD))) { |
| 8403 | in++; |
| 8404 | start = in; |
| 8405 | if (in >= end) { |
| 8406 | const xmlChar *oldbase = ctxt->input->base; |
| 8407 | GROW; |
| 8408 | if (oldbase != ctxt->input->base) { |
| 8409 | long delta = ctxt->input->base - oldbase; |
| 8410 | start = start + delta; |
| 8411 | in = in + delta; |
| 8412 | } |
| 8413 | end = ctxt->input->end; |
| 8414 | } |
| 8415 | } |
| 8416 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8417 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8418 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 8419 | if (in >= end) { |
| 8420 | const xmlChar *oldbase = ctxt->input->base; |
| 8421 | GROW; |
| 8422 | if (oldbase != ctxt->input->base) { |
| 8423 | long delta = ctxt->input->base - oldbase; |
| 8424 | start = start + delta; |
| 8425 | in = in + delta; |
| 8426 | } |
| 8427 | end = ctxt->input->end; |
| 8428 | } |
| 8429 | } |
| 8430 | last = in; |
| 8431 | /* |
| 8432 | * skip the trailing blanks |
| 8433 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 8434 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8435 | while ((in < end) && (*in != limit) && |
| 8436 | ((*in == 0x20) || (*in == 0x9) || |
| 8437 | (*in == 0xA) || (*in == 0xD))) { |
| 8438 | in++; |
| 8439 | if (in >= end) { |
| 8440 | const xmlChar *oldbase = ctxt->input->base; |
| 8441 | GROW; |
| 8442 | if (oldbase != ctxt->input->base) { |
| 8443 | long delta = ctxt->input->base - oldbase; |
| 8444 | start = start + delta; |
| 8445 | in = in + delta; |
| 8446 | last = last + delta; |
| 8447 | } |
| 8448 | end = ctxt->input->end; |
| 8449 | } |
| 8450 | } |
| 8451 | if (*in != limit) goto need_complex; |
| 8452 | } else { |
| 8453 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8454 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8455 | in++; |
| 8456 | if (in >= end) { |
| 8457 | const xmlChar *oldbase = ctxt->input->base; |
| 8458 | GROW; |
| 8459 | if (oldbase != ctxt->input->base) { |
| 8460 | long delta = ctxt->input->base - oldbase; |
| 8461 | start = start + delta; |
| 8462 | in = in + delta; |
| 8463 | } |
| 8464 | end = ctxt->input->end; |
| 8465 | } |
| 8466 | } |
| 8467 | last = in; |
| 8468 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8469 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8470 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8471 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8472 | *len = last - start; |
| 8473 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8474 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8475 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8476 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8477 | } |
| 8478 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8479 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8480 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8481 | need_complex: |
| 8482 | if (alloc) *alloc = 1; |
| 8483 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
| 8486 | /** |
| 8487 | * xmlParseAttribute2: |
| 8488 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8489 | * @pref: the element prefix |
| 8490 | * @elem: the element name |
| 8491 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8492 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8493 | * @len: an int * to save the length of the attribute |
| 8494 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8495 | * |
| 8496 | * parse an attribute in the new SAX2 framework. |
| 8497 | * |
| 8498 | * Returns the attribute name, and the value in *value, . |
| 8499 | */ |
| 8500 | |
| 8501 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8502 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8503 | const xmlChar * pref, const xmlChar * elem, |
| 8504 | const xmlChar ** prefix, xmlChar ** value, |
| 8505 | int *len, int *alloc) |
| 8506 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8507 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8508 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8509 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8510 | |
| 8511 | *value = NULL; |
| 8512 | GROW; |
| 8513 | name = xmlParseQName(ctxt, prefix); |
| 8514 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8515 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8516 | "error parsing attribute name\n"); |
| 8517 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
| 8520 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8521 | * get the type if needed |
| 8522 | */ |
| 8523 | if (ctxt->attsSpecial != NULL) { |
| 8524 | int type; |
| 8525 | |
| 8526 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8527 | pref, elem, *prefix, name); |
| 8528 | if (type != 0) |
| 8529 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8530 | } |
| 8531 | |
| 8532 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8533 | * read the value |
| 8534 | */ |
| 8535 | SKIP_BLANKS; |
| 8536 | if (RAW == '=') { |
| 8537 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8538 | SKIP_BLANKS; |
| 8539 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 8540 | if (normalize) { |
| 8541 | /* |
| 8542 | * Sometimes a second normalisation pass for spaces is needed |
| 8543 | * but that only happens if charrefs or entities refernces |
| 8544 | * have been used in the attribute value, i.e. the attribute |
| 8545 | * value have been extracted in an allocated string already. |
| 8546 | */ |
| 8547 | if (*alloc) { |
| 8548 | const xmlChar *val2; |
| 8549 | |
| 8550 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8551 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8552 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 8553 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8554 | } |
| 8555 | } |
| 8556 | } |
| 8557 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8558 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8559 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 8560 | "Specification mandate value for attribute %s\n", |
| 8561 | name); |
| 8562 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8563 | } |
| 8564 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8565 | if (*prefix == ctxt->str_xml) { |
| 8566 | /* |
| 8567 | * Check that xml:lang conforms to the specification |
| 8568 | * No more registered as an error, just generate a warning now |
| 8569 | * since this was deprecated in XML second edition |
| 8570 | */ |
| 8571 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 8572 | internal_val = xmlStrndup(val, *len); |
| 8573 | if (!xmlCheckLanguageID(internal_val)) { |
| 8574 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8575 | "Malformed value for xml:lang : %s\n", |
| 8576 | internal_val, NULL); |
| 8577 | } |
| 8578 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8579 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8580 | /* |
| 8581 | * Check that xml:space conforms to the specification |
| 8582 | */ |
| 8583 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 8584 | internal_val = xmlStrndup(val, *len); |
| 8585 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 8586 | *(ctxt->space) = 0; |
| 8587 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 8588 | *(ctxt->space) = 1; |
| 8589 | else { |
| 8590 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 8591 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 8592 | internal_val, NULL); |
| 8593 | } |
| 8594 | } |
| 8595 | if (internal_val) { |
| 8596 | xmlFree(internal_val); |
| 8597 | } |
| 8598 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8599 | |
| 8600 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8601 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8602 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8603 | /** |
| 8604 | * xmlParseStartTag2: |
| 8605 | * @ctxt: an XML parser context |
| 8606 | * |
| 8607 | * parse a start of tag either for rule element or |
| 8608 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8609 | * This routine is called when running SAX2 parsing |
| 8610 | * |
| 8611 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8612 | * |
| 8613 | * [ WFC: Unique Att Spec ] |
| 8614 | * No attribute name may appear more than once in the same start-tag or |
| 8615 | * empty-element tag. |
| 8616 | * |
| 8617 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8618 | * |
| 8619 | * [ WFC: Unique Att Spec ] |
| 8620 | * No attribute name may appear more than once in the same start-tag or |
| 8621 | * empty-element tag. |
| 8622 | * |
| 8623 | * With namespace: |
| 8624 | * |
| 8625 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8626 | * |
| 8627 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8628 | * |
| 8629 | * Returns the element name parsed |
| 8630 | */ |
| 8631 | |
| 8632 | static const xmlChar * |
| 8633 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8634 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8635 | const xmlChar *localname; |
| 8636 | const xmlChar *prefix; |
| 8637 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8638 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8639 | const xmlChar *nsname; |
| 8640 | xmlChar *attvalue; |
| 8641 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8642 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8643 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8644 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8645 | const xmlChar *base; |
| 8646 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8647 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8648 | |
| 8649 | if (RAW != '<') return(NULL); |
| 8650 | NEXT1; |
| 8651 | |
| 8652 | /* |
| 8653 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 8654 | * point since the attribute values may be stored as pointers to |
| 8655 | * the buffer and calling SHRINK would destroy them ! |
| 8656 | * The Shrinking is only possible once the full set of attribute |
| 8657 | * callbacks have been done. |
| 8658 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8659 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8660 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8661 | base = ctxt->input->base; |
| 8662 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8663 | oldline = ctxt->input->line; |
| 8664 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8665 | nbatts = 0; |
| 8666 | nratts = 0; |
| 8667 | nbdef = 0; |
| 8668 | nbNs = 0; |
| 8669 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8670 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 8671 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8672 | |
| 8673 | localname = xmlParseQName(ctxt, &prefix); |
| 8674 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8675 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8676 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8677 | return(NULL); |
| 8678 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8679 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8680 | |
| 8681 | /* |
| 8682 | * Now parse the attributes, it ends up with the ending |
| 8683 | * |
| 8684 | * (S Attribute)* S? |
| 8685 | */ |
| 8686 | SKIP_BLANKS; |
| 8687 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8688 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8689 | |
| 8690 | while ((RAW != '>') && |
| 8691 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8692 | (IS_BYTE_CHAR(RAW))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8693 | const xmlChar *q = CUR_PTR; |
| 8694 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8695 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8696 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8697 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 8698 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8699 | if (ctxt->input->base != base) { |
| 8700 | if ((attvalue != NULL) && (alloc != 0)) |
| 8701 | xmlFree(attvalue); |
| 8702 | attvalue = NULL; |
| 8703 | goto base_changed; |
| 8704 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8705 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8706 | if (len < 0) len = xmlStrlen(attvalue); |
| 8707 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8708 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 8709 | xmlURIPtr uri; |
| 8710 | |
| 8711 | if (*URL != 0) { |
| 8712 | uri = xmlParseURI((const char *) URL); |
| 8713 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8714 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 8715 | "xmlns: '%s' is not a valid URI\n", |
| 8716 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8717 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame] | 8718 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8719 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 8720 | "xmlns: URI %s is not absolute\n", |
| 8721 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8722 | } |
| 8723 | xmlFreeURI(uri); |
| 8724 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8725 | if (URL == ctxt->str_xml_ns) { |
| 8726 | if (attname != ctxt->str_xml) { |
| 8727 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8728 | "xml namespace URI cannot be the default namespace\n", |
| 8729 | NULL, NULL, NULL); |
| 8730 | } |
| 8731 | goto skip_default_ns; |
| 8732 | } |
| 8733 | if ((len == 29) && |
| 8734 | (xmlStrEqual(URL, |
| 8735 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 8736 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8737 | "reuse of the xmlns namespace name is forbidden\n", |
| 8738 | NULL, NULL, NULL); |
| 8739 | goto skip_default_ns; |
| 8740 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8741 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8742 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8743 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8744 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8745 | for (j = 1;j <= nbNs;j++) |
| 8746 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 8747 | break; |
| 8748 | if (j <= nbNs) |
| 8749 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 8750 | else |
| 8751 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8752 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8753 | if (alloc != 0) xmlFree(attvalue); |
| 8754 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8755 | continue; |
| 8756 | } |
| 8757 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8758 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 8759 | xmlURIPtr uri; |
| 8760 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8761 | if (attname == ctxt->str_xml) { |
| 8762 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8763 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8764 | "xml namespace prefix mapped to wrong URI\n", |
| 8765 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8766 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8767 | /* |
| 8768 | * Do not keep a namespace definition node |
| 8769 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8770 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8771 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8772 | if (URL == ctxt->str_xml_ns) { |
| 8773 | if (attname != ctxt->str_xml) { |
| 8774 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8775 | "xml namespace URI mapped to wrong prefix\n", |
| 8776 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8777 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8778 | goto skip_ns; |
| 8779 | } |
| 8780 | if (attname == ctxt->str_xmlns) { |
| 8781 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8782 | "redefinition of the xmlns prefix is forbidden\n", |
| 8783 | NULL, NULL, NULL); |
| 8784 | goto skip_ns; |
| 8785 | } |
| 8786 | if ((len == 29) && |
| 8787 | (xmlStrEqual(URL, |
| 8788 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 8789 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8790 | "reuse of the xmlns namespace name is forbidden\n", |
| 8791 | NULL, NULL, NULL); |
| 8792 | goto skip_ns; |
| 8793 | } |
| 8794 | if ((URL == NULL) || (URL[0] == 0)) { |
| 8795 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8796 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 8797 | attname, NULL, NULL); |
| 8798 | goto skip_ns; |
| 8799 | } else { |
| 8800 | uri = xmlParseURI((const char *) URL); |
| 8801 | if (uri == NULL) { |
| 8802 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 8803 | "xmlns:%s: '%s' is not a valid URI\n", |
| 8804 | attname, URL, NULL); |
| 8805 | } else { |
| 8806 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 8807 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 8808 | "xmlns:%s: URI %s is not absolute\n", |
| 8809 | attname, URL, NULL); |
| 8810 | } |
| 8811 | xmlFreeURI(uri); |
| 8812 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8813 | } |
| 8814 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8815 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8816 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8817 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8818 | for (j = 1;j <= nbNs;j++) |
| 8819 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 8820 | break; |
| 8821 | if (j <= nbNs) |
| 8822 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 8823 | else |
| 8824 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8825 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8826 | if (alloc != 0) xmlFree(attvalue); |
| 8827 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 8828 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8829 | continue; |
| 8830 | } |
| 8831 | |
| 8832 | /* |
| 8833 | * Add the pair to atts |
| 8834 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8835 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 8836 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8837 | if (attvalue[len] == 0) |
| 8838 | xmlFree(attvalue); |
| 8839 | goto failed; |
| 8840 | } |
| 8841 | maxatts = ctxt->maxatts; |
| 8842 | atts = ctxt->atts; |
| 8843 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8844 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8845 | atts[nbatts++] = attname; |
| 8846 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8847 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8848 | atts[nbatts++] = attvalue; |
| 8849 | attvalue += len; |
| 8850 | atts[nbatts++] = attvalue; |
| 8851 | /* |
| 8852 | * tag if some deallocation is needed |
| 8853 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8854 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8855 | } else { |
| 8856 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 8857 | xmlFree(attvalue); |
| 8858 | } |
| 8859 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8860 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8861 | |
| 8862 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8863 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8864 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8865 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8866 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8867 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8868 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 8869 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8870 | } |
| 8871 | SKIP_BLANKS; |
| 8872 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8873 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8874 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8875 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8876 | break; |
| 8877 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8878 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8879 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8880 | } |
| 8881 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8882 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8883 | * The attributes defaulting |
| 8884 | */ |
| 8885 | if (ctxt->attsDefault != NULL) { |
| 8886 | xmlDefAttrsPtr defaults; |
| 8887 | |
| 8888 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 8889 | if (defaults != NULL) { |
| 8890 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8891 | attname = defaults->values[5 * i]; |
| 8892 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8893 | |
| 8894 | /* |
| 8895 | * special work for namespaces defaulted defs |
| 8896 | */ |
| 8897 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 8898 | /* |
| 8899 | * check that it's not a defined namespace |
| 8900 | */ |
| 8901 | for (j = 1;j <= nbNs;j++) |
| 8902 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 8903 | break; |
| 8904 | if (j <= nbNs) continue; |
| 8905 | |
| 8906 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8907 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8908 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8909 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8910 | nbNs++; |
| 8911 | } |
| 8912 | } else if (aprefix == ctxt->str_xmlns) { |
| 8913 | /* |
| 8914 | * check that it's not a defined namespace |
| 8915 | */ |
| 8916 | for (j = 1;j <= nbNs;j++) |
| 8917 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 8918 | break; |
| 8919 | if (j <= nbNs) continue; |
| 8920 | |
| 8921 | nsname = xmlGetNamespace(ctxt, attname); |
| 8922 | if (nsname != defaults->values[2]) { |
| 8923 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8924 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8925 | nbNs++; |
| 8926 | } |
| 8927 | } else { |
| 8928 | /* |
| 8929 | * check that it's not a defined attribute |
| 8930 | */ |
| 8931 | for (j = 0;j < nbatts;j+=5) { |
| 8932 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 8933 | break; |
| 8934 | } |
| 8935 | if (j < nbatts) continue; |
| 8936 | |
| 8937 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 8938 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 8939 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8940 | } |
| 8941 | maxatts = ctxt->maxatts; |
| 8942 | atts = ctxt->atts; |
| 8943 | } |
| 8944 | atts[nbatts++] = attname; |
| 8945 | atts[nbatts++] = aprefix; |
| 8946 | if (aprefix == NULL) |
| 8947 | atts[nbatts++] = NULL; |
| 8948 | else |
| 8949 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8950 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 8951 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 8952 | if ((ctxt->standalone == 1) && |
| 8953 | (defaults->values[5 * i + 4] != NULL)) { |
| 8954 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
| 8955 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 8956 | attname, localname); |
| 8957 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8958 | nbdef++; |
| 8959 | } |
| 8960 | } |
| 8961 | } |
| 8962 | } |
| 8963 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 8964 | /* |
| 8965 | * The attributes checkings |
| 8966 | */ |
| 8967 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 8968 | /* |
| 8969 | * The default namespace does not apply to attribute names. |
| 8970 | */ |
| 8971 | if (atts[i + 1] != NULL) { |
| 8972 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 8973 | if (nsname == NULL) { |
| 8974 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 8975 | "Namespace prefix %s for %s on %s is not defined\n", |
| 8976 | atts[i + 1], atts[i], localname); |
| 8977 | } |
| 8978 | atts[i + 2] = nsname; |
| 8979 | } else |
| 8980 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 8981 | /* |
| 8982 | * [ WFC: Unique Att Spec ] |
| 8983 | * No attribute name may appear more than once in the same |
| 8984 | * start-tag or empty-element tag. |
| 8985 | * As extended by the Namespace in XML REC. |
| 8986 | */ |
| 8987 | for (j = 0; j < i;j += 5) { |
| 8988 | if (atts[i] == atts[j]) { |
| 8989 | if (atts[i+1] == atts[j+1]) { |
| 8990 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 8991 | break; |
| 8992 | } |
| 8993 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 8994 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 8995 | "Namespaced Attribute %s in '%s' redefined\n", |
| 8996 | atts[i], nsname, NULL); |
| 8997 | break; |
| 8998 | } |
| 8999 | } |
| 9000 | } |
| 9001 | } |
| 9002 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9003 | nsname = xmlGetNamespace(ctxt, prefix); |
| 9004 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9005 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 9006 | "Namespace prefix %s on %s is not defined\n", |
| 9007 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9008 | } |
| 9009 | *pref = prefix; |
| 9010 | *URI = nsname; |
| 9011 | |
| 9012 | /* |
| 9013 | * SAX: Start of Element ! |
| 9014 | */ |
| 9015 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 9016 | (!ctxt->disableSAX)) { |
| 9017 | if (nbNs > 0) |
| 9018 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9019 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 9020 | nbatts / 5, nbdef, atts); |
| 9021 | else |
| 9022 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9023 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9024 | } |
| 9025 | |
| 9026 | /* |
| 9027 | * Free up attribute allocated strings if needed |
| 9028 | */ |
| 9029 | if (attval != 0) { |
| 9030 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9031 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9032 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9033 | } |
| 9034 | |
| 9035 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9036 | |
| 9037 | base_changed: |
| 9038 | /* |
| 9039 | * the attribute strings are valid iif the base didn't changed |
| 9040 | */ |
| 9041 | if (attval != 0) { |
| 9042 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9043 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9044 | xmlFree((xmlChar *) atts[i]); |
| 9045 | } |
| 9046 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9047 | ctxt->input->line = oldline; |
| 9048 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9049 | if (ctxt->wellFormed == 1) { |
| 9050 | goto reparse; |
| 9051 | } |
| 9052 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9053 | } |
| 9054 | |
| 9055 | /** |
| 9056 | * xmlParseEndTag2: |
| 9057 | * @ctxt: an XML parser context |
| 9058 | * @line: line of the start tag |
| 9059 | * @nsNr: number of namespaces on the start tag |
| 9060 | * |
| 9061 | * parse an end of tag |
| 9062 | * |
| 9063 | * [42] ETag ::= '</' Name S? '>' |
| 9064 | * |
| 9065 | * With namespace |
| 9066 | * |
| 9067 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9068 | */ |
| 9069 | |
| 9070 | static void |
| 9071 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9072 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9073 | const xmlChar *name; |
| 9074 | |
| 9075 | GROW; |
| 9076 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9077 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9078 | return; |
| 9079 | } |
| 9080 | SKIP(2); |
| 9081 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9082 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9083 | if (ctxt->input->cur[tlen] == '>') { |
| 9084 | ctxt->input->cur += tlen + 1; |
| 9085 | goto done; |
| 9086 | } |
| 9087 | ctxt->input->cur += tlen; |
| 9088 | name = (xmlChar*)1; |
| 9089 | } else { |
| 9090 | if (prefix == NULL) |
| 9091 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9092 | else |
| 9093 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9094 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9095 | |
| 9096 | /* |
| 9097 | * We should definitely be at the ending "S? '>'" part |
| 9098 | */ |
| 9099 | GROW; |
| 9100 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9101 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9102 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9103 | } else |
| 9104 | NEXT1; |
| 9105 | |
| 9106 | /* |
| 9107 | * [ WFC: Element Type Match ] |
| 9108 | * The Name in an element's end-tag must match the element type in the |
| 9109 | * start-tag. |
| 9110 | * |
| 9111 | */ |
| 9112 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9113 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 9114 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9115 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9116 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9117 | } |
| 9118 | |
| 9119 | /* |
| 9120 | * SAX: End of Tag |
| 9121 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9122 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9123 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9124 | (!ctxt->disableSAX)) |
| 9125 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9126 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9127 | spacePop(ctxt); |
| 9128 | if (nsNr != 0) |
| 9129 | nsPop(ctxt, nsNr); |
| 9130 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9131 | } |
| 9132 | |
| 9133 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9134 | * xmlParseCDSect: |
| 9135 | * @ctxt: an XML parser context |
| 9136 | * |
| 9137 | * Parse escaped pure raw content. |
| 9138 | * |
| 9139 | * [18] CDSect ::= CDStart CData CDEnd |
| 9140 | * |
| 9141 | * [19] CDStart ::= '<![CDATA[' |
| 9142 | * |
| 9143 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9144 | * |
| 9145 | * [21] CDEnd ::= ']]>' |
| 9146 | */ |
| 9147 | void |
| 9148 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9149 | xmlChar *buf = NULL; |
| 9150 | int len = 0; |
| 9151 | int size = XML_PARSER_BUFFER_SIZE; |
| 9152 | int r, rl; |
| 9153 | int s, sl; |
| 9154 | int cur, l; |
| 9155 | int count = 0; |
| 9156 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9157 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9158 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9159 | SKIP(9); |
| 9160 | } else |
| 9161 | return; |
| 9162 | |
| 9163 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9164 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9165 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9166 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9167 | ctxt->instate = XML_PARSER_CONTENT; |
| 9168 | return; |
| 9169 | } |
| 9170 | NEXTL(rl); |
| 9171 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9172 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9173 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9174 | ctxt->instate = XML_PARSER_CONTENT; |
| 9175 | return; |
| 9176 | } |
| 9177 | NEXTL(sl); |
| 9178 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9179 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9180 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9181 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9182 | return; |
| 9183 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9184 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9185 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9186 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9187 | xmlChar *tmp; |
| 9188 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9189 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9190 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9191 | if (tmp == NULL) { |
| 9192 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9193 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9194 | return; |
| 9195 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9196 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9197 | } |
| 9198 | COPY_BUF(rl,buf,len,r); |
| 9199 | r = s; |
| 9200 | rl = sl; |
| 9201 | s = cur; |
| 9202 | sl = l; |
| 9203 | count++; |
| 9204 | if (count > 50) { |
| 9205 | GROW; |
| 9206 | count = 0; |
| 9207 | } |
| 9208 | NEXTL(l); |
| 9209 | cur = CUR_CHAR(l); |
| 9210 | } |
| 9211 | buf[len] = 0; |
| 9212 | ctxt->instate = XML_PARSER_CONTENT; |
| 9213 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9214 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9215 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9216 | xmlFree(buf); |
| 9217 | return; |
| 9218 | } |
| 9219 | NEXTL(l); |
| 9220 | |
| 9221 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9222 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9223 | */ |
| 9224 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9225 | if (ctxt->sax->cdataBlock != NULL) |
| 9226 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9227 | else if (ctxt->sax->characters != NULL) |
| 9228 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9229 | } |
| 9230 | xmlFree(buf); |
| 9231 | } |
| 9232 | |
| 9233 | /** |
| 9234 | * xmlParseContent: |
| 9235 | * @ctxt: an XML parser context |
| 9236 | * |
| 9237 | * Parse a content: |
| 9238 | * |
| 9239 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9240 | */ |
| 9241 | |
| 9242 | void |
| 9243 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9244 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9245 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9246 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9247 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9248 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9249 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9250 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9251 | |
| 9252 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9253 | * First case : a Processing Instruction. |
| 9254 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9255 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9256 | xmlParsePI(ctxt); |
| 9257 | } |
| 9258 | |
| 9259 | /* |
| 9260 | * Second case : a CDSection |
| 9261 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9262 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9263 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9264 | xmlParseCDSect(ctxt); |
| 9265 | } |
| 9266 | |
| 9267 | /* |
| 9268 | * Third case : a comment |
| 9269 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9270 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9271 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9272 | xmlParseComment(ctxt); |
| 9273 | ctxt->instate = XML_PARSER_CONTENT; |
| 9274 | } |
| 9275 | |
| 9276 | /* |
| 9277 | * Fourth case : a sub-element. |
| 9278 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9279 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9280 | xmlParseElement(ctxt); |
| 9281 | } |
| 9282 | |
| 9283 | /* |
| 9284 | * Fifth case : a reference. If if has not been resolved, |
| 9285 | * parsing returns it's Name, create the node |
| 9286 | */ |
| 9287 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9288 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9289 | xmlParseReference(ctxt); |
| 9290 | } |
| 9291 | |
| 9292 | /* |
| 9293 | * Last case, text. Note that References are handled directly. |
| 9294 | */ |
| 9295 | else { |
| 9296 | xmlParseCharData(ctxt, 0); |
| 9297 | } |
| 9298 | |
| 9299 | GROW; |
| 9300 | /* |
| 9301 | * Pop-up of finished entities. |
| 9302 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9303 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9304 | xmlPopInput(ctxt); |
| 9305 | SHRINK; |
| 9306 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9307 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9308 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9309 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9310 | ctxt->instate = XML_PARSER_EOF; |
| 9311 | break; |
| 9312 | } |
| 9313 | } |
| 9314 | } |
| 9315 | |
| 9316 | /** |
| 9317 | * xmlParseElement: |
| 9318 | * @ctxt: an XML parser context |
| 9319 | * |
| 9320 | * parse an XML element, this is highly recursive |
| 9321 | * |
| 9322 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9323 | * |
| 9324 | * [ WFC: Element Type Match ] |
| 9325 | * The Name in an element's end-tag must match the element type in the |
| 9326 | * start-tag. |
| 9327 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9328 | */ |
| 9329 | |
| 9330 | void |
| 9331 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 9332 | const xmlChar *name; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9333 | const xmlChar *prefix; |
| 9334 | const xmlChar *URI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9335 | xmlParserNodeInfo node_info; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9336 | int line, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9337 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9338 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9339 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 9340 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 9341 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9342 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9343 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 9344 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9345 | ctxt->instate = XML_PARSER_EOF; |
| 9346 | return; |
| 9347 | } |
| 9348 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9349 | /* Capture start position */ |
| 9350 | if (ctxt->record_info) { |
| 9351 | node_info.begin_pos = ctxt->input->consumed + |
| 9352 | (CUR_PTR - ctxt->input->base); |
| 9353 | node_info.begin_line = ctxt->input->line; |
| 9354 | } |
| 9355 | |
| 9356 | if (ctxt->spaceNr == 0) |
| 9357 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 9358 | else if (*ctxt->space == -2) |
| 9359 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9360 | else |
| 9361 | spacePush(ctxt, *ctxt->space); |
| 9362 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9363 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9364 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9365 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9366 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9367 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9368 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9369 | else |
| 9370 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9371 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9372 | if (name == NULL) { |
| 9373 | spacePop(ctxt); |
| 9374 | return; |
| 9375 | } |
| 9376 | namePush(ctxt, name); |
| 9377 | ret = ctxt->node; |
| 9378 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9379 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9380 | /* |
| 9381 | * [ VC: Root Element Type ] |
| 9382 | * The Name in the document type declaration must match the element |
| 9383 | * type of the root element. |
| 9384 | */ |
| 9385 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9386 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9387 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9388 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9389 | |
| 9390 | /* |
| 9391 | * Check for an Empty Element. |
| 9392 | */ |
| 9393 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9394 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9395 | if (ctxt->sax2) { |
| 9396 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9397 | (!ctxt->disableSAX)) |
| 9398 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9399 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9400 | } else { |
| 9401 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 9402 | (!ctxt->disableSAX)) |
| 9403 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9404 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9405 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9406 | namePop(ctxt); |
| 9407 | spacePop(ctxt); |
| 9408 | if (nsNr != ctxt->nsNr) |
| 9409 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9410 | if ( ret != NULL && ctxt->record_info ) { |
| 9411 | node_info.end_pos = ctxt->input->consumed + |
| 9412 | (CUR_PTR - ctxt->input->base); |
| 9413 | node_info.end_line = ctxt->input->line; |
| 9414 | node_info.node = ret; |
| 9415 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9416 | } |
| 9417 | return; |
| 9418 | } |
| 9419 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9420 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9421 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9422 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 9423 | "Couldn't find end of Start Tag %s line %d\n", |
| 9424 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9425 | |
| 9426 | /* |
| 9427 | * end of parsing of this node. |
| 9428 | */ |
| 9429 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9430 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9431 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9432 | if (nsNr != ctxt->nsNr) |
| 9433 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9434 | |
| 9435 | /* |
| 9436 | * Capture end position and add node |
| 9437 | */ |
| 9438 | if ( ret != NULL && ctxt->record_info ) { |
| 9439 | node_info.end_pos = ctxt->input->consumed + |
| 9440 | (CUR_PTR - ctxt->input->base); |
| 9441 | node_info.end_line = ctxt->input->line; |
| 9442 | node_info.node = ret; |
| 9443 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9444 | } |
| 9445 | return; |
| 9446 | } |
| 9447 | |
| 9448 | /* |
| 9449 | * Parse the content of the element: |
| 9450 | */ |
| 9451 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9452 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9453 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 9454 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9455 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9456 | |
| 9457 | /* |
| 9458 | * end of parsing of this node. |
| 9459 | */ |
| 9460 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9461 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9462 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9463 | if (nsNr != ctxt->nsNr) |
| 9464 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9465 | return; |
| 9466 | } |
| 9467 | |
| 9468 | /* |
| 9469 | * parse the end of tag: '</' should be here. |
| 9470 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9471 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9472 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9473 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9474 | } |
| 9475 | #ifdef LIBXML_SAX1_ENABLED |
| 9476 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9477 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9478 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9479 | |
| 9480 | /* |
| 9481 | * Capture end position and add node |
| 9482 | */ |
| 9483 | if ( ret != NULL && ctxt->record_info ) { |
| 9484 | node_info.end_pos = ctxt->input->consumed + |
| 9485 | (CUR_PTR - ctxt->input->base); |
| 9486 | node_info.end_line = ctxt->input->line; |
| 9487 | node_info.node = ret; |
| 9488 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9489 | } |
| 9490 | } |
| 9491 | |
| 9492 | /** |
| 9493 | * xmlParseVersionNum: |
| 9494 | * @ctxt: an XML parser context |
| 9495 | * |
| 9496 | * parse the XML version value. |
| 9497 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9498 | * [26] VersionNum ::= '1.' [0-9]+ |
| 9499 | * |
| 9500 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9501 | * |
| 9502 | * Returns the string giving the XML version number, or NULL |
| 9503 | */ |
| 9504 | xmlChar * |
| 9505 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 9506 | xmlChar *buf = NULL; |
| 9507 | int len = 0; |
| 9508 | int size = 10; |
| 9509 | xmlChar cur; |
| 9510 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9511 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9512 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9513 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9514 | return(NULL); |
| 9515 | } |
| 9516 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9517 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9518 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9519 | return(NULL); |
| 9520 | } |
| 9521 | buf[len++] = cur; |
| 9522 | NEXT; |
| 9523 | cur=CUR; |
| 9524 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9525 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9526 | return(NULL); |
| 9527 | } |
| 9528 | buf[len++] = cur; |
| 9529 | NEXT; |
| 9530 | cur=CUR; |
| 9531 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9532 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9533 | xmlChar *tmp; |
| 9534 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9535 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9536 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9537 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9538 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9539 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9540 | return(NULL); |
| 9541 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9542 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9543 | } |
| 9544 | buf[len++] = cur; |
| 9545 | NEXT; |
| 9546 | cur=CUR; |
| 9547 | } |
| 9548 | buf[len] = 0; |
| 9549 | return(buf); |
| 9550 | } |
| 9551 | |
| 9552 | /** |
| 9553 | * xmlParseVersionInfo: |
| 9554 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9555 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9556 | * parse the XML version. |
| 9557 | * |
| 9558 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9559 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9560 | * [25] Eq ::= S? '=' S? |
| 9561 | * |
| 9562 | * Returns the version string, e.g. "1.0" |
| 9563 | */ |
| 9564 | |
| 9565 | xmlChar * |
| 9566 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 9567 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9568 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9569 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9570 | SKIP(7); |
| 9571 | SKIP_BLANKS; |
| 9572 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9573 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9574 | return(NULL); |
| 9575 | } |
| 9576 | NEXT; |
| 9577 | SKIP_BLANKS; |
| 9578 | if (RAW == '"') { |
| 9579 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9580 | version = xmlParseVersionNum(ctxt); |
| 9581 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9582 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9583 | } else |
| 9584 | NEXT; |
| 9585 | } else if (RAW == '\''){ |
| 9586 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9587 | version = xmlParseVersionNum(ctxt); |
| 9588 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9589 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9590 | } else |
| 9591 | NEXT; |
| 9592 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9593 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9594 | } |
| 9595 | } |
| 9596 | return(version); |
| 9597 | } |
| 9598 | |
| 9599 | /** |
| 9600 | * xmlParseEncName: |
| 9601 | * @ctxt: an XML parser context |
| 9602 | * |
| 9603 | * parse the XML encoding name |
| 9604 | * |
| 9605 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 9606 | * |
| 9607 | * Returns the encoding name value or NULL |
| 9608 | */ |
| 9609 | xmlChar * |
| 9610 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 9611 | xmlChar *buf = NULL; |
| 9612 | int len = 0; |
| 9613 | int size = 10; |
| 9614 | xmlChar cur; |
| 9615 | |
| 9616 | cur = CUR; |
| 9617 | if (((cur >= 'a') && (cur <= 'z')) || |
| 9618 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9619 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9620 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9621 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9622 | return(NULL); |
| 9623 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9624 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9625 | buf[len++] = cur; |
| 9626 | NEXT; |
| 9627 | cur = CUR; |
| 9628 | while (((cur >= 'a') && (cur <= 'z')) || |
| 9629 | ((cur >= 'A') && (cur <= 'Z')) || |
| 9630 | ((cur >= '0') && (cur <= '9')) || |
| 9631 | (cur == '.') || (cur == '_') || |
| 9632 | (cur == '-')) { |
| 9633 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9634 | xmlChar *tmp; |
| 9635 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9636 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9637 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9638 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9639 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9640 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9641 | return(NULL); |
| 9642 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9643 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9644 | } |
| 9645 | buf[len++] = cur; |
| 9646 | NEXT; |
| 9647 | cur = CUR; |
| 9648 | if (cur == 0) { |
| 9649 | SHRINK; |
| 9650 | GROW; |
| 9651 | cur = CUR; |
| 9652 | } |
| 9653 | } |
| 9654 | buf[len] = 0; |
| 9655 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9656 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9657 | } |
| 9658 | return(buf); |
| 9659 | } |
| 9660 | |
| 9661 | /** |
| 9662 | * xmlParseEncodingDecl: |
| 9663 | * @ctxt: an XML parser context |
| 9664 | * |
| 9665 | * parse the XML encoding declaration |
| 9666 | * |
| 9667 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 9668 | * |
| 9669 | * this setups the conversion filters. |
| 9670 | * |
| 9671 | * Returns the encoding value or NULL |
| 9672 | */ |
| 9673 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9674 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9675 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 9676 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9677 | |
| 9678 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9679 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9680 | SKIP(8); |
| 9681 | SKIP_BLANKS; |
| 9682 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9683 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9684 | return(NULL); |
| 9685 | } |
| 9686 | NEXT; |
| 9687 | SKIP_BLANKS; |
| 9688 | if (RAW == '"') { |
| 9689 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9690 | encoding = xmlParseEncName(ctxt); |
| 9691 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9692 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9693 | } else |
| 9694 | NEXT; |
| 9695 | } else if (RAW == '\''){ |
| 9696 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9697 | encoding = xmlParseEncName(ctxt); |
| 9698 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9699 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9700 | } else |
| 9701 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 9702 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9703 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9704 | } |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9705 | /* |
| 9706 | * UTF-16 encoding stwich has already taken place at this stage, |
| 9707 | * more over the little-endian/big-endian selection is already done |
| 9708 | */ |
| 9709 | if ((encoding != NULL) && |
| 9710 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 9711 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9712 | /* |
| 9713 | * If no encoding was passed to the parser, that we are |
| 9714 | * using UTF-16 and no decoder is present i.e. the |
| 9715 | * document is apparently UTF-8 compatible, then raise an |
| 9716 | * encoding mismatch fatal error |
| 9717 | */ |
| 9718 | if ((ctxt->encoding == NULL) && |
| 9719 | (ctxt->input->buf != NULL) && |
| 9720 | (ctxt->input->buf->encoder == NULL)) { |
| 9721 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 9722 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 9723 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9724 | if (ctxt->encoding != NULL) |
| 9725 | xmlFree((xmlChar *) ctxt->encoding); |
| 9726 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9727 | } |
| 9728 | /* |
| 9729 | * UTF-8 encoding is handled natively |
| 9730 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9731 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9732 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 9733 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9734 | if (ctxt->encoding != NULL) |
| 9735 | xmlFree((xmlChar *) ctxt->encoding); |
| 9736 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9737 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9738 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9739 | xmlCharEncodingHandlerPtr handler; |
| 9740 | |
| 9741 | if (ctxt->input->encoding != NULL) |
| 9742 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 9743 | ctxt->input->encoding = encoding; |
| 9744 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 9745 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 9746 | if (handler != NULL) { |
| 9747 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9748 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9749 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 9750 | "Unsupported encoding %s\n", encoding); |
| 9751 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9752 | } |
| 9753 | } |
| 9754 | } |
| 9755 | return(encoding); |
| 9756 | } |
| 9757 | |
| 9758 | /** |
| 9759 | * xmlParseSDDecl: |
| 9760 | * @ctxt: an XML parser context |
| 9761 | * |
| 9762 | * parse the XML standalone declaration |
| 9763 | * |
| 9764 | * [32] SDDecl ::= S 'standalone' Eq |
| 9765 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 9766 | * |
| 9767 | * [ VC: Standalone Document Declaration ] |
| 9768 | * TODO The standalone document declaration must have the value "no" |
| 9769 | * if any external markup declarations contain declarations of: |
| 9770 | * - attributes with default values, if elements to which these |
| 9771 | * attributes apply appear in the document without specifications |
| 9772 | * of values for these attributes, or |
| 9773 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 9774 | * to those entities appear in the document, or |
| 9775 | * - attributes with values subject to normalization, where the |
| 9776 | * attribute appears in the document with a value which will change |
| 9777 | * as a result of normalization, or |
| 9778 | * - element types with element content, if white space occurs directly |
| 9779 | * within any instance of those types. |
| 9780 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 9781 | * Returns: |
| 9782 | * 1 if standalone="yes" |
| 9783 | * 0 if standalone="no" |
| 9784 | * -2 if standalone attribute is missing or invalid |
| 9785 | * (A standalone value of -2 means that the XML declaration was found, |
| 9786 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9787 | */ |
| 9788 | |
| 9789 | int |
| 9790 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 9791 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9792 | |
| 9793 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9794 | 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] | 9795 | SKIP(10); |
| 9796 | SKIP_BLANKS; |
| 9797 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9798 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9799 | return(standalone); |
| 9800 | } |
| 9801 | NEXT; |
| 9802 | SKIP_BLANKS; |
| 9803 | if (RAW == '\''){ |
| 9804 | NEXT; |
| 9805 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 9806 | standalone = 0; |
| 9807 | SKIP(2); |
| 9808 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 9809 | (NXT(2) == 's')) { |
| 9810 | standalone = 1; |
| 9811 | SKIP(3); |
| 9812 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9813 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9814 | } |
| 9815 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9816 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9817 | } else |
| 9818 | NEXT; |
| 9819 | } else if (RAW == '"'){ |
| 9820 | NEXT; |
| 9821 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 9822 | standalone = 0; |
| 9823 | SKIP(2); |
| 9824 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 9825 | (NXT(2) == 's')) { |
| 9826 | standalone = 1; |
| 9827 | SKIP(3); |
| 9828 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9829 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9830 | } |
| 9831 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9832 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9833 | } else |
| 9834 | NEXT; |
| 9835 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9836 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9837 | } |
| 9838 | } |
| 9839 | return(standalone); |
| 9840 | } |
| 9841 | |
| 9842 | /** |
| 9843 | * xmlParseXMLDecl: |
| 9844 | * @ctxt: an XML parser context |
| 9845 | * |
| 9846 | * parse an XML declaration header |
| 9847 | * |
| 9848 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 9849 | */ |
| 9850 | |
| 9851 | void |
| 9852 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 9853 | xmlChar *version; |
| 9854 | |
| 9855 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 9856 | * This value for standalone indicates that the document has an |
| 9857 | * XML declaration but it does not have a standalone attribute. |
| 9858 | * It will be overwritten later if a standalone attribute is found. |
| 9859 | */ |
| 9860 | ctxt->input->standalone = -2; |
| 9861 | |
| 9862 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9863 | * We know that '<?xml' is here. |
| 9864 | */ |
| 9865 | SKIP(5); |
| 9866 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9867 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9868 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9869 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9870 | } |
| 9871 | SKIP_BLANKS; |
| 9872 | |
| 9873 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9874 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9875 | */ |
| 9876 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9877 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9878 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9879 | } else { |
| 9880 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 9881 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9882 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9883 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9884 | if (ctxt->options & XML_PARSE_OLD10) { |
| 9885 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 9886 | "Unsupported version '%s'\n", |
| 9887 | version); |
| 9888 | } else { |
| 9889 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 9890 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 9891 | "Unsupported version '%s'\n", |
| 9892 | version, NULL); |
| 9893 | } else { |
| 9894 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 9895 | "Unsupported version '%s'\n", |
| 9896 | version); |
| 9897 | } |
| 9898 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9899 | } |
| 9900 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 9901 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9902 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 9903 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9904 | |
| 9905 | /* |
| 9906 | * We may have the encoding declaration |
| 9907 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9908 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9909 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9910 | SKIP(2); |
| 9911 | return; |
| 9912 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9913 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9914 | } |
| 9915 | xmlParseEncodingDecl(ctxt); |
| 9916 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 9917 | /* |
| 9918 | * The XML REC instructs us to stop parsing right here |
| 9919 | */ |
| 9920 | return; |
| 9921 | } |
| 9922 | |
| 9923 | /* |
| 9924 | * We may have the standalone status. |
| 9925 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9926 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9927 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9928 | SKIP(2); |
| 9929 | return; |
| 9930 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9931 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9932 | } |
| 9933 | SKIP_BLANKS; |
| 9934 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 9935 | |
| 9936 | SKIP_BLANKS; |
| 9937 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9938 | SKIP(2); |
| 9939 | } else if (RAW == '>') { |
| 9940 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9941 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9942 | NEXT; |
| 9943 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9944 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9945 | MOVETO_ENDTAG(CUR_PTR); |
| 9946 | NEXT; |
| 9947 | } |
| 9948 | } |
| 9949 | |
| 9950 | /** |
| 9951 | * xmlParseMisc: |
| 9952 | * @ctxt: an XML parser context |
| 9953 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9954 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9955 | * |
| 9956 | * [27] Misc ::= Comment | PI | S |
| 9957 | */ |
| 9958 | |
| 9959 | void |
| 9960 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9961 | while (((RAW == '<') && (NXT(1) == '?')) || |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9962 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9963 | IS_BLANK_CH(CUR)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9964 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9965 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9966 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9967 | NEXT; |
| 9968 | } else |
| 9969 | xmlParseComment(ctxt); |
| 9970 | } |
| 9971 | } |
| 9972 | |
| 9973 | /** |
| 9974 | * xmlParseDocument: |
| 9975 | * @ctxt: an XML parser context |
| 9976 | * |
| 9977 | * parse an XML document (and build a tree if using the standard SAX |
| 9978 | * interface). |
| 9979 | * |
| 9980 | * [1] document ::= prolog element Misc* |
| 9981 | * |
| 9982 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 9983 | * |
| 9984 | * Returns 0, -1 in case of error. the parser context is augmented |
| 9985 | * as a result of the parsing. |
| 9986 | */ |
| 9987 | |
| 9988 | int |
| 9989 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 9990 | xmlChar start[4]; |
| 9991 | xmlCharEncoding enc; |
| 9992 | |
| 9993 | xmlInitParser(); |
| 9994 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 9995 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 9996 | return(-1); |
| 9997 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9998 | GROW; |
| 9999 | |
| 10000 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10001 | * SAX: detecting the level. |
| 10002 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10003 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 10004 | |
| 10005 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10006 | * SAX: beginning of the document processing. |
| 10007 | */ |
| 10008 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10009 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10010 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10011 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 10012 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10013 | /* |
| 10014 | * Get the 4 first bytes and decode the charset |
| 10015 | * if enc != XML_CHAR_ENCODING_NONE |
| 10016 | * plug some encoding conversion routines. |
| 10017 | */ |
| 10018 | start[0] = RAW; |
| 10019 | start[1] = NXT(1); |
| 10020 | start[2] = NXT(2); |
| 10021 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10022 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10023 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10024 | xmlSwitchEncoding(ctxt, enc); |
| 10025 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10026 | } |
| 10027 | |
| 10028 | |
| 10029 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10030 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10031 | } |
| 10032 | |
| 10033 | /* |
| 10034 | * Check for the XMLDecl in the Prolog. |
| 10035 | */ |
| 10036 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10037 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10038 | |
| 10039 | /* |
| 10040 | * Note that we will switch encoding on the fly. |
| 10041 | */ |
| 10042 | xmlParseXMLDecl(ctxt); |
| 10043 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10044 | /* |
| 10045 | * The XML REC instructs us to stop parsing right here |
| 10046 | */ |
| 10047 | return(-1); |
| 10048 | } |
| 10049 | ctxt->standalone = ctxt->input->standalone; |
| 10050 | SKIP_BLANKS; |
| 10051 | } else { |
| 10052 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10053 | } |
| 10054 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10055 | ctxt->sax->startDocument(ctxt->userData); |
| 10056 | |
| 10057 | /* |
| 10058 | * The Misc part of the Prolog |
| 10059 | */ |
| 10060 | GROW; |
| 10061 | xmlParseMisc(ctxt); |
| 10062 | |
| 10063 | /* |
| 10064 | * Then possibly doc type declaration(s) and more Misc |
| 10065 | * (doctypedecl Misc*)? |
| 10066 | */ |
| 10067 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10068 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10069 | |
| 10070 | ctxt->inSubset = 1; |
| 10071 | xmlParseDocTypeDecl(ctxt); |
| 10072 | if (RAW == '[') { |
| 10073 | ctxt->instate = XML_PARSER_DTD; |
| 10074 | xmlParseInternalSubset(ctxt); |
| 10075 | } |
| 10076 | |
| 10077 | /* |
| 10078 | * Create and update the external subset. |
| 10079 | */ |
| 10080 | ctxt->inSubset = 2; |
| 10081 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10082 | (!ctxt->disableSAX)) |
| 10083 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10084 | ctxt->extSubSystem, ctxt->extSubURI); |
| 10085 | ctxt->inSubset = 0; |
| 10086 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10087 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10088 | |
| 10089 | ctxt->instate = XML_PARSER_PROLOG; |
| 10090 | xmlParseMisc(ctxt); |
| 10091 | } |
| 10092 | |
| 10093 | /* |
| 10094 | * Time to start parsing the tree itself |
| 10095 | */ |
| 10096 | GROW; |
| 10097 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10098 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10099 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10100 | } else { |
| 10101 | ctxt->instate = XML_PARSER_CONTENT; |
| 10102 | xmlParseElement(ctxt); |
| 10103 | ctxt->instate = XML_PARSER_EPILOG; |
| 10104 | |
| 10105 | |
| 10106 | /* |
| 10107 | * The Misc part at the end |
| 10108 | */ |
| 10109 | xmlParseMisc(ctxt); |
| 10110 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10111 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10112 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10113 | } |
| 10114 | ctxt->instate = XML_PARSER_EOF; |
| 10115 | } |
| 10116 | |
| 10117 | /* |
| 10118 | * SAX: end of the document processing. |
| 10119 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10120 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10121 | ctxt->sax->endDocument(ctxt->userData); |
| 10122 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10123 | /* |
| 10124 | * Remove locally kept entity definitions if the tree was not built |
| 10125 | */ |
| 10126 | if ((ctxt->myDoc != NULL) && |
| 10127 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10128 | xmlFreeDoc(ctxt->myDoc); |
| 10129 | ctxt->myDoc = NULL; |
| 10130 | } |
| 10131 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10132 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10133 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10134 | if (ctxt->valid) |
| 10135 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10136 | if (ctxt->nsWellFormed) |
| 10137 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10138 | if (ctxt->options & XML_PARSE_OLD10) |
| 10139 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10140 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10141 | if (! ctxt->wellFormed) { |
| 10142 | ctxt->valid = 0; |
| 10143 | return(-1); |
| 10144 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10145 | return(0); |
| 10146 | } |
| 10147 | |
| 10148 | /** |
| 10149 | * xmlParseExtParsedEnt: |
| 10150 | * @ctxt: an XML parser context |
| 10151 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10152 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10153 | * An external general parsed entity is well-formed if it matches the |
| 10154 | * production labeled extParsedEnt. |
| 10155 | * |
| 10156 | * [78] extParsedEnt ::= TextDecl? content |
| 10157 | * |
| 10158 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10159 | * as a result of the parsing. |
| 10160 | */ |
| 10161 | |
| 10162 | int |
| 10163 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10164 | xmlChar start[4]; |
| 10165 | xmlCharEncoding enc; |
| 10166 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10167 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10168 | return(-1); |
| 10169 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10170 | xmlDefaultSAXHandlerInit(); |
| 10171 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10172 | xmlDetectSAX2(ctxt); |
| 10173 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10174 | GROW; |
| 10175 | |
| 10176 | /* |
| 10177 | * SAX: beginning of the document processing. |
| 10178 | */ |
| 10179 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10180 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10181 | |
| 10182 | /* |
| 10183 | * Get the 4 first bytes and decode the charset |
| 10184 | * if enc != XML_CHAR_ENCODING_NONE |
| 10185 | * plug some encoding conversion routines. |
| 10186 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10187 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10188 | start[0] = RAW; |
| 10189 | start[1] = NXT(1); |
| 10190 | start[2] = NXT(2); |
| 10191 | start[3] = NXT(3); |
| 10192 | enc = xmlDetectCharEncoding(start, 4); |
| 10193 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10194 | xmlSwitchEncoding(ctxt, enc); |
| 10195 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10196 | } |
| 10197 | |
| 10198 | |
| 10199 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10200 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10201 | } |
| 10202 | |
| 10203 | /* |
| 10204 | * Check for the XMLDecl in the Prolog. |
| 10205 | */ |
| 10206 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10207 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10208 | |
| 10209 | /* |
| 10210 | * Note that we will switch encoding on the fly. |
| 10211 | */ |
| 10212 | xmlParseXMLDecl(ctxt); |
| 10213 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10214 | /* |
| 10215 | * The XML REC instructs us to stop parsing right here |
| 10216 | */ |
| 10217 | return(-1); |
| 10218 | } |
| 10219 | SKIP_BLANKS; |
| 10220 | } else { |
| 10221 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10222 | } |
| 10223 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10224 | ctxt->sax->startDocument(ctxt->userData); |
| 10225 | |
| 10226 | /* |
| 10227 | * Doing validity checking on chunk doesn't make sense |
| 10228 | */ |
| 10229 | ctxt->instate = XML_PARSER_CONTENT; |
| 10230 | ctxt->validate = 0; |
| 10231 | ctxt->loadsubset = 0; |
| 10232 | ctxt->depth = 0; |
| 10233 | |
| 10234 | xmlParseContent(ctxt); |
| 10235 | |
| 10236 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10237 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10238 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10239 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10240 | } |
| 10241 | |
| 10242 | /* |
| 10243 | * SAX: end of the document processing. |
| 10244 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10245 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10246 | ctxt->sax->endDocument(ctxt->userData); |
| 10247 | |
| 10248 | if (! ctxt->wellFormed) return(-1); |
| 10249 | return(0); |
| 10250 | } |
| 10251 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10252 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10253 | /************************************************************************ |
| 10254 | * * |
| 10255 | * Progressive parsing interfaces * |
| 10256 | * * |
| 10257 | ************************************************************************/ |
| 10258 | |
| 10259 | /** |
| 10260 | * xmlParseLookupSequence: |
| 10261 | * @ctxt: an XML parser context |
| 10262 | * @first: the first char to lookup |
| 10263 | * @next: the next char to lookup or zero |
| 10264 | * @third: the next char to lookup or zero |
| 10265 | * |
| 10266 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10267 | * (first) is available in the input stream. |
| 10268 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10269 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10270 | * parser, do not use liberally. |
| 10271 | * |
| 10272 | * Returns the index to the current parsing point if the full sequence |
| 10273 | * is available, -1 otherwise. |
| 10274 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10275 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10276 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10277 | xmlChar next, xmlChar third) { |
| 10278 | int base, len; |
| 10279 | xmlParserInputPtr in; |
| 10280 | const xmlChar *buf; |
| 10281 | |
| 10282 | in = ctxt->input; |
| 10283 | if (in == NULL) return(-1); |
| 10284 | base = in->cur - in->base; |
| 10285 | if (base < 0) return(-1); |
| 10286 | if (ctxt->checkIndex > base) |
| 10287 | base = ctxt->checkIndex; |
| 10288 | if (in->buf == NULL) { |
| 10289 | buf = in->base; |
| 10290 | len = in->length; |
| 10291 | } else { |
| 10292 | buf = in->buf->buffer->content; |
| 10293 | len = in->buf->buffer->use; |
| 10294 | } |
| 10295 | /* take into account the sequence length */ |
| 10296 | if (third) len -= 2; |
| 10297 | else if (next) len --; |
| 10298 | for (;base < len;base++) { |
| 10299 | if (buf[base] == first) { |
| 10300 | if (third != 0) { |
| 10301 | if ((buf[base + 1] != next) || |
| 10302 | (buf[base + 2] != third)) continue; |
| 10303 | } else if (next != 0) { |
| 10304 | if (buf[base + 1] != next) continue; |
| 10305 | } |
| 10306 | ctxt->checkIndex = 0; |
| 10307 | #ifdef DEBUG_PUSH |
| 10308 | if (next == 0) |
| 10309 | xmlGenericError(xmlGenericErrorContext, |
| 10310 | "PP: lookup '%c' found at %d\n", |
| 10311 | first, base); |
| 10312 | else if (third == 0) |
| 10313 | xmlGenericError(xmlGenericErrorContext, |
| 10314 | "PP: lookup '%c%c' found at %d\n", |
| 10315 | first, next, base); |
| 10316 | else |
| 10317 | xmlGenericError(xmlGenericErrorContext, |
| 10318 | "PP: lookup '%c%c%c' found at %d\n", |
| 10319 | first, next, third, base); |
| 10320 | #endif |
| 10321 | return(base - (in->cur - in->base)); |
| 10322 | } |
| 10323 | } |
| 10324 | ctxt->checkIndex = base; |
| 10325 | #ifdef DEBUG_PUSH |
| 10326 | if (next == 0) |
| 10327 | xmlGenericError(xmlGenericErrorContext, |
| 10328 | "PP: lookup '%c' failed\n", first); |
| 10329 | else if (third == 0) |
| 10330 | xmlGenericError(xmlGenericErrorContext, |
| 10331 | "PP: lookup '%c%c' failed\n", first, next); |
| 10332 | else |
| 10333 | xmlGenericError(xmlGenericErrorContext, |
| 10334 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 10335 | #endif |
| 10336 | return(-1); |
| 10337 | } |
| 10338 | |
| 10339 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10340 | * xmlParseGetLasts: |
| 10341 | * @ctxt: an XML parser context |
| 10342 | * @lastlt: pointer to store the last '<' from the input |
| 10343 | * @lastgt: pointer to store the last '>' from the input |
| 10344 | * |
| 10345 | * Lookup the last < and > in the current chunk |
| 10346 | */ |
| 10347 | static void |
| 10348 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 10349 | const xmlChar **lastgt) { |
| 10350 | const xmlChar *tmp; |
| 10351 | |
| 10352 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 10353 | xmlGenericError(xmlGenericErrorContext, |
| 10354 | "Internal error: xmlParseGetLasts\n"); |
| 10355 | return; |
| 10356 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 10357 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10358 | tmp = ctxt->input->end; |
| 10359 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10360 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10361 | if (tmp < ctxt->input->base) { |
| 10362 | *lastlt = NULL; |
| 10363 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10364 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10365 | *lastlt = tmp; |
| 10366 | tmp++; |
| 10367 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 10368 | if (*tmp == '\'') { |
| 10369 | tmp++; |
| 10370 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 10371 | if (tmp < ctxt->input->end) tmp++; |
| 10372 | } else if (*tmp == '"') { |
| 10373 | tmp++; |
| 10374 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 10375 | if (tmp < ctxt->input->end) tmp++; |
| 10376 | } else |
| 10377 | tmp++; |
| 10378 | } |
| 10379 | if (tmp < ctxt->input->end) |
| 10380 | *lastgt = tmp; |
| 10381 | else { |
| 10382 | tmp = *lastlt; |
| 10383 | tmp--; |
| 10384 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 10385 | if (tmp >= ctxt->input->base) |
| 10386 | *lastgt = tmp; |
| 10387 | else |
| 10388 | *lastgt = NULL; |
| 10389 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10390 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10391 | } else { |
| 10392 | *lastlt = NULL; |
| 10393 | *lastgt = NULL; |
| 10394 | } |
| 10395 | } |
| 10396 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10397 | * xmlCheckCdataPush: |
| 10398 | * @cur: pointer to the bock of characters |
| 10399 | * @len: length of the block in bytes |
| 10400 | * |
| 10401 | * Check that the block of characters is okay as SCdata content [20] |
| 10402 | * |
| 10403 | * Returns the number of bytes to pass if okay, a negative index where an |
| 10404 | * UTF-8 error occured otherwise |
| 10405 | */ |
| 10406 | static int |
| 10407 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 10408 | int ix; |
| 10409 | unsigned char c; |
| 10410 | int codepoint; |
| 10411 | |
| 10412 | if ((utf == NULL) || (len <= 0)) |
| 10413 | return(0); |
| 10414 | |
| 10415 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 10416 | c = utf[ix]; |
| 10417 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 10418 | if (c >= 0x20) |
| 10419 | ix++; |
| 10420 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 10421 | ix++; |
| 10422 | else |
| 10423 | return(-ix); |
| 10424 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 10425 | if (ix + 2 > len) return(ix); |
| 10426 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 10427 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10428 | codepoint = (utf[ix] & 0x1f) << 6; |
| 10429 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10430 | if (!xmlIsCharQ(codepoint)) |
| 10431 | return(-ix); |
| 10432 | ix += 2; |
| 10433 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 10434 | if (ix + 3 > len) return(ix); |
| 10435 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10436 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 10437 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10438 | codepoint = (utf[ix] & 0xf) << 12; |
| 10439 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 10440 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10441 | if (!xmlIsCharQ(codepoint)) |
| 10442 | return(-ix); |
| 10443 | ix += 3; |
| 10444 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 10445 | if (ix + 4 > len) return(ix); |
| 10446 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10447 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 10448 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 10449 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10450 | codepoint = (utf[ix] & 0x7) << 18; |
| 10451 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 10452 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 10453 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10454 | if (!xmlIsCharQ(codepoint)) |
| 10455 | return(-ix); |
| 10456 | ix += 4; |
| 10457 | } else /* unknown encoding */ |
| 10458 | return(-ix); |
| 10459 | } |
| 10460 | return(ix); |
| 10461 | } |
| 10462 | |
| 10463 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10464 | * xmlParseTryOrFinish: |
| 10465 | * @ctxt: an XML parser context |
| 10466 | * @terminate: last chunk indicator |
| 10467 | * |
| 10468 | * Try to progress on parsing |
| 10469 | * |
| 10470 | * Returns zero if no parsing was possible |
| 10471 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10472 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10473 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 10474 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10475 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10476 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10477 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10478 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10479 | if (ctxt->input == NULL) |
| 10480 | return(0); |
| 10481 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10482 | #ifdef DEBUG_PUSH |
| 10483 | switch (ctxt->instate) { |
| 10484 | case XML_PARSER_EOF: |
| 10485 | xmlGenericError(xmlGenericErrorContext, |
| 10486 | "PP: try EOF\n"); break; |
| 10487 | case XML_PARSER_START: |
| 10488 | xmlGenericError(xmlGenericErrorContext, |
| 10489 | "PP: try START\n"); break; |
| 10490 | case XML_PARSER_MISC: |
| 10491 | xmlGenericError(xmlGenericErrorContext, |
| 10492 | "PP: try MISC\n");break; |
| 10493 | case XML_PARSER_COMMENT: |
| 10494 | xmlGenericError(xmlGenericErrorContext, |
| 10495 | "PP: try COMMENT\n");break; |
| 10496 | case XML_PARSER_PROLOG: |
| 10497 | xmlGenericError(xmlGenericErrorContext, |
| 10498 | "PP: try PROLOG\n");break; |
| 10499 | case XML_PARSER_START_TAG: |
| 10500 | xmlGenericError(xmlGenericErrorContext, |
| 10501 | "PP: try START_TAG\n");break; |
| 10502 | case XML_PARSER_CONTENT: |
| 10503 | xmlGenericError(xmlGenericErrorContext, |
| 10504 | "PP: try CONTENT\n");break; |
| 10505 | case XML_PARSER_CDATA_SECTION: |
| 10506 | xmlGenericError(xmlGenericErrorContext, |
| 10507 | "PP: try CDATA_SECTION\n");break; |
| 10508 | case XML_PARSER_END_TAG: |
| 10509 | xmlGenericError(xmlGenericErrorContext, |
| 10510 | "PP: try END_TAG\n");break; |
| 10511 | case XML_PARSER_ENTITY_DECL: |
| 10512 | xmlGenericError(xmlGenericErrorContext, |
| 10513 | "PP: try ENTITY_DECL\n");break; |
| 10514 | case XML_PARSER_ENTITY_VALUE: |
| 10515 | xmlGenericError(xmlGenericErrorContext, |
| 10516 | "PP: try ENTITY_VALUE\n");break; |
| 10517 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 10518 | xmlGenericError(xmlGenericErrorContext, |
| 10519 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 10520 | case XML_PARSER_DTD: |
| 10521 | xmlGenericError(xmlGenericErrorContext, |
| 10522 | "PP: try DTD\n");break; |
| 10523 | case XML_PARSER_EPILOG: |
| 10524 | xmlGenericError(xmlGenericErrorContext, |
| 10525 | "PP: try EPILOG\n");break; |
| 10526 | case XML_PARSER_PI: |
| 10527 | xmlGenericError(xmlGenericErrorContext, |
| 10528 | "PP: try PI\n");break; |
| 10529 | case XML_PARSER_IGNORE: |
| 10530 | xmlGenericError(xmlGenericErrorContext, |
| 10531 | "PP: try IGNORE\n");break; |
| 10532 | } |
| 10533 | #endif |
| 10534 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10535 | if ((ctxt->input != NULL) && |
| 10536 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10537 | xmlSHRINK(ctxt); |
| 10538 | ctxt->checkIndex = 0; |
| 10539 | } |
| 10540 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 10541 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10542 | while (1) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 10543 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10544 | return(0); |
| 10545 | |
| 10546 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10547 | /* |
| 10548 | * Pop-up of finished entities. |
| 10549 | */ |
| 10550 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 10551 | xmlPopInput(ctxt); |
| 10552 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10553 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10554 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10555 | avail = ctxt->input->length - |
| 10556 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 10557 | else { |
| 10558 | /* |
| 10559 | * If we are operating on converted input, try to flush |
| 10560 | * remainng chars to avoid them stalling in the non-converted |
| 10561 | * buffer. |
| 10562 | */ |
| 10563 | if ((ctxt->input->buf->raw != NULL) && |
| 10564 | (ctxt->input->buf->raw->use > 0)) { |
| 10565 | int base = ctxt->input->base - |
| 10566 | ctxt->input->buf->buffer->content; |
| 10567 | int current = ctxt->input->cur - ctxt->input->base; |
| 10568 | |
| 10569 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 10570 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10571 | ctxt->input->cur = ctxt->input->base + current; |
| 10572 | ctxt->input->end = |
| 10573 | &ctxt->input->buf->buffer->content[ |
| 10574 | ctxt->input->buf->buffer->use]; |
| 10575 | } |
| 10576 | avail = ctxt->input->buf->buffer->use - |
| 10577 | (ctxt->input->cur - ctxt->input->base); |
| 10578 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10579 | if (avail < 1) |
| 10580 | goto done; |
| 10581 | switch (ctxt->instate) { |
| 10582 | case XML_PARSER_EOF: |
| 10583 | /* |
| 10584 | * Document parsing is done ! |
| 10585 | */ |
| 10586 | goto done; |
| 10587 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10588 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 10589 | xmlChar start[4]; |
| 10590 | xmlCharEncoding enc; |
| 10591 | |
| 10592 | /* |
| 10593 | * Very first chars read from the document flow. |
| 10594 | */ |
| 10595 | if (avail < 4) |
| 10596 | goto done; |
| 10597 | |
| 10598 | /* |
| 10599 | * Get the 4 first bytes and decode the charset |
| 10600 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10601 | * plug some encoding conversion routines, |
| 10602 | * else xmlSwitchEncoding will set to (default) |
| 10603 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10604 | */ |
| 10605 | start[0] = RAW; |
| 10606 | start[1] = NXT(1); |
| 10607 | start[2] = NXT(2); |
| 10608 | start[3] = NXT(3); |
| 10609 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10610 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10611 | break; |
| 10612 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10613 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 10614 | if (avail < 2) |
| 10615 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10616 | cur = ctxt->input->cur[0]; |
| 10617 | next = ctxt->input->cur[1]; |
| 10618 | if (cur == 0) { |
| 10619 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10620 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10621 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10622 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10623 | ctxt->instate = XML_PARSER_EOF; |
| 10624 | #ifdef DEBUG_PUSH |
| 10625 | xmlGenericError(xmlGenericErrorContext, |
| 10626 | "PP: entering EOF\n"); |
| 10627 | #endif |
| 10628 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10629 | ctxt->sax->endDocument(ctxt->userData); |
| 10630 | goto done; |
| 10631 | } |
| 10632 | if ((cur == '<') && (next == '?')) { |
| 10633 | /* PI or XML decl */ |
| 10634 | if (avail < 5) return(ret); |
| 10635 | if ((!terminate) && |
| 10636 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10637 | return(ret); |
| 10638 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10639 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10640 | &xmlDefaultSAXLocator); |
| 10641 | if ((ctxt->input->cur[2] == 'x') && |
| 10642 | (ctxt->input->cur[3] == 'm') && |
| 10643 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10644 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10645 | ret += 5; |
| 10646 | #ifdef DEBUG_PUSH |
| 10647 | xmlGenericError(xmlGenericErrorContext, |
| 10648 | "PP: Parsing XML Decl\n"); |
| 10649 | #endif |
| 10650 | xmlParseXMLDecl(ctxt); |
| 10651 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10652 | /* |
| 10653 | * The XML REC instructs us to stop parsing right |
| 10654 | * here |
| 10655 | */ |
| 10656 | ctxt->instate = XML_PARSER_EOF; |
| 10657 | return(0); |
| 10658 | } |
| 10659 | ctxt->standalone = ctxt->input->standalone; |
| 10660 | if ((ctxt->encoding == NULL) && |
| 10661 | (ctxt->input->encoding != NULL)) |
| 10662 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 10663 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10664 | (!ctxt->disableSAX)) |
| 10665 | ctxt->sax->startDocument(ctxt->userData); |
| 10666 | ctxt->instate = XML_PARSER_MISC; |
| 10667 | #ifdef DEBUG_PUSH |
| 10668 | xmlGenericError(xmlGenericErrorContext, |
| 10669 | "PP: entering MISC\n"); |
| 10670 | #endif |
| 10671 | } else { |
| 10672 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10673 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10674 | (!ctxt->disableSAX)) |
| 10675 | ctxt->sax->startDocument(ctxt->userData); |
| 10676 | ctxt->instate = XML_PARSER_MISC; |
| 10677 | #ifdef DEBUG_PUSH |
| 10678 | xmlGenericError(xmlGenericErrorContext, |
| 10679 | "PP: entering MISC\n"); |
| 10680 | #endif |
| 10681 | } |
| 10682 | } else { |
| 10683 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10684 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10685 | &xmlDefaultSAXLocator); |
| 10686 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10687 | if (ctxt->version == NULL) { |
| 10688 | xmlErrMemory(ctxt, NULL); |
| 10689 | break; |
| 10690 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10691 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10692 | (!ctxt->disableSAX)) |
| 10693 | ctxt->sax->startDocument(ctxt->userData); |
| 10694 | ctxt->instate = XML_PARSER_MISC; |
| 10695 | #ifdef DEBUG_PUSH |
| 10696 | xmlGenericError(xmlGenericErrorContext, |
| 10697 | "PP: entering MISC\n"); |
| 10698 | #endif |
| 10699 | } |
| 10700 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10701 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10702 | const xmlChar *name; |
| 10703 | const xmlChar *prefix; |
| 10704 | const xmlChar *URI; |
| 10705 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10706 | |
| 10707 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 10708 | goto done; |
| 10709 | cur = ctxt->input->cur[0]; |
| 10710 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10711 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10712 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10713 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10714 | ctxt->sax->endDocument(ctxt->userData); |
| 10715 | goto done; |
| 10716 | } |
| 10717 | if (!terminate) { |
| 10718 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 10719 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10720 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10721 | goto done; |
| 10722 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 10723 | goto done; |
| 10724 | } |
| 10725 | } |
| 10726 | if (ctxt->spaceNr == 0) |
| 10727 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 10728 | else if (*ctxt->space == -2) |
| 10729 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10730 | else |
| 10731 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10732 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10733 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10734 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10735 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10736 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10737 | else |
| 10738 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10739 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10740 | if (name == NULL) { |
| 10741 | spacePop(ctxt); |
| 10742 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10743 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10744 | ctxt->sax->endDocument(ctxt->userData); |
| 10745 | goto done; |
| 10746 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10747 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10748 | /* |
| 10749 | * [ VC: Root Element Type ] |
| 10750 | * The Name in the document type declaration must match |
| 10751 | * the element type of the root element. |
| 10752 | */ |
| 10753 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 10754 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 10755 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10756 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10757 | |
| 10758 | /* |
| 10759 | * Check for an Empty Element. |
| 10760 | */ |
| 10761 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 10762 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10763 | |
| 10764 | if (ctxt->sax2) { |
| 10765 | if ((ctxt->sax != NULL) && |
| 10766 | (ctxt->sax->endElementNs != NULL) && |
| 10767 | (!ctxt->disableSAX)) |
| 10768 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 10769 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 10770 | if (ctxt->nsNr - nsNr > 0) |
| 10771 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10772 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10773 | } else { |
| 10774 | if ((ctxt->sax != NULL) && |
| 10775 | (ctxt->sax->endElement != NULL) && |
| 10776 | (!ctxt->disableSAX)) |
| 10777 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10778 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10779 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10780 | spacePop(ctxt); |
| 10781 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10782 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10783 | } else { |
| 10784 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10785 | } |
| 10786 | break; |
| 10787 | } |
| 10788 | if (RAW == '>') { |
| 10789 | NEXT; |
| 10790 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 10791 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10792 | "Couldn't find end of Start Tag %s\n", |
| 10793 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10794 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10795 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10796 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10797 | if (ctxt->sax2) |
| 10798 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10799 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10800 | else |
| 10801 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10802 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10803 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10804 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10805 | break; |
| 10806 | } |
| 10807 | case XML_PARSER_CONTENT: { |
| 10808 | const xmlChar *test; |
| 10809 | unsigned int cons; |
| 10810 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 10811 | goto done; |
| 10812 | cur = ctxt->input->cur[0]; |
| 10813 | next = ctxt->input->cur[1]; |
| 10814 | |
| 10815 | test = CUR_PTR; |
| 10816 | cons = ctxt->input->consumed; |
| 10817 | if ((cur == '<') && (next == '/')) { |
| 10818 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10819 | break; |
| 10820 | } else if ((cur == '<') && (next == '?')) { |
| 10821 | if ((!terminate) && |
| 10822 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10823 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10824 | xmlParsePI(ctxt); |
| 10825 | } else if ((cur == '<') && (next != '!')) { |
| 10826 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10827 | break; |
| 10828 | } else if ((cur == '<') && (next == '!') && |
| 10829 | (ctxt->input->cur[2] == '-') && |
| 10830 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 10831 | int term; |
| 10832 | |
| 10833 | if (avail < 4) |
| 10834 | goto done; |
| 10835 | ctxt->input->cur += 4; |
| 10836 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 10837 | ctxt->input->cur -= 4; |
| 10838 | if ((!terminate) && (term < 0)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10839 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10840 | xmlParseComment(ctxt); |
| 10841 | ctxt->instate = XML_PARSER_CONTENT; |
| 10842 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 10843 | (ctxt->input->cur[2] == '[') && |
| 10844 | (ctxt->input->cur[3] == 'C') && |
| 10845 | (ctxt->input->cur[4] == 'D') && |
| 10846 | (ctxt->input->cur[5] == 'A') && |
| 10847 | (ctxt->input->cur[6] == 'T') && |
| 10848 | (ctxt->input->cur[7] == 'A') && |
| 10849 | (ctxt->input->cur[8] == '[')) { |
| 10850 | SKIP(9); |
| 10851 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10852 | break; |
| 10853 | } else if ((cur == '<') && (next == '!') && |
| 10854 | (avail < 9)) { |
| 10855 | goto done; |
| 10856 | } else if (cur == '&') { |
| 10857 | if ((!terminate) && |
| 10858 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 10859 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10860 | xmlParseReference(ctxt); |
| 10861 | } else { |
| 10862 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 10863 | /* |
| 10864 | * Goal of the following test is: |
| 10865 | * - minimize calls to the SAX 'character' callback |
| 10866 | * when they are mergeable |
| 10867 | * - handle an problem for isBlank when we only parse |
| 10868 | * a sequence of blank chars and the next one is |
| 10869 | * not available to check against '<' presence. |
| 10870 | * - tries to homogenize the differences in SAX |
| 10871 | * callbacks between the push and pull versions |
| 10872 | * of the parser. |
| 10873 | */ |
| 10874 | if ((ctxt->inputNr == 1) && |
| 10875 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 10876 | if (!terminate) { |
| 10877 | if (ctxt->progressive) { |
| 10878 | if ((lastlt == NULL) || |
| 10879 | (ctxt->input->cur > lastlt)) |
| 10880 | goto done; |
| 10881 | } else if (xmlParseLookupSequence(ctxt, |
| 10882 | '<', 0, 0) < 0) { |
| 10883 | goto done; |
| 10884 | } |
| 10885 | } |
| 10886 | } |
| 10887 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10888 | xmlParseCharData(ctxt, 0); |
| 10889 | } |
| 10890 | /* |
| 10891 | * Pop-up of finished entities. |
| 10892 | */ |
| 10893 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 10894 | xmlPopInput(ctxt); |
| 10895 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10896 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 10897 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10898 | ctxt->instate = XML_PARSER_EOF; |
| 10899 | break; |
| 10900 | } |
| 10901 | break; |
| 10902 | } |
| 10903 | case XML_PARSER_END_TAG: |
| 10904 | if (avail < 2) |
| 10905 | goto done; |
| 10906 | if (!terminate) { |
| 10907 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10908 | /* > can be found unescaped in attribute values */ |
| 10909 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10910 | goto done; |
| 10911 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 10912 | goto done; |
| 10913 | } |
| 10914 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10915 | if (ctxt->sax2) { |
| 10916 | xmlParseEndTag2(ctxt, |
| 10917 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 10918 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10919 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10920 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10921 | } |
| 10922 | #ifdef LIBXML_SAX1_ENABLED |
| 10923 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10924 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10925 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10926 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10927 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10928 | } else { |
| 10929 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10930 | } |
| 10931 | break; |
| 10932 | case XML_PARSER_CDATA_SECTION: { |
| 10933 | /* |
| 10934 | * The Push mode need to have the SAX callback for |
| 10935 | * cdataBlock merge back contiguous callbacks. |
| 10936 | */ |
| 10937 | int base; |
| 10938 | |
| 10939 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 10940 | if (base < 0) { |
| 10941 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10942 | int tmp; |
| 10943 | |
| 10944 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
| 10945 | XML_PARSER_BIG_BUFFER_SIZE); |
| 10946 | if (tmp < 0) { |
| 10947 | tmp = -tmp; |
| 10948 | ctxt->input->cur += tmp; |
| 10949 | goto encoding_error; |
| 10950 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10951 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 10952 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10953 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10954 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10955 | else if (ctxt->sax->characters != NULL) |
| 10956 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10957 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10958 | } |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10959 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10960 | ctxt->checkIndex = 0; |
| 10961 | } |
| 10962 | goto done; |
| 10963 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10964 | int tmp; |
| 10965 | |
| 10966 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 10967 | if ((tmp < 0) || (tmp != base)) { |
| 10968 | tmp = -tmp; |
| 10969 | ctxt->input->cur += tmp; |
| 10970 | goto encoding_error; |
| 10971 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 10972 | if ((ctxt->sax != NULL) && (base == 0) && |
| 10973 | (ctxt->sax->cdataBlock != NULL) && |
| 10974 | (!ctxt->disableSAX)) { |
| 10975 | /* |
| 10976 | * Special case to provide identical behaviour |
| 10977 | * between pull and push parsers on enpty CDATA |
| 10978 | * sections |
| 10979 | */ |
| 10980 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 10981 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 10982 | "<![CDATA[", 9))) |
| 10983 | ctxt->sax->cdataBlock(ctxt->userData, |
| 10984 | BAD_CAST "", 0); |
| 10985 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10986 | (!ctxt->disableSAX)) { |
| 10987 | if (ctxt->sax->cdataBlock != NULL) |
| 10988 | ctxt->sax->cdataBlock(ctxt->userData, |
| 10989 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10990 | else if (ctxt->sax->characters != NULL) |
| 10991 | ctxt->sax->characters(ctxt->userData, |
| 10992 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10993 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 10994 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10995 | ctxt->checkIndex = 0; |
| 10996 | ctxt->instate = XML_PARSER_CONTENT; |
| 10997 | #ifdef DEBUG_PUSH |
| 10998 | xmlGenericError(xmlGenericErrorContext, |
| 10999 | "PP: entering CONTENT\n"); |
| 11000 | #endif |
| 11001 | } |
| 11002 | break; |
| 11003 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11004 | case XML_PARSER_MISC: |
| 11005 | SKIP_BLANKS; |
| 11006 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11007 | avail = ctxt->input->length - |
| 11008 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11009 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11010 | avail = ctxt->input->buf->buffer->use - |
| 11011 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11012 | if (avail < 2) |
| 11013 | goto done; |
| 11014 | cur = ctxt->input->cur[0]; |
| 11015 | next = ctxt->input->cur[1]; |
| 11016 | if ((cur == '<') && (next == '?')) { |
| 11017 | if ((!terminate) && |
| 11018 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11019 | goto done; |
| 11020 | #ifdef DEBUG_PUSH |
| 11021 | xmlGenericError(xmlGenericErrorContext, |
| 11022 | "PP: Parsing PI\n"); |
| 11023 | #endif |
| 11024 | xmlParsePI(ctxt); |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11025 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11026 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11027 | (ctxt->input->cur[2] == '-') && |
| 11028 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11029 | if ((!terminate) && |
| 11030 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11031 | goto done; |
| 11032 | #ifdef DEBUG_PUSH |
| 11033 | xmlGenericError(xmlGenericErrorContext, |
| 11034 | "PP: Parsing Comment\n"); |
| 11035 | #endif |
| 11036 | xmlParseComment(ctxt); |
| 11037 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11038 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11039 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11040 | (ctxt->input->cur[2] == 'D') && |
| 11041 | (ctxt->input->cur[3] == 'O') && |
| 11042 | (ctxt->input->cur[4] == 'C') && |
| 11043 | (ctxt->input->cur[5] == 'T') && |
| 11044 | (ctxt->input->cur[6] == 'Y') && |
| 11045 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11046 | (ctxt->input->cur[8] == 'E')) { |
| 11047 | if ((!terminate) && |
| 11048 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 11049 | goto done; |
| 11050 | #ifdef DEBUG_PUSH |
| 11051 | xmlGenericError(xmlGenericErrorContext, |
| 11052 | "PP: Parsing internal subset\n"); |
| 11053 | #endif |
| 11054 | ctxt->inSubset = 1; |
| 11055 | xmlParseDocTypeDecl(ctxt); |
| 11056 | if (RAW == '[') { |
| 11057 | ctxt->instate = XML_PARSER_DTD; |
| 11058 | #ifdef DEBUG_PUSH |
| 11059 | xmlGenericError(xmlGenericErrorContext, |
| 11060 | "PP: entering DTD\n"); |
| 11061 | #endif |
| 11062 | } else { |
| 11063 | /* |
| 11064 | * Create and update the external subset. |
| 11065 | */ |
| 11066 | ctxt->inSubset = 2; |
| 11067 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11068 | (ctxt->sax->externalSubset != NULL)) |
| 11069 | ctxt->sax->externalSubset(ctxt->userData, |
| 11070 | ctxt->intSubName, ctxt->extSubSystem, |
| 11071 | ctxt->extSubURI); |
| 11072 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11073 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11074 | ctxt->instate = XML_PARSER_PROLOG; |
| 11075 | #ifdef DEBUG_PUSH |
| 11076 | xmlGenericError(xmlGenericErrorContext, |
| 11077 | "PP: entering PROLOG\n"); |
| 11078 | #endif |
| 11079 | } |
| 11080 | } else if ((cur == '<') && (next == '!') && |
| 11081 | (avail < 9)) { |
| 11082 | goto done; |
| 11083 | } else { |
| 11084 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11085 | ctxt->progressive = 1; |
| 11086 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11087 | #ifdef DEBUG_PUSH |
| 11088 | xmlGenericError(xmlGenericErrorContext, |
| 11089 | "PP: entering START_TAG\n"); |
| 11090 | #endif |
| 11091 | } |
| 11092 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11093 | case XML_PARSER_PROLOG: |
| 11094 | SKIP_BLANKS; |
| 11095 | if (ctxt->input->buf == NULL) |
| 11096 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11097 | else |
| 11098 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11099 | if (avail < 2) |
| 11100 | goto done; |
| 11101 | cur = ctxt->input->cur[0]; |
| 11102 | next = ctxt->input->cur[1]; |
| 11103 | if ((cur == '<') && (next == '?')) { |
| 11104 | if ((!terminate) && |
| 11105 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11106 | goto done; |
| 11107 | #ifdef DEBUG_PUSH |
| 11108 | xmlGenericError(xmlGenericErrorContext, |
| 11109 | "PP: Parsing PI\n"); |
| 11110 | #endif |
| 11111 | xmlParsePI(ctxt); |
| 11112 | } else if ((cur == '<') && (next == '!') && |
| 11113 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11114 | if ((!terminate) && |
| 11115 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11116 | goto done; |
| 11117 | #ifdef DEBUG_PUSH |
| 11118 | xmlGenericError(xmlGenericErrorContext, |
| 11119 | "PP: Parsing Comment\n"); |
| 11120 | #endif |
| 11121 | xmlParseComment(ctxt); |
| 11122 | ctxt->instate = XML_PARSER_PROLOG; |
| 11123 | } else if ((cur == '<') && (next == '!') && |
| 11124 | (avail < 4)) { |
| 11125 | goto done; |
| 11126 | } else { |
| 11127 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11128 | if (ctxt->progressive == 0) |
| 11129 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11130 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11131 | #ifdef DEBUG_PUSH |
| 11132 | xmlGenericError(xmlGenericErrorContext, |
| 11133 | "PP: entering START_TAG\n"); |
| 11134 | #endif |
| 11135 | } |
| 11136 | break; |
| 11137 | case XML_PARSER_EPILOG: |
| 11138 | SKIP_BLANKS; |
| 11139 | if (ctxt->input->buf == NULL) |
| 11140 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11141 | else |
| 11142 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11143 | if (avail < 2) |
| 11144 | goto done; |
| 11145 | cur = ctxt->input->cur[0]; |
| 11146 | next = ctxt->input->cur[1]; |
| 11147 | if ((cur == '<') && (next == '?')) { |
| 11148 | if ((!terminate) && |
| 11149 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11150 | goto done; |
| 11151 | #ifdef DEBUG_PUSH |
| 11152 | xmlGenericError(xmlGenericErrorContext, |
| 11153 | "PP: Parsing PI\n"); |
| 11154 | #endif |
| 11155 | xmlParsePI(ctxt); |
| 11156 | ctxt->instate = XML_PARSER_EPILOG; |
| 11157 | } else if ((cur == '<') && (next == '!') && |
| 11158 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11159 | if ((!terminate) && |
| 11160 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11161 | goto done; |
| 11162 | #ifdef DEBUG_PUSH |
| 11163 | xmlGenericError(xmlGenericErrorContext, |
| 11164 | "PP: Parsing Comment\n"); |
| 11165 | #endif |
| 11166 | xmlParseComment(ctxt); |
| 11167 | ctxt->instate = XML_PARSER_EPILOG; |
| 11168 | } else if ((cur == '<') && (next == '!') && |
| 11169 | (avail < 4)) { |
| 11170 | goto done; |
| 11171 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11172 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11173 | ctxt->instate = XML_PARSER_EOF; |
| 11174 | #ifdef DEBUG_PUSH |
| 11175 | xmlGenericError(xmlGenericErrorContext, |
| 11176 | "PP: entering EOF\n"); |
| 11177 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11178 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11179 | ctxt->sax->endDocument(ctxt->userData); |
| 11180 | goto done; |
| 11181 | } |
| 11182 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11183 | case XML_PARSER_DTD: { |
| 11184 | /* |
| 11185 | * Sorry but progressive parsing of the internal subset |
| 11186 | * is not expected to be supported. We first check that |
| 11187 | * the full content of the internal subset is available and |
| 11188 | * the parsing is launched only at that point. |
| 11189 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11190 | * section and not in a ']]>' sequence which are conditional |
| 11191 | * sections (whoever argued to keep that crap in XML deserve |
| 11192 | * a place in hell !). |
| 11193 | */ |
| 11194 | int base, i; |
| 11195 | xmlChar *buf; |
| 11196 | xmlChar quote = 0; |
| 11197 | |
| 11198 | base = ctxt->input->cur - ctxt->input->base; |
| 11199 | if (base < 0) return(0); |
| 11200 | if (ctxt->checkIndex > base) |
| 11201 | base = ctxt->checkIndex; |
| 11202 | buf = ctxt->input->buf->buffer->content; |
| 11203 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 11204 | base++) { |
| 11205 | if (quote != 0) { |
| 11206 | if (buf[base] == quote) |
| 11207 | quote = 0; |
| 11208 | continue; |
| 11209 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11210 | if ((quote == 0) && (buf[base] == '<')) { |
| 11211 | int found = 0; |
| 11212 | /* special handling of comments */ |
| 11213 | if (((unsigned int) base + 4 < |
| 11214 | ctxt->input->buf->buffer->use) && |
| 11215 | (buf[base + 1] == '!') && |
| 11216 | (buf[base + 2] == '-') && |
| 11217 | (buf[base + 3] == '-')) { |
| 11218 | for (;(unsigned int) base + 3 < |
| 11219 | ctxt->input->buf->buffer->use; base++) { |
| 11220 | if ((buf[base] == '-') && |
| 11221 | (buf[base + 1] == '-') && |
| 11222 | (buf[base + 2] == '>')) { |
| 11223 | found = 1; |
| 11224 | base += 2; |
| 11225 | break; |
| 11226 | } |
| 11227 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11228 | if (!found) { |
| 11229 | #if 0 |
| 11230 | fprintf(stderr, "unfinished comment\n"); |
| 11231 | #endif |
| 11232 | break; /* for */ |
| 11233 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11234 | continue; |
| 11235 | } |
| 11236 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11237 | if (buf[base] == '"') { |
| 11238 | quote = '"'; |
| 11239 | continue; |
| 11240 | } |
| 11241 | if (buf[base] == '\'') { |
| 11242 | quote = '\''; |
| 11243 | continue; |
| 11244 | } |
| 11245 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11246 | #if 0 |
| 11247 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 11248 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 11249 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11250 | if ((unsigned int) base +1 >= |
| 11251 | ctxt->input->buf->buffer->use) |
| 11252 | break; |
| 11253 | if (buf[base + 1] == ']') { |
| 11254 | /* conditional crap, skip both ']' ! */ |
| 11255 | base++; |
| 11256 | continue; |
| 11257 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11258 | for (i = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11259 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 11260 | i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11261 | if (buf[base + i] == '>') { |
| 11262 | #if 0 |
| 11263 | fprintf(stderr, "found\n"); |
| 11264 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11265 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11266 | } |
| 11267 | if (!IS_BLANK_CH(buf[base + i])) { |
| 11268 | #if 0 |
| 11269 | fprintf(stderr, "not found\n"); |
| 11270 | #endif |
| 11271 | goto not_end_of_int_subset; |
| 11272 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11273 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11274 | #if 0 |
| 11275 | fprintf(stderr, "end of stream\n"); |
| 11276 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11277 | break; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11278 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11279 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11280 | not_end_of_int_subset: |
| 11281 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11282 | } |
| 11283 | /* |
| 11284 | * We didn't found the end of the Internal subset |
| 11285 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11286 | #ifdef DEBUG_PUSH |
| 11287 | if (next == 0) |
| 11288 | xmlGenericError(xmlGenericErrorContext, |
| 11289 | "PP: lookup of int subset end filed\n"); |
| 11290 | #endif |
| 11291 | goto done; |
| 11292 | |
| 11293 | found_end_int_subset: |
| 11294 | xmlParseInternalSubset(ctxt); |
| 11295 | ctxt->inSubset = 2; |
| 11296 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11297 | (ctxt->sax->externalSubset != NULL)) |
| 11298 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 11299 | ctxt->extSubSystem, ctxt->extSubURI); |
| 11300 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11301 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11302 | ctxt->instate = XML_PARSER_PROLOG; |
| 11303 | ctxt->checkIndex = 0; |
| 11304 | #ifdef DEBUG_PUSH |
| 11305 | xmlGenericError(xmlGenericErrorContext, |
| 11306 | "PP: entering PROLOG\n"); |
| 11307 | #endif |
| 11308 | break; |
| 11309 | } |
| 11310 | case XML_PARSER_COMMENT: |
| 11311 | xmlGenericError(xmlGenericErrorContext, |
| 11312 | "PP: internal error, state == COMMENT\n"); |
| 11313 | ctxt->instate = XML_PARSER_CONTENT; |
| 11314 | #ifdef DEBUG_PUSH |
| 11315 | xmlGenericError(xmlGenericErrorContext, |
| 11316 | "PP: entering CONTENT\n"); |
| 11317 | #endif |
| 11318 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11319 | case XML_PARSER_IGNORE: |
| 11320 | xmlGenericError(xmlGenericErrorContext, |
| 11321 | "PP: internal error, state == IGNORE"); |
| 11322 | ctxt->instate = XML_PARSER_DTD; |
| 11323 | #ifdef DEBUG_PUSH |
| 11324 | xmlGenericError(xmlGenericErrorContext, |
| 11325 | "PP: entering DTD\n"); |
| 11326 | #endif |
| 11327 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11328 | case XML_PARSER_PI: |
| 11329 | xmlGenericError(xmlGenericErrorContext, |
| 11330 | "PP: internal error, state == PI\n"); |
| 11331 | ctxt->instate = XML_PARSER_CONTENT; |
| 11332 | #ifdef DEBUG_PUSH |
| 11333 | xmlGenericError(xmlGenericErrorContext, |
| 11334 | "PP: entering CONTENT\n"); |
| 11335 | #endif |
| 11336 | break; |
| 11337 | case XML_PARSER_ENTITY_DECL: |
| 11338 | xmlGenericError(xmlGenericErrorContext, |
| 11339 | "PP: internal error, state == ENTITY_DECL\n"); |
| 11340 | ctxt->instate = XML_PARSER_DTD; |
| 11341 | #ifdef DEBUG_PUSH |
| 11342 | xmlGenericError(xmlGenericErrorContext, |
| 11343 | "PP: entering DTD\n"); |
| 11344 | #endif |
| 11345 | break; |
| 11346 | case XML_PARSER_ENTITY_VALUE: |
| 11347 | xmlGenericError(xmlGenericErrorContext, |
| 11348 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 11349 | ctxt->instate = XML_PARSER_CONTENT; |
| 11350 | #ifdef DEBUG_PUSH |
| 11351 | xmlGenericError(xmlGenericErrorContext, |
| 11352 | "PP: entering DTD\n"); |
| 11353 | #endif |
| 11354 | break; |
| 11355 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11356 | xmlGenericError(xmlGenericErrorContext, |
| 11357 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 11358 | ctxt->instate = XML_PARSER_START_TAG; |
| 11359 | #ifdef DEBUG_PUSH |
| 11360 | xmlGenericError(xmlGenericErrorContext, |
| 11361 | "PP: entering START_TAG\n"); |
| 11362 | #endif |
| 11363 | break; |
| 11364 | case XML_PARSER_SYSTEM_LITERAL: |
| 11365 | xmlGenericError(xmlGenericErrorContext, |
| 11366 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 11367 | ctxt->instate = XML_PARSER_START_TAG; |
| 11368 | #ifdef DEBUG_PUSH |
| 11369 | xmlGenericError(xmlGenericErrorContext, |
| 11370 | "PP: entering START_TAG\n"); |
| 11371 | #endif |
| 11372 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 11373 | case XML_PARSER_PUBLIC_LITERAL: |
| 11374 | xmlGenericError(xmlGenericErrorContext, |
| 11375 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 11376 | ctxt->instate = XML_PARSER_START_TAG; |
| 11377 | #ifdef DEBUG_PUSH |
| 11378 | xmlGenericError(xmlGenericErrorContext, |
| 11379 | "PP: entering START_TAG\n"); |
| 11380 | #endif |
| 11381 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11382 | } |
| 11383 | } |
| 11384 | done: |
| 11385 | #ifdef DEBUG_PUSH |
| 11386 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 11387 | #endif |
| 11388 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11389 | encoding_error: |
| 11390 | { |
| 11391 | char buffer[150]; |
| 11392 | |
| 11393 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 11394 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 11395 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 11396 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 11397 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 11398 | BAD_CAST buffer, NULL); |
| 11399 | } |
| 11400 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11401 | } |
| 11402 | |
| 11403 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11404 | * xmlParseChunk: |
| 11405 | * @ctxt: an XML parser context |
| 11406 | * @chunk: an char array |
| 11407 | * @size: the size in byte of the chunk |
| 11408 | * @terminate: last chunk indicator |
| 11409 | * |
| 11410 | * Parse a Chunk of memory |
| 11411 | * |
| 11412 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 11413 | */ |
| 11414 | int |
| 11415 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 11416 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11417 | int end_in_lf = 0; |
| 11418 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11419 | if (ctxt == NULL) |
| 11420 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11421 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11422 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 11423 | if (ctxt->instate == XML_PARSER_START) |
| 11424 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11425 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 11426 | (chunk[size - 1] == '\r')) { |
| 11427 | end_in_lf = 1; |
| 11428 | size--; |
| 11429 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11430 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 11431 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 11432 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11433 | int cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11434 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11435 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11436 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 11437 | if (res < 0) { |
| 11438 | ctxt->errNo = XML_PARSER_EOF; |
| 11439 | ctxt->disableSAX = 1; |
| 11440 | return (XML_PARSER_EOF); |
| 11441 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11442 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11443 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11444 | ctxt->input->end = |
| 11445 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11446 | #ifdef DEBUG_PUSH |
| 11447 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11448 | #endif |
| 11449 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11450 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 11451 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 11452 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 11453 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 11454 | (in->raw != NULL)) { |
| 11455 | int nbchars; |
| 11456 | |
| 11457 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 11458 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11459 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11460 | xmlGenericError(xmlGenericErrorContext, |
| 11461 | "xmlParseChunk: encoder error\n"); |
| 11462 | return(XML_ERR_INVALID_ENCODING); |
| 11463 | } |
| 11464 | } |
| 11465 | } |
| 11466 | } |
| 11467 | xmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11468 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 11469 | (ctxt->input->buf != NULL)) { |
| 11470 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
| 11471 | } |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11472 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11473 | return(ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11474 | if (terminate) { |
| 11475 | /* |
| 11476 | * Check for termination |
| 11477 | */ |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11478 | int avail = 0; |
| 11479 | |
| 11480 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11481 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11482 | avail = ctxt->input->length - |
| 11483 | (ctxt->input->cur - ctxt->input->base); |
| 11484 | else |
| 11485 | avail = ctxt->input->buf->buffer->use - |
| 11486 | (ctxt->input->cur - ctxt->input->base); |
| 11487 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11488 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11489 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 11490 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11491 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11492 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11493 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11494 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11495 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11496 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11497 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11498 | ctxt->sax->endDocument(ctxt->userData); |
| 11499 | } |
| 11500 | ctxt->instate = XML_PARSER_EOF; |
| 11501 | } |
| 11502 | return((xmlParserErrors) ctxt->errNo); |
| 11503 | } |
| 11504 | |
| 11505 | /************************************************************************ |
| 11506 | * * |
| 11507 | * I/O front end functions to the parser * |
| 11508 | * * |
| 11509 | ************************************************************************/ |
| 11510 | |
| 11511 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11512 | * xmlCreatePushParserCtxt: |
| 11513 | * @sax: a SAX handler |
| 11514 | * @user_data: The user data returned on SAX callbacks |
| 11515 | * @chunk: a pointer to an array of chars |
| 11516 | * @size: number of chars in the array |
| 11517 | * @filename: an optional file name or URI |
| 11518 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11519 | * Create a parser context for using the XML parser in push mode. |
| 11520 | * If @buffer and @size are non-NULL, the data is used to detect |
| 11521 | * the encoding. The remaining characters will be parsed so they |
| 11522 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11523 | * To allow content encoding detection, @size should be >= 4 |
| 11524 | * The value of @filename is used for fetching external entities |
| 11525 | * and error/warning reports. |
| 11526 | * |
| 11527 | * Returns the new parser context or NULL |
| 11528 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11529 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11530 | xmlParserCtxtPtr |
| 11531 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 11532 | const char *chunk, int size, const char *filename) { |
| 11533 | xmlParserCtxtPtr ctxt; |
| 11534 | xmlParserInputPtr inputStream; |
| 11535 | xmlParserInputBufferPtr buf; |
| 11536 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 11537 | |
| 11538 | /* |
| 11539 | * plug some encoding conversion routines |
| 11540 | */ |
| 11541 | if ((chunk != NULL) && (size >= 4)) |
| 11542 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 11543 | |
| 11544 | buf = xmlAllocParserInputBuffer(enc); |
| 11545 | if (buf == NULL) return(NULL); |
| 11546 | |
| 11547 | ctxt = xmlNewParserCtxt(); |
| 11548 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11549 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11550 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11551 | return(NULL); |
| 11552 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11553 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11554 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 11555 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11556 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11557 | xmlFreeParserInputBuffer(buf); |
| 11558 | xmlFreeParserCtxt(ctxt); |
| 11559 | return(NULL); |
| 11560 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11561 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11562 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11563 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11564 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11565 | xmlFree(ctxt->sax); |
| 11566 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 11567 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11568 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11569 | xmlFreeParserInputBuffer(buf); |
| 11570 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11571 | return(NULL); |
| 11572 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 11573 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 11574 | if (sax->initialized == XML_SAX2_MAGIC) |
| 11575 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 11576 | else |
| 11577 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11578 | if (user_data != NULL) |
| 11579 | ctxt->userData = user_data; |
| 11580 | } |
| 11581 | if (filename == NULL) { |
| 11582 | ctxt->directory = NULL; |
| 11583 | } else { |
| 11584 | ctxt->directory = xmlParserGetDirectory(filename); |
| 11585 | } |
| 11586 | |
| 11587 | inputStream = xmlNewInputStream(ctxt); |
| 11588 | if (inputStream == NULL) { |
| 11589 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11590 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11591 | return(NULL); |
| 11592 | } |
| 11593 | |
| 11594 | if (filename == NULL) |
| 11595 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11596 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 11597 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11598 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11599 | if (inputStream->filename == NULL) { |
| 11600 | xmlFreeParserCtxt(ctxt); |
| 11601 | xmlFreeParserInputBuffer(buf); |
| 11602 | return(NULL); |
| 11603 | } |
| 11604 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11605 | inputStream->buf = buf; |
| 11606 | inputStream->base = inputStream->buf->buffer->content; |
| 11607 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11608 | inputStream->end = |
| 11609 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11610 | |
| 11611 | inputPush(ctxt, inputStream); |
| 11612 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11613 | /* |
| 11614 | * If the caller didn't provide an initial 'chunk' for determining |
| 11615 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 11616 | * that it can be automatically determined later |
| 11617 | */ |
| 11618 | if ((size == 0) || (chunk == NULL)) { |
| 11619 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 11620 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11621 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11622 | int cur = ctxt->input->cur - ctxt->input->base; |
| 11623 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11624 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11625 | |
| 11626 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11627 | ctxt->input->cur = ctxt->input->base + cur; |
| 11628 | ctxt->input->end = |
| 11629 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11630 | #ifdef DEBUG_PUSH |
| 11631 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11632 | #endif |
| 11633 | } |
| 11634 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11635 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11636 | xmlSwitchEncoding(ctxt, enc); |
| 11637 | } |
| 11638 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11639 | return(ctxt); |
| 11640 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 11641 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11642 | |
| 11643 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 11644 | * xmlStopParser: |
| 11645 | * @ctxt: an XML parser context |
| 11646 | * |
| 11647 | * Blocks further parser processing |
| 11648 | */ |
| 11649 | void |
| 11650 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 11651 | if (ctxt == NULL) |
| 11652 | return; |
| 11653 | ctxt->instate = XML_PARSER_EOF; |
| 11654 | ctxt->disableSAX = 1; |
| 11655 | if (ctxt->input != NULL) { |
| 11656 | ctxt->input->cur = BAD_CAST""; |
| 11657 | ctxt->input->base = ctxt->input->cur; |
| 11658 | } |
| 11659 | } |
| 11660 | |
| 11661 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11662 | * xmlCreateIOParserCtxt: |
| 11663 | * @sax: a SAX handler |
| 11664 | * @user_data: The user data returned on SAX callbacks |
| 11665 | * @ioread: an I/O read function |
| 11666 | * @ioclose: an I/O close function |
| 11667 | * @ioctx: an I/O handler |
| 11668 | * @enc: the charset encoding if known |
| 11669 | * |
| 11670 | * Create a parser context for using the XML parser with an existing |
| 11671 | * I/O stream |
| 11672 | * |
| 11673 | * Returns the new parser context or NULL |
| 11674 | */ |
| 11675 | xmlParserCtxtPtr |
| 11676 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 11677 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 11678 | void *ioctx, xmlCharEncoding enc) { |
| 11679 | xmlParserCtxtPtr ctxt; |
| 11680 | xmlParserInputPtr inputStream; |
| 11681 | xmlParserInputBufferPtr buf; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 11682 | |
| 11683 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11684 | |
| 11685 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
| 11686 | if (buf == NULL) return(NULL); |
| 11687 | |
| 11688 | ctxt = xmlNewParserCtxt(); |
| 11689 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 11690 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11691 | return(NULL); |
| 11692 | } |
| 11693 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11694 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11695 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11696 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11697 | xmlFree(ctxt->sax); |
| 11698 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 11699 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11700 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 11701 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11702 | return(NULL); |
| 11703 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 11704 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 11705 | if (sax->initialized == XML_SAX2_MAGIC) |
| 11706 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 11707 | else |
| 11708 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11709 | if (user_data != NULL) |
| 11710 | ctxt->userData = user_data; |
| 11711 | } |
| 11712 | |
| 11713 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 11714 | if (inputStream == NULL) { |
| 11715 | xmlFreeParserCtxt(ctxt); |
| 11716 | return(NULL); |
| 11717 | } |
| 11718 | inputPush(ctxt, inputStream); |
| 11719 | |
| 11720 | return(ctxt); |
| 11721 | } |
| 11722 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11723 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11724 | /************************************************************************ |
| 11725 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11726 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11727 | * * |
| 11728 | ************************************************************************/ |
| 11729 | |
| 11730 | /** |
| 11731 | * xmlIOParseDTD: |
| 11732 | * @sax: the SAX handler block or NULL |
| 11733 | * @input: an Input Buffer |
| 11734 | * @enc: the charset encoding if known |
| 11735 | * |
| 11736 | * Load and parse a DTD |
| 11737 | * |
| 11738 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11739 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11740 | */ |
| 11741 | |
| 11742 | xmlDtdPtr |
| 11743 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 11744 | xmlCharEncoding enc) { |
| 11745 | xmlDtdPtr ret = NULL; |
| 11746 | xmlParserCtxtPtr ctxt; |
| 11747 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11748 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11749 | |
| 11750 | if (input == NULL) |
| 11751 | return(NULL); |
| 11752 | |
| 11753 | ctxt = xmlNewParserCtxt(); |
| 11754 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11755 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11756 | return(NULL); |
| 11757 | } |
| 11758 | |
| 11759 | /* |
| 11760 | * Set-up the SAX context |
| 11761 | */ |
| 11762 | if (sax != NULL) { |
| 11763 | if (ctxt->sax != NULL) |
| 11764 | xmlFree(ctxt->sax); |
| 11765 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 11766 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11767 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11768 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11769 | |
| 11770 | /* |
| 11771 | * generate a parser input from the I/O handler |
| 11772 | */ |
| 11773 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 11774 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11775 | if (pinput == NULL) { |
| 11776 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11777 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11778 | xmlFreeParserCtxt(ctxt); |
| 11779 | return(NULL); |
| 11780 | } |
| 11781 | |
| 11782 | /* |
| 11783 | * plug some encoding conversion routines here. |
| 11784 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 11785 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 11786 | if (sax != NULL) ctxt->sax = NULL; |
| 11787 | xmlFreeParserCtxt(ctxt); |
| 11788 | return(NULL); |
| 11789 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 11790 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11791 | xmlSwitchEncoding(ctxt, enc); |
| 11792 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11793 | |
| 11794 | pinput->filename = NULL; |
| 11795 | pinput->line = 1; |
| 11796 | pinput->col = 1; |
| 11797 | pinput->base = ctxt->input->cur; |
| 11798 | pinput->cur = ctxt->input->cur; |
| 11799 | pinput->free = NULL; |
| 11800 | |
| 11801 | /* |
| 11802 | * let's parse that entity knowing it's an external subset. |
| 11803 | */ |
| 11804 | ctxt->inSubset = 2; |
| 11805 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 11806 | if (ctxt->myDoc == NULL) { |
| 11807 | xmlErrMemory(ctxt, "New Doc failed"); |
| 11808 | return(NULL); |
| 11809 | } |
| 11810 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11811 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 11812 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11813 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 11814 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 11815 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11816 | /* |
| 11817 | * Get the 4 first bytes and decode the charset |
| 11818 | * if enc != XML_CHAR_ENCODING_NONE |
| 11819 | * plug some encoding conversion routines. |
| 11820 | */ |
| 11821 | start[0] = RAW; |
| 11822 | start[1] = NXT(1); |
| 11823 | start[2] = NXT(2); |
| 11824 | start[3] = NXT(3); |
| 11825 | enc = xmlDetectCharEncoding(start, 4); |
| 11826 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11827 | xmlSwitchEncoding(ctxt, enc); |
| 11828 | } |
| 11829 | } |
| 11830 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11831 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 11832 | |
| 11833 | if (ctxt->myDoc != NULL) { |
| 11834 | if (ctxt->wellFormed) { |
| 11835 | ret = ctxt->myDoc->extSubset; |
| 11836 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 11837 | if (ret != NULL) { |
| 11838 | xmlNodePtr tmp; |
| 11839 | |
| 11840 | ret->doc = NULL; |
| 11841 | tmp = ret->children; |
| 11842 | while (tmp != NULL) { |
| 11843 | tmp->doc = NULL; |
| 11844 | tmp = tmp->next; |
| 11845 | } |
| 11846 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11847 | } else { |
| 11848 | ret = NULL; |
| 11849 | } |
| 11850 | xmlFreeDoc(ctxt->myDoc); |
| 11851 | ctxt->myDoc = NULL; |
| 11852 | } |
| 11853 | if (sax != NULL) ctxt->sax = NULL; |
| 11854 | xmlFreeParserCtxt(ctxt); |
| 11855 | |
| 11856 | return(ret); |
| 11857 | } |
| 11858 | |
| 11859 | /** |
| 11860 | * xmlSAXParseDTD: |
| 11861 | * @sax: the SAX handler block |
| 11862 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 11863 | * @SystemID: a NAME* containing the URL to the DTD |
| 11864 | * |
| 11865 | * Load and parse an external subset. |
| 11866 | * |
| 11867 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 11868 | */ |
| 11869 | |
| 11870 | xmlDtdPtr |
| 11871 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 11872 | const xmlChar *SystemID) { |
| 11873 | xmlDtdPtr ret = NULL; |
| 11874 | xmlParserCtxtPtr ctxt; |
| 11875 | xmlParserInputPtr input = NULL; |
| 11876 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11877 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11878 | |
| 11879 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 11880 | |
| 11881 | ctxt = xmlNewParserCtxt(); |
| 11882 | if (ctxt == NULL) { |
| 11883 | return(NULL); |
| 11884 | } |
| 11885 | |
| 11886 | /* |
| 11887 | * Set-up the SAX context |
| 11888 | */ |
| 11889 | if (sax != NULL) { |
| 11890 | if (ctxt->sax != NULL) |
| 11891 | xmlFree(ctxt->sax); |
| 11892 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 11893 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11894 | } |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11895 | |
| 11896 | /* |
| 11897 | * Canonicalise the system ID |
| 11898 | */ |
| 11899 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 11900 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11901 | xmlFreeParserCtxt(ctxt); |
| 11902 | return(NULL); |
| 11903 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11904 | |
| 11905 | /* |
| 11906 | * Ask the Entity resolver to load the damn thing |
| 11907 | */ |
| 11908 | |
| 11909 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 11910 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 11911 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11912 | if (input == NULL) { |
| 11913 | if (sax != NULL) ctxt->sax = NULL; |
| 11914 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 11915 | if (systemIdCanonic != NULL) |
| 11916 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11917 | return(NULL); |
| 11918 | } |
| 11919 | |
| 11920 | /* |
| 11921 | * plug some encoding conversion routines here. |
| 11922 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 11923 | if (xmlPushInput(ctxt, input) < 0) { |
| 11924 | if (sax != NULL) ctxt->sax = NULL; |
| 11925 | xmlFreeParserCtxt(ctxt); |
| 11926 | if (systemIdCanonic != NULL) |
| 11927 | xmlFree(systemIdCanonic); |
| 11928 | return(NULL); |
| 11929 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 11930 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 11931 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 11932 | xmlSwitchEncoding(ctxt, enc); |
| 11933 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11934 | |
| 11935 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11936 | input->filename = (char *) systemIdCanonic; |
| 11937 | else |
| 11938 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11939 | input->line = 1; |
| 11940 | input->col = 1; |
| 11941 | input->base = ctxt->input->cur; |
| 11942 | input->cur = ctxt->input->cur; |
| 11943 | input->free = NULL; |
| 11944 | |
| 11945 | /* |
| 11946 | * let's parse that entity knowing it's an external subset. |
| 11947 | */ |
| 11948 | ctxt->inSubset = 2; |
| 11949 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 11950 | if (ctxt->myDoc == NULL) { |
| 11951 | xmlErrMemory(ctxt, "New Doc failed"); |
| 11952 | if (sax != NULL) ctxt->sax = NULL; |
| 11953 | xmlFreeParserCtxt(ctxt); |
| 11954 | return(NULL); |
| 11955 | } |
| 11956 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11957 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 11958 | ExternalID, SystemID); |
| 11959 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 11960 | |
| 11961 | if (ctxt->myDoc != NULL) { |
| 11962 | if (ctxt->wellFormed) { |
| 11963 | ret = ctxt->myDoc->extSubset; |
| 11964 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 11965 | if (ret != NULL) { |
| 11966 | xmlNodePtr tmp; |
| 11967 | |
| 11968 | ret->doc = NULL; |
| 11969 | tmp = ret->children; |
| 11970 | while (tmp != NULL) { |
| 11971 | tmp->doc = NULL; |
| 11972 | tmp = tmp->next; |
| 11973 | } |
| 11974 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11975 | } else { |
| 11976 | ret = NULL; |
| 11977 | } |
| 11978 | xmlFreeDoc(ctxt->myDoc); |
| 11979 | ctxt->myDoc = NULL; |
| 11980 | } |
| 11981 | if (sax != NULL) ctxt->sax = NULL; |
| 11982 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 11983 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11984 | return(ret); |
| 11985 | } |
| 11986 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11987 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11988 | /** |
| 11989 | * xmlParseDTD: |
| 11990 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 11991 | * @SystemID: a NAME* containing the URL to the DTD |
| 11992 | * |
| 11993 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 11994 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11995 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 11996 | */ |
| 11997 | |
| 11998 | xmlDtdPtr |
| 11999 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 12000 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 12001 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12002 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12003 | |
| 12004 | /************************************************************************ |
| 12005 | * * |
| 12006 | * Front ends when parsing an Entity * |
| 12007 | * * |
| 12008 | ************************************************************************/ |
| 12009 | |
| 12010 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12011 | * xmlParseCtxtExternalEntity: |
| 12012 | * @ctx: the existing parsing context |
| 12013 | * @URL: the URL for the entity to load |
| 12014 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12015 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12016 | * |
| 12017 | * Parse an external general entity within an existing parsing context |
| 12018 | * An external general parsed entity is well-formed if it matches the |
| 12019 | * production labeled extParsedEnt. |
| 12020 | * |
| 12021 | * [78] extParsedEnt ::= TextDecl? content |
| 12022 | * |
| 12023 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12024 | * the parser error code otherwise |
| 12025 | */ |
| 12026 | |
| 12027 | int |
| 12028 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12029 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12030 | xmlParserCtxtPtr ctxt; |
| 12031 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12032 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12033 | xmlSAXHandlerPtr oldsax = NULL; |
| 12034 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12035 | xmlChar start[4]; |
| 12036 | xmlCharEncoding enc; |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12037 | xmlParserInputPtr inputStream; |
| 12038 | char *directory = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12039 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12040 | if (ctx == NULL) return(-1); |
| 12041 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12042 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12043 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12044 | return(XML_ERR_ENTITY_LOOP); |
| 12045 | } |
| 12046 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12047 | if (lst != NULL) |
| 12048 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12049 | if ((URL == NULL) && (ID == NULL)) |
| 12050 | return(-1); |
| 12051 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12052 | return(-1); |
| 12053 | |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12054 | ctxt = xmlNewParserCtxt(); |
| 12055 | if (ctxt == NULL) { |
| 12056 | return(-1); |
| 12057 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12058 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12059 | ctxt->userData = ctxt; |
Daniel Veillard | e2830f1 | 2003-01-08 17:47:49 +0000 | [diff] [blame] | 12060 | ctxt->_private = ctx->_private; |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12061 | |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12062 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 12063 | if (inputStream == NULL) { |
| 12064 | xmlFreeParserCtxt(ctxt); |
| 12065 | return(-1); |
| 12066 | } |
| 12067 | |
| 12068 | inputPush(ctxt, inputStream); |
| 12069 | |
| 12070 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 12071 | directory = xmlParserGetDirectory((char *)URL); |
| 12072 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 12073 | ctxt->directory = directory; |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12074 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12075 | oldsax = ctxt->sax; |
| 12076 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12077 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12078 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12079 | if (newDoc == NULL) { |
| 12080 | xmlFreeParserCtxt(ctxt); |
| 12081 | return(-1); |
| 12082 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12083 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12084 | if (ctx->myDoc->dict) { |
| 12085 | newDoc->dict = ctx->myDoc->dict; |
| 12086 | xmlDictReference(newDoc->dict); |
| 12087 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12088 | if (ctx->myDoc != NULL) { |
| 12089 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12090 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12091 | } |
| 12092 | if (ctx->myDoc->URL != NULL) { |
| 12093 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12094 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12095 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12096 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12097 | ctxt->sax = oldsax; |
| 12098 | xmlFreeParserCtxt(ctxt); |
| 12099 | newDoc->intSubset = NULL; |
| 12100 | newDoc->extSubset = NULL; |
| 12101 | xmlFreeDoc(newDoc); |
| 12102 | return(-1); |
| 12103 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12104 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12105 | nodePush(ctxt, newDoc->children); |
| 12106 | if (ctx->myDoc == NULL) { |
| 12107 | ctxt->myDoc = newDoc; |
| 12108 | } else { |
| 12109 | ctxt->myDoc = ctx->myDoc; |
| 12110 | newDoc->children->doc = ctx->myDoc; |
| 12111 | } |
| 12112 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12113 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12114 | * Get the 4 first bytes and decode the charset |
| 12115 | * if enc != XML_CHAR_ENCODING_NONE |
| 12116 | * plug some encoding conversion routines. |
| 12117 | */ |
| 12118 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12119 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12120 | start[0] = RAW; |
| 12121 | start[1] = NXT(1); |
| 12122 | start[2] = NXT(2); |
| 12123 | start[3] = NXT(3); |
| 12124 | enc = xmlDetectCharEncoding(start, 4); |
| 12125 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12126 | xmlSwitchEncoding(ctxt, enc); |
| 12127 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12128 | } |
| 12129 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12130 | /* |
| 12131 | * Parse a possible text declaration first |
| 12132 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12133 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12134 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12135 | /* |
| 12136 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 12137 | */ |
| 12138 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 12139 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
| 12140 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
| 12141 | "Version mismatch between document and entity\n"); |
| 12142 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12143 | } |
| 12144 | |
| 12145 | /* |
| 12146 | * Doing validity checking on chunk doesn't make sense |
| 12147 | */ |
| 12148 | ctxt->instate = XML_PARSER_CONTENT; |
| 12149 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12150 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12151 | ctxt->loadsubset = ctx->loadsubset; |
| 12152 | ctxt->depth = ctx->depth + 1; |
| 12153 | ctxt->replaceEntities = ctx->replaceEntities; |
| 12154 | if (ctxt->validate) { |
| 12155 | ctxt->vctxt.error = ctx->vctxt.error; |
| 12156 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12157 | } else { |
| 12158 | ctxt->vctxt.error = NULL; |
| 12159 | ctxt->vctxt.warning = NULL; |
| 12160 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 12161 | ctxt->vctxt.nodeTab = NULL; |
| 12162 | ctxt->vctxt.nodeNr = 0; |
| 12163 | ctxt->vctxt.nodeMax = 0; |
| 12164 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12165 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12166 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12167 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12168 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12169 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12170 | ctxt->dictNames = ctx->dictNames; |
| 12171 | ctxt->attsDefault = ctx->attsDefault; |
| 12172 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 12173 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12174 | |
| 12175 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12176 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12177 | ctx->validate = ctxt->validate; |
| 12178 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12179 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12180 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12181 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12182 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12183 | } |
| 12184 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12185 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12186 | } |
| 12187 | |
| 12188 | if (!ctxt->wellFormed) { |
| 12189 | if (ctxt->errNo == 0) |
| 12190 | ret = 1; |
| 12191 | else |
| 12192 | ret = ctxt->errNo; |
| 12193 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12194 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12195 | xmlNodePtr cur; |
| 12196 | |
| 12197 | /* |
| 12198 | * Return the newly created nodeset after unlinking it from |
| 12199 | * they pseudo parent. |
| 12200 | */ |
| 12201 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12202 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12203 | while (cur != NULL) { |
| 12204 | cur->parent = NULL; |
| 12205 | cur = cur->next; |
| 12206 | } |
| 12207 | newDoc->children->children = NULL; |
| 12208 | } |
| 12209 | ret = 0; |
| 12210 | } |
| 12211 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12212 | ctxt->dict = NULL; |
| 12213 | ctxt->attsDefault = NULL; |
| 12214 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12215 | xmlFreeParserCtxt(ctxt); |
| 12216 | newDoc->intSubset = NULL; |
| 12217 | newDoc->extSubset = NULL; |
| 12218 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12219 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12220 | return(ret); |
| 12221 | } |
| 12222 | |
| 12223 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12224 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12225 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12226 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12227 | * @sax: the SAX handler bloc (possibly NULL) |
| 12228 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12229 | * @depth: Used for loop detection, use 0 |
| 12230 | * @URL: the URL for the entity to load |
| 12231 | * @ID: the System ID for the entity to load |
| 12232 | * @list: the return value for the set of parsed nodes |
| 12233 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12234 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12235 | * |
| 12236 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12237 | * the parser error code otherwise |
| 12238 | */ |
| 12239 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12240 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12241 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 12242 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12243 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12244 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12245 | xmlParserCtxtPtr ctxt; |
| 12246 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12247 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12248 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12249 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12250 | xmlChar start[4]; |
| 12251 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12252 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12253 | if (((depth > 40) && |
| 12254 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12255 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12256 | return(XML_ERR_ENTITY_LOOP); |
| 12257 | } |
| 12258 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12259 | if (list != NULL) |
| 12260 | *list = NULL; |
| 12261 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12262 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12263 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12264 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12265 | |
| 12266 | |
| 12267 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 12268 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12269 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12270 | if (oldctxt != NULL) { |
| 12271 | ctxt->_private = oldctxt->_private; |
| 12272 | ctxt->loadsubset = oldctxt->loadsubset; |
| 12273 | ctxt->validate = oldctxt->validate; |
| 12274 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12275 | ctxt->record_info = oldctxt->record_info; |
| 12276 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 12277 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 12278 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12279 | } else { |
| 12280 | /* |
| 12281 | * Doing validity checking on chunk without context |
| 12282 | * doesn't make sense |
| 12283 | */ |
| 12284 | ctxt->_private = NULL; |
| 12285 | ctxt->validate = 0; |
| 12286 | ctxt->external = 2; |
| 12287 | ctxt->loadsubset = 0; |
| 12288 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12289 | if (sax != NULL) { |
| 12290 | oldsax = ctxt->sax; |
| 12291 | ctxt->sax = sax; |
| 12292 | if (user_data != NULL) |
| 12293 | ctxt->userData = user_data; |
| 12294 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12295 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12296 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12297 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12298 | ctxt->node_seq.maximum = 0; |
| 12299 | ctxt->node_seq.length = 0; |
| 12300 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12301 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12302 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12303 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12304 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12305 | newDoc->intSubset = doc->intSubset; |
| 12306 | newDoc->extSubset = doc->extSubset; |
| 12307 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12308 | xmlDictReference(newDoc->dict); |
| 12309 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12310 | if (doc->URL != NULL) { |
| 12311 | newDoc->URL = xmlStrdup(doc->URL); |
| 12312 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12313 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12314 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12315 | if (sax != NULL) |
| 12316 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12317 | ctxt->node_seq.maximum = 0; |
| 12318 | ctxt->node_seq.length = 0; |
| 12319 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12320 | xmlFreeParserCtxt(ctxt); |
| 12321 | newDoc->intSubset = NULL; |
| 12322 | newDoc->extSubset = NULL; |
| 12323 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12324 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12325 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12326 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12327 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12328 | ctxt->myDoc = doc; |
| 12329 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12330 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12331 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12332 | * Get the 4 first bytes and decode the charset |
| 12333 | * if enc != XML_CHAR_ENCODING_NONE |
| 12334 | * plug some encoding conversion routines. |
| 12335 | */ |
| 12336 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12337 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12338 | start[0] = RAW; |
| 12339 | start[1] = NXT(1); |
| 12340 | start[2] = NXT(2); |
| 12341 | start[3] = NXT(3); |
| 12342 | enc = xmlDetectCharEncoding(start, 4); |
| 12343 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12344 | xmlSwitchEncoding(ctxt, enc); |
| 12345 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12346 | } |
| 12347 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12348 | /* |
| 12349 | * Parse a possible text declaration first |
| 12350 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12351 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12352 | xmlParseTextDecl(ctxt); |
| 12353 | } |
| 12354 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12355 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12356 | ctxt->depth = depth; |
| 12357 | |
| 12358 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12359 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12360 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12361 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12362 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12363 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12364 | } |
| 12365 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12366 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12367 | } |
| 12368 | |
| 12369 | if (!ctxt->wellFormed) { |
| 12370 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12371 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12372 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12373 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12374 | } else { |
| 12375 | if (list != NULL) { |
| 12376 | xmlNodePtr cur; |
| 12377 | |
| 12378 | /* |
| 12379 | * Return the newly created nodeset after unlinking it from |
| 12380 | * they pseudo parent. |
| 12381 | */ |
| 12382 | cur = newDoc->children->children; |
| 12383 | *list = cur; |
| 12384 | while (cur != NULL) { |
| 12385 | cur->parent = NULL; |
| 12386 | cur = cur->next; |
| 12387 | } |
| 12388 | newDoc->children->children = NULL; |
| 12389 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12390 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12391 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12392 | |
| 12393 | /* |
| 12394 | * Record in the parent context the number of entities replacement |
| 12395 | * done when parsing that reference. |
| 12396 | */ |
| 12397 | oldctxt->nbentities += ctxt->nbentities; |
| 12398 | /* |
| 12399 | * Also record the size of the entity parsed |
| 12400 | */ |
| 12401 | if (ctxt->input != NULL) { |
| 12402 | oldctxt->sizeentities += ctxt->input->consumed; |
| 12403 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 12404 | } |
| 12405 | /* |
| 12406 | * And record the last error if any |
| 12407 | */ |
| 12408 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12409 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12410 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12411 | if (sax != NULL) |
| 12412 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 12413 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 12414 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 12415 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12416 | ctxt->node_seq.maximum = 0; |
| 12417 | ctxt->node_seq.length = 0; |
| 12418 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12419 | xmlFreeParserCtxt(ctxt); |
| 12420 | newDoc->intSubset = NULL; |
| 12421 | newDoc->extSubset = NULL; |
| 12422 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12423 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12424 | return(ret); |
| 12425 | } |
| 12426 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12427 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12428 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12429 | * xmlParseExternalEntity: |
| 12430 | * @doc: the document the chunk pertains to |
| 12431 | * @sax: the SAX handler bloc (possibly NULL) |
| 12432 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12433 | * @depth: Used for loop detection, use 0 |
| 12434 | * @URL: the URL for the entity to load |
| 12435 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12436 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12437 | * |
| 12438 | * Parse an external general entity |
| 12439 | * An external general parsed entity is well-formed if it matches the |
| 12440 | * production labeled extParsedEnt. |
| 12441 | * |
| 12442 | * [78] extParsedEnt ::= TextDecl? content |
| 12443 | * |
| 12444 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12445 | * the parser error code otherwise |
| 12446 | */ |
| 12447 | |
| 12448 | int |
| 12449 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12450 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12451 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12452 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12453 | } |
| 12454 | |
| 12455 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 12456 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12457 | * @doc: the document the chunk pertains to |
| 12458 | * @sax: the SAX handler bloc (possibly NULL) |
| 12459 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12460 | * @depth: Used for loop detection, use 0 |
| 12461 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12462 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12463 | * |
| 12464 | * Parse a well-balanced chunk of an XML document |
| 12465 | * called by the parser |
| 12466 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12467 | * the content production in the XML grammar: |
| 12468 | * |
| 12469 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12470 | * |
| 12471 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 12472 | * the parser error code otherwise |
| 12473 | */ |
| 12474 | |
| 12475 | int |
| 12476 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12477 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12478 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 12479 | depth, string, lst, 0 ); |
| 12480 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12481 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12482 | |
| 12483 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12484 | * xmlParseBalancedChunkMemoryInternal: |
| 12485 | * @oldctxt: the existing parsing context |
| 12486 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 12487 | * @user_data: the user data field for the parser context |
| 12488 | * @lst: the return value for the set of parsed nodes |
| 12489 | * |
| 12490 | * |
| 12491 | * Parse a well-balanced chunk of an XML document |
| 12492 | * called by the parser |
| 12493 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12494 | * the content production in the XML grammar: |
| 12495 | * |
| 12496 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12497 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12498 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 12499 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12500 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12501 | * 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] | 12502 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12503 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12504 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12505 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 12506 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 12507 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12508 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12509 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12510 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12511 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12512 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12513 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12514 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12515 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12516 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12517 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12518 | return(XML_ERR_ENTITY_LOOP); |
| 12519 | } |
| 12520 | |
| 12521 | |
| 12522 | if (lst != NULL) |
| 12523 | *lst = NULL; |
| 12524 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12525 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12526 | |
| 12527 | size = xmlStrlen(string); |
| 12528 | |
| 12529 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12530 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12531 | if (user_data != NULL) |
| 12532 | ctxt->userData = user_data; |
| 12533 | else |
| 12534 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12535 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12536 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12537 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12538 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12539 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12540 | |
| 12541 | oldsax = ctxt->sax; |
| 12542 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12543 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12544 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 12545 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12546 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 12547 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12548 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12549 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12550 | if (newDoc == NULL) { |
| 12551 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12552 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12553 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12554 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12555 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12556 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12557 | newDoc->dict = ctxt->dict; |
| 12558 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12559 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12560 | } else { |
| 12561 | ctxt->myDoc = oldctxt->myDoc; |
| 12562 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12563 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12564 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12565 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12566 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12567 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12568 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12569 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12570 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12571 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12572 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12573 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12574 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12575 | ctxt->myDoc->children = NULL; |
| 12576 | ctxt->myDoc->last = NULL; |
| 12577 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12578 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12579 | ctxt->instate = XML_PARSER_CONTENT; |
| 12580 | ctxt->depth = oldctxt->depth + 1; |
| 12581 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12582 | ctxt->validate = 0; |
| 12583 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 12584 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 12585 | /* |
| 12586 | * ID/IDREF registration will be done in xmlValidateElement below |
| 12587 | */ |
| 12588 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 12589 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12590 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12591 | ctxt->attsDefault = oldctxt->attsDefault; |
| 12592 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12593 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12594 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12595 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12596 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12597 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12598 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12599 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12600 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12601 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12602 | } |
| 12603 | |
| 12604 | if (!ctxt->wellFormed) { |
| 12605 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12606 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12607 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12608 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12609 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12610 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12611 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12612 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12613 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12614 | xmlNodePtr cur; |
| 12615 | |
| 12616 | /* |
| 12617 | * Return the newly created nodeset after unlinking it from |
| 12618 | * they pseudo parent. |
| 12619 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12620 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12621 | *lst = cur; |
| 12622 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12623 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 12624 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 12625 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 12626 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 12627 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 12628 | oldctxt->myDoc, cur); |
| 12629 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12630 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12631 | cur->parent = NULL; |
| 12632 | cur = cur->next; |
| 12633 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12634 | ctxt->myDoc->children->children = NULL; |
| 12635 | } |
| 12636 | if (ctxt->myDoc != NULL) { |
| 12637 | xmlFreeNode(ctxt->myDoc->children); |
| 12638 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12639 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12640 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12641 | |
| 12642 | /* |
| 12643 | * Record in the parent context the number of entities replacement |
| 12644 | * done when parsing that reference. |
| 12645 | */ |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 12646 | oldctxt->nbentities += ctxt->nbentities; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12647 | /* |
| 12648 | * Also record the last error if any |
| 12649 | */ |
| 12650 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12651 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12652 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12653 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12654 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12655 | ctxt->attsDefault = NULL; |
| 12656 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12657 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12658 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12659 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12660 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12661 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12662 | return(ret); |
| 12663 | } |
| 12664 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12665 | /** |
| 12666 | * xmlParseInNodeContext: |
| 12667 | * @node: the context node |
| 12668 | * @data: the input string |
| 12669 | * @datalen: the input string length in bytes |
| 12670 | * @options: a combination of xmlParserOption |
| 12671 | * @lst: the return value for the set of parsed nodes |
| 12672 | * |
| 12673 | * Parse a well-balanced chunk of an XML document |
| 12674 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 12675 | * |
| 12676 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 12677 | * the content production in the XML grammar: |
| 12678 | * |
| 12679 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12680 | * |
| 12681 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 12682 | * error code otherwise |
| 12683 | */ |
| 12684 | xmlParserErrors |
| 12685 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 12686 | int options, xmlNodePtr *lst) { |
| 12687 | #ifdef SAX2 |
| 12688 | xmlParserCtxtPtr ctxt; |
| 12689 | xmlDocPtr doc = NULL; |
| 12690 | xmlNodePtr fake, cur; |
| 12691 | int nsnr = 0; |
| 12692 | |
| 12693 | xmlParserErrors ret = XML_ERR_OK; |
| 12694 | |
| 12695 | /* |
| 12696 | * check all input parameters, grab the document |
| 12697 | */ |
| 12698 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 12699 | return(XML_ERR_INTERNAL_ERROR); |
| 12700 | switch (node->type) { |
| 12701 | case XML_ELEMENT_NODE: |
| 12702 | case XML_ATTRIBUTE_NODE: |
| 12703 | case XML_TEXT_NODE: |
| 12704 | case XML_CDATA_SECTION_NODE: |
| 12705 | case XML_ENTITY_REF_NODE: |
| 12706 | case XML_PI_NODE: |
| 12707 | case XML_COMMENT_NODE: |
| 12708 | case XML_DOCUMENT_NODE: |
| 12709 | case XML_HTML_DOCUMENT_NODE: |
| 12710 | break; |
| 12711 | default: |
| 12712 | return(XML_ERR_INTERNAL_ERROR); |
| 12713 | |
| 12714 | } |
| 12715 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 12716 | (node->type != XML_DOCUMENT_NODE) && |
| 12717 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 12718 | node = node->parent; |
| 12719 | if (node == NULL) |
| 12720 | return(XML_ERR_INTERNAL_ERROR); |
| 12721 | if (node->type == XML_ELEMENT_NODE) |
| 12722 | doc = node->doc; |
| 12723 | else |
| 12724 | doc = (xmlDocPtr) node; |
| 12725 | if (doc == NULL) |
| 12726 | return(XML_ERR_INTERNAL_ERROR); |
| 12727 | |
| 12728 | /* |
| 12729 | * allocate a context and set-up everything not related to the |
| 12730 | * node position in the tree |
| 12731 | */ |
| 12732 | if (doc->type == XML_DOCUMENT_NODE) |
| 12733 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 12734 | #ifdef LIBXML_HTML_ENABLED |
| 12735 | else if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 12736 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
| 12737 | #endif |
| 12738 | else |
| 12739 | return(XML_ERR_INTERNAL_ERROR); |
| 12740 | |
| 12741 | if (ctxt == NULL) |
| 12742 | return(XML_ERR_NO_MEMORY); |
| 12743 | fake = xmlNewComment(NULL); |
| 12744 | if (fake == NULL) { |
| 12745 | xmlFreeParserCtxt(ctxt); |
| 12746 | return(XML_ERR_NO_MEMORY); |
| 12747 | } |
| 12748 | xmlAddChild(node, fake); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12749 | |
| 12750 | /* |
| 12751 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 12752 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 12753 | * we must wait until the last moment to free the original one. |
| 12754 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12755 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12756 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12757 | xmlDictFree(ctxt->dict); |
| 12758 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12759 | } else |
| 12760 | options |= XML_PARSE_NODICT; |
| 12761 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 12762 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12763 | xmlDetectSAX2(ctxt); |
| 12764 | ctxt->myDoc = doc; |
| 12765 | |
| 12766 | if (node->type == XML_ELEMENT_NODE) { |
| 12767 | nodePush(ctxt, node); |
| 12768 | /* |
| 12769 | * initialize the SAX2 namespaces stack |
| 12770 | */ |
| 12771 | cur = node; |
| 12772 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 12773 | xmlNsPtr ns = cur->nsDef; |
| 12774 | const xmlChar *iprefix, *ihref; |
| 12775 | |
| 12776 | while (ns != NULL) { |
| 12777 | if (ctxt->dict) { |
| 12778 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 12779 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 12780 | } else { |
| 12781 | iprefix = ns->prefix; |
| 12782 | ihref = ns->href; |
| 12783 | } |
| 12784 | |
| 12785 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 12786 | nsPush(ctxt, iprefix, ihref); |
| 12787 | nsnr++; |
| 12788 | } |
| 12789 | ns = ns->next; |
| 12790 | } |
| 12791 | cur = cur->parent; |
| 12792 | } |
| 12793 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12794 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12795 | |
| 12796 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 12797 | /* |
| 12798 | * ID/IDREF registration will be done in xmlValidateElement below |
| 12799 | */ |
| 12800 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 12801 | } |
| 12802 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 12803 | #ifdef LIBXML_HTML_ENABLED |
| 12804 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 12805 | __htmlParseContent(ctxt); |
| 12806 | else |
| 12807 | #endif |
| 12808 | xmlParseContent(ctxt); |
| 12809 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12810 | nsPop(ctxt, nsnr); |
| 12811 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 12812 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 12813 | } else if (RAW != 0) { |
| 12814 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 12815 | } |
| 12816 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 12817 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 12818 | ctxt->wellFormed = 0; |
| 12819 | } |
| 12820 | |
| 12821 | if (!ctxt->wellFormed) { |
| 12822 | if (ctxt->errNo == 0) |
| 12823 | ret = XML_ERR_INTERNAL_ERROR; |
| 12824 | else |
| 12825 | ret = (xmlParserErrors)ctxt->errNo; |
| 12826 | } else { |
| 12827 | ret = XML_ERR_OK; |
| 12828 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12829 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12830 | /* |
| 12831 | * Return the newly created nodeset after unlinking it from |
| 12832 | * the pseudo sibling. |
| 12833 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12834 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12835 | cur = fake->next; |
| 12836 | fake->next = NULL; |
| 12837 | node->last = fake; |
| 12838 | |
| 12839 | if (cur != NULL) { |
| 12840 | cur->prev = NULL; |
| 12841 | } |
| 12842 | |
| 12843 | *lst = cur; |
| 12844 | |
| 12845 | while (cur != NULL) { |
| 12846 | cur->parent = NULL; |
| 12847 | cur = cur->next; |
| 12848 | } |
| 12849 | |
| 12850 | xmlUnlinkNode(fake); |
| 12851 | xmlFreeNode(fake); |
| 12852 | |
| 12853 | |
| 12854 | if (ret != XML_ERR_OK) { |
| 12855 | xmlFreeNodeList(*lst); |
| 12856 | *lst = NULL; |
| 12857 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12858 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 12859 | if (doc->dict != NULL) |
| 12860 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12861 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12862 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12863 | return(ret); |
| 12864 | #else /* !SAX2 */ |
| 12865 | return(XML_ERR_INTERNAL_ERROR); |
| 12866 | #endif |
| 12867 | } |
| 12868 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12869 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12870 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12871 | * xmlParseBalancedChunkMemoryRecover: |
| 12872 | * @doc: the document the chunk pertains to |
| 12873 | * @sax: the SAX handler bloc (possibly NULL) |
| 12874 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12875 | * @depth: Used for loop detection, use 0 |
| 12876 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 12877 | * @lst: the return value for the set of parsed nodes |
| 12878 | * @recover: return nodes even if the data is broken (use 0) |
| 12879 | * |
| 12880 | * |
| 12881 | * Parse a well-balanced chunk of an XML document |
| 12882 | * called by the parser |
| 12883 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12884 | * the content production in the XML grammar: |
| 12885 | * |
| 12886 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12887 | * |
| 12888 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 12889 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 12890 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12891 | * 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] | 12892 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 12893 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12894 | */ |
| 12895 | int |
| 12896 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 12897 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12898 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12899 | xmlParserCtxtPtr ctxt; |
| 12900 | xmlDocPtr newDoc; |
| 12901 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12902 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12903 | int size; |
| 12904 | int ret = 0; |
| 12905 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12906 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12907 | return(XML_ERR_ENTITY_LOOP); |
| 12908 | } |
| 12909 | |
| 12910 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12911 | if (lst != NULL) |
| 12912 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12913 | if (string == NULL) |
| 12914 | return(-1); |
| 12915 | |
| 12916 | size = xmlStrlen(string); |
| 12917 | |
| 12918 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 12919 | if (ctxt == NULL) return(-1); |
| 12920 | ctxt->userData = ctxt; |
| 12921 | if (sax != NULL) { |
| 12922 | oldsax = ctxt->sax; |
| 12923 | ctxt->sax = sax; |
| 12924 | if (user_data != NULL) |
| 12925 | ctxt->userData = user_data; |
| 12926 | } |
| 12927 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12928 | if (newDoc == NULL) { |
| 12929 | xmlFreeParserCtxt(ctxt); |
| 12930 | return(-1); |
| 12931 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12932 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12933 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 12934 | xmlDictFree(ctxt->dict); |
| 12935 | ctxt->dict = doc->dict; |
| 12936 | xmlDictReference(ctxt->dict); |
| 12937 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12938 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12939 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 12940 | ctxt->dictNames = 1; |
| 12941 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 12942 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12943 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12944 | if (doc != NULL) { |
| 12945 | newDoc->intSubset = doc->intSubset; |
| 12946 | newDoc->extSubset = doc->extSubset; |
| 12947 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12948 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12949 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12950 | if (sax != NULL) |
| 12951 | ctxt->sax = oldsax; |
| 12952 | xmlFreeParserCtxt(ctxt); |
| 12953 | newDoc->intSubset = NULL; |
| 12954 | newDoc->extSubset = NULL; |
| 12955 | xmlFreeDoc(newDoc); |
| 12956 | return(-1); |
| 12957 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12958 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 12959 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12960 | if (doc == NULL) { |
| 12961 | ctxt->myDoc = newDoc; |
| 12962 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 12963 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12964 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 12965 | /* Ensure that doc has XML spec namespace */ |
| 12966 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 12967 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12968 | } |
| 12969 | ctxt->instate = XML_PARSER_CONTENT; |
| 12970 | ctxt->depth = depth; |
| 12971 | |
| 12972 | /* |
| 12973 | * Doing validity checking on chunk doesn't make sense |
| 12974 | */ |
| 12975 | ctxt->validate = 0; |
| 12976 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12977 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12978 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 12979 | if ( doc != NULL ){ |
| 12980 | content = doc->children; |
| 12981 | doc->children = NULL; |
| 12982 | xmlParseContent(ctxt); |
| 12983 | doc->children = content; |
| 12984 | } |
| 12985 | else { |
| 12986 | xmlParseContent(ctxt); |
| 12987 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12988 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12989 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12990 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12991 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12992 | } |
| 12993 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12994 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12995 | } |
| 12996 | |
| 12997 | if (!ctxt->wellFormed) { |
| 12998 | if (ctxt->errNo == 0) |
| 12999 | ret = 1; |
| 13000 | else |
| 13001 | ret = ctxt->errNo; |
| 13002 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 13003 | ret = 0; |
| 13004 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13005 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13006 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 13007 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13008 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13009 | /* |
| 13010 | * Return the newly created nodeset after unlinking it from |
| 13011 | * they pseudo parent. |
| 13012 | */ |
| 13013 | cur = newDoc->children->children; |
| 13014 | *lst = cur; |
| 13015 | while (cur != NULL) { |
| 13016 | xmlSetTreeDoc(cur, doc); |
| 13017 | cur->parent = NULL; |
| 13018 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13019 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 13020 | newDoc->children->children = NULL; |
| 13021 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13022 | |
| 13023 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13024 | ctxt->sax = oldsax; |
| 13025 | xmlFreeParserCtxt(ctxt); |
| 13026 | newDoc->intSubset = NULL; |
| 13027 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13028 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13029 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13030 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13031 | return(ret); |
| 13032 | } |
| 13033 | |
| 13034 | /** |
| 13035 | * xmlSAXParseEntity: |
| 13036 | * @sax: the SAX handler block |
| 13037 | * @filename: the filename |
| 13038 | * |
| 13039 | * parse an XML external entity out of context and build a tree. |
| 13040 | * It use the given SAX function block to handle the parsing callback. |
| 13041 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13042 | * |
| 13043 | * [78] extParsedEnt ::= TextDecl? content |
| 13044 | * |
| 13045 | * This correspond to a "Well Balanced" chunk |
| 13046 | * |
| 13047 | * Returns the resulting document tree |
| 13048 | */ |
| 13049 | |
| 13050 | xmlDocPtr |
| 13051 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13052 | xmlDocPtr ret; |
| 13053 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13054 | |
| 13055 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13056 | if (ctxt == NULL) { |
| 13057 | return(NULL); |
| 13058 | } |
| 13059 | if (sax != NULL) { |
| 13060 | if (ctxt->sax != NULL) |
| 13061 | xmlFree(ctxt->sax); |
| 13062 | ctxt->sax = sax; |
| 13063 | ctxt->userData = NULL; |
| 13064 | } |
| 13065 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13066 | xmlParseExtParsedEnt(ctxt); |
| 13067 | |
| 13068 | if (ctxt->wellFormed) |
| 13069 | ret = ctxt->myDoc; |
| 13070 | else { |
| 13071 | ret = NULL; |
| 13072 | xmlFreeDoc(ctxt->myDoc); |
| 13073 | ctxt->myDoc = NULL; |
| 13074 | } |
| 13075 | if (sax != NULL) |
| 13076 | ctxt->sax = NULL; |
| 13077 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13078 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13079 | return(ret); |
| 13080 | } |
| 13081 | |
| 13082 | /** |
| 13083 | * xmlParseEntity: |
| 13084 | * @filename: the filename |
| 13085 | * |
| 13086 | * parse an XML external entity out of context and build a tree. |
| 13087 | * |
| 13088 | * [78] extParsedEnt ::= TextDecl? content |
| 13089 | * |
| 13090 | * This correspond to a "Well Balanced" chunk |
| 13091 | * |
| 13092 | * Returns the resulting document tree |
| 13093 | */ |
| 13094 | |
| 13095 | xmlDocPtr |
| 13096 | xmlParseEntity(const char *filename) { |
| 13097 | return(xmlSAXParseEntity(NULL, filename)); |
| 13098 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13099 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13100 | |
| 13101 | /** |
| 13102 | * xmlCreateEntityParserCtxt: |
| 13103 | * @URL: the entity URL |
| 13104 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13105 | * @base: a possible base for the target URI |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13106 | * |
| 13107 | * Create a parser context for an external entity |
| 13108 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13109 | * by default if found at compile-time. |
| 13110 | * |
| 13111 | * Returns the new parser context or NULL |
| 13112 | */ |
| 13113 | xmlParserCtxtPtr |
| 13114 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 13115 | const xmlChar *base) { |
| 13116 | xmlParserCtxtPtr ctxt; |
| 13117 | xmlParserInputPtr inputStream; |
| 13118 | char *directory = NULL; |
| 13119 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13120 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13121 | ctxt = xmlNewParserCtxt(); |
| 13122 | if (ctxt == NULL) { |
| 13123 | return(NULL); |
| 13124 | } |
| 13125 | |
| 13126 | uri = xmlBuildURI(URL, base); |
| 13127 | |
| 13128 | if (uri == NULL) { |
| 13129 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 13130 | if (inputStream == NULL) { |
| 13131 | xmlFreeParserCtxt(ctxt); |
| 13132 | return(NULL); |
| 13133 | } |
| 13134 | |
| 13135 | inputPush(ctxt, inputStream); |
| 13136 | |
| 13137 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13138 | directory = xmlParserGetDirectory((char *)URL); |
| 13139 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13140 | ctxt->directory = directory; |
| 13141 | } else { |
| 13142 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 13143 | if (inputStream == NULL) { |
| 13144 | xmlFree(uri); |
| 13145 | xmlFreeParserCtxt(ctxt); |
| 13146 | return(NULL); |
| 13147 | } |
| 13148 | |
| 13149 | inputPush(ctxt, inputStream); |
| 13150 | |
| 13151 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13152 | directory = xmlParserGetDirectory((char *)uri); |
| 13153 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13154 | ctxt->directory = directory; |
| 13155 | xmlFree(uri); |
| 13156 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13157 | return(ctxt); |
| 13158 | } |
| 13159 | |
| 13160 | /************************************************************************ |
| 13161 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13162 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13163 | * * |
| 13164 | ************************************************************************/ |
| 13165 | |
| 13166 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13167 | * xmlCreateURLParserCtxt: |
| 13168 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13169 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13170 | * |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13171 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13172 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13173 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13174 | * |
| 13175 | * Returns the new parser context or NULL |
| 13176 | */ |
| 13177 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13178 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13179 | { |
| 13180 | xmlParserCtxtPtr ctxt; |
| 13181 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13182 | char *directory = NULL; |
| 13183 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13184 | ctxt = xmlNewParserCtxt(); |
| 13185 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13186 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13187 | return(NULL); |
| 13188 | } |
| 13189 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13190 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13191 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13192 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13193 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 13194 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13195 | if (inputStream == NULL) { |
| 13196 | xmlFreeParserCtxt(ctxt); |
| 13197 | return(NULL); |
| 13198 | } |
| 13199 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13200 | inputPush(ctxt, inputStream); |
| 13201 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 13202 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13203 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13204 | ctxt->directory = directory; |
| 13205 | |
| 13206 | return(ctxt); |
| 13207 | } |
| 13208 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13209 | /** |
| 13210 | * xmlCreateFileParserCtxt: |
| 13211 | * @filename: the filename |
| 13212 | * |
| 13213 | * Create a parser context for a file content. |
| 13214 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13215 | * by default if found at compile-time. |
| 13216 | * |
| 13217 | * Returns the new parser context or NULL |
| 13218 | */ |
| 13219 | xmlParserCtxtPtr |
| 13220 | xmlCreateFileParserCtxt(const char *filename) |
| 13221 | { |
| 13222 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 13223 | } |
| 13224 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13225 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13226 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13227 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13228 | * @sax: the SAX handler block |
| 13229 | * @filename: the filename |
| 13230 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13231 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13232 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13233 | * |
| 13234 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13235 | * compressed document is provided by default if found at compile-time. |
| 13236 | * It use the given SAX function block to handle the parsing callback. |
| 13237 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13238 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 13239 | * User data (void *) is stored within the parser context in the |
| 13240 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13241 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13242 | * Returns the resulting document tree |
| 13243 | */ |
| 13244 | |
| 13245 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13246 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 13247 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13248 | xmlDocPtr ret; |
| 13249 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13250 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 13251 | xmlInitParser(); |
| 13252 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13253 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13254 | if (ctxt == NULL) { |
| 13255 | return(NULL); |
| 13256 | } |
| 13257 | if (sax != NULL) { |
| 13258 | if (ctxt->sax != NULL) |
| 13259 | xmlFree(ctxt->sax); |
| 13260 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13261 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13262 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13263 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 13264 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13265 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13266 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 13267 | if (ctxt->directory == NULL) |
| 13268 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13269 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 13270 | ctxt->recovery = recovery; |
| 13271 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13272 | xmlParseDocument(ctxt); |
| 13273 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13274 | if ((ctxt->wellFormed) || recovery) { |
| 13275 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 13276 | if (ret != NULL) { |
| 13277 | if (ctxt->input->buf->compressed > 0) |
| 13278 | ret->compression = 9; |
| 13279 | else |
| 13280 | ret->compression = ctxt->input->buf->compressed; |
| 13281 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13282 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13283 | else { |
| 13284 | ret = NULL; |
| 13285 | xmlFreeDoc(ctxt->myDoc); |
| 13286 | ctxt->myDoc = NULL; |
| 13287 | } |
| 13288 | if (sax != NULL) |
| 13289 | ctxt->sax = NULL; |
| 13290 | xmlFreeParserCtxt(ctxt); |
| 13291 | |
| 13292 | return(ret); |
| 13293 | } |
| 13294 | |
| 13295 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13296 | * xmlSAXParseFile: |
| 13297 | * @sax: the SAX handler block |
| 13298 | * @filename: the filename |
| 13299 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13300 | * documents |
| 13301 | * |
| 13302 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13303 | * compressed document is provided by default if found at compile-time. |
| 13304 | * It use the given SAX function block to handle the parsing callback. |
| 13305 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13306 | * |
| 13307 | * Returns the resulting document tree |
| 13308 | */ |
| 13309 | |
| 13310 | xmlDocPtr |
| 13311 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 13312 | int recovery) { |
| 13313 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 13314 | } |
| 13315 | |
| 13316 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13317 | * xmlRecoverDoc: |
| 13318 | * @cur: a pointer to an array of xmlChar |
| 13319 | * |
| 13320 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13321 | * In the case the document is not Well Formed, a attempt to build a |
| 13322 | * tree is tried anyway |
| 13323 | * |
| 13324 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13325 | */ |
| 13326 | |
| 13327 | xmlDocPtr |
| 13328 | xmlRecoverDoc(xmlChar *cur) { |
| 13329 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 13330 | } |
| 13331 | |
| 13332 | /** |
| 13333 | * xmlParseFile: |
| 13334 | * @filename: the filename |
| 13335 | * |
| 13336 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13337 | * compressed document is provided by default if found at compile-time. |
| 13338 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 13339 | * Returns the resulting document tree if the file was wellformed, |
| 13340 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13341 | */ |
| 13342 | |
| 13343 | xmlDocPtr |
| 13344 | xmlParseFile(const char *filename) { |
| 13345 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 13346 | } |
| 13347 | |
| 13348 | /** |
| 13349 | * xmlRecoverFile: |
| 13350 | * @filename: the filename |
| 13351 | * |
| 13352 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13353 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13354 | * In the case the document is not Well Formed, it attempts to build |
| 13355 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13356 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13357 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13358 | */ |
| 13359 | |
| 13360 | xmlDocPtr |
| 13361 | xmlRecoverFile(const char *filename) { |
| 13362 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 13363 | } |
| 13364 | |
| 13365 | |
| 13366 | /** |
| 13367 | * xmlSetupParserForBuffer: |
| 13368 | * @ctxt: an XML parser context |
| 13369 | * @buffer: a xmlChar * buffer |
| 13370 | * @filename: a file name |
| 13371 | * |
| 13372 | * Setup the parser context to parse a new buffer; Clears any prior |
| 13373 | * contents from the parser context. The buffer parameter must not be |
| 13374 | * NULL, but the filename parameter can be |
| 13375 | */ |
| 13376 | void |
| 13377 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 13378 | const char* filename) |
| 13379 | { |
| 13380 | xmlParserInputPtr input; |
| 13381 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13382 | if ((ctxt == NULL) || (buffer == NULL)) |
| 13383 | return; |
| 13384 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13385 | input = xmlNewInputStream(ctxt); |
| 13386 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 13387 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13388 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13389 | return; |
| 13390 | } |
| 13391 | |
| 13392 | xmlClearParserCtxt(ctxt); |
| 13393 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 13394 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13395 | input->base = buffer; |
| 13396 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13397 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13398 | inputPush(ctxt, input); |
| 13399 | } |
| 13400 | |
| 13401 | /** |
| 13402 | * xmlSAXUserParseFile: |
| 13403 | * @sax: a SAX handler |
| 13404 | * @user_data: The user data returned on SAX callbacks |
| 13405 | * @filename: a file name |
| 13406 | * |
| 13407 | * parse an XML file and call the given SAX handler routines. |
| 13408 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13409 | * |
| 13410 | * Returns 0 in case of success or a error number otherwise |
| 13411 | */ |
| 13412 | int |
| 13413 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 13414 | const char *filename) { |
| 13415 | int ret = 0; |
| 13416 | xmlParserCtxtPtr ctxt; |
| 13417 | |
| 13418 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13419 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 13420 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13421 | xmlFree(ctxt->sax); |
| 13422 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13423 | xmlDetectSAX2(ctxt); |
| 13424 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13425 | if (user_data != NULL) |
| 13426 | ctxt->userData = user_data; |
| 13427 | |
| 13428 | xmlParseDocument(ctxt); |
| 13429 | |
| 13430 | if (ctxt->wellFormed) |
| 13431 | ret = 0; |
| 13432 | else { |
| 13433 | if (ctxt->errNo != 0) |
| 13434 | ret = ctxt->errNo; |
| 13435 | else |
| 13436 | ret = -1; |
| 13437 | } |
| 13438 | if (sax != NULL) |
| 13439 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13440 | if (ctxt->myDoc != NULL) { |
| 13441 | xmlFreeDoc(ctxt->myDoc); |
| 13442 | ctxt->myDoc = NULL; |
| 13443 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13444 | xmlFreeParserCtxt(ctxt); |
| 13445 | |
| 13446 | return ret; |
| 13447 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13448 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13449 | |
| 13450 | /************************************************************************ |
| 13451 | * * |
| 13452 | * Front ends when parsing from memory * |
| 13453 | * * |
| 13454 | ************************************************************************/ |
| 13455 | |
| 13456 | /** |
| 13457 | * xmlCreateMemoryParserCtxt: |
| 13458 | * @buffer: a pointer to a char array |
| 13459 | * @size: the size of the array |
| 13460 | * |
| 13461 | * Create a parser context for an XML in-memory document. |
| 13462 | * |
| 13463 | * Returns the new parser context or NULL |
| 13464 | */ |
| 13465 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 13466 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13467 | xmlParserCtxtPtr ctxt; |
| 13468 | xmlParserInputPtr input; |
| 13469 | xmlParserInputBufferPtr buf; |
| 13470 | |
| 13471 | if (buffer == NULL) |
| 13472 | return(NULL); |
| 13473 | if (size <= 0) |
| 13474 | return(NULL); |
| 13475 | |
| 13476 | ctxt = xmlNewParserCtxt(); |
| 13477 | if (ctxt == NULL) |
| 13478 | return(NULL); |
| 13479 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 13480 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13481 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13482 | if (buf == NULL) { |
| 13483 | xmlFreeParserCtxt(ctxt); |
| 13484 | return(NULL); |
| 13485 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13486 | |
| 13487 | input = xmlNewInputStream(ctxt); |
| 13488 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13489 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13490 | xmlFreeParserCtxt(ctxt); |
| 13491 | return(NULL); |
| 13492 | } |
| 13493 | |
| 13494 | input->filename = NULL; |
| 13495 | input->buf = buf; |
| 13496 | input->base = input->buf->buffer->content; |
| 13497 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13498 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13499 | |
| 13500 | inputPush(ctxt, input); |
| 13501 | return(ctxt); |
| 13502 | } |
| 13503 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13504 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13505 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13506 | * xmlSAXParseMemoryWithData: |
| 13507 | * @sax: the SAX handler block |
| 13508 | * @buffer: an pointer to a char array |
| 13509 | * @size: the size of the array |
| 13510 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13511 | * documents |
| 13512 | * @data: the userdata |
| 13513 | * |
| 13514 | * parse an XML in-memory block and use the given SAX function block |
| 13515 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13516 | * DOM tree building routines. |
| 13517 | * |
| 13518 | * User data (void *) is stored within the parser context in the |
| 13519 | * context's _private member, so it is available nearly everywhere in libxml |
| 13520 | * |
| 13521 | * Returns the resulting document tree |
| 13522 | */ |
| 13523 | |
| 13524 | xmlDocPtr |
| 13525 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 13526 | int size, int recovery, void *data) { |
| 13527 | xmlDocPtr ret; |
| 13528 | xmlParserCtxtPtr ctxt; |
| 13529 | |
| 13530 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 13531 | if (ctxt == NULL) return(NULL); |
| 13532 | if (sax != NULL) { |
| 13533 | if (ctxt->sax != NULL) |
| 13534 | xmlFree(ctxt->sax); |
| 13535 | ctxt->sax = sax; |
| 13536 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13537 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13538 | if (data!=NULL) { |
| 13539 | ctxt->_private=data; |
| 13540 | } |
| 13541 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 13542 | ctxt->recovery = recovery; |
| 13543 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13544 | xmlParseDocument(ctxt); |
| 13545 | |
| 13546 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 13547 | else { |
| 13548 | ret = NULL; |
| 13549 | xmlFreeDoc(ctxt->myDoc); |
| 13550 | ctxt->myDoc = NULL; |
| 13551 | } |
| 13552 | if (sax != NULL) |
| 13553 | ctxt->sax = NULL; |
| 13554 | xmlFreeParserCtxt(ctxt); |
| 13555 | |
| 13556 | return(ret); |
| 13557 | } |
| 13558 | |
| 13559 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13560 | * xmlSAXParseMemory: |
| 13561 | * @sax: the SAX handler block |
| 13562 | * @buffer: an pointer to a char array |
| 13563 | * @size: the size of the array |
| 13564 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 13565 | * documents |
| 13566 | * |
| 13567 | * parse an XML in-memory block and use the given SAX function block |
| 13568 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13569 | * DOM tree building routines. |
| 13570 | * |
| 13571 | * Returns the resulting document tree |
| 13572 | */ |
| 13573 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13574 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 13575 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13576 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13577 | } |
| 13578 | |
| 13579 | /** |
| 13580 | * xmlParseMemory: |
| 13581 | * @buffer: an pointer to a char array |
| 13582 | * @size: the size of the array |
| 13583 | * |
| 13584 | * parse an XML in-memory block and build a tree. |
| 13585 | * |
| 13586 | * Returns the resulting document tree |
| 13587 | */ |
| 13588 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13589 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13590 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 13591 | } |
| 13592 | |
| 13593 | /** |
| 13594 | * xmlRecoverMemory: |
| 13595 | * @buffer: an pointer to a char array |
| 13596 | * @size: the size of the array |
| 13597 | * |
| 13598 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13599 | * In the case the document is not Well Formed, an attempt to |
| 13600 | * build a tree is tried anyway |
| 13601 | * |
| 13602 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13603 | */ |
| 13604 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13605 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13606 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 13607 | } |
| 13608 | |
| 13609 | /** |
| 13610 | * xmlSAXUserParseMemory: |
| 13611 | * @sax: a SAX handler |
| 13612 | * @user_data: The user data returned on SAX callbacks |
| 13613 | * @buffer: an in-memory XML document input |
| 13614 | * @size: the length of the XML document in bytes |
| 13615 | * |
| 13616 | * A better SAX parsing routine. |
| 13617 | * parse an XML in-memory buffer and call the given SAX handler routines. |
| 13618 | * |
| 13619 | * Returns 0 in case of success or a error number otherwise |
| 13620 | */ |
| 13621 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 13622 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13623 | int ret = 0; |
| 13624 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13625 | |
| 13626 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 13627 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13628 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 13629 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 13630 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13631 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13632 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 13633 | if (user_data != NULL) |
| 13634 | ctxt->userData = user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13635 | |
| 13636 | xmlParseDocument(ctxt); |
| 13637 | |
| 13638 | if (ctxt->wellFormed) |
| 13639 | ret = 0; |
| 13640 | else { |
| 13641 | if (ctxt->errNo != 0) |
| 13642 | ret = ctxt->errNo; |
| 13643 | else |
| 13644 | ret = -1; |
| 13645 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13646 | if (sax != NULL) |
| 13647 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13648 | if (ctxt->myDoc != NULL) { |
| 13649 | xmlFreeDoc(ctxt->myDoc); |
| 13650 | ctxt->myDoc = NULL; |
| 13651 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13652 | xmlFreeParserCtxt(ctxt); |
| 13653 | |
| 13654 | return ret; |
| 13655 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13656 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13657 | |
| 13658 | /** |
| 13659 | * xmlCreateDocParserCtxt: |
| 13660 | * @cur: a pointer to an array of xmlChar |
| 13661 | * |
| 13662 | * Creates a parser context for an XML in-memory document. |
| 13663 | * |
| 13664 | * Returns the new parser context or NULL |
| 13665 | */ |
| 13666 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13667 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13668 | int len; |
| 13669 | |
| 13670 | if (cur == NULL) |
| 13671 | return(NULL); |
| 13672 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13673 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13674 | } |
| 13675 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13676 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13677 | /** |
| 13678 | * xmlSAXParseDoc: |
| 13679 | * @sax: the SAX handler block |
| 13680 | * @cur: a pointer to an array of xmlChar |
| 13681 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13682 | * documents |
| 13683 | * |
| 13684 | * parse an XML in-memory document and build a tree. |
| 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 |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 13692 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13693 | xmlDocPtr ret; |
| 13694 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13695 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13696 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13697 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13698 | |
| 13699 | |
| 13700 | ctxt = xmlCreateDocParserCtxt(cur); |
| 13701 | if (ctxt == NULL) return(NULL); |
| 13702 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13703 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13704 | ctxt->sax = sax; |
| 13705 | ctxt->userData = NULL; |
| 13706 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13707 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13708 | |
| 13709 | xmlParseDocument(ctxt); |
| 13710 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 13711 | else { |
| 13712 | ret = NULL; |
| 13713 | xmlFreeDoc(ctxt->myDoc); |
| 13714 | ctxt->myDoc = NULL; |
| 13715 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13716 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13717 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13718 | xmlFreeParserCtxt(ctxt); |
| 13719 | |
| 13720 | return(ret); |
| 13721 | } |
| 13722 | |
| 13723 | /** |
| 13724 | * xmlParseDoc: |
| 13725 | * @cur: a pointer to an array of xmlChar |
| 13726 | * |
| 13727 | * parse an XML in-memory document and build a tree. |
| 13728 | * |
| 13729 | * Returns the resulting document tree |
| 13730 | */ |
| 13731 | |
| 13732 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 13733 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13734 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 13735 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13736 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13737 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13738 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 13739 | /************************************************************************ |
| 13740 | * * |
| 13741 | * Specific function to keep track of entities references * |
| 13742 | * and used by the XSLT debugger * |
| 13743 | * * |
| 13744 | ************************************************************************/ |
| 13745 | |
| 13746 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 13747 | |
| 13748 | /** |
| 13749 | * xmlAddEntityReference: |
| 13750 | * @ent : A valid entity |
| 13751 | * @firstNode : A valid first node for children of entity |
| 13752 | * @lastNode : A valid last node of children entity |
| 13753 | * |
| 13754 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 13755 | */ |
| 13756 | static void |
| 13757 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 13758 | xmlNodePtr lastNode) |
| 13759 | { |
| 13760 | if (xmlEntityRefFunc != NULL) { |
| 13761 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 13762 | } |
| 13763 | } |
| 13764 | |
| 13765 | |
| 13766 | /** |
| 13767 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 13768 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 13769 | * |
| 13770 | * Set the function to call call back when a xml reference has been made |
| 13771 | */ |
| 13772 | void |
| 13773 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 13774 | { |
| 13775 | xmlEntityRefFunc = func; |
| 13776 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13777 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13778 | |
| 13779 | /************************************************************************ |
| 13780 | * * |
| 13781 | * Miscellaneous * |
| 13782 | * * |
| 13783 | ************************************************************************/ |
| 13784 | |
| 13785 | #ifdef LIBXML_XPATH_ENABLED |
| 13786 | #include <libxml/xpath.h> |
| 13787 | #endif |
| 13788 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 13789 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13790 | static int xmlParserInitialized = 0; |
| 13791 | |
| 13792 | /** |
| 13793 | * xmlInitParser: |
| 13794 | * |
| 13795 | * Initialization function for the XML parser. |
| 13796 | * This is not reentrant. Call once before processing in case of |
| 13797 | * use in multithreaded programs. |
| 13798 | */ |
| 13799 | |
| 13800 | void |
| 13801 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 13802 | if (xmlParserInitialized != 0) |
| 13803 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13804 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13805 | #ifdef LIBXML_THREAD_ENABLED |
| 13806 | __xmlGlobalInitMutexLock(); |
| 13807 | if (xmlParserInitialized == 0) { |
| 13808 | #endif |
| 13809 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 13810 | (xmlGenericError == NULL)) |
| 13811 | initGenericErrorDefaultFunc(NULL); |
| 13812 | xmlInitGlobals(); |
| 13813 | xmlInitThreads(); |
| 13814 | xmlInitMemory(); |
| 13815 | xmlInitCharEncodingHandlers(); |
| 13816 | xmlDefaultSAXHandlerInit(); |
| 13817 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 13818 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13819 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 13820 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13821 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13822 | htmlInitAutoClose(); |
| 13823 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13824 | #endif |
| 13825 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13826 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13827 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13828 | xmlParserInitialized = 1; |
| 13829 | #ifdef LIBXML_THREAD_ENABLED |
| 13830 | } |
| 13831 | __xmlGlobalInitMutexUnlock(); |
| 13832 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13833 | } |
| 13834 | |
| 13835 | /** |
| 13836 | * xmlCleanupParser: |
| 13837 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 13838 | * This function name is somewhat misleading. It does not clean up |
| 13839 | * parser state, it cleans up memory allocated by the library itself. |
| 13840 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 13841 | * related global memory allocated for the library processing. |
| 13842 | * It doesn't deallocate any document related memory. One should |
| 13843 | * call xmlCleanupParser() only when the process has finished using |
| 13844 | * the library and all XML/HTML documents built with it. |
| 13845 | * See also xmlInitParser() which has the opposite function of preparing |
| 13846 | * the library for operations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13847 | */ |
| 13848 | |
| 13849 | void |
| 13850 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 13851 | if (!xmlParserInitialized) |
| 13852 | return; |
| 13853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13854 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 13855 | #ifdef LIBXML_CATALOG_ENABLED |
| 13856 | xmlCatalogCleanup(); |
| 13857 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 13858 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 13859 | xmlCleanupInputCallbacks(); |
| 13860 | #ifdef LIBXML_OUTPUT_ENABLED |
| 13861 | xmlCleanupOutputCallbacks(); |
| 13862 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13863 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 13864 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 13865 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13866 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 13867 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 13868 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 13869 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13870 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 13871 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13872 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13873 | |
| 13874 | /************************************************************************ |
| 13875 | * * |
| 13876 | * New set (2.6.0) of simpler and more flexible APIs * |
| 13877 | * * |
| 13878 | ************************************************************************/ |
| 13879 | |
| 13880 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13881 | * DICT_FREE: |
| 13882 | * @str: a string |
| 13883 | * |
| 13884 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 13885 | * current scope |
| 13886 | */ |
| 13887 | #define DICT_FREE(str) \ |
| 13888 | if ((str) && ((!dict) || \ |
| 13889 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 13890 | xmlFree((char *)(str)); |
| 13891 | |
| 13892 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13893 | * xmlCtxtReset: |
| 13894 | * @ctxt: an XML parser context |
| 13895 | * |
| 13896 | * Reset a parser context |
| 13897 | */ |
| 13898 | void |
| 13899 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 13900 | { |
| 13901 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13902 | xmlDictPtr dict; |
| 13903 | |
| 13904 | if (ctxt == NULL) |
| 13905 | return; |
| 13906 | |
| 13907 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13908 | |
| 13909 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 13910 | xmlFreeInputStream(input); |
| 13911 | } |
| 13912 | ctxt->inputNr = 0; |
| 13913 | ctxt->input = NULL; |
| 13914 | |
| 13915 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 13916 | if (ctxt->spaceTab != NULL) { |
| 13917 | ctxt->spaceTab[0] = -1; |
| 13918 | ctxt->space = &ctxt->spaceTab[0]; |
| 13919 | } else { |
| 13920 | ctxt->space = NULL; |
| 13921 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13922 | |
| 13923 | |
| 13924 | ctxt->nodeNr = 0; |
| 13925 | ctxt->node = NULL; |
| 13926 | |
| 13927 | ctxt->nameNr = 0; |
| 13928 | ctxt->name = NULL; |
| 13929 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13930 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13931 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13932 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13933 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13934 | DICT_FREE(ctxt->directory); |
| 13935 | ctxt->directory = NULL; |
| 13936 | DICT_FREE(ctxt->extSubURI); |
| 13937 | ctxt->extSubURI = NULL; |
| 13938 | DICT_FREE(ctxt->extSubSystem); |
| 13939 | ctxt->extSubSystem = NULL; |
| 13940 | if (ctxt->myDoc != NULL) |
| 13941 | xmlFreeDoc(ctxt->myDoc); |
| 13942 | ctxt->myDoc = NULL; |
| 13943 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13944 | ctxt->standalone = -1; |
| 13945 | ctxt->hasExternalSubset = 0; |
| 13946 | ctxt->hasPErefs = 0; |
| 13947 | ctxt->html = 0; |
| 13948 | ctxt->external = 0; |
| 13949 | ctxt->instate = XML_PARSER_START; |
| 13950 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13951 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13952 | ctxt->wellFormed = 1; |
| 13953 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 13954 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13955 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 13956 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13957 | ctxt->vctxt.userData = ctxt; |
| 13958 | ctxt->vctxt.error = xmlParserValidityError; |
| 13959 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 13960 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13961 | ctxt->record_info = 0; |
| 13962 | ctxt->nbChars = 0; |
| 13963 | ctxt->checkIndex = 0; |
| 13964 | ctxt->inSubset = 0; |
| 13965 | ctxt->errNo = XML_ERR_OK; |
| 13966 | ctxt->depth = 0; |
| 13967 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 13968 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 13969 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13970 | ctxt->sizeentities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13971 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 13972 | |
| 13973 | if (ctxt->attsDefault != NULL) { |
| 13974 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 13975 | ctxt->attsDefault = NULL; |
| 13976 | } |
| 13977 | if (ctxt->attsSpecial != NULL) { |
| 13978 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 13979 | ctxt->attsSpecial = NULL; |
| 13980 | } |
| 13981 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13982 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13983 | if (ctxt->catalogs != NULL) |
| 13984 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13985 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 13986 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13987 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13988 | } |
| 13989 | |
| 13990 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 13991 | * xmlCtxtResetPush: |
| 13992 | * @ctxt: an XML parser context |
| 13993 | * @chunk: a pointer to an array of chars |
| 13994 | * @size: number of chars in the array |
| 13995 | * @filename: an optional file name or URI |
| 13996 | * @encoding: the document encoding, or NULL |
| 13997 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13998 | * Reset a push parser context |
| 13999 | * |
| 14000 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14001 | */ |
| 14002 | int |
| 14003 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 14004 | int size, const char *filename, const char *encoding) |
| 14005 | { |
| 14006 | xmlParserInputPtr inputStream; |
| 14007 | xmlParserInputBufferPtr buf; |
| 14008 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 14009 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 14010 | if (ctxt == NULL) |
| 14011 | return(1); |
| 14012 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14013 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 14014 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 14015 | |
| 14016 | buf = xmlAllocParserInputBuffer(enc); |
| 14017 | if (buf == NULL) |
| 14018 | return(1); |
| 14019 | |
| 14020 | if (ctxt == NULL) { |
| 14021 | xmlFreeParserInputBuffer(buf); |
| 14022 | return(1); |
| 14023 | } |
| 14024 | |
| 14025 | xmlCtxtReset(ctxt); |
| 14026 | |
| 14027 | if (ctxt->pushTab == NULL) { |
| 14028 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 14029 | sizeof(xmlChar *)); |
| 14030 | if (ctxt->pushTab == NULL) { |
| 14031 | xmlErrMemory(ctxt, NULL); |
| 14032 | xmlFreeParserInputBuffer(buf); |
| 14033 | return(1); |
| 14034 | } |
| 14035 | } |
| 14036 | |
| 14037 | if (filename == NULL) { |
| 14038 | ctxt->directory = NULL; |
| 14039 | } else { |
| 14040 | ctxt->directory = xmlParserGetDirectory(filename); |
| 14041 | } |
| 14042 | |
| 14043 | inputStream = xmlNewInputStream(ctxt); |
| 14044 | if (inputStream == NULL) { |
| 14045 | xmlFreeParserInputBuffer(buf); |
| 14046 | return(1); |
| 14047 | } |
| 14048 | |
| 14049 | if (filename == NULL) |
| 14050 | inputStream->filename = NULL; |
| 14051 | else |
| 14052 | inputStream->filename = (char *) |
| 14053 | xmlCanonicPath((const xmlChar *) filename); |
| 14054 | inputStream->buf = buf; |
| 14055 | inputStream->base = inputStream->buf->buffer->content; |
| 14056 | inputStream->cur = inputStream->buf->buffer->content; |
| 14057 | inputStream->end = |
| 14058 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 14059 | |
| 14060 | inputPush(ctxt, inputStream); |
| 14061 | |
| 14062 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 14063 | (ctxt->input->buf != NULL)) { |
| 14064 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 14065 | int cur = ctxt->input->cur - ctxt->input->base; |
| 14066 | |
| 14067 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 14068 | |
| 14069 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 14070 | ctxt->input->cur = ctxt->input->base + cur; |
| 14071 | ctxt->input->end = |
| 14072 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer-> |
| 14073 | use]; |
| 14074 | #ifdef DEBUG_PUSH |
| 14075 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 14076 | #endif |
| 14077 | } |
| 14078 | |
| 14079 | if (encoding != NULL) { |
| 14080 | xmlCharEncodingHandlerPtr hdlr; |
| 14081 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14082 | if (ctxt->encoding != NULL) |
| 14083 | xmlFree((xmlChar *) ctxt->encoding); |
| 14084 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14085 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14086 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14087 | if (hdlr != NULL) { |
| 14088 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14089 | } else { |
| 14090 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 14091 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 14092 | } |
| 14093 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 14094 | xmlSwitchEncoding(ctxt, enc); |
| 14095 | } |
| 14096 | |
| 14097 | return(0); |
| 14098 | } |
| 14099 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14100 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14101 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14102 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14103 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14104 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14105 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14106 | * |
| 14107 | * Applies the options to the parser context |
| 14108 | * |
| 14109 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14110 | * in case of error. |
| 14111 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14112 | static int |
| 14113 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14114 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14115 | if (ctxt == NULL) |
| 14116 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14117 | if (encoding != NULL) { |
| 14118 | if (ctxt->encoding != NULL) |
| 14119 | xmlFree((xmlChar *) ctxt->encoding); |
| 14120 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14121 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14122 | if (options & XML_PARSE_RECOVER) { |
| 14123 | ctxt->recovery = 1; |
| 14124 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14125 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14126 | } else |
| 14127 | ctxt->recovery = 0; |
| 14128 | if (options & XML_PARSE_DTDLOAD) { |
| 14129 | ctxt->loadsubset = XML_DETECT_IDS; |
| 14130 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14131 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14132 | } else |
| 14133 | ctxt->loadsubset = 0; |
| 14134 | if (options & XML_PARSE_DTDATTR) { |
| 14135 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 14136 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14137 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14138 | } |
| 14139 | if (options & XML_PARSE_NOENT) { |
| 14140 | ctxt->replaceEntities = 1; |
| 14141 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 14142 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14143 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14144 | } else |
| 14145 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14146 | if (options & XML_PARSE_PEDANTIC) { |
| 14147 | ctxt->pedantic = 1; |
| 14148 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14149 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14150 | } else |
| 14151 | ctxt->pedantic = 0; |
| 14152 | if (options & XML_PARSE_NOBLANKS) { |
| 14153 | ctxt->keepBlanks = 0; |
| 14154 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 14155 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14156 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14157 | } else |
| 14158 | ctxt->keepBlanks = 1; |
| 14159 | if (options & XML_PARSE_DTDVALID) { |
| 14160 | ctxt->validate = 1; |
| 14161 | if (options & XML_PARSE_NOWARNING) |
| 14162 | ctxt->vctxt.warning = NULL; |
| 14163 | if (options & XML_PARSE_NOERROR) |
| 14164 | ctxt->vctxt.error = NULL; |
| 14165 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14166 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14167 | } else |
| 14168 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 14169 | if (options & XML_PARSE_NOWARNING) { |
| 14170 | ctxt->sax->warning = NULL; |
| 14171 | options -= XML_PARSE_NOWARNING; |
| 14172 | } |
| 14173 | if (options & XML_PARSE_NOERROR) { |
| 14174 | ctxt->sax->error = NULL; |
| 14175 | ctxt->sax->fatalError = NULL; |
| 14176 | options -= XML_PARSE_NOERROR; |
| 14177 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14178 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14179 | if (options & XML_PARSE_SAX1) { |
| 14180 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 14181 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 14182 | ctxt->sax->startElementNs = NULL; |
| 14183 | ctxt->sax->endElementNs = NULL; |
| 14184 | ctxt->sax->initialized = 1; |
| 14185 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14186 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14187 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14188 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14189 | if (options & XML_PARSE_NODICT) { |
| 14190 | ctxt->dictNames = 0; |
| 14191 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14192 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14193 | } else { |
| 14194 | ctxt->dictNames = 1; |
| 14195 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14196 | if (options & XML_PARSE_NOCDATA) { |
| 14197 | ctxt->sax->cdataBlock = NULL; |
| 14198 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14199 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14200 | } |
| 14201 | if (options & XML_PARSE_NSCLEAN) { |
| 14202 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 14203 | options -= XML_PARSE_NSCLEAN; |
| 14204 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14205 | if (options & XML_PARSE_NONET) { |
| 14206 | ctxt->options |= XML_PARSE_NONET; |
| 14207 | options -= XML_PARSE_NONET; |
| 14208 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 14209 | if (options & XML_PARSE_COMPACT) { |
| 14210 | ctxt->options |= XML_PARSE_COMPACT; |
| 14211 | options -= XML_PARSE_COMPACT; |
| 14212 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 14213 | if (options & XML_PARSE_OLD10) { |
| 14214 | ctxt->options |= XML_PARSE_OLD10; |
| 14215 | options -= XML_PARSE_OLD10; |
| 14216 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 14217 | if (options & XML_PARSE_NOBASEFIX) { |
| 14218 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 14219 | options -= XML_PARSE_NOBASEFIX; |
| 14220 | } |
| 14221 | if (options & XML_PARSE_HUGE) { |
| 14222 | ctxt->options |= XML_PARSE_HUGE; |
| 14223 | options -= XML_PARSE_HUGE; |
| 14224 | } |
Rob Richards | b9ed017 | 2009-01-05 17:28:50 +0000 | [diff] [blame^] | 14225 | if (options & XML_PARSE_OLDSAX) { |
| 14226 | ctxt->options |= XML_PARSE_OLDSAX; |
| 14227 | options -= XML_PARSE_OLDSAX; |
| 14228 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 14229 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14230 | return (options); |
| 14231 | } |
| 14232 | |
| 14233 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14234 | * xmlCtxtUseOptions: |
| 14235 | * @ctxt: an XML parser context |
| 14236 | * @options: a combination of xmlParserOption |
| 14237 | * |
| 14238 | * Applies the options to the parser context |
| 14239 | * |
| 14240 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14241 | * in case of error. |
| 14242 | */ |
| 14243 | int |
| 14244 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 14245 | { |
| 14246 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 14247 | } |
| 14248 | |
| 14249 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14250 | * xmlDoRead: |
| 14251 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14252 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14253 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14254 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14255 | * @reuse: keep the context for reuse |
| 14256 | * |
| 14257 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14258 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14259 | * Returns the resulting document tree or NULL |
| 14260 | */ |
| 14261 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14262 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 14263 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14264 | { |
| 14265 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14266 | |
| 14267 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14268 | if (encoding != NULL) { |
| 14269 | xmlCharEncodingHandlerPtr hdlr; |
| 14270 | |
| 14271 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14272 | if (hdlr != NULL) |
| 14273 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14274 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14275 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 14276 | (ctxt->input->filename == NULL)) |
| 14277 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14278 | xmlParseDocument(ctxt); |
| 14279 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 14280 | ret = ctxt->myDoc; |
| 14281 | else { |
| 14282 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14283 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14284 | xmlFreeDoc(ctxt->myDoc); |
| 14285 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14286 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14287 | ctxt->myDoc = NULL; |
| 14288 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14289 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14290 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14291 | |
| 14292 | return (ret); |
| 14293 | } |
| 14294 | |
| 14295 | /** |
| 14296 | * xmlReadDoc: |
| 14297 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14298 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14299 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14300 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14301 | * |
| 14302 | * parse an XML in-memory document and build a tree. |
| 14303 | * |
| 14304 | * Returns the resulting document tree |
| 14305 | */ |
| 14306 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14307 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14308 | { |
| 14309 | xmlParserCtxtPtr ctxt; |
| 14310 | |
| 14311 | if (cur == NULL) |
| 14312 | return (NULL); |
| 14313 | |
| 14314 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14315 | if (ctxt == NULL) |
| 14316 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14317 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14318 | } |
| 14319 | |
| 14320 | /** |
| 14321 | * xmlReadFile: |
| 14322 | * @filename: a file or URL |
| 14323 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14324 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14325 | * |
| 14326 | * parse an XML file from the filesystem or the network. |
| 14327 | * |
| 14328 | * Returns the resulting document tree |
| 14329 | */ |
| 14330 | xmlDocPtr |
| 14331 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 14332 | { |
| 14333 | xmlParserCtxtPtr ctxt; |
| 14334 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14335 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14336 | if (ctxt == NULL) |
| 14337 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14338 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14339 | } |
| 14340 | |
| 14341 | /** |
| 14342 | * xmlReadMemory: |
| 14343 | * @buffer: a pointer to a char array |
| 14344 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14345 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14346 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14347 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14348 | * |
| 14349 | * parse an XML in-memory document and build a tree. |
| 14350 | * |
| 14351 | * Returns the resulting document tree |
| 14352 | */ |
| 14353 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14354 | 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] | 14355 | { |
| 14356 | xmlParserCtxtPtr ctxt; |
| 14357 | |
| 14358 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14359 | if (ctxt == NULL) |
| 14360 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14361 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14362 | } |
| 14363 | |
| 14364 | /** |
| 14365 | * xmlReadFd: |
| 14366 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14367 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14368 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14369 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14370 | * |
| 14371 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14372 | * NOTE that the file descriptor will not be closed when the |
| 14373 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14374 | * |
| 14375 | * Returns the resulting document tree |
| 14376 | */ |
| 14377 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14378 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14379 | { |
| 14380 | xmlParserCtxtPtr ctxt; |
| 14381 | xmlParserInputBufferPtr input; |
| 14382 | xmlParserInputPtr stream; |
| 14383 | |
| 14384 | if (fd < 0) |
| 14385 | return (NULL); |
| 14386 | |
| 14387 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14388 | if (input == NULL) |
| 14389 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14390 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14391 | ctxt = xmlNewParserCtxt(); |
| 14392 | if (ctxt == NULL) { |
| 14393 | xmlFreeParserInputBuffer(input); |
| 14394 | return (NULL); |
| 14395 | } |
| 14396 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14397 | if (stream == NULL) { |
| 14398 | xmlFreeParserInputBuffer(input); |
| 14399 | xmlFreeParserCtxt(ctxt); |
| 14400 | return (NULL); |
| 14401 | } |
| 14402 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14403 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14404 | } |
| 14405 | |
| 14406 | /** |
| 14407 | * xmlReadIO: |
| 14408 | * @ioread: an I/O read function |
| 14409 | * @ioclose: an I/O close function |
| 14410 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14411 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14412 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14413 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14414 | * |
| 14415 | * parse an XML document from I/O functions and source and build a tree. |
| 14416 | * |
| 14417 | * Returns the resulting document tree |
| 14418 | */ |
| 14419 | xmlDocPtr |
| 14420 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14421 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14422 | { |
| 14423 | xmlParserCtxtPtr ctxt; |
| 14424 | xmlParserInputBufferPtr input; |
| 14425 | xmlParserInputPtr stream; |
| 14426 | |
| 14427 | if (ioread == NULL) |
| 14428 | return (NULL); |
| 14429 | |
| 14430 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 14431 | XML_CHAR_ENCODING_NONE); |
| 14432 | if (input == NULL) |
| 14433 | return (NULL); |
| 14434 | ctxt = xmlNewParserCtxt(); |
| 14435 | if (ctxt == NULL) { |
| 14436 | xmlFreeParserInputBuffer(input); |
| 14437 | return (NULL); |
| 14438 | } |
| 14439 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14440 | if (stream == NULL) { |
| 14441 | xmlFreeParserInputBuffer(input); |
| 14442 | xmlFreeParserCtxt(ctxt); |
| 14443 | return (NULL); |
| 14444 | } |
| 14445 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14446 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14447 | } |
| 14448 | |
| 14449 | /** |
| 14450 | * xmlCtxtReadDoc: |
| 14451 | * @ctxt: an XML parser context |
| 14452 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14453 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14454 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14455 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14456 | * |
| 14457 | * parse an XML in-memory document and build a tree. |
| 14458 | * This reuses the existing @ctxt parser context |
| 14459 | * |
| 14460 | * Returns the resulting document tree |
| 14461 | */ |
| 14462 | xmlDocPtr |
| 14463 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14464 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14465 | { |
| 14466 | xmlParserInputPtr stream; |
| 14467 | |
| 14468 | if (cur == NULL) |
| 14469 | return (NULL); |
| 14470 | if (ctxt == NULL) |
| 14471 | return (NULL); |
| 14472 | |
| 14473 | xmlCtxtReset(ctxt); |
| 14474 | |
| 14475 | stream = xmlNewStringInputStream(ctxt, cur); |
| 14476 | if (stream == NULL) { |
| 14477 | return (NULL); |
| 14478 | } |
| 14479 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14480 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14481 | } |
| 14482 | |
| 14483 | /** |
| 14484 | * xmlCtxtReadFile: |
| 14485 | * @ctxt: an XML parser context |
| 14486 | * @filename: a file or URL |
| 14487 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14488 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14489 | * |
| 14490 | * parse an XML file from the filesystem or the network. |
| 14491 | * This reuses the existing @ctxt parser context |
| 14492 | * |
| 14493 | * Returns the resulting document tree |
| 14494 | */ |
| 14495 | xmlDocPtr |
| 14496 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 14497 | const char *encoding, int options) |
| 14498 | { |
| 14499 | xmlParserInputPtr stream; |
| 14500 | |
| 14501 | if (filename == NULL) |
| 14502 | return (NULL); |
| 14503 | if (ctxt == NULL) |
| 14504 | return (NULL); |
| 14505 | |
| 14506 | xmlCtxtReset(ctxt); |
| 14507 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 14508 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14509 | if (stream == NULL) { |
| 14510 | return (NULL); |
| 14511 | } |
| 14512 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14513 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14514 | } |
| 14515 | |
| 14516 | /** |
| 14517 | * xmlCtxtReadMemory: |
| 14518 | * @ctxt: an XML parser context |
| 14519 | * @buffer: a pointer to a char array |
| 14520 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14521 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14522 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14523 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14524 | * |
| 14525 | * parse an XML in-memory document and build a tree. |
| 14526 | * This reuses the existing @ctxt parser context |
| 14527 | * |
| 14528 | * Returns the resulting document tree |
| 14529 | */ |
| 14530 | xmlDocPtr |
| 14531 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14532 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14533 | { |
| 14534 | xmlParserInputBufferPtr input; |
| 14535 | xmlParserInputPtr stream; |
| 14536 | |
| 14537 | if (ctxt == NULL) |
| 14538 | return (NULL); |
| 14539 | if (buffer == NULL) |
| 14540 | return (NULL); |
| 14541 | |
| 14542 | xmlCtxtReset(ctxt); |
| 14543 | |
| 14544 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 14545 | if (input == NULL) { |
| 14546 | return(NULL); |
| 14547 | } |
| 14548 | |
| 14549 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14550 | if (stream == NULL) { |
| 14551 | xmlFreeParserInputBuffer(input); |
| 14552 | return(NULL); |
| 14553 | } |
| 14554 | |
| 14555 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14556 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14557 | } |
| 14558 | |
| 14559 | /** |
| 14560 | * xmlCtxtReadFd: |
| 14561 | * @ctxt: an XML parser context |
| 14562 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14563 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14564 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14565 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14566 | * |
| 14567 | * parse an XML from a file descriptor and build a tree. |
| 14568 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14569 | * NOTE that the file descriptor will not be closed when the |
| 14570 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14571 | * |
| 14572 | * Returns the resulting document tree |
| 14573 | */ |
| 14574 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14575 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 14576 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14577 | { |
| 14578 | xmlParserInputBufferPtr input; |
| 14579 | xmlParserInputPtr stream; |
| 14580 | |
| 14581 | if (fd < 0) |
| 14582 | return (NULL); |
| 14583 | if (ctxt == NULL) |
| 14584 | return (NULL); |
| 14585 | |
| 14586 | xmlCtxtReset(ctxt); |
| 14587 | |
| 14588 | |
| 14589 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14590 | if (input == NULL) |
| 14591 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14592 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14593 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14594 | if (stream == NULL) { |
| 14595 | xmlFreeParserInputBuffer(input); |
| 14596 | return (NULL); |
| 14597 | } |
| 14598 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14599 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14600 | } |
| 14601 | |
| 14602 | /** |
| 14603 | * xmlCtxtReadIO: |
| 14604 | * @ctxt: an XML parser context |
| 14605 | * @ioread: an I/O read function |
| 14606 | * @ioclose: an I/O close function |
| 14607 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14608 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14609 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14610 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14611 | * |
| 14612 | * parse an XML document from I/O functions and source and build a tree. |
| 14613 | * This reuses the existing @ctxt parser context |
| 14614 | * |
| 14615 | * Returns the resulting document tree |
| 14616 | */ |
| 14617 | xmlDocPtr |
| 14618 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 14619 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14620 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14621 | const char *encoding, int options) |
| 14622 | { |
| 14623 | xmlParserInputBufferPtr input; |
| 14624 | xmlParserInputPtr stream; |
| 14625 | |
| 14626 | if (ioread == NULL) |
| 14627 | return (NULL); |
| 14628 | if (ctxt == NULL) |
| 14629 | return (NULL); |
| 14630 | |
| 14631 | xmlCtxtReset(ctxt); |
| 14632 | |
| 14633 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 14634 | XML_CHAR_ENCODING_NONE); |
| 14635 | if (input == NULL) |
| 14636 | return (NULL); |
| 14637 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14638 | if (stream == NULL) { |
| 14639 | xmlFreeParserInputBuffer(input); |
| 14640 | return (NULL); |
| 14641 | } |
| 14642 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14643 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14644 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 14645 | |
| 14646 | #define bottom_parser |
| 14647 | #include "elfgcchack.h" |